Finding Real and Imaginary Parts

Write a NumPy program to find the real and imaginary parts of an array of complex numbers. Return the real part and the imaginary part as separate arrays.

Example 1:

Input: np.array([1+2j, 3+4j, 5+6j]) 
Output: Real part: array([1., 3., 5.]), Imaginary part: array([2., 4., 6.])

Example 2:

Input: np.array([7+8j, 9+10j, 11+12j]) 
Output: Real part: array([ 7., 9., 11.]), Imaginary part: array([ 8., 10., 12.])

You can use .real and .imag attributes to get the real part and the imaginary part of complex numbers.

import numpy as np

def find_parts(array):
    return array.real, array.imag

print(find_parts(np.array([1+2j, 3+4j, 5+6j])))
print(find_parts(np.array([7+8j, 9+10j, 11+12j])))


© Let’s Data Science


Unlock AI & Data Science treasures. Log in!