ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fvar_m40.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 
18  (const banded_lower_triangular_dvar_matrix& S) : bw(S.bw), d(S.d)
19 {}
20 
26  (const banded_symmetric_dvar_matrix& S) : bw(S.bw), d(S.d)
27 {}
28 
29 #if !defined(OPT_LIB)
30 
35 {
36  return d(i);
37 }
38 
44 {
45  return d(i-j,i);
46 }
47 
53 {
54  return d(i-j,i);
55 }
56 
58  const
59 {
60  return d(i-j,i);
61 }
62 
68 {
69  return d(i);
70 }
71 #endif
72 
78 {
79  for (int i=rowmin();i<=rowmax();i++)
80  {
81  (*this)(i).initialize();
82  }
83 }
84 
90 {
91  for (int i=rowmin();i<=rowmax();i++)
92  {
93  (*this)(i).initialize();
94  }
95 }
96 
102  (int _min,int _max,int _bw)
103 {
104  bw=_bw;
105  ivector lb(0,_bw-1);
106  lb.fill_seqadd(_min,1);
107  d.allocate(0,_bw-1,lb,_max);
108 }
109 
115 {
116  int bw=v.bandwidth();
118  for (int i=0;i<=bw-1;i++)
119  {
120  w.d(i)=value(v.d(i));
121  }
122  return w;
123 }
124 
131 {
132  int bw=v.bandwidth();
134  for (int i=0;i<=bw-1;i++)
135  {
136  w.d(i)=value(v.d(i));
137  }
138  return w;
139 }
140 
145 ostream& operator<<(const ostream& _ofs, const banded_symmetric_dvar_matrix& S1)
146 {
147  ostream& ofs= (ostream&) _ofs;
149 
150  int imin=S.indexmin();
151  int imax=S.indexmax();
152  int bw=S.bandwidth();
153  int i1;
154  int j1;
155  for (int i=imin;i<=imax;i++)
156  {
157  for (int j=imin;j<=imax;j++)
158  {
159  if (j<i)
160  {
161  j1=j;
162  i1=i;
163  }
164  else
165  {
166  j1=i;
167  i1=j;
168  }
169  if ( (i1-j1) < bw)
170  ofs << S(i1,j1) << " ";
171  else
172  ofs << 0.0 << " ";
173  }
174  if (i<imax) ofs << endl;
175  }
176  return (ostream&)ofs;
177 }
178 
184  (int _min,int _max,int _bw)
185 {
186  bw=_bw;
187  ivector lb(0,_bw-1);
188  lb.fill_seqadd(_min,1);
189  d.allocate(0,_bw-1,lb,_max);
190 }
191 
196 ostream& operator<<(const ostream& _ofs,
198 {
199  ostream& ofs= (ostream&) _ofs;
202  int imin=S.indexmin();
203  int imax=S.indexmax();
204  int bw=S.bandwidth();
205  for (int i=imin;i<=imax;i++)
206  {
207  for (int j=imin;j<=imax;j++)
208  {
209  if (j<=i)
210  {
211  if ( (i-j) < bw)
212  ofs << S(i,j) << " ";
213  else
214  ofs << 0.0 << " ";
215  }
216  else
217  {
218  ofs << 0.0 << " ";
219  }
220  }
221  if (i<imax) ofs << endl;
222  }
223  return (ostream&)ofs;
224 }
225 
226 
227 
228 #include "fvar.hpp"
229 
230 #ifdef __TURBOC__
231 #pragma hdrstop
232 #include <iostream.h>
233 #endif
234 
235 #ifdef __ZTC__
236 #include <iostream.hpp>
237 #endif
238 
239 #ifdef __SUN__
240 #include <iostream.h>
241 #endif
242 #ifdef __NDPX__
243 #include <iostream.h>
244 #endif
245 void dfcholeski_decomp(void);
246 void dfcholeski_decomp_banded(void);
247 
250  int& ierr)
251 {
253  dvariable ld=0.0;
254  if (ierr==1)
255  {
256  return ld;
257  }
258 
259  int mmin=tmp.indexmin();
260  int mmax=tmp.indexmax();
261  for (int i=mmin;i<=mmax;i++)
262  {
263  ld+=log(tmp(i,i));
264  }
265  return 2.0*ld;
266 }
269  int& ierr)
270 {
271  // kludge to deal with constantness
272  ierr = 0;
274  int n=M.indexmax();
275 
276  int bw=M.bandwidth();
278 #ifndef SAFE_INITIALIZE
279  L.initialize();
280 #endif
281 
282  int i,j,k;
283  double tmp;
284  if (M(1,1)<=0)
285  {
286  cerr << "Error matrix not positive definite in choleski_decomp"
287  " value was " << M(1,1) << " for index 1" <<endl;
288  ierr=1;
289  M(1,1)=1.0;
290  }
291  L.elem_value(1,1)=sqrt(value(M(1,1)));
292  for (i=2;i<=bw;i++)
293  {
294  L.elem_value(i,1)=value(M(i,1))/L.elem_value(1,1);
295  }
296 
297  for (i=2;i<=n;i++)
298  {
299  int jmin=admax(2,i-bw+1);
300  for (j=jmin;j<=i-1;j++)
301  {
302  tmp=value(M(i,j));
303  int kmin=max(1,j-bw+1,i-bw+1);
304  for (k=kmin;k<=j-1;k++)
305  {
306  tmp-=L.elem_value(i,k)*L.elem_value(j,k);
307  }
308  L.elem_value(i,j)=tmp/L.elem_value(j,j);
309  }
310  tmp=value(M(i,i));
311  int kmin=admax(i-bw+1,1);
312  //double vmax=fabs(L.elem_value(i,kmin));
313  //for (k=kmin;k<=i-1;k++)
314  //{
315  // if (fabs(L.elem_value(i,k))>vmax) vmax=fabs(L.elem_value(i,k));
316  //}
317  //tmp/=square(vmax);
318  for (k=kmin;k<=i-1;k++)
319  {
320  tmp-=L.elem_value(i,k)*L.elem_value(i,k);
321  //tmp-=square(L.elem_value(i,k)/vmax);
322  }
323  if (tmp<=0)
324  {
325  cerr << "Error matrix not positive definite in choleski_decomp"
326  " value was " << tmp << " for index " << i <<endl;
327  ierr=1;
328 
329 #ifdef DIAG
330  int print_switch=0;
331  if (print_switch)
332  {
333  dmatrix CMM=dmatrix(value(MM));
334  ofstream ofs("hh");
335  {
336  ofs << setprecision(3) << setscientific() << setw(11)
337  << CMM << endl<< endl;
338  }
339  dvector ev(CMM.indexmin(),CMM.indexmax());
340  dmatrix evec=eigenvectors(CMM,ev);
341  ofs << setprecision(3) << setscientific() << setw(11)
342  << ev << endl<< endl;
343  ofs << setprecision(3) << setscientific() << setw(11)
344  << evec << endl<< endl;
345  uostream uos("uos");
346  uos << CMM.indexmax()-CMM.indexmin()+1;
347  uos << CMM;
348  }
349 #endif
350  /*
351  dmatrix N(1,4,1,4);
352 
353  for (int i=1;i<=4;i++)
354  {
355  N(i,i)=value(M(i,i));
356  for (int j=1;j<i;j++)
357  {
358  N(i,j)=value(M(i,j));
359  N(j,i)=value(M(i,j));
360  }
361  }
362  cout << N << endl;
363  cout << eigenvalues(N) << endl;
364  cout << choleski_decomp(N) << endl;
365  */
366 
367  tmp=1.0;
368  }
369  //L.elem_value(i,i)=vmax*sqrt(tmp);
370  L.elem_value(i,i)=sqrt(tmp);
371  }
372 
375  //banded_lower_triangular_dvar_matrix vc=nograd_assign(L);
379  fp->save_dvar_matrix_value(MM.d);
384 
385  return L;
386 }
387 
393 {
395 
406 
407  int n=M.indexmax();
408  int bw=M.bandwidth();
409 
411  banded_lower_triangular_dmatrix tmp1(1,n,bw);
412  banded_lower_triangular_dmatrix dftmp1(1,n,bw);
413  banded_symmetric_dmatrix dfM(1,n,bw);
414  dvector tmp(1,n);
415  dvector dftmp(1,n);
416  tmp.initialize();
417  tmp1.initialize();
418  dftmp.initialize();
419  dftmp1.initialize();
420  dfM.initialize();
421 #ifndef SAFE_INITIALIZE
422  L.initialize();
423 #endif
424 
425  int i,j,k;
426  if (M(1,1)<=0)
427  {
428  cerr << "Error matrix not positive definite in choleski_decomp"
429  <<endl;
430  M(1,1)=1.0;
431  //ad_exit(1);
432  }
433  L(1,1)=sqrt(M(1,1));
434  for (i=2;i<=bw;i++)
435  {
436  L(i,1)=M(i,1)/L(1,1);
437  }
438 
439  for (i=2;i<=n;i++)
440  {
441  int jmin=admax(2,i-bw+1);
442  for (j=jmin;j<=i-1;j++)
443  {
444  tmp1(i,j)=M(i,j);
445  int kmin=max(1,j-bw+1,i-bw+1);
446  for (k=kmin;k<=j-1;k++)
447  {
448  tmp1(i,j)-=L(i,k)*L(j,k);
449  }
450  L(i,j)=tmp1(i,j)/L(j,j);
451  }
452  tmp(i)=M(i,i);
453  int kmin=admax(i-bw+1,1);
454  //double vmax=fabs(L(i,kmin));
455  //for (k=kmin;k<=i-1;k++)
456  // {
457  // if (fabs(L(i,k))>vmax) vmax=fabs(L(i,k));
458  // }
459  //tmp(i)/=square(vmax);
460  for (k=kmin;k<=i-1;k++)
461  {
462  tmp(i)-=L(i,k)*L(i,k);
463  //tmp(i)-=square(L(i,k)/vmax);
464  }
465  if (tmp(i)<=0)
466  {
467  cerr << "Error matrix not positive definite in choleski_decomp"
468  <<endl;
469  tmp(i)=1.0;
470  //ad_exit(1);
471  }
472  //L(i,i)=vmax*sqrt(tmp(i));
473  L(i,i)=sqrt(tmp(i));
474  }
475  //*******************************************************************8
476  //*******************************************************************8
477  //*******************************************************************8
478  for (i=n;i>=2;i--)
479  {
480  //L(i,i)=sqrt(tmp(i));
481  dftmp(i)+=dfL(i,i)/(2.0*L(i,i));
482  dfL(i,i)=0.0;
483  int kmin=admax(i-bw+1,1);
484  for (k=i-1;k>=kmin;k--)
485  {
486  //tmp(i)-=L(i,k)*L(i,k);
487  dfL(i,k)-=2.*dftmp(i)*L(i,k);
488  }
489  //tmp(i)=M(i,i);
490  dfM(i,i)+=dftmp(i);
491  dftmp(i)=0.0;
492  int jmin=admax(2,i-bw+1);
493  for (j=i-1;j>=jmin;j--)
494  {
495  //L(i,j)=tmp1(i,j)/L(j,j);
496  double linv=1./L(j,j);
497  dftmp1(i,j)+=dfL(i,j)*linv;
498  dfL(j,j)-=dfL(i,j)*tmp1(i,j)*linv*linv;
499  dfL(i,j)=0.0;
500  kmin=max(1,j-bw+1,i-bw+1);
501  for (k=j-1;k>=kmin;k--)
502  {
503  //tmp(i,j)-=L(i,k)*L(j,k);
504  dfL(i,k)-=dftmp1(i,j)*L(j,k);
505  dfL(j,k)-=dftmp1(i,j)*L(i,k);
506  }
507  //tmp(i,j)=M(i,j);
508  dfM(i,j)+=dftmp1(i,j);
509  dftmp1(i,j)=0.0;
510  }
511  }
512  double linv=1./L(1,1);
513  for (i=bw;i>=2;i--)
514  {
515  //L(i,1)=M(i,1)/L(1,1);
516  dfM(i,1)+=dfL(i,1)*linv;
517  dfL(1,1)-=dfL(i,1)*M(i,1)*linv*linv;
518  dfL(i,1)=0.0;
519  }
520  //L(1,1)=sqrt(M(1,1));
521  dfM(1,1)+=dfL(1,1)/(2.*L(1,1));
522 
523 
524  //*******************************************************************8
525  //*******************************************************************8
526  //*******************************************************************8
527 
528  dfM.save_dmatrix_derivatives(MMpos);
529 }
530 
536 {
538  int imin=S.indexmin();
539  int imax=S.indexmax();
540  int bw=S.bandwidth();
541  allocate(imin,imax,imin,imax);
542  int i1;
543  int j1;
544  initialize();
545  for (int i=imin;i<=imax;i++)
546  {
547  for (int j=imin;j<=imax;j++)
548  {
549  if (j<=i)
550  {
551  j1=j;
552  i1=i;
553  }
554  else
555  {
556  j1=i;
557  i1=j;
558  }
559  if ( (i1-j1) < bw)
560  (*this)(i,j)=S(i1,j1);
561  }
562  }
563 }
564 
570 {
573  int imin=S.indexmin();
574  int imax=S.indexmax();
575  int bw=S.bandwidth();
576  allocate(imin,imax,imin,imax);
577  initialize();
578  for (int i=imin;i<=imax;i++)
579  {
580  for (int j=imin;j<=imax;j++)
581  {
582  if (j<=i)
583  {
584  if ( (i-j) < bw)
585  (*this)(i,j)=S(i,j);
586  }
587  }
588  }
589 }
590 
595 int max(int i,int j,int k)
596 {
597  if (i>j)
598  if ( i>k)
599  return i;
600  else
601  return k;
602  else
603  if ( j>k)
604  return j;
605  else
606  return k;
607 }
608 
614  const dvar_matrix_position& mpos)
615 {
617 
618  // restores the size, address, and value information for a dvar_matrix
620  //int ierr;
621  int min=out.rowmin();
622  int max=out.rowmax();
623  for (int i=max;i>=min;i--)
624  {
626  out(i)=restore_dvar_vector_value(vpos);
627  }
628  return out;
629 }
630 
636  const int& _ierr)
637 {
638  int& ierr=(int&)(_ierr);
639  // kludge to deal with constantness
640  ierr =0;
642  int n=M.indexmax();
643 
644  int bw=M.bandwidth();
646 #ifndef SAFE_INITIALIZE
647  L.initialize();
648 #endif
649 
650  int i,j,k;
651  double tmp;
652  if (M(1,1)<=0)
653  {
654  cerr << "Error matrix not positive definite in choleski_decomp"
655  " value was " << M(1,1) << " for index 1" <<endl;
656  ierr=1;
657  return;
658  //M(1,1)=1.0;
659  }
660  L.elem_value(1,1)=sqrt(value(M(1,1)));
661  for (i=2;i<=bw;i++)
662  {
663  L.elem_value(i,1)=value(M(i,1))/L.elem_value(1,1);
664  }
665 
666  for (i=2;i<=n;i++)
667  {
668  int jmin=admax(2,i-bw+1);
669  for (j=jmin;j<=i-1;j++)
670  {
671  tmp=value(M(i,j));
672  int kmin=max(1,j-bw+1,i-bw+1);
673  for (k=kmin;k<=j-1;k++)
674  {
675  tmp-=L.elem_value(i,k)*L.elem_value(j,k);
676  }
677  L.elem_value(i,j)=tmp/L.elem_value(j,j);
678  }
679  tmp=value(M(i,i));
680  int kmin=admax(i-bw+1,1);
681  for (k=kmin;k<=i-1;k++)
682  {
683  tmp-=L.elem_value(i,k)*L.elem_value(i,k);
684  }
685  if (tmp<=0)
686  {
687  cerr << "Error matrix not positive definite in choleski_decomp"
688  " value was " << tmp << " for index " << i <<endl;
689  ierr=1;
690  return;
691  //tmp=1.0;
692  }
693  L.elem_value(i,i)=sqrt(tmp);
694  }
695 }
696 
702 {
703  return sqrt(norm2(B));
704 }
705 
711 {
712  dvariable nm=0.0;
713  for (int i=1;i<=B.bw-1;i++)
714  {
715  nm+=norm2(B.d(i));
716  }
717  nm*=2;
718  nm+=norm2(B.d(0));
719  return nm;
720 }
void save_dmatrix_derivatives(const dvar_matrix_position &) const
Description not yet available.
Definition: cmpdif11.cpp:118
int indexmax(void) const
Definition: fvar.hpp:8079
Description not yet available.
Definition: fvar.hpp:7981
Description not yet available.
Definition: fvar.hpp:920
Base class for dvariable.
Definition: fvar.hpp:1315
void initialize(void)
Description not yet available.
Definition: fvar_m40.cpp:77
banded_lower_triangular_dmatrix restore_banded_lower_triangular_dvar_matrix_derivatives(const dvar_matrix_position &_pos)
Description not yet available.
Definition: cmpdif11.cpp:170
int admax(int i, int j)
Definition: fvar.hpp:8979
double sumsq(const d3_array &a)
Definition: d3arr2a.cpp:181
Vector of double precision numbers.
Definition: dvector.h:50
Description not yet available.
Definition: fvar.hpp:8190
banded_lower_triangular_dvar_matrix(int _min, int _max, int _bw)
Description not yet available.
Definition: fvar_m40.cpp:184
int indexmin() const
Definition: fvar.hpp:2917
void initialize(void)
Zero initialize allocated dvar_matrix, then saves adjoint function and position data.
Definition: fvar_ma7.cpp:48
int rowmin(void) const
Definition: fvar.hpp:8139
int rowmax(void) const
Definition: fvar.hpp:8143
Description not yet available.
Definition: fvar.hpp:4814
int indexmax(void) const
Definition: fvar.hpp:7999
int bandwidth(void) const
Definition: fvar.hpp:8071
double & elem_value(int i, int j)
Definition: fvar.hpp:8246
void initialize(void)
Description not yet available.
Definition: dmat41.cpp:29
int indexmin(void) const
Definition: fvar.hpp:8075
ADMB variable vector.
Definition: fvar.hpp:2172
void verify_identifier_string(const char *)
Verifies gradient stack string.
Definition: cmpdif3.cpp:149
void fill_seqadd(int, int)
Fills ivector elements with values starting from base and incremented by offset.
Definition: cranfill.cpp:73
double norm(const d3_array &a)
Return computed norm value of a.
Definition: d3arr2a.cpp:190
df1_one_matrix choleski_decomp(const df1_one_matrix &MM)
Definition: df11fun.cpp:606
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
int rowmin(void) const
Definition: fvar.hpp:8083
prescientific setscientific(void)
Description not yet available.
Definition: admanip.cpp:80
prnstream & endl(prnstream &)
dmatrix eigenvectors(const banded_symmetric_dmatrix &_SS, const dvector &_e)
Description not yet available.
Definition: dmat28.cpp:442
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
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 initialize(void)
Description not yet available.
Definition: dmat41.cpp:17
dvar_vector_position restore_dvar_vector_position()
Definition: cmpdif4.cpp:69
#define min(a, b)
Definition: cbivnorm.cpp:188
Description not yet available.
Definition: fvar.hpp:3398
int rowmax(void) const
Definition: fvar.hpp:8087
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
void allocate(void)
Does not allocate, but initializes members.
Definition: fvar_mat.cpp:479
void dfcholeski_decomp_banded(void)
Description not yet available.
Definition: fvar_m40.cpp:392
Description not yet available.
Definition: fvar.hpp:8120
#define M
Definition: rngen.cpp:57
banded_symmetric_dvar_matrix(int _min, int _max, int _bw)
Description not yet available.
Definition: fvar_m40.cpp:102
Description not yet available.
Definition: fvar.hpp:2819
void initialize(void)
Initialze all elements of dvector to zero.
Definition: dvect5.cpp:10
double norm2(const d3_array &a)
Return sum of squared elements in a.
Definition: d3arr2a.cpp:167
int save_identifier_string(const char *)
Writes a gradient stack verification string.
Definition: cmpdif2.cpp:315
dvector restore_dvar_vector_value(const dvar_vector_position &tmp)
Restores the size, address, and value information for a dvar_vector.
Definition: cmpdif4.cpp:227
int indexmax() const
Definition: fvar.hpp:2921
dvar_matrix_position restore_dvar_matrix_position()
Definition: cmpdif6.cpp:114
static _THREAD DF_FILE * fp
void save_dvar_matrix_position(const dvar_matrix &m)
Definition: cmpdif5.cpp:345
dvar_matrix()
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: fvar_mat.cpp:15
ostream & operator<<(const ostream &_s, preshowpoint p)
Description not yet available.
Definition: admanip.cpp:48
dmatrix restore_lower_triangular_dvar_matrix_value(const dvar_matrix_position &mpos)
Description not yet available.
Definition: fvar_m40.cpp:613
#define w
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 save_dvar_matrix_value(const dvar_matrix &m)
Definition: cmpdif4.cpp:252
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
static _THREAD grad_stack * GRAD_STACK1
#define max(a, b)
Definition: cbivnorm.cpp:189
void check_choleski_decomp(const banded_symmetric_dvar_matrix &MM, int &ierr)
prevariable operator()(int i, int j)
Definition: fvar.hpp:8224
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
banded_symmetric_dmatrix restore_banded_symmetric_dvar_matrix_value(const dvar_matrix_position &mpos)
Description not yet available.
Definition: cmpdif11.cpp:55
Description not yet available.
Definition: fvar.hpp:8065
int bandwidth(void) const
Definition: fvar.hpp:7991
void initialize(void)
Description not yet available.
Definition: fvar_m40.cpp:89
prevariable operator()(int i, int j)
Definition: fvar.hpp:8098
Description not yet available.
Definition: fvar.hpp:4843