Python NumPy imag() Function

NumPy imag() Function:

The imag() function of the NumPy module returns the element-wise imaginary part of the complex argument.

Syntax:

numpy.imag(a)

Parameters

a: This is required. It is an array given as input.

Return Value: 

The element-by-element imaginary part of the complex argument(array) is returned by the imag() function of the NumPy module

The type of “a” is utilized for the output if “a” is real. The returning type is float if “a” contains complex items.

NumPy real() Function in Python

Example1: (For 1-Dimensional Array)

Approach:

  • Import numpy module using the import keyword
  • Pass some random complex numbers list(1-Dimensional) as an argument to the
    array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the above-given array as an argument to the imag() function of the numpy module to get the imaginary part of all the complex elements of the given array.
  • Store it in another variable.
  • Print the imaginary part of all the elements of the given array.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random complex numbers list(1-Dimensional) as an argument to the 
# array() function to create an array. 
# Store it in a variable.
gvn_arry = np.array([1+3j, 4+3j, 2+2j,-1-5j])                     
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
print()
# Pass the above given array as an argument to the imag() function of the 
# numpy module to get the imaginary part of all the complex elements of
# the given array
# Store it in another variable.
rslt = np.imag(gvn_arry)
# Print the imaginary part of all the elements of the given array 
print("The imaginary part of all the elements of the given array:")
print(rslt)

Output:

The above given array is:
[ 1.+3.j 4.+3.j 2.+2.j -1.-5.j]

The imaginary part of all the elements of the given array:
[ 3. 3. 2. -5.]

Example2: (For n-Dimensional Array, where n is an integer)

Approach:

  • Import numpy module using the import keyword
  • Pass some random complex numbers list(n-Dimensional) as an argument to the
    array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the above-given array as an argument to the imag() function of the numpy module to get the imaginary part of all the complex elements of the given array.
  • Store it in another variable.
  • Print the imaginary part of all the elements of the given array.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random complex numbers list(n-Dimensional) as an argument to the 
# array() function to create an array. 
# Store it in a variable.
gvn_arry = np.array([[1+3j, 4+3j],
                     [2+4j, 5+6j]])           
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
print()
# Pass the above given array as an argument to the imag() function of the 
# numpy module to get the imaginary part of all the complex elements of
# the given array
# Store it in another variable.
rslt = np.imag(gvn_arry)
# Print the imaginary part of all the elements of the given array 
print("The imaginary part of all the elements of the given array:")
print(rslt)

Output:

The above given array is:
[[1.+3.j 4.+3.j]
 [2.+4.j 5.+6.j]]

The imaginary part of all the elements of the given array:
[[3. 3.]
 [4. 6.]]