Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Related question: How can I convert a complex number a+b I to the exponent form A Exp(I phi)?

Mathematica insists on displaying complex number in form a+I b when a or b are not exact:

Clear["Global`*"]
z = 3 + 4 I;
Abs[z] Exp[I Arg[z]]

Mathematica graphics

z = 3.0 + 4 I;
Abs[z] Exp[I Arg[z]]

Mathematica graphics

What I'd like is a polarForm wrapper that keeps the polar form even when a or b are not exact, like this:

Clear["Global`*"]
(z = 3.0 + 4 I) // polarForm

Mathematica graphics

(z = 3 + 4 I) // polarForm

Mathematica graphics

In the above, polarForm is the wrapper needed.

share|improve this question
add comment

3 Answers 3

up vote 12 down vote accepted

Specify the display format of something using MakeBoxes, like so:

MakeBoxes[polarForm[z_Complex], form_] := 
 With[{r = Abs[z], ϕ = Arg[z]}, 
  RowBox[{If[r == 1, Sequence @@ {}, MakeBoxes[r, form]], 
    If[ϕ == 0, Sequence @@ {}, 
     SuperscriptBox[MakeBoxes[E, form], 
      RowBox[{MakeBoxes["\[ImaginaryI]", form], 
        If[ϕ == 1, Sequence @@ {}, 
         MakeBoxes[ϕ, form]]}]]]}]]

Sqrt[5] E^(I ArcTan[2]) // N // polarForm
(* 2.23607E^(I1.10715) *)
share|improve this answer
1  
+1 -- FYI you can use ##&[] in place of Sequence @@ {} -- see (1) and (2), and the comments below. –  Mr.Wizard Apr 11 '13 at 15:04
add comment

Here's an alternative:

polarForm[z_] := Module[{rt, f},
          If[Im[z] == 0 && Positive[Re[z]], Return[z]];
          rt = Through[{Abs, Arg}[z]];
          f = Which[
                    rt[[1]] == 1, Defer[E^(I #2)] &,
                    rt[[2]] == 1, Defer[#1 E^I] &,
                    True, Defer[#1 E^(I #2)] &];
          f @@ rt]

Sqrt[5] E^(I ArcTan[2]) // N // polarForm using this version should yield the same result as in Xerxes's answer. The upshot of this method is that the output can be copied and used as executable input, thanks to Defer[].

share|improve this answer
add comment

You could use David Park's Presentations add-on (see http://home.comcast.net/~djmpark/DrawGraphicsPage.html):

   << Presentations`
   ComplexToPolar[3 + 4 I]
      5 ∠ ArcTan[4/3]

Numbers in ComplexPolar form can be added, multiplied, etc., then converted using PolarToComplex.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.