ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dmat35.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  */
11 #include "fvar.hpp"
12 
13 #ifdef __TURBOC__
14 #pragma hdrstop
15 #include <iostream.h>
16 #endif
17 
18 #ifdef __ZTC__
19 #include <iostream.hpp>
20 #endif
21 
22 #ifdef __SUN__
23 #include <iostream.h>
24 #endif
25 #ifdef __NDPX__
26 #include <iostream.h>
27 #endif
28 
33 dmatrix choleski_decomp_positive(const dmatrix& MM,double bound)
34 {
35  // kludge to deal with constantness
36  dmatrix & M= * (dmatrix *) &MM;
37  if (M.colsize() != M.rowsize())
38  {
39  cerr << "Error in chol_decomp. Matrix not square" << endl;
40  ad_exit(1);
41  }
42  int rowsave=M.rowmin();
43  int colsave=M.colmin();
44  M.rowshift(1);
45  M.colshift(1);
46  int n=M.rowmax();
47 
48  dmatrix L(1,n,1,n);
49 #ifndef SAFE_INITIALIZE
50  L.initialize();
51 #endif
52 
53  int i,j,k;
54  double tmp;
55  if (M(1,1)<=bound)
56  {
57  M(1,1)=bound;
58  }
59  L(1,1)=sqrt(M(1,1));
60  for (i=2;i<=n;i++)
61  {
62  L(i,1)=M(i,1)/L(1,1);
63  }
64 
65  for (i=2;i<=n;i++)
66  {
67  for (j=2;j<=i-1;j++)
68  {
69  tmp=M(i,j);
70  for (k=1;k<=j-1;k++)
71  {
72  tmp-=L(i,k)*L(j,k);
73  }
74  L(i,j)=tmp/L(j,j);
75  }
76  tmp=M(i,i);
77  for (k=1;k<=i-1;k++)
78  {
79  tmp-=L(i,k)*L(i,k);
80  if (tmp<=bound) break;
81  }
82  if (tmp<=bound)
83  {
84  tmp=bound;
85  }
86  L(i,i)=sqrt(tmp);
87  }
88  L.rowshift(rowsave);
89  L.colshift(colsave);
90 
91  return L;
92 }
93 
98 dmatrix choleski_decomp_positive(const dmatrix& MM,const int& _ierr)
99 {
100  // kludge to deal with constantness
101  int & ierr = (int &)(_ierr);
102  ierr=0;
103  dmatrix & M= * (dmatrix *) &MM;
104  if (M.colsize() != M.rowsize())
105  {
106  cerr << "Error in chol_decomp. Matrix not square" << endl;
107  ad_exit(1);
108  }
109  int rowsave=M.rowmin();
110  int colsave=M.colmin();
111  M.rowshift(1);
112  M.colshift(1);
113  int n=M.rowmax();
114 
115  dmatrix L(1,n,1,n);
116 #ifndef SAFE_INITIALIZE
117  L.initialize();
118 #endif
119 
120  int i,j,k;
121  double tmp;
122  if (M(1,1)<=0.0)
123  {
124  ierr =1;
125  L.rowshift(rowsave);
126  L.colshift(colsave);
127  return L;
128  }
129  L(1,1)=sqrt(M(1,1));
130  for (i=2;i<=n;i++)
131  {
132  L(i,1)=M(i,1)/L(1,1);
133  }
134 
135  for (i=2;i<=n;i++)
136  {
137  for (j=2;j<=i-1;j++)
138  {
139  tmp=M(i,j);
140  for (k=1;k<=j-1;k++)
141  {
142  tmp-=L(i,k)*L(j,k);
143  }
144  L(i,j)=tmp/L(j,j);
145  }
146  tmp=M(i,i);
147  for (k=1;k<=i-1;k++)
148  {
149  tmp-=L(i,k)*L(i,k);
150  }
151  if (tmp<=0.0)
152  {
153  ierr =1;
154  L.rowshift(rowsave);
155  L.colshift(colsave);
156  return L;
157  }
158  L(i,i)=sqrt(tmp);
159  }
160  L.rowshift(rowsave);
161  L.colshift(colsave);
162 
163  return L;
164 }
165 
171  (const dmatrix& MM,const int& _ierr)
172 {
173  // kludge to deal with constantness
174  int & ierr = (int &)(_ierr);
175  ierr=0;
176  dmatrix & M= * (dmatrix *) &MM;
177  if (M.colsize() != M.rowsize())
178  {
179  cerr << "Error in chol_decomp. Matrix not square" << endl;
180  ad_exit(1);
181  }
182  int rowsave=M.rowmin();
183  int colsave=M.colmin();
184  M.rowshift(1);
185  M.colshift(1);
186  int n=M.rowmax();
187 
189 #ifndef SAFE_INITIALIZE
190  L.initialize();
191 #endif
192 
193  int i,j,k;
194  double tmp;
195  if (M(1,1)<=0.0)
196  {
197  ierr =1;
198  L.rowshift(rowsave);
199  L.colshift(colsave);
200  return L;
201  }
202  L(1,1)=sqrt(M(1,1));
203  for (i=2;i<=n;i++)
204  {
205  L(i,1)=M(i,1)/L(1,1);
206  }
207 
208  for (i=2;i<=n;i++)
209  {
210  for (j=2;j<=i-1;j++)
211  {
212  tmp=M(i,j);
213  for (k=1;k<=j-1;k++)
214  {
215  tmp-=L(i,k)*L(j,k);
216  }
217  L(i,j)=tmp/L(j,j);
218  }
219  tmp=M(i,i);
220  for (k=1;k<=i-1;k++)
221  {
222  tmp-=L(i,k)*L(i,k);
223  }
224  if (tmp<=0.0)
225  {
226  ierr =1;
227  L.rowshift(rowsave);
228  L.colshift(colsave);
229  return L;
230  }
231  L(i,i)=sqrt(tmp);
232  }
233  return L;
234 }
exitptr ad_exit
Definition: gradstrc.cpp:53
unsigned int colsize() const
Definition: fvar.hpp:2948
prnstream & endl(prnstream &)
int rowmax() const
Definition: fvar.hpp:2929
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.
#define M
Definition: rngen.cpp:57
int colmin(void) const
Definition: fvar.hpp:2939
Description not yet available.
Definition: fvar.hpp:2819
lower_triangular_dmatrix lower_triangular_choleski_decomp_positive(const dmatrix &MM, const int &_ierr)
Description not yet available.
Definition: dmat35.cpp:171
void rowshift(int min)
Changes the range of valid indices for the rows.
Definition: dmat9.cpp:48
Description not yet available.
Definition: fvar.hpp:8960
void colshift(int min)
Description not yet available.
Definition: dmat9.cpp:68
unsigned int rowsize() const
Definition: fvar.hpp:2934
void initialize(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat7.cpp:12
int rowmin() const
Definition: fvar.hpp:2925
dmatrix choleski_decomp_positive(const dmatrix &MM, double bound)
Description not yet available.
Definition: dmat35.cpp:33