使用 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 提升輸入值的冪的結果,請使用 scimath.power() 方法 -

print("\nResult...\n",np.emath.power(arr, 2))

示例

import numpy as np

# Create a numpy array using the array() method
# The array elements also includes negative values
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.+0.j 16.-0.j 64.-0.j 256.+0.j 1024.-0.j]

更新於: 2022-03-01

92 次檢視

啟動你的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.