在 Python 中返回複數引數的虛部


要返回複數引數的虛部,請使用 numpy.imag() 方法。該方法返回複數引數的虛部。如果 val 為實數,則輸出使用 val 的型別。如果 val 包含複數元素,則返回型別為浮點數。第一個引數 val 是輸入陣列

步驟

首先,匯入所需的庫 -

import numpy as np

使用 array() 方法建立陣列 -

arr = np.array([36.+5.j , 27.+3.j , 68.+2.j , 23.+7.j])

顯示陣列 -

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...\n",arr.shape)

要在 Python 中返回複數引數的虛部,請使用 numpy.imag() 方法。該方法返回複數引數的虛部。如果 val 為實數,則輸出使用 val 的型別。如果 val 包含複數元素,則返回型別為浮點數。第一個引數 val 是輸入陣列 -

print("\nResult (Imaginary part)...\n",np.imag(arr))

示例

import numpy as np

# Create an array using the array() method
arr = np.array([36.+5.j , 27.+3.j , 68.+2.j , 23.+7.j])

# 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...\n",arr.shape)

# To return the imaginary part of the complex argument, use the numpy.imag() method in Python
print("\nResult (Imaginary part)...\n",np.imag(arr))

輸出

Our Array...
[36.+5.j 27.+3.j 68.+2.j 23.+7.j]

Dimensions of our Array...
1

Datatype of our Array object...
complex128

Shape of our Array...
(4,)

Result (Imaginary part)...
[5. 3. 2. 7.]

更新於: 2022年2月25日

165 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.