ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
f1b2lndt.cpp
Go to the documentation of this file.
1 
7 #include <df1b2fun.h>
8 
9 #ifdef ISZERO
10  #undef ISZERO
11 #endif
12 #define ISZERO(d) ((d)==0.0)
13 
15 {
19 public:
20  df1b2matrix_pair(const df1b2matrix & _a,const df1b2matrix & _b);
21  df1b2matrix get_a(void){return a;}
22  df1b2matrix get_b(void){return b;}
23 };
24 
25 dmatrix ludcmp(const dmatrix& M,int kludge);
26 df1b2matrix reorder(const df1b2matrix& M,const ivector& indx);
27 df1b2vector reorder(const df1b2vector& M,const ivector& indx);
28 void ludcmp(const df1b2matrix& a,int k);
30 
42  ivector & ,const df1b2vector& b)
43 {
44  int i,ii=0,j;
46  int mmin=b.indexmin();
47  int mmax=b.indexmax();
48  if (mmin !=1)
49  {
50  cerr << "not implemented" << endl;
51  ad_exit(1);
52  }
53  int n=mmax;
54  df1b2vector c(mmin,mmax);
55  c=b;
56 
57  for (i=1;i<=n;i++)
58  {
59  sum=c(i);
60  if (ii)
61  {
62  df1b2vector bt=c(ii,i-1);
63  df1b2vector at=alpha(i)(ii,i-1);
64  sum-=at*bt;
65  }
66  else
67  {
68  if (!ISZERO(value(sum))) ii=i;
69  }
70  c(i)=sum;
71  }
72  for (i=n;i>=1;i--)
73  {
74  sum=c(i);
75  for (j=i+1;j<=n;j++) sum -= beta(j)(i)*c(j);
76  c(i)=sum/beta(i)(i);
77  }
78  return c;
79 }
80 
82 {
83  // determinant of a lower triangular matrix
84  int i;
85  int mmin=b.indexmin();
86  int mmax=b.indexmax();
87  sgn=1;
89  ln_det=0.0;
90  for (i=mmin;i<=mmax;i++)
91  {
92  if (value(b(i,i))<0.0)
93  {
94  ln_det+=log(-b(i,i));
95  sgn=-sgn;
96  }
97  else
98  {
99  ln_det+=log(b(i,i));
100  }
101  }
102  return ln_det;
103 }
104 
106  ivector & ,const df1b2matrix& B)
107 {
108  int i,ii=0,j;
109  int rmin=B.indexmin();
110  int rmax=B.indexmin();
111  df1b2matrix C(rmin,rmax);
112  for (int k=rmin;k<=rmax;k++)
113  {
115  int mmin=B(k).indexmin();
116  int mmax=B(k).indexmax();
117  if (mmin !=1)
118  {
119  cerr << "not implemented" << endl;
120  ad_exit(1);
121  }
122  int n=mmax;
123  df1b2vector c(mmin,mmax);
124  c=B(k);
125 
126  for (i=1;i<=n;i++) {
127  sum=c(i);
128  if (ii)
129  {
130  df1b2vector bt=c(ii,i-1);
131  df1b2vector at=alpha(i)(ii,i-1);
132  sum-=at*bt;
133  }
134  else
135  {
136  if (!ISZERO(value(sum))) ii=i;
137  }
138  c(i)=sum;
139  }
140  for (i=n;i>=1;i--)
141  {
142  sum=c(i);
143  for (j=i+1;j<=n;j++) sum -= beta(j)(i)*c(j);
144  c(i)=sum/beta(i)(i);
145  }
146  C(i)=c;
147  }
148  return C;
149 }
150 
152  const df1b2matrix& _b) : a(_a), b(_b)
153 {}
154 
155 void ludcmp(const df1b2matrix& M,int kludge)
156 {
157  // do lu decomp once to get ordering
158  int mmin=M.indexmin();
159  int mmax=M.indexmax();
160 
161  ivector indx(mmin,mmax);
162  ivector indx2(mmin,mmax);
163  double d;
164  dmatrix MC=value(M);
165  ludcmp(MC,indx,d);
166 
167  df1b2matrix RM=reorder(M,indx);
168 
169  ludcmp(RM);
170 }
171 
173 {
174  int i;
175  int mmin=M.indexmin();
176  int mmax=M.indexmax();
177  if ( (indx.indexmin() != mmin) || (indx.indexmax() != mmax)
178  || (M(mmin).indexmin() != mmin)
179  || (M(mmin).indexmax() != mmax) )
180  {
181  cerr << " Inconsistent sizes in "
182  " dmatrix reorder(const dmatrix& M,const ivector& indx) "
183  << endl;
184  ad_exit(1);
185  }
186  df1b2matrix RM(mmin,mmax);
187  ivector ir(mmin,mmax);
188  ir.fill_seqadd(mmin,1);
189 
190  for (i=mmin;i<=mmax;i++)
191  {
192  int tmp=ir(i);
193  ir(i)=ir(indx(i));
194  ir(indx(i))=tmp;
195  }
196  //cout << ir << endl;
197  for (i=mmin;i<=mmax;i++)
198  {
199  RM(i)=M(ir(i));
200  }
201  return RM;
202 }
204 {
205  int i;
206  int mmin=M.indexmin();
207  int mmax=M.indexmax();
208  if ( (indx.indexmin() != mmin) || (indx.indexmax() != mmax) )
209  {
210  cerr << " Inconsistent sizes in "
211  " dvector reorder(const dvector& M,const ivector& indx) "
212  << endl;
213  ad_exit(1);
214  }
215  df1b2vector RM(mmin,mmax);
216  ivector ir(mmin,mmax);
217  ir.fill_seqadd(mmin,1);
218 
219  for (i=mmin;i<=mmax;i++)
220  {
221  int tmp=ir(i);
222  ir(i)=ir(indx(i));
223  ir(indx(i))=tmp;
224  }
225  //cout << ir << endl;
226  for (i=mmin;i<=mmax;i++)
227  {
228  RM(i)=M(ir(i));
229  }
230  return RM;
231 }
232 
234 {
235  int i,j;
236 
237  int n=a.indexmax();
238  ivector ishape(1,n);
239  ishape.fill_seqadd(1,1);
240  df1b2matrix alpha(1,n,1,ishape);
241  df1b2matrix beta(1,n,1,ishape);
242  //df1b2matrix delta(1,n,1,n);
243  df1b2vector gamma(1,n);
244  alpha.initialize();
245  beta.initialize();
246  gamma.initialize();
247 
249  for (j=1;j<=n;j++)
250  {
251  for (i=1;i<j;i++)
252  {
253  df1b2vector& ai=alpha(i);
254  df1b2vector& bj=beta(j);
255  sum=a(i,j);
256  //for (k=1;k<i;k++) { sum -= ai(k)*bj(k); }
257  sum-=ai(1,i)*bj(1,i);
258  beta(j,i)=sum;
259  }
260  for (i=j;i<=n;i++)
261  {
262  df1b2vector& ai=alpha(i);
263  df1b2vector& bj=beta(j);
264  sum=a(i,j);
265  //for (k=1;k<j;k++) { sum -= ai(k)*bj(k); }
266  sum-=ai(1,j)*bj(1,j);
267  if (i==j)
268  {
269  beta(i,j)=sum;
270  }
271  else
272  {
273  if (j !=n)
274  {
275  //delta(i,j)=sum;
276  alpha(i,j)=sum;
277  }
278  else
279  {
280  alpha(i,j)=sum;
281  }
282  }
283  }
284  if (j != n)
285  {
286  gamma(j)=1.0/beta(j,j);
287  for (i=j+1;i<=n;i++)
288  {
289  alpha(i,j)*=gamma(j);
290  //alpha(i,j) = delta(i,j)*gamma(j);
291  }
292  }
293  }
294  return df1b2matrix_pair(alpha,beta);
295 }
296 dmatrix reorder(const dmatrix& CM,ivector & indx);
297 ivector getreindex(ivector & indx);
298 
300 {
301  int mmin=M.indexmin();
302  int mmax=M.indexmax();
303  if (mmin !=1)
304  {
305  cerr << "not implemented" << endl;
306  ad_exit(1);
307  }
308  int ssgn=0;
309  dmatrix CM=value(M);
310  double cld=ln_det(CM,ssgn);
311  ivector indx(1,mmax);
312  double dd;
313  ludcmp(CM,indx,dd);
314  ivector reindex=getreindex(indx);
315  df1b2matrix RM=reorder(M,indx);
316  df1b2matrix_pair p=ludcmp(RM);
317  int isg=0;
318  df1b2variable ld=get_ln_det(p.get_b(),isg);
319  cout << setprecision(16) << cld-value(ld) << " ";
320  //cout << dd << " " << isg << " " << dd*isg << endl;
321  return ld;
322 }
323 
325 {
326  int sgn;
327  return ln_det(M,sgn);
328 }
329 
330 dmatrix reorder(const dmatrix& CM,ivector & indx)
331 {
332  int mmin=CM.indexmin();
333  int mmax=CM.indexmax();
334  dmatrix M(mmin,mmax,mmin,mmax);
335  dvector tmp(mmin,mmax);
336  dvector in(mmin,mmax);
337  in.fill_seqadd(1,1);
338  for (int i=mmin;i<=mmax;i++)
339  {
340  M(i)=CM(indx(i));
341  }
342  return M;
343 }
344 void xswitch(int & i1,int & i2)
345 {
346  int tmp=i1;
347  i1=i2;
348  i2=tmp;
349 }
350 
352 {
353  int mmin=indx.indexmin();
354  int mmax=indx.indexmax();
355  ivector in1(mmin,mmax);
356  ivector in2(mmin,mmax);
357  in1.fill_seqadd(1,1);
358  in2.fill_seqadd(1,1);
359  for (int i=mmin;i<=mmax;i++)
360  {
361  xswitch(in1(i),in1(indx(i)));
362  }
363  return in1;
364 }
365 
367 {
368  int mmin=M.indexmin();
369  int mmax=M.indexmax();
370  if (mmin !=1)
371  {
372  cerr << "not implemented" << endl;
373  ad_exit(1);
374  }
375  dmatrix CM=value(M);
376  ivector indx(1,mmax);
377  double dd;
378  ludcmp(CM,indx,dd);
379  df1b2matrix RM=reorder(M,indx);
380  df1b2vector rb=reorder(v,indx);
381  df1b2matrix_pair p=ludcmp(RM);
382  return lubksb(p.get_a(),p.get_b(),indx,rb);
383 }
384 
386 {
387  df1b2variable& ln_det = (df1b2variable&) _ln_det;
388  int mmin=M.indexmin();
389  int mmax=M.indexmax();
390  if (mmin !=1)
391  {
392  cerr << "not implemented" << endl;
393  ad_exit(1);
394  }
395  dmatrix CM=value(M);
396  ivector indx(1,mmax);
397  double dd;
398  ludcmp(CM,indx,dd);
399  df1b2matrix RM=reorder(M,indx);
400  df1b2vector rb=reorder(v,indx);
401  df1b2matrix_pair p=ludcmp(RM);
402  int isg;
403  ln_det=get_ln_det(p.get_b(),isg);
404  return lubksb(p.get_a(),p.get_b(),indx,rb);
405 }
406 
408  const int& sgn)
409 {
410  df1b2variable& ln_det = (df1b2variable&) _ln_det;
411  int mmin=M.indexmin();
412  int mmax=M.indexmax();
413  if (mmin !=1)
414  {
415  cerr << "not implemented" << endl;
416  ad_exit(1);
417  }
418  dmatrix CM=value(M);
419  ivector indx(1,mmax);
420  double dd;
421  ludcmp(CM,indx,dd);
422  df1b2matrix RM=reorder(M,indx);
423  df1b2vector rb=reorder(v,indx);
424  df1b2matrix_pair p=ludcmp(RM);
425  int isg;
426  ln_det=get_ln_det(p.get_b(),isg);
427  return lubksb(p.get_a(),p.get_b(),indx,rb);
428 }
void ludcmp(const dmatrix &a, const ivector &indx, const double &d)
Lu decomposition of a constant matrix.
Definition: dmat3.cpp:221
void xswitch(int &i1, int &i2)
Definition: f1b2lndt.cpp:344
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin(void) const
Definition: df1b2fun.h:1054
void lubksb(dmatrix a, const ivector &indx, dvector b)
LU decomposition back susbstitution alogrithm for constant object.
Definition: dmat3.cpp:455
int indexmin() const
Definition: fvar.hpp:2917
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr.cpp:21
void fill_seqadd(double, double)
Fills dvector elements with values starting from base and incremented by offset.
Definition: cranfill.cpp:58
Description not yet available.
Definition: df1b2fun.h:953
exitptr ad_exit
Definition: gradstrc.cpp:53
void initialize(void)
Description not yet available.
Definition: f1b2vc2.cpp:157
#define ISZERO(d)
Definition: f1b2lndt.cpp:12
int indexmin(void) const
Definition: df1b2fun.h:969
void fill_seqadd(int, int)
Fills ivector elements with values starting from base and incremented by offset.
Definition: cranfill.cpp:73
Description not yet available.
Definition: df1b2fun.h:266
df1b2matrix get_a(void)
Definition: f1b2lndt.cpp:21
ivector sgn(const dvector &v)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dvect24.cpp:11
dvector solve(const dmatrix &aa, const dvector &z)
Solve a linear system using LU decomposition.
Definition: dmat34.cpp:46
prnstream & endl(prnstream &)
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
int indexmin() const
Definition: ivector.h:99
#define M
Definition: rngen.cpp:57
dvariable beta(const prevariable &a, const prevariable &b)
Beta density function.
Definition: vbeta.cpp:18
int indexmax() const
Definition: ivector.h:104
Description not yet available.
Definition: fvar.hpp:2819
double ln_det(const dmatrix &m1, int &sgn)
Compute log determinant of a constant matrix.
Definition: dmat3.cpp:536
int indexmax() const
Definition: fvar.hpp:2921
df1b2matrix a
Definition: f1b2lndt.cpp:16
df1b2matrix get_b(void)
Definition: f1b2lndt.cpp:22
Description not yet available.
Definition: df1b2fun.h:1042
df1b2matrix_pair(const df1b2matrix &_a, const df1b2matrix &_b)
Definition: f1b2lndt.cpp:151
int indexmax(void) const
Definition: df1b2fun.h:970
int indexmax(void) const
Definition: df1b2fun.h:1055
df1b2matrix reorder(const df1b2matrix &M, const ivector &indx)
Definition: f1b2lndt.cpp:172
df1b2variable get_ln_det(const df1b2matrix &b, int &sgn)
Definition: f1b2lndt.cpp:81
Description not yet available.
void initialize(void)
Description not yet available.
Definition: f1b2vc3.cpp:287
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
df1b2matrix b
Definition: f1b2lndt.cpp:17
ivector getreindex(ivector &indx)
Definition: f1b2lndt.cpp:351
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13