Extracting the real and imaginary parts of a NumPy array of complex numbers


In Python, we can extract the real and imaginary parts of a NumPy array of the complex number using the real and imag attributes of the array, respectively. Numpy is a Python library that is used for complex calculations and also provides support for complex numbers. In this article, we will understand how we can extract the real and imaginary parts separately of a complex number.

Understanding Complex Number in Numpy

In Numpy we represent a complex number as a combination of the real and imaginary parts using the complex data type. We can create a complex number in NumPy using the complex() function.

Example

In the below example, we create a complex number in two ways- using a complex function and by using the array() numpy function. The complex function creates a single complex number whereas the array() function creates a list of the complex number.

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) 

Output

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

Extracting Real and imaginary parts of Complex number

To extract the real and imaginary parts of a complex number we will be using the real and imag attributes of numpy, newaxis() method, and numpy.real() and numpy.imag() function in the below examples.

Method 1: Using real and imag attributes of numpy

The real and imag attributes of the numpy array are used to extract the real and imaginary parts of the array respectively.

Algorithm

Here is an algorithm for extracting the real and imaginary parts of a NumPy array of complex numbers using the real and imag attributes −

  • Define a NumPy array of complex numbers.

  • Use the real attribute to extract the real parts of the complex numbers in the array.

  • Use the imag attribute to extract the imaginary parts of the complex numbers in the array.

  • Manipulate the real and imaginary parts of the complex numbers separately as needed.

  • Combine the manipulated real and imaginary parts as necessary.

Syntax

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

Here, my_complex_array is a NumPy array of complex numbers, and real_part and imag_part are arrays containing the real and imaginary parts of the complex numbers in my_complex_array, respectively.

Example

In the below example, we create a list of a complex number using the array() function of numpy and then extract the real and imaginary parts of each complex number using the real and imag attributes, respectively. The resulting real and imaginary parts are then printed using the print() function.

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)

Output

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

Method 2: Using newaxis() method

The newaxis() method allows us to add an extra dimension to a NumPy array. When applied to an array of complex numbers, we can use it to separate the real and imaginary parts into separate dimensions.

Algorithm

  • Define a NumPy array of complex numbers.

  • Use newaxis() to add an extra dimension to the array.

  • Use the real attribute to extract the real parts of the complex numbers in the array.

  • Use the imag attribute to extract the imaginary parts of the complex numbers in the array.

  • Manipulate the real and imaginary parts of the complex numbers separately as needed.

  • Combine the manipulated real and imaginary parts as necessary.

Syntax

import numpy as np

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

Here, the newaxis() method is used to add an extra dimension to the original my_array. The : specifies that all rows in the original array should be preserved, and np.newaxis is used to add a new dimension at the end of the array. The resulting my_array_with_new_dim will have one extra dimension compared to my_array.

Example

In the below example, we first create an array of complex numbers a. We then use newaxis() to add an extra dimension to the array, which we can then use to separate the real and imaginary parts into separate dimensions. The resulting real and imaginary parts are then printed using the print() function.

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)

Output

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

Method 3: Using numpy.real() and numpy.imag() function

The numpy.real() and numpy.imag() functions can also be used to extract the real and imaginary parts of an array of complex numbers. These functions take a NumPy array of complex numbers as input and return separate arrays containing the real and imaginary parts, respectively.

Algorithm

  • Define a NumPy array of complex numbers.

  • Use the numpy.real() function to extract the real parts of the complex numbers in the array.

  • Use the numpy.imag() function to extract the imaginary parts of the complex numbers in the array.

  • Manipulate the real and imaginary parts of the complex numbers separately as needed.

  • Combine the manipulated real and imaginary parts as necessary.

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)

Here, the numpy.real() function is used to extract the real parts of the complex numbers in my_complex_array, and the numpy.imag() function is used to extract the imaginary parts of the complex numbers in my_complex_array. The resulting real_part and imag_part arrays will contain only the real and imaginary parts of the original complex numbers, respectively.

Example

In the below example, we create an array of complex numbers a and use the numpy.real() and numpy.imag() functions to extract the real and imaginary parts, respectively. The resulting real and imaginary parts are then printed using the print() function.

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)

Output

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

Conclusion

In this article, we discussed how we can extract the real and imaginary parts of a Numpy array of a complex number using real and imag attributes respectively. By using these attributes, we can manipulate the real and imaginary parts of complex numbers separately, which can be useful in various scientific and engineering applications.

Updated on: 10-Jul-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements