e_lgammal_r.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * ====================================================
  3. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  4. *
  5. * Developed at SunPro, a Sun Microsystems, Inc. business.
  6. * Permission to use, copy, modify, and distribute this
  7. * software is freely granted, provided that this notice
  8. * is preserved.
  9. * ====================================================
  10. */
  11. /*
  12. * Copyright (c) 2008 Stephen L. Moshier <[email protected]>
  13. *
  14. * Permission to use, copy, modify, and distribute this software for any
  15. * purpose with or without fee is hereby granted, provided that the above
  16. * copyright notice and this permission notice appear in all copies.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  19. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  21. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  22. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  23. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  24. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  25. */
  26. /* lgammal_r(x, signgamp)
  27. * Reentrant version of the logarithm of the Gamma function
  28. * with user provide pointer for the sign of Gamma(x).
  29. *
  30. * Method:
  31. * 1. Argument Reduction for 0 < x <= 8
  32. * Since gamma(1+s)=s*gamma(s), for x in [0,8], we may
  33. * reduce x to a number in [1.5,2.5] by
  34. * lgamma(1+s) = log(s) + lgamma(s)
  35. * for example,
  36. * lgamma(7.3) = log(6.3) + lgamma(6.3)
  37. * = log(6.3*5.3) + lgamma(5.3)
  38. * = log(6.3*5.3*4.3*3.3*2.3) + lgamma(2.3)
  39. * 2. Polynomial approximation of lgamma around its
  40. * minimun ymin=1.461632144968362245 to maintain monotonicity.
  41. * On [ymin-0.23, ymin+0.27] (i.e., [1.23164,1.73163]), use
  42. * Let z = x-ymin;
  43. * lgamma(x) = -1.214862905358496078218 + z^2*poly(z)
  44. * 2. Rational approximation in the primary interval [2,3]
  45. * We use the following approximation:
  46. * s = x-2.0;
  47. * lgamma(x) = 0.5*s + s*P(s)/Q(s)
  48. * Our algorithms are based on the following observation
  49. *
  50. * zeta(2)-1 2 zeta(3)-1 3
  51. * lgamma(2+s) = s*(1-Euler) + --------- * s - --------- * s + ...
  52. * 2 3
  53. *
  54. * where Euler = 0.5771... is the Euler constant, which is very
  55. * close to 0.5.
  56. *
  57. * 3. For x>=8, we have
  58. * lgamma(x)~(x-0.5)log(x)-x+0.5*log(2pi)+1/(12x)-1/(360x**3)+....
  59. * (better formula:
  60. * lgamma(x)~(x-0.5)*(log(x)-1)-.5*(log(2pi)-1) + ...)
  61. * Let z = 1/x, then we approximation
  62. * f(z) = lgamma(x) - (x-0.5)(log(x)-1)
  63. * by
  64. * 3 5 11
  65. * w = w0 + w1*z + w2*z + w3*z + ... + w6*z
  66. *
  67. * 4. For negative x, since (G is gamma function)
  68. * -x*G(-x)*G(x) = pi/sin(pi*x),
  69. * we have
  70. * G(x) = pi/(sin(pi*x)*(-x)*G(-x))
  71. * since G(-x) is positive, sign(G(x)) = sign(sin(pi*x)) for x<0
  72. * Hence, for x<0, signgam = sign(sin(pi*x)) and
  73. * lgamma(x) = log(|Gamma(x)|)
  74. * = log(pi/(|x*sin(pi*x)|)) - lgamma(-x);
  75. * Note: one should avoid compute pi*(-x) directly in the
  76. * computation of sin(pi*(-x)).
  77. *
  78. * 5. Special Cases
  79. * lgamma(2+s) ~ s*(1-Euler) for tiny s
  80. * lgamma(1)=lgamma(2)=0
  81. * lgamma(x) ~ -log(x) for tiny x
  82. * lgamma(0) = lgamma(inf) = inf
  83. * lgamma(-integer) = +-inf
  84. *
  85. */
  86. #include <openlibm_math.h>
  87. #include "math_private.h"
  88. static const long double
  89. half = 0.5L,
  90. one = 1.0L,
  91. pi = 3.14159265358979323846264L,
  92. two63 = 9.223372036854775808e18L,
  93. /* lgam(1+x) = 0.5 x + x a(x)/b(x)
  94. -0.268402099609375 <= x <= 0
  95. peak relative error 6.6e-22 */
  96. a0 = -6.343246574721079391729402781192128239938E2L,
  97. a1 = 1.856560238672465796768677717168371401378E3L,
  98. a2 = 2.404733102163746263689288466865843408429E3L,
  99. a3 = 8.804188795790383497379532868917517596322E2L,
  100. a4 = 1.135361354097447729740103745999661157426E2L,
  101. a5 = 3.766956539107615557608581581190400021285E0L,
  102. b0 = 8.214973713960928795704317259806842490498E3L,
  103. b1 = 1.026343508841367384879065363925870888012E4L,
  104. b2 = 4.553337477045763320522762343132210919277E3L,
  105. b3 = 8.506975785032585797446253359230031874803E2L,
  106. b4 = 6.042447899703295436820744186992189445813E1L,
  107. /* b5 = 1.000000000000000000000000000000000000000E0 */
  108. tc = 1.4616321449683623412626595423257213284682E0L,
  109. tf = -1.2148629053584961146050602565082954242826E-1,/* double precision */
  110. /* tt = (tail of tf), i.e. tf + tt has extended precision. */
  111. tt = 3.3649914684731379602768989080467587736363E-18L,
  112. /* lgam ( 1.4616321449683623412626595423257213284682E0 ) =
  113. -1.2148629053584960809551455717769158215135617312999903886372437313313530E-1 */
  114. /* lgam (x + tc) = tf + tt + x g(x)/h(x)
  115. - 0.230003726999612341262659542325721328468 <= x
  116. <= 0.2699962730003876587373404576742786715318
  117. peak relative error 2.1e-21 */
  118. g0 = 3.645529916721223331888305293534095553827E-18L,
  119. g1 = 5.126654642791082497002594216163574795690E3L,
  120. g2 = 8.828603575854624811911631336122070070327E3L,
  121. g3 = 5.464186426932117031234820886525701595203E3L,
  122. g4 = 1.455427403530884193180776558102868592293E3L,
  123. g5 = 1.541735456969245924860307497029155838446E2L,
  124. g6 = 4.335498275274822298341872707453445815118E0L,
  125. h0 = 1.059584930106085509696730443974495979641E4L,
  126. h1 = 2.147921653490043010629481226937850618860E4L,
  127. h2 = 1.643014770044524804175197151958100656728E4L,
  128. h3 = 5.869021995186925517228323497501767586078E3L,
  129. h4 = 9.764244777714344488787381271643502742293E2L,
  130. h5 = 6.442485441570592541741092969581997002349E1L,
  131. /* h6 = 1.000000000000000000000000000000000000000E0 */
  132. /* lgam (x+1) = -0.5 x + x u(x)/v(x)
  133. -0.100006103515625 <= x <= 0.231639862060546875
  134. peak relative error 1.3e-21 */
  135. u0 = -8.886217500092090678492242071879342025627E1L,
  136. u1 = 6.840109978129177639438792958320783599310E2L,
  137. u2 = 2.042626104514127267855588786511809932433E3L,
  138. u3 = 1.911723903442667422201651063009856064275E3L,
  139. u4 = 7.447065275665887457628865263491667767695E2L,
  140. u5 = 1.132256494121790736268471016493103952637E2L,
  141. u6 = 4.484398885516614191003094714505960972894E0L,
  142. v0 = 1.150830924194461522996462401210374632929E3L,
  143. v1 = 3.399692260848747447377972081399737098610E3L,
  144. v2 = 3.786631705644460255229513563657226008015E3L,
  145. v3 = 1.966450123004478374557778781564114347876E3L,
  146. v4 = 4.741359068914069299837355438370682773122E2L,
  147. v5 = 4.508989649747184050907206782117647852364E1L,
  148. /* v6 = 1.000000000000000000000000000000000000000E0 */
  149. /* lgam (x+2) = .5 x + x s(x)/r(x)
  150. 0 <= x <= 1
  151. peak relative error 7.2e-22 */
  152. s0 = 1.454726263410661942989109455292824853344E6L,
  153. s1 = -3.901428390086348447890408306153378922752E6L,
  154. s2 = -6.573568698209374121847873064292963089438E6L,
  155. s3 = -3.319055881485044417245964508099095984643E6L,
  156. s4 = -7.094891568758439227560184618114707107977E5L,
  157. s5 = -6.263426646464505837422314539808112478303E4L,
  158. s6 = -1.684926520999477529949915657519454051529E3L,
  159. r0 = -1.883978160734303518163008696712983134698E7L,
  160. r1 = -2.815206082812062064902202753264922306830E7L,
  161. r2 = -1.600245495251915899081846093343626358398E7L,
  162. r3 = -4.310526301881305003489257052083370058799E6L,
  163. r4 = -5.563807682263923279438235987186184968542E5L,
  164. r5 = -3.027734654434169996032905158145259713083E4L,
  165. r6 = -4.501995652861105629217250715790764371267E2L,
  166. /* r6 = 1.000000000000000000000000000000000000000E0 */
  167. /* lgam(x) = ( x - 0.5 ) * log(x) - x + LS2PI + 1/x w(1/x^2)
  168. x >= 8
  169. Peak relative error 1.51e-21
  170. w0 = LS2PI - 0.5 */
  171. w0 = 4.189385332046727417803e-1L,
  172. w1 = 8.333333333333331447505E-2L,
  173. w2 = -2.777777777750349603440E-3L,
  174. w3 = 7.936507795855070755671E-4L,
  175. w4 = -5.952345851765688514613E-4L,
  176. w5 = 8.412723297322498080632E-4L,
  177. w6 = -1.880801938119376907179E-3L,
  178. w7 = 4.885026142432270781165E-3L;
  179. static const long double zero = 0.0L;
  180. static long double
  181. sin_pi(long double x)
  182. {
  183. long double y, z;
  184. int n, ix;
  185. u_int32_t se, i0, i1;
  186. GET_LDOUBLE_WORDS (se, i0, i1, x);
  187. ix = se & 0x7fff;
  188. ix = (ix << 16) | (i0 >> 16);
  189. if (ix < 0x3ffd8000) /* 0.25 */
  190. return sinl (pi * x);
  191. y = -x; /* x is assume negative */
  192. /*
  193. * argument reduction, make sure inexact flag not raised if input
  194. * is an integer
  195. */
  196. z = floorl (y);
  197. if (z != y)
  198. { /* inexact anyway */
  199. y *= 0.5;
  200. y = 2.0*(y - floorl(y)); /* y = |x| mod 2.0 */
  201. n = (int) (y*4.0);
  202. }
  203. else
  204. {
  205. if (ix >= 0x403f8000) /* 2^64 */
  206. {
  207. y = zero; n = 0; /* y must be even */
  208. }
  209. else
  210. {
  211. if (ix < 0x403e8000) /* 2^63 */
  212. z = y + two63; /* exact */
  213. GET_LDOUBLE_WORDS (se, i0, i1, z);
  214. n = i1 & 1;
  215. y = n;
  216. n <<= 2;
  217. }
  218. }
  219. switch (n)
  220. {
  221. case 0:
  222. y = sinl (pi * y);
  223. break;
  224. case 1:
  225. case 2:
  226. y = cosl (pi * (half - y));
  227. break;
  228. case 3:
  229. case 4:
  230. y = sinl (pi * (one - y));
  231. break;
  232. case 5:
  233. case 6:
  234. y = -cosl (pi * (y - 1.5));
  235. break;
  236. default:
  237. y = sinl (pi * (y - 2.0));
  238. break;
  239. }
  240. return -y;
  241. }
  242. long double
  243. lgammal_r(long double x, int *signgamp)
  244. {
  245. long double t, y, z, nadj, p, p1, p2, q, r, w;
  246. int i, ix;
  247. u_int32_t se, i0, i1;
  248. *signgamp = 1;
  249. GET_LDOUBLE_WORDS (se, i0, i1, x);
  250. ix = se & 0x7fff;
  251. if ((ix | i0 | i1) == 0)
  252. {
  253. if (se & 0x8000)
  254. *signgamp = -1;
  255. return one / fabsl (x);
  256. }
  257. ix = (ix << 16) | (i0 >> 16);
  258. /* purge off +-inf, NaN, +-0, and negative arguments */
  259. if (ix >= 0x7fff0000)
  260. return x * x;
  261. if (ix < 0x3fc08000) /* 2^-63 */
  262. { /* |x|<2**-63, return -log(|x|) */
  263. if (se & 0x8000)
  264. {
  265. *signgamp = -1;
  266. return -logl (-x);
  267. }
  268. else
  269. return -logl (x);
  270. }
  271. if (se & 0x8000)
  272. {
  273. t = sin_pi (x);
  274. if (t == zero)
  275. return one / fabsl (t); /* -integer */
  276. nadj = logl (pi / fabsl (t * x));
  277. if (t < zero)
  278. *signgamp = -1;
  279. x = -x;
  280. }
  281. /* purge off 1 and 2 */
  282. if ((((ix - 0x3fff8000) | i0 | i1) == 0)
  283. || (((ix - 0x40008000) | i0 | i1) == 0))
  284. r = 0;
  285. else if (ix < 0x40008000) /* 2.0 */
  286. {
  287. /* x < 2.0 */
  288. if (ix <= 0x3ffee666) /* 8.99993896484375e-1 */
  289. {
  290. /* lgamma(x) = lgamma(x+1) - log(x) */
  291. r = -logl (x);
  292. if (ix >= 0x3ffebb4a) /* 7.31597900390625e-1 */
  293. {
  294. y = x - one;
  295. i = 0;
  296. }
  297. else if (ix >= 0x3ffced33)/* 2.31639862060546875e-1 */
  298. {
  299. y = x - (tc - one);
  300. i = 1;
  301. }
  302. else
  303. {
  304. /* x < 0.23 */
  305. y = x;
  306. i = 2;
  307. }
  308. }
  309. else
  310. {
  311. r = zero;
  312. if (ix >= 0x3fffdda6) /* 1.73162841796875 */
  313. {
  314. /* [1.7316,2] */
  315. y = x - 2.0;
  316. i = 0;
  317. }
  318. else if (ix >= 0x3fff9da6)/* 1.23162841796875 */
  319. {
  320. /* [1.23,1.73] */
  321. y = x - tc;
  322. i = 1;
  323. }
  324. else
  325. {
  326. /* [0.9, 1.23] */
  327. y = x - one;
  328. i = 2;
  329. }
  330. }
  331. switch (i)
  332. {
  333. case 0:
  334. p1 = a0 + y * (a1 + y * (a2 + y * (a3 + y * (a4 + y * a5))));
  335. p2 = b0 + y * (b1 + y * (b2 + y * (b3 + y * (b4 + y))));
  336. r += half * y + y * p1/p2;
  337. break;
  338. case 1:
  339. p1 = g0 + y * (g1 + y * (g2 + y * (g3 + y * (g4 + y * (g5 + y * g6)))));
  340. p2 = h0 + y * (h1 + y * (h2 + y * (h3 + y * (h4 + y * (h5 + y)))));
  341. p = tt + y * p1/p2;
  342. r += (tf + p);
  343. break;
  344. case 2:
  345. p1 = y * (u0 + y * (u1 + y * (u2 + y * (u3 + y * (u4 + y * (u5 + y * u6))))));
  346. p2 = v0 + y * (v1 + y * (v2 + y * (v3 + y * (v4 + y * (v5 + y)))));
  347. r += (-half * y + p1 / p2);
  348. }
  349. }
  350. else if (ix < 0x40028000) /* 8.0 */
  351. {
  352. /* x < 8.0 */
  353. i = (int) x;
  354. t = zero;
  355. y = x - (double) i;
  356. p = y *
  357. (s0 + y * (s1 + y * (s2 + y * (s3 + y * (s4 + y * (s5 + y * s6))))));
  358. q = r0 + y * (r1 + y * (r2 + y * (r3 + y * (r4 + y * (r5 + y * (r6 + y))))));
  359. r = half * y + p / q;
  360. z = one; /* lgamma(1+s) = log(s) + lgamma(s) */
  361. switch (i)
  362. {
  363. case 7:
  364. z *= (y + 6.0); /* FALLTHRU */
  365. case 6:
  366. z *= (y + 5.0); /* FALLTHRU */
  367. case 5:
  368. z *= (y + 4.0); /* FALLTHRU */
  369. case 4:
  370. z *= (y + 3.0); /* FALLTHRU */
  371. case 3:
  372. z *= (y + 2.0); /* FALLTHRU */
  373. r += logl (z);
  374. break;
  375. }
  376. }
  377. else if (ix < 0x40418000) /* 2^66 */
  378. {
  379. /* 8.0 <= x < 2**66 */
  380. t = logl (x);
  381. z = one / x;
  382. y = z * z;
  383. w = w0 + z * (w1
  384. + y * (w2 + y * (w3 + y * (w4 + y * (w5 + y * (w6 + y * w7))))));
  385. r = (x - half) * (t - one) + w;
  386. }
  387. else
  388. /* 2**66 <= x <= inf */
  389. r = x * (logl (x) - one);
  390. if (se & 0x8000)
  391. r = nadj - r;
  392. return r;
  393. }