使用Python中的scimath返回輸入值負次冪的結果


要返回使用scimath計算輸入值次冪的結果,請使用Python中的scimath.power()方法。返回x的p次冪的結果,即x**p的結果。如果x和p是標量,則輸出也是標量,否則返回陣列。如果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...
",arr)

檢查維度:

print("
Dimensions of our Array...
",arr.ndim)

獲取資料型別:

print("
Datatype of our Array object...
",arr.dtype)

獲取形狀:

print("
Shape of our Array object...
",arr.shape)

要返回使用scimath計算輸入值次冪的結果,請使用Python中的scimath.power()方法:

print("
Result...
",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...
",arr) # Check the Dimensions print("
Dimensions of our Array...
",arr.ndim) # Get the Datatype print("
Datatype of our Array object...
",arr.dtype) # Get the Shape print("
Shape of our Array object...
",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("
Result...
",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...
[0.25 0.0625 0.015625 0.00390625 0.00097656]

更新於:2022年3月1日

瀏覽量:179

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.