ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dmat34.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 
13 #ifdef __TURBOC__
14  #pragma hdrstop
15  #include <iostream.h>
16 #endif
17 
18 #if defined (__WAT32__)
19  #include <iostream.h>
20  #include <strstrea.h>
21 #endif
22 
23 #ifdef __ZTC__
24  #include <iostream.hpp>
25 #endif
26 
27 #define TINY 1.0e-20;
28 void dmdv_solve(void);
29 
30 
32 dvector csolve(const dmatrix& aa,const dvector& z)
33 {
34  double ln_unsigned_det;
35  double sign;
36  dvector sol=solve(aa,z,ln_unsigned_det,sign);
37  return sol;
38 }
39 
46 dvector solve(const dmatrix& aa,const dvector& z)
47 {
48  double ln_unsigned_det;
49  double sign;
50  dvector sol=solve(aa,z,ln_unsigned_det,sign);
51  return sol;
52 }
53 
66 dvector solve(const dmatrix& aa,const dvector& z,
67  const double& _ln_unsigned_det,double& sign)
68 {
69  double& ln_unsigned_det=(double&) (_ln_unsigned_det);
70 #if !defined(OPT_LIB) && (__cplusplus >= 201103L)
71  int n = [](unsigned int colsize) -> int
72  {
73  assert(colsize <= INT_MAX);
74  return static_cast<int>(colsize);
75  } (aa.colsize());
76 #else
77  int n = static_cast<int>(aa.colsize());
78 #endif
79  int lb=aa.colmin();
80  int ub=aa.colmax();
81  if (lb!=aa.rowmin()||ub!=aa.rowmax())
82  {
83  cerr << "Error matrix not square in solve()"<<endl;
84  ad_exit(1);
85  }
86  dmatrix bb(lb,ub,lb,ub);
87  bb=aa;
88  ivector indx(lb,ub);
89  int One=1;
90  indx.fill_seqadd(lb,One);
91  double d;
92  double big,dum,sum,temp;
93  dvector vv(lb,ub);
94 
95  d=1.0;
96  dvector* pbbi = &bb(lb);
97  double* pvvi = &vv(lb);
98  for (int i=lb;i<=ub;i++)
99  {
100  big=0.0;
101  double* pbbij = pbbi->get_v() + lb;
102  for (int j=lb;j<=ub;j++)
103  {
104  temp=fabs(*pbbij);
105  if (temp > big)
106  {
107  big=temp;
108  }
109  ++pbbij;
110  }
111  if (big == 0.0)
112  {
113  cerr << "Error in matrix inverse -- matrix singular in inv(dmatrix)\n";
114  }
115  *pvvi = 1.0 / big;
116 
117  ++pvvi;
118  ++pbbi;
119  }
120 
121  dvector* pbbj = &bb(lb);
122  double* pvvj = &vv(lb);
123  for (int j=lb;j<=ub;j++)
124  {
125  dvector* pbbi = &bb(lb);
126  for (int i=lb;i<j;i++)
127  {
128  sum = *(pbbi->get_v() + j);
129  double* pbbik = pbbi->get_v() + lb;
130  dvector* pbbk = &bb(lb);
131  for (int k=lb;k<i;k++)
132  {
133  sum -= *pbbik * *(pbbk->get_v() + j);
134 
135  ++pbbik;
136  ++pbbk;
137  }
138  //a[i][j]=sum;
139  *(pbbi->get_v() + j) = sum;
140 
141  ++pbbi;
142  }
143  int imax = j;
144  big=0.0;
145 
146  pbbi = &bb(j);
147  pvvi = &vv(j);
148  for (int i=j;i<=ub;i++)
149  {
150  sum = *(pbbi->get_v() + j);
151  double* pbbik = pbbi->get_v() + lb;
152  dvector* pbbk = &bb(lb);
153  for (int k=lb;k<j;k++)
154  {
155  sum -= *pbbik * *(pbbk->get_v() + j);
156 
157  ++pbbik;
158  ++pbbk;
159  }
160  *(pbbi->get_v() + j) = sum;
161  dum = *pvvi * fabs(sum);
162  if (dum >= big)
163  {
164  big = dum;
165  imax = i;
166  }
167  ++pbbi;
168  ++pvvi;
169  }
170  if (j != imax)
171  {
172  double* pbbjk = pbbj->get_v() + lb;
173  double* pbbimaxk = bb(imax).get_v() + lb;
174  for (int k=lb;k<=ub;k++)
175  {
176  dum = *pbbimaxk;
177  *pbbimaxk = *pbbjk;
178  *pbbjk = dum;
179 
180  ++pbbjk;
181  ++pbbimaxk;
182  }
183  d = -1.*d;
184  vv[imax] = *pvvj;
185 
186  //if (j<ub)
187  {
188  int itemp=indx(imax);
189  indx(imax)=indx(j);
190  indx(j)=itemp;
191  }
192  //cout << "indx= " <<indx<<endl;
193  }
194 
195  double* pbbjj = pbbj->get_v() + j;
196  if (*pbbjj == 0.0)
197  {
198  *pbbjj = TINY;
199  }
200 
201  if (j != n)
202  {
203  dum = 1.0 / *pbbjj;
204  pbbi = &bb(j + 1);
205  for (int i=j+1;i<=ub;i++)
206  {
207  *(pbbi->get_v() + j) = *(pbbi->get_v() + j) * dum;
208 
209  ++pbbi;
210  }
211  }
212  ++pbbj;
213  ++pvvj;
214  }
215 
216  // get the determinant
217  sign=d;
218  dvector part_prod(lb,ub);
219  part_prod(lb)=log(fabs(bb(lb,lb)));
220  if (bb(lb,lb)<0) sign=-sign;
221 
222  for (int j=lb+1;j<=ub;j++)
223  {
224  double bbjj = bb(j, j);
225  if (bbjj < 0) sign=-sign;
226  part_prod(j)=part_prod(j-1)+log(fabs(bbjj));
227  }
228  ln_unsigned_det=part_prod(ub);
229 
230  dvector x(lb,ub);
231  dvector y(lb,ub);
232  //int lb=rowmin;
233  //int ub=rowmax;
234  //dmatrix& b=bb;
235  ivector indxinv(lb,ub);
236 
237  int* pindxi = indx.get_v() + lb;
238  for (int i=lb;i<=ub;i++)
239  {
240  indxinv(*pindxi) = i;
241  ++pindxi;
242  }
243 
244  int* pindxinvi = indxinv.get_v() + lb;
245  for (int i=lb;i<=ub;i++)
246  {
247  y(*pindxinvi)=z(i);
248  ++pindxinvi;
249  }
250 
251  pbbi = &bb(lb);
252  double* pyi = &y(lb);
253  for (int i=lb;i<=ub;i++)
254  {
255  sum = *pyi;
256 
257  double* pbbij = pbbi->get_v() + lb;
258  double* pyj = &y(lb);
259  for (int j=lb;j<=i-1;j++)
260  {
261  sum -= *pbbij * *pyj;
262 
263  ++pyj;
264  ++pbbij;
265  }
266  *pyi = sum;
267 
268  ++pyi;
269  ++pbbi;
270  }
271  pbbi = &bb(ub);
272  pyi = &y(ub);
273  double* pxi = x.get_v() + ub;
274  for (int i=ub;i>=lb;i--)
275  {
276  sum = *pyi;
277 
278  double* pbbij = pbbi->get_v() + i + 1;
279  double* pxj = x.get_v() + i + 1;
280  for (int j=i+1;j<=ub;j++)
281  {
282  sum -= *pbbij * *pxj;
283 
284  ++pbbij;
285  ++pxj;
286  }
287  *pxi = sum / *(pbbi->get_v() + i);
288 
289  --pbbi;
290  --pyi;
291  --pxi;
292  }
293 
294  return x;
295 }
296 #undef TINY
#define TINY
Definition: dmat34.cpp:27
void dmdv_solve(void)
Adjoint code for dvar_vector solve(const dvar_matrix&amp; aa, const dvar_vector&amp; z,.
Definition: fvar_m24.cpp:339
#define x
Vector of double precision numbers.
Definition: dvector.h:50
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
void fill_seqadd(int, int)
Fills ivector elements with values starting from base and incremented by offset.
Definition: cranfill.cpp:73
unsigned int colsize() const
Definition: fvar.hpp:2948
dvector solve(const dmatrix &aa, const dvector &z)
Solve a linear system using LU decomposition.
Definition: dmat34.cpp:46
int * get_v() const
Definition: ivector.h:114
prnstream & endl(prnstream &)
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
int rowmax() const
Definition: fvar.hpp:2929
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
int colmin(void) const
Definition: fvar.hpp:2939
Description not yet available.
Definition: fvar.hpp:2819
dvector csolve(const dmatrix &aa, const dvector &z)
Solve a linear system using LU decomposition.
Definition: dmat34.cpp:32
double sign(const double x)
The sign of a number.
Definition: gaussher.cpp:25
double *& get_v(void)
Definition: dvector.h:148
int rowmin() const
Definition: fvar.hpp:2925
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13
int colmax(void) const
Definition: fvar.hpp:2943