1*2fe8fb19SBen Gras /* $NetBSD: n_lgamma.c,v 1.6 2006/11/24 21:15:54 wiz Exp $ */
2*2fe8fb19SBen Gras /*-
3*2fe8fb19SBen Gras * Copyright (c) 1992, 1993
4*2fe8fb19SBen Gras * The Regents of the University of California. All rights reserved.
5*2fe8fb19SBen Gras *
6*2fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
7*2fe8fb19SBen Gras * modification, are permitted provided that the following conditions
8*2fe8fb19SBen Gras * are met:
9*2fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
10*2fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
11*2fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
12*2fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
13*2fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
14*2fe8fb19SBen Gras * 3. Neither the name of the University nor the names of its contributors
15*2fe8fb19SBen Gras * may be used to endorse or promote products derived from this software
16*2fe8fb19SBen Gras * without specific prior written permission.
17*2fe8fb19SBen Gras *
18*2fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19*2fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*2fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*2fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22*2fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*2fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*2fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*2fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*2fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*2fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*2fe8fb19SBen Gras * SUCH DAMAGE.
29*2fe8fb19SBen Gras */
30*2fe8fb19SBen Gras
31*2fe8fb19SBen Gras #ifndef lint
32*2fe8fb19SBen Gras #if 0
33*2fe8fb19SBen Gras static char sccsid[] = "@(#)lgamma.c 8.2 (Berkeley) 11/30/93";
34*2fe8fb19SBen Gras #endif
35*2fe8fb19SBen Gras #endif /* not lint */
36*2fe8fb19SBen Gras
37*2fe8fb19SBen Gras /*
38*2fe8fb19SBen Gras * Coded by Peter McIlroy, Nov 1992;
39*2fe8fb19SBen Gras *
40*2fe8fb19SBen Gras * The financial support of UUNET Communications Services is gratefully
41*2fe8fb19SBen Gras * acknowledged.
42*2fe8fb19SBen Gras */
43*2fe8fb19SBen Gras
44*2fe8fb19SBen Gras #include <math.h>
45*2fe8fb19SBen Gras #include <errno.h>
46*2fe8fb19SBen Gras
47*2fe8fb19SBen Gras #include "mathimpl.h"
48*2fe8fb19SBen Gras
49*2fe8fb19SBen Gras /* Log gamma function.
50*2fe8fb19SBen Gras * Error: x > 0 error < 1.3ulp.
51*2fe8fb19SBen Gras * x > 4, error < 1ulp.
52*2fe8fb19SBen Gras * x > 9, error < .6ulp.
53*2fe8fb19SBen Gras * x < 0, all bets are off. (When G(x) ~ 1, log(G(x)) ~ 0)
54*2fe8fb19SBen Gras * Method:
55*2fe8fb19SBen Gras * x > 6:
56*2fe8fb19SBen Gras * Use the asymptotic expansion (Stirling's Formula)
57*2fe8fb19SBen Gras * 0 < x < 6:
58*2fe8fb19SBen Gras * Use gamma(x+1) = x*gamma(x) for argument reduction.
59*2fe8fb19SBen Gras * Use rational approximation in
60*2fe8fb19SBen Gras * the range 1.2, 2.5
61*2fe8fb19SBen Gras * Two approximations are used, one centered at the
62*2fe8fb19SBen Gras * minimum to ensure monotonicity; one centered at 2
63*2fe8fb19SBen Gras * to maintain small relative error.
64*2fe8fb19SBen Gras * x < 0:
65*2fe8fb19SBen Gras * Use the reflection formula,
66*2fe8fb19SBen Gras * G(1-x)G(x) = PI/sin(PI*x)
67*2fe8fb19SBen Gras * Special values:
68*2fe8fb19SBen Gras * non-positive integer returns +Inf.
69*2fe8fb19SBen Gras * NaN returns NaN
70*2fe8fb19SBen Gras */
71*2fe8fb19SBen Gras #if defined(__vax__) || defined(tahoe)
72*2fe8fb19SBen Gras #define _IEEE 0
73*2fe8fb19SBen Gras /* double and float have same size exponent field */
74*2fe8fb19SBen Gras #define TRUNC(x) x = (double) (float) (x)
75*2fe8fb19SBen Gras #else
76*2fe8fb19SBen Gras static int endian;
77*2fe8fb19SBen Gras #define _IEEE 1
78*2fe8fb19SBen Gras #define TRUNC(x) *(((int *) &x) + endian) &= 0xf8000000
79*2fe8fb19SBen Gras #define infnan(x) 0.0
80*2fe8fb19SBen Gras #endif
81*2fe8fb19SBen Gras
82*2fe8fb19SBen Gras static double small_lgam(double);
83*2fe8fb19SBen Gras static double large_lgam(double);
84*2fe8fb19SBen Gras static double neg_lgam(double);
85*2fe8fb19SBen Gras static const double one = 1.0;
86*2fe8fb19SBen Gras int signgam;
87*2fe8fb19SBen Gras
88*2fe8fb19SBen Gras #define UNDERFL (1e-1020 * 1e-1020)
89*2fe8fb19SBen Gras
90*2fe8fb19SBen Gras #define LEFT (1.0 - (x0 + .25))
91*2fe8fb19SBen Gras #define RIGHT (x0 - .218)
92*2fe8fb19SBen Gras /*
93*2fe8fb19SBen Gras * Constants for approximation in [1.244,1.712]
94*2fe8fb19SBen Gras */
95*2fe8fb19SBen Gras #define x0 0.461632144968362356785
96*2fe8fb19SBen Gras #define x0_lo -.000000000000000015522348162858676890521
97*2fe8fb19SBen Gras #define a0_hi -0.12148629128932952880859
98*2fe8fb19SBen Gras #define a0_lo .0000000007534799204229502
99*2fe8fb19SBen Gras #define r0 -2.771227512955130520e-002
100*2fe8fb19SBen Gras #define r1 -2.980729795228150847e-001
101*2fe8fb19SBen Gras #define r2 -3.257411333183093394e-001
102*2fe8fb19SBen Gras #define r3 -1.126814387531706041e-001
103*2fe8fb19SBen Gras #define r4 -1.129130057170225562e-002
104*2fe8fb19SBen Gras #define r5 -2.259650588213369095e-005
105*2fe8fb19SBen Gras #define s0 1.714457160001714442e+000
106*2fe8fb19SBen Gras #define s1 2.786469504618194648e+000
107*2fe8fb19SBen Gras #define s2 1.564546365519179805e+000
108*2fe8fb19SBen Gras #define s3 3.485846389981109850e-001
109*2fe8fb19SBen Gras #define s4 2.467759345363656348e-002
110*2fe8fb19SBen Gras /*
111*2fe8fb19SBen Gras * Constants for approximation in [1.71, 2.5]
112*2fe8fb19SBen Gras */
113*2fe8fb19SBen Gras #define a1_hi 4.227843350984671344505727574870e-01
114*2fe8fb19SBen Gras #define a1_lo 4.670126436531227189e-18
115*2fe8fb19SBen Gras #define p0 3.224670334241133695662995251041e-01
116*2fe8fb19SBen Gras #define p1 3.569659696950364669021382724168e-01
117*2fe8fb19SBen Gras #define p2 1.342918716072560025853732668111e-01
118*2fe8fb19SBen Gras #define p3 1.950702176409779831089963408886e-02
119*2fe8fb19SBen Gras #define p4 8.546740251667538090796227834289e-04
120*2fe8fb19SBen Gras #define q0 1.000000000000000444089209850062e+00
121*2fe8fb19SBen Gras #define q1 1.315850076960161985084596381057e+00
122*2fe8fb19SBen Gras #define q2 6.274644311862156431658377186977e-01
123*2fe8fb19SBen Gras #define q3 1.304706631926259297049597307705e-01
124*2fe8fb19SBen Gras #define q4 1.102815279606722369265536798366e-02
125*2fe8fb19SBen Gras #define q5 2.512690594856678929537585620579e-04
126*2fe8fb19SBen Gras #define q6 -1.003597548112371003358107325598e-06
127*2fe8fb19SBen Gras /*
128*2fe8fb19SBen Gras * Stirling's Formula, adjusted for equal-ripple. x in [6,Inf].
129*2fe8fb19SBen Gras */
130*2fe8fb19SBen Gras #define lns2pi .418938533204672741780329736405
131*2fe8fb19SBen Gras #define pb0 8.33333333333333148296162562474e-02
132*2fe8fb19SBen Gras #define pb1 -2.77777777774548123579378966497e-03
133*2fe8fb19SBen Gras #define pb2 7.93650778754435631476282786423e-04
134*2fe8fb19SBen Gras #define pb3 -5.95235082566672847950717262222e-04
135*2fe8fb19SBen Gras #define pb4 8.41428560346653702135821806252e-04
136*2fe8fb19SBen Gras #define pb5 -1.89773526463879200348872089421e-03
137*2fe8fb19SBen Gras #define pb6 5.69394463439411649408050664078e-03
138*2fe8fb19SBen Gras #define pb7 -1.44705562421428915453880392761e-02
139*2fe8fb19SBen Gras
140*2fe8fb19SBen Gras __pure double
lgamma(double x)141*2fe8fb19SBen Gras lgamma(double x)
142*2fe8fb19SBen Gras {
143*2fe8fb19SBen Gras double r;
144*2fe8fb19SBen Gras
145*2fe8fb19SBen Gras signgam = 1;
146*2fe8fb19SBen Gras #if _IEEE
147*2fe8fb19SBen Gras endian = ((*(int *) &one)) ? 1 : 0;
148*2fe8fb19SBen Gras #endif
149*2fe8fb19SBen Gras
150*2fe8fb19SBen Gras if (!finite(x)) {
151*2fe8fb19SBen Gras if (_IEEE)
152*2fe8fb19SBen Gras return (x+x);
153*2fe8fb19SBen Gras else return (infnan(EDOM));
154*2fe8fb19SBen Gras }
155*2fe8fb19SBen Gras
156*2fe8fb19SBen Gras if (x > 6 + RIGHT) {
157*2fe8fb19SBen Gras r = large_lgam(x);
158*2fe8fb19SBen Gras return (r);
159*2fe8fb19SBen Gras } else if (x > 1e-16)
160*2fe8fb19SBen Gras return (small_lgam(x));
161*2fe8fb19SBen Gras else if (x > -1e-16) {
162*2fe8fb19SBen Gras if (x < 0)
163*2fe8fb19SBen Gras signgam = -1, x = -x;
164*2fe8fb19SBen Gras return (-log(x));
165*2fe8fb19SBen Gras } else
166*2fe8fb19SBen Gras return (neg_lgam(x));
167*2fe8fb19SBen Gras }
168*2fe8fb19SBen Gras
169*2fe8fb19SBen Gras static double
large_lgam(double x)170*2fe8fb19SBen Gras large_lgam(double x)
171*2fe8fb19SBen Gras {
172*2fe8fb19SBen Gras double z, p, x1;
173*2fe8fb19SBen Gras struct Double t, u, v;
174*2fe8fb19SBen Gras u = __log__D(x);
175*2fe8fb19SBen Gras u.a -= 1.0;
176*2fe8fb19SBen Gras if (x > 1e15) {
177*2fe8fb19SBen Gras v.a = x - 0.5;
178*2fe8fb19SBen Gras TRUNC(v.a);
179*2fe8fb19SBen Gras v.b = (x - v.a) - 0.5;
180*2fe8fb19SBen Gras t.a = u.a*v.a;
181*2fe8fb19SBen Gras t.b = x*u.b + v.b*u.a;
182*2fe8fb19SBen Gras if (_IEEE == 0 && !finite(t.a))
183*2fe8fb19SBen Gras return(infnan(ERANGE));
184*2fe8fb19SBen Gras return(t.a + t.b);
185*2fe8fb19SBen Gras }
186*2fe8fb19SBen Gras x1 = 1./x;
187*2fe8fb19SBen Gras z = x1*x1;
188*2fe8fb19SBen Gras p = pb0+z*(pb1+z*(pb2+z*(pb3+z*(pb4+z*(pb5+z*(pb6+z*pb7))))));
189*2fe8fb19SBen Gras /* error in approximation = 2.8e-19 */
190*2fe8fb19SBen Gras
191*2fe8fb19SBen Gras p = p*x1; /* error < 2.3e-18 absolute */
192*2fe8fb19SBen Gras /* 0 < p < 1/64 (at x = 5.5) */
193*2fe8fb19SBen Gras v.a = x = x - 0.5;
194*2fe8fb19SBen Gras TRUNC(v.a); /* truncate v.a to 26 bits. */
195*2fe8fb19SBen Gras v.b = x - v.a;
196*2fe8fb19SBen Gras t.a = v.a*u.a; /* t = (x-.5)*(log(x)-1) */
197*2fe8fb19SBen Gras t.b = v.b*u.a + x*u.b;
198*2fe8fb19SBen Gras t.b += p; t.b += lns2pi; /* return t + lns2pi + p */
199*2fe8fb19SBen Gras return (t.a + t.b);
200*2fe8fb19SBen Gras }
201*2fe8fb19SBen Gras
202*2fe8fb19SBen Gras static double
small_lgam(double x)203*2fe8fb19SBen Gras small_lgam(double x)
204*2fe8fb19SBen Gras {
205*2fe8fb19SBen Gras int x_int;
206*2fe8fb19SBen Gras double y, z, t, r = 0, p, q, hi, lo;
207*2fe8fb19SBen Gras struct Double rr;
208*2fe8fb19SBen Gras x_int = (x + .5);
209*2fe8fb19SBen Gras y = x - x_int;
210*2fe8fb19SBen Gras if (x_int <= 2 && y > RIGHT) {
211*2fe8fb19SBen Gras t = y - x0;
212*2fe8fb19SBen Gras y--; x_int++;
213*2fe8fb19SBen Gras goto CONTINUE;
214*2fe8fb19SBen Gras } else if (y < -LEFT) {
215*2fe8fb19SBen Gras t = y +(1.0-x0);
216*2fe8fb19SBen Gras CONTINUE:
217*2fe8fb19SBen Gras z = t - x0_lo;
218*2fe8fb19SBen Gras p = r0+z*(r1+z*(r2+z*(r3+z*(r4+z*r5))));
219*2fe8fb19SBen Gras q = s0+z*(s1+z*(s2+z*(s3+z*s4)));
220*2fe8fb19SBen Gras r = t*(z*(p/q) - x0_lo);
221*2fe8fb19SBen Gras t = .5*t*t;
222*2fe8fb19SBen Gras z = 1.0;
223*2fe8fb19SBen Gras switch (x_int) {
224*2fe8fb19SBen Gras case 6: z = (y + 5);
225*2fe8fb19SBen Gras case 5: z *= (y + 4);
226*2fe8fb19SBen Gras case 4: z *= (y + 3);
227*2fe8fb19SBen Gras case 3: z *= (y + 2);
228*2fe8fb19SBen Gras rr = __log__D(z);
229*2fe8fb19SBen Gras rr.b += a0_lo; rr.a += a0_hi;
230*2fe8fb19SBen Gras return(((r+rr.b)+t+rr.a));
231*2fe8fb19SBen Gras case 2: return(((r+a0_lo)+t)+a0_hi);
232*2fe8fb19SBen Gras case 0: r -= log1p(x);
233*2fe8fb19SBen Gras default: rr = __log__D(x);
234*2fe8fb19SBen Gras rr.a -= a0_hi; rr.b -= a0_lo;
235*2fe8fb19SBen Gras return(((r - rr.b) + t) - rr.a);
236*2fe8fb19SBen Gras }
237*2fe8fb19SBen Gras } else {
238*2fe8fb19SBen Gras p = p0+y*(p1+y*(p2+y*(p3+y*p4)));
239*2fe8fb19SBen Gras q = q0+y*(q1+y*(q2+y*(q3+y*(q4+y*(q5+y*q6)))));
240*2fe8fb19SBen Gras p = p*(y/q);
241*2fe8fb19SBen Gras t = (double)(float) y;
242*2fe8fb19SBen Gras z = y-t;
243*2fe8fb19SBen Gras hi = (double)(float) (p+a1_hi);
244*2fe8fb19SBen Gras lo = a1_hi - hi; lo += p; lo += a1_lo;
245*2fe8fb19SBen Gras r = lo*y + z*hi; /* q + r = y*(a0+p/q) */
246*2fe8fb19SBen Gras q = hi*t;
247*2fe8fb19SBen Gras z = 1.0;
248*2fe8fb19SBen Gras switch (x_int) {
249*2fe8fb19SBen Gras case 6: z = (y + 5);
250*2fe8fb19SBen Gras case 5: z *= (y + 4);
251*2fe8fb19SBen Gras case 4: z *= (y + 3);
252*2fe8fb19SBen Gras case 3: z *= (y + 2);
253*2fe8fb19SBen Gras rr = __log__D(z);
254*2fe8fb19SBen Gras r += rr.b; r += q;
255*2fe8fb19SBen Gras return(rr.a + r);
256*2fe8fb19SBen Gras case 2: return (q+ r);
257*2fe8fb19SBen Gras case 0: rr = __log__D(x);
258*2fe8fb19SBen Gras r -= rr.b; r -= log1p(x);
259*2fe8fb19SBen Gras r += q; r-= rr.a;
260*2fe8fb19SBen Gras return(r);
261*2fe8fb19SBen Gras default: rr = __log__D(x);
262*2fe8fb19SBen Gras r -= rr.b;
263*2fe8fb19SBen Gras q -= rr.a;
264*2fe8fb19SBen Gras return (r+q);
265*2fe8fb19SBen Gras }
266*2fe8fb19SBen Gras }
267*2fe8fb19SBen Gras }
268*2fe8fb19SBen Gras
269*2fe8fb19SBen Gras static double
neg_lgam(double x)270*2fe8fb19SBen Gras neg_lgam(double x)
271*2fe8fb19SBen Gras {
272*2fe8fb19SBen Gras int xi;
273*2fe8fb19SBen Gras double y, z, zero = 0.0;
274*2fe8fb19SBen Gras
275*2fe8fb19SBen Gras /* avoid destructive cancellation as much as possible */
276*2fe8fb19SBen Gras if (x > -170) {
277*2fe8fb19SBen Gras xi = x;
278*2fe8fb19SBen Gras if (xi == x) {
279*2fe8fb19SBen Gras if (_IEEE)
280*2fe8fb19SBen Gras return(one/zero);
281*2fe8fb19SBen Gras else
282*2fe8fb19SBen Gras return(infnan(ERANGE));
283*2fe8fb19SBen Gras }
284*2fe8fb19SBen Gras y = gamma(x);
285*2fe8fb19SBen Gras if (y < 0)
286*2fe8fb19SBen Gras y = -y, signgam = -1;
287*2fe8fb19SBen Gras return (log(y));
288*2fe8fb19SBen Gras }
289*2fe8fb19SBen Gras z = floor(x + .5);
290*2fe8fb19SBen Gras if (z == x) { /* convention: G(-(integer)) -> +Inf */
291*2fe8fb19SBen Gras if (_IEEE)
292*2fe8fb19SBen Gras return (one/zero);
293*2fe8fb19SBen Gras else
294*2fe8fb19SBen Gras return (infnan(ERANGE));
295*2fe8fb19SBen Gras }
296*2fe8fb19SBen Gras y = .5*ceil(x);
297*2fe8fb19SBen Gras if (y == ceil(y))
298*2fe8fb19SBen Gras signgam = -1;
299*2fe8fb19SBen Gras x = -x;
300*2fe8fb19SBen Gras z = fabs(x + z); /* 0 < z <= .5 */
301*2fe8fb19SBen Gras if (z < .25)
302*2fe8fb19SBen Gras z = sin(M_PI*z);
303*2fe8fb19SBen Gras else
304*2fe8fb19SBen Gras z = cos(M_PI*(0.5-z));
305*2fe8fb19SBen Gras z = log(M_PI/(z*x));
306*2fe8fb19SBen Gras y = large_lgam(x);
307*2fe8fb19SBen Gras return (z - y);
308*2fe8fb19SBen Gras }
309