Julia Complex & Rational Numbers
Many languages need a library for complex numbers or exact fractions. In Julia both are built in and work with every arithmetic operator and most maths functions.
Complex numbers
The global constant im is the imaginary unit √−1. Write a complex number as a + b*im, or with juxtaposition, a + bim:
z = 3 + 4im
w = 1 - 2im
println(z)
println(typeof(z))
println(z + w, " ", z * w, " ", z / w)
println(real(z), " ", imag(z))
println(abs(z), " ", conj(z))
println(im^2)3 + 4im
Complex{Int64}
4 + 2im 11 - 2im -1.0 + 2.0im
3 4
5.0 3 - 4im
-1 + 0im
| Function | Meaning |
|---|---|
real(z), imag(z) |
real and imaginary parts |
abs(z) |
modulus (distance from 0) |
angle(z) |
argument (angle in radians) |
conj(z) |
complex conjugate |
complex(a, b) |
build a + b*im |
cis(θ) |
cos(θ) + im*sin(θ) |
Square roots of negative real numbers are an error unless you ask for a complex result — Julia never silently changes the type of a result based on a value:
try
sqrt(-4)
catch e
println(sprint(showerror, e))
end
println(sqrt(-4 + 0im))
println(sqrt(complex(-9)))DomainError with -4.0:
sqrt was called with a negative real argument but will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).
0.0 + 2.0im
0.0 + 3.0im
Euler's formula
Rotation by an angle θ is multiplication by cis(θ). Rounding hides the tiny floating-point errors:
θ = π / 2
r = cis(θ) # e^(iθ)
println(round(r, digits=10))
point = 2 + 0im
rotated = point * cis(π / 2) # rotate 90° counter-clockwise
println(round(rotated, digits=10))
println(round(exp(im * π) + 1, digits=10)) # Euler's identity
println(round(rad2deg(angle(1 + 1im))))0.0 + 1.0im
0.0 + 2.0im
0.0 + 0.0im
45.0
Rational numbers
The // operator creates an exact fraction. Rationals are automatically reduced to lowest terms, and arithmetic on them never loses precision:
a = 1 // 3
b = 2 // 6
println(a, " ", b, " ", a == b)
println(typeof(a))
println(a + 1 // 6)
println(3 // 4 * 2)
println(numerator(10 // 4), " ", denominator(10 // 4))
println(float(1 // 8))
println(1 // 10 + 2 // 10 == 3 // 10)1//3 1//3 true
Rational{Int64}
1//2
3//2
5 2
0.125
true
The last line contrasts nicely with floats, where 0.1 + 0.2 == 0.3 is false. Rationals are slower than floats and their numerator and denominator can overflow, so use them where exactness matters, for example probabilities in a board game or exact recipe scaling.
# Exact harmonic numbers: 1 + 1/2 + 1/3 + ... + 1/n
H(n) = sum(1 // k for k in 1:n)
for n in 1:5
println("H(", n, ") = ", H(n))
end
println(rationalize(0.75))H(1) = 1//1
H(2) = 3//2
H(3) = 11//6
H(4) = 25//12
H(5) = 137//60
3//4
rationalize finds a fraction close to a float. Mixing a rational with a float gives a float, while mixing it with an integer stays exact.
Test yourself
What is (1 + 2im) * im?
- 1 + 2im
- -2 + 1im
- 2 - 1im
- -1 - 2im
What does 6 // 8 display as?
- 6//8
- 3//4
- 0.75
- 0
Why does sqrt(-1) throw a DomainError?
- Julia has no complex numbers
- The result type would depend on the value, so you must pass a complex argument to get a complex result
- Negative numbers have no square roots
- It's a bug