How to extract the real and imaginary parts of a complex number in python ?

Daidalos November 15, 2019

To extract the the real and imaginary parts of a complex number z=a+ib in python, a solution is to use z.real and z.imag:

Extract the real and imaginary parts of a complex number

>>> z = complex(2,5)
>>> z
(2+5j)
>>> z.real
2.0
>>> z.imag
5.0


Matrix of complex number

It also works with matrix of complex numbers:

>>> import numpy as np
>>> Z = np.array([[1+2j,1+3j],[5+6j,3+8j]])
>>> Z
array([[ 1.+2.j,  1.+3.j],
       [ 5.+6.j,  3.+8.j]])
>>> Z.real
array([[ 1.,  1.],
       [ 5.,  3.]])
>>> Z.imag
array([[ 2.,  3.],
       [ 6.,  8.]])


References

Complex numbers usage in python stackoverflow
separate real and imaginary part of a complex number in python stackoverflow
Mathematical functions for complex numbers python doc

Licence