Guest User

Untitled

a guest
Jun 18th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. >>> math.pow(-3, float(1)/3)
  2. nan
  3.  
  4. -math.pow(3, float(1)/3)
  5.  
  6. if x > 0:
  7. return math.pow(x, float(1)/3)
  8. elif x < 0:
  9. return -math.pow(abs(x), float(1)/3)
  10. else:
  11. return 0
  12.  
  13. import numpy
  14. import math
  15. def cuberoot( z ):
  16. z = complex(z)
  17. x = z.real
  18. y = z.imag
  19. mag = abs(z)
  20. arg = math.atan2(y,x)
  21. return [ mag**(1./3) * numpy.exp( 1j*(arg+2*n*math.pi)/3 ) for n in range(1,4) ]
  22.  
  23. def cuberoot( z ):
  24. z = complex(z)
  25. x = z.real
  26. y = z.imag
  27. mag = abs(z)
  28. arg = math.atan2(y,x)
  29. resMag = mag**(1./3)
  30. resArg = [ (arg+2*math.pi*n)/3. for n in range(1,4) ]
  31. return [ resMag*(math.cos(a) + math.sin(a)*1j) for a in resArg ]
  32.  
  33. math.pow(abs(x),float(1)/3) * (1,-1)[x<0]
  34.  
  35. import cmath
  36.  
  37. x, t = -3., 3 # x**(1/t)
  38.  
  39. a = cmath.exp((1./t)*cmath.log(x))
  40. p = cmath.exp(1j*2*cmath.pi*(1./t))
  41.  
  42. r = [a*(p**i) for i in range(t)]
  43.  
  44. import math
  45. def cubic_root(x):
  46. return math.copysign(math.pow(abs(x), 1.0/3.0), x)
  47.  
  48. from ctypes import *
  49. libm = cdll.LoadLibrary('libm.so.6')
  50. libm.cbrt.restype = c_double
  51. libm.cbrt.argtypes = [c_double]
  52. libm.cbrt(-8.0)
  53.  
  54. -2.0
  55.  
  56. >>> from scipy.special import cbrt
  57. >>> cbrt(-3)
  58. -1.4422495703074083
  59.  
  60. cbrt = lambda n: n/abs(n)*abs(n)**(1./3)
  61.  
  62. def cubic_root(nr):
  63. if nr<0:
  64. return -math.pow(-nr, float(1)/3)
  65. else:
  66. return math.pow(nr, float(1)/3)
  67.  
  68. import numpy as np
  69.  
  70. x = np.array([-81,25])
  71. print x
  72. #>>> [-81 25]
  73.  
  74. xRoot5 = np.sign(x) * np.absolute(x)**(1.0/5.0)
  75. print xRoot5
  76. #>>> [-2.40822469 1.90365394]
  77.  
  78. print xRoot5**5
  79. #>>> [-81. 25.]
  80.  
  81. import numpy as np
  82.  
  83. y = -3.
  84. np.sign(y) * np.absolute(y)**(1./3.)
  85. #>>> -1.4422495703074083
  86.  
  87. >>> -3.0**(1/3)
  88. -1.4422495703074083
  89.  
  90. >>> p = [1,0,0,3]
  91. >>> numpy.roots(p)
  92. [-3.0+0.j 1.5+2.59807621j 1.5-2.59807621j]
  93.  
  94. >>> import numpy as np
  95. >>> np.cbrt(-8)
  96. -2.0
  97.  
  98. >>> np.cbrt([-8, 27])
  99. array([-2., 3.])
Add Comment
Please, Sign In to add comment