ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fvar_m20.cpp
Go to the documentation of this file.
1 
7 #include <fvar.hpp>
8 #ifndef OPT_LIB
9  #include <cassert>
10  #include <climits>
11 #endif
12 #ifdef __TURBOC__
13  #pragma hdrstop
14  #include <iomanip.h>
15 #endif
16 
17 #ifdef __ZTC__
18  #include <iomanip.hpp>
19 #endif
20 
21 #define TINY 1.0e-20;
22 void dfinvpret(void);
23 
24 // int min(int a,int b);
25 void df_xdet(void);
26 
38 {
39  int i,j,k;
40 #if !defined(OPT_LIB) && (__cplusplus >= 201103L)
41  int n = [](unsigned int colsize) -> int
42  {
43  assert(colsize <= INT_MAX);
44  return static_cast<int>(colsize);
45  } (aa.colsize());
46 #else
47  int n = static_cast<int>(aa.colsize());
48 #endif
49  int lb=aa.colmin();
50  int ub=aa.colmax();
51  ivector indx(lb,ub);
52  if (lb!=aa.rowmin()||ub!=aa.colmax())
53  {
54  cerr << "Error matrix not square in det()"<<endl;
55  ad_exit(1);
56  }
57  int One=1;
58  indx.fill_seqadd(lb,One);
59  double d;
60  double big,dum,sum,temp;
61  dvar_matrix_position dmp(aa,1);
62  dmatrix bb=value(aa);
63  dvector vv(lb,ub);
64  dvector part_prod(lb,ub);
65 
66  d=1.0;
67  for (i=lb;i<=ub;i++)
68  {
69  big=0.0;
70  for (j=lb;j<=ub;j++)
71  {
72  temp=fabs(bb.elem(i,j));
73  if (temp > big)
74  {
75  big=temp;
76  }
77  }
78  if (big == 0.0)
79  {
80  cerr << "Error in matrix inverse -- matrix singular in inv(dmatrix)\n";
81  }
82  vv[i]=1.0/big;
83  }
84 
85  for (j=lb;j<=ub;j++)
86  {
87  for (i=lb;i<j;i++)
88  {
89  sum=bb.elem(i,j);
90  for (k=lb;k<i;k++)
91  {
92  sum = sum - bb.elem(i,k)*bb.elem(k,j);
93  }
94  //a[i][j]=sum;
95  bb(i,j)=sum;
96  }
97  int imax = j;
98  big=0.0;
99  for (i=j;i<=ub;i++)
100  {
101  sum=bb.elem(i,j);
102  for (k=lb;k<j;k++)
103  {
104  sum = sum - bb(i,k)*bb(k,j);
105  }
106  bb(i,j)=sum;
107  dum=vv.elem(i)*fabs(sum);
108  if ( dum >= big)
109  {
110  big=dum;
111  imax=i;
112  }
113  }
114  if (j != imax)
115  {
116  for (k=lb;k<=ub;k++)
117  {
118  dum=bb.elem(imax,k);
119  bb.elem(imax,k)=bb.elem(j,k);
120  bb.elem(j,k)=dum;
121  }
122  d = -1.*d;
123  vv.elem(imax)=vv.elem(j);
124 
125  //if (j<ub)
126  {
127  int itemp=indx.elem(imax);
128  indx.elem(imax)=indx.elem(j);
129  indx.elem(j)=itemp;
130  }
131  //cout << "indx= " <<indx<<endl;
132  }
133 
134  if (bb.elem(j,j) == 0.0)
135  {
136  bb(j,j)=TINY;
137  }
138 
139  if (j != n)
140  {
141  dum=1.0/bb(j,j);
142  for (i=j+1;i<=ub;i++)
143  {
144  bb.elem(i,j) *= dum;
145  }
146  }
147  }
148  //double det=d;
149  // SM Bug 129, issue appears to be at this line
150  // part_prod is declared above as dvector(lb,ub)
151  // cout<<"Bug 129 ="<<part_prod(lb)<<endl;
152  // part_prod(1)=d*bb(1,1); // replaced this line with:
153  part_prod(lb) = d*bb(lb,lb);
154  // cout<<"Ok got this far; det = "<<det<<endl;
155  for (j=lb+1;j<=ub;j++)
156  {
157  part_prod(j)=part_prod(j-1)*bb(j,j);
158  }
159  double det=part_prod(ub);
160  dvariable rdet=nograd_assign(det);
163  save_identifier_string("PLACE7");
164  fp->save_dvector_value(part_prod);
165  fp->save_dvector_position(part_prod);
166  fp->save_ivector_value(indx);
167  fp->save_ivector_position(indx);
168  save_identifier_string("PLACE3");
170  save_identifier_string("PLACE2b");
171  fp->save_prevariable_position(rdet);
172  save_identifier_string("PLACE2a");
173  fp->save_dmatrix_value(bb);
174  save_identifier_string("PLACE2");
175  fp->save_dmatrix_position(bb);
176  save_identifier_string("PLACE1");
177  fp->save_double_value(d);
178  save_identifier_string("PLACE0");
180  return rdet;
181 }
182 
184 void df_xdet(void)
185 {
187 
188  verify_identifier_string("PLACE0");
189  double d=fp->restore_double_value();
190  verify_identifier_string("PLACE1");
192  verify_identifier_string("PLACE2");
193  dmatrix b=fp->restore_dmatrix_value(bpos);
194  verify_identifier_string("PLACE2a");
195  //dvar_matrix_position rdet_pos=restore_prevariable_position();
196  double dfdet=fp->restore_prevariable_derivative();
197  verify_identifier_string("PLACE2b");
199  verify_identifier_string("PLACE3");
201  ivector indx=restore_ivector_value(indx_pos);
202  dvector_position part_prod_pos=fp->restore_dvector_position();
203  dvector part_prod=fp->restore_dvector_value(part_prod_pos);
204  verify_identifier_string("PLACE7");
205  int lb=b.colmin();
206  int ub=b.colmax();
207  dmatrix dfb(lb,ub,lb,ub);
208 
209  dvector dfpart_prod(lb,ub);
210 
211  #ifndef SAFE_INITIALIZE
212  dfb.initialize();
213  dfpart_prod.initialize();
214  #endif
215 
216 
217  dfpart_prod(ub)=dfdet;
218  int j;
219  for (j=ub;j>=lb+1;j--)
220  {
221  // part_prod(j)=part_prod(j-1)*b(j,j);
222  dfpart_prod(j-1)+=dfpart_prod(j)*b(j,j);
223  dfb(j,j)+=dfpart_prod(j)*part_prod(j-1);
224  dfpart_prod(j)=0.;
225  }
226  //part_prod(1)=d*b(lb,lb);
227  dfb(lb,lb)+=dfpart_prod(lb)*d;
228  dfpart_prod(lb)=0.;
229 
230  double dfsum=0.;
231  for (j=ub;j>=lb;j--)
232  {
233  for (int i=ub;i>=lb;i--)
234  {
235  if (i<=j)
236  {
237  // b(i,j)=sum;
238  dfsum+=dfb(i,j);
239  dfb(i,j)=0.;
240  }
241  else
242  {
243  // b(i,j)=sum/b(j,j);
244  dfsum+=dfb(i,j)/b(j,j);
245  dfb(j,j)-=dfb(i,j)*b(i,j)/b(j,j);
246  dfb(i,j)=0.;
247  }
248 
249  for (int k=min(i-1,j-1);k>=lb;k--)
250  {
251  // sum-=b(i,k)*b(k,j);
252  dfb(i,k)-=dfsum*b(k,j);
253  dfb(k,j)-=dfsum*b(i,k);
254  }
255  // sum=value(a(indx(i),j);
256  save_dmatrix_derivatives(a_pos,dfsum,indx(i),j); // like this
257  dfsum=0.;
258  }
259  }
260 }
261 
262 #undef TINY
Description not yet available.
Definition: fvar.hpp:4883
int & elem(int i)
Definition: ivector.h:90
double & elem(int i)
Definition: dvector.h:152
double restore_prevariable_derivative()
Definition: cmpdif8.cpp:147
void save_ivector_position(const ivector &v)
Definition: cmpdif3.cpp:198
void dfinvpret(void)
Adjoint code for dvar_matrix inv(const dvar_matrix&amp; aa).
Definition: fvar_m15.cpp:287
int colmin(void) const
Definition: fvar.hpp:2552
Vector of double precision numbers.
Definition: dvector.h:50
ivector_position restore_ivector_position()
Definition: cmpdif4.cpp:49
void save_dmatrix_position(const dmatrix &m)
Definition: cmpdif6.cpp:31
dvar_vector nograd_assign(dvector tmp)
Description not yet available.
Definition: cmpdif6.cpp:252
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr.cpp:21
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
exitptr ad_exit
Definition: gradstrc.cpp:53
dmatrix_position restore_dmatrix_position()
Definition: cmpdif6.cpp:147
Description not yet available.
Definition: fvar.hpp:4923
void save_dmatrix_value(const dmatrix &m)
Definition: cmpdif5.cpp:26
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
dvector restore_dvector_value(const dvector_position &tmp)
Definition: cmpdif4.cpp:178
dmatrix restore_dmatrix_value(const dmatrix_position &mpos)
Definition: cmpdif5.cpp:100
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
double det(const dmatrix &m1)
Compute determinant of a constant matrix.
Definition: dmat3.cpp:499
void save_dvector_position(const dvector &v)
Definition: cmpdif4.cpp:32
prnstream & endl(prnstream &)
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
void save_prevariable_position(const prevariable &v)
Definition: cmpdif8.cpp:60
void save_dvector_value(const dvector &v)
Definition: cmpdif4.cpp:130
#define min(a, b)
Definition: cbivnorm.cpp:188
Description not yet available.
Definition: fvar.hpp:4947
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
void df_xdet(void)
Adjoint code for dvariable det(const dvar_matrix&amp; aa)
Definition: fvar_m20.cpp:184
double restore_double_value()
Definition: cmpdif8.cpp:186
static _THREAD gradient_structure * _instance
int colmin(void) const
Definition: fvar.hpp:2939
int rowmin(void) const
Definition: fvar.hpp:2560
Description not yet available.
Definition: fvar.hpp:2819
void initialize(void)
Initialze all elements of dvector to zero.
Definition: dvect5.cpp:10
dvector & elem(int i)
Definition: fvar.hpp:3011
int save_identifier_string(const char *)
Writes a gradient stack verification string.
Definition: cmpdif2.cpp:315
dvector_position restore_dvector_position()
Definition: cmpdif4.cpp:88
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
ivector restore_ivector_value(const ivector_position &tmp)
Description not yet available.
Definition: cmpdif4.cpp:198
Class definition of matrix with derivitive information .
Definition: fvar.hpp:2480
Stores the adjoint gradient data that will be processed by gradcalc.
#define TINY
Definition: fvar_m20.cpp:21
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
class for things related to the gradient structures, including dimension of arrays, size of buffers, etc.
void save_dmatrix_derivatives(const dvar_matrix_position &_pos, const double x, const int &i, int &j)
Description not yet available.
Definition: cmpdif5.cpp:265
void initialize(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat7.cpp:12
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
void save_ivector_value(const ivector &v)
Definition: cmpdif4.cpp:152
int colmax(void) const
Definition: fvar.hpp:2943
int colmax(void) const
Definition: fvar.hpp:2556
Description not yet available.
Definition: fvar.hpp:4843
unsigned int colsize() const
Definition: fvar.hpp:2584