ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fvar_m50.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 
15 
21  const banded_symmetric_dvar_matrix& MM, double eps,
22  dvariable& _fpen)
23 {
24  // kludge to deal with constantness
26  int n=M.indexmax();
27 
28  int bw=M.bandwidth();
30 #ifndef SAFE_INITIALIZE
31  L.initialize();
32 #endif
33 
34  int i,j,k;
35  double tmp;
36  double ptmp;
37  double fpen=0.0;
38  ptmp=posfun(value(M(1,1)),eps,fpen);
39  L.elem_value(1,1)=sqrt(ptmp);
40  for (i=2;i<=bw;i++)
41  {
42  L.elem_value(i,1)=value(M(i,1))/L.elem_value(1,1);
43  }
44 
45  for (i=2;i<=n;i++)
46  {
47  int jmin=admax(2,i-bw+1);
48  for (j=jmin;j<=i-1;j++)
49  {
50  tmp=value(M(i,j));
51  int kmin=max(1,j-bw+1,i-bw+1);
52  for (k=kmin;k<=j-1;k++)
53  {
54  tmp-=L.elem_value(i,k)*L.elem_value(j,k);
55  }
56  L.elem_value(i,j)=tmp/L.elem_value(j,j);
57  }
58  tmp=value(M(i,i));
59  int kmin=admax(i-bw+1,1);
60  for (k=kmin;k<=i-1;k++)
61  {
62  tmp-=L.elem_value(i,k)*L.elem_value(i,k);
63  }
64  ptmp=posfun(tmp,eps,fpen);
65  L.elem_value(i,i)=sqrt(ptmp);
66  }
67 
68 
69  if (fpen>0)
70  cout << "fpen = " << fpen << endl;
71 
72  value(_fpen)=fpen;
73  //banded_lower_triangular_dvar_matrix vc=nograd_assign(L);
77  fp->save_prevariable_position(_fpen);
78  fp->save_double_value(eps);
82  fp->save_dvar_matrix_value(MM.d);
87 
88  return L;
89 }
90 
96 {
98 
109  double eps=fp->restore_double_value();
112  double dfpen=restore_prevariable_derivative(fpenpos);
113 
114  int n=M.indexmax();
115  int bw=M.bandwidth();
116 
117  double fpen=0.0;
119  banded_lower_triangular_dmatrix tmp1(1,n,bw);
120  banded_lower_triangular_dmatrix dftmp1(1,n,bw);
121  banded_symmetric_dmatrix dfM(1,n,bw);
122  dvector tmp(1,n);
123  dvector ptmp(1,n);
124  dvector dftmp(1,n);
125  dvector dfptmp(1,n);
126  tmp.initialize();
127  ptmp.initialize();
128  tmp1.initialize();
129  dftmp.initialize();
130  dfptmp.initialize();
131  dftmp1.initialize();
132  dfM.initialize();
133 #ifndef SAFE_INITIALIZE
134  L.initialize();
135 #endif
136 
137  int i,j,k;
138  ptmp(1)=posfun(M(1,1),eps,fpen);
139  L(1,1)=sqrt(ptmp(1));
140  L(1,1)=sqrt(M(1,1));
141  for (i=2;i<=bw;i++)
142  {
143  L(i,1)=M(i,1)/L(1,1);
144  }
145 
146  for (i=2;i<=n;i++)
147  {
148  int jmin=admax(2,i-bw+1);
149  for (j=jmin;j<=i-1;j++)
150  {
151  tmp1(i,j)=M(i,j);
152  int kmin=max(1,j-bw+1,i-bw+1);
153  for (k=kmin;k<=j-1;k++)
154  {
155  tmp1(i,j)-=L(i,k)*L(j,k);
156  }
157  L(i,j)=tmp1(i,j)/L(j,j);
158  }
159  tmp(i)=M(i,i);
160  int kmin=admax(i-bw+1,1);
161  for (k=kmin;k<=i-1;k++)
162  {
163  tmp(i)-=L(i,k)*L(i,k);
164  }
165  double pen;
166  ptmp(i)=posfun(tmp(i),eps,pen);
167  L(i,i)=sqrt(ptmp(i));
168  }
169 
170  dfptmp.initialize();
171 
172  for (i=n;i>=2;i--)
173  {
174  //L(i,i)=sqrt(ptmp(i));
175  dfptmp(i)+=dfL(i,i)/(2.0*L(i,i));
176  dfL(i,i)=0.0;
177  // ptmp(i)=posfun(tmp(i),eps);
178  dftmp(i)=dfptmp(i)*dfposfun(tmp(i),eps);
179  dftmp(i)+=dfpen*dfposfun1(tmp(i),eps);
180  dfptmp(i)=0.0;
181 
182  int kmin=admax(i-bw+1,1);
183  for (k=i-1;k>=kmin;k--)
184  {
185  //tmp(i)-=L(i,k)*L(i,k);
186  dfL(i,k)-=2.*dftmp(i)*L(i,k);
187  }
188  //tmp(i)=M(i,i);
189  dfM(i,i)+=dftmp(i);
190  dftmp(i)=0.0;
191  int jmin=admax(2,i-bw+1);
192  for (j=i-1;j>=jmin;j--)
193  {
194  //L(i,j)=tmp1(i,j)/L(j,j);
195  double linv=1./L(j,j);
196  dftmp1(i,j)+=dfL(i,j)*linv;
197  dfL(j,j)-=dfL(i,j)*tmp1(i,j)*linv*linv;
198  dfL(i,j)=0.0;
199  kmin=max(1,j-bw+1,i-bw+1);
200  for (k=j-1;k>=kmin;k--)
201  {
202  //tmp(i,j)-=L(i,k)*L(j,k);
203  dfL(i,k)-=dftmp1(i,j)*L(j,k);
204  dfL(j,k)-=dftmp1(i,j)*L(i,k);
205  }
206  //tmp(i,j)=M(i,j);
207  dfM(i,j)+=dftmp1(i,j);
208  dftmp1(i,j)=0.0;
209  }
210  }
211  double linv=1./L(1,1);
212  for (i=bw;i>=2;i--)
213  {
214  //L(i,1)=M(i,1)/L(1,1);
215  dfM(i,1)+=dfL(i,1)*linv;
216  dfL(1,1)-=dfL(i,1)*M(i,1)*linv*linv;
217  dfL(i,1)=0.0;
218  }
219  //L(1,1)=sqrt(M(1,1));
220  //dfM(1,1)+=dfL(1,1)/(2.*L(1,1));
221 
222  //L(1,1)=sqrt(ptmp(1));
223  dfptmp(1)+=dfL(1,1)/(2.*L(1,1));
224  dfL(1,1)=0.0;
225  //ptmp(1)=posfun(M(1,1),eps,fpen);
226  dfM(1,1)+=dfptmp(1)*dfposfun(M(1,1),eps);
227  dfM(1,1)+=dfpen*dfposfun1(M(1,1),eps);;
228  dfptmp(1)=0.0;
229 
230  dfM.save_dmatrix_derivatives(MMpos);
231 }
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
double restore_prevariable_derivative(const prevariable_position &_pos)
Description not yet available.
Definition: cmpdif8.cpp:131
Description not yet available.
Definition: fvar.hpp:7981
Description not yet available.
Definition: fvar.hpp:920
banded_lower_triangular_dmatrix restore_banded_lower_triangular_dvar_matrix_derivatives(const dvar_matrix_position &_pos)
Description not yet available.
Definition: cmpdif11.cpp:170
Description not yet available.
Definition: fvar.hpp:4440
int admax(int i, int j)
Definition: fvar.hpp:8979
double dfposfun(const double &x, const double eps)
Adjoint code for posfun; possibly not used.
Definition: posfunc.cpp:19
void dfcholeski_decomp_banded_positive(void)
Description not yet available.
Definition: fvar_m50.cpp:95
Vector of double precision numbers.
Definition: dvector.h:50
Description not yet available.
Definition: fvar.hpp:8190
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
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
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
prnstream & endl(prnstream &)
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
void initialize(void)
Description not yet available.
Definition: dmat41.cpp:17
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Description not yet available.
Definition: fvar.hpp:8120
double restore_double_value()
Definition: cmpdif8.cpp:186
#define M
Definition: rngen.cpp:57
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
double eps
Definition: ftweak.cpp:13
Stores the adjoint gradient data that will be processed by gradcalc.
void save_dvar_matrix_value(const dvar_matrix &m)
Definition: cmpdif4.cpp:252
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
#define max(a, b)
Definition: cbivnorm.cpp:189
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
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
Description not yet available.
Definition: fvar.hpp:4843