在 NumPy 中計算複數的絕對值


要返回複數值的絕對值,請在 Python NumPy 中使用 **numpy.absolute()** 方法。輸出是儲存結果的位置。如果提供,它必須具有輸入廣播到的形狀。如果未提供或為 None,則返回一個新分配的陣列。元組(僅作為關鍵字引數可能)的長度必須等於輸出的數量。

條件在輸入上廣播。在條件為 True 的位置,out 陣列將設定為 ufunc 結果。在其他地方,out 陣列將保留其原始值。請注意,如果透過預設的 out=None 建立了一個未初始化的 out 陣列,則其中條件為 False 的位置將保持未初始化狀態。

NumPy 提供了全面的數學函式、隨機數生成器、線性代數例程、傅立葉變換等等。它支援廣泛的硬體和計算平臺,並且可以很好地與分散式、GPU 和稀疏陣列庫配合使用。

步驟

首先,匯入所需的庫 -

import numpy as np

使用 array() 方法建立一個具有複數型別的陣列 -

arr = np.array([56.+0.j, 27.+0.j, 68.+0.j, 49.+0.j, 120.+0.j,3 + 4.j])

顯示陣列 -

print("Array...
", arr)

獲取陣列的型別 -

print("
Our Array type...
", arr.dtype)

獲取陣列的維度 -

print("
Our Array Dimension...
",arr.ndim)

獲取陣列的形狀 -

print("
Our Array Shape...
",arr.shape)

要返回複數值的絕對值,請在 Python NumPy 中使用 numpy.absolute() 方法 -

print("
Result...
",np.absolute(arr))

示例

import numpy as np

# Create an array with complex type using the array() method
arr = np.array([56.+0.j, 27.+0.j, 68.+0.j, 49.+0.j, 120.+0.j,3 + 4.j])

# Display the array
print("Array...
", arr) # Get the type of the array print("
Our Array type...
", arr.dtype) # Get the dimensions of the Array print("
Our Array Dimension...
",arr.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr.shape) # To return the absolute value of complex values, use the numpy.absolute() method in Python Numpy print("
Result...
",np.absolute(arr))

輸出

Array...
[ 56.+0.j 27.+0.j 68.+0.j 49.+0.j 120.+0.j 3.+4.j]

Our Array type...
complex128

Our Array Dimension...
1

Our Array Shape...
(6,)

Result...
[ 56. 27. 68. 49. 120. 5.]

更新於: 2022年2月8日

1K+ 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.