complex.nim 11 KB

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