I cannot understand the following output. I would expect Numpy to return -10
(or an approximation). Why is it a complex number?
我看不懂下面的输出。我期望Numpy返回-10(或近似)。为什么是复数?
print((-1000)**(1/3.))
Numpy answer
Numpy回答
(5+8.660254037844384j)
Numpy official tutorial says the answer is nan
. You can find it in the middle of this tutorial.
Numpy官方教程说答案是nan。您可以在本教程的中间找到它。
1 个解决方案
#1
5
You are exponentiating a regular Python scalar rather than a numpy array.
您是在取一个普通的Python标量,而不是一个numpy数组。
Try this:
试试这个:
import numpy as np
print(np.array(-1000) ** (1. / 3))
# nan
The difference is that numpy does not automatically promote the result to a complex type, whereas a Python 3 scalar gets promoted to a complex value (in Python 2.7 you would just get a ValueError
).
不同之处在于numpy不自动将结果提升为复杂类型,而Python 3标量提升为复杂值(在Python 2.7中,您只会得到ValueError)。
As explained in the link @jonrsharpe gave above, negative numbers have multiple cube roots. To get the root you are looking for, you could do something like this:
正如上面@jonrsharpe给出的链接所解释的,负数有多个立方根。要得到你想要的根,你可以这样做:
x = -1000
print(np.copysign(np.abs(x) ** (1. / 3), x))
# -10.0
Update 1
Mark Dickinson is absolutely right about the underlying cause of the problem - 1. / 3
is not exactly the same as a third because of rounding error, so x ** (1. / 3)
is not quite the same thing as the cube root of x
.
马克·迪金森对问题的根本原因是绝对正确的- 1。/ 3由于舍入误差不等于1 / 3,所以x * (1)/ 3)和√x不完全一样。
A better solution would be to use scipy.special.cbrt
, which computes the 'exact' cube root rather than x ** (1./3)
:
更好的解决办法是使用剪刀。cbrt计算“准确”的立方根,而不是x ** * (1./3):
from scipy.special import cbrt
print(cbrt(-1000))
# -10.0
Update 2
It's also worth noting that versions of numpy >= 0.10.0 will have a new np.cbrt
function based on the C99 cbrt
function.
同样值得注意的是,numpy >= 0.10.0的版本将有一个新的np。基于C99 cbrt函数的cbrt函数。
#1
5
You are exponentiating a regular Python scalar rather than a numpy array.
您是在取一个普通的Python标量,而不是一个numpy数组。
Try this:
试试这个:
import numpy as np
print(np.array(-1000) ** (1. / 3))
# nan
The difference is that numpy does not automatically promote the result to a complex type, whereas a Python 3 scalar gets promoted to a complex value (in Python 2.7 you would just get a ValueError
).
不同之处在于numpy不自动将结果提升为复杂类型,而Python 3标量提升为复杂值(在Python 2.7中,您只会得到ValueError)。
As explained in the link @jonrsharpe gave above, negative numbers have multiple cube roots. To get the root you are looking for, you could do something like this:
正如上面@jonrsharpe给出的链接所解释的,负数有多个立方根。要得到你想要的根,你可以这样做:
x = -1000
print(np.copysign(np.abs(x) ** (1. / 3), x))
# -10.0
Update 1
Mark Dickinson is absolutely right about the underlying cause of the problem - 1. / 3
is not exactly the same as a third because of rounding error, so x ** (1. / 3)
is not quite the same thing as the cube root of x
.
马克·迪金森对问题的根本原因是绝对正确的- 1。/ 3由于舍入误差不等于1 / 3,所以x * (1)/ 3)和√x不完全一样。
A better solution would be to use scipy.special.cbrt
, which computes the 'exact' cube root rather than x ** (1./3)
:
更好的解决办法是使用剪刀。cbrt计算“准确”的立方根,而不是x ** * (1./3):
from scipy.special import cbrt
print(cbrt(-1000))
# -10.0
Update 2
It's also worth noting that versions of numpy >= 0.10.0 will have a new np.cbrt
function based on the C99 cbrt
function.
同样值得注意的是,numpy >= 0.10.0的版本将有一个新的np。基于C99 cbrt函数的cbrt函数。