python python上负数的三次方根

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1361740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:03:36  来源:igfitidea点击:

Cubic root of the negative number on python

pythonmath

提问by Vladimir Prudnikov

Can someone help me to find a solution on how to calculate a cubic root of the negative number using python?

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

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

it does not work. Cubic root of the negative number is negative number. Any solutions?

这是行不通的。负数的三次方根是负数。任何解决方案?

采纳答案by DrAl

You could use:

你可以使用:

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

Or more generally:

或更一般地:

if x > 0:
    return math.pow(x, float(1)/3)
elif x < 0:
    return -math.pow(abs(x), float(1)/3)
else:
    return 0

回答by Andrew Walker

A simple use of De Moivre's formula, is sufficient to show that the cube root of a value, regardless of sign, is a multi-valued function. That means, for any input value, there will be three solutions. Most of the solutions presented to far only return the principle root. A solution that returns all valid roots, and explicitly tests for non-complex special cases, is shown below.

De Moivre 公式的简单使用,足以表明一个值的立方根,无论符号如何,都是一个多值函数。这意味着,对于任何输入值,都会有三种解决方案。提出的大多数解决方案只返回主根。返回所有有效根并显式测试非复杂特殊情况的解决方案如下所示。

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

Edit:As requested, in cases where it is inappropriate to have dependency on numpy, the following code does the same thing.

编辑:根据要求,在不适合依赖 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 ]

回答by David

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

回答by user9876

Taking the earlier answers and making it into a one-liner:

将较早的答案制成单行:

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

回答by tom10

You can get the complete (all n roots) and more general (any sign, any power) solution using:

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

Explanation: a is using the equation xu= exp(u*log(x)). This solution will then be one of the roots, and to get the others, rotate it in the complex plane by a (full rotation)/t.

解释:a 使用方程 x u= exp(u*log(x))。该解将成为其中一个根,为了得到其他根,将它在复平面中旋转(完整旋转)/ t。

回答by Otto Allmendinger

You can also wrap the libmlibrary that offers a cbrt(cube root) function:

您还可以包装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)

gives the expected

给出预期

-2.0

回答by Kirk Broadhurst

The cubic root of a negative number is just the negative of the cubic root of the absolute value of that number.

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

i.e. x^(1/3) for x < 0 is the same as (-1)*(|x|)^(1/3)

即 x^(1/3) for x < 0 与 (-1)*(|x|)^(1/3) 相同

Just make your number positive, and then perform cubic root.

只需使您的数字为正,然后执行三次方根。

回答by Matěj ?míd

You can use cbrtfrom scipy.special:

您可以使用cbrtscipy.special

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

This also works for arrays.

这也适用于数组。

回答by kidnakyo

this works with numpy array as well:

这也适用于 numpy 数组:

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

回答by ijoseph

numpyhas an inbuilt cube root function cbrtthat handles negative numbers fine:

numpy一个内置的立方根函数cbrt,可以很好地处理负数:

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

This was added in version 1.10.0(released 2015-10-06).

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

Also works for numpyarray/ listinputs:

也适用于numpyarray/list输入:

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