Reputation: 4996
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?
Upvotes: 3
Views: 176
Reputation: 117771
Complex numbers aren't "in polar form", or "in Cartesian representation", they just are. You can convert them into a polar form, or to a Cartesian representation.
re
and im
convert to such a Cartesian representation. If you want to convert to a polar form you'll need to actually do that. In Sympy the pair of functions for that is sp.Abs
to get the magnitude of the complex number, and sp.arg
to get the complex argument.
Upvotes: 4
Reputation: 13185
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
Upvotes: 3