complex.nim 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2010 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module implements complex numbers.
  10. ## Complex numbers are currently implemented as generic on a 64-bit or 32-bit float.
  11. {.push checks: off, line_dir: off, stack_trace: off, debugger: off.}
  12. # the user does not want to trace a part of the standard library!
  13. import math
  14. type
  15. Complex*[T: SomeFloat] = object
  16. re*, im*: T
  17. ## A complex number, consisting of a real and an imaginary part.
  18. Complex64* = Complex[float64]
  19. ## Alias for a pair of 64-bit floats.
  20. Complex32* = Complex[float32]
  21. ## Alias for a pair of 32-bit floats.
  22. proc complex*[T: SomeFloat](re: T; im: T = 0.0): Complex[T] =
  23. result.re = re
  24. result.im = im
  25. proc complex32*(re: float32; im: float32 = 0.0): Complex[float32] =
  26. result.re = re
  27. result.im = im
  28. proc complex64*(re: float64; im: float64 = 0.0): Complex[float64] =
  29. result.re = re
  30. result.im = im
  31. template im*(arg: typedesc[float32]): Complex32 = complex[float32](0, 1)
  32. template im*(arg: typedesc[float64]): Complex64 = complex[float64](0, 1)
  33. template im*(arg: float32): Complex32 = complex[float32](0, arg)
  34. template im*(arg: float64): Complex64 = complex[float64](0, arg)
  35. proc abs*[T](z: Complex[T]): T =
  36. ## Return the distance from (0,0) to ``z``.
  37. result = hypot(z.re, z.im)
  38. proc abs2*[T](z: Complex[T]): T =
  39. ## Return the squared distance from (0,0) to ``z``.
  40. result = z.re*z.re + z.im*z.im
  41. proc conjugate*[T](z: Complex[T]): Complex[T] =
  42. ## Conjugate of complex number ``z``.
  43. result.re = z.re
  44. result.im = -z.im
  45. proc inv*[T](z: Complex[T]): Complex[T] =
  46. ## Multiplicative inverse of complex number ``z``.
  47. conjugate(z) / abs2(z)
  48. proc `==` *[T](x, y: Complex[T]): bool =
  49. ## Compare two complex numbers ``x`` and ``y`` for equality.
  50. result = x.re == y.re and x.im == y.im
  51. proc `+` *[T](x: T; y: Complex[T]): Complex[T] =
  52. ## Add a real number to a complex number.
  53. result.re = x + y.re
  54. result.im = y.im
  55. proc `+` *[T](x: Complex[T]; y: T): Complex[T] =
  56. ## Add a complex number to a real number.
  57. result.re = x.re + y
  58. result.im = x.im
  59. proc `+` *[T](x, y: Complex[T]): Complex[T] =
  60. ## Add two complex numbers.
  61. result.re = x.re + y.re
  62. result.im = x.im + y.im
  63. proc `-` *[T](z: Complex[T]): Complex[T] =
  64. ## Unary minus for complex numbers.
  65. result.re = -z.re
  66. result.im = -z.im
  67. proc `-` *[T](x: T; y: Complex[T]): Complex[T] =
  68. ## Subtract a complex number from a real number.
  69. x + (-y)
  70. proc `-` *[T](x: Complex[T]; y: T): Complex[T] =
  71. ## Subtract a real number from a complex number.
  72. result.re = x.re - y
  73. result.im = x.im
  74. proc `-` *[T](x, y: Complex[T]): Complex[T] =
  75. ## Subtract two complex numbers.
  76. result.re = x.re - y.re
  77. result.im = x.im - y.im
  78. proc `/` *[T](x: Complex[T]; y: T): Complex[T] =
  79. ## Divide complex number ``x`` by real number ``y``.
  80. result.re = x.re / y
  81. result.im = x.im / y
  82. proc `/` *[T](x: T; y: Complex[T]): Complex[T] =
  83. ## Divide real number ``x`` by complex number ``y``.
  84. result = x * inv(y)
  85. proc `/` *[T](x, y: Complex[T]): Complex[T] =
  86. ## Divide ``x`` by ``y``.
  87. var r, den: T
  88. if abs(y.re) < abs(y.im):
  89. r = y.re / y.im
  90. den = y.im + r * y.re
  91. result.re = (x.re * r + x.im) / den
  92. result.im = (x.im * r - x.re) / den
  93. else:
  94. r = y.im / y.re
  95. den = y.re + r * y.im
  96. result.re = (x.re + r * x.im) / den
  97. result.im = (x.im - r * x.re) / den
  98. proc `*` *[T](x: T; y: Complex[T]): Complex[T] =
  99. ## Multiply a real number and a complex number.
  100. result.re = x * y.re
  101. result.im = x * y.im
  102. proc `*` *[T](x: Complex[T]; y: T): Complex[T] =
  103. ## Multiply a complex number with a real number.
  104. result.re = x.re * y
  105. result.im = x.im * y
  106. proc `*` *[T](x, y: Complex[T]): Complex[T] =
  107. ## Multiply ``x`` with ``y``.
  108. result.re = x.re * y.re - x.im * y.im
  109. result.im = x.im * y.re + x.re * y.im
  110. proc `+=` *[T](x: var Complex[T]; y: Complex[T]) =
  111. ## Add ``y`` to ``x``.
  112. x.re += y.re
  113. x.im += y.im
  114. proc `-=` *[T](x: var Complex[T]; y: Complex[T]) =
  115. ## Subtract ``y`` from ``x``.
  116. x.re -= y.re
  117. x.im -= y.im
  118. proc `*=` *[T](x: var Complex[T]; y: Complex[T]) =
  119. ## Multiply ``y`` to ``x``.
  120. let im = x.im * y.re + x.re * y.im
  121. x.re = x.re * y.re - x.im * y.im
  122. x.im = im
  123. proc `/=` *[T](x: var Complex[T]; y: Complex[T]) =
  124. ## Divide ``x`` by ``y`` in place.
  125. x = x / y
  126. proc sqrt*[T](z: Complex[T]): Complex[T] =
  127. ## Square root for a complex number ``z``.
  128. var x, y, w, r: T
  129. if z.re == 0.0 and z.im == 0.0:
  130. result = z
  131. else:
  132. x = abs(z.re)
  133. y = abs(z.im)
  134. if x >= y:
  135. r = y / x
  136. w = sqrt(x) * sqrt(0.5 * (1.0 + sqrt(1.0 + r * r)))
  137. else:
  138. r = x / y
  139. w = sqrt(y) * sqrt(0.5 * (r + sqrt(1.0 + r * r)))
  140. if z.re >= 0.0:
  141. result.re = w
  142. result.im = z.im / (w * 2.0)
  143. else:
  144. result.im = if z.im >= 0.0: w else: -w
  145. result.re = z.im / (result.im + result.im)
  146. proc exp*[T](z: Complex[T]): Complex[T] =
  147. ## ``e`` raised to the power ``z``.
  148. var
  149. rho = exp(z.re)
  150. theta = z.im
  151. result.re = rho * cos(theta)
  152. result.im = rho * sin(theta)
  153. proc ln*[T](z: Complex[T]): Complex[T] =
  154. ## Returns the natural log of ``z``.
  155. result.re = ln(abs(z))
  156. result.im = arctan2(z.im, z.re)
  157. proc log10*[T](z: Complex[T]): Complex[T] =
  158. ## Returns the log base 10 of ``z``.
  159. result = ln(z) / ln(10.0)
  160. proc log2*[T](z: Complex[T]): Complex[T] =
  161. ## Returns the log base 2 of ``z``.
  162. result = ln(z) / ln(2.0)
  163. proc pow*[T](x, y: Complex[T]): Complex[T] =
  164. ## ``x`` raised to the power ``y``.
  165. if x.re == 0.0 and x.im == 0.0:
  166. if y.re == 0.0 and y.im == 0.0:
  167. result.re = 1.0
  168. result.im = 0.0
  169. else:
  170. result.re = 0.0
  171. result.im = 0.0
  172. elif y.re == 1.0 and y.im == 0.0:
  173. result = x
  174. elif y.re == -1.0 and y.im == 0.0:
  175. result = T(1.0) / x
  176. else:
  177. var
  178. rho = abs(x)
  179. theta = arctan2(x.im, x.re)
  180. s = pow(rho, y.re) * exp(-y.im * theta)
  181. r = y.re * theta + y.im * ln(rho)
  182. result.re = s * cos(r)
  183. result.im = s * sin(r)
  184. proc pow*[T](x: Complex[T]; y: T): Complex[T] =
  185. ## Complex number ``x`` raised to the power ``y``.
  186. pow(x, complex[T](y))
  187. proc sin*[T](z: Complex[T]): Complex[T] =
  188. ## Returns the sine of ``z``.
  189. result.re = sin(z.re) * cosh(z.im)
  190. result.im = cos(z.re) * sinh(z.im)
  191. proc arcsin*[T](z: Complex[T]): Complex[T] =
  192. ## Returns the inverse sine of ``z``.
  193. result = -im(T) * ln(im(T) * z + sqrt(T(1.0) - z*z))
  194. proc cos*[T](z: Complex[T]): Complex[T] =
  195. ## Returns the cosine of ``z``.
  196. result.re = cos(z.re) * cosh(z.im)
  197. result.im = -sin(z.re) * sinh(z.im)
  198. proc arccos*[T](z: Complex[T]): Complex[T] =
  199. ## Returns the inverse cosine of ``z``.
  200. result = -im(T) * ln(z + sqrt(z*z - T(1.0)))
  201. proc tan*[T](z: Complex[T]): Complex[T] =
  202. ## Returns the tangent of ``z``.
  203. result = sin(z) / cos(z)
  204. proc arctan*[T](z: Complex[T]): Complex[T] =
  205. ## Returns the inverse tangent of ``z``.
  206. result = T(0.5)*im(T) * (ln(T(1.0) - im(T)*z) - ln(T(1.0) + im(T)*z))
  207. proc cot*[T](z: Complex[T]): Complex[T] =
  208. ## Returns the cotangent of ``z``.
  209. result = cos(z)/sin(z)
  210. proc arccot*[T](z: Complex[T]): Complex[T] =
  211. ## Returns the inverse cotangent of ``z``.
  212. result = T(0.5)*im(T) * (ln(T(1.0) - im(T)/z) - ln(T(1.0) + im(T)/z))
  213. proc sec*[T](z: Complex[T]): Complex[T] =
  214. ## Returns the secant of ``z``.
  215. result = T(1.0) / cos(z)
  216. proc arcsec*[T](z: Complex[T]): Complex[T] =
  217. ## Returns the inverse secant of ``z``.
  218. result = -im(T) * ln(im(T) * sqrt(1.0 - 1.0/(z*z)) + T(1.0)/z)
  219. proc csc*[T](z: Complex[T]): Complex[T] =
  220. ## Returns the cosecant of ``z``.
  221. result = T(1.0) / sin(z)
  222. proc arccsc*[T](z: Complex[T]): Complex[T] =
  223. ## Returns the inverse cosecant of ``z``.
  224. result = -im(T) * ln(sqrt(T(1.0) - T(1.0)/(z*z)) + im(T)/z)
  225. proc sinh*[T](z: Complex[T]): Complex[T] =
  226. ## Returns the hyperbolic sine of ``z``.
  227. result = T(0.5) * (exp(z) - exp(-z))
  228. proc arcsinh*[T](z: Complex[T]): Complex[T] =
  229. ## Returns the inverse hyperbolic sine of ``z``.
  230. result = ln(z + sqrt(z*z + 1.0))
  231. proc cosh*[T](z: Complex[T]): Complex[T] =
  232. ## Returns the hyperbolic cosine of ``z``.
  233. result = T(0.5) * (exp(z) + exp(-z))
  234. proc arccosh*[T](z: Complex[T]): Complex[T] =
  235. ## Returns the inverse hyperbolic cosine of ``z``.
  236. result = ln(z + sqrt(z*z - T(1.0)))
  237. proc tanh*[T](z: Complex[T]): Complex[T] =
  238. ## Returns the hyperbolic tangent of ``z``.
  239. result = sinh(z) / cosh(z)
  240. proc arctanh*[T](z: Complex[T]): Complex[T] =
  241. ## Returns the inverse hyperbolic tangent of ``z``.
  242. result = T(0.5) * (ln((T(1.0)+z) / (T(1.0)-z)))
  243. proc sech*[T](z: Complex[T]): Complex[T] =
  244. ## Returns the hyperbolic secant of ``z``.
  245. result = T(2.0) / (exp(z) + exp(-z))
  246. proc arcsech*[T](z: Complex[T]): Complex[T] =
  247. ## Returns the inverse hyperbolic secant of ``z``.
  248. result = ln(1.0/z + sqrt(T(1.0)/z+T(1.0)) * sqrt(T(1.0)/z-T(1.0)))
  249. proc csch*[T](z: Complex[T]): Complex[T] =
  250. ## Returns the hyperbolic cosecant of ``z``.
  251. result = T(2.0) / (exp(z) - exp(-z))
  252. proc arccsch*[T](z: Complex[T]): Complex[T] =
  253. ## Returns the inverse hyperbolic cosecant of ``z``.
  254. result = ln(T(1.0)/z + sqrt(T(1.0)/(z*z) + T(1.0)))
  255. proc coth*[T](z: Complex[T]): Complex[T] =
  256. ## Returns the hyperbolic cotangent of ``z``.
  257. result = cosh(z) / sinh(z)
  258. proc arccoth*[T](z: Complex[T]): Complex[T] =
  259. ## Returns the inverse hyperbolic cotangent of ``z``.
  260. result = T(0.5) * (ln(T(1.0) + T(1.0)/z) - ln(T(1.0) - T(1.0)/z))
  261. proc phase*[T](z: Complex[T]): T =
  262. ## Returns the phase of ``z``.
  263. arctan2(z.im, z.re)
  264. proc polar*[T](z: Complex[T]): tuple[r, phi: T] =
  265. ## Returns ``z`` in polar coordinates.
  266. (r: abs(z), phi: phase(z))
  267. proc rect*[T](r, phi: T): Complex[T] =
  268. ## Returns the complex number with polar coordinates ``r`` and ``phi``.
  269. ##
  270. ## | ``result.re = r * cos(phi)``
  271. ## | ``result.im = r * sin(phi)``
  272. complex(r * cos(phi), r * sin(phi))
  273. proc `$`*(z: Complex): string =
  274. ## Returns ``z``'s string representation as ``"(re, im)"``.
  275. result = "(" & $z.re & ", " & $z.im & ")"
  276. {.pop.}
  277. when isMainModule:
  278. proc `=~`[T](x, y: Complex[T]): bool =
  279. result = abs(x.re-y.re) < 1e-6 and abs(x.im-y.im) < 1e-6
  280. proc `=~`[T](x: Complex[T]; y: T): bool =
  281. result = abs(x.re-y) < 1e-6 and abs(x.im) < 1e-6
  282. var
  283. z: Complex64 = complex(0.0, 0.0)
  284. oo: Complex64 = complex(1.0, 1.0)
  285. a: Complex64 = complex(1.0, 2.0)
  286. b: Complex64 = complex(-1.0, -2.0)
  287. m1: Complex64 = complex(-1.0, 0.0)
  288. i: Complex64 = complex(0.0, 1.0)
  289. one: Complex64 = complex(1.0, 0.0)
  290. tt: Complex64 = complex(10.0, 20.0)
  291. ipi: Complex64 = complex(0.0, -PI)
  292. doAssert(a/2.0 =~ complex(0.5, 1.0))
  293. doAssert(a == a)
  294. doAssert((a-a) == z)
  295. doAssert((a+b) == z)
  296. doAssert((a+b) =~ 0.0)
  297. doAssert((a/b) == m1)
  298. doAssert((1.0/a) =~ complex(0.2, -0.4))
  299. doAssert((a*b) == complex(3.0, -4.0))
  300. doAssert(10.0*a == tt)
  301. doAssert(a*10.0 == tt)
  302. doAssert(tt/10.0 == a)
  303. doAssert(oo+(-1.0) == i)
  304. doAssert( (-1.0)+oo == i)
  305. doAssert(abs(oo) == sqrt(2.0))
  306. doAssert(conjugate(a) == complex(1.0, -2.0))
  307. doAssert(sqrt(m1) == i)
  308. doAssert(exp(ipi) =~ m1)
  309. doAssert(pow(a, b) =~ complex(-3.72999124927876, -1.68815826725068))
  310. doAssert(pow(z, a) =~ complex(0.0, 0.0))
  311. doAssert(pow(z, z) =~ complex(1.0, 0.0))
  312. doAssert(pow(a, one) =~ a)
  313. doAssert(pow(a, m1) =~ complex(0.2, -0.4))
  314. doAssert(pow(a, 2.0) =~ complex(-3.0, 4.0))
  315. doAssert(pow(a, 2) =~ complex(-3.0, 4.0))
  316. doAssert(not(pow(a, 2.0) =~ a))
  317. doAssert(ln(a) =~ complex(0.804718956217050, 1.107148717794090))
  318. doAssert(log10(a) =~ complex(0.349485002168009, 0.480828578784234))
  319. doAssert(log2(a) =~ complex(1.16096404744368, 1.59727796468811))
  320. doAssert(sin(a) =~ complex(3.16577851321617, 1.95960104142161))
  321. doAssert(cos(a) =~ complex(2.03272300701967, -3.05189779915180))
  322. doAssert(tan(a) =~ complex(0.0338128260798967, 1.0147936161466335))
  323. doAssert(cot(a) =~ 1.0 / tan(a))
  324. doAssert(sec(a) =~ 1.0 / cos(a))
  325. doAssert(csc(a) =~ 1.0 / sin(a))
  326. doAssert(arcsin(a) =~ complex(0.427078586392476, 1.528570919480998))
  327. doAssert(arccos(a) =~ complex(1.14371774040242, -1.52857091948100))
  328. doAssert(arctan(a) =~ complex(1.338972522294494, 0.402359478108525))
  329. doAssert(arccot(a) =~ complex(0.2318238045004031, -0.402359478108525))
  330. doAssert(arcsec(a) =~ complex(1.384478272687081, 0.3965682301123288))
  331. doAssert(arccsc(a) =~ complex(0.1863180541078155, -0.3965682301123291))
  332. doAssert(cosh(a) =~ complex(-0.642148124715520, 1.068607421382778))
  333. doAssert(sinh(a) =~ complex(-0.489056259041294, 1.403119250622040))
  334. doAssert(tanh(a) =~ complex(1.1667362572409199, -0.243458201185725))
  335. doAssert(sech(a) =~ 1.0 / cosh(a))
  336. doAssert(csch(a) =~ 1.0 / sinh(a))
  337. doAssert(coth(a) =~ 1.0 / tanh(a))
  338. doAssert(arccosh(a) =~ complex(1.528570919480998, 1.14371774040242))
  339. doAssert(arcsinh(a) =~ complex(1.469351744368185, 1.06344002357775))
  340. doAssert(arctanh(a) =~ complex(0.173286795139986, 1.17809724509617))
  341. doAssert(arcsech(a) =~ arccosh(1.0/a))
  342. doAssert(arccsch(a) =~ arcsinh(1.0/a))
  343. doAssert(arccoth(a) =~ arctanh(1.0/a))
  344. doAssert(phase(a) == 1.1071487177940904)
  345. var t = polar(a)
  346. doAssert(rect(t.r, t.phi) =~ a)
  347. doAssert(rect(1.0, 2.0) =~ complex(-0.4161468365471424, 0.9092974268256817))
  348. var
  349. i64: Complex32 = complex(0.0f, 1.0f)
  350. a64: Complex32 = 2.0f*i64 + 1.0.float32
  351. b64: Complex32 = complex(-1.0'f32, -2.0'f32)
  352. doAssert(a64 == a64)
  353. doAssert(a64 == -b64)
  354. doAssert(a64 + b64 =~ 0.0'f32)
  355. doAssert(not(pow(a64, b64) =~ a64))
  356. doAssert(pow(a64, 0.5f) =~ sqrt(a64))
  357. doAssert(pow(a64, 2) =~ complex(-3.0'f32, 4.0'f32))
  358. doAssert(sin(arcsin(b64)) =~ b64)
  359. doAssert(cosh(arccosh(a64)) =~ a64)
  360. doAssert(phase(a64) - 1.107149f < 1e-6)
  361. var t64 = polar(a64)
  362. doAssert(rect(t64.r, t64.phi) =~ a64)
  363. doAssert(rect(1.0f, 2.0f) =~ complex(-0.4161468f, 0.90929742f))
  364. doAssert(sizeof(a64) == 8)
  365. doAssert(sizeof(a) == 16)
  366. doAssert 123.0.im + 456.0 == complex64(456, 123)
  367. var localA = complex(0.1'f32)
  368. doAssert localA.im is float32