# complex

Dark Mode

- Manual
- Standard library
- Index
- devel, stable

Search:
Group by:

This module implements complex numbers. Complex numbers are currently implemented as generic on a 64-bit or 32-bit float.

# Imports

math

# Types

    Complex[T] = object
      re*, im*: T                ## A complex number, consisting of a real and an imaginary part.

  Source Edit

    Complex64 = Complex[float64]

  Alias for a pair of 64-bit floats. Source Edit

    Complex32 = Complex[float32]

  Alias for a pair of 32-bit floats. Source Edit

# Procs

    proc complex[T: SomeFloat](re: T; im: T = 0.0): Complex[T]

  Source Edit

    proc complex32(re: float32; im: float32 = 0.0): Complex[float32] {...}{.raises: [],
        tags: [].}

  Source Edit

    proc complex64(re: float64; im: float64 = 0.0): Complex[float64] {...}{.raises: [],
        tags: [].}

  Source Edit

    proc abs[T](z: Complex[T]): T

  Return the distance from (0,0) to `z`. Source Edit

    proc abs2[T](z: Complex[T]): T

  Return the squared distance from (0,0) to `z`. Source Edit

    proc conjugate[T](z: Complex[T]): Complex[T]

  Conjugate of complex number `z`. Source Edit

    proc inv[T](z: Complex[T]): Complex[T]

  Multiplicative inverse of complex number `z`. Source Edit

    proc `==`[T](x, y: Complex[T]): bool

  Compare two complex numbers `x` and `y` for equality. Source Edit

    proc `+`[T](x: T; y: Complex[T]): Complex[T]

  Add a real number to a complex number. Source Edit

    proc `+`[T](x: Complex[T]; y: T): Complex[T]

  Add a complex number to a real number. Source Edit

    proc `+`[T](x, y: Complex[T]): Complex[T]

  Add two complex numbers. Source Edit

    proc `-`[T](z: Complex[T]): Complex[T]

  Unary minus for complex numbers. Source Edit

    proc `-`[T](x: T; y: Complex[T]): Complex[T]

  Subtract a complex number from a real number. Source Edit

    proc `-`[T](x: Complex[T]; y: T): Complex[T]

  Subtract a real number from a complex number. Source Edit

    proc `-`[T](x, y: Complex[T]): Complex[T]

  Subtract two complex numbers. Source Edit

    proc `/`[T](x: Complex[T]; y: T): Complex[T]

  Divide complex number `x` by real number `y`. Source Edit

    proc `/`[T](x: T; y: Complex[T]): Complex[T]

  Divide real number `x` by complex number `y`. Source Edit

    proc `/`[T](x, y: Complex[T]): Complex[T]

  Divide `x` by `y`. Source Edit

    proc `*`[T](x: T; y: Complex[T]): Complex[T]

  Multiply a real number and a complex number. Source Edit

    proc `*`[T](x: Complex[T]; y: T): Complex[T]

  Multiply a complex number with a real number. Source Edit

    proc `*`[T](x, y: Complex[T]): Complex[T]

  Multiply `x` with `y`. Source Edit

    proc `+=`[T](x: var Complex[T]; y: Complex[T])

  Add `y` to `x`. Source Edit

    proc `-=`[T](x: var Complex[T]; y: Complex[T])

  Subtract `y` from `x`. Source Edit

    proc `*=`[T](x: var Complex[T]; y: Complex[T])

  Multiply `y` to `x`. Source Edit

    proc `/=`[T](x: var Complex[T]; y: Complex[T])

  Divide `x` by `y` in place. Source Edit

    proc sqrt[T](z: Complex[T]): Complex[T]

  Square root for a complex number `z`. Source Edit

    proc exp[T](z: Complex[T]): Complex[T]

  `e` raised to the power `z`. Source Edit

    proc ln[T](z: Complex[T]): Complex[T]

  Returns the natural log of `z`. Source Edit

    proc log10[T](z: Complex[T]): Complex[T]

  Returns the log base 10 of `z`. Source Edit

    proc log2[T](z: Complex[T]): Complex[T]

  Returns the log base 2 of `z`. Source Edit

    proc pow[T](x, y: Complex[T]): Complex[T]

  `x` raised to the power `y`. Source Edit

    proc pow[T](x: Complex[T]; y: T): Complex[T]

  Complex number `x` raised to the power `y`. Source Edit

    proc sin[T](z: Complex[T]): Complex[T]

  Returns the sine of `z`. Source Edit

    proc arcsin[T](z: Complex[T]): Complex[T]

  Returns the inverse sine of `z`. Source Edit

    proc cos[T](z: Complex[T]): Complex[T]

  Returns the cosine of `z`. Source Edit

    proc arccos[T](z: Complex[T]): Complex[T]

  Returns the inverse cosine of `z`. Source Edit

    proc tan[T](z: Complex[T]): Complex[T]

  Returns the tangent of `z`. Source Edit

    proc arctan[T](z: Complex[T]): Complex[T]

  Returns the inverse tangent of `z`. Source Edit

    proc cot[T](z: Complex[T]): Complex[T]

  Returns the cotangent of `z`. Source Edit

    proc arccot[T](z: Complex[T]): Complex[T]

  Returns the inverse cotangent of `z`. Source Edit

    proc sec[T](z: Complex[T]): Complex[T]

  Returns the secant of `z`. Source Edit

    proc arcsec[T](z: Complex[T]): Complex[T]

  Returns the inverse secant of `z`. Source Edit

    proc csc[T](z: Complex[T]): Complex[T]

  Returns the cosecant of `z`. Source Edit

    proc arccsc[T](z: Complex[T]): Complex[T]

  Returns the inverse cosecant of `z`. Source Edit

    proc sinh[T](z: Complex[T]): Complex[T]

  Returns the hyperbolic sine of `z`. Source Edit

    proc arcsinh[T](z: Complex[T]): Complex[T]

  Returns the inverse hyperbolic sine of `z`. Source Edit

    proc cosh[T](z: Complex[T]): Complex[T]

  Returns the hyperbolic cosine of `z`. Source Edit

    proc arccosh[T](z: Complex[T]): Complex[T]

  Returns the inverse hyperbolic cosine of `z`. Source Edit

    proc tanh[T](z: Complex[T]): Complex[T]

  Returns the hyperbolic tangent of `z`. Source Edit

    proc arctanh[T](z: Complex[T]): Complex[T]

  Returns the inverse hyperbolic tangent of `z`. Source Edit

    proc sech[T](z: Complex[T]): Complex[T]

  Returns the hyperbolic secant of `z`. Source Edit

    proc arcsech[T](z: Complex[T]): Complex[T]

  Returns the inverse hyperbolic secant of `z`. Source Edit

    proc csch[T](z: Complex[T]): Complex[T]

  Returns the hyperbolic cosecant of `z`. Source Edit

    proc arccsch[T](z: Complex[T]): Complex[T]

  Returns the inverse hyperbolic cosecant of `z`. Source Edit

    proc coth[T](z: Complex[T]): Complex[T]

  Returns the hyperbolic cotangent of `z`. Source Edit

    proc arccoth[T](z: Complex[T]): Complex[T]

  Returns the inverse hyperbolic cotangent of `z`. Source Edit

    proc phase[T](z: Complex[T]): T

  Returns the phase of `z`. Source Edit

    proc polar[T](z: Complex[T]): tuple[r, phi: T]

  Returns `z` in polar coordinates. Source Edit

    proc rect[T](r, phi: T): Complex[T]

  Returns the complex number with polar coordinates `r` and `phi`.

  `result.re = r * cos(phi)`
  `result.im = r * sin(phi)`

  Source Edit

    proc `$`(z: Complex): string

  Returns `z`'s string representation as `"(re, im)"`. Source Edit

# Templates

    template im(arg: typedesc[float32]): Complex32

  Source Edit

    template im(arg: typedesc[float64]): Complex64

  Source Edit

    template im(arg: float32): Complex32

  Source Edit

    template im(arg: float64): Complex64

  Source Edit