Mastering Complex Roots of Unity with Python
Complex numbers can look intimidating at first, especially when equations involve imaginary values. However, Python makes it much easier to experiment with complex mathematics and understand what is happening behind the equations.
One particularly interesting topic is roots of unity. They connect complex numbers with geometry, trigonometry, algebra, and computer science.
In this tutorial, we will learn what roots of unity are, understand the formulas in simple copyable form, and create Python programs to calculate, verify, and visualize them.
What Are Roots of Unity?
A root of unity is a complex number that satisfies an equation of the form:
z^n = 1
Here, n is a positive integer.
For example, consider:
z^2 = 1
The two solutions are:
z = 1
z = -1
Therefore, 1 and -1 are the second roots of unity.
If we consider:
z^3 = 1
there are three solutions. One is the familiar number 1, while the other two are complex numbers.
In general, the equation:
z^n = 1
has exactly n different complex roots.
The Formula for Roots of Unity
The roots can be calculated using the following formula:
z_k = e^(2*pi*i*k/n)
where:
k = 0, 1, 2, ..., n-1
Using Euler's formula, the same expression can be written as:
z_k = cos(2*pi*k/n) + i*sin(2*pi*k/n)
This second form is particularly useful when we want to understand the real and imaginary parts of each root.
Here:
i = sqrt(-1)
Python uses j instead of i for the imaginary unit.
Understanding the Unit Circle
Roots of unity have an interesting geometric property.
Every root lies on a circle with radius 1, called the unit circle.
The general form is:
z = cos(theta) + i*sin(theta)
The distance of this point from the origin is always 1.
The roots are evenly distributed around the circle. This means that if there are 4 roots, they form a square. If there are 5 roots, they form a pentagon. If there are 8 roots, they form an octagon.
This gives us a beautiful connection between algebra and geometry.
Complex Numbers in Python
Python supports complex numbers directly.
For example:
z = 3 + 4j
print(z)
Output:
(3+4j)
Python uses j instead of the mathematical i.
You can also access the real and imaginary parts:
z = 3 + 4j
print("Real part:", z.real)
print("Imaginary part:", z.imag)
Output:
Real part: 3.0
Imaginary part: 4.0
This built-in support makes Python convenient for experimenting with roots of unity.
Calculating Roots of Unity with Python
Python's cmath module provides mathematical functions for complex numbers.
Here is a simple program:
import cmath
import math
n = 5
for k in range(n):
angle = 2 * math.pi * k / n
root = cmath.exp(1j * angle)
print(root)
This program calculates the five roots satisfying:
z^5 = 1
The values may be displayed as decimal approximations because computers work with floating-point numbers.
Calculating Roots Using Sine and Cosine
We can also implement the mathematical formula directly.
import math
def roots_of_unity(n):
roots = []
for k in range(n):
angle = 2 * math.pi * k / n
real = math.cos(angle)
imaginary = math.sin(angle)
root = complex(real, imaginary)
roots.append(root)
return roots
roots = roots_of_unity(6)
for root in roots:
print(root)
This approach is useful because it clearly shows the relationship between the mathematical formula and the Python program.
The important part is:
angle = 2*pi*k/n
Then:
real = cos(angle)
imaginary = sin(angle)
Finally, Python combines the two components into a complex number.
Example: Cube Roots of Unity
Let's solve:
z^3 = 1
There are three roots.
The formula is:
z_k = e^(2*pi*i*k/3)
For the three values of k:
k = 0, 1, 2
the roots are:
z0 = 1
z1 = -1/2 + (sqrt(3)/2)i
z2 = -1/2 - (sqrt(3)/2)i
Python can calculate them:
import cmath
import math
n = 3
for k in range(n):
angle = 2 * math.pi * k / n
root = cmath.exp(1j * angle)
print(root)
The output will be decimal approximations of the three roots.
Verifying the Roots
We can ask Python to check whether each calculated root actually satisfies:
z^n = 1
For example:
import cmath
import math
n = 5
for k in range(n):
angle = 2 * math.pi * k / n
root = cmath.exp(1j * angle)
result = root ** n
print("Root:", root)
print("Root raised to n:", result)
print()
The second result should be extremely close to:
1 + 0j
This confirms that the calculated values are roots of the equation.
Why Does Python Sometimes Show Tiny Errors?
You might see something like:
(1-2.4492935982947064e-16j)
instead of:
1+0j
This is caused by floating-point precision.
Mathematically:
2.4492935982947064e-16
is extremely close to zero.
Therefore, we should not normally compare floating-point complex numbers using an exact equality test.
Instead, we can use a small tolerance:
if abs(root ** n - 1) < 1e-10:
print("Valid root")
This allows for tiny numerical errors.
Finding a Specific Root
Sometimes we don't need all the roots. We may want a particular root.
We can create a function:
import cmath
import math
def find_root(n, k):
angle = 2 * math.pi * k / n
return cmath.exp(1j * angle)
root = find_root(10, 3)
print(root)
Here:
n = 10
means we are calculating tenth roots of unity.
The value:
k = 3
selects the fourth root in the sequence because Python starts counting from zero.
Building an Interactive Root Calculator
We can turn the idea into a small Python project.
import cmath
import math
def calculate_roots(n):
if n <= 0:
raise ValueError("n must be positive")
roots = []
for k in range(n):
angle = 2 * math.pi * k / n
root = cmath.exp(1j * angle)
roots.append(root)
return roots
n = int(input("Enter the value of n: "))
roots = calculate_roots(n)
print(f"\nRoots of z^{n} = 1\n")
for index, root in enumerate(roots):
print(f"Root {index}: {root}")
If the user enters:
Enter the value of n: 4
the program calculates the four roots of:
z^4 = 1
These are:
1
i
-1
-i
Understanding the Fourth Roots
The fourth roots of unity are particularly easy to visualize.
The equation is:
z^4 = 1
The four solutions are:
z0 = 1
z1 = i
z2 = -1
z3 = -i
They are positioned at 90-degree intervals around the unit circle.
The angles are:
0 degrees
90 degrees
180 degrees
270 degrees
This creates a square.
Visualizing Roots with Python
A graph can make the concept much easier to understand.
We can use Matplotlib to plot the roots.
First install it if necessary:
pip install matplotlib
Then use:
import cmath
import math
import matplotlib.pyplot as plt
n = 8
roots = [
cmath.exp(2j * math.pi * k / n)
for k in range(n)
]
x = [root.real for root in roots]
y = [root.imag for root in roots]
plt.scatter(x, y)
plt.axhline(0)
plt.axvline(0)
plt.xlabel("Real")
plt.ylabel("Imaginary")
plt.title(f"{n}th Roots of Unity")
plt.axis("equal")
plt.grid(True)
plt.show()
For:
n = 8
the eight points form a regular octagon.
Try changing the value:
n = 3
You will get a triangle.
Try:
n = 6
and you will get a hexagon.
A Useful Property of Roots of Unity
There is another elegant way to represent the roots.
Let:
omega = e^(2*pi*i/n)
Then all the roots can be represented as:
1
omega
omega^2
omega^3
...
omega^(n-1)
The most important property is:
omega^n = 1
This means that after raising omega to the nth power, we return to 1.
Python can demonstrate this:
import cmath
import math
n = 5
omega = cmath.exp(2j * math.pi / n)
for k in range(n + 1):
print(k, omega ** k)
The final value will be approximately 1.
Calculating the Magnitude
Every root of unity has magnitude 1.
Python allows us to calculate the magnitude using abs().
import cmath
import math
n = 6
for k in range(n):
angle = 2 * math.pi * k / n
root = cmath.exp(1j * angle)
print("Root:", root)
print("Magnitude:", abs(root))
The magnitude should be approximately:
1.0
for every root.
This confirms the geometric fact that all the roots lie on the unit circle.
Calculating the Angle
Python's cmath.phase() function can be used to find the angle of a complex number.
import cmath
z = 1 + 1j
angle = cmath.phase(z)
print("Angle in radians:", angle)
The result is expressed in radians.
For roots of unity, these angles are evenly distributed around the circle.
Applications of Roots of Unity
Roots of unity have applications far beyond classroom mathematics.
Fourier Analysis
Roots of unity are fundamental to the Discrete Fourier Transform, which is used to break signals into their frequency components.
The Fast Fourier Transform, commonly known as FFT, makes these calculations much faster and is widely used in computing.
Digital Signal Processing
Audio, images, communication systems, and other digital signals can be analyzed using Fourier-based methods.
Computer Graphics
Complex numbers can represent rotations in two-dimensional mathematics. This makes them useful in certain graphics and geometric calculations.
Polynomial Equations
Roots of unity provide useful examples for understanding complex polynomial equations and their solutions.
Number Theory
Roots of unity also appear in advanced topics involving algebra, modular arithmetic, and number theory.
Complete Python Project
Here is a complete version that calculates and verifies the roots:
import cmath
import math
def roots_of_unity(n):
roots = []
for k in range(n):
angle = 2 * math.pi * k / n
root = cmath.exp(1j * angle)
roots.append(root)
return roots
n = int(input("Enter a positive integer: "))
if n <= 0:
print("Please enter a positive integer.")
else:
roots = roots_of_unity(n)
print(f"\nRoots of z^{n} = 1\n")
for k, root in enumerate(roots):
result = root ** n
print(f"Root {k}: {root}")
print(f"Verification: {result}")
print()
This small project combines several important Python concepts:
- Functions
- Lists
- Loops
- User input
- Complex numbers
- Mathematical calculations
- Error checking
- Numerical verification
Final Thoughts
Complex roots of unity are a great example of how programming can make advanced mathematics easier to explore.
The central equation is:
z^n = 1
and the general solution is:
z_k = e^(2*pi*i*k/n)
or, using sine and cosine:
z_k = cos(2*pi*k/n) + i*sin(2*pi*k/n)
Python allows us to calculate these values, verify that they satisfy the original equation, and plot them on the unit circle.
The most interesting part is the connection between algebra and geometry. Although the roots come from solving an equation, they form a perfectly regular polygon when plotted.
Once you are comfortable with roots of unity, you can go further into complex-number programming, Fourier transforms, FFT algorithms, signal processing, and other areas where mathematics and Python come together.