ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fvar_ma4.cpp
Go to the documentation of this file.
1 
7 #include "fvar.hpp"
8 #include <math.h>
9 #ifndef OPT_LIB
10  #include <cassert>
11  #include <climits>
12 #endif
13 
14 #ifdef ISZERO
15  #undef ISZERO
16 #endif
17 #define ISZERO(d) ((d)==0.0)
18 
19 static double eps0=1.e-50;
20 
21 void lubksb(dvar_matrix a, const ivector& indx,dvar_vector b);
22 void ludcmp(const dvar_matrix& a, const ivector& indx, const prevariable& d);
23 
32 void ludcmp(const dvar_matrix& _a, const ivector& _indx, const prevariable& _d)
33 {
36  ivector& indx= (ivector&) _indx;
37  int i,j,k;
38 
39 #if !defined(OPT_LIB) && (__cplusplus >= 201103L)
40  int n = [](unsigned int colsize) -> int
41  {
42  assert(colsize <= INT_MAX);
43  return static_cast<int>(colsize);
44  } (a.colsize());
45 #else
46  int n = static_cast<int>(a.colsize());
47 #endif
48 
49  int lb=a.colmin();
50  int ub=a.colmax();
51 
52  dvariable big,dum,sum,temp;
53 
54  dvar_vector vv(lb,ub);
55 
56 
57  d=1.0;
58 
59  for (i=lb;i<=ub;i++)
60  {
61  big=0.0;
62  for (j=lb;j<=ub;j++)
63  {
64  temp=fabs(a(i,j));
65  if (temp > big)
66  {
67  big=temp;
68  }
69  }
70  if (big == 0.0)
71  {
72  cerr << "Error in matrix inverse -- matrix singular in inv(dmatrix)\n";
73  }
74  vv(i)=1.0/big;
75  }
76 
77  for (j=lb;j<=ub;j++)
78  {
79  for (i=lb;i<j;i++)
80  {
81  sum=a(i,j);
82  for (k=lb;k<i;k++)
83  {
84  sum = sum - a(i,k)*a(k,j);
85  }
86  a(i,j)=sum;
87  }
88  int imax = j;
89  big=0.0;
90  for (i=j;i<=ub;i++)
91  {
92  sum=a(i,j);
93  for (k=lb;k<j;k++)
94  {
95  sum = sum - a(i,k)*a(k,j);
96  }
97  a(i,j)=sum;
98  dum=vv(i)*fabs(sum);
99  if ( dum >= big)
100  {
101  big=dum;
102  imax=i;
103  }
104  }
105  if (j != imax)
106  {
107  for (k=lb;k<=ub;k++)
108  {
109  dum=a(imax,k);
110  a(imax,k)=a(j,k);
111  a(j,k)=dum;
112  }
113  d = -1.*d;
114  vv(imax)=vv(j);
115  }
116  indx(j)=imax;
117 
118  if (a(j,j) == 0.0)
119  {
120  a(j,j)=eps0;
121  }
122 
123  if (j != n)
124  {
125  dum=1.0/(a(j,j));
126  for (i=j+1;i<=ub;i++)
127  {
128  a(i,j) = a(i,j) * dum;
129  }
130  }
131  }
132 }
133 
143 void lubksb(dvar_matrix a, const ivector& indx,dvar_vector b)
144 {
145  int i,ii=0,ip,j,iiflag=0;
146  dvariable sum;
147  int lb=a.colmin();
148  int ub=a.colmax();
149  for (i=lb;i<=ub;i++)
150  {
151  ip=indx(i);
152  sum=b(ip);
153  b(ip)=b(i);
154  if (iiflag)
155  {
156  for (j=ii;j<=i-1;j++)
157  {
158  sum -= a.elem(i,j)*b.elem(j);
159  }
160  }
161  else if (!ISZERO(value(sum)))
162  {
163  ii=i;
164  iiflag=1;
165  }
166  b(i)=sum;
167  }
168 
169  for (i=ub;i>=lb;i--)
170  {
171  sum=b(i);
172  for (j=i+1;j<=ub;j++)
173  { // !!! remove to show bug
174  sum -= a.elem(i,j)*b.elem(j);
175  } // !!! remove to show bug
176  b.elem(i)=sum/a.elem(i,i);
177  }
178 }
179 
180 
Base class for dvariable.
Definition: fvar.hpp:1315
void ludcmp(const dmatrix &a, const ivector &indx, const double &d)
Lu decomposition of a constant matrix.
Definition: dmat3.cpp:221
dvar_vector & elem(int i)
Definition: fvar.hpp:2507
int colmin(void) const
Definition: fvar.hpp:2552
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Definition: fvar.hpp:140
void lubksb(dmatrix a, const ivector &indx, dvector b)
LU decomposition back susbstitution alogrithm for constant object.
Definition: dmat3.cpp:455
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr.cpp:21
#define ISZERO(d)
Definition: fvar_ma4.cpp:17
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
prevariable elem(int i)
Definition: fvar.hpp:2221
ADMB variable vector.
Definition: fvar.hpp:2172
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
static double eps0
Definition: fvar_ma4.cpp:19
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Class definition of matrix with derivitive information .
Definition: fvar.hpp:2480
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
int colmax(void) const
Definition: fvar.hpp:2556