What is the numpy.imag() function in NumPy?

Overview

The imag() function in NumPy returns the imaginary part of the complex argument passed to it.

Syntax

numpy.imag(val)
Syntax for the imag() function

Parameter value

The imag() function takes a single parameter value (val) that represents an input with complex values. This val value can be a variable or an array.

Return value

The imag() function returns the imaginary component of the input (val) passed to it.

Example

import numpy as np
# creating a complex variable and an array of complex values
my_var = 1+4j
my_array = np.array([1+2j, 2+3j, 1+5j])
# implementing the imag() function
imag_var = np.imag(my_var)
imag_array = np.imag(my_array)
print(my_var)
print(imag_var)
print(my_array)
print(imag_array)

Explanation

  • Line 1: We import the numpy module.
  • Lines 4–5We create a complex-valued variable, my_varAnd an array, my_array that contains complex values.
  • Lines 8–9: We implement the imag() function on the inputs. We assign the result of my_var to a variable, imag_varAnd the result of my_array to imag_array.
  • Line 11: We print my_var, which contains the complex argument.
  • Line 12: We print the variable, imag_varwhich only contains the imaginary value of my_var.
  • Line 13: We print my_array, which contains the complex values.
  • Line 14: We print the array, imag_arraywhich only contains the imaginary values of my_array.

Free Resources