numpy.lib.scimath.sqrt#
- lib.scimath.sqrt(x)[source]#
計算 x 的平方根。
對於負數輸入元素,會傳回複數值 (不同於
numpy.sqrt
,後者會傳回 NaN)。- 參數:
- 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