Guest User

Untitled

a guest
Apr 23rd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. import numpy as np
  2.  
  3. def cart2pol(x, y):
  4. rho = np.sqrt(x**2 + y**2)
  5. phi = np.arctan2(y, x)
  6. return(rho, phi)
  7.  
  8. def pol2cart(rho, phi):
  9. x = rho * np.cos(phi)
  10. y = rho * np.sin(phi)
  11. return(x, y)
  12.  
  13. from numpy import exp, abs, angle
  14.  
  15. def polar2z(r,theta):
  16. return r * exp( 1j * theta )
  17.  
  18. def z2polar(z):
  19. return ( abs(z), angle(z) )
  20.  
  21. polar2z = lambda r,θ: r * exp( 1j * θ )
  22. z2polar = lambda z: ( abs(z), angle(z) )
  23.  
  24. rS, thetaS = z2polar( [z1,z2,z3] )
  25. zS = polar2z( rS, thetaS )
  26.  
  27. import math
  28.  
  29. def rect(r, theta):
  30. """theta in degrees
  31.  
  32. returns tuple; (float, float); (x,y)
  33. """
  34. x = r * math.cos(math.radians(theta))
  35. y = r * math.sin(math.radians(theta))
  36. return x,y
  37.  
  38. def polar(x, y):
  39. """returns r, theta(degrees)
  40. """
  41. r = (x ** 2 + y ** 2) ** .5
  42. if y == 0:
  43. theta = 180 if x < 0 else 0
  44. elif x == 0:
  45. theta = 90 if y > 0 else 270
  46. else:
  47. theta = math.degrees(math.atan(float(y) / x))
  48. return r, theta
  49.  
  50. class Point(object):
  51. def __init__(self, x=None, y=None, r=None, theta=None):
  52. """x and y or r and theta(degrees)
  53. """
  54. if x and y:
  55. self.c_polar(x, y)
  56. elif r and theta:
  57. self.c_rect(r, theta)
  58. else:
  59. raise ValueError('Must specify x and y or r and theta')
  60. def c_polar(self, x, y, f = polar):
  61. self._x = x
  62. self._y = y
  63. self._r, self._theta = f(self._x, self._y)
  64. self._theta_radians = math.radians(self._theta)
  65. def c_rect(self, r, theta, f = rect):
  66. """theta in degrees
  67. """
  68. self._r = r
  69. self._theta = theta
  70. self._theta_radians = math.radians(theta)
  71. self._x, self._y = f(self._r, self._theta)
  72. def setx(self, x):
  73. self.c_polar(x, self._y)
  74. def getx(self):
  75. return self._x
  76. x = property(fget = getx, fset = setx)
  77. def sety(self, y):
  78. self.c_polar(self._x, y)
  79. def gety(self):
  80. return self._y
  81. y = property(fget = gety, fset = sety)
  82. def setxy(self, x, y):
  83. self.c_polar(x, y)
  84. def getxy(self):
  85. return self._x, self._y
  86. xy = property(fget = getxy, fset = setxy)
  87. def setr(self, r):
  88. self.c_rect(r, self._theta)
  89. def getr(self):
  90. return self._r
  91. r = property(fget = getr, fset = setr)
  92. def settheta(self, theta):
  93. """theta in degrees
  94. """
  95. self.c_rect(self._r, theta)
  96. def gettheta(self):
  97. return self._theta
  98. theta = property(fget = gettheta, fset = settheta)
  99. def set_r_theta(self, r, theta):
  100. """theta in degrees
  101. """
  102. self.c_rect(r, theta)
  103. def get_r_theta(self):
  104. return self._r, self._theta
  105. r_theta = property(fget = get_r_theta, fset = set_r_theta)
  106. def __str__(self):
  107. return '({},{})'.format(self._x, self._y)
  108.  
  109. def polar(x,y):
  110. `returns r, theta(degrees)`
  111. return math.hypot(x,y),math.degrees(math.atan2(y,x))
  112.  
  113. import cmath
  114. input_num = complex(input().strip())
  115. r, phi = cmath.polar(input_num)
  116.  
  117. class Point(object)
  118. def setCartesian(self, x, y)
  119. def setPolar(self, rho, theta)
  120. def getX(self)
  121. def getY(self)
  122. def getRho(self)
  123. def setTheta(self)
Add Comment
Please, Sign In to add comment