complex.nim 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. let
  16. z1 = complex(1.0, 2.0)
  17. z2 = complex(3.0, -4.0)
  18. assert almostEqual(z1 + z2, complex(4.0, -2.0))
  19. assert almostEqual(z1 - z2, complex(-2.0, 6.0))
  20. assert almostEqual(z1 * z2, complex(11.0, 2.0))
  21. assert almostEqual(z1 / z2, complex(-0.2, 0.4))
  22. assert almostEqual(abs(z1), sqrt(5.0))
  23. assert almostEqual(conjugate(z1), complex(1.0, -2.0))
  24. let (r, phi) = z1.polar
  25. assert almostEqual(rect(r, phi), z1)
  26. {.push checks: off, line_dir: off, stack_trace: off, debugger: off.}
  27. # the user does not want to trace a part of the standard library!
  28. import std/[math, strformat]
  29. type
  30. Complex*[T: SomeFloat] = object
  31. ## A complex number, consisting of a real and an imaginary part.
  32. re*, im*: T
  33. Complex64* = Complex[float64]
  34. ## Alias for a complex number using 64-bit floats.
  35. Complex32* = Complex[float32]
  36. ## Alias for a complex number using 32-bit floats.
  37. func complex*[T: SomeFloat](re: T; im: T = 0.0): Complex[T] =
  38. ## Returns a `Complex[T]` with real part `re` and imaginary part `im`.
  39. result.re = re
  40. result.im = im
  41. func complex32*(re: float32; im: float32 = 0.0): Complex32 =
  42. ## Returns a `Complex32` with real part `re` and imaginary part `im`.
  43. result.re = re
  44. result.im = im
  45. func complex64*(re: float64; im: float64 = 0.0): Complex64 =
  46. ## Returns a `Complex64` with real part `re` and imaginary part `im`.
  47. result.re = re
  48. result.im = im
  49. template im*(arg: typedesc[float32]): Complex32 = complex32(0, 1)
  50. ## Returns the imaginary unit (`complex32(0, 1)`).
  51. template im*(arg: typedesc[float64]): Complex64 = complex64(0, 1)
  52. ## Returns the imaginary unit (`complex64(0, 1)`).
  53. template im*(arg: float32): Complex32 = complex32(0, arg)
  54. ## Returns `arg` as an imaginary number (`complex32(0, arg)`).
  55. template im*(arg: float64): Complex64 = complex64(0, arg)
  56. ## Returns `arg` as an imaginary number (`complex64(0, arg)`).
  57. func abs*[T](z: Complex[T]): T =
  58. ## Returns the absolute value of `z`,
  59. ## that is the distance from (0, 0) to `z`.
  60. result = hypot(z.re, z.im)
  61. func abs2*[T](z: Complex[T]): T =
  62. ## Returns the squared absolute value of `z`,
  63. ## that is the squared distance from (0, 0) to `z`.
  64. ## This is more efficient than `abs(z) ^ 2`.
  65. result = z.re * z.re + z.im * z.im
  66. func sgn*[T](z: Complex[T]): Complex[T] =
  67. ## Returns the phase of `z` as a unit complex number,
  68. ## or 0 if `z` is 0.
  69. let a = abs(z)
  70. if a != 0:
  71. result = z / a
  72. func conjugate*[T](z: Complex[T]): Complex[T] =
  73. ## Returns the complex conjugate of `z` (`complex(z.re, -z.im)`).
  74. result.re = z.re
  75. result.im = -z.im
  76. func inv*[T](z: Complex[T]): Complex[T] =
  77. ## Returns the multiplicative inverse of `z` (`1/z`).
  78. conjugate(z) / abs2(z)
  79. func `==`*[T](x, y: Complex[T]): bool =
  80. ## Compares two complex numbers for equality.
  81. result = x.re == y.re and x.im == y.im
  82. func `+`*[T](x: T; y: Complex[T]): Complex[T] =
  83. ## Adds a real number to a complex number.
  84. result.re = x + y.re
  85. result.im = y.im
  86. func `+`*[T](x: Complex[T]; y: T): Complex[T] =
  87. ## Adds a complex number to a real number.
  88. result.re = x.re + y
  89. result.im = x.im
  90. func `+`*[T](x, y: Complex[T]): Complex[T] =
  91. ## Adds two complex numbers.
  92. result.re = x.re + y.re
  93. result.im = x.im + y.im
  94. func `-`*[T](z: Complex[T]): Complex[T] =
  95. ## Unary minus for complex numbers.
  96. result.re = -z.re
  97. result.im = -z.im
  98. func `-`*[T](x: T; y: Complex[T]): Complex[T] =
  99. ## Subtracts a complex number from a real number.
  100. result.re = x - y.re
  101. result.im = -y.im
  102. func `-`*[T](x: Complex[T]; y: T): Complex[T] =
  103. ## Subtracts a real number from a complex number.
  104. result.re = x.re - y
  105. result.im = x.im
  106. func `-`*[T](x, y: Complex[T]): Complex[T] =
  107. ## Subtracts two complex numbers.
  108. result.re = x.re - y.re
  109. result.im = x.im - y.im
  110. func `*`*[T](x: T; y: Complex[T]): Complex[T] =
  111. ## Multiplies a real number with a complex number.
  112. result.re = x * y.re
  113. result.im = x * y.im
  114. func `*`*[T](x: Complex[T]; y: T): Complex[T] =
  115. ## Multiplies a complex number with a real number.
  116. result.re = x.re * y
  117. result.im = x.im * y
  118. func `*`*[T](x, y: Complex[T]): Complex[T] =
  119. ## Multiplies two complex numbers.
  120. result.re = x.re * y.re - x.im * y.im
  121. result.im = x.im * y.re + x.re * y.im
  122. func `/`*[T](x: Complex[T]; y: T): Complex[T] =
  123. ## Divides a complex number by a real number.
  124. result.re = x.re / y
  125. result.im = x.im / y
  126. func `/`*[T](x: T; y: Complex[T]): Complex[T] =
  127. ## Divides a real number by a complex number.
  128. result = x * inv(y)
  129. func `/`*[T](x, y: Complex[T]): Complex[T] =
  130. ## Divides two complex numbers.
  131. x * conjugate(y) / abs2(y)
  132. func `+=`*[T](x: var Complex[T]; y: Complex[T]) =
  133. ## Adds `y` to `x`.
  134. x.re += y.re
  135. x.im += y.im
  136. func `-=`*[T](x: var Complex[T]; y: Complex[T]) =
  137. ## Subtracts `y` from `x`.
  138. x.re -= y.re
  139. x.im -= y.im
  140. func `*=`*[T](x: var Complex[T]; y: Complex[T]) =
  141. ## Multiplies `x` by `y`.
  142. let im = x.im * y.re + x.re * y.im
  143. x.re = x.re * y.re - x.im * y.im
  144. x.im = im
  145. func `/=`*[T](x: var Complex[T]; y: Complex[T]) =
  146. ## Divides `x` by `y` in place.
  147. x = x / y
  148. func sqrt*[T](z: Complex[T]): Complex[T] =
  149. ## Computes the
  150. ## ([principal](https://en.wikipedia.org/wiki/Square_root#Principal_square_root_of_a_complex_number))
  151. ## square root of a complex number `z`.
  152. var x, y, w, r: T
  153. if z.re == 0.0 and z.im == 0.0:
  154. result = z
  155. else:
  156. x = abs(z.re)
  157. y = abs(z.im)
  158. if x >= y:
  159. r = y / x
  160. w = sqrt(x) * sqrt(0.5 * (1.0 + sqrt(1.0 + r * r)))
  161. else:
  162. r = x / y
  163. w = sqrt(y) * sqrt(0.5 * (r + sqrt(1.0 + r * r)))
  164. if z.re >= 0.0:
  165. result.re = w
  166. result.im = z.im / (w * 2.0)
  167. else:
  168. result.im = if z.im >= 0.0: w else: -w
  169. result.re = z.im / (result.im + result.im)
  170. func exp*[T](z: Complex[T]): Complex[T] =
  171. ## Computes the exponential function (`e^z`).
  172. let
  173. rho = exp(z.re)
  174. theta = z.im
  175. result.re = rho * cos(theta)
  176. result.im = rho * sin(theta)
  177. func ln*[T](z: Complex[T]): Complex[T] =
  178. ## Returns the
  179. ## ([principal value](https://en.wikipedia.org/wiki/Complex_logarithm#Principal_value)
  180. ## of the) natural logarithm of `z`.
  181. result.re = ln(abs(z))
  182. result.im = arctan2(z.im, z.re)
  183. func log10*[T](z: Complex[T]): Complex[T] =
  184. ## Returns the logarithm base 10 of `z`.
  185. ##
  186. ## **See also:**
  187. ## * `ln func<#ln,Complex[T]>`_
  188. result = ln(z) / ln(10.0)
  189. func log2*[T](z: Complex[T]): Complex[T] =
  190. ## Returns the logarithm base 2 of `z`.
  191. ##
  192. ## **See also:**
  193. ## * `ln func<#ln,Complex[T]>`_
  194. result = ln(z) / ln(2.0)
  195. func pow*[T](x, y: Complex[T]): Complex[T] =
  196. ## `x` raised to the power of `y`.
  197. if x.re == 0.0 and x.im == 0.0:
  198. if y.re == 0.0 and y.im == 0.0:
  199. result.re = 1.0
  200. result.im = 0.0
  201. else:
  202. result.re = 0.0
  203. result.im = 0.0
  204. elif y.im == 0.0:
  205. if y.re == 1.0:
  206. result = x
  207. elif y.re == -1.0:
  208. result = T(1.0) / x
  209. elif y.re == 2.0:
  210. result = x * x
  211. elif y.re == 0.5:
  212. result = sqrt(x)
  213. elif x.im == 0.0:
  214. # Revert to real pow when both base and exponent are real
  215. result.re = pow(x.re, y.re)
  216. result.im = 0.0
  217. else:
  218. # Special case when the exponent is real
  219. let
  220. rho = abs(x)
  221. theta = arctan2(x.im, x.re)
  222. s = pow(rho, y.re)
  223. r = y.re * theta
  224. result.re = s * cos(r)
  225. result.im = s * sin(r)
  226. elif x.im == 0.0 and x.re == E:
  227. # Special case Euler's formula
  228. result = exp(y)
  229. else:
  230. let
  231. rho = abs(x)
  232. theta = arctan2(x.im, x.re)
  233. s = pow(rho, y.re) * exp(-y.im * theta)
  234. r = y.re * theta + y.im * ln(rho)
  235. result.re = s * cos(r)
  236. result.im = s * sin(r)
  237. func pow*[T](x: Complex[T]; y: T): Complex[T] =
  238. ## The complex number `x` raised to the power of the real number `y`.
  239. pow(x, complex[T](y))
  240. func sin*[T](z: Complex[T]): Complex[T] =
  241. ## Returns the sine of `z`.
  242. result.re = sin(z.re) * cosh(z.im)
  243. result.im = cos(z.re) * sinh(z.im)
  244. func arcsin*[T](z: Complex[T]): Complex[T] =
  245. ## Returns the inverse sine of `z`.
  246. result = -im(T) * ln(im(T) * z + sqrt(T(1.0) - z*z))
  247. func cos*[T](z: Complex[T]): Complex[T] =
  248. ## Returns the cosine of `z`.
  249. result.re = cos(z.re) * cosh(z.im)
  250. result.im = -sin(z.re) * sinh(z.im)
  251. func arccos*[T](z: Complex[T]): Complex[T] =
  252. ## Returns the inverse cosine of `z`.
  253. result = -im(T) * ln(z + sqrt(z*z - T(1.0)))
  254. func tan*[T](z: Complex[T]): Complex[T] =
  255. ## Returns the tangent of `z`.
  256. result = sin(z) / cos(z)
  257. func arctan*[T](z: Complex[T]): Complex[T] =
  258. ## Returns the inverse tangent of `z`.
  259. result = T(0.5)*im(T) * (ln(T(1.0) - im(T)*z) - ln(T(1.0) + im(T)*z))
  260. func cot*[T](z: Complex[T]): Complex[T] =
  261. ## Returns the cotangent of `z`.
  262. result = cos(z)/sin(z)
  263. func arccot*[T](z: Complex[T]): Complex[T] =
  264. ## Returns the inverse cotangent of `z`.
  265. result = T(0.5)*im(T) * (ln(T(1.0) - im(T)/z) - ln(T(1.0) + im(T)/z))
  266. func sec*[T](z: Complex[T]): Complex[T] =
  267. ## Returns the secant of `z`.
  268. result = T(1.0) / cos(z)
  269. func arcsec*[T](z: Complex[T]): Complex[T] =
  270. ## Returns the inverse secant of `z`.
  271. result = -im(T) * ln(im(T) * sqrt(1.0 - 1.0/(z*z)) + T(1.0)/z)
  272. func csc*[T](z: Complex[T]): Complex[T] =
  273. ## Returns the cosecant of `z`.
  274. result = T(1.0) / sin(z)
  275. func arccsc*[T](z: Complex[T]): Complex[T] =
  276. ## Returns the inverse cosecant of `z`.
  277. result = -im(T) * ln(sqrt(T(1.0) - T(1.0)/(z*z)) + im(T)/z)
  278. func sinh*[T](z: Complex[T]): Complex[T] =
  279. ## Returns the hyperbolic sine of `z`.
  280. result = T(0.5) * (exp(z) - exp(-z))
  281. func arcsinh*[T](z: Complex[T]): Complex[T] =
  282. ## Returns the inverse hyperbolic sine of `z`.
  283. result = ln(z + sqrt(z*z + 1.0))
  284. func cosh*[T](z: Complex[T]): Complex[T] =
  285. ## Returns the hyperbolic cosine of `z`.
  286. result = T(0.5) * (exp(z) + exp(-z))
  287. func arccosh*[T](z: Complex[T]): Complex[T] =
  288. ## Returns the inverse hyperbolic cosine of `z`.
  289. result = ln(z + sqrt(z*z - T(1.0)))
  290. func tanh*[T](z: Complex[T]): Complex[T] =
  291. ## Returns the hyperbolic tangent of `z`.
  292. result = sinh(z) / cosh(z)
  293. func arctanh*[T](z: Complex[T]): Complex[T] =
  294. ## Returns the inverse hyperbolic tangent of `z`.
  295. result = T(0.5) * (ln((T(1.0)+z) / (T(1.0)-z)))
  296. func coth*[T](z: Complex[T]): Complex[T] =
  297. ## Returns the hyperbolic cotangent of `z`.
  298. result = cosh(z) / sinh(z)
  299. func arccoth*[T](z: Complex[T]): Complex[T] =
  300. ## Returns the inverse hyperbolic cotangent of `z`.
  301. result = T(0.5) * (ln(T(1.0) + T(1.0)/z) - ln(T(1.0) - T(1.0)/z))
  302. func sech*[T](z: Complex[T]): Complex[T] =
  303. ## Returns the hyperbolic secant of `z`.
  304. result = T(2.0) / (exp(z) + exp(-z))
  305. func arcsech*[T](z: Complex[T]): Complex[T] =
  306. ## Returns the inverse hyperbolic secant of `z`.
  307. result = ln(1.0/z + sqrt(T(1.0)/z+T(1.0)) * sqrt(T(1.0)/z-T(1.0)))
  308. func csch*[T](z: Complex[T]): Complex[T] =
  309. ## Returns the hyperbolic cosecant of `z`.
  310. result = T(2.0) / (exp(z) - exp(-z))
  311. func arccsch*[T](z: Complex[T]): Complex[T] =
  312. ## Returns the inverse hyperbolic cosecant of `z`.
  313. result = ln(T(1.0)/z + sqrt(T(1.0)/(z*z) + T(1.0)))
  314. func phase*[T](z: Complex[T]): T =
  315. ## Returns the phase (or argument) of `z`, that is the angle in polar representation.
  316. ##
  317. ## | `result = arctan2(z.im, z.re)`
  318. arctan2(z.im, z.re)
  319. func polar*[T](z: Complex[T]): tuple[r, phi: T] =
  320. ## Returns `z` in polar coordinates.
  321. ##
  322. ## | `result.r = abs(z)`
  323. ## | `result.phi = phase(z)`
  324. ##
  325. ## **See also:**
  326. ## * `rect func<#rect,T,T>`_ for the inverse operation
  327. (r: abs(z), phi: phase(z))
  328. func rect*[T](r, phi: T): Complex[T] =
  329. ## Returns the complex number with polar coordinates `r` and `phi`.
  330. ##
  331. ## | `result.re = r * cos(phi)`
  332. ## | `result.im = r * sin(phi)`
  333. ##
  334. ## **See also:**
  335. ## * `polar func<#polar,Complex[T]>`_ for the inverse operation
  336. complex(r * cos(phi), r * sin(phi))
  337. func almostEqual*[T: SomeFloat](x, y: Complex[T]; unitsInLastPlace: Natural = 4): bool =
  338. ## Checks if two complex values are almost equal, using the
  339. ## [machine epsilon](https://en.wikipedia.org/wiki/Machine_epsilon).
  340. ##
  341. ## Two complex values are considered almost equal if their real and imaginary
  342. ## components are almost equal.
  343. ##
  344. ## `unitsInLastPlace` is the max number of
  345. ## [units in the last place](https://en.wikipedia.org/wiki/Unit_in_the_last_place)
  346. ## difference tolerated when comparing two numbers. The larger the value, the
  347. ## more error is allowed. A `0` value means that two numbers must be exactly the
  348. ## same to be considered equal.
  349. ##
  350. ## The machine epsilon has to be scaled to the magnitude of the values used
  351. ## and multiplied by the desired precision in ULPs unless the difference is
  352. ## subnormal.
  353. almostEqual(x.re, y.re, unitsInLastPlace = unitsInLastPlace) and
  354. almostEqual(x.im, y.im, unitsInLastPlace = unitsInLastPlace)
  355. func `$`*(z: Complex): string =
  356. ## Returns `z`'s string representation as `"(re, im)"`.
  357. runnableExamples:
  358. doAssert $complex(1.0, 2.0) == "(1.0, 2.0)"
  359. result = "(" & $z.re & ", " & $z.im & ")"
  360. proc formatValueAsTuple(result: var string; value: Complex; specifier: string) =
  361. ## Format implementation for `Complex` representing the value as a (real, imaginary) tuple.
  362. result.add "("
  363. formatValue(result, value.re, specifier)
  364. result.add ", "
  365. formatValue(result, value.im, specifier)
  366. result.add ")"
  367. proc formatValueAsComplexNumber(result: var string; value: Complex; specifier: string) =
  368. ## Format implementation for `Complex` representing the value as a (RE+IMj) number
  369. ## By default, the real and imaginary parts are formatted using the general ('g') format
  370. let specifier = if specifier.contains({'e', 'E', 'f', 'F', 'g', 'G'}):
  371. specifier.replace("j")
  372. else:
  373. specifier.replace('j', 'g')
  374. result.add "("
  375. formatValue(result, value.re, specifier)
  376. if value.im >= 0 and not specifier.contains({'+', '-'}):
  377. result.add "+"
  378. formatValue(result, value.im, specifier)
  379. result.add "j)"
  380. proc formatValue*(result: var string; value: Complex; specifier: string) =
  381. ## Standard format implementation for `Complex`. It makes little
  382. ## sense to call this directly, but it is required to exist
  383. ## by the `&` macro.
  384. ## For complex numbers, we add a specific 'j' specifier, which formats
  385. ## the value as (A+Bj) like in mathematics.
  386. if specifier.len == 0:
  387. result.add $value
  388. elif 'j' in specifier:
  389. formatValueAsComplexNumber(result, value, specifier)
  390. else:
  391. formatValueAsTuple(result, value, specifier)
  392. {.pop.}