ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
combc.cpp
Go to the documentation of this file.
1 /*
2  $Id$
3 
4  Author: David Fournier
5  Copyright (c) 2009-2012 ADMB Foundation
6  */
14 #include <fvar.hpp>
15 
16 double factln(double n);
17 double gammln(double xx);
18 
27 double log_comb(double n, double k)
28 {
29  return factln(n)-factln(k)-factln(n-k);
30 }
31 
38 double factln(double n)
39 {
40  return gammln(n+1.0);
41 }
42 
52 double gammln(double xx)
53 {
54  double x,tmp,ser;
55  static double cof[6]={76.18009173,-86.50532033,24.01409822,
56  -1.231739516,0.120858003e-2,-0.536382e-5};
57  int j;
58  x=xx-1.0;
59  tmp=x+5.5;
60  tmp -= (x+0.5)*log(tmp);
61  ser=1.0;
62  for (j=0;j<=5;j++)
63  {
64  x += 1.0;
65  ser += cof[j]/x;
66  }
67  return -tmp+log(2.50662827465*ser);
68 }
69 
77 dvector log_comb(const dvector& n, const dvector& r)
78 {
79  int mmin=n.indexmin();
80  int mmax=n.indexmax();
81  if (mmin != r.indexmin() || mmax != r.indexmax())
82  {
83  cerr << "Incompatible array bounds in function "
84  "dvector log_comb(const dvector& n, const dvector& r)" << endl;
85  ad_exit(1);
86  }
87  dvector tmp(mmin,mmax);
88  for (int i=mmin;i<=mmax;i++)
89  {
90  tmp(i)=log_comb(n(i),r(i));
91  }
92  return tmp;
93 }
94 
101 dvector log_comb(double n, const dvector& r)
102 {
103  int mmin=r.indexmin();
104  int mmax=r.indexmax();
105  dvector tmp(mmin,mmax);
106  for (int i=mmin;i<=mmax;i++)
107  {
108  tmp(i)=log_comb(n,r(i));
109  }
110  return tmp;
111 }
112 
120 {
121  int mmin=v.indexmin();
122  int mmax=v.indexmax();
123  dvector tmp(mmin,mmax);
124  for (int i=mmin;i<=mmax;i++)
125  {
126  tmp(i)=gammln(v(i));
127  }
128  return tmp;
129 }
130 
138 {
139  int mmin=r.indexmin();
140  int mmax=r.indexmax();
141  dvector tmp(mmin,mmax);
142  for (int i=mmin;i<=mmax;i++)
143  {
144  tmp(i)=gammln(r(i)+1.0);
145  }
146  return tmp;
147 }
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
exitptr ad_exit
Definition: gradstrc.cpp:53
prnstream & endl(prnstream &)
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13