ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dmat38.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 
29 dmatrix solve(const dmatrix& aa,const dmatrix& tz,
30  const double& ln_unsigned_det,double& sign);
31 
36 dmatrix solve(const dmatrix& aa, const dmatrix& tz)
37 {
38  double ln = 0;
39  double sgn = 0;
40  return solve(aa,tz,ln,sgn);
41 }
42 
49 dmatrix solve(const dmatrix& aa,const dmatrix& tz,
50  const double& _ln_unsigned_det,double& sign)
51 {
52  double& ln_unsigned_det = (double&)_ln_unsigned_det;
53 #if !defined(OPT_LIB) && (__cplusplus >= 201103L)
54  int n = [](unsigned int colsize) -> int
55  {
56  assert(colsize <= INT_MAX);
57  return static_cast<int>(colsize);
58  } (aa.colsize());
59 #else
60  int n = static_cast<int>(aa.colsize());
61 #endif
62  int lb=aa.colmin();
63  int ub=aa.colmax();
64  if (lb!=aa.rowmin()||ub!=aa.colmax())
65  {
66  cerr << "Error matrix not square in solve()"<<endl;
67  ad_exit(1);
68  }
69  dmatrix bb(lb,ub,lb,ub);
70  bb=aa;
71  ivector indx(lb,ub);
72  int One=1;
73  indx.fill_seqadd(lb,One);
74  dvector vv(lb,ub);
75 
76  double d = 1.0;
77  for (int i=lb;i<=ub;i++)
78  {
79  double big=0.0;
80  for (int j=lb;j<=ub;j++)
81  {
82  double temp=fabs(bb(i,j));
83  if (temp > big)
84  {
85  big=temp;
86  }
87  }
88  if (big == 0.0)
89  {
90  cerr << "Error in matrix inverse -- matrix singular in inv(dmatrix)\n";
91  }
92  vv[i]=1.0/big;
93  }
94 
95  for (int j=lb;j<=ub;j++)
96  {
97  for (int i=lb;i<j;i++)
98  {
99  double sum=bb(i,j);
100  for (int k=lb;k<i;k++)
101  {
102  sum -= bb(i,k)*bb(k,j);
103  }
104  //a[i][j]=sum;
105  bb(i,j)=sum;
106  }
107  int imax = j;
108  double big=0.0;
109  for (int i=j;i<=ub;i++)
110  {
111  double sum=bb(i,j);
112  for (int k=lb;k<j;k++)
113  {
114  sum -= bb(i,k)*bb(k,j);
115  }
116  bb(i,j)=sum;
117  double dum=vv[i]*fabs(sum);
118  if (dum >= big)
119  {
120  big=dum;
121  imax=i;
122  }
123  }
124  if (j != imax)
125  {
126  for (int k=lb;k<=ub;k++)
127  {
128  double dum=bb(imax,k);
129  bb(imax,k)=bb(j,k);
130  bb(j,k)=dum;
131  }
132  d = -1.*d;
133  vv[imax]=vv[j];
134 
135  //if (j<ub)
136  {
137  int itemp=indx(imax);
138  indx(imax)=indx(j);
139  indx(j)=itemp;
140  }
141  //cout << "indx= " <<indx<<endl;
142  }
143 
144  if (bb(j,j) == 0.0)
145  {
146  bb(j,j)=TINY;
147  }
148 
149  if (j != n)
150  {
151  double dum=1.0/bb(j,j);
152  for (int i=j+1;i<=ub;i++)
153  {
154  bb(i,j) = bb(i,j) * dum;
155  }
156  }
157  }
158 
159  // get the determinant
160  sign=d;
161  dvector part_prod(lb,ub);
162  part_prod(lb)=log(fabs(bb(lb,lb)));
163  if (bb(lb,lb)<0) sign=-sign;
164  for (int j=lb+1;j<=ub;j++)
165  {
166  if (bb(j,j)<0) sign=-sign;
167  part_prod(j)=part_prod(j-1)+log(fabs(bb(j,j)));
168  }
169  ln_unsigned_det=part_prod(ub);
170 
171  dmatrix z=trans(tz);
172  int mmin=z.indexmin();
173  int mmax=z.indexmax();
174  dmatrix x(mmin,mmax,lb,ub);
175  //dvector x(lb,ub);
176 
177  dvector y(lb,ub);
178  //int lb=rowmin;
179  //int ub=rowmax;
180  dmatrix& b=bb;
181  ivector indxinv(lb,ub);
182  for (int i=lb;i<=ub;i++)
183  {
184  indxinv(indx(i))=i;
185  }
186  for (int kk=mmin;kk<=mmax;kk++)
187  {
188  for (int i=lb;i<=ub;i++)
189  {
190  y(indxinv(i))=z(kk)(i);
191  }
192 
193  for (int i=lb;i<=ub;i++)
194  {
195  double sum=y(i);
196  for (int j=lb;j<=i-1;j++)
197  {
198  sum-=b(i,j)*y(j);
199  }
200  y(i)=sum;
201  }
202  for (int i=ub;i>=lb;i--)
203  {
204  double sum=y(i);
205  for (int j=i+1;j<=ub;j++)
206  {
207  sum-=b(i,j)*x(kk)(j);
208  }
209  x(kk)(i)=sum/b(i,i);
210  }
211  }
212  return trans(x);
213 }
214 
219  const banded_symmetric_dmatrix& MM,
220  int& ierr)
221 {
223 
224  int mmin=tmp.indexmin();
225  int mmax=tmp.indexmax();
226  double ld=0.0;
227  for (int i=mmin;i<=mmax;i++)
228  {
229  ld+=log(tmp(i,i));
230  }
231  return 2.0*ld;
232 }
233 
235 {
236  return sqrt(norm2(B));
237 }
238 
240 {
241  double nm=0.0;
242  for (int i=1;i<=B.bw-1;i++)
243  {
244  nm+=norm2(B.d(i));
245  }
246  nm*=2;
247  nm+=norm2(B.d(0));
248  return nm;
249 }
250 
251 #undef TINY
252 
Description not yet available.
Definition: fvar.hpp:7981
dmatrix trans(const dmatrix &m1)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat2.cpp:13
#define x
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Definition: fvar.hpp:2917
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
double norm(const d3_array &a)
Return computed norm value of a.
Definition: d3arr2a.cpp:190
unsigned int colsize() const
Definition: fvar.hpp:2948
df1_one_matrix choleski_decomp(const df1_one_matrix &MM)
Definition: df11fun.cpp:606
ivector sgn(const dvector &v)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dvect24.cpp:11
dvector solve(const dmatrix &aa, const dvector &z)
Solve a linear system using LU decomposition.
Definition: dmat34.cpp:46
prnstream & endl(prnstream &)
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
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.
Description not yet available.
Definition: fvar.hpp:8120
int colmin(void) const
Definition: fvar.hpp:2939
Description not yet available.
Definition: fvar.hpp:2819
int indexmin(void) const
Definition: fvar.hpp:8131
int indexmax(void) const
Definition: fvar.hpp:8135
double norm2(const d3_array &a)
Return sum of squared elements in a.
Definition: d3arr2a.cpp:167
int indexmax() const
Definition: fvar.hpp:2921
double ln_det_choleski(const banded_symmetric_dmatrix &MM, int &ierr)
Definition: dmat38.cpp:218
double sign(const double x)
The sign of a number.
Definition: gaussher.cpp:25
#define TINY
$Id$
Definition: dmat38.cpp:27
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