ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dmat15.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 
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)<=0)
56  {
57  cerr << "Error matrix not positive definite in choleski_decomp"
58  <<endl;
59  ad_exit(1);
60  }
61  L(1,1)=sqrt(M(1,1));
62  for (i=2;i<=n;i++)
63  {
64  L(i,1)=M(i,1)/L(1,1);
65  }
66 
67  for (i=2;i<=n;i++)
68  {
69  for (j=2;j<=i-1;j++)
70  {
71  tmp=M(i,j);
72  for (k=1;k<=j-1;k++)
73  {
74  tmp-=L(i,k)*L(j,k);
75  }
76  L(i,j)=tmp/L(j,j);
77  }
78  tmp=M(i,i);
79  for (k=1;k<=i-1;k++)
80  {
81  tmp-=L(i,k)*L(i,k);
82  }
83  if (tmp<=0)
84  {
85  cerr << "Error matrix not positive definite in choleski_decomp"
86  <<endl;
87  ad_exit(1);
88  }
89  L(i,i)=sqrt(tmp);
90  }
91  L.rowshift(rowsave);
92  L.colshift(colsave);
93 
94  return L;
95 }
96 
101 static dmatrix onerror(dmatrix& L,int & ierror)
102 {
103  ierror=1;
104  return L;
105 }
106 
111 dmatrix choleski_decomp_error(const dmatrix& MM,int& ierror)
112 {
113  ierror=0;
114  // kludge to deal with constantness
115  dmatrix & M= * (dmatrix *) &MM;
116  if (M.colsize() != M.rowsize())
117  {
118  cerr << "Error in chol_decomp. Matrix not square" << endl;
119  ad_exit(1);
120  }
121  int rowsave=M.rowmin();
122  int colsave=M.colmin();
123  M.rowshift(1);
124  M.colshift(1);
125  int n=M.rowmax();
126 
127  dmatrix L(1,n,1,n);
128 #ifndef SAFE_INITIALIZE
129  L.initialize();
130 #endif
131 
132  int i,j,k;
133  double tmp;
134  if (M(1,1)<=0)
135  {
136  return onerror(L,ierror);
137  }
138  L(1,1)=sqrt(M(1,1));
139  for (i=2;i<=n;i++)
140  {
141  L(i,1)=M(i,1)/L(1,1);
142  }
143 
144  for (i=2;i<=n;i++)
145  {
146  for (j=2;j<=i-1;j++)
147  {
148  tmp=M(i,j);
149  for (k=1;k<=j-1;k++)
150  {
151  tmp-=L(i,k)*L(j,k);
152  }
153  L(i,j)=tmp/L(j,j);
154  }
155  tmp=M(i,i);
156  for (k=1;k<=i-1;k++)
157  {
158  tmp-=L(i,k)*L(i,k);
159  }
160  if (tmp<=0)
161  {
162  return onerror(L,ierror);
163  }
164  L(i,i)=sqrt(tmp);
165  }
166  L.rowshift(rowsave);
167  L.colshift(colsave);
168 
169  return L;
170 }
171 
177 {
178  ierror=0;
179  // kludge to deal with constantness
180  dmatrix & M= * (dmatrix *) &MM;
181  if (M.colsize() != M.rowsize())
182  {
183  cerr << "Error in chol_decomp. Matrix not square" << endl;
184  ad_exit(1);
185  }
186  int rowsave=M.rowmin();
187  int colsave=M.colmin();
188  M.rowshift(1);
189  M.colshift(1);
190  int n=M.rowmax();
191 
192  dmatrix L(1,n,1,n);
193 #ifndef SAFE_INITIALIZE
194  L.initialize();
195 #endif
196 
197  int i,j,k;
198  double tmp;
199  if (M(1,1)>=0)
200  {
201  return onerror(L,ierror);
202  }
203  L(1,1)=sqrt(-M(1,1));
204  for (i=2;i<=n;i++)
205  {
206  L(i,1)=-M(i,1)/L(1,1);
207  }
208 
209  for (i=2;i<=n;i++)
210  {
211  for (j=2;j<=i-1;j++)
212  {
213  tmp=-M(i,j);
214  for (k=1;k<=j-1;k++)
215  {
216  tmp-=L(i,k)*L(j,k);
217  }
218  L(i,j)=tmp/L(j,j);
219  }
220  tmp=-M(i,i);
221  for (k=1;k<=i-1;k++)
222  {
223  tmp-=L(i,k)*L(i,k);
224  }
225  if (tmp<=0)
226  {
227  return onerror(L,ierror);
228  }
229  L(i,i)=sqrt(tmp);
230  }
231  L.rowshift(rowsave);
232  L.colshift(colsave);
233 
234  return L;
235 }
static dmatrix onerror(dmatrix &L, int &ierror)
Description not yet available.
Definition: dmat15.cpp:101
dmatrix choleski_decomp_neghess_error(const dmatrix &MM, int &ierror)
Description not yet available.
Definition: dmat15.cpp:176
exitptr ad_exit
Definition: gradstrc.cpp:53
unsigned int colsize() const
Definition: fvar.hpp:2948
df1_one_matrix choleski_decomp(const df1_one_matrix &MM)
Definition: df11fun.cpp:606
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.
dmatrix choleski_decomp_error(const dmatrix &MM, int &ierror)
Description not yet available.
Definition: dmat15.cpp:111
#define M
Definition: rngen.cpp:57
int colmin(void) const
Definition: fvar.hpp:2939
Description not yet available.
Definition: fvar.hpp:2819
void rowshift(int min)
Changes the range of valid indices for the rows.
Definition: dmat9.cpp:48
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