使用 Python 中的 emath 計算負數的平方根
要計算輸入的平方根,請在 Python Numpy 中使用 scimath.sqrt() 方法。該方法返回 x 的平方根。如果 x 是標量,則輸出也是標量,否則返回陣列。引數 x 是輸入值。對於負輸入元素,將返回複數值。
步驟
首先,匯入所需的庫 -
import numpy as np
使用 array() 方法建立 NumPy 陣列 -
arr = np.array([1, -4, -9, 16, -25, 36])
顯示陣列 -
print("Our Array...\n",arr)
檢查維度 -
print("\nDimensions of our Array...\n",arr.ndim)
獲取資料型別 -
print("\nDatatype of our Array object...\n",arr.dtype)
獲取形狀 -
print("\nShape of our Array object...\n",arr.shape)
要計算輸入的平方根,請在 Python Numpy 中使用 scimath.sqrt() 方法。該方法返回 x 的平方根。如果 x 是標量,則輸出也是標量,否則返回陣列 -
print("\nResult...\n",np.emath.sqrt(arr))
示例
import numpy as np # Creating a numpy array using the array() method arr = np.array([1, -4, -9, 16, -25, 36]) # Display the array print("Our Array...\n",arr) # Check the Dimensions print("\nDimensions of our Array...\n",arr.ndim) # Get the Datatype print("\nDatatype of our Array object...\n",arr.dtype) # Get the Shape print("\nShape of our Array object...\n",arr.shape) # To compute the square root of input, use the scimath.sqrt() method in Python Numpy print("\nResult...\n",np.emath.sqrt(arr))
輸出
Our Array... [ 1 -4 -9 16 -25 36] Dimensions of our Array... 1 Datatype of our Array object... int64 Shape of our Array object... (6,) Result... [1.+0.j 0.+2.j 0.+3.j 4.+0.j 0.+5.j 6.+0.j]
廣告