complex
nimony/lib/std/complex.nim
This module implements complex numbers and basic mathematical operations on them.
A complex number is a number of the form a + b*i, where a is the real part and b is the imaginary part (and i is the imaginary unit with the property i*i == -1).
Complex[T] is generic over the floating-point component type T. Following the modelling of std/math, each operation depends precisely on the concepts it needs: the arithmetic operators come from Arithmetic, and the transcendental functions add HasSqrt, HasExp, HasLn, HasSin, HasCos and HasArctan2. So Complex[float32] and Complex[float64] both work, and a user float type satisfying the relevant concepts works too.
type Complex = object re: T im: T
type Complex64 = ComplexA complex number with 64-bit float components.
type Complex32 = ComplexA complex number with 32-bit float components.
func complex(re: T; im: T): Complex[T]Returns a complex number with real part
reand imaginary partim.func complex(re: T): Complex[T]Returns a complex number with real part
reand zero imaginary part.func abs2(z: Complex[T]): TReturns the squared absolute value of
z, i.e.z.re^2 + z.im^2. Cheaper thanabsbecause it avoids the square root.func abs(z: Complex[T]): TReturns the absolute value (modulus) of
z.func arg(z: Complex[T]): TReturns the argument (phase angle, in radians) of
z.func conjugate(z: Complex[T]): Complex[T]Returns the complex conjugate of
z(z.imnegated).func ==(a: Complex[T]; b: Complex[T]): boolCompares two complex numbers for equality.
func +(a: Complex[T]; b: Complex[T]): Complex[T]func +(a: Complex[T]; b: T): Complex[T]func +(a: T; b: Complex[T]): Complex[T]func -(z: Complex[T]): Complex[T]Unary minus.
func -(a: Complex[T]; b: Complex[T]): Complex[T]func -(a: Complex[T]; b: T): Complex[T]func -(a: T; b: Complex[T]): Complex[T]func *(a: Complex[T]; b: Complex[T]): Complex[T]func *(a: Complex[T]; b: T): Complex[T]func *(a: T; b: Complex[T]): Complex[T]func /(a: Complex[T]; b: Complex[T]): Complex[T]func /(a: Complex[T]; b: T): Complex[T]func inv(z: Complex[T]): Complex[T]Returns the multiplicative inverse
1 / z.func sqrt(z: Complex[T]): Complex[T]Returns the principal square root of
z.func exp(z: Complex[T]): Complex[T]Returns the exponential
e^z.func ln(z: Complex[T]): Complex[T]Returns the principal natural logarithm of
z.func pow(z: Complex[T]; w: Complex[T]): Complex[T]Returns
zraised to the powerw(principal value).proc $(z: Complex[T]): stringReturns a textual representation of
zas"(re, im)".