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