numpy.lib.scimath.sqrt#

lib.scimath.sqrt(x)[source]#

计算 x 的平方根。

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

参数:
xarray_like

输入值。

返回:
outndarray 或 scalar

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