更改Python中複數引數的虛部


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

步驟

首先,匯入所需的庫:

import numpy as np

使用array()方法建立一個數組:

arr = np.array([36.+1.j , 27.+2.j , 68.+3.j , 23.+2.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("\nImaginary part...\n",np.imag(arr))

更改虛部:

arr.imag = 5
print("\nUpdated result...\n",arr)

示例

import numpy as np

# Create an array using the array() method
arr = np.array([36.+1.j , 27.+2.j , 68.+3.j , 23.+2.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("\nImaginary part...\n",np.imag(arr))

# Change the imaginary part
arr.imag = 5
print("\nUpdated result...\n",arr)

輸出

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

Dimensions of our Array...
1

Datatype of our Array object...
complex128

Shape of our Array...
(4,)

Imaginary part...
[1. 2. 3. 2.]

Updated result...
[36.+5.j 27.+5.j 68.+5.j 23.+5.j]

更新於:2022年2月28日

1K+ 次瀏覽

啟動你的職業生涯

完成課程獲得認證

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