ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fvar_m51.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  */
11 #include "fvar.hpp"
12 
13 #ifdef __TURBOC__
14 #pragma hdrstop
15 #include <iostream.h>
16 #endif
17 
18 #ifdef __ZTC__
19 #include <iostream.hpp>
20 #endif
21 
22 #ifdef __SUN__
23 #include <iostream.h>
24 #endif
25 #ifdef __NDPX__
26 #include <iostream.h>
27 #endif
28 void dfcholeski_decomp(void);
29 
30 // compute log of determinant of a spd matrix
31 void df_ln_det_choleski(void);
32 
38 {
39  // kludge to deal with constantness
40  dmatrix M=value(MM);
41  if (M.colsize() != M.rowsize())
42  {
43  cerr << "Error in chol_decomp. Matrix not square" << endl;
44  ad_exit(1);
45  }
46  if (M.colsize() == 1)
47  {
48  int mmin=M.indexmin();
49  return log(MM(mmin,mmin));
50  }
51 
52  //int rowsave=M.rowmin();
53  //int colsave=M.colmin();
54  M.rowshift(1);
55  M.colshift(1);
56  int n=M.rowmax();
57 
58  dmatrix L(1,n,1,n);
59  //dmatrix C(1,n,1,n);
60  //imatrix B(1,n,1,n);
61  //B.initialize();
62 #ifndef SAFE_INITIALIZE
63  L.initialize();
64 #endif
65 
66  if (M(1,1)<=0)
67  {
68  cerr << "Error matrix not positive definite in choleski_decomp"
69  <<endl;
70  ad_exit(1);
71  }
72  L(1,1)=sqrt(M(1,1));
73  for (int i=2;i<=n;i++)
74  {
75  L(i,1)=M(i,1)/L(1,1);
76  }
77 
78  double tmp;
79  for (int i=2;i<=n;i++)
80  {
81  for (int j=2;j<=i-1;j++)
82  {
83  tmp=M(i,j);
84  for (int k=1;k<=j-1;k++)
85  {
86  tmp-=L(i,k)*L(j,k);
87  }
88  L(i,j)=tmp/L(j,j);
89  }
90  tmp=M(i,i);
91  for (int k=1;k<=i-1;k++)
92  {
93  tmp-=L(i,k)*L(i,k);
94  }
95  if (tmp<=0)
96  {
97  cerr << "Error matrix not positive definite in ln_det_choleski"
98  <<endl;
99  ad_exit(1);
100  }
101  L(i,i)=sqrt(tmp);
102  }
103  //L.rowshift(rowsave);
104  //L.colshift(colsave);
105  double log_det1=0.0;
106  for (int i=1;i<=n;i++)
107  {
108  log_det1+=log(L(i,i));
109  }
110  /*
111  double minL=L(1,1);
112  for (i=2;i<=k;i++)
113  {
114  if (L(i,i)<minL) minL=L(i,i);
115  }
116  cout << "min in choleski = " << minL << endl;
117  */
118 
119 
120  double log_det=2.0*log_det1;
121  dvariable vlog_det=nograd_assign(log_det);
124 
126  fp->save_prevariable_position(vlog_det);
128  fp->save_dvar_matrix_value(MM);
133  return vlog_det;
134 }
135 
141 {
143 
149  //prevariable_position vcpos=restore_prevariable_position();
150  double dflog_det=fp->restore_prevariable_derivative();
152 
153  if (M.colsize() != M.rowsize())
154  {
155  cerr << "Error in chol_decomp. Matrix not square" << endl;
156  ad_exit(1);
157  }
158  int rowsave=M.rowmin();
159  int colsave=M.colmin();
160  M.rowshift(1);
161  M.colshift(1);
162  int n=M.rowmax();
163 
164  dmatrix L(1,n,1,n);
165  dmatrix dfL(1,n,1,n);
166  dvector tmp(1,n);
167  dmatrix tmp1(1,n,1,n);
168  dmatrix dftmp1(1,n,1,n);
169  dmatrix dfM(1,n,1,n);
170  dvector dftmp(1,n);
171  tmp.initialize();
172  tmp1.initialize();
173  dftmp.initialize();
174  dftmp1.initialize();
175  dfM.initialize();
176  dfL.initialize();
177 #ifndef SAFE_INITIALIZE
178  L.initialize();
179 #endif
180 
181  if (M(1,1)<=0)
182  {
183  cerr << "Error matrix not positive definite in choleski_decomp"
184  <<endl;
185  ad_exit(1);
186  }
187  L(1,1)=sqrt(M(1,1));
188  for (int i=2;i<=n;i++)
189  {
190  L(i,1)=M(i,1)/L(1,1);
191  }
192 
193  for (int i=2;i<=n;i++)
194  {
195  for (int j=2;j<=i-1;j++)
196  {
197  tmp1(i,j)=M(i,j);
198  for (int k=1;k<=j-1;k++)
199  {
200  tmp1(i,j)-=L(i,k)*L(j,k);
201  }
202  L(i,j)=tmp1(i,j)/L(j,j);
203  }
204  tmp(i)=M(i,i);
205  for (int k=1;k<=i-1;k++)
206  {
207  tmp(i)-=L(i,k)*L(i,k);
208  }
209  if (tmp(i)<=0)
210  {
211  cerr << "Error matrix not positive definite in choleski_decomp"
212  <<endl;
213  ad_exit(1);
214  }
215  L(i,i)=sqrt(tmp(i));
216  }
217  double log_det1=0.0;
218  for (int i=1;i<=n;i++)
219  {
220  log_det1+=log(L(i,i));
221  }
222  //double log_det=2.0*log_det1;
223 
224  //*******************************************************************8
225  //*******************************************************************8
226  //*******************************************************************8
227 
228  //double log_det=2.0*log_det1;
229  double dflog_det1=2.0*dflog_det;
230  for (int i=1;i<=n;i++)
231  {
232  //log_det1+=log(L(i,i));
233  dfL(i,i)=dflog_det1/L(i,i);
234  }
235 
236  for (int i=n;i>=2;i--)
237  {
238  //L(i,i)=sqrt(tmp(i));
239  dftmp(i)+=dfL(i,i)/(2.0*L(i,i));
240  dfL(i,i)=0.0;
241  for (int k=i-1;k>=1;k--)
242  {
243  //tmp(i)-=L(i,k)*L(i,k);
244  dfL(i,k)-=2.*dftmp(i)*L(i,k);
245  }
246  //tmp(i)=M(i,i);
247  dfM(i,i)+=dftmp(i);
248  dftmp(i)=0.0;
249  for (int j=i-1;j>=2;j--)
250  {
251  //L(i,j)=tmp1(i,j)/L(j,j);
252  double linv=1./L(j,j);
253  dftmp1(i,j)+=dfL(i,j)*linv;
254  dfL(j,j)-=dfL(i,j)*tmp1(i,j)*linv*linv;
255  dfL(i,j)=0.0;
256  for (int k=j-1;k>=1;k--)
257  {
258  //tmp(i,j)-=L(i,k)*L(j,k);
259  dfL(i,k)-=dftmp1(i,j)*L(j,k);
260  dfL(j,k)-=dftmp1(i,j)*L(i,k);
261  }
262  //tmp(i,j)=M(i,j);
263  dfM(i,j)+=dftmp1(i,j);
264  dftmp1(i,j)=0.0;
265  }
266  }
267  double linv=1./L(1,1);
268  for (int i=n;i>=2;i--)
269  {
270  //L(i,1)=M(i,1)/L(1,1);
271  dfM(i,1)+=dfL(i,1)*linv;
272  dfL(1,1)-=dfL(i,1)*M(i,1)*linv*linv;
273  dfL(i,1)=0.0;
274  }
275  //L(1,1)=sqrt(M(1,1));
276  dfM(1,1)+=dfL(1,1)/(2.*L(1,1));
277 
278 
279  //*******************************************************************8
280  //*******************************************************************8
281  //*******************************************************************8
282 
283  dfM.rowshift(rowsave);
284  dfM.colshift(colsave);
285 
286  dfM.save_dmatrix_derivatives(MMpos);
287 }
288 
294 {
295  onerror=1;
296  dvariable v=0.0;
297  return v;
298 }
299 
305 {
306  onerror=0;
307  // kludge to deal with constantness
308  dmatrix M=value(MM);
309  if (M.colsize() != M.rowsize())
310  {
311  cerr << "Error in chol_decomp. Matrix not square" << endl;
312  ad_exit(1);
313  }
314  if (M.colsize() == 1)
315  {
316  int mmin=M.indexmin();
317  return log(MM(mmin,mmin));
318  }
319 
320  //int rowsave=M.rowmin();
321  //int colsave=M.colmin();
322  M.rowshift(1);
323  M.colshift(1);
324  int n=M.rowmax();
325 
326  dmatrix L(1,n,1,n);
327  //dmatrix C(1,n,1,n);
328  //imatrix B(1,n,1,n);
329  //B.initialize();
330 #ifndef SAFE_INITIALIZE
331  L.initialize();
332 #endif
333 
334  if (M(1,1)<=0)
335  {
336  return error_condition(onerror);
337  }
338  L(1,1)=sqrt(M(1,1));
339  for (int i=2;i<=n;i++)
340  {
341  L(i,1)=M(i,1)/L(1,1);
342  }
343 
344  double tmp;
345  for (int i=2;i<=n;i++)
346  {
347  for (int j=2;j<=i-1;j++)
348  {
349  tmp=M(i,j);
350  for (int k=1;k<=j-1;k++)
351  {
352  tmp-=L(i,k)*L(j,k);
353  }
354  L(i,j)=tmp/L(j,j);
355  }
356  tmp=M(i,i);
357  for (int k=1;k<=i-1;k++)
358  {
359  tmp-=L(i,k)*L(i,k);
360  }
361  if (tmp<=0)
362  {
363  return error_condition(onerror);
364  }
365  L(i,i)=sqrt(tmp);
366  }
367  //L.rowshift(rowsave);
368  //L.colshift(colsave);
369  double log_det1=0.0;
370  for (int i=1;i<=n;i++)
371  {
372  log_det1+=log(L(i,i));
373  }
374  /*
375  double minL=L(1,1);
376  for (i=2;i<=k;i++)
377  {
378  if (L(i,i)<minL) minL=L(i,i);
379  }
380  cout << "min in choleski = " << minL << endl;
381  */
382  double log_det=2.0*log_det1;
383  dvariable vlog_det=nograd_assign(log_det);
387  fp->save_prevariable_position(vlog_det);
389  fp->save_dvar_matrix_value(MM);
394  return vlog_det;
395 }
Description not yet available.
Definition: fvar.hpp:920
double restore_prevariable_derivative()
Definition: cmpdif8.cpp:147
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Definition: fvar.hpp:2917
static dmatrix onerror(dmatrix &L, int &ierror)
Description not yet available.
Definition: dmat15.cpp:101
dvar_vector nograd_assign(dvector tmp)
Description not yet available.
Definition: cmpdif6.cpp:252
exitptr ad_exit
Definition: gradstrc.cpp:53
void verify_identifier_string(const char *)
Verifies gradient stack string.
Definition: cmpdif3.cpp:149
unsigned int colsize() const
Definition: fvar.hpp:2948
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
void dfcholeski_decomp(void)
Description not yet available.
Definition: fvar_m39.cpp:170
dvariable ln_det_choleski_error(const dvar_matrix &, int &ierr)
Description not yet available.
Definition: fvar_m51.cpp:304
dmatrix restore_dvar_matrix_value(const dvar_matrix_position &mpos)
Definition: cmpdif5.cpp:74
prnstream & endl(prnstream &)
int rowmax() const
Definition: fvar.hpp:2929
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
void save_prevariable_position(const prevariable &v)
Definition: cmpdif8.cpp:60
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
void df_ln_det_choleski(void)
Description not yet available.
Definition: fvar_m51.cpp:140
void save_dmatrix_derivatives(const dvar_matrix_position &pos) const
Description not yet available.
Definition: cmpdif5.cpp:285
#define M
Definition: rngen.cpp:57
int colmin(void) const
Definition: fvar.hpp:2939
Description not yet available.
Definition: fvar.hpp:2819
void initialize(void)
Initialze all elements of dvector to zero.
Definition: dvect5.cpp:10
int save_identifier_string(const char *)
Writes a gradient stack verification string.
Definition: cmpdif2.cpp:315
dvar_matrix_position restore_dvar_matrix_position()
Definition: cmpdif6.cpp:114
static dvariable error_condition(int &onerror)
Description not yet available.
Definition: fvar_m51.cpp:293
static _THREAD DF_FILE * fp
void save_dvar_matrix_position(const dvar_matrix &m)
Definition: cmpdif5.cpp:345
void rowshift(int min)
Changes the range of valid indices for the rows.
Definition: dmat9.cpp:48
Class definition of matrix with derivitive information .
Definition: fvar.hpp:2480
Stores the adjoint gradient data that will be processed by gradcalc.
double ln_det_choleski(const banded_symmetric_dmatrix &MM, int &ierr)
Definition: dmat38.cpp:218
void colshift(int min)
Description not yet available.
Definition: dmat9.cpp:68
void save_dvar_matrix_value(const dvar_matrix &m)
Definition: cmpdif4.cpp:252
unsigned int rowsize() const
Definition: fvar.hpp:2934
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
static _THREAD grad_stack * GRAD_STACK1
void initialize(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat7.cpp:12
int rowmin() const
Definition: fvar.hpp:2925
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
Description not yet available.
Definition: fvar.hpp:4843