Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >>> math.pow(-3, float(1)/3)
- nan
- -math.pow(3, float(1)/3)
- if x > 0:
- return math.pow(x, float(1)/3)
- elif x < 0:
- return -math.pow(abs(x), float(1)/3)
- else:
- return 0
- import numpy
- import math
- def cuberoot( z ):
- z = complex(z)
- x = z.real
- y = z.imag
- mag = abs(z)
- arg = math.atan2(y,x)
- return [ mag**(1./3) * numpy.exp( 1j*(arg+2*n*math.pi)/3 ) for n in range(1,4) ]
- def cuberoot( z ):
- z = complex(z)
- x = z.real
- y = z.imag
- mag = abs(z)
- arg = math.atan2(y,x)
- resMag = mag**(1./3)
- resArg = [ (arg+2*math.pi*n)/3. for n in range(1,4) ]
- return [ resMag*(math.cos(a) + math.sin(a)*1j) for a in resArg ]
- math.pow(abs(x),float(1)/3) * (1,-1)[x<0]
- import cmath
- x, t = -3., 3 # x**(1/t)
- a = cmath.exp((1./t)*cmath.log(x))
- p = cmath.exp(1j*2*cmath.pi*(1./t))
- r = [a*(p**i) for i in range(t)]
- import math
- def cubic_root(x):
- return math.copysign(math.pow(abs(x), 1.0/3.0), x)
- from ctypes import *
- libm = cdll.LoadLibrary('libm.so.6')
- libm.cbrt.restype = c_double
- libm.cbrt.argtypes = [c_double]
- libm.cbrt(-8.0)
- -2.0
- >>> from scipy.special import cbrt
- >>> cbrt(-3)
- -1.4422495703074083
- cbrt = lambda n: n/abs(n)*abs(n)**(1./3)
- def cubic_root(nr):
- if nr<0:
- return -math.pow(-nr, float(1)/3)
- else:
- return math.pow(nr, float(1)/3)
- import numpy as np
- x = np.array([-81,25])
- print x
- #>>> [-81 25]
- xRoot5 = np.sign(x) * np.absolute(x)**(1.0/5.0)
- print xRoot5
- #>>> [-2.40822469 1.90365394]
- print xRoot5**5
- #>>> [-81. 25.]
- import numpy as np
- y = -3.
- np.sign(y) * np.absolute(y)**(1./3.)
- #>>> -1.4422495703074083
- >>> -3.0**(1/3)
- -1.4422495703074083
- >>> p = [1,0,0,3]
- >>> numpy.roots(p)
- [-3.0+0.j 1.5+2.59807621j 1.5-2.59807621j]
- >>> import numpy as np
- >>> np.cbrt(-8)
- -2.0
- >>> np.cbrt([-8, 27])
- array([-2., 3.])
Add Comment
Please, Sign In to add comment