math_gamma.odin 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package math
  2. // The original C code, the long comment, and the constants
  3. // below are from http://netlib.sandia.gov/cephes/cprob/gamma.c.
  4. //
  5. // tgamma.c
  6. //
  7. // Gamma function
  8. //
  9. // SYNOPSIS:
  10. //
  11. // double x, y, tgamma();
  12. // extern int signgam;
  13. //
  14. // y = tgamma( x );
  15. //
  16. // DESCRIPTION:
  17. //
  18. // Returns gamma function of the argument. The result is
  19. // correctly signed, and the sign (+1 or -1) is also
  20. // returned in a global (extern) variable named signgam.
  21. // This variable is also filled in by the logarithmic gamma
  22. // function lgamma().
  23. //
  24. // Arguments |x| <= 34 are reduced by recurrence and the function
  25. // approximated by a rational function of degree 6/7 in the
  26. // interval (2,3). Large arguments are handled by Stirling's
  27. // formula. Large negative arguments are made positive using
  28. // a reflection formula.
  29. //
  30. // ACCURACY:
  31. //
  32. // Relative error:
  33. // arithmetic domain # trials peak rms
  34. // DEC -34, 34 10000 1.3e-16 2.5e-17
  35. // IEEE -170,-33 20000 2.3e-15 3.3e-16
  36. // IEEE -33, 33 20000 9.4e-16 2.2e-16
  37. // IEEE 33, 171.6 20000 2.3e-15 3.2e-16
  38. //
  39. // Error for arguments outside the test range will be larger
  40. // owing to error amplification by the exponential function.
  41. //
  42. // Cephes Math Library Release 2.8: June, 2000
  43. // Copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier
  44. //
  45. // The readme file at http://netlib.sandia.gov/cephes/ says:
  46. // Some software in this archive may be from the book _Methods and
  47. // Programs for Mathematical Functions_ (Prentice-Hall or Simon & Schuster
  48. // International, 1989) or from the Cephes Mathematical Library, a
  49. // commercial product. In either event, it is copyrighted by the author.
  50. // What you see here may be used freely but it comes with no support or
  51. // guarantee.
  52. //
  53. // The two known misprints in the book are repaired here in the
  54. // source listings for the gamma function and the incomplete beta
  55. // integral.
  56. //
  57. // Stephen L. Moshier
  58. // [email protected]
  59. // Gamma function computed by Stirling's formula.
  60. // The pair of results must be multiplied together to get the actual answer.
  61. // The multiplication is left to the caller so that, if careful, the caller can avoid
  62. // infinity for 172 <= x <= 180.
  63. // The polynomial is valid for 33 <= x <= 172; larger values are only used
  64. // in reciprocal and produce denormalized floats. The lower precision there
  65. // masks any imprecision in the polynomial.
  66. @(private="file", require_results)
  67. stirling :: proc "contextless" (x: f64) -> (f64, f64) {
  68. @(static) gamS := [?]f64{
  69. +7.87311395793093628397e-04,
  70. -2.29549961613378126380e-04,
  71. -2.68132617805781232825e-03,
  72. +3.47222221605458667310e-03,
  73. +8.33333333333482257126e-02,
  74. }
  75. if x > 200 {
  76. return inf_f64(1), 1
  77. }
  78. SQRT_TWO_PI :: 0h40040d931ff62706 // 2.506628274631000502417
  79. MAX_STIRLING :: 143.01608
  80. w := 1 / x
  81. w = 1 + w*((((gamS[0]*w+gamS[1])*w+gamS[2])*w+gamS[3])*w+gamS[4])
  82. y1 := exp(x)
  83. y2 := 1.0
  84. if x > MAX_STIRLING { // avoid pow() overflow
  85. v := pow(x, 0.5*x-0.25)
  86. y1, y2 = v, v/y1
  87. } else {
  88. y1 = pow(x, x-0.5) / y1
  89. }
  90. return y1, SQRT_TWO_PI * w * y2
  91. }
  92. @(require_results)
  93. gamma_f64 :: proc "contextless" (x: f64) -> f64 {
  94. is_neg_int :: proc "contextless" (x: f64) -> bool {
  95. if x < 0 {
  96. _, xf := modf(x)
  97. return xf == 0
  98. }
  99. return false
  100. }
  101. @(static) gamP := [?]f64{
  102. 1.60119522476751861407e-04,
  103. 1.19135147006586384913e-03,
  104. 1.04213797561761569935e-02,
  105. 4.76367800457137231464e-02,
  106. 2.07448227648435975150e-01,
  107. 4.94214826801497100753e-01,
  108. 9.99999999999999996796e-01,
  109. }
  110. @(static) gamQ := [?]f64{
  111. -2.31581873324120129819e-05,
  112. +5.39605580493303397842e-04,
  113. -4.45641913851797240494e-03,
  114. +1.18139785222060435552e-02,
  115. +3.58236398605498653373e-02,
  116. -2.34591795718243348568e-01,
  117. +7.14304917030273074085e-02,
  118. +1.00000000000000000320e+00,
  119. }
  120. EULER :: 0.57721566490153286060651209008240243104215933593992 // A001620
  121. switch {
  122. case is_neg_int(x) || is_inf(x, -1) || is_nan(x):
  123. return nan_f64()
  124. case is_inf(x, 1):
  125. return inf_f64(1)
  126. case x == 0:
  127. if signbit(x) {
  128. return inf_f64(-1)
  129. }
  130. return inf_f64(1)
  131. }
  132. x := x
  133. q := abs(x)
  134. p := floor(q)
  135. if q > 33 {
  136. if x >= 0 {
  137. y1, y2 := stirling(x)
  138. return y1 * y2
  139. }
  140. // Note: x is negative but (checked above) not a negative integer,
  141. // so x must be small enough to be in range for conversion to i64.
  142. // If |x| were >= 2⁶³ it would have to be an integer.
  143. signgam := 1
  144. if ip := i64(p); ip&1 == 0 {
  145. signgam = -1
  146. }
  147. z := q - p
  148. if z > 0.5 {
  149. p = p + 1
  150. z = q - p
  151. }
  152. z = q * sin(PI*z)
  153. if z == 0 {
  154. return inf_f64(signgam)
  155. }
  156. sq1, sq2 := stirling(q)
  157. absz := abs(z)
  158. d := absz * sq1 * sq2
  159. if is_inf(d, 0) {
  160. z = PI / absz / sq1 / sq2
  161. } else {
  162. z = PI / d
  163. }
  164. return f64(signgam) * z
  165. }
  166. // Reduce argument
  167. z := 1.0
  168. for x >= 3 {
  169. x = x - 1
  170. z = z * x
  171. }
  172. for x < 0 {
  173. if x > -1e-09 {
  174. if x == 0 {
  175. return inf_f64(1)
  176. }
  177. return z / ((1 + EULER*x) * x)
  178. }
  179. z = z / x
  180. x = x + 1
  181. }
  182. for x < 2 {
  183. if x < 1e-09 {
  184. if x == 0 {
  185. return inf_f64(1)
  186. }
  187. return z / ((1 + EULER*x) * x)
  188. }
  189. z = z / x
  190. x = x + 1
  191. }
  192. if x == 2 {
  193. return z
  194. }
  195. x = x - 2
  196. p = (((((x*gamP[0]+gamP[1])*x+gamP[2])*x+gamP[3])*x+gamP[4])*x+gamP[5])*x + gamP[6]
  197. q = ((((((x*gamQ[0]+gamQ[1])*x+gamQ[2])*x+gamQ[3])*x+gamQ[4])*x+gamQ[5])*x+gamQ[6])*x + gamQ[7]
  198. return z * p / q
  199. }
  200. @(require_results) gamma_f16 :: proc "contextless" (x: f16) -> f16 { return f16(gamma_f64(f64(x))) }
  201. @(require_results) gamma_f16le :: proc "contextless" (x: f16le) -> f16le { return f16le(gamma_f64(f64(x))) }
  202. @(require_results) gamma_f16be :: proc "contextless" (x: f16be) -> f16be { return f16be(gamma_f64(f64(x))) }
  203. @(require_results) gamma_f32 :: proc "contextless" (x: f32) -> f32 { return f32(gamma_f64(f64(x))) }
  204. @(require_results) gamma_f32le :: proc "contextless" (x: f32le) -> f32le { return f32le(gamma_f64(f64(x))) }
  205. @(require_results) gamma_f32be :: proc "contextless" (x: f32be) -> f32be { return f32be(gamma_f64(f64(x))) }
  206. @(require_results) gamma_f64le :: proc "contextless" (x: f64le) -> f64le { return f64le(gamma_f64(f64(x))) }
  207. @(require_results) gamma_f64be :: proc "contextless" (x: f64be) -> f64be { return f64be(gamma_f64(f64(x))) }
  208. gamma :: proc{
  209. gamma_f16, gamma_f16le, gamma_f16be,
  210. gamma_f32, gamma_f32le, gamma_f32be,
  211. gamma_f64, gamma_f64le, gamma_f64be,
  212. }