cmplx_trig.odin 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. package math_cmplx
  2. import "core:math"
  3. import "core:math/bits"
  4. // The original C code, the long comment, and the constants
  5. // below are from http://netlib.sandia.gov/cephes/c9x-complex/clog.c.
  6. // The go code is a simplified version of the original C.
  7. //
  8. // Cephes Math Library Release 2.8: June, 2000
  9. // Copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier
  10. //
  11. // The readme file at http://netlib.sandia.gov/cephes/ says:
  12. // Some software in this archive may be from the book _Methods and
  13. // Programs for Mathematical Functions_ (Prentice-Hall or Simon & Schuster
  14. // International, 1989) or from the Cephes Mathematical Library, a
  15. // commercial product. In either event, it is copyrighted by the author.
  16. // What you see here may be used freely but it comes with no support or
  17. // guarantee.
  18. //
  19. // The two known misprints in the book are repaired here in the
  20. // source listings for the gamma function and the incomplete beta
  21. // integral.
  22. //
  23. // Stephen L. Moshier
  24. // [email protected]
  25. sin_complex128 :: proc "contextless" (x: complex128) -> complex128 {
  26. // Complex circular sine
  27. //
  28. // DESCRIPTION:
  29. //
  30. // If
  31. // z = x + iy,
  32. //
  33. // then
  34. //
  35. // w = sin x cosh y + i cos x sinh y.
  36. //
  37. // csin(z) = -i csinh(iz).
  38. //
  39. // ACCURACY:
  40. //
  41. // Relative error:
  42. // arithmetic domain # trials peak rms
  43. // DEC -10,+10 8400 5.3e-17 1.3e-17
  44. // IEEE -10,+10 30000 3.8e-16 1.0e-16
  45. // Also tested by csin(casin(z)) = z.
  46. switch re, im := real(x), imag(x); {
  47. case im == 0 && (math.is_inf(re, 0) || math.is_nan(re)):
  48. return complex(math.nan_f64(), im)
  49. case math.is_inf(im, 0):
  50. switch {
  51. case re == 0:
  52. return x
  53. case math.is_inf(re, 0) || math.is_nan(re):
  54. return complex(math.nan_f64(), im)
  55. }
  56. case re == 0 && math.is_nan(im):
  57. return x
  58. }
  59. s, c := math.sincos(real(x))
  60. sh, ch := _sinhcosh_f64(imag(x))
  61. return complex(s*ch, c*sh)
  62. }
  63. cos_complex128 :: proc "contextless" (x: complex128) -> complex128 {
  64. // Complex circular cosine
  65. //
  66. // DESCRIPTION:
  67. //
  68. // If
  69. // z = x + iy,
  70. //
  71. // then
  72. //
  73. // w = cos x cosh y - i sin x sinh y.
  74. //
  75. // ACCURACY:
  76. //
  77. // Relative error:
  78. // arithmetic domain # trials peak rms
  79. // DEC -10,+10 8400 4.5e-17 1.3e-17
  80. // IEEE -10,+10 30000 3.8e-16 1.0e-16
  81. switch re, im := real(x), imag(x); {
  82. case im == 0 && (math.is_inf(re, 0) || math.is_nan(re)):
  83. return complex(math.nan_f64(), -im*math.copy_sign(0, re))
  84. case math.is_inf(im, 0):
  85. switch {
  86. case re == 0:
  87. return complex(math.inf_f64(1), -re*math.copy_sign(0, im))
  88. case math.is_inf(re, 0) || math.is_nan(re):
  89. return complex(math.inf_f64(1), math.nan_f64())
  90. }
  91. case re == 0 && math.is_nan(im):
  92. return complex(math.nan_f64(), 0)
  93. }
  94. s, c := math.sincos(real(x))
  95. sh, ch := _sinhcosh_f64(imag(x))
  96. return complex(c*ch, -s*sh)
  97. }
  98. sinh_complex128 :: proc "contextless" (x: complex128) -> complex128 {
  99. // Complex hyperbolic sine
  100. //
  101. // DESCRIPTION:
  102. //
  103. // csinh z = (cexp(z) - cexp(-z))/2
  104. // = sinh x * cos y + i cosh x * sin y .
  105. //
  106. // ACCURACY:
  107. //
  108. // Relative error:
  109. // arithmetic domain # trials peak rms
  110. // IEEE -10,+10 30000 3.1e-16 8.2e-17
  111. switch re, im := real(x), imag(x); {
  112. case re == 0 && (math.is_inf(im, 0) || math.is_nan(im)):
  113. return complex(re, math.nan_f64())
  114. case math.is_inf(re, 0):
  115. switch {
  116. case im == 0:
  117. return complex(re, im)
  118. case math.is_inf(im, 0) || math.is_nan(im):
  119. return complex(re, math.nan_f64())
  120. }
  121. case im == 0 && math.is_nan(re):
  122. return complex(math.nan_f64(), im)
  123. }
  124. s, c := math.sincos(imag(x))
  125. sh, ch := _sinhcosh_f64(real(x))
  126. return complex(c*sh, s*ch)
  127. }
  128. cosh_complex128 :: proc "contextless" (x: complex128) -> complex128 {
  129. // Complex hyperbolic cosine
  130. //
  131. // DESCRIPTION:
  132. //
  133. // ccosh(z) = cosh x cos y + i sinh x sin y .
  134. //
  135. // ACCURACY:
  136. //
  137. // Relative error:
  138. // arithmetic domain # trials peak rms
  139. // IEEE -10,+10 30000 2.9e-16 8.1e-17
  140. switch re, im := real(x), imag(x); {
  141. case re == 0 && (math.is_inf(im, 0) || math.is_nan(im)):
  142. return complex(math.nan_f64(), re*math.copy_sign(0, im))
  143. case math.is_inf(re, 0):
  144. switch {
  145. case im == 0:
  146. return complex(math.inf_f64(1), im*math.copy_sign(0, re))
  147. case math.is_inf(im, 0) || math.is_nan(im):
  148. return complex(math.inf_f64(1), math.nan_f64())
  149. }
  150. case im == 0 && math.is_nan(re):
  151. return complex(math.nan_f64(), im)
  152. }
  153. s, c := math.sincos(imag(x))
  154. sh, ch := _sinhcosh_f64(real(x))
  155. return complex(c*ch, s*sh)
  156. }
  157. tan_complex128 :: proc "contextless" (x: complex128) -> complex128 {
  158. // Complex circular tangent
  159. //
  160. // DESCRIPTION:
  161. //
  162. // If
  163. // z = x + iy,
  164. //
  165. // then
  166. //
  167. // sin 2x + i sinh 2y
  168. // w = --------------------.
  169. // cos 2x + cosh 2y
  170. //
  171. // On the real axis the denominator is zero at odd multiples
  172. // of PI/2. The denominator is evaluated by its Taylor
  173. // series near these points.
  174. //
  175. // ctan(z) = -i ctanh(iz).
  176. //
  177. // ACCURACY:
  178. //
  179. // Relative error:
  180. // arithmetic domain # trials peak rms
  181. // DEC -10,+10 5200 7.1e-17 1.6e-17
  182. // IEEE -10,+10 30000 7.2e-16 1.2e-16
  183. // Also tested by ctan * ccot = 1 and catan(ctan(z)) = z.
  184. switch re, im := real(x), imag(x); {
  185. case math.is_inf(im, 0):
  186. switch {
  187. case math.is_inf(re, 0) || math.is_nan(re):
  188. return complex(math.copy_sign(0, re), math.copy_sign(1, im))
  189. }
  190. return complex(math.copy_sign(0, math.sin(2*re)), math.copy_sign(1, im))
  191. case re == 0 && math.is_nan(im):
  192. return x
  193. }
  194. d := math.cos(2*real(x)) + math.cosh(2*imag(x))
  195. if abs(d) < 0.25 {
  196. d = _tan_series_f64(x)
  197. }
  198. if d == 0 {
  199. return inf_complex128()
  200. }
  201. return complex(math.sin(2*real(x))/d, math.sinh(2*imag(x))/d)
  202. }
  203. tanh_complex128 :: proc "contextless" (x: complex128) -> complex128 {
  204. switch re, im := real(x), imag(x); {
  205. case math.is_inf(re, 0):
  206. switch {
  207. case math.is_inf(im, 0) || math.is_nan(im):
  208. return complex(math.copy_sign(1, re), math.copy_sign(0, im))
  209. }
  210. return complex(math.copy_sign(1, re), math.copy_sign(0, math.sin(2*im)))
  211. case im == 0 && math.is_nan(re):
  212. return x
  213. }
  214. d := math.cosh(2*real(x)) + math.cos(2*imag(x))
  215. if d == 0 {
  216. return inf_complex128()
  217. }
  218. return complex(math.sinh(2*real(x))/d, math.sin(2*imag(x))/d)
  219. }
  220. cot_complex128 :: proc "contextless" (x: complex128) -> complex128 {
  221. d := math.cosh(2*imag(x)) - math.cos(2*real(x))
  222. if abs(d) < 0.25 {
  223. d = _tan_series_f64(x)
  224. }
  225. if d == 0 {
  226. return inf_complex128()
  227. }
  228. return complex(math.sin(2*real(x))/d, -math.sinh(2*imag(x))/d)
  229. }
  230. @(private="file")
  231. _sinhcosh_f64 :: proc "contextless" (x: f64) -> (sh, ch: f64) {
  232. if abs(x) <= 0.5 {
  233. return math.sinh(x), math.cosh(x)
  234. }
  235. e := math.exp(x)
  236. ei := 0.5 / e
  237. e *= 0.5
  238. return e - ei, e + ei
  239. }
  240. // taylor series of cosh(2y) - cos(2x)
  241. @(private)
  242. _tan_series_f64 :: proc "contextless" (z: complex128) -> f64 {
  243. MACH_EPSILON :: 1.0 / (1 << 53)
  244. x := abs(2 * real(z))
  245. y := abs(2 * imag(z))
  246. x = _reduce_pi_f64(x)
  247. x, y = x * x, y * y
  248. x2, y2 := 1.0, 1.0
  249. f, rn, d := 1.0, 0.0, 0.0
  250. for {
  251. rn += 1
  252. f *= rn
  253. rn += 1
  254. f *= rn
  255. x2 *= x
  256. y2 *= y
  257. t := y2 + x2
  258. t /= f
  259. d += t
  260. rn += 1
  261. f *= rn
  262. rn += 1
  263. f *= rn
  264. x2 *= x
  265. y2 *= y
  266. t = y2 - x2
  267. t /= f
  268. d += t
  269. if !(abs(t/d) > MACH_EPSILON) { // don't use <=, because of floating point nonsense and NaN
  270. break
  271. }
  272. }
  273. return d
  274. }
  275. // _reduce_pi_f64 reduces the input argument x to the range (-PI/2, PI/2].
  276. // x must be greater than or equal to 0. For small arguments it
  277. // uses Cody-Waite reduction in 3 f64 parts based on:
  278. // "Elementary Function Evaluation: Algorithms and Implementation"
  279. // Jean-Michel Muller, 1997.
  280. // For very large arguments it uses Payne-Hanek range reduction based on:
  281. // "ARGUMENT REDUCTION FOR HUGE ARGUMENTS: Good to the Last Bit"
  282. @(private)
  283. _reduce_pi_f64 :: proc "contextless" (x: f64) -> f64 #no_bounds_check {
  284. x := x
  285. // REDUCE_THRESHOLD is the maximum value of x where the reduction using
  286. // Cody-Waite reduction still gives accurate results. This threshold
  287. // is set by t*PIn being representable as a f64 without error
  288. // where t is given by t = floor(x * (1 / PI)) and PIn are the leading partial
  289. // terms of PI. Since the leading terms, PI1 and PI2 below, have 30 and 32
  290. // trailing zero bits respectively, t should have less than 30 significant bits.
  291. // t < 1<<30 -> floor(x*(1/PI)+0.5) < 1<<30 -> x < (1<<30-1) * PI - 0.5
  292. // So, conservatively we can take x < 1<<30.
  293. REDUCE_THRESHOLD :: f64(1 << 30)
  294. if abs(x) < REDUCE_THRESHOLD {
  295. // Use Cody-Waite reduction in three parts.
  296. // PI1, PI2 and PI3 comprise an extended precision value of PI
  297. // such that PI ~= PI1 + PI2 + PI3. The parts are chosen so
  298. // that PI1 and PI2 have an approximately equal number of trailing
  299. // zero bits. This ensures that t*PI1 and t*PI2 are exact for
  300. // large integer values of t. The full precision PI3 ensures the
  301. // approximation of PI is accurate to 102 bits to handle cancellation
  302. // during subtraction.
  303. PI1 :: 0h400921fb40000000 // 3.141592502593994
  304. PI2 :: 0h3e84442d00000000 // 1.5099578831723193e-07
  305. PI3 :: 0h3d08469898cc5170 // 1.0780605716316238e-14
  306. t := x / math.PI
  307. t += 0.5
  308. t = f64(i64(t)) // i64(t) = the multiple
  309. return ((x - t*PI1) - t*PI2) - t*PI3
  310. }
  311. // Must apply Payne-Hanek range reduction
  312. MASK :: 0x7FF
  313. SHIFT :: 64 - 11 - 1
  314. BIAS :: 1023
  315. FRAC_MASK :: 1<<SHIFT - 1
  316. // Extract out the integer and exponent such that,
  317. // x = ix * 2 ** exp.
  318. ix := transmute(u64)(x)
  319. exp := int(ix>>SHIFT&MASK) - BIAS - SHIFT
  320. ix &= FRAC_MASK
  321. ix |= 1 << SHIFT
  322. // bdpi is the binary digits of 1/PI as a u64 array,
  323. // that is, 1/PI = SUM bdpi[i]*2^(-64*i).
  324. // 19 64-bit digits give 1216 bits of precision
  325. // to handle the largest possible f64 exponent.
  326. @static bdpi := [?]u64{
  327. 0x0000000000000000,
  328. 0x517cc1b727220a94,
  329. 0xfe13abe8fa9a6ee0,
  330. 0x6db14acc9e21c820,
  331. 0xff28b1d5ef5de2b0,
  332. 0xdb92371d2126e970,
  333. 0x0324977504e8c90e,
  334. 0x7f0ef58e5894d39f,
  335. 0x74411afa975da242,
  336. 0x74ce38135a2fbf20,
  337. 0x9cc8eb1cc1a99cfa,
  338. 0x4e422fc5defc941d,
  339. 0x8ffc4bffef02cc07,
  340. 0xf79788c5ad05368f,
  341. 0xb69b3f6793e584db,
  342. 0xa7a31fb34f2ff516,
  343. 0xba93dd63f5f2f8bd,
  344. 0x9e839cfbc5294975,
  345. 0x35fdafd88fc6ae84,
  346. 0x2b0198237e3db5d5,
  347. }
  348. // Use the exponent to extract the 3 appropriate u64 digits from bdpi,
  349. // B ~ (z0, z1, z2), such that the product leading digit has the exponent -64.
  350. // Note, exp >= 50 since x >= REDUCE_THRESHOLD and exp < 971 for maximum f64.
  351. digit, bitshift := uint(exp+64)/64, uint(exp+64)%64
  352. z0 := (bdpi[digit] << bitshift) | (bdpi[digit+1] >> (64 - bitshift))
  353. z1 := (bdpi[digit+1] << bitshift) | (bdpi[digit+2] >> (64 - bitshift))
  354. z2 := (bdpi[digit+2] << bitshift) | (bdpi[digit+3] >> (64 - bitshift))
  355. // Multiply mantissa by the digits and extract the upper two digits (hi, lo).
  356. z2hi, _ := bits.mul(z2, ix)
  357. z1hi, z1lo := bits.mul(z1, ix)
  358. z0lo := z0 * ix
  359. lo, c := bits.add(z1lo, z2hi, 0)
  360. hi, _ := bits.add(z0lo, z1hi, c)
  361. // Find the magnitude of the fraction.
  362. lz := uint(bits.leading_zeros(hi))
  363. e := u64(BIAS - (lz + 1))
  364. // Clear implicit mantissa bit and shift into place.
  365. hi = (hi << (lz + 1)) | (lo >> (64 - (lz + 1)))
  366. hi >>= 64 - SHIFT
  367. // Include the exponent and convert to a float.
  368. hi |= e << SHIFT
  369. x = transmute(f64)(hi)
  370. // map to (-PI/2, PI/2]
  371. if x > 0.5 {
  372. x -= 1
  373. }
  374. return math.PI * x
  375. }