complex.nim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. ## and basic mathematical operations on them.
  11. ##
  12. ## Complex numbers are currently generic over 64-bit or 32-bit floats.
  13. runnableExamples:
  14. from std/math import almostEqual, sqrt
  15. func almostEqual(a, b: Complex): bool =
  16. almostEqual(a.re, b.re) and almostEqual(a.im, b.im)
  17. let
  18. z1 = complex(1.0, 2.0)
  19. z2 = complex(3.0, -4.0)
  20. assert almostEqual(z1 + z2, complex(4.0, -2.0))
  21. assert almostEqual(z1 - z2, complex(-2.0, 6.0))
  22. assert almostEqual(z1 * z2, complex(11.0, 2.0))
  23. assert almostEqual(z1 / z2, complex(-0.2, 0.4))
  24. assert almostEqual(abs(z1), sqrt(5.0))
  25. assert almostEqual(conjugate(z1), complex(1.0, -2.0))
  26. let (r, phi) = z1.polar
  27. assert almostEqual(rect(r, phi), z1)
  28. {.push checks: off, line_dir: off, stack_trace: off, debugger: off.}
  29. # the user does not want to trace a part of the standard library!
  30. import math
  31. type
  32. Complex*[T: SomeFloat] = object
  33. ## A complex number, consisting of a real and an imaginary part.
  34. re*, im*: T
  35. Complex64* = Complex[float64]
  36. ## Alias for a complex number using 64-bit floats.
  37. Complex32* = Complex[float32]
  38. ## Alias for a complex number using 32-bit floats.
  39. func complex*[T: SomeFloat](re: T; im: T = 0.0): Complex[T] =
  40. ## Returns a `Complex[T]` with real part `re` and imaginary part `im`.
  41. result.re = re
  42. result.im = im
  43. func complex32*(re: float32; im: float32 = 0.0): Complex32 =
  44. ## Returns a `Complex32` with real part `re` and imaginary part `im`.
  45. result.re = re
  46. result.im = im
  47. func complex64*(re: float64; im: float64 = 0.0): Complex64 =
  48. ## Returns a `Complex64` with real part `re` and imaginary part `im`.
  49. result.re = re
  50. result.im = im
  51. template im*(arg: typedesc[float32]): Complex32 = complex32(0, 1)
  52. ## Returns the imaginary unit (`complex32(0, 1)`).
  53. template im*(arg: typedesc[float64]): Complex64 = complex64(0, 1)
  54. ## Returns the imaginary unit (`complex64(0, 1)`).
  55. template im*(arg: float32): Complex32 = complex32(0, arg)
  56. ## Returns `arg` as an imaginary number (`complex32(0, arg)`).
  57. template im*(arg: float64): Complex64 = complex64(0, arg)
  58. ## Returns `arg` as an imaginary number (`complex64(0, arg)`).
  59. func abs*[T](z: Complex[T]): T =
  60. ## Returns the absolute value of `z`,
  61. ## that is the distance from (0, 0) to `z`.
  62. result = hypot(z.re, z.im)
  63. func abs2*[T](z: Complex[T]): T =
  64. ## Returns the squared absolute value of `z`,
  65. ## that is the squared distance from (0, 0) to `z`.
  66. ## This is more efficient than `abs(z) ^ 2`.
  67. result = z.re * z.re + z.im * z.im
  68. func conjugate*[T](z: Complex[T]): Complex[T] =
  69. ## Returns the complex conjugate of `z` (`complex(z.re, -z.im)`).
  70. result.re = z.re
  71. result.im = -z.im
  72. func inv*[T](z: Complex[T]): Complex[T] =
  73. ## Returns the multiplicative inverse of `z` (`1/z`).
  74. conjugate(z) / abs2(z)
  75. func `==`*[T](x, y: Complex[T]): bool =
  76. ## Compares two complex numbers for equality.
  77. result = x.re == y.re and x.im == y.im
  78. func `+`*[T](x: T; y: Complex[T]): Complex[T] =
  79. ## Adds a real number to a complex number.
  80. result.re = x + y.re
  81. result.im = y.im
  82. func `+`*[T](x: Complex[T]; y: T): Complex[T] =
  83. ## Adds a complex number to a real number.
  84. result.re = x.re + y
  85. result.im = x.im
  86. func `+`*[T](x, y: Complex[T]): Complex[T] =
  87. ## Adds two complex numbers.
  88. result.re = x.re + y.re
  89. result.im = x.im + y.im
  90. func `-`*[T](z: Complex[T]): Complex[T] =
  91. ## Unary minus for complex numbers.
  92. result.re = -z.re
  93. result.im = -z.im
  94. func `-`*[T](x: T; y: Complex[T]): Complex[T] =
  95. ## Subtracts a complex number from a real number.
  96. result.re = x - y.re
  97. result.im = -y.im
  98. func `-`*[T](x: Complex[T]; y: T): Complex[T] =
  99. ## Subtracts a real number from a complex number.
  100. result.re = x.re - y
  101. result.im = x.im
  102. func `-`*[T](x, y: Complex[T]): Complex[T] =
  103. ## Subtracts two complex numbers.
  104. result.re = x.re - y.re
  105. result.im = x.im - y.im
  106. func `*`*[T](x: T; y: Complex[T]): Complex[T] =
  107. ## Multiplies a real number with a complex number.
  108. result.re = x * y.re
  109. result.im = x * y.im
  110. func `*`*[T](x: Complex[T]; y: T): Complex[T] =
  111. ## Multiplies a complex number with a real number.
  112. result.re = x.re * y
  113. result.im = x.im * y
  114. func `*`*[T](x, y: Complex[T]): Complex[T] =
  115. ## Multiplies two complex numbers.
  116. result.re = x.re * y.re - x.im * y.im
  117. result.im = x.im * y.re + x.re * y.im
  118. func `/`*[T](x: Complex[T]; y: T): Complex[T] =
  119. ## Divides a complex number by a real number.
  120. result.re = x.re / y
  121. result.im = x.im / y
  122. func `/`*[T](x: T; y: Complex[T]): Complex[T] =
  123. ## Divides a real number by a complex number.
  124. result = x * inv(y)
  125. func `/`*[T](x, y: Complex[T]): Complex[T] =
  126. ## Divides two complex numbers.
  127. var r, den: T
  128. if abs(y.re) < abs(y.im):
  129. r = y.re / y.im
  130. den = y.im + r * y.re
  131. result.re = (x.re * r + x.im) / den
  132. result.im = (x.im * r - x.re) / den
  133. else:
  134. r = y.im / y.re
  135. den = y.re + r * y.im
  136. result.re = (x.re + r * x.im) / den
  137. result.im = (x.im - r * x.re) / den
  138. func `+=`*[T](x: var Complex[T]; y: Complex[T]) =
  139. ## Adds `y` to `x`.
  140. x.re += y.re
  141. x.im += y.im
  142. func `-=`*[T](x: var Complex[T]; y: Complex[T]) =
  143. ## Subtracts `y` from `x`.
  144. x.re -= y.re
  145. x.im -= y.im
  146. func `*=`*[T](x: var Complex[T]; y: Complex[T]) =
  147. ## Multiplies `x` by `y`.
  148. let im = x.im * y.re + x.re * y.im
  149. x.re = x.re * y.re - x.im * y.im
  150. x.im = im
  151. func `/=`*[T](x: var Complex[T]; y: Complex[T]) =
  152. ## Divides `x` by `y` in place.
  153. x = x / y
  154. func sqrt*[T](z: Complex[T]): Complex[T] =
  155. ## Computes the
  156. ## ([principal](https://en.wikipedia.org/wiki/Square_root#Principal_square_root_of_a_complex_number))
  157. ## square root of a complex number `z`.
  158. var x, y, w, r: T
  159. if z.re == 0.0 and z.im == 0.0:
  160. result = z
  161. else:
  162. x = abs(z.re)
  163. y = abs(z.im)
  164. if x >= y:
  165. r = y / x
  166. w = sqrt(x) * sqrt(0.5 * (1.0 + sqrt(1.0 + r * r)))
  167. else:
  168. r = x / y
  169. w = sqrt(y) * sqrt(0.5 * (r + sqrt(1.0 + r * r)))
  170. if z.re >= 0.0:
  171. result.re = w
  172. result.im = z.im / (w * 2.0)
  173. else:
  174. result.im = if z.im >= 0.0: w else: -w
  175. result.re = z.im / (result.im + result.im)
  176. func exp*[T](z: Complex[T]): Complex[T] =
  177. ## Computes the exponential function (`e^z`).
  178. let
  179. rho = exp(z.re)
  180. theta = z.im
  181. result.re = rho * cos(theta)
  182. result.im = rho * sin(theta)
  183. func ln*[T](z: Complex[T]): Complex[T] =
  184. ## Returns the
  185. ## ([principal value](https://en.wikipedia.org/wiki/Complex_logarithm#Principal_value)
  186. ## of the) natural logarithm of `z`.
  187. result.re = ln(abs(z))
  188. result.im = arctan2(z.im, z.re)
  189. func log10*[T](z: Complex[T]): Complex[T] =
  190. ## Returns the logarithm base 10 of `z`.
  191. ##
  192. ## **See also:**
  193. ## * `ln func<#ln,Complex[T]>`_
  194. result = ln(z) / ln(10.0)
  195. func log2*[T](z: Complex[T]): Complex[T] =
  196. ## Returns the logarithm base 2 of `z`.
  197. ##
  198. ## **See also:**
  199. ## * `ln func<#ln,Complex[T]>`_
  200. result = ln(z) / ln(2.0)
  201. func pow*[T](x, y: Complex[T]): Complex[T] =
  202. ## `x` raised to the power of `y`.
  203. if x.re == 0.0 and x.im == 0.0:
  204. if y.re == 0.0 and y.im == 0.0:
  205. result.re = 1.0
  206. result.im = 0.0
  207. else:
  208. result.re = 0.0
  209. result.im = 0.0
  210. elif y.re == 1.0 and y.im == 0.0:
  211. result = x
  212. elif y.re == -1.0 and y.im == 0.0:
  213. result = T(1.0) / x
  214. else:
  215. let
  216. rho = abs(x)
  217. theta = arctan2(x.im, x.re)
  218. s = pow(rho, y.re) * exp(-y.im * theta)
  219. r = y.re * theta + y.im * ln(rho)
  220. result.re = s * cos(r)
  221. result.im = s * sin(r)
  222. func pow*[T](x: Complex[T]; y: T): Complex[T] =
  223. ## The complex number `x` raised to the power of the real number `y`.
  224. pow(x, complex[T](y))
  225. func sin*[T](z: Complex[T]): Complex[T] =
  226. ## Returns the sine of `z`.
  227. result.re = sin(z.re) * cosh(z.im)
  228. result.im = cos(z.re) * sinh(z.im)
  229. func arcsin*[T](z: Complex[T]): Complex[T] =
  230. ## Returns the inverse sine of `z`.
  231. result = -im(T) * ln(im(T) * z + sqrt(T(1.0) - z*z))
  232. func cos*[T](z: Complex[T]): Complex[T] =
  233. ## Returns the cosine of `z`.
  234. result.re = cos(z.re) * cosh(z.im)
  235. result.im = -sin(z.re) * sinh(z.im)
  236. func arccos*[T](z: Complex[T]): Complex[T] =
  237. ## Returns the inverse cosine of `z`.
  238. result = -im(T) * ln(z + sqrt(z*z - T(1.0)))
  239. func tan*[T](z: Complex[T]): Complex[T] =
  240. ## Returns the tangent of `z`.
  241. result = sin(z) / cos(z)
  242. func arctan*[T](z: Complex[T]): Complex[T] =
  243. ## Returns the inverse tangent of `z`.
  244. result = T(0.5)*im(T) * (ln(T(1.0) - im(T)*z) - ln(T(1.0) + im(T)*z))
  245. func cot*[T](z: Complex[T]): Complex[T] =
  246. ## Returns the cotangent of `z`.
  247. result = cos(z)/sin(z)
  248. func arccot*[T](z: Complex[T]): Complex[T] =
  249. ## Returns the inverse cotangent of `z`.
  250. result = T(0.5)*im(T) * (ln(T(1.0) - im(T)/z) - ln(T(1.0) + im(T)/z))
  251. func sec*[T](z: Complex[T]): Complex[T] =
  252. ## Returns the secant of `z`.
  253. result = T(1.0) / cos(z)
  254. func arcsec*[T](z: Complex[T]): Complex[T] =
  255. ## Returns the inverse secant of `z`.
  256. result = -im(T) * ln(im(T) * sqrt(1.0 - 1.0/(z*z)) + T(1.0)/z)
  257. func csc*[T](z: Complex[T]): Complex[T] =
  258. ## Returns the cosecant of `z`.
  259. result = T(1.0) / sin(z)
  260. func arccsc*[T](z: Complex[T]): Complex[T] =
  261. ## Returns the inverse cosecant of `z`.
  262. result = -im(T) * ln(sqrt(T(1.0) - T(1.0)/(z*z)) + im(T)/z)
  263. func sinh*[T](z: Complex[T]): Complex[T] =
  264. ## Returns the hyperbolic sine of `z`.
  265. result = T(0.5) * (exp(z) - exp(-z))
  266. func arcsinh*[T](z: Complex[T]): Complex[T] =
  267. ## Returns the inverse hyperbolic sine of `z`.
  268. result = ln(z + sqrt(z*z + 1.0))
  269. func cosh*[T](z: Complex[T]): Complex[T] =
  270. ## Returns the hyperbolic cosine of `z`.
  271. result = T(0.5) * (exp(z) + exp(-z))
  272. func arccosh*[T](z: Complex[T]): Complex[T] =
  273. ## Returns the inverse hyperbolic cosine of `z`.
  274. result = ln(z + sqrt(z*z - T(1.0)))
  275. func tanh*[T](z: Complex[T]): Complex[T] =
  276. ## Returns the hyperbolic tangent of `z`.
  277. result = sinh(z) / cosh(z)
  278. func arctanh*[T](z: Complex[T]): Complex[T] =
  279. ## Returns the inverse hyperbolic tangent of `z`.
  280. result = T(0.5) * (ln((T(1.0)+z) / (T(1.0)-z)))
  281. func coth*[T](z: Complex[T]): Complex[T] =
  282. ## Returns the hyperbolic cotangent of `z`.
  283. result = cosh(z) / sinh(z)
  284. func arccoth*[T](z: Complex[T]): Complex[T] =
  285. ## Returns the inverse hyperbolic cotangent of `z`.
  286. result = T(0.5) * (ln(T(1.0) + T(1.0)/z) - ln(T(1.0) - T(1.0)/z))
  287. func sech*[T](z: Complex[T]): Complex[T] =
  288. ## Returns the hyperbolic secant of `z`.
  289. result = T(2.0) / (exp(z) + exp(-z))
  290. func arcsech*[T](z: Complex[T]): Complex[T] =
  291. ## Returns the inverse hyperbolic secant of `z`.
  292. result = ln(1.0/z + sqrt(T(1.0)/z+T(1.0)) * sqrt(T(1.0)/z-T(1.0)))
  293. func csch*[T](z: Complex[T]): Complex[T] =
  294. ## Returns the hyperbolic cosecant of `z`.
  295. result = T(2.0) / (exp(z) - exp(-z))
  296. func arccsch*[T](z: Complex[T]): Complex[T] =
  297. ## Returns the inverse hyperbolic cosecant of `z`.
  298. result = ln(T(1.0)/z + sqrt(T(1.0)/(z*z) + T(1.0)))
  299. func phase*[T](z: Complex[T]): T =
  300. ## Returns the phase (or argument) of `z`, that is the angle in polar representation.
  301. ##
  302. ## | `result = arctan2(z.im, z.re)`
  303. arctan2(z.im, z.re)
  304. func polar*[T](z: Complex[T]): tuple[r, phi: T] =
  305. ## Returns `z` in polar coordinates.
  306. ##
  307. ## | `result.r = abs(z)`
  308. ## | `result.phi = phase(z)`
  309. ##
  310. ## **See also:**
  311. ## * `rect func<#rect,T,T>`_ for the inverse operation
  312. (r: abs(z), phi: phase(z))
  313. func rect*[T](r, phi: T): Complex[T] =
  314. ## Returns the complex number with polar coordinates `r` and `phi`.
  315. ##
  316. ## | `result.re = r * cos(phi)`
  317. ## | `result.im = r * sin(phi)`
  318. ##
  319. ## **See also:**
  320. ## * `polar func<#polar,Complex[T]>`_ for the inverse operation
  321. complex(r * cos(phi), r * sin(phi))
  322. func `$`*(z: Complex): string =
  323. ## Returns `z`'s string representation as `"(re, im)"`.
  324. runnableExamples:
  325. doAssert $complex(1.0, 2.0) == "(1.0, 2.0)"
  326. result = "(" & $z.re & ", " & $z.im & ")"
  327. {.pop.}