Skip to main content

Complex

structure Complex {
re: Real
im: Real
}

Complex numbers consist of a real part and an imaginary part. They extend the real numbers and satisfy the equation i² = -1.

GitHub


abs_squared

define abs_squared(self) -> Real {
self.re * self.re + self.im * self.im
}

Computes the squared magnitude |z|² = re² + im².

conj

define conj(self) -> Complex {
Complex.new(self.re, -self.im)
}

Yields the complex conjugate (negates the imaginary part).

div

define div(self, other: Complex) -> Complex {
self * other.inverse
}

Divides this complex number by another. Division by zero returns zero (making this a total function).

from_real

let from_real: Real -> Complex = function(r: Real) {
Complex.new(r, Real.0)
}

Converts a real number to a complex number (with zero imaginary part).

i

let i: Complex = Complex.new(Real.0, Real.1)

The imaginary unit, satisfying i² = -1.

im

Complex.im: Complex -> Real

The imaginary part of the complex number.

is_imaginary

define is_imaginary(self) -> Bool {
self.re = Real.0 and self.im != Real.0
}

True if this complex number is purely imaginary (no real component).

is_real

define is_real(self) -> Bool {
self.im = Real.0
}

True if this complex number has no imaginary component.

re

Complex.re: Complex -> Real

The real part of the complex number.