mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-07 05:23:20 +00:00
complex.nim: Use func everywhere (#16294)
This commit is contained in:
@@ -24,15 +24,15 @@ type
|
||||
Complex32* = Complex[float32]
|
||||
## Alias for a pair of 32-bit floats.
|
||||
|
||||
proc complex*[T: SomeFloat](re: T; im: T = 0.0): Complex[T] =
|
||||
func complex*[T: SomeFloat](re: T; im: T = 0.0): Complex[T] =
|
||||
result.re = re
|
||||
result.im = im
|
||||
|
||||
proc complex32*(re: float32; im: float32 = 0.0): Complex[float32] =
|
||||
func complex32*(re: float32; im: float32 = 0.0): Complex[float32] =
|
||||
result.re = re
|
||||
result.im = im
|
||||
|
||||
proc complex64*(re: float64; im: float64 = 0.0): Complex[float64] =
|
||||
func complex64*(re: float64; im: float64 = 0.0): Complex[float64] =
|
||||
result.re = re
|
||||
result.im = im
|
||||
|
||||
@@ -41,71 +41,71 @@ template im*(arg: typedesc[float64]): Complex64 = complex[float64](0, 1)
|
||||
template im*(arg: float32): Complex32 = complex[float32](0, arg)
|
||||
template im*(arg: float64): Complex64 = complex[float64](0, arg)
|
||||
|
||||
proc abs*[T](z: Complex[T]): T =
|
||||
func abs*[T](z: Complex[T]): T =
|
||||
## Returns the distance from (0,0) to ``z``.
|
||||
result = hypot(z.re, z.im)
|
||||
|
||||
proc abs2*[T](z: Complex[T]): T =
|
||||
func abs2*[T](z: Complex[T]): T =
|
||||
## Returns the squared distance from (0,0) to ``z``.
|
||||
result = z.re*z.re + z.im*z.im
|
||||
|
||||
proc conjugate*[T](z: Complex[T]): Complex[T] =
|
||||
func conjugate*[T](z: Complex[T]): Complex[T] =
|
||||
## Conjugates of complex number ``z``.
|
||||
result.re = z.re
|
||||
result.im = -z.im
|
||||
|
||||
proc inv*[T](z: Complex[T]): Complex[T] =
|
||||
func inv*[T](z: Complex[T]): Complex[T] =
|
||||
## Multiplicatives inverse of complex number ``z``.
|
||||
conjugate(z) / abs2(z)
|
||||
|
||||
proc `==` *[T](x, y: Complex[T]): bool =
|
||||
func `==` *[T](x, y: Complex[T]): bool =
|
||||
## Compares two complex numbers ``x`` and ``y`` for equality.
|
||||
result = x.re == y.re and x.im == y.im
|
||||
|
||||
proc `+` *[T](x: T; y: Complex[T]): Complex[T] =
|
||||
func `+` *[T](x: T; y: Complex[T]): Complex[T] =
|
||||
## Adds a real number to a complex number.
|
||||
result.re = x + y.re
|
||||
result.im = y.im
|
||||
|
||||
proc `+` *[T](x: Complex[T]; y: T): Complex[T] =
|
||||
func `+` *[T](x: Complex[T]; y: T): Complex[T] =
|
||||
## Adds a complex number to a real number.
|
||||
result.re = x.re + y
|
||||
result.im = x.im
|
||||
|
||||
proc `+` *[T](x, y: Complex[T]): Complex[T] =
|
||||
func `+` *[T](x, y: Complex[T]): Complex[T] =
|
||||
## Adds two complex numbers.
|
||||
result.re = x.re + y.re
|
||||
result.im = x.im + y.im
|
||||
|
||||
proc `-` *[T](z: Complex[T]): Complex[T] =
|
||||
func `-` *[T](z: Complex[T]): Complex[T] =
|
||||
## Unary minus for complex numbers.
|
||||
result.re = -z.re
|
||||
result.im = -z.im
|
||||
|
||||
proc `-` *[T](x: T; y: Complex[T]): Complex[T] =
|
||||
func `-` *[T](x: T; y: Complex[T]): Complex[T] =
|
||||
## Subtracts a complex number from a real number.
|
||||
x + (-y)
|
||||
|
||||
proc `-` *[T](x: Complex[T]; y: T): Complex[T] =
|
||||
func `-` *[T](x: Complex[T]; y: T): Complex[T] =
|
||||
## Subtracts a real number from a complex number.
|
||||
result.re = x.re - y
|
||||
result.im = x.im
|
||||
|
||||
proc `-` *[T](x, y: Complex[T]): Complex[T] =
|
||||
func `-` *[T](x, y: Complex[T]): Complex[T] =
|
||||
## Subtracts two complex numbers.
|
||||
result.re = x.re - y.re
|
||||
result.im = x.im - y.im
|
||||
|
||||
proc `/` *[T](x: Complex[T]; y: T): Complex[T] =
|
||||
func `/` *[T](x: Complex[T]; y: T): Complex[T] =
|
||||
## Divides complex number ``x`` by real number ``y``.
|
||||
result.re = x.re / y
|
||||
result.im = x.im / y
|
||||
|
||||
proc `/` *[T](x: T; y: Complex[T]): Complex[T] =
|
||||
func `/` *[T](x: T; y: Complex[T]): Complex[T] =
|
||||
## Divides real number ``x`` by complex number ``y``.
|
||||
result = x * inv(y)
|
||||
|
||||
proc `/` *[T](x, y: Complex[T]): Complex[T] =
|
||||
func `/` *[T](x, y: Complex[T]): Complex[T] =
|
||||
## Divides ``x`` by ``y``.
|
||||
var r, den: T
|
||||
if abs(y.re) < abs(y.im):
|
||||
@@ -119,44 +119,44 @@ proc `/` *[T](x, y: Complex[T]): Complex[T] =
|
||||
result.re = (x.re + r * x.im) / den
|
||||
result.im = (x.im - r * x.re) / den
|
||||
|
||||
proc `*` *[T](x: T; y: Complex[T]): Complex[T] =
|
||||
func `*` *[T](x: T; y: Complex[T]): Complex[T] =
|
||||
## Multiplies a real number and a complex number.
|
||||
result.re = x * y.re
|
||||
result.im = x * y.im
|
||||
|
||||
proc `*` *[T](x: Complex[T]; y: T): Complex[T] =
|
||||
func `*` *[T](x: Complex[T]; y: T): Complex[T] =
|
||||
## Multiplies a complex number with a real number.
|
||||
result.re = x.re * y
|
||||
result.im = x.im * y
|
||||
|
||||
proc `*` *[T](x, y: Complex[T]): Complex[T] =
|
||||
func `*` *[T](x, y: Complex[T]): Complex[T] =
|
||||
## Multiplies ``x`` with ``y``.
|
||||
result.re = x.re * y.re - x.im * y.im
|
||||
result.im = x.im * y.re + x.re * y.im
|
||||
|
||||
|
||||
proc `+=` *[T](x: var Complex[T]; y: Complex[T]) =
|
||||
func `+=` *[T](x: var Complex[T]; y: Complex[T]) =
|
||||
## Adds ``y`` to ``x``.
|
||||
x.re += y.re
|
||||
x.im += y.im
|
||||
|
||||
proc `-=` *[T](x: var Complex[T]; y: Complex[T]) =
|
||||
func `-=` *[T](x: var Complex[T]; y: Complex[T]) =
|
||||
## Subtracts ``y`` from ``x``.
|
||||
x.re -= y.re
|
||||
x.im -= y.im
|
||||
|
||||
proc `*=` *[T](x: var Complex[T]; y: Complex[T]) =
|
||||
func `*=` *[T](x: var Complex[T]; y: Complex[T]) =
|
||||
## Multiplies ``y`` to ``x``.
|
||||
let im = x.im * y.re + x.re * y.im
|
||||
x.re = x.re * y.re - x.im * y.im
|
||||
x.im = im
|
||||
|
||||
proc `/=` *[T](x: var Complex[T]; y: Complex[T]) =
|
||||
func `/=` *[T](x: var Complex[T]; y: Complex[T]) =
|
||||
## Divides ``x`` by ``y`` in place.
|
||||
x = x / y
|
||||
|
||||
|
||||
proc sqrt*[T](z: Complex[T]): Complex[T] =
|
||||
func sqrt*[T](z: Complex[T]): Complex[T] =
|
||||
## Square root for a complex number ``z``.
|
||||
var x, y, w, r: T
|
||||
|
||||
@@ -179,7 +179,7 @@ proc sqrt*[T](z: Complex[T]): Complex[T] =
|
||||
result.im = if z.im >= 0.0: w else: -w
|
||||
result.re = z.im / (result.im + result.im)
|
||||
|
||||
proc exp*[T](z: Complex[T]): Complex[T] =
|
||||
func exp*[T](z: Complex[T]): Complex[T] =
|
||||
## ``e`` raised to the power ``z``.
|
||||
var
|
||||
rho = exp(z.re)
|
||||
@@ -187,20 +187,20 @@ proc exp*[T](z: Complex[T]): Complex[T] =
|
||||
result.re = rho * cos(theta)
|
||||
result.im = rho * sin(theta)
|
||||
|
||||
proc ln*[T](z: Complex[T]): Complex[T] =
|
||||
func ln*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the natural log of ``z``.
|
||||
result.re = ln(abs(z))
|
||||
result.im = arctan2(z.im, z.re)
|
||||
|
||||
proc log10*[T](z: Complex[T]): Complex[T] =
|
||||
func log10*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the log base 10 of ``z``.
|
||||
result = ln(z) / ln(10.0)
|
||||
|
||||
proc log2*[T](z: Complex[T]): Complex[T] =
|
||||
func log2*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the log base 2 of ``z``.
|
||||
result = ln(z) / ln(2.0)
|
||||
|
||||
proc pow*[T](x, y: Complex[T]): Complex[T] =
|
||||
func pow*[T](x, y: Complex[T]): Complex[T] =
|
||||
## ``x`` raised to the power ``y``.
|
||||
if x.re == 0.0 and x.im == 0.0:
|
||||
if y.re == 0.0 and y.im == 0.0:
|
||||
@@ -222,118 +222,118 @@ proc pow*[T](x, y: Complex[T]): Complex[T] =
|
||||
result.re = s * cos(r)
|
||||
result.im = s * sin(r)
|
||||
|
||||
proc pow*[T](x: Complex[T]; y: T): Complex[T] =
|
||||
func pow*[T](x: Complex[T]; y: T): Complex[T] =
|
||||
## Complex number ``x`` raised to the power ``y``.
|
||||
pow(x, complex[T](y))
|
||||
|
||||
|
||||
proc sin*[T](z: Complex[T]): Complex[T] =
|
||||
func sin*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the sine of ``z``.
|
||||
result.re = sin(z.re) * cosh(z.im)
|
||||
result.im = cos(z.re) * sinh(z.im)
|
||||
|
||||
proc arcsin*[T](z: Complex[T]): Complex[T] =
|
||||
func arcsin*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the inverse sine of ``z``.
|
||||
result = -im(T) * ln(im(T) * z + sqrt(T(1.0) - z*z))
|
||||
|
||||
proc cos*[T](z: Complex[T]): Complex[T] =
|
||||
func cos*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the cosine of ``z``.
|
||||
result.re = cos(z.re) * cosh(z.im)
|
||||
result.im = -sin(z.re) * sinh(z.im)
|
||||
|
||||
proc arccos*[T](z: Complex[T]): Complex[T] =
|
||||
func arccos*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the inverse cosine of ``z``.
|
||||
result = -im(T) * ln(z + sqrt(z*z - T(1.0)))
|
||||
|
||||
proc tan*[T](z: Complex[T]): Complex[T] =
|
||||
func tan*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the tangent of ``z``.
|
||||
result = sin(z) / cos(z)
|
||||
|
||||
proc arctan*[T](z: Complex[T]): Complex[T] =
|
||||
func arctan*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the inverse tangent of ``z``.
|
||||
result = T(0.5)*im(T) * (ln(T(1.0) - im(T)*z) - ln(T(1.0) + im(T)*z))
|
||||
|
||||
proc cot*[T](z: Complex[T]): Complex[T] =
|
||||
func cot*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the cotangent of ``z``.
|
||||
result = cos(z)/sin(z)
|
||||
|
||||
proc arccot*[T](z: Complex[T]): Complex[T] =
|
||||
func arccot*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the inverse cotangent of ``z``.
|
||||
result = T(0.5)*im(T) * (ln(T(1.0) - im(T)/z) - ln(T(1.0) + im(T)/z))
|
||||
|
||||
proc sec*[T](z: Complex[T]): Complex[T] =
|
||||
func sec*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the secant of ``z``.
|
||||
result = T(1.0) / cos(z)
|
||||
|
||||
proc arcsec*[T](z: Complex[T]): Complex[T] =
|
||||
func arcsec*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the inverse secant of ``z``.
|
||||
result = -im(T) * ln(im(T) * sqrt(1.0 - 1.0/(z*z)) + T(1.0)/z)
|
||||
|
||||
proc csc*[T](z: Complex[T]): Complex[T] =
|
||||
func csc*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the cosecant of ``z``.
|
||||
result = T(1.0) / sin(z)
|
||||
|
||||
proc arccsc*[T](z: Complex[T]): Complex[T] =
|
||||
func arccsc*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the inverse cosecant of ``z``.
|
||||
result = -im(T) * ln(sqrt(T(1.0) - T(1.0)/(z*z)) + im(T)/z)
|
||||
|
||||
proc sinh*[T](z: Complex[T]): Complex[T] =
|
||||
func sinh*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the hyperbolic sine of ``z``.
|
||||
result = T(0.5) * (exp(z) - exp(-z))
|
||||
|
||||
proc arcsinh*[T](z: Complex[T]): Complex[T] =
|
||||
func arcsinh*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the inverse hyperbolic sine of ``z``.
|
||||
result = ln(z + sqrt(z*z + 1.0))
|
||||
|
||||
proc cosh*[T](z: Complex[T]): Complex[T] =
|
||||
func cosh*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the hyperbolic cosine of ``z``.
|
||||
result = T(0.5) * (exp(z) + exp(-z))
|
||||
|
||||
proc arccosh*[T](z: Complex[T]): Complex[T] =
|
||||
func arccosh*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the inverse hyperbolic cosine of ``z``.
|
||||
result = ln(z + sqrt(z*z - T(1.0)))
|
||||
|
||||
proc tanh*[T](z: Complex[T]): Complex[T] =
|
||||
func tanh*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the hyperbolic tangent of ``z``.
|
||||
result = sinh(z) / cosh(z)
|
||||
|
||||
proc arctanh*[T](z: Complex[T]): Complex[T] =
|
||||
func arctanh*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the inverse hyperbolic tangent of ``z``.
|
||||
result = T(0.5) * (ln((T(1.0)+z) / (T(1.0)-z)))
|
||||
|
||||
proc sech*[T](z: Complex[T]): Complex[T] =
|
||||
func sech*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the hyperbolic secant of ``z``.
|
||||
result = T(2.0) / (exp(z) + exp(-z))
|
||||
|
||||
proc arcsech*[T](z: Complex[T]): Complex[T] =
|
||||
func arcsech*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the inverse hyperbolic secant of ``z``.
|
||||
result = ln(1.0/z + sqrt(T(1.0)/z+T(1.0)) * sqrt(T(1.0)/z-T(1.0)))
|
||||
|
||||
proc csch*[T](z: Complex[T]): Complex[T] =
|
||||
func csch*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the hyperbolic cosecant of ``z``.
|
||||
result = T(2.0) / (exp(z) - exp(-z))
|
||||
|
||||
proc arccsch*[T](z: Complex[T]): Complex[T] =
|
||||
func arccsch*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the inverse hyperbolic cosecant of ``z``.
|
||||
result = ln(T(1.0)/z + sqrt(T(1.0)/(z*z) + T(1.0)))
|
||||
|
||||
proc coth*[T](z: Complex[T]): Complex[T] =
|
||||
func coth*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the hyperbolic cotangent of ``z``.
|
||||
result = cosh(z) / sinh(z)
|
||||
|
||||
proc arccoth*[T](z: Complex[T]): Complex[T] =
|
||||
func arccoth*[T](z: Complex[T]): Complex[T] =
|
||||
## Returns the inverse hyperbolic cotangent of ``z``.
|
||||
result = T(0.5) * (ln(T(1.0) + T(1.0)/z) - ln(T(1.0) - T(1.0)/z))
|
||||
|
||||
proc phase*[T](z: Complex[T]): T =
|
||||
func phase*[T](z: Complex[T]): T =
|
||||
## Returns the phase of ``z``.
|
||||
arctan2(z.im, z.re)
|
||||
|
||||
proc polar*[T](z: Complex[T]): tuple[r, phi: T] =
|
||||
func polar*[T](z: Complex[T]): tuple[r, phi: T] =
|
||||
## Returns ``z`` in polar coordinates.
|
||||
(r: abs(z), phi: phase(z))
|
||||
|
||||
proc rect*[T](r, phi: T): Complex[T] =
|
||||
func rect*[T](r, phi: T): Complex[T] =
|
||||
## Returns the complex number with polar coordinates ``r`` and ``phi``.
|
||||
##
|
||||
## | ``result.re = r * cos(phi)``
|
||||
@@ -341,7 +341,7 @@ proc rect*[T](r, phi: T): Complex[T] =
|
||||
complex(r * cos(phi), r * sin(phi))
|
||||
|
||||
|
||||
proc `$`*(z: Complex): string =
|
||||
func `$`*(z: Complex): string =
|
||||
## Returns ``z``'s string representation as ``"(re, im)"``.
|
||||
result = "(" & $z.re & ", " & $z.im & ")"
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import tables, streams, parsecsv
|
||||
# We import the below modules to check that they compile with `strictFuncs`.
|
||||
# They are otherwise unused in this file.
|
||||
import
|
||||
complex,
|
||||
httpcore,
|
||||
math,
|
||||
nre,
|
||||
|
||||
Reference in New Issue
Block a user