lgamma.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package math
  5. /*
  6. Floating-point logarithm of the Gamma function.
  7. */
  8. // The original C code and the long comment below are
  9. // from FreeBSD's /usr/src/lib/msun/src/e_lgamma_r.c and
  10. // came with this notice. The go code is a simplified
  11. // version of the original C.
  12. //
  13. // ====================================================
  14. // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  15. //
  16. // Developed at SunPro, a Sun Microsystems, Inc. business.
  17. // Permission to use, copy, modify, and distribute this
  18. // software is freely granted, provided that this notice
  19. // is preserved.
  20. // ====================================================
  21. //
  22. // __ieee754_lgamma_r(x, signgamp)
  23. // Reentrant version of the logarithm of the Gamma function
  24. // with user provided pointer for the sign of Gamma(x).
  25. //
  26. // Method:
  27. // 1. Argument Reduction for 0 < x <= 8
  28. // Since gamma(1+s)=s*gamma(s), for x in [0,8], we may
  29. // reduce x to a number in [1.5,2.5] by
  30. // lgamma(1+s) = log(s) + lgamma(s)
  31. // for example,
  32. // lgamma(7.3) = log(6.3) + lgamma(6.3)
  33. // = log(6.3*5.3) + lgamma(5.3)
  34. // = log(6.3*5.3*4.3*3.3*2.3) + lgamma(2.3)
  35. // 2. Polynomial approximation of lgamma around its
  36. // minimum (ymin=1.461632144968362245) to maintain monotonicity.
  37. // On [ymin-0.23, ymin+0.27] (i.e., [1.23164,1.73163]), use
  38. // Let z = x-ymin;
  39. // lgamma(x) = -1.214862905358496078218 + z**2*poly(z)
  40. // poly(z) is a 14 degree polynomial.
  41. // 2. Rational approximation in the primary interval [2,3]
  42. // We use the following approximation:
  43. // s = x-2.0;
  44. // lgamma(x) = 0.5*s + s*P(s)/Q(s)
  45. // with accuracy
  46. // |P/Q - (lgamma(x)-0.5s)| < 2**-61.71
  47. // Our algorithms are based on the following observation
  48. //
  49. // zeta(2)-1 2 zeta(3)-1 3
  50. // lgamma(2+s) = s*(1-Euler) + --------- * s - --------- * s + ...
  51. // 2 3
  52. //
  53. // where Euler = 0.5772156649... is the Euler constant, which
  54. // is very close to 0.5.
  55. //
  56. // 3. For x>=8, we have
  57. // lgamma(x)~(x-0.5)log(x)-x+0.5*log(2pi)+1/(12x)-1/(360x**3)+....
  58. // (better formula:
  59. // lgamma(x)~(x-0.5)*(log(x)-1)-.5*(log(2pi)-1) + ...)
  60. // Let z = 1/x, then we approximation
  61. // f(z) = lgamma(x) - (x-0.5)(log(x)-1)
  62. // by
  63. // 3 5 11
  64. // w = w0 + w1*z + w2*z + w3*z + ... + w6*z
  65. // where
  66. // |w - f(z)| < 2**-58.74
  67. //
  68. // 4. For negative x, since (G is gamma function)
  69. // -x*G(-x)*G(x) = pi/sin(pi*x),
  70. // we have
  71. // G(x) = pi/(sin(pi*x)*(-x)*G(-x))
  72. // since G(-x) is positive, sign(G(x)) = sign(sin(pi*x)) for x<0
  73. // Hence, for x<0, signgam = sign(sin(pi*x)) and
  74. // lgamma(x) = log(|Gamma(x)|)
  75. // = log(pi/(|x*sin(pi*x)|)) - lgamma(-x);
  76. // Note: one should avoid computing pi*(-x) directly in the
  77. // computation of sin(pi*(-x)).
  78. //
  79. // 5. Special Cases
  80. // lgamma(2+s) ~ s*(1-Euler) for tiny s
  81. // lgamma(1)=lgamma(2)=0
  82. // lgamma(x) ~ -log(x) for tiny x
  83. // lgamma(0) = lgamma(inf) = inf
  84. // lgamma(-integer) = +-inf
  85. //
  86. //
  87. var _lgamA = [...]float64{
  88. 7.72156649015328655494e-02, // 0x3FB3C467E37DB0C8
  89. 3.22467033424113591611e-01, // 0x3FD4A34CC4A60FAD
  90. 6.73523010531292681824e-02, // 0x3FB13E001A5562A7
  91. 2.05808084325167332806e-02, // 0x3F951322AC92547B
  92. 7.38555086081402883957e-03, // 0x3F7E404FB68FEFE8
  93. 2.89051383673415629091e-03, // 0x3F67ADD8CCB7926B
  94. 1.19270763183362067845e-03, // 0x3F538A94116F3F5D
  95. 5.10069792153511336608e-04, // 0x3F40B6C689B99C00
  96. 2.20862790713908385557e-04, // 0x3F2CF2ECED10E54D
  97. 1.08011567247583939954e-04, // 0x3F1C5088987DFB07
  98. 2.52144565451257326939e-05, // 0x3EFA7074428CFA52
  99. 4.48640949618915160150e-05, // 0x3F07858E90A45837
  100. }
  101. var _lgamR = [...]float64{
  102. 1.0, // placeholder
  103. 1.39200533467621045958e+00, // 0x3FF645A762C4AB74
  104. 7.21935547567138069525e-01, // 0x3FE71A1893D3DCDC
  105. 1.71933865632803078993e-01, // 0x3FC601EDCCFBDF27
  106. 1.86459191715652901344e-02, // 0x3F9317EA742ED475
  107. 7.77942496381893596434e-04, // 0x3F497DDACA41A95B
  108. 7.32668430744625636189e-06, // 0x3EDEBAF7A5B38140
  109. }
  110. var _lgamS = [...]float64{
  111. -7.72156649015328655494e-02, // 0xBFB3C467E37DB0C8
  112. 2.14982415960608852501e-01, // 0x3FCB848B36E20878
  113. 3.25778796408930981787e-01, // 0x3FD4D98F4F139F59
  114. 1.46350472652464452805e-01, // 0x3FC2BB9CBEE5F2F7
  115. 2.66422703033638609560e-02, // 0x3F9B481C7E939961
  116. 1.84028451407337715652e-03, // 0x3F5E26B67368F239
  117. 3.19475326584100867617e-05, // 0x3F00BFECDD17E945
  118. }
  119. var _lgamT = [...]float64{
  120. 4.83836122723810047042e-01, // 0x3FDEF72BC8EE38A2
  121. -1.47587722994593911752e-01, // 0xBFC2E4278DC6C509
  122. 6.46249402391333854778e-02, // 0x3FB08B4294D5419B
  123. -3.27885410759859649565e-02, // 0xBFA0C9A8DF35B713
  124. 1.79706750811820387126e-02, // 0x3F9266E7970AF9EC
  125. -1.03142241298341437450e-02, // 0xBF851F9FBA91EC6A
  126. 6.10053870246291332635e-03, // 0x3F78FCE0E370E344
  127. -3.68452016781138256760e-03, // 0xBF6E2EFFB3E914D7
  128. 2.25964780900612472250e-03, // 0x3F6282D32E15C915
  129. -1.40346469989232843813e-03, // 0xBF56FE8EBF2D1AF1
  130. 8.81081882437654011382e-04, // 0x3F4CDF0CEF61A8E9
  131. -5.38595305356740546715e-04, // 0xBF41A6109C73E0EC
  132. 3.15632070903625950361e-04, // 0x3F34AF6D6C0EBBF7
  133. -3.12754168375120860518e-04, // 0xBF347F24ECC38C38
  134. 3.35529192635519073543e-04, // 0x3F35FD3EE8C2D3F4
  135. }
  136. var _lgamU = [...]float64{
  137. -7.72156649015328655494e-02, // 0xBFB3C467E37DB0C8
  138. 6.32827064025093366517e-01, // 0x3FE4401E8B005DFF
  139. 1.45492250137234768737e+00, // 0x3FF7475CD119BD6F
  140. 9.77717527963372745603e-01, // 0x3FEF497644EA8450
  141. 2.28963728064692451092e-01, // 0x3FCD4EAEF6010924
  142. 1.33810918536787660377e-02, // 0x3F8B678BBF2BAB09
  143. }
  144. var _lgamV = [...]float64{
  145. 1.0,
  146. 2.45597793713041134822e+00, // 0x4003A5D7C2BD619C
  147. 2.12848976379893395361e+00, // 0x40010725A42B18F5
  148. 7.69285150456672783825e-01, // 0x3FE89DFBE45050AF
  149. 1.04222645593369134254e-01, // 0x3FBAAE55D6537C88
  150. 3.21709242282423911810e-03, // 0x3F6A5ABB57D0CF61
  151. }
  152. var _lgamW = [...]float64{
  153. 4.18938533204672725052e-01, // 0x3FDACFE390C97D69
  154. 8.33333333333329678849e-02, // 0x3FB555555555553B
  155. -2.77777777728775536470e-03, // 0xBF66C16C16B02E5C
  156. 7.93650558643019558500e-04, // 0x3F4A019F98CF38B6
  157. -5.95187557450339963135e-04, // 0xBF4380CB8C0FE741
  158. 8.36339918996282139126e-04, // 0x3F4B67BA4CDAD5D1
  159. -1.63092934096575273989e-03, // 0xBF5AB89D0B9E43E4
  160. }
  161. // Lgamma returns the natural logarithm and sign (-1 or +1) of Gamma(x).
  162. //
  163. // Special cases are:
  164. // Lgamma(+Inf) = +Inf
  165. // Lgamma(0) = +Inf
  166. // Lgamma(-integer) = +Inf
  167. // Lgamma(-Inf) = -Inf
  168. // Lgamma(NaN) = NaN
  169. func Lgamma(x float64) (lgamma float64, sign int) {
  170. const (
  171. Ymin = 1.461632144968362245
  172. Two52 = 1 << 52 // 0x4330000000000000 ~4.5036e+15
  173. Two53 = 1 << 53 // 0x4340000000000000 ~9.0072e+15
  174. Two58 = 1 << 58 // 0x4390000000000000 ~2.8823e+17
  175. Tiny = 1.0 / (1 << 70) // 0x3b90000000000000 ~8.47033e-22
  176. Tc = 1.46163214496836224576e+00 // 0x3FF762D86356BE3F
  177. Tf = -1.21486290535849611461e-01 // 0xBFBF19B9BCC38A42
  178. // Tt = -(tail of Tf)
  179. Tt = -3.63867699703950536541e-18 // 0xBC50C7CAA48A971F
  180. )
  181. // special cases
  182. sign = 1
  183. switch {
  184. case IsNaN(x):
  185. lgamma = x
  186. return
  187. case IsInf(x, 0):
  188. lgamma = x
  189. return
  190. case x == 0:
  191. lgamma = Inf(1)
  192. return
  193. }
  194. neg := false
  195. if x < 0 {
  196. x = -x
  197. neg = true
  198. }
  199. if x < Tiny { // if |x| < 2**-70, return -log(|x|)
  200. if neg {
  201. sign = -1
  202. }
  203. lgamma = -Log(x)
  204. return
  205. }
  206. var nadj float64
  207. if neg {
  208. if x >= Two52 { // |x| >= 2**52, must be -integer
  209. lgamma = Inf(1)
  210. return
  211. }
  212. t := sinPi(x)
  213. if t == 0 {
  214. lgamma = Inf(1) // -integer
  215. return
  216. }
  217. nadj = Log(Pi / Abs(t*x))
  218. if t < 0 {
  219. sign = -1
  220. }
  221. }
  222. switch {
  223. case x == 1 || x == 2: // purge off 1 and 2
  224. lgamma = 0
  225. return
  226. case x < 2: // use lgamma(x) = lgamma(x+1) - log(x)
  227. var y float64
  228. var i int
  229. if x <= 0.9 {
  230. lgamma = -Log(x)
  231. switch {
  232. case x >= (Ymin - 1 + 0.27): // 0.7316 <= x <= 0.9
  233. y = 1 - x
  234. i = 0
  235. case x >= (Ymin - 1 - 0.27): // 0.2316 <= x < 0.7316
  236. y = x - (Tc - 1)
  237. i = 1
  238. default: // 0 < x < 0.2316
  239. y = x
  240. i = 2
  241. }
  242. } else {
  243. lgamma = 0
  244. switch {
  245. case x >= (Ymin + 0.27): // 1.7316 <= x < 2
  246. y = 2 - x
  247. i = 0
  248. case x >= (Ymin - 0.27): // 1.2316 <= x < 1.7316
  249. y = x - Tc
  250. i = 1
  251. default: // 0.9 < x < 1.2316
  252. y = x - 1
  253. i = 2
  254. }
  255. }
  256. switch i {
  257. case 0:
  258. z := y * y
  259. p1 := _lgamA[0] + z*(_lgamA[2]+z*(_lgamA[4]+z*(_lgamA[6]+z*(_lgamA[8]+z*_lgamA[10]))))
  260. p2 := z * (_lgamA[1] + z*(+_lgamA[3]+z*(_lgamA[5]+z*(_lgamA[7]+z*(_lgamA[9]+z*_lgamA[11])))))
  261. p := y*p1 + p2
  262. lgamma += (p - 0.5*y)
  263. case 1:
  264. z := y * y
  265. w := z * y
  266. p1 := _lgamT[0] + w*(_lgamT[3]+w*(_lgamT[6]+w*(_lgamT[9]+w*_lgamT[12]))) // parallel comp
  267. p2 := _lgamT[1] + w*(_lgamT[4]+w*(_lgamT[7]+w*(_lgamT[10]+w*_lgamT[13])))
  268. p3 := _lgamT[2] + w*(_lgamT[5]+w*(_lgamT[8]+w*(_lgamT[11]+w*_lgamT[14])))
  269. p := z*p1 - (Tt - w*(p2+y*p3))
  270. lgamma += (Tf + p)
  271. case 2:
  272. p1 := y * (_lgamU[0] + y*(_lgamU[1]+y*(_lgamU[2]+y*(_lgamU[3]+y*(_lgamU[4]+y*_lgamU[5])))))
  273. p2 := 1 + y*(_lgamV[1]+y*(_lgamV[2]+y*(_lgamV[3]+y*(_lgamV[4]+y*_lgamV[5]))))
  274. lgamma += (-0.5*y + p1/p2)
  275. }
  276. case x < 8: // 2 <= x < 8
  277. i := int(x)
  278. y := x - float64(i)
  279. p := y * (_lgamS[0] + y*(_lgamS[1]+y*(_lgamS[2]+y*(_lgamS[3]+y*(_lgamS[4]+y*(_lgamS[5]+y*_lgamS[6]))))))
  280. q := 1 + y*(_lgamR[1]+y*(_lgamR[2]+y*(_lgamR[3]+y*(_lgamR[4]+y*(_lgamR[5]+y*_lgamR[6])))))
  281. lgamma = 0.5*y + p/q
  282. z := 1.0 // Lgamma(1+s) = Log(s) + Lgamma(s)
  283. switch i {
  284. case 7:
  285. z *= (y + 6)
  286. fallthrough
  287. case 6:
  288. z *= (y + 5)
  289. fallthrough
  290. case 5:
  291. z *= (y + 4)
  292. fallthrough
  293. case 4:
  294. z *= (y + 3)
  295. fallthrough
  296. case 3:
  297. z *= (y + 2)
  298. lgamma += Log(z)
  299. }
  300. case x < Two58: // 8 <= x < 2**58
  301. t := Log(x)
  302. z := 1 / x
  303. y := z * z
  304. w := _lgamW[0] + z*(_lgamW[1]+y*(_lgamW[2]+y*(_lgamW[3]+y*(_lgamW[4]+y*(_lgamW[5]+y*_lgamW[6])))))
  305. lgamma = (x-0.5)*(t-1) + w
  306. default: // 2**58 <= x <= Inf
  307. lgamma = x * (Log(x) - 1)
  308. }
  309. if neg {
  310. lgamma = nadj - lgamma
  311. }
  312. return
  313. }
  314. // sinPi(x) is a helper function for negative x
  315. func sinPi(x float64) float64 {
  316. const (
  317. Two52 = 1 << 52 // 0x4330000000000000 ~4.5036e+15
  318. Two53 = 1 << 53 // 0x4340000000000000 ~9.0072e+15
  319. )
  320. if x < 0.25 {
  321. return -Sin(Pi * x)
  322. }
  323. // argument reduction
  324. z := Floor(x)
  325. var n int
  326. if z != x { // inexact
  327. x = Mod(x, 2)
  328. n = int(x * 4)
  329. } else {
  330. if x >= Two53 { // x must be even
  331. x = 0
  332. n = 0
  333. } else {
  334. if x < Two52 {
  335. z = x + Two52 // exact
  336. }
  337. n = int(1 & Float64bits(z))
  338. x = float64(n)
  339. n <<= 2
  340. }
  341. }
  342. switch n {
  343. case 0:
  344. x = Sin(Pi * x)
  345. case 1, 2:
  346. x = Cos(Pi * (0.5 - x))
  347. case 3, 4:
  348. x = Sin(Pi * (1 - x))
  349. case 5, 6:
  350. x = -Cos(Pi * (x - 1.5))
  351. default:
  352. x = Sin(Pi * (x - 2))
  353. }
  354. return -x
  355. }