ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fvar_m47.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
30  dvariable& _fpen);
31 
37  dvariable& _fpen)
38 {
39  dvar_matrix ch_m=choleski_decomp_positive(MM,eps,_fpen);
40  return ch_m*trans(ch_m);
41 }
42 
48  dvariable& _fpen)
49 {
50  // kludge to deal with constantness
51  dmatrix M=value(MM);
52  double fpen;
53  if (M.colsize() != M.rowsize())
54  {
55  cerr << "Error in chol_decomp. Matrix not square" << endl;
56  ad_exit(1);
57  }
58  int rowsave=M.rowmin();
59  int colsave=M.colmin();
60  M.rowshift(1);
61  M.colshift(1);
62  int n=M.rowmax();
63 
64  dmatrix L(1,n,1,n);
65  //dmatrix C(1,n,1,n);
66  //imatrix B(1,n,1,n);
67  //B.initialize();
68 #ifndef SAFE_INITIALIZE
69  L.initialize();
70 #endif
71 
72  int i,j,k;
73  double tmp;
74  double ptmp;
75  fpen=0.0;
76  ptmp=posfun(M(1,1),eps,fpen);
77  L(1,1)=sqrt(ptmp);
78  for (i=2;i<=n;i++)
79  {
80  L(i,1)=M(i,1)/L(1,1);
81  }
82 
83  for (i=2;i<=n;i++)
84  {
85  for (j=2;j<=i-1;j++)
86  {
87  tmp=M(i,j);
88  for (k=1;k<=j-1;k++)
89  {
90  tmp-=L(i,k)*L(j,k);
91  }
92  L(i,j)=tmp/L(j,j);
93  }
94  tmp=M(i,i);
95  for (k=1;k<=i-1;k++)
96  {
97  tmp-=L(i,k)*L(i,k);
98  }
99  ptmp=posfun(tmp,eps,fpen);
100  L(i,i)=sqrt(ptmp);
101  }
102  L.rowshift(rowsave);
103  L.colshift(colsave);
104 
105  value(_fpen)=fpen;
107 
111  fp->save_prevariable_position(_fpen);
112  fp->save_double_value(eps);
114  fp->save_dvar_matrix_value(MM);
119 
120  return vc;
121 }
122 
128 {
130 
136  double eps=fp->restore_double_value();
140  double dfpen=restore_prevariable_derivative(fpenpos);
141 
142  if (M.colsize() != M.rowsize())
143  {
144  cerr << "Error in chol_decomp. Matrix not square" << endl;
145  ad_exit(1);
146  }
147  int rowsave=M.rowmin();
148  int colsave=M.colmin();
149  dfL.rowshift(1);
150  dfL.colshift(1);
151  M.rowshift(1);
152  M.colshift(1);
153  int n=M.rowmax();
154 
155  dmatrix L(1,n,1,n);
156  dvector tmp(1,n);
157  dvector ptmp(1,n);
158  dmatrix tmp1(1,n,1,n);
159  dmatrix dftmp1(1,n,1,n);
160  dmatrix dfM(1,n,1,n);
161  dvector dfptmp(1,n);
162  dvector dftmp(1,n);
163  tmp.initialize();
164  tmp1.initialize();
165  dftmp.initialize();
166  dftmp1.initialize();
167  dfM.initialize();
168 #ifndef SAFE_INITIALIZE
169  L.initialize();
170 #endif
171 
172  double fpen;
173  int i,j,k;
174  ptmp(1)=posfun(M(1,1),eps,fpen);
175  L(1,1)=sqrt(ptmp(1));
176  for (i=2;i<=n;i++)
177  {
178  L(i,1)=M(i,1)/L(1,1);
179  }
180 
181  for (i=2;i<=n;i++)
182  {
183  for (j=2;j<=i-1;j++)
184  {
185  tmp1(i,j)=M(i,j);
186  for (k=1;k<=j-1;k++)
187  {
188  tmp1(i,j)-=L(i,k)*L(j,k);
189  }
190  L(i,j)=tmp1(i,j)/L(j,j);
191  }
192  tmp(i)=M(i,i);
193  for (k=1;k<=i-1;k++)
194  {
195  tmp(i)-=L(i,k)*L(i,k);
196  }
197  double pen;
198  ptmp(i)=posfun(tmp(i),eps,pen);
199  L(i,i)=sqrt(ptmp(i));
200  }
201 
202  dfptmp.initialize();
203 
204  //*******************************************************************8
205  //*******************************************************************8
206  //*******************************************************************8
207  for (i=n;i>=2;i--)
208  {
209  //L(i,i)=sqrt(ptmp(i));
210  dfptmp(i)+=dfL(i,i)/(2.0*L(i,i));
211  dfL(i,i)=0.0;
212  // ptmp(i)=posfun(tmp(i),eps);
213  dftmp(i)=dfptmp(i)*dfposfun(tmp(i),eps);
214  dftmp(i)+=dfpen*dfposfun1(tmp(i),eps);
215  dfptmp(i)=0.0;
216 
217  for (k=i-1;k>=1;k--)
218  {
219  //tmp(i)-=L(i,k)*L(i,k);
220  dfL(i,k)-=2.*dftmp(i)*L(i,k);
221  }
222  //tmp(i)=M(i,i);
223  dfM(i,i)+=dftmp(i);
224  dftmp(i)=0.0;
225  for (j=i-1;j>=2;j--)
226  {
227  //L(i,j)=tmp1(i,j)/L(j,j);
228  double linv=1./L(j,j);
229  dftmp1(i,j)+=dfL(i,j)*linv;
230  dfL(j,j)-=dfL(i,j)*tmp1(i,j)*linv*linv;
231  dfL(i,j)=0.0;
232  for (k=j-1;k>=1;k--)
233  {
234  //tmp1(i,j)-=L(i,k)*L(j,k);
235  dfL(i,k)-=dftmp1(i,j)*L(j,k);
236  dfL(j,k)-=dftmp1(i,j)*L(i,k);
237  }
238  //tmp1(i,j)=M(i,j);
239  dfM(i,j)+=dftmp1(i,j);
240  dftmp1(i,j)=0.0;
241  }
242  }
243  double linv=1./L(1,1);
244  for (i=n;i>=2;i--)
245  {
246  //L(i,1)=M(i,1)/L(1,1);
247  dfM(i,1)+=dfL(i,1)*linv;
248  dfL(1,1)-=dfL(i,1)*M(i,1)*linv*linv;
249  dfL(i,1)=0.0;
250  }
251  //L(1,1)=sqrt(ptmp(1));
252  dfptmp(1)+=dfL(1,1)/(2.*L(1,1));
253  dfL(1,1)=0.0;
254  //ptmp(1)=posfun(M(1,1),eps,fpen);
255  dfM(1,1)+=dfptmp(1)*dfposfun(M(1,1),eps);
256  dfM(1,1)+=dfpen*dfposfun1(M(1,1),eps);;
257  dfptmp(1)=0.0;
258 
259 
260  //*******************************************************************8
261  //*******************************************************************8
262  //*******************************************************************8
263  dfM.rowshift(rowsave);
264  dfM.colshift(colsave);
265 
266  dfM.save_dmatrix_derivatives(MMpos);
267 }
double restore_prevariable_derivative(const prevariable_position &_pos)
Description not yet available.
Definition: cmpdif8.cpp:131
Description not yet available.
Definition: fvar.hpp:920
Description not yet available.
Definition: fvar.hpp:4440
double dfposfun(const double &x, const double eps)
Adjoint code for posfun; possibly not used.
Definition: posfunc.cpp:19
dvar_matrix positive_definite_matrix(const dvar_matrix &MM, double eps, dvariable &fpen)
Description not yet available.
Definition: fvar_m47.cpp:36
dmatrix trans(const dmatrix &m1)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat2.cpp:13
Vector of double precision numbers.
Definition: dvector.h:50
dmatrix restore_dvar_matrix_derivatives(const dvar_matrix_position &_pos)
Description not yet available.
Definition: cmpdif6.cpp:178
dvar_vector nograd_assign(dvector tmp)
Description not yet available.
Definition: cmpdif6.cpp:252
exitptr ad_exit
Definition: gradstrc.cpp:53
double dfposfun1(const double &x, const double eps)
Adjoint code for posfun; possibly not used.
Definition: posfunc.cpp:39
dvar_vector posfun(const dvar_vector &x, double eps, const prevariable &pen)
Description not yet available.
Definition: fvar_a62.cpp:17
void verify_identifier_string(const char *)
Verifies gradient stack string.
Definition: cmpdif3.cpp:149
unsigned int colsize() const
Definition: fvar.hpp:2948
void dfcholeski_decomp_positive(void)
Description not yet available.
Definition: fvar_m47.cpp:127
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
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
prevariable_position restore_prevariable_position()
Definition: cmpdif8.cpp:43
void save_prevariable_position(const prevariable &v)
Definition: cmpdif8.cpp:60
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
double restore_double_value()
Definition: cmpdif8.cpp:186
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 _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
double eps
Definition: ftweak.cpp:13
Stores the adjoint gradient data that will be processed by gradcalc.
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
void save_double_value(double x)
Definition: cmpdif8.cpp:94
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
dmatrix choleski_decomp_positive(const dmatrix &MM, double bound)
Description not yet available.
Definition: dmat35.cpp:33
Description not yet available.
Definition: fvar.hpp:4843