Math 421  •  Fall 2006

Cartesian and polar forms of complex numbers

Copyright © 2004–2006 by Murray Eisenberg.  All rights reserved.

The author expresses appreciation to David Park for making a number of helpful suggestions about, and spotting several errata in, the orignal version of this notebook.

Introduction

This notebook discusses some functions that allow you to convert between complex numbers and their cartesian (x  +  i y) and polar (r (e^(i θ))) forms.  In particular, it discusses how to perform such conversions when the input is a symbolic expression that represents complex numbers—a situation that beginners can find especially frustrating.

All the input shown uses the display form i obtained by typing EsciiEsc but everything would work identically if you typed I instead.

Some of the functions used are built-in Mathematica functions; some others are provided by David Park's Cardano3 application.

The initialization cells, which you should have allowed to be evaluated when you opened this notebook load the Cardano3 application packages.  (If you didn't allow that, you will get a second chance when you first evaluate any Mathematica expression in this notebook.)

Initializations

Load David Park's Cardano3 application packages.

Needs["Cardano3`ComplexGraphics`"]
Needs["Cardano3`ComplexRoutines`"]

Turn off some annoying warning messages that might otherwise appear about similarly-named objects.  (If you want to see these messages so as to be sure to avoid harm, evaluate expressions like the ones below but replace Off there by On.)

Off[General::spell]
Off[General::spell1]

Cartesian form

Built-in functions Re and Im

To go from z  =  x  +  i y to (x,  y), the basic functions are the built-in Re and Im:

?Re

?Im

For a specific complex number, there is no difficulty in extracting the real and imaginary part:

z  =  2  +  3 I;
{Re[z],  Im[z]}

An object such as 2 + 3i is regarded by Mathematica as being numeric:

NumericQ[z]

And such an object is actually stored not as a sum of its real part and i times its imaginary part, but rather as an object of type Complex:

Head[z]
FullForm[z]

Difficulty does arise when you have only a symbolic expression representing complex numbers.  For example:

z  =  (((x  +  1))^2)   +  I (((2  - x y))^2);
{Re[z],  Im[z]}

NumericQ[z]
Head[z]
FullForm[z]

Mathematica does not "know" that variables such as x and y are real!  You have to tell it.   Do so by using the built-in function ComplexExpand.

Built-in function ComplexExpand

?ComplexExpand

Look again at the symbolic example:

z  =  (((x  +  1))^2)   +  I (((2  - x y))^2)

ComplexExpand[z]

And now you can take the real and imaginary parts like this:

{ComplexExpand[Re[z]],  ComplexExpand[Im[z]]}

Advanced topic: Assumed complex real variables and target functions in ComplexExpand

When used with one argument, ComplexExpand assumes that all variables are real.  You may explicitly specify that some are to be regarded as complex by including them in a list that forms a second argument.  For example:

w  =  (((1  +  ζ))^2) (t^2) + I (2  -  
              s) (ξ  +  1)

ComplexExpand[w,  {ζ,  ξ}]

Usually, ComplexExpand will try to express its result so as to involve "target functions" Re and Im.  But you can force it to use other functions by means of the option TargetFunctions.  For example:

ComplexExpand[1/ζ,  {ζ}]

ComplexExpand[1/ζ,  {ζ},  
      TargetFunctions  ->  {Abs,  Conjugate}]

ComplexExpand[ζ Conjugate[ζ],  {ζ}]

ComplexExpand[ζ Conjugate[ζ],  {ζ},  
      TargetFunctions  ->  Abs]

To force ComplexExpand to use Re and Im as the target functions, you may on occasion want to use Park's function ReImExpand.

?ReImExpand

Park's definition of ReImExpand is simply
	ReImExpand[expr_] := ComplexExpand[expr, TargetFunctions -> {Re, Im}]
(but he extends the definition so as to apply to a whole list of complex expressions as well).

For more about such advanced uses of ComplexExpand, see below.

The Cardano3 function ToCoordinates

The function ToCoordinates from Park's Cardano3 application shortens the above sort of expression.

?ToCoordinates

For example:

ToCoordinates[2  +  3  I]

Recall again the symbolic example:

z  =  (((x  +  1))^2)   +  I (((2  - x y))^2)

Try this:

ToCoordinates[z]

Still, Mathematica does not know that you intend x and y to be real.  But again ComplexExpand comes to the rescue:

ComplexExpand[ToCoordinates[z]]

To extract the real and imaginary parts of a symbolic complex quantity z use:
	ComplexExpand[ToCoordinates[z]]

Here's the same thing using function composition @ notation to eliminate nested brackets:

ComplexExpand @ ToCoordinates [z]

Note that using ComplexExpand and ToCoordinates in the opposite order will not accomplish what you undoubtedly want:

ToCoordinates[ComplexExpand[z]]

Advanced topic:  extracting real and imaginary parts of symbolic expressions without using Park's ToCoordinates

Park's definition of ToCoordinates is simply:
		ToCoordinates[z_] := {Re[z], Im[z]}

So if you want to avoid ToCoordinates, then instead you may use Map with ComplexExpand, Re, and Im to accomplish the same thing as before:

Map[ComplexExpand,  {Re[z],  Im[z]}]

To make that terser, employ the /@ special input form for Map:

ComplexExpand  /@  {Re[z],  Im[z]}

Note that evaluating ComplexExpand first and then taking Re and Im of the result will not do what you probably want:

{Re[ComplexExpand[z]],  Im[ComplexExpand[z]]}

When you want to use such an expression like ComplexExpand /@ {Re[z], Im[z]} on-the-fly without having assigned the complex variable expression involving the symbols x and y to a name z, you may want to avoid typing the expression twice, or even avoid copying and pasting it.  Here's an advanced way to accomplish that, using a pure function:

ComplexExpand  /@  ({Re[#],  
              Im[#]} &) [ (((x  +  1))^2)   +  
          I (((2  - x y))^2)]

From {x, y} to x + i y: the Cardano3 function ToComplex

To go backwards from a list of real and imaginary parts to a complex number presents no difficulty even for symbolic expressions.

Clear[xy,  s,  t]
xy  =  {1 + 2 s + (s^2), 
          4 - 4 s t + (s^2) (t^2)};
First[xy]  +  I Last[xy]

The Cardano3 function ToComplex makes that easier to do.

?ToComplex

ToComplex[{1 + 2 s + (s^2), 4 - 4 s t + (s^2) (t^2)}]

Park's definition of ToComplex is simply:
	ToComplex[{x_, y_}] := x + i y

Conjugate

The built-in function Conjugate finds—you guessed it!—the conjugate of a complex number.

?Conjugate

Conjugate[2  +  3 I]

As usual, there's difficulty when you don't have a specific complex number, but rather a symbolic expression representing complex numbers:

z  =  (((x  +  1))^2)   +  I (((2  - x y))^2);

Conjugate[z]

The simples way to handle this is by wrapping the whole expression with ComplexExpand:

ComplexExpand[Conjugate[z]]

One way to obtain the conjugate in the form you probably want is to use a rule:

z  /.  u_  +  I v_  ->  u  -  I v

Yet another way is to use the function ComplexCollect from Cardano3—see the following subsection.

The Cardano3 function ComplexCollect

Park's function ComplexCollect can also assist in forming the conjugate of a symbolic expression representing complex numbers.

?ComplexCollect

For example:

z  =  (((x  +  1))^2)   +  I (((2  - x y))^2);
ComplexCollect[Conjugate[z]]

There are other uses for ComplexCollect that will not be discussed here.

The Cardano3 overbar notation for conjugate

When you are typing an expression involving a conjugate in a text cell, you will probably want to use the traditional overbar notation.

To create an overbar for conjugate:
 	• load Park's Cardano3 application if you have not already done so;
 	• type the symbol or expression whose conjugate you want;
  	• select that symbol or expression;
 	• press simultaneously the Ctrl and & keys (you do not need to use ⇧ with the & key);
 	• type the underscore character by pressing simultaneously the ⇧ key and the underscore key (that's the key with the hyphen and underscore on it, usually just to the right of the number 0 key on the main keyboard).

Try this now:  in the following text cell, complete the sentence by inserting \(TraditionalForm\`z\&_\) in the first indicated spot and \(TraditionalForm\`\(2\  + \ 3  i\)\&_\) in the second indicated spot.

Traditionally [first spot ->]   stands for the conjugate of z; for example, [second spot ->]   = 2  -  3 i.

By default, Mathematica does not know that \(TraditionalForm\`z\&_\) denotes the conjugate of z.  However, one of the definitions from Park's Cardano3 application teaches Mathematica that this is so.

\(\(2\  + \ 3\ I\)\&_\)

Clear[w]
\(w\&_\)

You'll still need to use ComplexCollect (or another method) to actually find the conjugate of a symbolic expresion:

z  =  (((x  +  1))^2)   +  I (((2  - x y))^2);

\(z\&_\)

\(ComplexCollect[z\&_]\)

The Mathematica star-like notation for conjugate (Mathematica 5.1–5.2 only)

Another traditional notation for complex conjugate is z* .  Starting with Mathematica version 5.1, you can use this notation directly.

To create the star-like notation for conjugate:
 	• type the symbol or expression whose conjugate you want; and immediately after that,
  	• type EsccoEsc

(You do not use Ctrl^ to get to a superscript position!)   For example, if you type zEsccoEsc, you will get the result shown in the following input cell:

z\[Conjugate]

When you evaluate such an expression, Mathematica will automatically interpret it to mean Conjugate[z].  Thus:

z  =  (((x  +  1))^2)   +  I (((2  - x y))^2);
ComplexExpand[z\[Conjugate] ]

Polar form

Built-in functions Abs for modulus and Arg for principal argument

The modulus and principal argument of a complex number are returned by the built-in functions Abs and Arg.

?Abs

?Arg

{Abs[2  +  3  I],  
      Arg[2  +  3  I]}

Observe that Mathematica follows our convention that -π  <  
        Arg(z)  ≤   π:

Arg[-1]

⚠ Caution: Don't confuse Abs with either of the built-in objects Mod and Modulus.  The latter refer to arithmetic modulo an integer.

The usual difficulty in indentifying what is real and what is not arises when you have only a symbolic expression representing complex numbers.  For example:

z  =  (((x  +  1))^2)   +  I (((2  - x y))^2);
{Abs[z],  Arg[z]}

Fortunately, wrapping an Abs expression with ComplexExpand (see above) remedies the trouble in finding the modulus:

ComplexExpand[Abs[z]]

Unfortunately, the corresponding method does not serve to find the principal argument:

ComplexExpand[Arg[z]]

The reason is that Mathematica leaves an expression of the form Arg[z] unevaluated unless z is actually numeric.

Argument and the arctangent function

If you have a complex number in the form z  =  x  +  i y with x  ≠  0, you might think that Arg(z)  =  arctan(y/x), but that is not always true!  The trouble is that the ratio y/x obscures which quadrant the point (x,  y) lies in.  For example:

{Arg[-1  +  I],  ArcTan[(1/(-1))]}

{Arg[-1  -  I],  ArcTan[((-1)/(-1))]}

⚠ Caution:  It is very dangerous to use the Mathematica expression ArcTan[y/x] to find Arg[x + i y].

What you can more safely use, however, is the two-argument form ArcTan[x, y] to find Arg[x + i y].  For example:

{Arg[-1  +  I],  
      ArcTan[-1,  1]}
{Arg[-1  -  I],  
      ArcTan[-1,  -1]}

And whereas ArcTan[1/0] is indeterminate, the two-argument form ArcTan[0, 1] works:

{Arg[I],  ArcTan[0,  1]}

But still a bit dangerous to use ArcTan[x, y] to find Arg[x + i y]: both x and y might be 0.  And recall that Arg(0) is not defined mathematically.  Try this:  see what Mathematica gives for the value of Arg[0].

Traditional notation for modulus

When you are typing text in a text cell, to get the traditional notation |z| to denote the modulus of a complex number, just use the | key on the keykboard.

To cause Mathematica to evaluate the notation |z| as if it meant Abs[z] takes more work.  This requires that you use the standard add-on Utilities`Notation` package.  You may do this by evaluating the following two input cells.

<< Utilities`Notation`

Notation[|z_| ⟺ Abs[z_]]

⚠ Caution: Do not type the second of those input cells yourself, but rather either copy and paste the entire cell into your notebook or else reproduce it using the Notation palette that pops-up when you load the Notation add-on.

You may now use the traditional notation as input:

|2 + 3  I|

You'll still need to use ComplexExpand, too, when the expression inside the | | involves symbols:

z  =  (((x  +  1))^2)   +  I (((2  - x y))^2);
ComplexExpand[|z|]

Converting cartesian to polar form

To convert a specific, numeric complex number z  =  a  +  b i in cartesian form to polar form z  =  
        r (e^(i θ)), do the obvious thing:

(Subscript[z, 0])  =  2  +  3  I;
Abs[(Subscript[z, 0])] Exp[I Arg[(Subscript[z, 0])]]

When the argument is one of the standard values that Mathematica "knows", it will be found explicitly:

(Subscript[z, 1])  =  1  +  I;
Abs[(Subscript[z, 1])] Exp[I Arg[(Subscript[z, 1])]]

You will ordinarily have considerable difficulty in obtaining the polar form of a cartesian form symbolic expression representing complex numbers.  The best you will be able to do usually is to obtain an explicit form for the modulus.  For example:

z  =  (((x  +  1))^2)   +  I (((2  - x y))^2);
ComplexExpand[Abs[z]] Exp[I Arg[z]]

For a more explicit representation of Arg[z] there, see below.

Advanced topic: using a rule to obtain a value of Arg

In the preceding example, the expression Arg[z] was left in the form Arg[(((1 + x))^2) + i (((2 - x y))^2)].  To obtain a more explicit representation of that, in terms of Mathematica's ArcTan function, invoke Park's Cardano3 function ReImExpand:

z  =  (((x  +  1))^2)   +  I (((2  - x y))^2);
ReImExpand[Arg[z]]

To obtain that representation in the entire example, you may use the Mathematica "rule" z:Arg[_] :> ReImExpand[z] as follows:

z  =  (((x  +  1))^2)   +  I (((2  - x y))^2);
ComplexExpand[Abs[z]] Exp[I Arg[z]]  /.  
      z : Arg[_]  :>  ReImExpand[z]

Converting a function of a polar form complex variable to cartesian form

When a complex variable z is already specified in polar form, then a cartesian form for a function of that variable may be obtained in terms of the modulus and argument of z by using ComplexExpand.  Here are two examples of such conversions.

Clear[z]
z  =  r Exp[I θ]

ComplexExpand[(z^2)]

ComplexExpand[(z^2)  +  3  z  +  I  +  5]

Advanced topic: More about assumed complex real variables and target functions in ComplexExpand

This continues the discussion begun above about ComplexExpand with a second argument or a TargetFunctions option.

To express a function of a complex variable in terms of that variable's modulus and argument, you may use the form of ComplexExpand in which you declare that variable as complex and specify Abs and Arg as the target functions.  For example:

Clear[z]
ComplexExpand[(z^2),  z,  
      TargetFunctions  ->  {Abs,  Arg}]

Trig form

By the "trig form" of a complex number z we mean here the form 
	z  =  
        r cos θ  +  i sin θ 
rather than the equivalent exponential form z  =  
        r (e^(i θ)).  The built-in Mathematica functions ExpToTrig and TrigToExp convert back and forth between the two forms.

Here are some examples with symbolic expressions for complex numbers.

Clear[z,  r,  θ]
z  =  r Exp[I θ]

ExpToTrig[z]

ExpToTrig[(z^3)]

TrigToExp[%]

Thus Mathematica knows DeMoivre's formula!

TrigToExp[
      Cos[3  θ]  +  I Sin[2  θ]]

These functions may also be used with specific complex numbers.  For example:

ExpToTrig[(((-1))^(1/3))]

Note that the value of (((-1))^(1/3)) just found is the principal cube-root.  Indeed:

{Abs[-1],  Arg[-1]}

The definition of the principal cube-root of -1 is |-1|\(TraditionalForm\`\(\(\ \)\(e\^\(\(\ \)\(i\ \(Arg(\(-1\))\)\/3\)\)\)\)\).   In Mathematica, that exponential form is:

Abs[-1] Exp[Arg[-1]/3 I]

ExpToTrig[%]

The preceding result could also have been obtained from ComplexExpand[%].

Advanced topic: standard add-on package Algebra`ReIm`

If you load the standard add-on package Algebra`ReIm`, then Mathematica will use additional algebraic identities when evaluating expressions involving Re, Im, Abs, Conjugate, and Arg.

Look at what happens for a variable θ that is not known to be real and that package is not loaded:

Clear[θ]

Re[(((θ  +  1))^2)]

Conjugate[Exp[I θ]]

Nowload the package Algebra`ReIm`:

Needs["Algebra`ReIm`"]

To declare that θ is real, attach the rule that Im[θ] is 0 to the symbol θ:

θ  /:  Im[θ]  =  0

Now evaluate the same expressions with Re and Conjugate as before:

Re[(((θ  +  1))^2)]

Conjugate[Exp[I θ]]

Clear[θ]

Another way to declare a variable to be real is to assign a so-called "upvalue" of 0 to Im[θ], as follows:

Im[θ]  ^=  0

Conjugate[Exp[I θ]]

Similarly by using an expression of the form w /: Re[w] = 0 or Re[w] ^= 0 you can declare a symbol w to be pure imaginary.