kex docs Standard Library

Complex

module Complex

Complex numbers with real and imaginary components, plus polar conversion.

Components accept Number values. Trigonometric angles are in radians.

using Complex
let z = Rectangular { re: 3, im: 4 }
z.abs   # => 5.0

ZERO constant

The additive identity, +0 + 0i+.

ONE constant

The multiplicative identity, +1 + 0i+.

I constant

The imaginary unit, +0 + 1i+.

NEG_I constant

The negative imaginary unit, 0 - 1i.

fromPolar

fromPolar(radius: Number, theta: Number) -> Rectangular

Constructs a complex number from a radius and an angle in radians.

Parameters
radius
distance from the origin
theta
angle in radians

Returns the rectangular representation

Examples

A quarter turn

Complex.fromPolar(2, Math.PI / 2).im   # approximately 2.0

fromPolarCoordinates

fromPolarCoordinates(polar: Polar) -> Rectangular

Constructs a complex number from a Polar value.

Parameters
polar
radius and angle in radians

Returns the rectangular representation

Examples
Complex.fromPolarCoordinates(Polar { radius: 3, theta: 0 }).re # => 3.0

conjugateOf

conjugateOf(value: Rectangular) -> Rectangular

Returns the conjugate of a complex number.

Parameters
value
the number to conjugate

Returns the number with its imaginary sign reversed

Examples
Complex.conjugateOf(Complex.I).im # => -1

record Rectangular

A complex number in rectangular form: re is the real part and im is the imaginary part.

Fields

Implements Showable.

Functions

showValue Showable

showValue : String

Renders the number in rectangular form, such as +3+4i+.

Returns the real and signed imaginary components

Examples
(Rectangular { re: 3, im: -4 }).showValue # => "3-4i"

+

+(other: Rectangular) -> Rectangular

Adds two complex numbers component by component.

Examples
(Rectangular { re: 1, im: 2 } + Complex.I).im # => 3

-

-(other: Rectangular) -> Rectangular

Subtracts other from this complex number.

Examples
(Rectangular { re: 1, im: 2 } - Complex.I).im # => 1

*

*(other: Rectangular) -> Rectangular

Multiplies two complex numbers.

Examples
(Complex.I * Complex.I).re # => -1

/

/(other: Rectangular) -> Rectangular

Divides by other using the complex conjugate formula. The divisor must be non-zero.

Examples
(Complex.I / Complex.I).re # => 1.0

conjugate

conjugate : Rectangular

Returns the complex conjugate, negating the imaginary part.

Examples
let conjugated: Rectangular = Complex.conjugateOf(Complex.I)
conjugated.im # => -1

sqrt

sqrt : Rectangular

Returns the principal square root. The result's argument lies in the right half-plane.

Examples
(Rectangular { re: -1, im: 0 }).sqrt.im # approximately 1.0

exp

exp : Rectangular

Returns e raised to this complex number.

Examples
Complex.ZERO.exp.re # => 1.0

log

log : Rectangular

Returns the principal natural logarithm. Zero has no logarithm.

Examples
Complex.ONE.log.re # => 0.0

sin

sin : Rectangular

Returns the complex sine.

Examples
Complex.ZERO.sin.re # => 0.0

cos

cos : Rectangular

Returns the complex cosine.

Examples
Complex.ZERO.cos.re # => 1.0

tan

tan : Rectangular

Returns the complex tangent.

Examples
Complex.ZERO.tan.re # => 0.0

^

^(exponent: Number) -> Rectangular

Raises this number to a real power using the principal argument.

Examples
(Complex.I ^ 2).re # approximately -1.0

absSquared

absSquared : Number

Returns the square of the magnitude.

Returns real² + imaginary²

Examples
(Rectangular { re: 3, im: 4 }).absSquared # => 25

abs

abs : Float

Returns the magnitude of this complex number.

Returns distance from the origin

Examples
(Rectangular { re: 3, im: 4 }).abs # => 5.0

arg

arg : Float

Returns the argument in radians, in the range chosen by Math.atan2.

Returns angle in radians

Examples
Complex.I.arg # => Math.PI / 2.0

polar

polar : Polar

Converts this number to polar coordinates.

Returns magnitude and argument in radians

Examples
(Rectangular { re: 3, im: 4 }).polar.radius # => 5.0

From Showable

record Polar

A complex number in polar form, with a non-negative radius and angle in radians.

Fields

radius
Number
theta
Number

Implements Showable.

Functions

showValue Showable

showValue : String

Renders the radius and angle in radians.

Returns the polar coordinates

Examples
(Polar { radius: 2, theta: 0 }).showValue # => "(2, 0)"

rectangular

rectangular : Rectangular

Converts these polar coordinates to a complex number.

Returns the rectangular representation

Examples
(Polar { radius: 2, theta: 0 }).rectangular.re # => 2.0

From Showable

module Complex.Polar

ZERO constant