numpy.emath.sqrt#

emath.sqrt(x)[源代码]#

计算 x 的平方根。

对于负数输入元素,将返回一个复数值 (与返回 NaN 的 numpy.sqrt 不同)。

参数:
x类数组

输入值。

返回:
outndarray 或标量

x 的平方根。如果 x 是标量,out 也是标量,否则返回一个数组。

另请参见

numpy.sqrt

示例

对于实数、非负数输入,其行为与 numpy.sqrt 完全相同

>>> import numpy as np
>>> np.emath.sqrt(1)
1.0
>>> np.emath.sqrt([1, 4])
array([1.,  2.])

但它会自动处理负数输入

>>> np.emath.sqrt(-1)
1j
>>> np.emath.sqrt([-1,4])
array([0.+1.j, 2.+0.j])

预期结果不同,因为浮点数 0.0 和 -0.0 是不同的。

如需更多控制,可按如下方式显式使用 complex()

>>> np.emath.sqrt(complex(-4.0, 0.0))
2j
>>> np.emath.sqrt(complex(-4.0, -0.0))
-2j