CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
IncompleteGamma.cc
Go to the documentation of this file.
1// -*- C++ -*-
2// $Id: IncompleteGamma.cc,v 1.4 2003/10/10 17:40:39 garren Exp $
3
4#include "CLHEP/GenericFunctions/IncompleteGamma.hh"
5#include <assert.h>
6#include <cmath>
7#include <iostream>
8
9using namespace std;
10
11namespace Genfun {
12FUNCTION_OBJECT_IMP(IncompleteGamma)
13
14const int IncompleteGamma::ITMAX =100;
15const double IncompleteGamma::EPS =3.0E-7;
16const double IncompleteGamma::FPMIN =1.0e-30;
17
18
20 _a("a", 1.0, 0,10)
21{}
22
24AbsFunction(right), _a(right._a) {
25}
26
28}
29
30double IncompleteGamma::operator() (double x) const {
31
32 assert(x>=0.0 && _a.getValue() > 0.0);
33
34 if (x < (_a.getValue()+1.0))
35 return _gamser(_a.getValue(),x,_logGamma(_a.getValue()));
36 else
37 return 1.0-_gammcf(_a.getValue(),x,_logGamma(_a.getValue()));
38}
39
41 return _a;
42}
43
44/* ------------------Incomplete gamma function-----------------*/
45/* ------------------via its series representation-------------*/
46double IncompleteGamma::_gamser(double xa, double x, double logGamma) const {
47 double n;
48 double ap,del,sum;
49
50 ap=xa;
51 del=sum=1.0/xa;
52 for (n=1;n<ITMAX;n++) {
53 ++ap;
54 del *= x/ap;
55 sum += del;
56 if (fabs(del) < fabs(sum)*EPS) return sum*exp(-x + xa*log(x) - logGamma);
57 }
58 assert(0);
59 return 0;
60}
61
62/* ------------------Incomplete gamma function complement------*/
63/* ------------------via its continued fraction representation-*/
64
65double IncompleteGamma::_gammcf(double xa, double x, double logGamma) const {
66
67 double an,b,c,d,del,h;
68 int i;
69
70 b = x + 1.0 -xa;
71 c = 1.0/FPMIN;
72 d = 1.0/b;
73 h = d;
74 for (i=1;i<ITMAX;i++) {
75 an = -i*(i-xa);
76 b+=2.0;
77 d=an*d+b;
78 if (fabs(d) < FPMIN) d = FPMIN;
79 c = b+an/c;
80 if (fabs(c) < FPMIN) c = FPMIN;
81 d = 1.0/d;
82 del=d*c;
83 h *= del;
84 if (fabs(del-1.0) < EPS) return exp(-x+xa*log(x)-logGamma)*h;
85 }
86 assert(0);
87 return 0;
88}
89
90
91
92
93} // namespace Genfun
#define FUNCTION_OBJECT_IMP(classname)
Definition: AbsFunction.hh:149
virtual double operator()(double argument) const override
virtual double getValue() const
Definition: Parameter.cc:29
Definition: Abs.hh:14