0% found this document useful (0 votes)
5 views16 pages

Complex Numbers and Cmath Explained

Uploaded by

rubysapphire000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views16 pages

Complex Numbers and Cmath Explained

Uploaded by

rubysapphire000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Appendix J

Complex Numbers and cmath

The square root of a negative number (e.g., √−1, √−5, √−9) is called an
imaginary number, and it’s convenient to introduce the symbol 𝑖 = √−1 and
adopt √−𝑛 = 𝑖√𝑛 as its standard form (e.g. √−5 = 𝑖√5). 𝑖 has the property
𝑖2 = −1, and for higher powers:
𝑖3 = 𝑖2 ⋅ 𝑖 = (−1)𝑖 = −𝑖, 𝑖4 = (𝑖2 )2 = (−1)2 = 1, 𝑖5 = 𝑖4 ⋅ 𝑖 = 𝑖, etc.
𝑎+𝑏𝑖, where 𝑎 and 𝑏 are reals, is called a complex number. The first term 𝑎 is
its real part, and the second term 𝑏𝑖 is the imaginary part. Two complex numbers,
𝑎 + 𝑏𝑖 and 𝑐 + 𝑑𝑖, are equal if and only if 𝑎 = 𝑐 and 𝑏 = 𝑑.
The conjugate of 𝑎 + 𝑏𝑖 is 𝑎 − 𝑏𝑖. Multiplying a number to its conjugate
produces a real number, the square of its magnitude:
(𝑎 + 𝑏𝑖)(𝑎 − 𝑏𝑖) = 𝑎2 + 𝑏2
The basic algebraic operations:
• Addition. Separately add the real parts and imaginary parts. For example,
(2 + 3𝑖) + (4 − 5𝑖) = (2 + 4) + (3 − 5)𝑖 = 6 − 2𝑖.

• Subtraction. Separately subtract the real parts and imaginary parts. For ex-
ample,
(2 + 3𝑖) − (4 − 5𝑖) = (2 − 4) + [3 − (−5)]𝑖 = −2 + 8𝑖.

• Multiplication. Perform a multiplication as if each number was a tuple of


two values, and replace 𝑖2 by −1 in the result.
(𝑎 + 𝑏𝑖)(𝑐 + 𝑑𝑖) = 𝑎𝑐 − 𝑏𝑑 + (𝑎𝑑 + 𝑏𝑐)𝑖
839
840 Appendix J. Complex Numbers and cmath

For example,
(2 + 3𝑖)(4 − 5𝑖) = 8 − 15(−1) + (−10 + 12)𝑖 = 23 + 2𝑖.

• Division. Multiply the fraction’s numerator and denominator by the conju-


gate of the denominator. For example,
2 + 3𝑖 (2 + 3𝑖)(4 + 5𝑖) (8 − 15) + (10 + 12)𝑖 7 22
= = = − + 𝑖.
4 − 5𝑖 (4 − 5𝑖)(4 + 5𝑖) 16 + 25 41 41

Figure J.1. A Complex Number Graphically

In Fig. J.1, 𝑥 + 𝑦𝑖 is represented by the point P at (𝑥, 𝑦). It can also be encoded
as the directed line segment 𝑂𝑃. ⃗ This vector (and hence the complex number)
can be described in terms of its length 𝑟 and the angle 𝜃 it makes with the positive
𝑥-axis. 𝑟 = √𝑥2 + 𝑦2 is the modulus (magnitude or absolute value) of the com-
plex number, while 𝜃 is the phase (amplitude or arg), and usually the smallest,
positive angle for which tan 𝜃 = 𝑦/𝑥.
From Fig. J.1, we see that 𝑥 = 𝑟 cos 𝜃 and 𝑦 = 𝑟 sin 𝜃, so 𝑧 = 𝑥 + 𝑦𝑖 =
𝑟 cos 𝜃 + 𝑖𝑟 sin 𝜃 = 𝑟(cos 𝜃 + 𝑖 sin 𝜃). We call this the polar or trigonometric form,
and 𝑧 = 𝑥 + 𝑦𝑖 the rectangular form.

Figure J.2. 1 − 𝑖√3 in Polar Form


J.2. De Moivre’s Formula 841

Consider 𝑧 = 1 − 𝑖√3 in Fig. J.2. Its modulus 𝑟 = √(1)2 + (−√3)2 = 2, and


since tan 𝜃 = 𝑦/𝑥 = −√3/1 = −√3, the phase 𝜃 is either 120∘ or 300∘ . However,
since 𝑧 lies in quadrant IV then 𝜃 = 300∘ , and its polar form is
𝑧 = 𝑟(cos 𝜃 + 𝑖 sin 𝜃) = 2(cos 300∘ + 𝑖 sin 300∘ ).
Note that 𝑧 may also be represented by
𝑧 = 2[cos(300∘ + 𝑛360∘ ) + 𝑖 sin(300∘ + 𝑛360∘ )]
where 𝑛 is any integer. For instance, we could write 𝑧 = 2(𝑐𝑜𝑠(−60∘ )+𝑖𝑠𝑖𝑛(−60∘ ).

J.1 Multiplication in Polar Form


The polar representation is particularly useful when multiplying complex num-
bers together. If
𝑧 = 𝜌(cos 𝜙 + 𝑖 sin 𝜙),
and
𝑧 ′ = 𝜌′ (cos 𝜙′ + 𝑖 sin 𝜙′ ),
then
𝑧𝑧 ′ = 𝜌𝜌′ [(cos 𝜙 cos 𝜙′ − sin 𝜙 sin 𝜙′ ) + 𝑖(cos 𝜙 sin 𝜙′ + sin 𝜙 cos 𝜙′ )]
Using the addition theorems for sine and cosine,
cos 𝜙 cos 𝜙′ − sin 𝜙 sin 𝜙′ = cos(𝜙 + 𝜙′ ),
cos 𝜙 sin 𝜙′ + sin 𝜙 cos 𝜙′ = sin(𝜙 + 𝜙′ ).
the equation can be rewritten as:
𝑧𝑧 ′ = 𝜌𝜌′ [cos(𝜙 + 𝜙′ ) + 𝑖 sin(𝜙 + 𝜙′ )].
This form has modulus 𝜌𝜌′ and phase 𝜙 + 𝜙′ . In other words, to multiply two
complex numbers, we multiply their moduli and add their angles, as visualized
in Fig. J.3.

J.2 De Moivre’s Formula


The polar multiplication formula has a particularly important consequence when
𝑧 = 𝑧 ′ , for then we have
𝑧 2 = 𝜌2 (cos 2𝜙 + 𝑖 sin 2𝜙).
Multiplying again by 𝑧 we obtain
𝑧 3 = 𝜌3 (cos 3𝜙 + 𝑖 sin 3𝜙),
842 Appendix J. Complex Numbers and cmath

Figure J.3. 1 − 𝑖√3 in Polar Form

and continuing indefinitely in this way,

𝑧 𝑛 = 𝜌𝑛 (cos 𝑛𝜙 + 𝑖 sin 𝑛𝜙), for any integer 𝑛.

For example,

(√3 − 𝑖)10 = {2(cos 330∘ + 𝑖 sin 330∘ )}10


= 210 (cos(10 ⋅ 330∘ ) + 𝑖 sin(10 ⋅ 330∘ ))
= 1024(cos 60∘ + 𝑖 sin 60∘ ) = 1024(1/2 + 𝑖√3/2)
= 512 + 512𝑖√3.

If 𝑧 is a point on the unit circle, with 𝜌 = 1, we obtain the formula discovered


by A. De Moivre (1667–1754):

(cos 𝜙 + 𝑖 sin 𝜙)𝑛 = cos 𝑛𝜙 + 𝑖 sin 𝑛𝜙.

J.3 The 𝑛th Root of a Number


A complex number 𝑎 + 𝑏𝑖 = 𝑟(cos 𝜃 + 𝑖 sin 𝜃) has exactly 𝑛 distinct 𝑛th roots,
which can be readily obtained by applying De Moivre’s formula. As an example,
let’s find the fifth roots of (4 − 4𝑖) by considering its polar version 4√2(cos 315∘ +
𝑖 sin 315∘ ) in its most general form

4√2[cos(315∘ + 𝑘360∘ ) + 𝑖 sin(315∘ + 𝑘360∘ )],


J.4. Euler’s Formula 843

where 𝑘 is any integer, including zero. Using De Moivre’s theorem, the fifth root
of (4 − 4𝑖) is

{4√2[cos(315∘ + 𝑘360∘ ) + 𝑖 sin(315∘ + 𝑘360∘ )]}1/5


315∘ + 𝑘360∘ 315∘ + 𝑘360∘
= (4√2)1/5 (cos + 𝑖 sin )
5 5
= √2[cos(63∘ + 𝑘72∘ ) + 𝑖 sin(63∘ + 𝑘72∘ )].
Assigning in turn the values 𝑘 = 0, 1, 2, ⋯, we find

𝑘=0∶ √2(cos 63∘ + 𝑖 sin 63∘ ) = 𝑅1 (see Fig. J.4)


𝑘=1∶ √2(cos 135∘ + 𝑖 sin 135∘ ) = 𝑅2

𝑘=2∶ √2(cos 207∘ + 𝑖 sin 207∘ ) = 𝑅3

𝑘=3∶ √2(cos 279∘ + 𝑖 sin 279∘ ) = 𝑅4

𝑘=4∶ √2(cos 351∘ + 𝑖 sin 351∘ ) = 𝑅5

𝑘=5∶ √2(cos 423∘ + 𝑖 sin 423∘ ) = √2(cos 63∘ + 𝑖 sin 63∘ ) = 𝑅1


The five fifth roots were obtained with 𝑘 = 0, 1, 2, 3, 4, and in the general 𝑛th root
case by assigning 0, 1, 2, 3, … , 𝑛 − 1 to 𝑘.
The modulus of each of the roots is √2 so they all lie on a circle of radius √2
centered at the origin. The difference in phase of two consecutive roots is 72∘ so
the roots are equally spaced out around the circle, as in Fig. J.4.

J.4 Euler’s Formula


In what follows, the expression cos 𝜃 + 𝑖 sin 𝜃 is so common that I’ll abbreviate it
to cis 𝜃, so that, for example,
4 1 1
cis 𝜋 = cis 240∘ = cos 240∘ + 𝑖 sin 240∘ = − − 𝑖√3.
3 2 2
When
𝑧 = cis 𝜃 = cos 𝜃 + 𝑖 sin 𝜃,
then
𝑑𝑧/𝑑𝜃 = − sin 𝜃 + 𝑖 cos 𝜃 = 𝑖𝑧
We often see differential equations of this sort in physics: 𝑑𝑦/𝑑𝑡 = 𝑘𝑦, and the
solution is 𝑦 = 𝐴𝑒𝑘𝑡 (where 𝐴 is the value of 𝑦 when 𝑡 = 0). By analogy, it isn’t
too unreasonable to hope that the solution of our derivative might be something
like
𝑧 = 𝐴𝑒𝑖𝜃 , i.e., cis 𝜃 = 𝐴𝑒𝑖𝜃
844 Appendix J. Complex Numbers and cmath

Figure J.4. The Fifth Roots of 4 − 4𝑖

Putting 𝜃 = 0, we get 𝐴 = 1, so
cis 𝜃 = 𝑒𝑖𝜃 .
To express what we mean by a power 𝑎𝑧 when 𝑧 is complex, we need a
definition that produces the standard value of 𝑎𝑥 when 𝑧 is real, but also gives
𝑎𝑧1 × 𝑎𝑧2 = 𝑎𝑧1 +𝑧2 for all complex numbers 𝑧1 and 𝑧2 . A suitable definition
utilizes an infinite series of complex terms:
𝑒𝑧 = 1 + 𝑧 + 𝑧 2 /2! + 𝑧 3 /3! + 𝑧 4 /4! + ⋯ + 𝑧 𝑛 /𝑛! + ⋯
which can be shown to converge for all values of 𝑧.
It’s also possible to prove that 𝑒𝑧1 ⋅ 𝑒𝑧2 = 𝑒𝑧1 +𝑧2 , so that 𝑒𝑧 = 𝑒𝑥+𝑖𝑦 = 𝑒𝑥 ⋅ 𝑒𝑖𝑦 ,
where 𝑥 and 𝑦 are real. By definition, 𝑒𝑖𝑦 is
𝑒𝑖𝑦 = 1 + 𝑖𝑦 + (𝑖𝑦)2 /2! + (𝑖𝑦)3 /3! + ⋯
= 1 + 𝑖𝑦 − 𝑦2 /2! − 𝑖𝑦3 /3! + 𝑦4 /4! + ⋯
= (1 − 𝑦2 /2! + 𝑦4 /4! − ⋯) + 𝑖(𝑦 − 𝑦3 /3! + 𝑦5 /5! − ⋯)
It can be shown by Maclaurin’s expansion that for all 𝑦,
cos 𝑦 = 1 − 𝑦2 /2! + 𝑦4 /4! − ⋯ and sin 𝑦 = 𝑦 − 𝑦3 /3! + 𝑦5 /5! − ⋯
so we have
𝑒𝑖𝑦 = cos 𝑦 + 𝑖 sin 𝑦
or
𝑒𝑖𝜃 = cos 𝜃 + 𝑖 sin 𝜃,
J.5. Multi-valued Exponentiation 845

a formula due to the great mathematician Euler (1707-1783). Note that when
𝜃 = 𝜋, we have 𝑒𝑖𝜋 + 1 = 0, a remarkable equation connecting the mathematical
Big Five – 𝑒, 𝜋, 𝑖, 1, and 0.
The formula
cis 𝜃1 ⋅ cis 𝜃2 = cis(𝜃1 + 𝜃2 )

can now be rephrased as:


𝑒𝑖𝜃1 ⋅ 𝑒𝑖𝜃2 = 𝑒𝑖(𝜃1 +𝜃2 )

It’s trivial to derive De Moivre’s formula using Euler’s formula and the expo-
nential law for integer powers

(𝑒𝑖𝑥 )𝑛 = 𝑒𝑖𝑛𝑥 ,

since Euler’s formula implies that the left side of this equality is equivalent to
(cos 𝑥 + 𝑖 sin 𝑥)𝑛 while the right side is equal to cos 𝑛𝑥 + 𝑖 sin 𝑛𝑥.
Using the 𝑟, 𝜃 polar forms, three useful consequences are

𝑟1 𝑒𝑖𝜃1 ⋅ 𝑟2 𝑒𝑖𝜃2 = 𝑟1 𝑟2 𝑒𝑖(𝜃1 +𝜃2 )


(𝑟𝑒𝑖𝜃 )𝑛 = 𝑟𝑛 𝑒𝑖𝑛𝜃
(𝑟𝑒𝑖𝜃 )1/𝑛 = 𝑟1/𝑛 𝑒𝑖𝜃/𝑛

The angle 𝜃 is periodic — a complete rotation of 360∘ or 2𝜋 radians leaves


the trigonometric functions unchanged, so that:

𝑒𝑖𝜃 = 𝑒𝑖(𝜃+2𝜋) = 𝑒𝑖(𝜃+4𝜋) = 𝑒𝑖(𝜃+6𝜋) = …


(𝑟𝑒𝑖𝜃 )1/𝑛 = 𝑟1/𝑛 𝑒𝑖𝜃/𝑛 = 𝑟1/𝑛 𝑒𝑖(𝜃+2𝜋)/𝑛 = …

and so every complex number has 𝑛 n-th roots:


𝑛
√𝑧 = 𝑧 1/𝑛 = (𝑟𝑒𝑖𝜃 )1/𝑛 = 𝑟1/𝑛 𝑒𝑖(𝜃+2𝜋𝑘)/𝑛 , 𝑘 = 0, 1, 2, … , 𝑛 − 1

J.5 Multi-valued Exponentiation


𝑒𝑧 is not a one-to-one function for complex numbers, which is a problem because
a function can only have an inverse if it maps distinct values to distinct values. We
need to exclude enough complex numbers from 𝑧’s domain so that any 𝑒𝑧 result
can only be generated by a single 𝑧. The problem is that a complex number’s
polar angle can be represented by 𝜃 + 2𝜋𝑘, where 𝑘 is any integer. The most
common choice for restricting its range is to require −𝜋 < 𝜃 ≤ 𝜋, which is called
a principal branch.
846 Appendix J. Complex Numbers and cmath

J.5.1 The Complex Logarithm, log 𝑧. Once 𝑒𝑧 is defined to be one-to-one,


it makes sense to find its inverse, log 𝑧. We can define it by considering the polar
form 𝑧 = 𝑟𝑒𝑖𝜃 . Taking logs of both sides results in
log 𝑧 = log(𝑟𝑒𝑖𝜃 ) = log 𝑟 + 𝑖𝜃 = log 𝑟 + 𝑖(𝜃 + 2𝜋𝑘)
As usual, the angle is 𝜃 plus any multiple of 2𝜋, which poses a familiar problem.
The left side of Fig. J.5 shows the imaginary part of log 𝑧 spiralling up the z-axis,
highlighting how a single (𝑥 + 𝑖𝑦) point can generate an infinite number of log
values depending on how its polar angle is interpreted.

Figure J.5. 3D Surface Plot of log(𝑧) Showing its Imaginary Part

This is avoided by applying angle-based branch cuts to the log function sim-
ilar to those imposed on 𝑒𝑧 . The angles are constrained so that
log(𝑧) = log 𝑟 + 𝑖𝜃 where − 𝜋 < 𝜃 ≤ 𝜋
The right-hand side of Fig. J.5 shows what this does to the imaginary part of log 𝑧.
When 𝜃 ≈ 𝜋, 𝑧 is just above the negative real x-axis, and 𝜃 ≈ −𝜋 is just below the
axis.
Although this constrains log 𝑧 to be single-valued, it introduces a discontinu-
ity at the boundaries of the branch cuts along the negative x-axis – the gap visible
in the figure.

J.5.2 Other Complex Math Functions. The exponential function is the


foundation for most of the other complex math functions. For example,
𝑒𝑖𝑧 − 𝑒−𝑖𝑧 𝑒𝑖𝑧 + 𝑒−𝑖𝑧
sin 𝑧 = , cos 𝑧 = ,
2𝑖 2
and
𝑒𝑧 − 𝑒−𝑧 𝑒𝑧 + 𝑒−𝑧
sinh 𝑧 = , cosh 𝑧 = .
2 2
J.6. Complex Numbers in Python 847

Of course, this implies that


sin(𝑖𝑧) = 𝑖 sinh 𝑧, cos(𝑖𝑧) = cosh 𝑧.
The inverse complex functions are defined using logarithms:
arcsin 𝑧 = −𝑖 log(𝑖𝑧 + √1 − 𝑧 2 ), arccos 𝑧 = −𝑖 log(𝑧 + 𝑖√1 − 𝑧 2 ),
−1 −1
sinh 𝑧 = log(𝑧 + √𝑧 + 1),
2 cosh 𝑧 = log(𝑧 + √𝑧 2 − 1).
The multi-valued nature of the angles used in 𝑧 = 𝑟𝑒𝑖𝜃 mean that these functions
must also utilize branch cuts to limit their range of inputs and outputs.

J.6 Complex Numbers in Python


The imaginary unit symbol is written as j in Python, so 3 + 4𝑖 becomes 3+4j, but
𝑖 on its own is written as 1j, not j. Below is a sample session involving complex
numbers and simple arithmetic:

>>> a = 3 + 4j (-7+24j)
>>> b = 1 - 2j >>> a ** b # complex powers
>>> a + b (-21.083139690689016-24.00021070941257j)
(4+2j) >>> a**(1/2) # roots
>>> a - b (2+1j)
(2+6j) >>> [Link]()
>>> a * b (3-4j)
(11-2j) >>> abs(a) # the modulus
>>> a / b 5.0
(-1+2j) >>> [Link]
>>> a + 2 3.0
(5+4j) >>> [Link]
>>> a * 2 4.0
(6+8j) >>> import math
>>> a / 2 >>> [Link](
(1.5+2j) math.atan2([Link], [Link]))
>>> a**2 53.13010235415598

I didn’t need to import cmath for any of these basic operations, but math is
necessary if I want to get a number’s polar angle.
The following session shows some of the common errors a user may en-
counter when first using complex numbers in Python:
>>> c = 3 + 4i
^
SyntaxError: invalid syntax
>>> (1 + 2j) * (3 + 4j)
(-5+10j)
>>> 1 + 2j * (3 + 4j) # parenthesis needed
848 Appendix J. Complex Numbers and cmath

(-7+6j)
>>> (3 + 4j) // 2
TypeError: can't take floor of complex number.
>>> [Link](1 + 2j)
TypeError: can't convert complex to float
>>> [Link]((3 + 4j))
TypeError: can't convert complex to float

J.6.1 Using cmath. A common misunderstanding is that the functions in the


math module will work with complex numbers, which isn’t the case as the ses-
sion above illustrates. For that capability, the cmath module ([Link]
.[Link]/3/library/[Link]) adds an assortment of exponential,
logarithmic, trigonometric, and hyperbolic functions, as listed in Table J.1.

Exponential and Logarithmic Functions


exp(z) Return 𝑒𝑧
log(z[, base]) Return the logarithm of 𝑧 to the given base
(default: natural log)
log10(z) Return the base-10 logarithm of 𝑧
sqrt(z) Return the square root of 𝑧
Trigonometric Functions
sin(z), cos(z), tan(z), asin(z), acos(z), atan(z)
Hyperbolic Functions
sinh(z), cosh(z), tanh(z), asinh(z), acosh(z), atanh(z)
Complex Number Conversion
phase(z) Return the phase (angle) of complex number 𝑧 in
radians
polar(z) Convert complex number 𝑧 to polar coordinates
(𝑟, 𝜙)
rect(r, phi) Convert polar coordinates (𝑟, 𝜙) to complex number
Classification Functions
isfinite(z) Return True if both real and imaginary parts are
finite
isinf(z) Return True if either real or imaginary part is
infinity
isnan(z) Return True if either real or imaginary part is
NaN
isclose(a, b) Return True if values 𝑎 and 𝑏 are close to each
other
Constants
[Link], cmath.e, [Link], [Link], [Link], [Link], [Link]

Table J.1. Python cmath Module


J.6. Complex Numbers in Python 849

The following session shows a few uses of cmath:


>>> a = 3 + 4j
>>> import cmath
>>> r, theta = [Link](a)
>>> import math
>>> [Link](theta)
53.13010235415598
>>> r
5.0
>>> [Link](r, theta)
(3.0000000000000004+3.9999999999999996j)
>>> [Link](a)
(3.853738037919377-27.016813258003936j)
>>> [Link](a)
(-13.128783081462158-15.200784463067954j)
>>> [Link]([Link](a))
(3+3.999999999999999j)
>>> [Link](1j*[Link]) + 1 # Euler's equation e^{i pi) + 1 = 0
1.2246467991473532e-16j

J.6.2 The 𝑛th Root using Python. Let’s return to the 𝑛th root example
from earlier, but this time coded in Python. The aim is to find the fifth roots of
(4 − 4𝑖). The following code creates the variable:
>>> a = 4 - 4j
>>> [Link](a)
(5.656854249492381, -0.7853981633974483) # r, theta
>>> 4 * [Link](2) # this is r
5.656854249492381
>>> [Link](315) # this is not theta
5.497787143782138
>>> [Link](315-360) # this is theta
-0.7853981633974483
The polar angle for (4 − 4𝑗) has been normalized to fall between −𝜋 and 𝜋.
The next fragment shows the single value returned for 𝑎1/5 :
>>> root = a ** (1/5)
>>> root
(1.3968022466674208-0.22123174208247431j)
>>> [Link](root)
(1.4142135623730954, -0.15707963267948966) # r, theta
>>> [Link](root)
-0.15707963267948966 # theta again
>>> [Link]( [Link](root)) # in degrees
-9.0
If we look back at Fig. J.4, it turns out that Python is giving us 𝑅5 , but the angle
is −9∘ rather than 351∘ .
850 Appendix J. Complex Numbers and cmath

We could get the other roots by adding and subtracting multiples of 360/5∘
to this result, but a simpler approach is to use my getRoots() function, imported
from [Link] :
>>> import complexUtils
>>> rs = [Link](a, 5) # a^(1/5)
>>> for r in rs: print(r)
(1.3968022466674208-0.22123174208247431j)
(0.6420395219202063+1.2600735106701009j)
(-1+1.0000000000000002j)
(-1.260073510670101-0.642039521920206j)
(0.22123174208247404-1.3968022466674208j)
Once again referring to Fig. J.4, we can see that the roots are listed in counter-
clockwise order starting with 𝑅5 . The code for getRoots() implements the for-
mula:
𝑧 1/𝑛 = (𝑟𝑒𝑖𝜃 )1/𝑛 = 𝑟1/𝑛 ∗ 𝑒𝑖(𝜃+2𝜋𝑘)/𝑛 , for 𝑘 = 0, 1, 2, … , 𝑛 − 1
def getRoots(z, n):
# Handle zero specially
if [Link] == 0 and [Link] == 0:
return [complex(0, 0)]
# Get principal root
rRoot = abs(z)**(1/n)
theta = [Link](z)
# Calculate all n roots
roots = []
for k in range(n):
ang = (theta + 2*[Link] * k)/n
[Link]( rRoot*[Link](1j*ang))
return roots
Going one step better, [Link] plots these roots in the style of Fig. J.4.
In the following call, the user supplies three integers representing (4 − 4𝑖)1/5 :
> python [Link]
Real part: 4
Imaginary part: -4
Root number (n): 5
The image in Fig. J.6 is generated.

J.6.3 Investigating Branch Cuts. The branch cut for [Link]() restricts
a complex number’s angle to be in the range −𝜋 < 𝜃 ≤ 𝜋, which corresponds to
either side of the negative real x-axis. This can be confirmed by testing some
values which are just above and just below the branch cut:
>>> above = [Link](1, [Link]-0.00001) # e^(i pi - eps)
>>> above
J.6. Complex Numbers in Python 851

Figure J.6. The Fifth Roots of 4-4i using [Link]

(-0.99999999995+1.000000000002131e-05j) # (-1 + <small>i)


>>> below = [Link](1, -[Link]+0.00001) # e^(i -pi + eps)
>>> below
(-0.99999999995-1.000000000002131e-05j) # (-1 - <small>i)
>>> [Link](above)
(-4.1355554465750615e-18+3.141582653589793j) # 0 + i pi
>>> [Link](below)
(-4.1355554465750615e-18-3.141582653589793j) # 0 - i pi

above and below have different polar coordinates, but their rectangular forms
are just above and below 𝑥 = −1. Fig. J.7 shows these two points on the principal
branch for [Link]() from Fig. J.5.
The discontinuity in the log curve becomes apparent when the logarithms of
above and below are compared – their imaginary parts differ by almost 2𝜋. In
other words, two complex numbers in close proximity have been mapped to very
different places on the log curve.
This discontinuity issue exists for all of the complex math functions, so care
has to be taken when using them. [Link] offers a way to visualize these
curves by creating four plots of the cmath function supplied by the user. For
instance, in the following call the user has typed log:
> python [Link]
Enter function name: log
Plotting log(z)
852 Appendix J. Complex Numbers and cmath

Figure J.7. The above and below points on the Principal Branch for
[Link]()

Real range: [-3.0, 3.0]


Imaginary range: [-3.0, 3.0]

The program reads the ranges for the real and imaginary axes from the text file
[Link] , and generates the four plots of [Link](z) shown in Fig. J.8.
The plots can be individually rotated, and the 'Imaginary Part' graph in Fig.
J.8 has been turned so it corresponds to the image in Fig. J.5. The branch cut isn’t
rendered as a gap but rather as a very abrupt jump in the values plotted on the
z-axis.
Each graph is based on a series of (𝑥, 𝑦, 𝑧) points generated by calling the
specified function on (𝑥 + 𝑖𝑦) to obtain 𝑧. Each of these 𝑧 complex numbers is
converted in a different way appropriate for a particular graph. For example, the
imaginary plot utilizes [Link] values.
The drawback of 3D plots is that they only represent three dimensions of
data. Across the base of the graph, I’m plotting the real and imaginary parts of
each complex number input to the function (log 𝑧 in Fig. J.8), but only part of the
complex number returned by the function along the z-axis.
For example, the (𝑥 + 𝑖𝑦) output by a call to log 𝑧 is divided into the top-
right plot for each 𝑥 and the bottom-left plot for each 𝑦. Argueably this is useful
because the branch cut related to the polar angle range becomes clearer when the
imaginary output is separated out.
Another very common approach is to draw phase plots enhanced with do-
main coloring and contour lines. For example, Fig. J.9 is generated by [Link]
when the user enters log:
> python [Link]
Enter function name: log
Plotting log(z)
J.6. Complex Numbers in Python 853

Figure J.8. Visualizing [Link](z)

Real range: [-3.0, 3.0]


Imaginary range: [-3.0, 3.0]

Fig. J.9 corresponds most closely to the bottom-right plot of the phase infor-
mation in Fig. J.8, but the colors, and their ordering, have additional meaning.
Positive numbers are colored red, negative numbers are colored blue, and
zeros and poles occur at the points where all the colors meet. A zero is distin-
guished by the change from red, yellow, green, to blue in a counter-clockwise
order around the point. A pole, which is when the output extends off to infinity,
has its colors change in a clockwise order. This means that a zero is present on
the middle right of Fig. J.9.
A sudden jump from one color to another, like the one that occurs in the
middle left of Fig. J.9 is an indication of a discontinuity caused by a branch cut.
There are two types of contour line drawn over the surface – the black lines
are contours of equal phase, and are labelled in degrees. The white lines are
854 Appendix J. Complex Numbers and cmath

Figure J.9. Visualizing [Link](z) with a Phase Plot

contours of equal magnitude (modulus), and so are showing the data that is pre-
sented in the top-left graph of Fig. J.8. In Fig. J.9 these reinforce the fact that a
zero occurs on the middle right, and that the magnitudes increase to the middle
left.
Section 7.11.7 on solving cubic equations uses much of the same code (which
is located in [Link]). Alternatively, a very nice online tool that does
much the same job can be found at [Link]
x/function-plotter/[Link]. A good overview of the approach can be found
in [Weg16].

You might also like