Equana

Back to Tutorials

Complex Numbers

Work with complex arithmetic, polar form, and transcendental functions

Run AllReset

Complex numbers extend the real numbers with an imaginary unit ii where i2=1i^2 = -1. 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:

Code [1]Run
# 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.25im

You can also use the complex constructor, which is useful when building complex numbers from variables:

Code [2]Run
# Construct from two components
z = complex(3, 4)

# Real number as complex (imaginary part = 0)
w = complex(5)

# Access components directly
z.re
z.im

Arithmetic

All standard arithmetic operators work with complex numbers. The rules follow from i2=1i^2 = -1:

Code [3]Run
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+4i

Mixed operations between real and complex numbers work naturally:

Code [4]Run
z = 2 + 3im

# Scalar multiplication
3 * z          # 6 + 9im

# Add a real number
z + 10         # 12 + 3im

# Real division
z / 2          # 1 + 1.5im

Component Extraction

Extract the real and imaginary parts with real() and imag(), and compute the complex conjugate with conj():

Code [5]Run
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 + 4im

These functions also work on real numbers — real returns the value itself, imag returns zero, and conj is the identity:

Code [6]Run
# Real number passthrough
real(5)        # 5
imag(5)        # 0
conj(7)        # 7

Magnitude and Phase

Every complex number z=a+biz = a + bi can also be written in polar form as z=reiθz = r \cdot e^{i\theta}, where r=zr = |z| is the magnitude and θ\theta is the phase angle.

Code [7]Run
# 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:

Code [8]Run
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 eiθ=cosθ+isinθe^{i\theta} = \cos\theta + i\sin\theta. The cis function computes this directly — its name stands for cos + i sin:

Code [9]Run
# 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 eiπ+1=0e^{i\pi} + 1 = 0 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:

Code [10]Run
# 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:

sin(z)=eizeiz2i,cos(z)=eiz+eiz2\sin(z) = \frac{e^{iz} - e^{-iz}}{2i}, \qquad \cos(z) = \frac{e^{iz} + e^{-iz}}{2}

Code [11]Run
# 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: sin(iz)=isinh(z)\sin(iz) = i\sinh(z) — trig and hyperbolic functions are related by a rotation in the complex plane:

Code [12]Run
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:

Code [13]Run
z = 2 + 3im

typeof(z)          # Complex128
isreal(z)          # false
isreal(2 + 0im)    # true (imaginary part is zero)

Extended Example: Roots of Unity

The nnth roots of unity are the nn complex numbers zz satisfying zn=1z^n = 1. They are evenly spaced around the unit circle at angles 2πkn\frac{2\pi k}{n} for k=0,1,,n1k = 0, 1, \ldots, n-1.

Code [14]Run
# 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
Code [15]Run
# 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 nnth power, equals 1:

Code [16]Run
# 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
powers

Summary

Key Concepts

  • Complex literals — Write 3 + 4im or use complex(3, 4)
  • Arithmetic — All operators (+, -, *, /, ^) work with complex numbers
  • Components — Extract parts with real(), imag(), conj()
  • Polar form — Magnitude with abs(), phase with angle()
  • Euler's formulacis(θ) computes cosθ+isinθ\cos\theta + i\sin\theta
  • Transcendentalsexp, log, sqrt, trig, and hyperbolic functions all accept complex inputs

Function Reference

FunctionDescriptionExample
complex(a, b)Construct complex numbercomplex(3, 4)
real(z)Real partreal(3+4im)3.0
imag(z)Imaginary partimag(3+4im)4.0
conj(z)Complex conjugateconj(3+4im)3-4im
abs(z)Magnitudeabs(3+4im)5.0
abs2(z)Squared magnitudeabs2(3+4im)25
angle(z)Phase angle (radians)angle(1+im)≈ 0.785
cis(θ)cosθ+isinθ\cos\theta + i\sin\thetacis(pi)≈ -1+0im
exp(z)Complex exponentialexp(im*pi)≈ -1
log(z)Complex logarithmlog(1+im)
sqrt(z)Complex square rootsqrt(complex(-1,0))im
sin(z), cos(z), tan(z)Complex trigsin(1+2im)
sinh(z), cosh(z), tanh(z)Complex hyperbolicsinh(1+im)
isreal(z)Check if imaginary part is zeroisreal(2+0im)true

Further Reading

  • Explore the matrices tutorial for linear algebra with complex entries
  • Use the notebook to experiment with complex arithmetic interactively

Workbench

Clear
No variables in workbench

Next Steps