ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fvar_m39.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 
35 {
36  // kludge to deal with constantness
37  if (MM.colsize() != MM.rowsize())
38  {
39  cerr << "Error in chol_decomp. Matrix not square" << endl;
40  ad_exit(1);
41  }
42  if (MM.colsize()==1)
43  {
44  int mmin=MM.indexmin();
45  int mmax=MM.indexmax();
46  if (MM(mmin,mmin)<=0)
47  {
48  cerr << "Error matrix not positive definite in choleski_decomp"
49  <<endl;
50  ad_exit(1);
51  }
52  dvar_matrix vc(mmin,mmax,mmin,mmax);
53  vc(mmin,mmin)=sqrt(MM(mmin,mmin));
54  return vc;
55  }
56  dmatrix M=value(MM);
57  int rowsave=M.rowmin();
58  int colsave=M.colmin();
59  M.rowshift(1);
60  M.colshift(1);
61  int n=M.rowmax();
62 
63  dmatrix L(1,n,1,n);
64  //dmatrix C(1,n,1,n);
65  //imatrix B(1,n,1,n);
66  //B.initialize();
67 #ifndef SAFE_INITIALIZE
68  L.initialize();
69 #endif
70 
71  int i,j,k;
72  double tmp;
73  if (M(1,1)<=0)
74  {
75  cerr << "Error matrix not positive definite in choleski_decomp"
76  <<endl;
77  ad_exit(1);
78  }
79  L(1,1)=sqrt(M(1,1));
80  for (i=2;i<=n;i++)
81  {
82  /*
83  if (!B(1,1))
84  {
85  C(1,1)=L(1,1);
86  B(1,1)=1;
87  }
88  */
89  L(i,1)=M(i,1)/L(1,1);
90  }
91 
92  for (i=2;i<=n;i++)
93  {
94  for (j=2;j<=i-1;j++)
95  {
96  tmp=M(i,j);
97  for (k=1;k<=j-1;k++)
98  {
99  /*
100  if (!B(i,k))
101  {
102  C(i,k)=L(i,k);
103  B(i,k)=1;
104  }
105  if (!B(j,k))
106  {
107  C(j,k)=L(j,k);
108  B(j,k)=1;
109  }
110  */
111  tmp-=L(i,k)*L(j,k);
112  }
113  /*
114  if (!B(j,j))
115  {
116  C(j,j)=L(j,j);
117  B(j,j)=1;
118  }
119  */
120  L(i,j)=tmp/L(j,j);
121  }
122  tmp=M(i,i);
123  for (k=1;k<=i-1;k++)
124  {
125  /*
126  if (!B(i,k))
127  {
128  C(i,k)=L(i,k);
129  B(i,k)=1;
130  }
131  */
132  tmp-=L(i,k)*L(i,k);
133  }
134  if (tmp<=0)
135  {
136  cerr << "Error matrix not positive definite in choleski_decomp"
137  <<endl;
138  ad_exit(1);
139  }
140  L(i,i)=sqrt(tmp);
141  }
142  //C(n,n)=L(n,n);
143  //B(n,n)=1;
144  L.rowshift(rowsave);
145  L.colshift(colsave);
146  //cout << norm2(C-L) << endl;
147  //cout << B << endl;
148  //cout << L << endl;
149 
151 
157  fp->save_dvar_matrix_value(MM);
162 
163  return vc;
164 }
165 
171 {
173 
182 
183  if (M.colsize() != M.rowsize())
184  {
185  cerr << "Error in chol_decomp. Matrix not square" << endl;
186  ad_exit(1);
187  }
188  int rowsave=M.rowmin();
189  int colsave=M.colmin();
190  dfL.rowshift(1);
191  dfL.colshift(1);
192  M.rowshift(1);
193  M.colshift(1);
194  int n=M.rowmax();
195 
196  dmatrix L(1,n,1,n);
197  dvector tmp(1,n);
198  dmatrix tmp1(1,n,1,n);
199  dmatrix dftmp1(1,n,1,n);
200  dmatrix dfM(1,n,1,n);
201  dvector dftmp(1,n);
202  tmp.initialize();
203  tmp1.initialize();
204  dftmp.initialize();
205  dftmp1.initialize();
206  dfM.initialize();
207 #ifndef SAFE_INITIALIZE
208  L.initialize();
209 #endif
210 
211  int i,j,k;
212  if (M(1,1)<=0)
213  {
214  cerr << "Error matrix not positive definite in choleski_decomp"
215  <<endl;
216  ad_exit(1);
217  }
218  L(1,1)=sqrt(M(1,1));
219  for (i=2;i<=n;i++)
220  {
221  L(i,1)=M(i,1)/L(1,1);
222  }
223 
224  for (i=2;i<=n;i++)
225  {
226  for (j=2;j<=i-1;j++)
227  {
228  tmp1(i,j)=M(i,j);
229  for (k=1;k<=j-1;k++)
230  {
231  tmp1(i,j)-=L(i,k)*L(j,k);
232  }
233  L(i,j)=tmp1(i,j)/L(j,j);
234  }
235  tmp(i)=M(i,i);
236  for (k=1;k<=i-1;k++)
237  {
238  tmp(i)-=L(i,k)*L(i,k);
239  }
240  if (tmp(i)<=0)
241  {
242  cerr << "Error matrix not positive definite in choleski_decomp"
243  <<endl;
244  ad_exit(1);
245  }
246  L(i,i)=sqrt(tmp(i));
247  }
248  //*******************************************************************8
249  //*******************************************************************8
250  //*******************************************************************8
251  for (i=n;i>=2;i--)
252  {
253  //L(i,i)=sqrt(tmp(i));
254  dftmp(i)+=dfL(i,i)/(2.0*L(i,i));
255  dfL(i,i)=0.0;
256  for (k=i-1;k>=1;k--)
257  {
258  //tmp(i)-=L(i,k)*L(i,k);
259  dfL(i,k)-=2.*dftmp(i)*L(i,k);
260  }
261  //tmp(i)=M(i,i);
262  dfM(i,i)+=dftmp(i);
263  dftmp(i)=0.0;
264  for (j=i-1;j>=2;j--)
265  {
266  //L(i,j)=tmp1(i,j)/L(j,j);
267  double linv=1./L(j,j);
268  dftmp1(i,j)+=dfL(i,j)*linv;
269  dfL(j,j)-=dfL(i,j)*tmp1(i,j)*linv*linv;
270  dfL(i,j)=0.0;
271  for (k=j-1;k>=1;k--)
272  {
273  //tmp(i,j)-=L(i,k)*L(j,k);
274  dfL(i,k)-=dftmp1(i,j)*L(j,k);
275  dfL(j,k)-=dftmp1(i,j)*L(i,k);
276  }
277  //tmp(i,j)=M(i,j);
278  dfM(i,j)+=dftmp1(i,j);
279  dftmp1(i,j)=0.0;
280  }
281  }
282  double linv=1./L(1,1);
283  for (i=n;i>=2;i--)
284  {
285  //L(i,1)=M(i,1)/L(1,1);
286  dfM(i,1)+=dfL(i,1)*linv;
287  dfL(1,1)-=dfL(i,1)*M(i,1)*linv*linv;
288  dfL(i,1)=0.0;
289  }
290  //L(1,1)=sqrt(M(1,1));
291  dfM(1,1)+=dfL(1,1)/(2.*L(1,1));
292 
293 
294  //*******************************************************************8
295  //*******************************************************************8
296  //*******************************************************************8
297  dfM.rowshift(rowsave);
298  dfM.colshift(colsave);
299 
300  dfM.save_dmatrix_derivatives(MMpos);
301 }
Description not yet available.
Definition: fvar.hpp:920
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
void verify_identifier_string(const char *)
Verifies gradient stack string.
Definition: cmpdif3.cpp:149
unsigned int colsize() const
Definition: fvar.hpp:2948
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
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
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
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
unsigned int rowsize() const
Definition: fvar.hpp:2578
dvar_matrix_position restore_dvar_matrix_position()
Definition: cmpdif6.cpp:114
static _THREAD DF_FILE * fp
int indexmax(void) const
Definition: fvar.hpp:2572
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.
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 indexmin(void) const
Definition: fvar.hpp:2568
int rowmin() const
Definition: fvar.hpp:2925
Description not yet available.
Definition: fvar.hpp:4843
unsigned int colsize() const
Definition: fvar.hpp:2584