Complex Numbers
Work with complex arithmetic, polar form, and transcendental functions
Complex numbers extend the real numbers with an imaginary unit where . They appear throughout science and engineering — from electrical impedance to quantum mechanics to signal processing. Equana has native complex number support with a familiar syntax.
This tutorial covers how to create complex numbers, perform arithmetic, work with polar form, and use transcendental functions on complex inputs.
Creating Complex Numbers
Use the im suffix to write complex literals. The imaginary unit itself is just im:
# Complex literals with the im suffix
z1 = 3 + 4im
z2 = 1 - 2im
z3 = 5im # Pure imaginary
# The imaginary unit
im
# Floating-point components
z4 = 0.5 + 0.25imYou can also use the complex constructor, which is useful when building complex numbers from variables:
# Construct from two components
z = complex(3, 4)
# Real number as complex (imaginary part = 0)
w = complex(5)
# Access components directly
z.re
z.imArithmetic
All standard arithmetic operators work with complex numbers. The rules follow from :
a = 1 + 2im
b = 3 + 4im
# Addition and subtraction
a + b # 4 + 6im
a - b # -2 - 2im
# Multiplication: (1+2i)(3+4i) = 3+4i+6i+8i² = 3+10i-8 = -5+10i
a * b
# Division
a / b
# Exponentiation
a ^ 2 # (1+2i)² = 1+4i-4 = -3+4iMixed operations between real and complex numbers work naturally:
z = 2 + 3im
# Scalar multiplication
3 * z # 6 + 9im
# Add a real number
z + 10 # 12 + 3im
# Real division
z / 2 # 1 + 1.5imComponent Extraction
Extract the real and imaginary parts with real() and imag(), and compute the complex conjugate with conj():
z = 3 + 4im
# Extract components
real(z) # 3.0
imag(z) # 4.0
# Complex conjugate (flips the sign of the imaginary part)
conj(z) # 3 - 4im
# Conjugate of a conjugate returns the original
conj(conj(z)) # 3 + 4imThese functions also work on real numbers — real returns the value itself, imag returns zero, and conj is the identity:
# Real number passthrough
real(5) # 5
imag(5) # 0
conj(7) # 7Magnitude and Phase
Every complex number can also be written in polar form as , where is the magnitude and is the phase angle.
# The classic 3-4-5 right triangle
z = 3 + 4im
# Magnitude: sqrt(3² + 4²) = sqrt(25) = 5
abs(z)
# Squared magnitude (avoids the square root — faster)
abs2(z) # 25
# Phase angle in radians
angle(z) # ≈ 0.9273 (arctan(4/3))Some useful magnitude identities:
z = 2 + 3im
# |z|² = z * conj(z) — the imaginary part cancels out
z * conj(z)
# Compare with abs2
abs2(z)
# |z| is always non-negative
abs(-5 + 12im) # 13 (another Pythagorean triple)Euler's Formula and cis
Euler's formula states that . The cis function computes this directly — its name stands for cos + i sin:
# cis at special angles
cis(0) # 1 + 0im (angle = 0)
cis(pi / 2) # ≈ 0 + 1im (angle = π/2)
cis(pi) # ≈ -1 + 0im (angle = π)
# Every output lies on the unit circle: |cis(θ)| = 1
abs(cis(1.5))
# Round-trip: angle(cis(θ)) ≈ θ
angle(cis(0.7))Euler's identity — the famous equation connects five fundamental constants. We'll verify it in the next section.
Transcendental Functions
The exponential, logarithm, and square root extend naturally to complex inputs:
# Euler's identity: exp(iπ) ≈ -1
exp(im * pi)
# Complex exponential
exp(1 + 2im)
# Complex logarithm (principal value)
log(1 + im)
# Complex square root
sqrt(complex(-1, 0)) # im
sqrt(1 + 2im)Complex Trigonometry
Trigonometric and hyperbolic functions accept complex arguments. For complex inputs, they are defined through the exponential function:
# Trig functions on complex inputs
sin(1 + 2im)
cos(1 + 2im)
tan(1 + im)
# Hyperbolic functions
sinh(1 + im)
cosh(1 + im)
tanh(1 + im)A useful identity: — trig and hyperbolic functions are related by a rotation in the complex plane:
z = 0.5 + 0.3im
# sin(iz) should equal i*sinh(z)
sin(im * z)
im * sinh(z)Type Queries
Check properties of complex values with type query functions:
z = 2 + 3im
typeof(z) # Complex128
isreal(z) # false
isreal(2 + 0im) # true (imaginary part is zero)Extended Example: Roots of Unity
The th roots of unity are the complex numbers satisfying . They are evenly spaced around the unit circle at angles for .
# Compute the 5th roots of unity using map
n = 5
roots = map(0:(n-1), k -> cis(2 * pi * k / n))
# Each root lies on the unit circle
magnitudes = map(roots, r -> abs(r))
magnitudes# The roots sum to zero (a fundamental property)
n = 5
roots = map(0:(n-1), k -> cis(2 * pi * k / n))
# Sum the roots manually
total = reduce(roots, (a, b) -> a + b, complex(0, 0))
abs(total) # ≈ 0 (within floating-point precision)Verify that each root, when raised to the th power, equals 1:
# z^n should equal 1 for every root of unity
n = 5
roots = map(0:(n-1), k -> cis(2 * pi * k / n))
powers = map(roots, r -> r ^ n)
# All should be ≈ 1 + 0im
powersSummary
Key Concepts
- Complex literals — Write
3 + 4imor usecomplex(3, 4) - Arithmetic — All operators (
+,-,*,/,^) work with complex numbers - Components — Extract parts with
real(),imag(),conj() - Polar form — Magnitude with
abs(), phase withangle() - Euler's formula —
cis(θ)computes - Transcendentals —
exp,log,sqrt, trig, and hyperbolic functions all accept complex inputs
Function Reference
| Function | Description | Example |
|---|---|---|
complex(a, b) | Construct complex number | complex(3, 4) |
real(z) | Real part | real(3+4im) → 3.0 |
imag(z) | Imaginary part | imag(3+4im) → 4.0 |
conj(z) | Complex conjugate | conj(3+4im) → 3-4im |
abs(z) | Magnitude | abs(3+4im) → 5.0 |
abs2(z) | Squared magnitude | abs2(3+4im) → 25 |
angle(z) | Phase angle (radians) | angle(1+im) → ≈ 0.785 |
cis(θ) | cis(pi) → ≈ -1+0im | |
exp(z) | Complex exponential | exp(im*pi) → ≈ -1 |
log(z) | Complex logarithm | log(1+im) |
sqrt(z) | Complex square root | sqrt(complex(-1,0)) → im |
sin(z), cos(z), tan(z) | Complex trig | sin(1+2im) |
sinh(z), cosh(z), tanh(z) | Complex hyperbolic | sinh(1+im) |
isreal(z) | Check if imaginary part is zero | isreal(2+0im) → true |
Further Reading
- Explore the matrices tutorial for linear algebra with complex entries
- Use the notebook to experiment with complex arithmetic interactively