Extend complex to convert to/from polar coordinates
This commit is contained in:
parent
1ad1b93f0a
commit
00e82c2fc6
1 changed files with 25 additions and 2 deletions
|
|
@ -27,6 +27,11 @@ type
|
|||
|
||||
{.deprecated: [TComplex: Complex].}
|
||||
|
||||
proc toComplex*(x: SomeInteger): Complex =
|
||||
## Convert some integer ``x`` to a complex number.
|
||||
result.re = x
|
||||
result.im = 0
|
||||
|
||||
proc `==` *(x, y: Complex): bool =
|
||||
## Compare two complex numbers `x` and `y` for equality.
|
||||
result = x.re == y.re and x.im == y.im
|
||||
|
|
@ -291,6 +296,21 @@ proc cosh*(z: Complex): Complex =
|
|||
result = 0.5*(exp(z)+exp(-z))
|
||||
|
||||
|
||||
proc phase*(z: Complex): float =
|
||||
## Returns the phase of `z`.
|
||||
arctan2(z.im, z.re)
|
||||
|
||||
proc polar*(z: Complex): tuple[r, phi: float] =
|
||||
## Returns `z` in polar coordinates.
|
||||
result.r = abs(z)
|
||||
result.phi = phase(z)
|
||||
|
||||
proc rect*(r: float, phi: float): Complex =
|
||||
## Returns the complex number with poolar coordinates `r` and `phi`.
|
||||
result.re = r * cos(phi)
|
||||
result.im = sin(phi)
|
||||
|
||||
|
||||
proc `$`*(z: Complex): string =
|
||||
## Returns `z`'s string representation as ``"(re, im)"``.
|
||||
result = "(" & $z.re & ", " & $z.im & ")"
|
||||
|
|
@ -347,3 +367,6 @@ when isMainModule:
|
|||
assert( cosh(a) =~ (-0.642148124715520, 1.068607421382778) )
|
||||
assert( sinh(a) =~ (-0.489056259041294, 1.403119250622040) )
|
||||
|
||||
assert( phase(a) == 1.1071487177940904 )
|
||||
assert( polar(a) =~ (2.23606797749979, 1.1071487177940904) )
|
||||
assert( rect(1.0, 2.0) =~ (-0.4161468365471424, 0.9092974268256817) )
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue