TPCCLIB
Loading...
Searching...
No Matches
rgamma.c File Reference

Regularized gamma function. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <time.h>
#include <string.h>
#include "tpcextensions.h"
#include "tpcfunc.h"

Go to the source code of this file.

Functions

double igam (double a, double x)
double igamc (double a, double x)

Detailed Description

Regularized gamma function.

Note
C99 is required because of Gamma function, lgamma().
These functions could be used to fit the plasma parent fractions (Naganawa et al 2014).

Definition in file rgamma.c.

Function Documentation

◆ igam()

double igam ( double a,
double x )

Cumulative gamma distribution, or Regularized gamma function, more specifically, lower incomplete gamma function divided by gamma function.

Standard gamma distribution is assumed (Beta=1). f(a,x) = (1/Gamma(a)) * Integral(0,x)(e^-t * t^(a-1))dt

Returns
Returns the value of regularized gamma function, or NaN in case of an error.
See also
igamc, inverfc, factorial
Parameters
aShape parameter alpha; must be > 0.
xIntegral from 0 to x; must be >= 0.

Definition at line 29 of file rgamma.c.

34 {
35 /* Check parameters */
36 if(x==0.0) return(0.0);
37 if((x<0.0) || (a<=0.0)) return(nan(""));
38
39 if((x>1.0) && (x>a)) return(1.0 - igamc(a, x));
40
41 /* Left tail of incomplete Gamma function:
42 x^a * e^-x * Sum(k=0,Inf)(x^k / Gamma(a+k+1)) */
43
44 double ans, ax, c, r;
45
46 /* Compute x**a * exp(-x) / Gamma(a) */
47 ax=a*log(x) - x - lgamma(a);
48 if(ax<-DBL_MAX_10_EXP) return(0.0); // underflow
49 ax=exp(ax);
50
51 /* power series */
52 r=a; c=1.0; ans=1.0;
53 do {
54 r+=1.0;
55 c*=x/r;
56 ans+=c;
57 } while(c/ans > DBL_EPSILON);
58 return(ans*ax/a);
59}
double igamc(double a, double x)
Definition rgamma.c:70

Referenced by igamc(), and mfEvalY().

◆ igamc()

double igamc ( double a,
double x )

Regularized gamma function, more specifically, upper incomplete gamma function divided by gamma function.

f(a,x) = (1/Gamma(a)) * Integral(x,Inf)(e^-t * t^(a-1))dt Standard gamma distribution is assumed (Beta=1).

Returns
Returns the value of regularized gamma function, or NaN in case of an error.
See also
igam, inverfc, factorial
Parameters
aShape parameter alpha; must be > 0.
xIntegral from x to infinity; must be >= 0

Definition at line 70 of file rgamma.c.

75 {
76 /* Check parameters */
77 if((x<0.0) || (a<=0.0)) return(nan(""));
78
79 if((x<1.0) || (x<a)) return(1.0-igam(a, x));
80
81 double ans, ax, c, yc, r, t, y, z;
82 double pk, pkm1, pkm2, qk, qkm1, qkm2;
83 double big=4.503599627370496E+015;
84 double biginv=2.22044604925031308085E-016;
85
86 ax = a*log(x) - x - lgamma(a);
87 if(ax < -DBL_MAX_10_EXP) return(0.0); // underflow
88 ax=exp(ax);
89
90 /* continued fraction */
91 y=1.0-a; z=x+y+1.0; c=0.0;
92 pkm2=1.0; qkm2=x; pkm1=x+1.0; qkm1=z*x;
93 ans=pkm1/qkm1;
94 do {
95 c+=1.0; y+=1.0; z+=2.0;
96 yc=y*c; pk=pkm1*z - pkm2*yc; qk=qkm1*z - qkm2*yc;
97 if(qk!=0.0) {r=pk/qk; t=fabs((ans-r)/r); ans=r;}
98 else t=1.0;
99 pkm2=pkm1; pkm1=pk; qkm2=qkm1; qkm1=qk;
100 if(fabs(pk)>big) {pkm2*=biginv; pkm1*=biginv; qkm2*=biginv; qkm1*=biginv;}
101 } while(t>DBL_EPSILON);
102 return(ans*ax);
103}
double igam(double a, double x)
Definition rgamma.c:29

Referenced by igam().