代码之家  ›  专栏  ›  技术社区  ›  Vladimir Prudnikov

python上负数的三次方根

  •  19
  • Vladimir Prudnikov  · 技术社区  · 17 年前

    有人能帮我找到一个如何使用python计算负数的三次方根的解决方案吗?

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

    它不起作用。负数的立方根是负数。有什么解决方案吗?

    14 回复  |  直到 15 年前
        1
  •  19
  •   Andrew Walker    17 年前

    简单使用 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) ]
    

    编辑:

    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 ]
    
        2
  •  12
  •   David    17 年前

    您可以使用:

    -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
  •  11
  •   DrAl    17 年前
    math.pow(abs(x),float(1)/3) * (1,-1)[x<0]
    
        4
  •  10
  •   user9876    17 年前

    您可以使用以下命令获得完整(所有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))。然后,该解将是其中一个根,要获得其他根,请在复平面中将其旋转a(全旋转)/t。

        5
  •  9
  •   tom10    17 年前

    把前面的答案写成一句话:

    import math
    def cubic_root(x):
        return math.copysign(math.pow(abs(x), 1.0/3.0), x)
    
        6
  •  3
  •   Kirk Broadhurst    17 年前

        7
  •  3
  •   Otto Allmendinger    17 年前

    你也可以把 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
    
        8
  •  1
  •   Matěj Å míd    12 年前

    numpy 有 an inbuilt cube root function cbrt

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

    1.10.0 2015-10-06 ).

    array / list

    >>> np.cbrt([-8, 27])
    array([-2.,  3.])
    
        9
  •  1
  •   kidnakyo    11 年前

    cbrt scipy.special :

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

    这也适用于数组。

        10
  •  1
  •   ijoseph    8 年前

    这也适用于numpy数组:

    cbrt = lambda n: n/abs(n)*abs(n)**(1./3)
    
        11
  •  0
  •   Joachim Sauer    17 年前

    math.cbrt 它无缝处理负根:

    >>> import math
    >>> math.cbrt(-3)
    -1.4422495703074083
    
        12
  •  0
  •   Atlas7    11 年前

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

    可能非常不符合Python,但它应该有效。

        13
  •  0
  •   Daniel    11 年前

    this forum post .

    在nushell中,我们可以使用NumPy sign 和 absolute 帮助我们的方法。下面是一个对我有用的例子:

    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
    

    我希望这能有所帮助。

        14
  •  0
  •   tdy TheChimp    5 年前

    >>> -3.0**(1/3)
    -1.4422495703074083
    

    或 -3.0**(1./3)

    对于代数解 x**3 + (0*x**2 + 0*x) + 3 = 0

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