CMS 3D CMS Logo

GammaSeries.cc
Go to the documentation of this file.
2 #include <iostream>
3 #include <cmath>
4 
5 #define ITMAX 100 // maximum allowed number of iterations
6 #define EPS 3.0e-7 // relative accuracy
7 
8 float GammaSeries( float a, float x )
9 {
10  if( x < 0.0 )
11  std::cerr << "GammaSeries::negative argument x" << std::endl;
12 
13  if( x == 0. )
14  return 0.;
15 
16  if( a == 0. ) // this happens at the end, but save all the iterations
17  return 0.;
18 
19  // coefficient c_n of x^n is Gamma(a)/Gamma(a+1+n), which leads to the
20  // recurrence relation c_n = c_(n-1) / (a+n-1) with c_0 = 1/a
21  double term = 1/a;
22  double sum = term;
23  double aplus = a;
24  for( int index = 1; index <= ITMAX; index++) {
25  ++aplus;
26  term *= x/aplus;
27  sum += term;
28  if( fabs(term) < fabs(sum)*EPS )
29  // global coefficient e^-x * x^a / Gamma(a)
30  return sum;
31  }
32  std::cerr << "GammaSeries::a too large, ITMAX too small" << std::endl;
33  return 0.;
34 }
35 
#define ITMAX
Definition: GammaSeries.cc:5
T x() const
Cartesian x coordinate.
#define EPS
Definition: GammaSeries.cc:6
double a
Definition: hdecay.h:121
float GammaSeries(float a, float x)
Definition: GammaSeries.cc:8