SDDSlib
Loading...
Searching...
No Matches
gammai.c File Reference

Routines for computing the incomplete gamma functions. More...

#include "mdb.h"

Go to the source code of this file.

Macros

#define GAMMAI_ACCURACY   1e-12
 
#define MAX_SERIES   1000
 

Functions

double gammaIncSeries (double a, double x)
 
double gammaIncCFrac (double a, double x)
 
double gammaP (double a, double x)
 Compute the regularized lower incomplete gamma function P(a,x).
 
double gammaQ (double a, double x)
 Compute the regularized upper incomplete gamma function Q(a,x).
 

Detailed Description

Routines for computing the incomplete gamma functions.

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, C. Saunders, R. Soliday

Definition in file gammai.c.

Macro Definition Documentation

◆ GAMMAI_ACCURACY

#define GAMMAI_ACCURACY   1e-12

Definition at line 27 of file gammai.c.

◆ MAX_SERIES

#define MAX_SERIES   1000

Definition at line 78 of file gammai.c.

Function Documentation

◆ gammaIncCFrac()

double gammaIncCFrac ( double a,
double x )

Definition at line 99 of file gammai.c.

99 {
100 double A0, B0, A1, B1, A_1, B_1;
101 double an, bn, f1, f2, accuracy = 0, factor = 0;
102 long m;
103
104#if defined(vxWorks)
105 fprintf(stderr, "lgamma function not implemented in vxWorks");
106 exit(1);
107#else
108 accuracy = GAMMAI_ACCURACY / (factor = exp(-x - lgamma(a) + a * log(x)));
109#endif
110 A0 = 0;
111 B0 = 1;
112 A_1 = 1;
113 B_1 = 0;
114 an = 1;
115 bn = x + 1 - a;
116 A1 = bn * A0 + an * A_1;
117 B1 = bn * B0 + an * B_1;
118 f2 = A1 / B1;
119 m = 1;
120 do {
121 A_1 = A0;
122 B_1 = B0;
123 A0 = A1;
124 B0 = B1;
125 f1 = f2;
126 an = -m * (m - a);
127 bn += 2;
128 A1 = bn * A0 + an * A_1;
129 B1 = bn * B0 + an * B_1;
130 f2 = A1 / B1;
131 if (B1) {
132 /* rescale the recurrence to avoid over/underflow */
133 A0 /= B1;
134 B0 /= B1;
135 A1 /= B1;
136 B1 = 1;
137 }
138 m++;
139 } while (m < MAX_SERIES && fabs(f1 - f2) > accuracy);
140 return factor * f2;
141}

◆ gammaIncSeries()

double gammaIncSeries ( double a,
double x )

Definition at line 80 of file gammai.c.

80 {
81 double term = 0, sum;
82 long n;
83#if defined(vxWorks)
84 fprintf(stderr, "lgamma function not implemented in vxWorks");
85 exit(1);
86#else
87 term = exp(-x) * pow(x, a) / exp(lgamma(a + 1));
88#endif
89 sum = 0;
90 n = 0;
91 do {
92 sum += term;
93 n++;
94 term *= x / (a + n);
95 } while (term > GAMMAI_ACCURACY && n < MAX_SERIES);
96 return (sum + term);
97}

◆ gammaP()

double gammaP ( double a,
double x )

Compute the regularized lower incomplete gamma function P(a,x).

For given parameters a > 0 and x ≥ 0, this function returns the value of P(a,x) = γ(a,x)/Γ(a), where γ(a,x) is the lower incomplete gamma function. Uses series expansion for x < a+1, and a continued fraction expansion otherwise.

Parameters
aShape parameter (a > 0).
xThe upper limit of the integral (x ≥ 0).
Returns
The value of P(a,x), or -1 if inputs are invalid.

Definition at line 40 of file gammai.c.

40 {
41 if (a <= 0 || x < 0)
42 return -1;
43 if (x == 0)
44 return 0;
45 if (x < a + 1) {
46 /* use series */
47 return gammaIncSeries(a, x);
48 } else {
49 /* use continued fraction */
50 return 1 - gammaIncCFrac(a, x);
51 }
52}

◆ gammaQ()

double gammaQ ( double a,
double x )

Compute the regularized upper incomplete gamma function Q(a,x).

For given parameters a > 0 and x ≥ 0, this function returns Q(a,x) = 1 - P(a,x). It uses a series expansion when x < a+1 and a continued fraction expansion otherwise.

Parameters
aShape parameter (a > 0).
xThe upper limit of the integral (x ≥ 0).
Returns
The value of Q(a,x), or -1 if inputs are invalid.

Definition at line 64 of file gammai.c.

64 {
65 if (a <= 0 || x < 0)
66 return -1;
67 if (x == 0)
68 return 0;
69 if (x < a + 1) {
70 /* use series */
71 return 1 - gammaIncSeries(a, x);
72 } else {
73 /* use continued fraction */
74 return gammaIncCFrac(a, x);
75 }
76}