Complex Numbers
Complex numbers extend the real numbers with a square root of −1. They started as a trick for solving equations like , but they turned out to be the natural language for rotations, waves and signals. Python supports them out of the box.
The imaginary unit
Define as a number with
A complex number is , where is the real part and the imaginary part, both real. Real numbers are the complex numbers with .
The powers of repeat in a cycle of four: , , , , , …
Python writes the imaginary unit as j (the engineering convention), SymPy as I:
z = 3 + 4j
w = 1 - 2j
print("z + w =", z + w)
print("z * w =", z * w)
print("z / w =", z / w)
print("Re, Im of z:", z.real, z.imag)
print("conjugate of z:", z.conjugate())
print("|z| =", abs(z))
print("1j**2 =", 1j**2)z + w = (4+2j)
z * w = (11-2j)
z / w = (-1+2j)
Re, Im of z: 3.0 4.0
conjugate of z: (3-4j)
|z| = 5.0
1j**2 = (-1+0j)
Arithmetic
| Operation | Rule |
|---|---|
| add | |
| multiply | (expand, then use ) |
| conjugate | |
| modulus | , and |
| divide | multiply top and bottom by the conjugate of the bottom |
Division by hand, :
The complex plane and polar form
Draw as the point . Then is the distance from the origin and the angle from the positive real axis is the argument, . Every non-zero complex number can be written in polar form:
Euler's formula connects this to the exponential function:
and with it gives the famous . You will see why Euler's formula holds when you meet Taylor series.
Polar form makes multiplication easy:
Multiplying by rotates a point by around the origin. Multiplying by (which is ) rotates by 90°.
import cmath, math
z = 1 + 1j
r, theta = cmath.polar(z)
print(f"|z| = {r:.4f}, arg z = {theta:.4f} rad = {math.degrees(theta):.1f}°")
print("back from polar:", cmath.rect(r, theta))
print("e^(iπ) + 1 =", cmath.exp(1j * math.pi) + 1)
p = 2 + 0j # the point (2, 0)
for deg in [90, 180, 270, 45]:
q = p * cmath.exp(1j * math.radians(deg))
print(f"rotate (2, 0) by {deg:3}°: ({q.real:+.4f}, {q.imag:+.4f})")|z| = 1.4142, arg z = 0.7854 rad = 45.0°
back from polar: (1.0000000000000002+1j)
e^(iπ) + 1 = 1.2246467991473532e-16j
rotate (2, 0) by 90°: (+0.0000, +2.0000)
rotate (2, 0) by 180°: (-2.0000, +0.0000)
rotate (2, 0) by 270°: (-0.0000, -2.0000)
rotate (2, 0) by 45°: (+1.4142, +1.4142)
The tiny leftovers such as 1.2246…e-16j are floating-point rounding: mathematically is exactly 0.
Roots of polynomials
The fundamental theorem of algebra says that every polynomial of degree has exactly complex roots, counted with multiplicity. Complex numbers are what make root-finding complete. For real coefficients, non-real roots come in conjugate pairs.
The -th roots of unity, the solutions of , are for : points evenly spaced on the unit circle.
import sympy as sp
import numpy as np
z = sp.symbols('z')
print(sp.solve(z**2 + 4*z + 13, z))
print(sp.solve(z**4 - 1, z))
n = 6
roots = np.exp(2j * np.pi * np.arange(n) / n)
print(np.round(roots, 4))
print("all satisfy z^6 = 1:", np.allclose(roots**6, 1))[-2 - 3*I, -2 + 3*I]
[-1, 1, -I, I]
[ 1. +0.j 0.5+0.866j -0.5+0.866j -1. +0.j -0.5-0.866j 0.5-0.866j]
all satisfy z^6 = 1: True
In software
- 2D rotation: a rotation by is multiplication by . Quaternions extend the same idea to 3D rotation in game engines (Quaternions).
- The FFT splits a signal into frequencies by multiplying it with the roots of unity . It powers audio equalisers, MP3 and JPEG compression, and fast polynomial multiplication (SciPy FFT).
- Electrical engineering uses complex numbers (as
j) for AC circuits, where phase matters. - Fractals: the Mandelbrot set is the set of for which stays bounded.
- Rotary position embeddings (RoPE) in many LLMs rotate pairs of embedding dimensions by position-dependent angles, which is multiplication by .
Exercises
Exercise 1: Arithmetic by hand
With and , compute , and .
Answer
, , .
Solution
u, v = 2 + 3j, 4 - 1j
print(u + v, u * v, round(abs(v), 3))(6+2j) (11+10j) 4.123
Exercise 2: Divide
Compute .
Answer
Solution
Check: .
Exercise 3: Powers of
Simplify .
Answer
Solution
The powers repeat every 4, so only the remainder of matters: , so .
Exercise 4: Rotate a triangle
Rotate the triangle with corners , and by 90° counterclockwise about the origin using complex multiplication.
Answer
, , .
Solution
Multiplying by turns into , which is a 90° rotation.
corners = [1 + 0j, 3 + 0j, 2 + 2j]
print([(int(z.real), int(z.imag)) for z in (c * 1j for c in corners)])[(0, 1), (0, 3), (-2, 2)]
Exercise 5: Cube roots of unity
Find the three solutions of in the form and check that they add up to 0.
Answer
, , ; their sum is 0.
Solution
They are for , at angles 0°, 120° and 240°: , . The sum is .
import sympy as sp
z = sp.symbols('z')
roots = sp.solve(z**3 - 1, z)
print(roots, sp.simplify(sum(roots)))[1, -1/2 - sqrt(3)*I/2, -1/2 + sqrt(3)*I/2] 0
Test yourself
What is ?
- 2
- 0
Multiplying a complex number by does what to its point in the plane?
- Doubles its length
- Reflects it in the real axis
- Rotates it 60° counterclockwise about the origin
- Moves it units right
A polynomial with real coefficients has the root . Which other number must also be a root?