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

python上负数的立方根

  •  15
  • Vladimir Prudnikov  · 技术社区  · 15 年前

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

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

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

    简单使用 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    15 年前
    math.pow(abs(x),float(1)/3) * (1,-1)[x<0]
    
        3
  •  11
  •   DrAl    15 年前

    您可以使用:

    -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
    
        4
  •  10
  •   user9876    15 年前

    将前面的答案做成一行:

    import math
    def cubic_root(x):
        return math.copysign(math.pow(abs(x), 1.0/3.0), x)
    
        5
  •  9
  •   tom10    15 年前

    您可以使用以下方法获得完整(全部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)]
    

    说明: U =exp(u*log(x))。然后,该解将是其中一个根,为了得到其他根,在复平面上通过a(完全旋转)/t旋转它。

        6
  •  3
  •   Kirk Broadhurst    15 年前

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

    i、 e.x^(1/3)代表x<0与(-1)*(| x |)^(1/3)相同

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

        7
  •  3
  •   Otto Allmendinger    15 年前

    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    10 年前

    你可以用 cbrt scipy.special

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

    这也适用于阵列。

        9
  •  1
  •   kidnakyo    9 年前

    这也适用于numpy阵列:

    cbrt = lambda n: n/abs(n)*abs(n)**(1./3)
    
        10
  •  1
  •   ijoseph    6 年前

    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.])
    
        11
  •  0
  •   Joachim Sauer    15 年前

    原始解决方案:

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

        12
  •  0
  •   Atlas7    9 年前

    我只是遇到了一个非常类似的问题,并从中找到了NumPy解决方案 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
    

    我希望这有帮助。

        13
  •  0
  •   Daniel    9 年前

    对于Python 3中的算术、计算器式答案:

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

    -3.0**(1./3) 在Python 2中。

    关于代数解 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]
    
        14
  •  0
  •   tdy TheChimp    3 年前

    Python3.11中的新特性

    math.cbrt 它无缝地处理负根:

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