complex.nim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 sgn*[T](z: Complex[T]): Complex[T] =
  69. ## Returns the phase of `z` as a unit complex number,
  70. ## or 0 if `z` is 0.
  71. let a = abs(z)
  72. if a != 0:
  73. result = z / a
  74. func conjugate*[T](z: Complex[T]): Complex[T] =
  75. ## Returns the complex conjugate of `z` (`complex(z.re, -z.im)`).
  76. result.re = z.re
  77. result.im = -z.im
  78. func inv*[T](z: Complex[T]): Complex[T] =
  79. ## Returns the multiplicative inverse of `z` (`1/z`).
  80. conjugate(z) / abs2(z)
  81. func `==`*[T](x, y: Complex[T]): bool =
  82. ## Compares two complex numbers for equality.
  83. result = x.re == y.re and x.im == y.im
  84. func `+`*[T](x: T; y: Complex[T]): Complex[T] =
  85. ## Adds a real number to a complex number.
  86. result.re = x + y.re
  87. result.im = y.im
  88. func `+`*[T](x: Complex[T]; y: T): Complex[T] =
  89. ## Adds a complex number to a real number.
  90. result.re = x.re + y
  91. result.im = x.im
  92. func `+`*[T](x, y: Complex[T]): Complex[T] =
  93. ## Adds two complex numbers.
  94. result.re = x.re + y.re
  95. result.im = x.im + y.im
  96. func `-`*[T](z: Complex[T]): Complex[T] =
  97. ## Unary minus for complex numbers.
  98. result.re = -z.re
  99. result.im = -z.im
  100. func `-`*[T](x: T; y: Complex[T]): Complex[T] =
  101. ## Subtracts a complex number from a real number.
  102. result.re = x - y.re
  103. result.im = -y.im
  104. func `-`*[T](x: Complex[T]; y: T): Complex[T] =
  105. ## Subtracts a real number from a complex number.
  106. result.re = x.re - y
  107. result.im = x.im
  108. func `-`*[T](x, y: Complex[T]): Complex[T] =
  109. ## Subtracts two complex numbers.
  110. result.re = x.re - y.re
  111. result.im = x.im - y.im
  112. func `*`*[T](x: T; y: Complex[T]): Complex[T] =
  113. ## Multiplies a real number with a complex number.
  114. result.re = x * y.re
  115. result.im = x * y.im
  116. func `*`*[T](x: Complex[T]; y: T): Complex[T] =
  117. ## Multiplies a complex number with a real number.
  118. result.re = x.re * y
  119. result.im = x.im * y
  120. func `*`*[T](x, y: Complex[T]): Complex[T] =
  121. ## Multiplies two complex numbers.
  122. result.re = x.re * y.re - x.im * y.im
  123. result.im = x.im * y.re + x.re * y.im
  124. func `/`*[T](x: Complex[T]; y: T): Complex[T] =
  125. ## Divides a complex number by a real number.
  126. result.re = x.re / y
  127. result.im = x.im / y
  128. func `/`*[T](x: T; y: Complex[T]): Complex[T] =
  129. ## Divides a real number by a complex number.
  130. result = x * inv(y)
  131. func `/`*[T](x, y: Complex[T]): Complex[T] =
  132. ## Divides two complex numbers.
  133. x * conjugate(y) / abs2(y)
  134. func `+=`*[T](x: var Complex[T]; y: Complex[T]) =
  135. ## Adds `y` to `x`.
  136. x.re += y.re
  137. x.im += y.im
  138. func `-=`*[T](x: var Complex[T]; y: Complex[T]) =
  139. ## Subtracts `y` from `x`.
  140. x.re -= y.re
  141. x.im -= y.im
  142. func `*=`*[T](x: var Complex[T]; y: Complex[T]) =
  143. ## Multiplies `x` by `y`.
  144. let im = x.im * y.re + x.re * y.im
  145. x.re = x.re * y.re - x.im * y.im
  146. x.im = im
  147. func `/=`*[T](x: var Complex[T]; y: Complex[T]) =
  148. ## Divides `x` by `y` in place.
  149. x = x / y
  150. func sqrt*[T](z: Complex[T]): Complex[T] =
  151. ## Computes the
  152. ## ([principal](https://en.wikipedia.org/wiki/Square_root#Principal_square_root_of_a_complex_number))
  153. ## square root of a complex number `z`.
  154. var x, y, w, r: T
  155. if z.re == 0.0 and z.im == 0.0:
  156. result = z
  157. else:
  158. x = abs(z.re)
  159. y = abs(z.im)
  160. if x >= y:
  161. r = y / x
  162. w = sqrt(x) * sqrt(0.5 * (1.0 + sqrt(1.0 + r * r)))
  163. else:
  164. r = x / y
  165. w = sqrt(y) * sqrt(0.5 * (r + sqrt(1.0 + r * r)))
  166. if z.re >= 0.0:
  167. result.re = w
  168. result.im = z.im / (w * 2.0)
  169. else:
  170. result.im = if z.im >= 0.0: w else: -w
  171. result.re = z.im / (result.im + result.im)
  172. func exp*[T](z: Complex[T]): Complex[T] =
  173. ## Computes the exponential function (`e^z`).
  174. let
  175. rho = exp(z.re)
  176. theta = z.im
  177. result.re = rho * cos(theta)
  178. result.im = rho * sin(theta)
  179. func ln*[T](z: Complex[T]): Complex[T] =
  180. ## Returns the
  181. ## ([principal value](https://en.wikipedia.org/wiki/Complex_logarithm#Principal_value)
  182. ## of the) natural logarithm of `z`.
  183. result.re = ln(abs(z))
  184. result.im = arctan2(z.im, z.re)
  185. func log10*[T](z: Complex[T]): Complex[T] =
  186. ## Returns the logarithm base 10 of `z`.
  187. ##
  188. ## **See also:**
  189. ## * `ln func<#ln,Complex[T]>`_
  190. result = ln(z) / ln(10.0)
  191. func log2*[T](z: Complex[T]): Complex[T] =
  192. ## Returns the logarithm base 2 of `z`.
  193. ##
  194. ## **See also:**
  195. ## * `ln func<#ln,Complex[T]>`_
  196. result = ln(z) / ln(2.0)
  197. func pow*[T](x, y: Complex[T]): Complex[T] =
  198. ## `x` raised to the power of `y`.
  199. if x.re == 0.0 and x.im == 0.0:
  200. if y.re == 0.0 and y.im == 0.0:
  201. result.re = 1.0
  202. result.im = 0.0
  203. else:
  204. result.re = 0.0
  205. result.im = 0.0
  206. elif y.re == 1.0 and y.im == 0.0:
  207. result = x
  208. elif y.re == -1.0 and y.im == 0.0:
  209. result = T(1.0) / x
  210. else:
  211. let
  212. rho = abs(x)
  213. theta = arctan2(x.im, x.re)
  214. s = pow(rho, y.re) * exp(-y.im * theta)
  215. r = y.re * theta + y.im * ln(rho)
  216. result.re = s * cos(r)
  217. result.im = s * sin(r)
  218. func pow*[T](x: Complex[T]; y: T): Complex[T] =
  219. ## The complex number `x` raised to the power of the real number `y`.
  220. pow(x, complex[T](y))
  221. func sin*[T](z: Complex[T]): Complex[T] =
  222. ## Returns the sine of `z`.
  223. result.re = sin(z.re) * cosh(z.im)
  224. result.im = cos(z.re) * sinh(z.im)
  225. func arcsin*[T](z: Complex[T]): Complex[T] =
  226. ## Returns the inverse sine of `z`.
  227. result = -im(T) * ln(im(T) * z + sqrt(T(1.0) - z*z))
  228. func cos*[T](z: Complex[T]): Complex[T] =
  229. ## Returns the cosine of `z`.
  230. result.re = cos(z.re) * cosh(z.im)
  231. result.im = -sin(z.re) * sinh(z.im)
  232. func arccos*[T](z: Complex[T]): Complex[T] =
  233. ## Returns the inverse cosine of `z`.
  234. result = -im(T) * ln(z + sqrt(z*z - T(1.0)))
  235. func tan*[T](z: Complex[T]): Complex[T] =
  236. ## Returns the tangent of `z`.
  237. result = sin(z) / cos(z)
  238. func arctan*[T](z: Complex[T]): Complex[T] =
  239. ## Returns the inverse tangent of `z`.
  240. result = T(0.5)*im(T) * (ln(T(1.0) - im(T)*z) - ln(T(1.0) + im(T)*z))
  241. func cot*[T](z: Complex[T]): Complex[T] =
  242. ## Returns the cotangent of `z`.
  243. result = cos(z)/sin(z)
  244. func arccot*[T](z: Complex[T]): Complex[T] =
  245. ## Returns the inverse cotangent of `z`.
  246. result = T(0.5)*im(T) * (ln(T(1.0) - im(T)/z) - ln(T(1.0) + im(T)/z))
  247. func sec*[T](z: Complex[T]): Complex[T] =
  248. ## Returns the secant of `z`.
  249. result = T(1.0) / cos(z)
  250. func arcsec*[T](z: Complex[T]): Complex[T] =
  251. ## Returns the inverse secant of `z`.
  252. result = -im(T) * ln(im(T) * sqrt(1.0 - 1.0/(z*z)) + T(1.0)/z)
  253. func csc*[T](z: Complex[T]): Complex[T] =
  254. ## Returns the cosecant of `z`.
  255. result = T(1.0) / sin(z)
  256. func arccsc*[T](z: Complex[T]): Complex[T] =
  257. ## Returns the inverse cosecant of `z`.
  258. result = -im(T) * ln(sqrt(T(1.0) - T(1.0)/(z*z)) + im(T)/z)
  259. func sinh*[T](z: Complex[T]): Complex[T] =
  260. ## Returns the hyperbolic sine of `z`.
  261. result = T(0.5) * (exp(z) - exp(-z))
  262. func arcsinh*[T](z: Complex[T]): Complex[T] =
  263. ## Returns the inverse hyperbolic sine of `z`.
  264. result = ln(z + sqrt(z*z + 1.0))
  265. func cosh*[T](z: Complex[T]): Complex[T] =
  266. ## Returns the hyperbolic cosine of `z`.
  267. result = T(0.5) * (exp(z) + exp(-z))
  268. func arccosh*[T](z: Complex[T]): Complex[T] =
  269. ## Returns the inverse hyperbolic cosine of `z`.
  270. result = ln(z + sqrt(z*z - T(1.0)))
  271. func tanh*[T](z: Complex[T]): Complex[T] =
  272. ## Returns the hyperbolic tangent of `z`.
  273. result = sinh(z) / cosh(z)
  274. func arctanh*[T](z: Complex[T]): Complex[T] =
  275. ## Returns the inverse hyperbolic tangent of `z`.
  276. result = T(0.5) * (ln((T(1.0)+z) / (T(1.0)-z)))
  277. func coth*[T](z: Complex[T]): Complex[T] =
  278. ## Returns the hyperbolic cotangent of `z`.
  279. result = cosh(z) / sinh(z)
  280. func arccoth*[T](z: Complex[T]): Complex[T] =
  281. ## Returns the inverse hyperbolic cotangent of `z`.
  282. result = T(0.5) * (ln(T(1.0) + T(1.0)/z) - ln(T(1.0) - T(1.0)/z))
  283. func sech*[T](z: Complex[T]): Complex[T] =
  284. ## Returns the hyperbolic secant of `z`.
  285. result = T(2.0) / (exp(z) + exp(-z))
  286. func arcsech*[T](z: Complex[T]): Complex[T] =
  287. ## Returns the inverse hyperbolic secant of `z`.
  288. result = ln(1.0/z + sqrt(T(1.0)/z+T(1.0)) * sqrt(T(1.0)/z-T(1.0)))
  289. func csch*[T](z: Complex[T]): Complex[T] =
  290. ## Returns the hyperbolic cosecant of `z`.
  291. result = T(2.0) / (exp(z) - exp(-z))
  292. func arccsch*[T](z: Complex[T]): Complex[T] =
  293. ## Returns the inverse hyperbolic cosecant of `z`.
  294. result = ln(T(1.0)/z + sqrt(T(1.0)/(z*z) + T(1.0)))
  295. func phase*[T](z: Complex[T]): T =
  296. ## Returns the phase (or argument) of `z`, that is the angle in polar representation.
  297. ##
  298. ## | `result = arctan2(z.im, z.re)`
  299. arctan2(z.im, z.re)
  300. func polar*[T](z: Complex[T]): tuple[r, phi: T] =
  301. ## Returns `z` in polar coordinates.
  302. ##
  303. ## | `result.r = abs(z)`
  304. ## | `result.phi = phase(z)`
  305. ##
  306. ## **See also:**
  307. ## * `rect func<#rect,T,T>`_ for the inverse operation
  308. (r: abs(z), phi: phase(z))
  309. func rect*[T](r, phi: T): Complex[T] =
  310. ## Returns the complex number with polar coordinates `r` and `phi`.
  311. ##
  312. ## | `result.re = r * cos(phi)`
  313. ## | `result.im = r * sin(phi)`
  314. ##
  315. ## **See also:**
  316. ## * `polar func<#polar,Complex[T]>`_ for the inverse operation
  317. complex(r * cos(phi), r * sin(phi))
  318. func `$`*(z: Complex): string =
  319. ## Returns `z`'s string representation as `"(re, im)"`.
  320. runnableExamples:
  321. doAssert $complex(1.0, 2.0) == "(1.0, 2.0)"
  322. result = "(" & $z.re & ", " & $z.im & ")"
  323. {.pop.}