当我取-1的平方根时,它会给我一个错误:
在sqrt中遇到无效值
我该怎么解决这个问题呢?
/////////
arr=sqrt(-1)
print(arr)
OUTPUT
发布于 2013-07-21 06:33:24
您需要使用cmath模块(标准库的一部分)中的sqrt
>>> import cmath
>>> cmath.sqrt(-1)
1j
发布于 2013-07-21 05:55:00
为了避免invalid value
警告/错误,numpy的sqrt
函数的参数必须是复杂的:
In [8]: import numpy as np
In [9]: np.sqrt(-1+0j)
Out[9]: 1j
正如@AshwiniChaudhary在评论中指出的那样,你也可以使用cmath
标准库:
In [10]: cmath.sqrt(-1)
Out[10]: 1j
发布于 2014-02-06 20:47:02
我刚刚发现了numpy.lib.scimath.sqrt
在sqrt documentation中解释的方便函数。我的用法如下:
>>> from numpy.lib.scimath import sqrt as csqrt
>>> csqrt(-1)
1j
https://stackoverflow.com/questions/17766774
复制相似问题