ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dfgammp.cpp
Go to the documentation of this file.
1 
17 #include <fvar.hpp>
18 #define ITMAX 200
19 #define EPS 1.0e-9
20 //#define EPS 3.0e-7
21 #define FPMIN 1.0e-30
22 double get_values(double x,double y,int print_switch);
23 
30 void gcf(const dvariable& _gammcf,const dvariable& a,
31  const dvariable& x,const dvariable& _gln)
32 {
33  ADUNCONST(dvariable,gln)
34  ADUNCONST(dvariable,gammcf)
35  int i;
36  dvariable an,b,c,d,del,h;
37 
38  gln=gammln(a);
39  b=x+1.0-a;
40  c=1.0/FPMIN;
41  d=1.0/b;
42  h=d;
43  for (i=1;i<=ITMAX;i++) {
44  an = -i*(i-a);
45  b += 2.0;
46  d=an*d+b;
47  if (fabs(value(d)) < FPMIN) d=FPMIN;
48  c=b+an/c;
49  if (fabs(value(c)) < FPMIN) c=FPMIN;
50  d=1.0/d;
51  del=d*c;
52  h *= del;
53  if (fabs(value(del)-1.0) < EPS) break;
54  }
55  if (i > ITMAX)
56  cerr << "a too large, ITMAX too small in gcf" << endl;
57  gammcf=exp(-x+a*log(x)-(gln))*h;
58 }
59 
66 void gser(const dvariable& _gamser,const dvariable& a,
67  const dvariable& x,const dvariable& _gln)
68 {
69  ADUNCONST(dvariable,gln)
70  ADUNCONST(dvariable,gamser)
71  int n;
72  dvariable sum,del,ap;
73 
74  gln=gammln(a);
75 
76  if (value(x) <= 0.0) {
77  if (value(x) < 0.0)
78  cerr << "x less than 0 in routine gser" << endl;
79  gamser=0.0;
80  return;
81  }
82  else
83  {
84  ap=a;
85  del=sum=1.0/a;
86  for (n=1;n<=ITMAX;n++) {
87  ap+=1.0;
88  del *= x/ap;
89  sum += del;
90  if (fabs(value(del)) < fabs(value(sum))*EPS) {
91  gamser=sum*exp(-x+a*log(x)-(gln));
92  return;
93  }
94  }
95  cerr << "a too large, ITMAX too small in routine gser" << endl;
96  return;
97  }
98 }
99 
101 {
102  dvariable gamser,gammcf,gln;
103 
104  if (value(x) < 0.0 || value(a) <= 0.0)
105  cerr << "Invalid arguments in routine gammp" << endl;
106  if (value(x) < (value(a)+1.0)) {
107  gser(gamser,a,x,gln);
108  return gamser;
109  } else {
110  gcf(gammcf,a,x,gln);
111  return 1.0-gammcf;
112  }
113 }
double gammln(double xx)
Log gamma function.
Definition: combc.cpp:52
#define EPS
Definition: betacf_val.hpp:10
#define x
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Definition: fvar.hpp:140
double cumd_gamma(double x, double a)
Definition: cgamdev.cpp:38
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr.cpp:21
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
#define FPMIN
Definition: dfgammp.cpp:21
prnstream & endl(prnstream &)
#define ITMAX
Definition: dfgammp.cpp:18
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
d3_array exp(const d3_array &arr3)
Returns d3_array results with computed exp from elements in arr3.
Definition: d3arr2a.cpp:28
void gcf(double &gammcf, double a, double x, double &gln)
Incomplete gamma function.
Definition: cgamdev.cpp:59
void gser(double &gamser, double a, double x, double &gln)
Incomplete gamma function.
Definition: cgamdev.cpp:92
double get_values(double x, double y, int print_switch)
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13