Skip to content
Advertisement

Count the number of complex, real and pure imaginary numbers in a numpy matrix

Given a Numpy array/matrix, what is pythonic way to count the number of complex, pure real and pure imaginary number:

JavaScript

Note: Please ignore the fact that complex numbers are superset of Imaginary and Real numbers.

Advertisement

Answer

complex

A number is complex if and only if its imaginary part is not zero, and its real part is not zero. Therefore:

JavaScript

pure real

Use numpy.isreal.

JavaScript

pure imaginary number

A number is pure imaginary if and only if:

  • its imaginary part is not zero, and
  • its real part is zero.

Therefore:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement