Complex Numbers
This reference lesson covers complex numbers at the depth calculus needs: arithmetic, the complex plane, polar form, Euler's formula, De Moivre's theorem and roots. They return in second-order differential equations (complex roots of the characteristic equation) and they are a native type in Python and NumPy.
Definition and arithmetic
A complex number is with real and . The real part is and the imaginary part . Arithmetic follows the usual algebra rules plus :
| Operation | Rule |
|---|---|
| Sum | |
| Product | |
| Conjugate | , and |
| Quotient | |
| Modulus | , |
Every polynomial equation of degree has exactly complex roots counted with multiplicity (the Fundamental Theorem of Algebra), and the non-real roots of a real polynomial come in conjugate pairs. For example gives .
Python writes as j:
z, w = 3 + 4j, 1 - 2j
print("z + w =", z + w, " z*w =", z * w, " z/w =", z / w)
print("|z| =", abs(z), " conj(z) =", z.conjugate(), " z*conj(z) =", z * z.conjugate())
import numpy as np
print("roots of x^2 - 4x + 13:", np.roots([1, -4, 13]))z + w = (4+2j) z*w = (11-2j) z/w = (-1+2j)
|z| = 5.0 conj(z) = (3-4j) z*conj(z) = (25+0j)
roots of x^2 - 4x + 13: [2.+3.j 2.-3.j]
The complex plane and polar form
Plot as the point . With and (the angle from the positive real axis, found with , see Trigonometry Review):
The product rule follows from the addition formulas for sine and cosine. So multiplying by a complex number of modulus 1 is a rotation.
Euler's formula and De Moivre
One way to see it: substitute into the Maclaurin series of (see Taylor and Maclaurin Series). The even powers of are real and alternate in sign, building ; the odd powers are imaginary, building . Repeated multiplication gives De Moivre's theorem:
and the -th roots of are the numbers
which sit evenly spaced on a circle.
import cmath, math
z = 1 + 1j
r, theta = cmath.polar(z)
print("polar:", round(r, 6), round(theta, 6), " (sqrt 2, pi/4)")
print("De Moivre: r^8 =", round(r**8, 9), " 8*theta in turns =", round(8 * theta / (2 * math.pi), 9), " directly z^8 =", z**8)
print("e^(i pi) + 1 =", cmath.exp(1j * math.pi) + 1)
roots = [cmath.exp(2j * math.pi * k / 3) * 8 ** (1/3) for k in range(3)]
for k, w in enumerate(roots):
print(f"cube root {k} of 8: {w.real:+.4f} {w.imag:+.4f}i cubed = {w**3:.4f}")polar: 1.414214 0.785398 (sqrt 2, pi/4)
De Moivre: r^8 = 16.0 8*theta in turns = 1.0 directly z^8 = (16+0j)
e^(i pi) + 1 = 1.2246467991473532e-16j
cube root 0 of 8: +2.0000 +0.0000i cubed = 8.0000+0.0000j
cube root 1 of 8: -1.0000 +1.7321i cubed = 8.0000-0.0000j
cube root 2 of 8: -1.0000 -1.7321i cubed = 8.0000-0.0000j
comes out as instead of 0: rounding error in , not a failure of Euler's formula. The gentle survey is Complex Numbers.
In calculus
When has a characteristic equation with roots , Euler's formula turns into , and the real solutions are and : damped oscillations. See Second-Order Linear Equations.
In software
- 2D rotation. Multiplying the point by rotates it by about the origin, the same as the rotation matrix. Some 2D engines store orientations as unit complex numbers; quaternions are the 3D generalisation.
- Signals and the FFT. The discrete Fourier transform writes a signal as a sum of , the -th roots of unity. The FFT computes it in by exploiting their symmetry; audio, image compression and fast polynomial multiplication rely on it.
- Fractals. The Mandelbrot set iterates .
import numpy as np
point = 2 + 1j
print("rotate (2, 1) by 90 degrees:", point * np.exp(1j * np.pi / 2).round(12))
t = np.arange(8)
signal = 3 * np.cos(2 * np.pi * t / 8) + np.sin(2 * np.pi * 2 * t / 8)
spectrum = np.fft.fft(signal).round(6)
print("FFT magnitudes:", np.abs(spectrum).round(3).tolist())rotate (2, 1) by 90 degrees: (-1+2j)
FFT magnitudes: [0.0, 12.0, 4.0, 0.0, 0.0, 0.0, 4.0, 12.0]
The FFT finds frequency 1 with weight 3 (magnitude ) and frequency 2 with weight 1 (magnitude 4), each mirrored at .
Exercises
Exercise 1: Arithmetic
Compute and .
Answer
and .
Solution
Exercise 2: Polar form and a power
Write in polar form and compute .
Answer
; the sixth power is .
Solution
, . Then and , so the result is .
print((-2 + 2j) ** 6)(-0+512j)
The -0 is a signed floating-point zero, still 0.
Exercise 3: Fourth roots
Find all fourth roots of .
Answer
and .
Solution
. The roots are for : angles , giving .
Exercise 4: Rotate a polygon (code)
Rotate the triangle with vertices , , by about the point using complex numbers: . Print the new vertices rounded to 4 decimals.
Answer
, , (to 3 decimals).
Solution
import cmath, math
c = 2 + 1j
rot = cmath.exp(1j * math.radians(60))
for z in [1 + 0j, 3 + 0j, 2 + 2j]:
w = c + (z - c) * rot
print(round(w.real, 4), round(w.imag, 4))2.366 -0.366
3.366 1.366
1.134 1.5
Test yourself
What is ?
Multiplying two complex numbers in polar form…
- adds moduli and multiplies angles
- multiplies moduli and adds angles
- multiplies moduli and multiplies angles
- adds both
A real-coefficient polynomial has the root . Which other number must be a root?
- None necessarily