使用 Python 中的 scimath 返回輸入值冪的結果
要使用 scimath 返回輸入值冪的結果,請在 Python 中使用 scimath.power() 方法。返回 x 的 p 次冪,即 x**p 的結果。如果 x 和 p 是標量,則 out 也是標量,否則返回陣列。如果 x 包含負值,則輸出將轉換為複數域。引數 x 是輸入值。
引數 p 是 x 提升到的冪。如果 x 包含多個值,則 p 必須是標量,或者包含與 x 相同數量的值。在後一種情況下,結果為 x[0]**p[0]、x[1]**p[1] 等。
步驟
首先,匯入所需的庫 -
import numpy as np
使用 array() 方法建立 NumPy 陣列 -
arr = np.array([2, 4, 8, 16, 32])
顯示陣列 -
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)
要使用 scimath 返回輸入值冪的結果,請在 Python 中使用 scimath.power() 方法 -
print("\nResult...\n",np.emath.power(arr, 2))
示例
import numpy as np # Creating a numpy array using the array() method arr = np.array([2, 4, 8, 16, 32]) # 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 return the result of the power to which the input value is raised with scimath, use the scimath.power() method in Python print("\nResult...\n",np.emath.power(arr, 2))
輸出
Our Array... [ 2 4 8 16 32] Dimensions of our Array... 1 Datatype of our Array object... int64 Shape of our Array object... (5,) Result... [ 4 16 64 256 1024]
廣告