Numpy iscomplex: The Complete Overview

Python Numpy iscomplex() function returns a bool array, where True if the input element is complex.

Numpy iscomplex()

Numpy iscomplex() function tests if an element is a complex number or not. It only checks for complex numbers and not for Infinity or NaN values. The iscomplex() function returns the Boolean values in a Boolean array. 

The np.iscomplex() method is defined under numpy, which can be imported as import numpy as np. We can create multidimensional arrays and derive other mathematical statistics with the help of numpy, which is a library in Python.

Syntax

numpy.iscomplex(input array)

Parameters

The np iscomplex() function takes only one parameter. So the only parameter is the input array or the input for which we want to check whether it is a complex number or not.

Return Value

The iscomplex() function returns a Boolean array, which has the result if the number passed in the input array is a complex number or not.

Example programs on iscomplex() method in Python:

Write a program to show the working of iscomplex() function in Python.

# app.py

import numpy as np

print("Complex - : ", np.iscomplex(933), "\n")  # Scalar Values

print("Complex - : ", np.iscomplex([444, 555]), "\n")


# checking for complex numbers
print("Complex - ", np.iscomplex([2+3j, 3+1j]), "\n")

print("Complex - ", np.iscomplex([2+1j, 4+2j]), "\n")

Output

python3 app.py
Complex - :  False

Complex - :  [False False]

Complex -  [ True  True]

Complex -  [ True  True]

Explanation

In this example, we’ve seen that bypassing two scalar values in the function isinf(). Hence, we get False as it doesn’t represent any complex number. Still, other values in the example show true as there is a complex number present.

Write a program to use arange() function and create an array, then check if the elements of the array are complex or not.

See the following code.

# app.py

import numpy as np

a = np.arange(24).reshape(6, 4)
print("List = ", a)
print("\n")
print("Is complex -  ", np.iscomplex(a))

Output

python3 app.py
List =  [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]
 [16 17 18 19]
 [20 21 22 23]]


Is complex -   [[False False False False]
 [False False False False]
 [False False False False]
 [False False False False]
 [False False False False]
 [False False False False]]

In this example, we can see that after creating an array, we checked for every element of the array if it’s complex or not.

Conclusion

Numpy iscomplex() function returns a bool array, where True if the input element is complex. What is tested is whether the input has a non-zero imaginary part, not if the input type is complex.

See also

Numpy nan

Numpy save()

Numpy isrealobj()

Numpy expand_dims()

Numpy isreal()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.