ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
f1b2sol2.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 df1b2vector& z,
25  const df1b2variable & ln_unsigned_det,double& sign);
26 
28  const df1b2variable& ld, df1b2variable& sign)
29 {
30  double sgn = 0;
31  return solve(aa,z,ld,sgn);
32 }
33 
35 {
36  double ln_unsigned_det = 0;
37  double sign = 0;
38  df1b2vector sol=solve(aa,z,ln_unsigned_det,sign);
39  return sol;
40 }
41 
43 {
44  df1b2variable ln_unsigned_det;
45  double sign;
46  df1b2vector sol=solve(aa,z,ln_unsigned_det,sign);
47  return sol;
48 }
49 
56  const df1b2variable & _ln_unsigned_det,double& sign)
57 {
58  ADUNCONST(df1b2variable,ln_unsigned_det)
60  int k,n;
61  n=aa.colsize();
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  df1b2matrix bb(lb,ub,lb,ub);
70  bb=aa;
71  ivector indx(lb,ub);
72  int One=1;
73  indx.fill_seqadd(lb,One);
74  double d;
75  df1b2variable big,dum,sum,temp;
76  df1b2vector vv(lb,ub);
77 
78  d=1.0;
79  for (int i=lb;i<=ub;i++)
80  {
81  big=0.0;
82  for (int j=lb;j<=ub;j++)
83  {
84  temp=fabs(bb(i,j));
85  if (value(temp) > value(big))
86  {
87  big=temp;
88  }
89  }
90  if (value(big) == 0.0)
91  {
92  cerr <<
93  "Error in matrix inverse -- matrix singular in inv(df1b2matrix)\n";
94  }
95  vv[i]=1.0/big;
96  }
97 
98  for (int j=lb;j<=ub;j++)
99  {
100  for (int i=lb;i<j;i++)
101  {
102  sum=bb(i,j);
103  for (k=lb;k<i;k++)
104  {
105  sum -= bb(i,k)*bb(k,j);
106  }
107  //a[i][j]=sum;
108  bb(i,j)=sum;
109  }
110  int imax = j;
111  big=0.0;
112  for (int i=j;i<=ub;i++)
113  {
114  sum=bb(i,j);
115  for (k=lb;k<j;k++)
116  {
117  sum -= bb(i,k)*bb(k,j);
118  }
119  bb(i,j)=sum;
120  dum=vv[i]*fabs(sum);
121  if ( value(dum) >= value(big))
122  {
123  big=dum;
124  imax=i;
125  }
126  }
127  if (j != imax)
128  {
129  for (k=lb;k<=ub;k++)
130  {
131  dum=bb(imax,k);
132  bb(imax,k)=bb(j,k);
133  bb(j,k)=dum;
134  }
135  d = -1.*d;
136  vv[imax]=vv[j];
137 
138  //if (j<ub)
139  {
140  int itemp=indx(imax);
141  indx(imax)=indx(j);
142  indx(j)=itemp;
143  }
144  //cout << "indx= " <<indx<<endl;
145  }
146 
147  if (value(bb(j,j)) == 0.0)
148  {
149  bb(j,j)=TINY;
150  }
151 
152  if (j != n)
153  {
154  dum=1.0/bb(j,j);
155  for (int i=j+1;i<=ub;i++)
156  {
157  bb(i,j) = bb(i,j) * dum;
158  }
159  }
160  }
161 
162  // get the determinant
163  sign=d;
164  df1b2vector part_prod(lb,ub);
165  part_prod(lb)=log(fabs(bb(lb,lb)));
166  if (value(bb(lb,lb))<0) sign=-sign;
167  for (int j=lb+1;j<=ub;j++)
168  {
169  if (value(bb(j,j))<0) sign=-sign;
170  part_prod(j)=part_prod(j-1)+log(fabs(bb(j,j)));
171  }
172  ln_unsigned_det=part_prod(ub);
173 
174  df1b2vector x(lb,ub);
175  df1b2vector y(lb,ub);
176  //int lb=rowmin;
177  //int ub=rowmax;
178  df1b2matrix& b=bb;
179  ivector indxinv(lb,ub);
180  for (int i=lb;i<=ub;i++)
181  {
182  indxinv(indx(i))=i;
183  }
184 
185  for (int i=lb;i<=ub;i++)
186  {
187  y(indxinv(i))=z(i);
188  }
189 
190  for (int i=lb;i<=ub;i++)
191  {
192  sum=y(i);
193  for (int j=lb;j<=i-1;j++)
194  {
195  sum-=b(i,j)*y(j);
196  }
197  y(i)=sum;
198  }
199  for (int i=ub;i>=lb;i--)
200  {
201  sum=y(i);
202  for (int j=i+1;j<=ub;j++)
203  {
204  sum-=b(i,j)*x(j);
205  }
206  x(i)=sum/b(i,i);
207  }
208 
209  return x;
210 }
211 #undef TINY
#define x
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Definition: fvar.hpp:140
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
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
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
#define TINY
Definition: f1b2sol2.cpp:23
Description not yet available.
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