how to translate complex number from real imaginary form to polar form

246 views
Skip to first unread message

chaowen guo

unread,
Oct 29, 2016, 10:25:33 PM10/29/16
to sympy
Hi:

I know expand_complex can translate from polar form (say e*ix) to real imaginary form (cos(x)+isin(x)), however I also need to do it in the other way from (cos(x)+isin(x)) to e*ix. The ugly way is to:

a=cos(x)+isin(x)
a=sympy.Abs(a)*sympy.exp(sympy.I*sympy.arg(a))

I want to know is there any elegant way (just a sympy builtin function call) to do that?

Actually, I have a complex number expression, now I want to represent all the complex symbol in the polar form |x|e^(iarg(x)), how to do that?

Nathan Thern

unread,
Nov 1, 2016, 2:53:53 PM11/1/16
to sympy
>>> r, r1, r2 = symbols('r r1 r2', real=True)
>>> g1, g2, h1, h2 = symbols('g1 g2 h1 h2', cls=Function)
>>> g1=exp(I*r)
>>> h1=expand_complex(g1)
>>> h1
I*sin(r) + cos(r)
>>> h1.rewrite(exp)
exp(I*r)

>>> g2=r1*exp(I*r2)
>>> h2=expand_complex(g1)
>>> h2
I*r1*sin(r2) + r1*cos(r2)
>>> h2.rewrite(exp)
r1*(exp(I*r2)/2 + exp(-I*r2)/2) + r1*(exp(I*r2) - exp(-I*r2))/2
>>> h2.rewrite(exp).simplify()
r1*exp(I*r2)
Reply all
Reply to author
Forward
0 new messages