This module implements complex numbers and basic mathematical operations on them.
Complex numbers are currently generic over 64-bit or 32-bit floats.
Example:
import std/complex from std/math import almostEqual, sqrt func almostEqual(a, b: Complex): bool = almostEqual(a.re, b.re) and almostEqual(a.im, b.im) let z1 = complex(1.0, 2.0) z2 = complex(3.0, -4.0) assert almostEqual(z1 + z2, complex(4.0, -2.0)) assert almostEqual(z1 - z2, complex(-2.0, 6.0)) assert almostEqual(z1 * z2, complex(11.0, 2.0)) assert almostEqual(z1 / z2, complex(-0.2, 0.4)) assert almostEqual(abs(z1), sqrt(5.0)) assert almostEqual(conjugate(z1), complex(1.0, -2.0)) let (r, phi) = z1.polar assert almostEqual(rect(r, phi), z1)
Procs
func `$`(z: Complex): string
-
Returns z's string representation as "(re, im)".
Example:
doAssert $complex(1.0, 2.0) == "(1.0, 2.0)"
Source Edit func `*`[T](x: Complex[T]; y: T): Complex[T]
- Multiplies a complex number with a real number. Source Edit
func `*`[T](x: T; y: Complex[T]): Complex[T]
- Multiplies a real number with a complex number. Source Edit
func `-`[T](x: Complex[T]; y: T): Complex[T]
- Subtracts a real number from a complex number. Source Edit
func `-`[T](x: T; y: Complex[T]): Complex[T]
- Subtracts a complex number from a real number. Source Edit
func abs2[T](z: Complex[T]): T
- Returns the squared absolute value of z, that is the squared distance from (0, 0) to z. This is more efficient than abs(z) ^ 2. Source Edit
func abs[T](z: Complex[T]): T
- Returns the absolute value of z, that is the distance from (0, 0) to z. Source Edit
func arccoth[T](z: Complex[T]): Complex[T]
- Returns the inverse hyperbolic cotangent of z. Source Edit
func arccsch[T](z: Complex[T]): Complex[T]
- Returns the inverse hyperbolic cosecant of z. Source Edit
func complex32(re: float32; im: float32 = 0.0): Complex32 {....raises: [], tags: [].}
- Returns a Complex32 with real part re and imaginary part im. Source Edit
func complex64(re: float64; im: float64 = 0.0): Complex64 {....raises: [], tags: [].}
- Returns a Complex64 with real part re and imaginary part im. Source Edit
func complex[T: SomeFloat](re: T; im: T = 0.0): Complex[T]
- Returns a Complex[T] with real part re and imaginary part im. Source Edit
func conjugate[T](z: Complex[T]): Complex[T]
- Returns the complex conjugate of z (complex(z.re, -z.im)). Source Edit
func ln[T](z: Complex[T]): Complex[T]
- Returns the (principal value of the) natural logarithm of z. Source Edit
func phase[T](z: Complex[T]): T
-
Returns the phase (or argument) of z, that is the angle in polar representation.
result = arctan2(z.im, z.re)
Source Edit func polar[T](z: Complex[T]): tuple[r, phi: T]
-
Returns z in polar coordinates.
result.r = abs(z)
result.phi = phase(z)See also:
- rect func for the inverse operation
func pow[T](x: Complex[T]; y: T): Complex[T]
- The complex number x raised to the power of the real number y. Source Edit
func 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)See also:
- polar func for the inverse operation