Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Output complex numbers in polar (exponential) form?

558 views
Skip to first unread message

Mr.CRC

unread,
Sep 2, 2011, 3:32:06 AM9/2/11
to
Hi:

For ex:

In[2]:= ToExp[z_] := Abs[z] E^(I Arg[z])

In[4]:= ToExp[3 + 4 I]

Out[4]= 5 E^(I ArcTan[4/3])


However, this converts back to rectangular form when a float is involved:

In[5]:= ToExp[3 + 4. I]

Out[5]= 3.+ 4. I


Any idea how to make a consistent exponential complex number output?


Thanks.


--
_____________________
Mr.CRC
crobc...@REMOVETHISsbcglobal.net
SuSE 10.3 Linux 2.6.22.17

Bill Rowe

unread,
Sep 3, 2011, 8:05:43 AM9/3/11
to
On 9/2/11 at 3:29 AM, crobc...@REMOVETHISsbcglobal.net (Mr.CRC)
wrote:

>For ex:

>In[2]:= ToExp[z_] := Abs[z] E^(I Arg[z])

>In[4]:= ToExp[3 + 4 I]

>Out[4]= 5 E^(I ArcTan[4/3])

>However, this converts back to rectangular form when a float is
>involved:

>In[5]:= ToExp[3 + 4. I]

>Out[5]= 3.+ 4. I

>Any idea how to make a consistent exponential complex number output?

Use Rationalize to convert the machine precision values to exact
values, i.e.,

In[5]:= toExp[z_] := Module[{x = Rationalize[z]},
Abs[x] E^(I Arg[x])]

In[6]:= toExp[3. + 4. I]

Out[6]= 5*E^(I*ArcTan[4/3])


Simon

unread,
Sep 3, 2011, 8:08:16 AM9/3/11
to
Here's the answer I gave to a similar question (http://stackoverflow.com/q/3928489/421225) on stackoverflow.

If you only need to do it occasionally, then you could just define a function like

ComplexToPolar[z_] /; Element[z, Complexes] :=
Interpretation[
Grid[{{Abs[z], Superscript[E, Row[{I, Arg[z]}]]}}, Spacings -> .2],
z]

If always want objects with Head Complex, then something like

Unprotect[Complex];
Complex /: MakeBoxes[Complex[a_, b_], StandardForm] :=
With[{abs = Abs[Complex[a, b]], arg = Arg[Complex[a, b]]},
RowBox[{MakeBoxes[abs, StandardForm],
SuperscriptBox["\[ExponentialE]",
RowBox[{"\[ImaginaryI]", MakeBoxes[arg, StandardForm]}]]}]]
Protect[Complex];

For complex numbers such as 3 + 4 Pi I, the first option works, but the second doesn't. This is clear from the FullForm.

Mr.CRC

unread,
Sep 8, 2011, 5:27:05 AM9/8/11
to


Thanks for the response.

I think the first option works.

0 new messages