numpy.lib.scimath.sqrt#
- lib.scimath.sqrt(x)[源代码]#
计算 x 的平方根。
对于负输入元素,将返回一个复数值(与返回 NaN 的
numpy.sqrt不同)。- 参数:
- xarray_like
输入值。
- 返回:
- outndarray 或标量
x 的平方根。如果 x 是标量,则 out 也是标量,否则返回一个数组。
另请参阅
示例
对于实数、非负输入,此函数的功能与
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