Search code examples
pythonsympycomplex-numberspolar-coordinates

real and imaginary part of a complex number in polar form


I am a bit confused about the proper way to deal with complex numbers in polar form and the way to separate its real and imaginary part.

Notice that I am expecting as real part the radius, as imaginary part the angle.

The inbuilt re and im functions get always the real and imaginary part of the Cartesian representation of the complex number.

Here an example

from sympy import I, pi, re, im, exp, sqrt

# complex num in Cartesian form
z = -4 + I*4
print(re(z), im(z))
# 4 -4

# complex num in polar form
z = 4* sqrt(2) * exp(I * pi * 3/4)
print(re(z), im(z))
# 4 -4  but expacting 4*sqrt(2), pi*3/4

What is the most SymPytonic way to deal with such problem?


Solution

  • Maybe you are looking for Abs and arg functions?

    z = -4 + I*4
    print(Abs(z), arg(z))
    # 4*sqrt(2) 3*pi/4
    
    z = 4* sqrt(2) * exp(I * pi * 3/4)
    print(Abs(z), arg(z))
    # 4*sqrt(2) 3*pi/4