提取NumPy複數陣列的實部和虛部


在Python中,我們可以分別使用陣列的realimag屬性來提取NumPy複數陣列的實部和虛部。NumPy是一個用於複雜計算的Python庫,也支援複數。在本文中,我們將瞭解如何分別提取複數的實部和虛部。

理解NumPy中的複數

在NumPy中,我們使用complex資料型別將複數表示為實部和虛部的組合。我們可以使用complex()函式在NumPy中建立複數。

示例

在下面的示例中,我們透過兩種方式建立複數——使用complex函式和使用array() NumPy函式。complex函式建立一個單個複數,而array()函式建立一個複數列表。

import numpy as np

# create a complex number
z = complex(3, 4)
print(z) 

# create an array of complex numbers
a = np.array([complex(2, 3), complex(4, 5), complex(6, 7)])
print(a) 

輸出

(3+4j)
[2.+3.j 4.+5.j 6.+7.j]

提取複數的實部和虛部

為了提取複數的實部和虛部,我們將在下面的示例中使用NumPy的real和imag屬性、newaxis()方法以及numpy.real()和numpy.imag()函式。

方法1:使用NumPy的real和imag屬性

NumPy陣列的realimag屬性分別用於提取陣列的實部和虛部。

演算法

以下是使用real和imag屬性提取NumPy複數陣列的實部和虛部的演算法:

  • 定義一個NumPy複數陣列。

  • 使用real屬性提取陣列中複數的實部。

  • 使用imag屬性提取陣列中複數的虛部。

  • 根據需要分別處理複數的實部和虛部。

  • 根據需要組合處理後的實部和虛部。

語法

# extracting the real and imaginary parts of an array of complex numbers
real_part = my_complex_array.real
imag_part = my_complex_array.imag

這裡,my_complex_array是一個NumPy複數陣列,real_partimag_part分別是包含my_complex_array中複數的實部和虛部的陣列。

示例

在下面的示例中,我們使用NumPy的array()函式建立一個複數列表,然後分別使用real和imag屬性提取每個複數的實部和虛部。然後,使用print()函式列印生成的實部和虛部。

import numpy as np
# create an array of complex numbers
a = np.array([complex(2, 3), complex(4, 5), complex(6, 7)])

# extract the real and imaginary parts
real_part = a.real
imag_part = a.imag

print("Original Array: ", a)
print("Real Part: ", real_part)
print("Imaginary Part: ", imag_part)

輸出

Original Array:  [2.+3.j 4.+5.j 6.+7.j]
Real Part:  [2. 4. 6.]
Imaginary Part:  [3. 5. 7.]

方法2:使用newaxis()方法

newaxis()方法允許我們向NumPy陣列新增一個額外維度。當應用於複數陣列時,我們可以使用它將實部和虛部分離到不同的維度。

演算法

  • 定義一個NumPy複數陣列。

  • 使用newaxis()向陣列新增一個額外維度。

  • 使用real屬性提取陣列中複數的實部。

  • 使用imag屬性提取陣列中複數的虛部。

  • 根據需要分別處理複數的實部和虛部。

  • 根據需要組合處理後的實部和虛部。

語法

import numpy as np

# assume my_array is a NumPy array of any shape
my_array_with_new_dim = my_array[:, np.newaxis]

這裡,newaxis()方法用於向原始my_array新增一個額外維度。冒號(:)指定應保留原始陣列中的所有行,而np.newaxis用於在陣列末尾新增一個新維度。生成的my_array_with_new_dim將比my_array多一個維度。

示例

在下面的示例中,我們首先建立一個複數陣列a。然後,我們使用newaxis()向陣列新增一個額外維度,然後我們可以使用它將實部和虛部分離到不同的維度。然後,使用print()函式列印生成的實部和虛部。

import numpy as np

# create an array of complex numbers
a = np.array([2 + 3j, 4 + 5j, 6 + 7j])

# separate the real and imaginary parts
real_part = a[:, np.newaxis].real
imag_part = a[:, np.newaxis].imag

# print the results
print("Original Array: ", a)
print("Real Part: ", real_part)
print("Imaginary Part: ", imag_part)

輸出

Original Array:  [2.+3.j 4.+5.j 6.+7.j]
Real Part:  [[2.]
 [4.]
 [6.]]
Imaginary Part:  [[3.]
 [5.]
 [7.]]

方法3:使用numpy.real()和numpy.imag()函式

numpy.real()和numpy.imag()函式也可以用於提取複數陣列的實部和虛部。這些函式以NumPy複數陣列作為輸入,並分別返回包含實部和虛部的單獨陣列。

演算法

  • 定義一個NumPy複數陣列。

  • 使用numpy.real()函式提取陣列中複數的實部。

  • 使用numpy.imag()函式提取陣列中複數的虛部。

  • 根據需要分別處理複數的實部和虛部。

  • 根據需要組合處理後的實部和虛部。

import numpy as np

# assume my_complex_array is a NumPy array of complex numbers
real_part = np.real(my_complex_array)
imag_part = np.imag(my_complex_array)

這裡,numpy.real()函式用於提取my_complex_array中複數的實部,numpy.imag()函式用於提取my_complex_array中複數的虛部。生成的real_part和imag_part陣列將分別只包含原始複數的實部和虛部。

示例

在下面的示例中,我們建立一個複數陣列a,並使用numpy.real()和numpy.imag()函式分別提取實部和虛部。然後,使用print()函式列印生成的實部和虛部。

import numpy as np

# create an array of complex numbers
a = np.array([2 + 3j, 4 + 5j, 6 + 7j])

# separate the real and imaginary parts
real_part = np.real(a)
imag_part = np.imag(a)

# print the results
print("Original Array: ", a)
print("Real Part: ", real_part)
print("Imaginary Part: ", imag_part)

輸出

Original Array:  [2.+3.j 4.+5.j 6.+7.j]
Real Part:  [2. 4. 6.]
Imaginary Part:  [3. 5. 7.]

結論

在本文中,我們討論瞭如何分別使用realimag屬性提取NumPy複數陣列的實部和虛部。透過使用這些屬性,我們可以分別處理複數的實部和虛部,這在各種科學和工程應用中非常有用。

更新於:2023年7月10日

4K+ 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告