ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
f1b2solv.cpp
Go to the documentation of this file.
1 
7 #include <df1b2fun.h>
8 
9 #ifdef __TURBOC__
10  #pragma hdrstop
11  #include <iostream.h>
12 #endif
13 
14 #if defined (__WAT32__)
15  #include <iostream.h>
16  #include <strstrea.h>
17 #endif
18 
19 #ifdef __ZTC__
20  #include <iostream.hpp>
21 #endif
22 
23 #define TINY 1.0e-20;
24 df1b2vector solve(const df1b2matrix& aa,const dvector& z,
25  const df1b2variable & ln_unsigned_det,double& sign);
26 
27 
29 {
30  double ln_unsigned_det = 0;
31  double sign;
32  df1b2vector sol=solve(aa,z,ln_unsigned_det,sign);
33  return sol;
34 }
35 
36 df1b2vector solve(const df1b2matrix& aa,const dvector& z)
37 {
38  df1b2variable ln_unsigned_det;
39  double sign;
40  df1b2vector sol=solve(aa,z,ln_unsigned_det,sign);
41  return sol;
42 }
43 
44 
50 df1b2vector solve(const df1b2matrix& aa,const dvector& z,
51  const df1b2variable & _ln_unsigned_det,double& sign)
52 {
53  ADUNCONST(df1b2variable,ln_unsigned_det)
54  int n = aa.colsize();
55  int lb=aa.colmin();
56  int ub=aa.colmax();
57  if (lb!=aa.rowmin()||ub!=aa.colmax())
58  {
59  cerr << "Error matrix not square in solve()"<<endl;
60  ad_exit(1);
61  }
62  df1b2matrix bb(lb,ub,lb,ub);
63  bb=aa;
64  ivector indx(lb,ub);
65  int One=1;
66  indx.fill_seqadd(lb,One);
67  double d;
68  df1b2variable big,dum,sum,temp;
69  df1b2vector vv(lb,ub);
70 
71  d=1.0;
72  for (int i=lb;i<=ub;i++)
73  {
74  big=0.0;
75  for (int j=lb;j<=ub;j++)
76  {
77  temp=fabs(bb(i,j));
78  if (value(temp) > value(big))
79  {
80  big=temp;
81  }
82  }
83  if (value(big) == 0.0)
84  {
85  cerr <<
86  "Error in matrix inverse -- matrix singular in inv(df1b2matrix)\n";
87  }
88  vv[i]=1.0/big;
89  }
90 
91  for (int j=lb;j<=ub;j++)
92  {
93  for (int i=lb;i<j;i++)
94  {
95  sum=bb(i,j);
96  for (int k=lb;k<i;k++)
97  {
98  sum -= bb(i,k)*bb(k,j);
99  }
100  //a[i][j]=sum;
101  bb(i,j)=sum;
102  }
103  int imax = j;
104  big=0.0;
105  for (int i=j;i<=ub;i++)
106  {
107  sum=bb(i,j);
108  for (int k=lb;k<j;k++)
109  {
110  sum -= bb(i,k)*bb(k,j);
111  }
112  bb(i,j)=sum;
113  dum=vv[i]*fabs(sum);
114  if ( value(dum) >= value(big))
115  {
116  big=dum;
117  imax=i;
118  }
119  }
120  if (j != imax)
121  {
122  for (int k=lb;k<=ub;k++)
123  {
124  dum=bb(imax,k);
125  bb(imax,k)=bb(j,k);
126  bb(j,k)=dum;
127  }
128  d = -1.*d;
129  vv[imax]=vv[j];
130 
131  //if (j<ub)
132  {
133  int itemp=indx(imax);
134  indx(imax)=indx(j);
135  indx(j)=itemp;
136  }
137  //cout << "indx= " <<indx<<endl;
138  }
139 
140  if (value(bb(j,j)) == 0.0)
141  {
142  bb(j,j)=TINY;
143  }
144 
145  if (j != n)
146  {
147  dum=1.0/bb(j,j);
148  for (int i=j+1;i<=ub;i++)
149  {
150  bb(i,j) = bb(i,j) * dum;
151  }
152  }
153  }
154 
155  // get the determinant
156  sign=d;
157  df1b2vector part_prod(lb,ub);
158  part_prod(lb)=log(fabs(bb(lb,lb)));
159  if (value(bb(lb,lb))<0) sign=-sign;
160  for (int j=lb+1;j<=ub;j++)
161  {
162  if (value(bb(j,j))<0) sign=-sign;
163  part_prod(j)=part_prod(j-1)+log(fabs(bb(j,j)));
164  }
165  ln_unsigned_det=part_prod(ub);
166 
167  df1b2vector x(lb,ub);
168  df1b2vector y(lb,ub);
169  //int lb=rowmin;
170  //int ub=rowmax;
171  df1b2matrix& b=bb;
172  ivector indxinv(lb,ub);
173  for (int i=lb;i<=ub;i++)
174  {
175  indxinv(indx(i))=i;
176  }
177 
178  for (int i=lb;i<=ub;i++)
179  {
180  y(indxinv(i))=z(i);
181  }
182 
183  for (int i=lb;i<=ub;i++)
184  {
185  sum=y(i);
186  for (int j=lb;j<=i-1;j++)
187  {
188  sum-=b(i,j)*y(j);
189  }
190  y(i)=sum;
191  }
192  for (int i=ub;i>=lb;i--)
193  {
194  sum=y(i);
195  for (int j=i+1;j<=ub;j++)
196  {
197  sum-=b(i,j)*x(j);
198  }
199  x(i)=sum/b(i,i);
200  }
201 
202  return x;
203 }
204 
205 #undef TINY
206 #undef HOME_VERSION
207 
#define x
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Definition: fvar.hpp:140
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
Description not yet available.
Definition: df1b2fun.h:953
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
exitptr ad_exit
Definition: gradstrc.cpp:53
int rowmin(void) const
Definition: df1b2fun.h:1053
void fill_seqadd(int, int)
Fills ivector elements with values starting from base and incremented by offset.
Definition: cranfill.cpp:73
Description not yet available.
Definition: df1b2fun.h:266
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
dvector csolve(const dmatrix &aa, const dvector &z)
Solve a linear system using LU decomposition.
Definition: dmat34.cpp:32
Description not yet available.
Definition: df1b2fun.h:1042
Description not yet available.
#define TINY
Definition: f1b2solv.cpp:23
double sign(const double x)
The sign of a number.
Definition: gaussher.cpp:25
int colsize(void) const
Definition: df1b2fun.h:1091
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
int colmin(void) const
Definition: df1b2fun.h:1089
int colmax(void) const
Definition: df1b2fun.h:1090
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13