ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
combv.cpp
Go to the documentation of this file.
1 
11 #include <fvar.hpp>
12 
13 dvariable gammln(const dvariable& xx);
14 dvariable factln(const dvariable& n);
15 double factln(double n);
16 
24 dvariable log_comb(const dvariable& n, double k)
25 {
26  return factln(n)-factln(k)-factln(n-k);
27 }
28 
37 {
38  return factln(n)-factln(k)-factln(n-k);
39 }
40 
41 
49 dvariable log_comb(double n, const dvariable& k)
50 {
51  return factln(n)-factln(k)-factln(n-k);
52 }
53 
60 {
61  return gammln(n+1.0);
62 }
63 
71 {
74 
75  int mmin=n.indexmin();
76  int mmax=n.indexmax();
77  if (mmin != r.indexmin() || mmax != r.indexmax())
78  {
79  cerr << "Incompatible array bounds in function "
80  "dvar_vector log_comb(const dvar_vector& n, const dvector& r)" << endl;
81  ad_exit(1);
82  }
83  dvar_vector tmp(mmin,mmax);
84  for (int i=mmin;i<=mmax;i++)
85  {
86  tmp(i)=log_comb(n(i),r(i));
87  }
89  return tmp;
90 }
91 
99 {
102  int mmin=n.indexmin();
103  int mmax=n.indexmax();
104  if (mmin != r.indexmin() || mmax != r.indexmax())
105  {
106  cerr << "Incompatible array bounds in function "
107  "dvar_vector log_comb(const dvar_vector& n, const dvector& r)" << endl;
108  ad_exit(1);
109  }
110  dvar_vector tmp(mmin,mmax);
111  for (int i=mmin;i<=mmax;i++)
112  {
113  tmp(i)=log_comb(n(i),r(i));
114  }
116  return tmp;
117 }
118 
126 {
129  int mmin=n.indexmin();
130  int mmax=n.indexmax();
131  if (mmin != r.indexmin() || mmax != r.indexmax())
132  {
133  cerr << "Incompatible array bounds in function "
134  "dvar_vector log_comb(const dvar_vector& n, const dvector& r)" << endl;
135  ad_exit(1);
136  }
137  dvar_vector tmp(mmin,mmax);
138  for (int i=mmin;i<=mmax;i++)
139  {
140  tmp(i)=log_comb(n(i),r(i));
141  }
143  return tmp;
144 }
145 
153 {
156  int mmin=r.indexmin();
157  int mmax=r.indexmax();
158  dvar_vector tmp(mmin,mmax);
159  for (int i=mmin;i<=mmax;i++)
160  {
161  tmp(i)=log_comb(n,r(i));
162  }
164  return tmp;
165 }
166 
174 {
177  int mmin=r.indexmin();
178  int mmax=r.indexmax();
179  dvar_vector tmp(mmin,mmax);
180  for (int i=mmin;i<=mmax;i++)
181  {
182  tmp(i)=log_comb(n,r(i));
183  }
185  return tmp;
186 }
187 
194 {
197  int mmin=r.indexmin();
198  int mmax=r.indexmax();
199  dvar_vector tmp(mmin,mmax);
200  for (int i=mmin;i<=mmax;i++)
201  {
202  tmp(i)=gammln(r(i)+1.0);
203  }
205  return tmp;
206 }
207 
214 dvar_vector log_comb(double n, const dvar_vector& r)
215 {
218  int mmin=r.indexmin();
219  int mmax=r.indexmax();
220  dvar_vector tmp(mmin,mmax);
221  for (int i=mmin;i<=mmax;i++)
222  {
223  tmp(i)=log_comb(n,r(i));
224  }
226  return tmp;
227 }
228 
235 {
238  int mmin=v.indexmin();
239  int mmax=v.indexmax();
240  dvar_vector tmp(mmin,mmax);
241  for (int i=mmin;i<=mmax;i++)
242  {
243  tmp(i)=gammln(v(i));
244  }
246  return tmp;
247 }
248 
250 {
251  double z = value(_z);
252  //double zdot=1.0;
253  //const double lpi =1.1447298858494001741434272;
254  //const double pi =3.1415926535897932384626432;
255  const double lpp =0.9189385332046727417803297;
256  int n=7;
257  const double c[9]={0.99999999999980993,
258  676.5203681218851,
259  -1259.1392167224028,
260  771.32342877765313,
261  -176.61502916214059,
262  12.507343278686905,
263  -0.13857109526572012,
264  9.9843695780195716e-6,
265  1.5056327351493116e-7};
266  z-=1.0;
267  double x=c[0];
268  double xdot=0.0;
269  for (int i=1;i<=n+1;i++)
270  {
271  double zinv=1.0/(z+i);
272  x+=c[i]*zinv;
273  //xdot-=c[i]/square(z+i)*zdot; since zdot=1.0
274  xdot-=c[i]*square(zinv);
275  }
276  double t=z+n+0.5;
277  //return lpp + (z+0.5)*log(t) -t + log(x);
278  double ans= lpp + (z+0.5)*log(t) -t + log(x);
279  //double tdot=zdot;
280  //double ansdot=zdot*log(t) + (z+0.5)/t*tdot -tdot +xdot/x;
281  // since tdot=1.0
282  // since zdot=1.0
283  double ansdot=log(t) + (z+0.5)/t -1.0 +xdot/x;
284  dvariable u;
285  u.v->x=ans;
287  &(u.v->x), &(_z.v->x), ansdot );
288  return(u);
289 }
290 
292 {
293  const double lpi =1.1447298858494001741434272;
294  const double pi =3.1415926535897932384626432;
295  if (z<0.5)
296  {
297  return lpi - log(sin(pi*z)) - gammlnguts(1.0-z);
298  }
299  else
300  {
301  return gammlnguts(z);
302  }
303 }
Base class for dvariable.
Definition: fvar.hpp:1315
double factln(double n)
Log-factorial .
Definition: combc.cpp:38
double log_comb(double n, double k)
Log of the binomial coefficent; i.e log of &#39;n choose k&#39;.
Definition: combc.cpp:27
double gammln(double xx)
Log gamma function.
Definition: combc.cpp:52
#define x
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
d3_array sin(const d3_array &arr3)
Returns d3_array results with computed sin from elements in arr3.
Definition: d3arr2a.cpp:43
exitptr ad_exit
Definition: gradstrc.cpp:53
ADMB variable vector.
Definition: fvar.hpp:2172
void set_gradient_stack(void(*func)(void), double *dep_addr, double *ind_addr1=NULL, double mult1=0, double *ind_addr2=NULL, double mult2=0)
Description not yet available.
Definition: fvar.hpp:1045
prnstream & endl(prnstream &)
void RETURN_ARRAYS_INCREMENT()
Definition: gradstrc.cpp:478
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
void default_evaluation(void)
Description not yet available.
Definition: def_eval.cpp:61
static _THREAD gradient_structure * _instance
double_and_int * v
pointer to the data
Definition: fvar.hpp:1333
int indexmin() const
Definition: fvar.hpp:2287
void RETURN_ARRAYS_DECREMENT()
Definition: gradstrc.cpp:511
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
static dvariable gammlnguts(const prevariable &_z)
Definition: combv.cpp:249
static _THREAD grad_stack * GRAD_STACK1
class for things related to the gradient structures, including dimension of arrays, size of buffers, etc.
int indexmax() const
Definition: fvar.hpp:2292
double square(const double value)
Return square of value; constant object.
Definition: d3arr4.cpp:16
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
double x
&lt; value of the variable
Definition: fvar.hpp:195