Complex Numbers
Implement complex numbers: their parts, arithmetic, absolute value, conjugate and exponential, and arithmetic between a complex number and a real one.
Adapted from Exercism’s problem specification (MIT). Listed under Numbers & Math.
// FILE: complex_numbers.nomi
//# Complex numbers and their arithmetic.
/// A complex number `real + imaginary * i`.
pub opaque struct Complex
real: Float
imaginary: Float
end
impl Complex
/// `real + imaginary * i`.
pub fn new(real: Float, imaginary: Float) -> Complex
{real:, imaginary:}
end
/// The real part of `z`.
pub fn real(z: Complex) -> Float
z.real
end
/// The imaginary part of `z`.
pub fn imaginary(z: Complex) -> Float
z.imaginary
end
/// The distance of `z` from zero, `sqrt(a^2 + b^2)`.
pub fn abs(z: Complex) -> Float
Float.sqrt(z.real * z.real + z.imaginary * z.imaginary)
end
/// `z` with its imaginary part negated.
pub fn conjugate(z: Complex) -> Complex
{..z, imaginary: -z.imaginary}
end
/// `e` to the power `z`: `e^a * (cos(b) + i * sin(b))`.
pub fn exp(z: Complex) -> Complex
scale = Float.exp(z.real)
{real: scale * Float.cos(z.imaginary), imaginary: scale * Float.sin(z.imaginary)}
end
/// Whether each part of `a` is within `tolerance` of the same part of
/// `b`.
pub fn equal_within(a: Complex, b: Complex, tolerance: NonNegativeFloat) -> Bool
Float.equal_within(a.real, b.real, tolerance)
and Float.equal_within(a.imaginary, b.imaginary, tolerance)
end
/// `x` as a complex number with no imaginary part.
pub fn from_real(x: Float) -> Complex
{real: x, imaginary: 0.0}
end
end
impl Add<Complex, Complex> for Complex
fn add(lhs: Complex, rhs: Complex) -> Complex
{real: lhs.real + rhs.real, imaginary: lhs.imaginary + rhs.imaginary}
end
end
impl Subtract<Complex, Complex> for Complex
fn subtract(lhs: Complex, rhs: Complex) -> Complex
{real: lhs.real - rhs.real, imaginary: lhs.imaginary - rhs.imaginary}
end
end
impl Multiply<Complex, Complex> for Complex
fn multiply(lhs: Complex, rhs: Complex) -> Complex
{
real: lhs.real * rhs.real - lhs.imaginary * rhs.imaginary,
imaginary: lhs.imaginary * rhs.real + lhs.real * rhs.imaginary,
}
end
end
impl Divide<Complex, Complex> for Complex
/// Dividing by zero gives infinities and NaNs, as `/` on `Float` does.
fn divide(lhs: Complex, rhs: Complex) -> Complex
denominator = rhs.real * rhs.real + rhs.imaginary * rhs.imaginary
{
real: (lhs.real * rhs.real + lhs.imaginary * rhs.imaginary) / denominator,
imaginary: (lhs.imaginary * rhs.real - lhs.real * rhs.imaginary) / denominator,
}
end
end
impl Add<Float, Complex> for Complex
fn add(lhs: Complex, rhs: Float) -> Complex
lhs + Complex.from_real(rhs)
end
end
impl Subtract<Float, Complex> for Complex
fn subtract(lhs: Complex, rhs: Float) -> Complex
lhs - Complex.from_real(rhs)
end
end
impl Multiply<Float, Complex> for Complex
fn multiply(lhs: Complex, rhs: Float) -> Complex
lhs * Complex.from_real(rhs)
end
end
impl Divide<Float, Complex> for Complex
fn divide(lhs: Complex, rhs: Float) -> Complex
lhs / Complex.from_real(rhs)
end
end
impl Add<Complex, Complex> for Float
fn add(lhs: Float, rhs: Complex) -> Complex
Complex.from_real(lhs) + rhs
end
end
impl Subtract<Complex, Complex> for Float
fn subtract(lhs: Float, rhs: Complex) -> Complex
Complex.from_real(lhs) - rhs
end
end
impl Multiply<Complex, Complex> for Float
fn multiply(lhs: Float, rhs: Complex) -> Complex
Complex.from_real(lhs) * rhs
end
end
impl Divide<Complex, Complex> for Float
fn divide(lhs: Float, rhs: Complex) -> Complex
Complex.from_real(lhs) / rhs
end
end
// FILE: complex_numbers_test.nomi
import complex_numbers.Complex
// The canonical data asks for a tolerance on results that are not whole
// numbers.
once tolerance: NonNegativeFloat = 1e-12
test "Real part"
[
("Real part of a purely real number", Complex.new(1.0, 0.0), 1.0),
("Real part of a purely imaginary number", Complex.new(0.0, 1.0), 0.0),
("Real part of a number with real and imaginary part", Complex.new(1.0, 2.0), 1.0),
]
|> subtests (description, z, expected), description
assert Complex.real(z) == expected
end
end
test "Imaginary part"
[
("Imaginary part of a purely real number", Complex.new(1.0, 0.0), 0.0),
("Imaginary part of a purely imaginary number", Complex.new(0.0, 1.0), 1.0),
("Imaginary part of a number with real and imaginary part", Complex.new(1.0, 2.0), 2.0),
]
|> subtests (description, z, expected), description
assert Complex.imaginary(z) == expected
end
end
test "Imaginary unit"
assert Complex.new(0.0, 1.0) * Complex.new(0.0, 1.0) == Complex.new(-1.0, 0.0)
end
group "Arithmetic"
test "Addition"
[
(
"Add purely real numbers",
Complex.new(1.0, 0.0),
Complex.new(2.0, 0.0),
Complex.new(3.0, 0.0),
),
(
"Add purely imaginary numbers",
Complex.new(0.0, 1.0),
Complex.new(0.0, 2.0),
Complex.new(0.0, 3.0),
),
(
"Add numbers with real and imaginary part",
Complex.new(1.0, 2.0),
Complex.new(3.0, 4.0),
Complex.new(4.0, 6.0),
),
]
|> subtests (description, z1, z2, expected), description
assert z1 + z2 == expected
end
end
test "Subtraction"
[
(
"Subtract purely real numbers",
Complex.new(1.0, 0.0),
Complex.new(2.0, 0.0),
Complex.new(-1.0, 0.0),
),
(
"Subtract purely imaginary numbers",
Complex.new(0.0, 1.0),
Complex.new(0.0, 2.0),
Complex.new(0.0, -1.0),
),
(
"Subtract numbers with real and imaginary part",
Complex.new(1.0, 2.0),
Complex.new(3.0, 4.0),
Complex.new(-2.0, -2.0),
),
]
|> subtests (description, z1, z2, expected), description
assert z1 - z2 == expected
end
end
test "Multiplication"
[
(
"Multiply purely real numbers",
Complex.new(1.0, 0.0),
Complex.new(2.0, 0.0),
Complex.new(2.0, 0.0),
),
(
"Multiply purely imaginary numbers",
Complex.new(0.0, 1.0),
Complex.new(0.0, 2.0),
Complex.new(-2.0, 0.0),
),
(
"Multiply numbers with real and imaginary part",
Complex.new(1.0, 2.0),
Complex.new(3.0, 4.0),
Complex.new(-5.0, 10.0),
),
]
|> subtests (description, z1, z2, expected), description
assert z1 * z2 == expected
end
end
test "Division"
[
(
"Divide purely real numbers",
Complex.new(1.0, 0.0),
Complex.new(2.0, 0.0),
Complex.new(0.5, 0.0),
),
(
"Divide purely imaginary numbers",
Complex.new(0.0, 1.0),
Complex.new(0.0, 2.0),
Complex.new(0.5, 0.0),
),
(
"Divide numbers with real and imaginary part",
Complex.new(1.0, 2.0),
Complex.new(3.0, 4.0),
Complex.new(0.44, 0.08),
),
]
|> subtests (description, z1, z2, expected), description
assert Complex.equal_within(z1 / z2, expected, tolerance)
end
end
end
test "Absolute value"
[
("Absolute value of a positive purely real number", Complex.new(5.0, 0.0), 5.0),
("Absolute value of a negative purely real number", Complex.new(-5.0, 0.0), 5.0),
(
"Absolute value of a purely imaginary number with positive imaginary part",
Complex.new(0.0, 5.0),
5.0,
),
(
"Absolute value of a purely imaginary number with negative imaginary part",
Complex.new(0.0, -5.0),
5.0,
),
("Absolute value of a number with real and imaginary part", Complex.new(3.0, 4.0), 5.0),
]
|> subtests (description, z, expected), description
assert Complex.abs(z) == expected
end
end
test "Complex conjugate"
[
("Conjugate a purely real number", Complex.new(5.0, 0.0), Complex.new(5.0, 0.0)),
("Conjugate a purely imaginary number", Complex.new(0.0, 5.0), Complex.new(0.0, -5.0)),
(
"Conjugate a number with real and imaginary part",
Complex.new(1.0, 1.0),
Complex.new(1.0, -1.0),
),
]
|> subtests (description, z, expected), description
assert Complex.conjugate(z) == expected
end
end
test "Complex exponential function"
[
("Euler's identity/formula", Complex.new(0.0, Float.pi), Complex.new(-1.0, 0.0)),
("Exponential of 0", Complex.new(0.0, 0.0), Complex.new(1.0, 0.0)),
("Exponential of a purely real number", Complex.new(1.0, 0.0), Complex.new(Float.e, 0.0)),
(
"Exponential of a number with real and imaginary part",
Complex.new(Float.log(2.0), Float.pi),
Complex.new(-2.0, 0.0),
),
(
"Exponential resulting in a number with real and imaginary part",
Complex.new(Float.log(2.0) / 2.0, Float.pi / 4.0),
Complex.new(1.0, 1.0),
),
]
|> subtests (description, z, expected), description
assert z |> Complex.exp() |> Complex.equal_within(expected, tolerance)
end
end
test "Operations between real numbers and complex numbers"
[
("Add real number to complex number", Complex.new(1.0, 2.0) + 5.0, Complex.new(6.0, 2.0)),
("Add complex number to real number", 5.0 + Complex.new(1.0, 2.0), Complex.new(6.0, 2.0)),
(
"Subtract real number from complex number",
Complex.new(5.0, 7.0) - 4.0,
Complex.new(1.0, 7.0),
),
(
"Subtract complex number from real number",
4.0 - Complex.new(5.0, 7.0),
Complex.new(-1.0, -7.0),
),
(
"Multiply complex number by real number",
Complex.new(2.0, 5.0) * 5.0,
Complex.new(10.0, 25.0),
),
(
"Multiply real number by complex number",
5.0 * Complex.new(2.0, 5.0),
Complex.new(10.0, 25.0),
),
(
"Divide complex number by real number",
Complex.new(10.0, 100.0) / 10.0,
Complex.new(1.0, 10.0),
),
(
"Divide real number by complex number",
5.0 / Complex.new(1.0, 1.0),
Complex.new(2.5, -2.5),
),
]
|> subtests (description, actual, expected), description
assert Complex.equal_within(actual, expected, tolerance)
end
endok Real part (3 cases)
ok Imaginary part (3 cases)
ok Imaginary unit
ok Arithmetic / Addition (3 cases)
ok Arithmetic / Subtraction (3 cases)
ok Arithmetic / Multiplication (3 cases)
ok Arithmetic / Division (3 cases)
ok Absolute value (5 cases)
ok Complex conjugate (3 cases)
ok Complex exponential function (5 cases)
ok Operations between real numbers and complex numbers (8 cases)
test result: ok. 40 passed, 0 failed
How it’s written
Section titled “How it’s written”Complexis an opaque struct of twoFloats, built withComplex.new(real, imaginary)and read withComplex.realandComplex.imaginary.==compares both parts exactly, as it compares any struct, which the cases with whole-number results use.- The arithmetic is the operator interfaces, as rational-numbers does it:
impl Add<Complex, Complex> for Complexand the same forSubtract,MultiplyandDivide, so the tests writez1 + z2. - The canonical cases that mix a real number with a complex one are four more impls on
Complexwith aFloatright-hand side and four onFloatwith aComplexright-hand side, soComplex.new(1.0, 2.0) + 5.0and5.0 + Complex.new(1.0, 2.0)both work. Each lifts theFloatwithComplex.from_realand uses the complex operator. expise^a * (cos(b) + i * sin(b))from std::float’sexp,cosandsin.- The canonical data asks for a tolerance on results that are not whole numbers.
Complex.equal_withinholds both parts to it throughFloat.equal_within, and the division, exponential and mixed cases use it with1e-12. The tolerance is aNonNegativeFloat, asFloat.equal_withintakes it, so the tests’onceis annotated with it and its literal is checked when the program is compiled. - The canonical inputs write pi and e as the strings
"pi"and"e", and ln(2) as"ln(2)". The tests write std::float’sFloat.piandFloat.e, andFloat.log(2.0). - Each canonical group is a test with one
subteststable; the four arithmetic tests sit in agroup "Arithmetic", as the canonical data nests them. The mixed cases are one table whose actual column is the expression itself (5.0 * Complex.new(2.0, 5.0)), since the rows mix operand types.