summary refs log tree commit diff stats
diff options
context:
space:
mode:
authordef <dennis@felsin9.de>2015-01-02 21:51:20 +0100
committerdef <dennis@felsin9.de>2015-02-16 20:44:23 +0100
commit00e82c2fc6129a790fca9d261cc6414f5a204d51 (patch)
tree33b2bdbde9f8443483754f01d2cfb28162a4260b
parent1ad1b93f0ac27b250489591bfe1c05ae99ae495f (diff)
downloadNim-00e82c2fc6129a790fca9d261cc6414f5a204d51.tar.gz
Extend complex to convert to/from polar coordinates
-rw-r--r--lib/pure/complex.nim27
1 files changed, 25 insertions, 2 deletions
diff --git a/lib/pure/complex.nim b/lib/pure/complex.nim
index 04cffe4e4..9f1546eed 100644
--- a/lib/pure/complex.nim
+++ b/lib/pure/complex.nim
@@ -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 & ")"
@@ -344,6 +364,9 @@ when isMainModule:
   assert( arcsin(a) =~ (0.427078586392476, 1.528570919480998) )
   assert( arccos(a) =~ (1.14371774040242, -1.52857091948100) )
 
-  assert( cosh(a) =~ (-0.642148124715520, 1.068607421382778) ) 
+  assert( cosh(a) =~ (-0.642148124715520, 1.068607421382778) )
   assert( sinh(a) =~ (-0.489056259041294, 1.403119250622040) )
-  
\ No newline at end of file
+
+  assert( phase(a) == 1.1071487177940904 )
+  assert( polar(a) =~ (2.23606797749979, 1.1071487177940904) )
+  assert( rect(1.0, 2.0) =~ (-0.4161468365471424, 0.9092974268256817) )