[英]Why does exponentiating a numpy.float64 return nan?
When I am raising a negative numpy.float64
to an exponent, I am receiving an nan
.当我将负数
numpy.float64
提高到指数时,我收到了nan
。 Why is the complex math not supported?为什么不支持复杂的数学? Is the only workaround a cast to
float
?唯一的解决方法是强制转换
float
吗?
>>> from numpy import float64, power
>>> r = float64(-12025.433836763057)
>>> p = 0.74
>>> r**p
nan
>>> power(r, p)
__main__:1: RuntimeWarning: invalid value encountered in power
nan
>>> float(r)**p
(-715.6124638577838+762.049873596874j)
>>>
A suggested duplicate has a similar question, with an answer stating that this is a bug in numpy
.建议的副本有一个类似的问题,答案指出这是
numpy
的错误。 Is this the end of the road?这是路的尽头吗?
The numpy.float64
is probably represented under the hood by a C language data structure which is strongly typed. numpy.float64
可能在numpy.float64
由强类型的 C 语言数据结构表示。 The python float
is pythonic, and so plays nicely with the complex number handling that python provides. python
float
是 pythonic,因此可以很好地与 python 提供的复数处理配合使用。
See: https://docs.scipy.org/doc/numpy/reference/c-api.dtype.html请参阅: https : //docs.scipy.org/doc/numpy/reference/c-api.dtype.html
Behind the scenes, python is casting that float(r)**p to return a 'complex' type.在幕后,python 正在转换 float(r)**p 以返回“复杂”类型。
The numpy power function is intended to work with numpy array_like structures where all the items are the same size and stored in a contiguous block of memory and its return type is inferred from its arguments. numpy power 函数旨在与 numpy array_like 结构一起使用,其中所有项目的大小都相同并存储在连续的内存块中,并且其返回类型是从其参数中推断出来的。
If you are expecting complex numbers, the best approach would be to use the complex64 or complex128 types.如果您需要复数,最好的方法是使用 complex64 或 complex128 类型。 These require more memory because each complex type consists of a real and imaginary component.
这些需要更多内存,因为每个复杂类型都由实部和虚部组成。 So complex64 would consist of two float32 numbers and complex128 would consist of two float64 numbers.
所以 complex64 将由两个 float32 数字组成, complex128 将由两个 float64 数字组成。
>>> import numpy as np
>>> r = np.complex128(-12025.433836763057)
>>> p = 0.74
>>> np.power(r, p)
(-715.6124638577835+762.0498735968736j)
You can also cast directly in the power function:您也可以直接在 power 函数中进行转换:
>>> import numpy as np
>>> r = np.float64(-12025.433836763057)
>>> p = 0.74
>>> np.power(r.astype(np.complex128), p)
(-715.6124638577835+762.0498735968736j)
But the simplest approach might be to just alter the power function's return type to expect a complex number:但最简单的方法可能是改变幂函数的返回类型以期望一个复数:
>>> import numpy as np
>>> r = np.float64(-12025.433836763057)
>>> p = 0.74
>>> np.power(r, p, dtype=np.complex128)
(-715.6124638577835+762.0498735968736j)
What's interesting is that numpy does normally allow casting types from float64 to complex as long as they keep the same level of precision.有趣的是 numpy 通常允许将类型从 float64 转换为 complex,只要它们保持相同的精度级别。 However it doesn't seem to allow implicit casting of any ufunc function return types even if the casting='same_kind' kwarg is overridden.
然而,即使 cast='same_kind' kwarg 被覆盖,它似乎也不允许隐式转换任何 ufunc 函数返回类型。
>>> np.can_cast(np.float64, complex)
True
>>> np.can_cast(np.float64, np.complex64)
False
>>> np.can_cast(np.float64, np.complex128)
True
According to the docs, if scalar arguments are passed to the ufunc (as opposed to arrays) it uses the logic in np.result_type and np.promote_types to determine the ufunc's return type.根据文档,如果将标量参数传递给 ufunc(而不是数组),它将使用 np.result_type 和 np.promote_types 中的逻辑来确定 ufunc 的返回类型。
https://docs.scipy.org/doc/numpy/reference/ufuncs.html https://docs.scipy.org/doc/numpy/reference/ufuncs.html
https://docs.scipy.org/doc/numpy/reference/generated/numpy.result_type.html#numpy.result_type https://docs.scipy.org/doc/numpy/reference/generated/numpy.result_type.html#numpy.result_type
>>> np.result_type(r, p)
dtype('float64')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.