The iscomplex() function of NumPy module

Overview:

  • A complex number is the number of the form a+ib, where “a" is the real part and “b” is the imaginary part. Both “a” and “b” are real numbers.

  • Complex numbers have their applications from solving polynomials to describing wave forms, signal processing, fluid dynamics, quantum physics and other areas of science.

  • The NumPy function iscomplex() checks an element whether it is a complex number and the result is True if the imaginary part is non-zero. The results are returned as an array of Boolean values. When the input is scalar, the function returns a Boolean value.

Example:

In Python, a complex number can be directly constructed in two ways:

  1. By invoking the built-in function complex().
  2. By adding a complex number whose real part is zero(e.g., 1j, -2j, 5j) to a real or integer value e.g., 2+5j, 4-3j.

The NumPy example below constructs complex numbers using the second approach. e.g.,10+4j

# Example Python program that checks 
# complex numbers for non-zero imaginary part
import numpy

# Create an ndarray
complexArray = numpy.ndarray(shape = (2, 2), dtype = complex)

complex1 = 10+4j
print(complex1)
print(type(complex1))

complex2 = 3+0j
print(complex2)
print(type(complex2))

complex3 = 0+1j
complex4 = 0+0j

# Populate the ndarray with complex numbers
complexArray[0][0] = complex1
complexArray[0][1] = complex2
complexArray[1][0] = complex3
complexArray[1][1] = complex4
print(complexArray)

# Check for complex numbers whose imaginary part is non-zero
resultArray = numpy.iscomplex(complexArray)
print(resultArray)

Output:

(10+4j)

<class 'complex'>

(3+0j)

<class 'complex'>

[[10.+4.j  3.+0.j]

 [ 0.+1.j  0.+0.j]]

[[ True False]

 [ True False]]

 


Copyright 2024 © pythontic.com