-
Notifications
You must be signed in to change notification settings - Fork 1
/
gammq.c
170 lines (150 loc) · 4.96 KB
/
gammq.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <math.h>
#include "jLib.h"
/* ================================================================================
Returns the incomplete gamma function P(a,x) pp 172
================================================================================ */
double gammp( double a, double x ){
double gamser, gln;
void gser(), gcf(), nerror();
if( x<0.0 || a<=0.0 ) nerror( "gammp: Invalid arguments" );
if( x < (a+1.0) ){
gser( &gamser, a, x, &gln );
return gamser;
}
else {
gcf( &gamser, a, x, &gln );
return 1.0 - gamser;
}
}
/* =====================================================================================
Returns the incomplete gamma function Q(a,x) = 1 - P(a,x) (173-ff)
===================================================================================== */
double gammq( double a, double x ){
double gamser, gammcf, gln;
void gcf(), gser(), nerror();
if( x < 0.0 || a <= 0.0 ) nerror( "gammq : invalid argument" );
if( x < (a+1.0) ){
gser( &gamser, a, x, &gln );
return( 1.0 - gamser );
}
else {
gcf( &gammcf, a, x, &gln );
return gammcf;
}
}
/* =====================================================================================
returns the incomplete gamma function P(a,x) evaluated by its series representation
as gamser. Also returs ln(gamma(a)) in gln
===================================================================================== */
#define ITMAX 100
#define EPS 3.0e-7
void gser( double *gamser, double a, double x, double *gln ){
int n;
double sum, del, ap;
double gammln();
void nerror();
*gln = gammln( a );
if( x <= 0.0 ) {
if( x < 0.0 ) nerror( "gser : Error, x less than zero" );
*gamser = 0.0;
return;
}
else {
ap = a;
del = sum = 1.0/a;
for( n=1; n<=ITMAX; ++n ){
ap += 1.0;
del *= x/ap;
sum += del;
if( fabs(del) < fabs(sum)*EPS ) {
*gamser = sum * exp( -x + a*log(x)-(*gln));
return;
}
}
nerror( "gser : didn't converge; a too large, ITMAX too small" );
return;
}
}
/* =====================================================================================
return the incomplete gamma function Q(a,x) evaluated by its continued fraction
representation as gammcf. Also returs ln(gamma(a)) in gln.
===================================================================================== */
void gcf( double *gammcf, double a, double x, double *gln ){
int n;
double gold=0.0, g, fac=1.0, b1=1.0;
double b0=0.0, anf, ana, an, a1, a0=1.0;
double gammln();
void nerror();
*gln = gammln( a );
a1 = x;
for( n=1; n<=ITMAX; ++n ){
an = (double) n;
ana = an - a;
a0 = (a1 + a0*ana)*fac;
b0 = (b1 + b0*ana)*fac;
anf = an*fac;
a1 = x*a0 + anf*a1;
b1 = x*b0 + anf*b1;
if( a1 ) {
fac = 1.0/a1;
g = b1*fac;
if( fabs((g-gold)/g) < EPS ) {
*gammcf = exp( -x + a*log(x) - (*gln)) * g;
return;
}
gold = g;
}
}
nerror( "gcf: didn't converge; a too large, ITMAX too small" );
}
/* =====================================================================================
Returns the value of ln(abs(gamma(xx))) for xx>0.
Full accuracy is obtained for xx>1.
For 0<xx<1 the reflection formula can be used first.
===================================================================================== */
double gammln( double xx ){
double x, tmp, ser;
static double cof[6] = { 76.18009173,
-86.50532033,
24.01409822,
-1.231739516,
0.120858003e-2,
-0.536382e-5};
int j;
x = xx - 1.0;
tmp = x + 5.5;
tmp -= (x+0.5) * log(tmp);
ser = 1.0;
for( j=0; j<=5; ++j ) {
x += 1.0;
ser += cof[j]/x;
}
return -tmp + log( 2.50662827465 * ser );
}
/* ================================================================================
Return the error function erf(x) pp 175
================================================================================ */
double erf( double x ){
double gammp();
return x<0.0 ? -gammp(0.5, x*x) : gammp(0.5, x*x);
}
/* ================================================================================
Return the complementary error function erf(x) pp 175
================================================================================ */
double erfc( double x ){
double gammp(), gammq();
return x<0.0 ? 1.0+gammp(0.5, x*x) : gammq(0.5, x*x);
}
/* ================================================================================
Return the complementary error function with fractional error everywhere less
than 1.2 x 10^{-7}
================================================================================ */
double erfcc( double x ){
double t, z, ans;
z = fabs( x );
t = 1.0 / (1.0+0.5*z);
ans = t * exp( -z*z -1.26551223 + t*(1.00002368 +t*(0.37409196 +t*(0.09678418
+ t*(-0.18628806 +t*(0.27886807 +t*(-1.13520398 +t*(1.48851587
+ t*(-0.82215223 +t*(0.17087277 ))))))))));
return x>=0.0 ? ans : 2.0-ans;
}