python上负数的立方根

时间:2009-09-01 10:40:10

标签: python math

有人可以帮我找到如何使用python计算负数的立方根的解决方案吗?

>>> math.pow(-3, float(1)/3)
nan

它不起作用。负数的立方根是负数。任何解决方案?

14 个答案:

答案 0 :(得分:18)

De Moivre's formula的简单用法足以表明值的立方根,无论符号如何,都是多值函数。这意味着,对于任何输入值,将有三种解决方案。迄今为止提出的大多数解决方案仅返回原则根。返回所有有效根,并明确测试非复杂特殊情况的解决方案如下所示。

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) ]

编辑:根据要求,如果不适合依赖numpy,以下代码会做同样的事情。

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 ]

答案 1 :(得分:11)

math.pow(abs(x),float(1)/3) * (1,-1)[x<0]

答案 2 :(得分:10)

您可以使用:

-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

答案 3 :(得分:9)

将早期的答案变成单行代码:

import math
def cubic_root(x):
    return math.copysign(math.pow(abs(x), 1.0/3.0), x)

答案 4 :(得分:9)

您可以使用以下方式获得完整(所有n根)和更一般(任何标志,任何力量)解决方案:

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)]

说明: a使用等式x u = exp(u * log(x))。这个解决方案将成为其中一个根,并且为了获得其他解决方案,在复杂平面中通过(完全旋转)/ t旋转它。

答案 5 :(得分:3)

您还可以包装提供libm(立方根)功能的cbrt库:

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

答案 6 :(得分:3)

负数的立方根只是该数字绝对值的立方根的负数。

即。 x ^(1/3)表示x <1。 0与(-1)*(| x |)^(1/3)

相同

只需将您的数字设为正数,然后执行立方根。

答案 7 :(得分:1)

您可以使用cbrt中的scipy.special

>>> from scipy.special import cbrt
>>> cbrt(-3)
-1.4422495703074083

这也适用于数组。

答案 8 :(得分:1)

这也适用于numpy数组:

cbrt = lambda n: n/abs(n)*abs(n)**(1./3)

答案 9 :(得分:0)

原始解决方案:

def cubic_root(nr):
   if nr<0:
     return -math.pow(-nr, float(1)/3)
   else:
     return math.pow(nr, float(1)/3)

可能大规模非pythonic,但它应该工作。

答案 10 :(得分:0)

我遇到了一个非常类似的问题,并从this forum post找到了NumPy解决方案。

在nushell中,我们可以使用NumPy signabsolute方法来帮助我们。这是一个对我有用的例子:

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

我希望这会有所帮助。

答案 11 :(得分:0)

对于Python 3中的算术,类似计算器的答案:

>>> -3.0**(1/3)
-1.4422495703074083
Python 2中的

-3.0**(1./3)

对于x**3 + (0*x**2 + 0*x) + 3 = 0的代数解决方案,使用numpy:

>>> p = [1,0,0,3]
>>> numpy.roots(p)
[-3.0+0.j          1.5+2.59807621j  1.5-2.59807621j]

答案 12 :(得分:0)

numpyan inbuilt cube root function cbrt可以处理负数:

>>> import numpy as np
>>> np.cbrt(-8)
-2.0

这是在版本1.10.0中添加的(已发布2015-10-06)。

也适用于numpy array / list输入:

>>> np.cbrt([-8, 27])
array([-2.,  3.])

答案 13 :(得分:0)

Python 3.11 中的新功能

现在有 math.cbrt 可以无缝处理负根:

>>> import math
>>> math.cbrt(-3)
-1.4422495703074083