Complex

The Complex module provides complex number operations (numbers with real and imaginary parts).

Creation

Complex.new
Float -> Float -> Complex
Creates a complex number from real and imaginary parts.
c = Complex.new 3.0 4.0
println (Complex.to-string c)
# => "3+4i"
Complex.from-real
Float -> Complex
Creates a complex from a real number (imaginary = 0).
Complex.from-imaginary
Float -> Complex
Creates a complex from an imaginary number (real = 0).
Complex.from-polar
Float -> Float -> Complex
Creates from polar coordinates (magnitude, phase in radians).
Complex.parse
String -> Option Complex
Parses a string like "3+4i" or "5i".
Complex.i
() -> Complex
Returns the imaginary unit (0+1i).

Accessors

Complex.real
Complex -> Float
Returns the real part.
Complex.imag
Complex -> Float
Returns the imaginary part.

Arithmetic

Complex.add
Complex -> Complex -> Complex
Adds two complex numbers.
Complex.subtract
Complex -> Complex -> Complex
Subtracts two complex numbers.
Complex.multiply
Complex -> Complex -> Complex
Multiplies two complex numbers.
Complex.divide
Complex -> Complex -> Option Complex
Divides two complex numbers. Returns None for division by zero.
Complex.negate
Complex -> Complex
Negates (changes sign of both parts).
Complex.conjugate
Complex -> Complex
Returns the complex conjugate (negates imaginary part).
Complex.reciprocal
Complex -> Option Complex
Returns 1/z. Returns None for zero.
Complex.scale
Complex -> Float -> Complex
Multiplies by a real scalar.

Properties

Complex.magnitude
Complex -> Float
Returns the magnitude (absolute value).
c = Complex.new 3.0 4.0
println (Complex.magnitude c)
# => 5.0
Complex.magnitude-squared
Complex -> Float
Returns magnitude squared (avoids sqrt).
Complex.phase
Complex -> Float
Returns the phase angle in radians.

Predicates

Complex.zero?
Complex -> Bool
Returns true if zero.
Complex.real?
Complex -> Bool
Returns true if purely real (imag = 0).
Complex.imaginary?
Complex -> Bool
Returns true if purely imaginary (real = 0).
Complex.eq?
Complex -> Complex -> Bool
Tests equality.

Math Functions

Complex.exp
Complex -> Complex
Complex exponential (e^z).
Complex.log
Complex -> Option Complex
Complex natural logarithm.
Complex.sqrt
Complex -> Complex
Complex square root.
Complex.pow
Complex -> Complex -> Option Complex
Raises to a complex power.
Complex.pow-int
Complex -> Int -> Complex
Raises to an integer power.
Complex.sin
Complex -> Complex
Complex sine.
Complex.cos
Complex -> Complex
Complex cosine.
Complex.tan
Complex -> Option Complex
Complex tangent.

Conversion

Complex.to-polar
Complex -> {magnitude, phase}
Converts to polar form.
Complex.to-string
Complex -> String
Converts to string like "3+4i".