ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dmat28.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 //void get_eigenv(const dvector& _d,const dvector& _e,const dmatrix& _z);
14 
15 #if !defined(OPT_LIB)
16 
22 {
23  return d(i);
24 }
25 
31 {
32  return d(i);
33 }
34 
39 const double& banded_symmetric_dmatrix::operator()(int i, int j) const
40 {
41  return d(i-j,i);
42 }
43 
48 double& banded_symmetric_dmatrix::operator () (int i,int j)
49 {
50  return d(i-j,i);
51 }
52 
58 {
59  return d(i);
60 }
61 
67 {
68  return d(i);
69 }
70 
76 {
77  return d(i-j,i);
78 }
79 
84 const double& banded_lower_triangular_dmatrix::operator()(int i, int j) const
85 {
86  return d(i-j,i);
87 }
88 
89 #endif
90 
96  const banded_symmetric_dmatrix& BB,int _lb,int _ub) : bw(BB.bw), d(0,BB.bw-1)
97 {
99  if (_lb<B.indexmin() || _ub>B.indexmax())
100  {
101  cerr << "bounds error" << endl;
102  ad_exit(1);
103  }
104  d.allocate(0,bw-1);
105  for (int i=0;i<=bw-1;i++)
106  {
107  d(i)=B.d(i)(i+_lb,_ub);
108  }
109 }
110 
116 {
117  for (int i=0;i<bw;i++)
118  d(i).shift(j+i);
119 }
120 
126 {
127  for (int i=0;i<bw;i++)
128  d(i).shift(j+i);
129 }
130 
136  (int _min,int _max,int _bw)
137 {
138  bw=_bw;
139  ivector lb(0,bw-1);
140  lb.fill_seqadd(_min,1);
141  d.allocate(0,bw-1,lb,_max);
142 }
143 
149  const dvar_matrix_position& pos)
150 {
151  int nrl=pos.row_min;
152  int nrh=pos.row_max;
153  int cmin=pos.lb(nrl);
154  int cmax=pos.ub(nrl);
155  bw=nrh-nrl+1;
156  ivector lb(nrl,nrh);
157  lb.fill_seqadd(cmin,1);
158  d.allocate(nrl,nrh,lb,cmax);
159 }
160 
166  const dvar_matrix_position& pos)
167 {
168  int nrl=pos.row_min;
169  int nrh=pos.row_max;
170  int cmin=pos.lb(nrl);
171  int cmax=pos.ub(nrl);
172  bw=nrh-nrl+1;
173  ivector lb(nrl,nrh);
174  lb.fill_seqadd(cmin,1);
175  d.allocate(nrl,nrh,lb,cmax);
176 }
177 
183 {
184  int imin=S.indexmin();
185  int imax=S.indexmax();
186  int bw=S.bandwidth();
187  allocate(imin,imax,imin,imax);
188  for (int i=imin;i<=imax;i++)
189  {
190  for (int j=imin;j<=imax;j++)
191  {
192  if (j<=i)
193  {
194  if ( (i-j) < bw)
195  (*this)(i,j)=S(i,j);
196  else
197  (*this)(i,j)=0.0;
198  }
199  else
200  {
201  (*this)(i,j)=0.0;
202  }
203  }
204  }
205 }
206 
212 {
213  int imin=S.indexmin();
214  int imax=S.indexmax();
215  int bw=S.bandwidth();
216  allocate(imin,imax,imin,imax);
217  int i1;
218  int j1;
219  for (int i=imin;i<=imax;i++)
220  {
221  for (int j=imin;j<=imax;j++)
222  {
223  if (j<=i)
224  {
225  j1=j;
226  i1=i;
227  }
228  else
229  {
230  j1=i;
231  i1=j;
232  }
233  if ( (i1-j1) < bw)
234  (*this)(i,j)=S(i1,j1);
235  else
236  (*this)(i,j)=0.0;
237  }
238  }
239 }
240 
246  (int _min,int _max,int _bw)
247 {
248  bw=_bw;
249  ivector lb(0,_bw-1);
250  lb.fill_seqadd(_min,1);
251  d.allocate(0,_bw-1,lb,_max);
252 }
253 
258 ostream& operator<<(const ostream& _ofs,
260 {
261  ostream & ofs = (ostream&) _ofs;
263  int imin=S.indexmin();
264  int imax=S.indexmax();
265  int bw=S.bandwidth();
266  for (int i=imin;i<=imax;i++)
267  {
268  for (int j=imin;j<=imax;j++)
269  {
270  if (j<=i)
271  {
272  if ( (i-j) < bw)
273  ofs << S(i,j) << " ";
274  else
275  ofs << 0.0 << " ";
276  }
277  else
278  {
279  ofs << 0.0 << " ";
280  }
281  }
282  if (i<imax) ofs << endl;
283  }
284  return ofs;
285 }
286 
288  const banded_symmetric_dmatrix& MM)
289 {
290  int ierr = 0;
291  return choleski_decomp(MM, ierr);
292 }
294  const banded_symmetric_dmatrix& _M, int& ierr)
295 {
297  int minsave=M.indexmin();
298  M.shift(1);
299  int n=M.indexmax();
300 
301  int bw=M.bandwidth();
303 #ifndef SAFE_INITIALIZE
304  L.initialize();
305 #endif
306 
307  int i,j,k;
308  double tmp;
309  if (M(1,1)<=0)
310  {
311  if (ierr==0)
312  cerr << "Error matrix not positive definite in choleski_decomp"
313  <<endl;
314  ierr=1;
315  return L;
316  }
317  L(1,1)=sqrt(M(1,1));
318  for (i=2;i<=bw;i++)
319  {
320  L(i,1)=M(i,1)/L(1,1);
321  }
322 
323  for (i=2;i<=n;i++)
324  {
325  for (j=i-bw+1;j<=i-1;j++)
326  {
327  if (j>1)
328  {
329  tmp=M(i,j);
330  for (k=i-bw+1;k<=j-1;k++)
331  {
332  if (k>0 && k>j-bw)
333  tmp-=L(i,k)*L(j,k);
334  }
335  L(i,j)=tmp/L(j,j);
336  }
337  }
338  tmp=M(i,i);
339  for (k=i-bw+1;k<=i-1;k++)
340  {
341  if (k>0)
342  tmp-=L(i,k)*L(i,k);
343  }
344  if (tmp<=0)
345  {
346  if (ierr==0)
347  cerr << "Error matrix not positive definite in choleski_decomp"
348  <<endl;
349  ierr=1;
350  return L;
351  }
352  L(i,i)=sqrt(tmp);
353  }
354  M.shift(minsave);
355  L.shift(minsave);
356 
357  return L;
358 }
359 
364 banded_symmetric_dmatrix& banded_symmetric_dmatrix::operator =
366 {
367  int _bw=M.bandwidth();
368  int mmin=M.indexmin();
369  int mmax=M.indexmax();
370  if (!allocated(d))
371  {
372  bw=_bw;
373  ivector lb(0,_bw-1);
374  lb.fill_seqadd(mmin,1);
375  d.allocate(0,_bw-1,lb,mmax);
376  }
377  if (bw<bandwidth()) initialize();
378 
379  if (M.indexmin() != indexmin() || M.indexmax() != indexmax()
380  || M.bandwidth() > bandwidth() )
381  {
382  cerr << "incompatible shape in symmetric_dmatrix::operator = "
383  << endl;
384  ad_exit(1);
385  }
386 
387  for (int i=mmin;i<=mmax;i++)
388  {
389  int jmin=admax(mmin,i-bw+1);
390  for (int j=jmin;j<=i;j++)
391  {
392  (*this)(i,j)=M(i,j);
393  }
394  }
395  return *this;
396 }
397 
403  {
404  return banded_symmetric_dmatrix(*this,l,u);
405  }
406 
412  {
414  if (S.bandwidth() !=2)
415  {
416  cerr << "error bandwidth not equal 2" << endl;
417  ad_exit(1);
418  }
419 
420  int lb=S.indexmin();
421  int ub=S.indexmax();
422  int bw=S.bandwidth();
423  dmatrix M(lb,ub,lb,ub);
424  M.initialize();
425 
426  for(int i=lb;i<=ub;i++)
427  {
428  for(int j=i;j<=min(bw+i-1,ub);j++)
429  {
430  M(j,i) = S(j,i);
431  if(i!=j) M(i,j)=M(j,i);
432  }
433  }
434 
435  return eigenvalues(M);
436  }
437 
443  {
445  if (S.bandwidth() !=2)
446  {
447  cerr << "error bandwidth not equal 2" << endl;
448  ad_exit(1);
449  }
450 
451  int lb=S.indexmin();
452  int ub=S.indexmax();
453  int bw=S.bandwidth();
454  dmatrix M(lb,ub,lb,ub);
455  M.initialize();
456 
457  for(int i=lb;i<=ub;i++)
458  {
459  for(int j=i;j<=min(bw+i-1,ub);j++)
460  {
461  M(j,i) = S(j,i);
462  if(i!=j) M(i,j)=M(j,i);
463  }
464  }
465 
466  return eigenvectors(M);
467  }
468 /*
469  dvector eigenvalues(const banded_symmetric_dmatrix& _SS)
470  {
471  banded_symmetric_dmatrix& S = (banded_symmetric_dmatrix&) _SS;
472  if (S.bandwidth() !=2)
473  {
474  cerr << "error bandwidth not equal 2" << endl;
475  ad_exit(1);
476  }
477  int mmin=S.indexmin();
478  int mmax=S.indexmax();
479  dvector diag(mmin,mmax);
480  dvector offdiag(mmin,mmax);
481  diag=S(0);
482  offdiag(mmin+1,mmax)=S(1);
483  offdiag(mmin)=0.0;
484 
485  return get_eigen_values(diag.shift(1),offdiag.shift(1));
486  }
487 
488  dmatrix eigenvectors(const banded_symmetric_dmatrix& _SS,const dvector& _e)
489  {
490  banded_symmetric_dmatrix& S = (banded_symmetric_dmatrix&) _SS;
491  ADUNCONST(dvector,e);
492  if (S.bandwidth() !=2)
493  {
494  cerr << "error bandwidth not equal 2" << endl;
495  ad_exit(1);
496  }
497  int minsave=S.indexmin();
498  S.shift(1);
499  e.shift(1);
500  int mmin=1;
501  int mmax=S.indexmax();
502  dvector diag(mmin,mmax);
503  dvector offdiag(mmin,mmax);
504  diag=S(0);
505  offdiag(mmin+1,mmax)=S(1);
506  offdiag(mmin)=0.0;
507  dmatrix z(mmin,mmax,mmin,mmax);
508  z.initialize();
509  for (int i=mmin;i<=mmax;i++)
510  {
511  z(i,i)=1.0;
512  }
513 
514  get_eigenv(diag,offdiag,z);
515  e=diag;
516  e.shift(minsave);
517  S.shift(minsave);
518  z.colshift(minsave);
519  z.rowshift(minsave);
520  return z;
521  }
522 */
Description not yet available.
Definition: fvar.hpp:7981
void allocate(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat0.cpp:8
double & operator()(int i, int j)
Definition: fvar.hpp:8160
int admax(int i, int j)
Definition: fvar.hpp:8979
dmatrix(void)
Default constructor.
Definition: dmat0.cpp:16
dvector eigenvalues(const banded_symmetric_dmatrix &_SS)
Description not yet available.
Definition: dmat28.cpp:411
int allocated(const ivector &v)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: fvar_a59.cpp:13
#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
int bandwidth(void) const
Definition: fvar.hpp:8127
int indexmax(void) const
Definition: fvar.hpp:7999
exitptr ad_exit
Definition: gradstrc.cpp:53
void initialize(void)
Description not yet available.
Definition: dmat41.cpp:29
void fill_seqadd(int, int)
Fills ivector elements with values starting from base and incremented by offset.
Definition: cranfill.cpp:73
df1_one_matrix choleski_decomp(const df1_one_matrix &MM)
Definition: df11fun.cpp:606
prnstream & endl(prnstream &)
dmatrix eigenvectors(const banded_symmetric_dmatrix &_SS, const dvector &_e)
Description not yet available.
Definition: dmat28.cpp:442
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
banded_lower_triangular_dmatrix(const dvar_matrix_position &mpos)
Description not yet available.
Definition: dmat28.cpp:165
#define min(a, b)
Definition: cbivnorm.cpp:188
double & operator()(int i, int j)
Definition: fvar.hpp:8030
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Description not yet available.
Definition: fvar.hpp:8120
#define M
Definition: rngen.cpp:57
Description not yet available.
Definition: fvar.hpp:2819
int indexmin(void) const
Definition: fvar.hpp:8131
void shift(int)
Description not yet available.
Definition: dmat28.cpp:125
int indexmax(void) const
Definition: fvar.hpp:8135
ostream & operator<<(const ostream &_s, preshowpoint p)
Description not yet available.
Definition: admanip.cpp:48
banded_symmetric_dmatrix(void)
Definition: fvar.hpp:8012
banded_symmetric_dmatrix sub(int l, int u)
Description not yet available.
Definition: dmat28.cpp:402
size_t pos(const adstring &substr, const adstring &s)
Definition: string3.cpp:56
int indexmin(void) const
Definition: fvar.hpp:7995
void shift(int)
Description not yet available.
Definition: dmat28.cpp:115
void initialize(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat7.cpp:12
int bandwidth(void) const
Definition: fvar.hpp:7991
Description not yet available.
Definition: fvar.hpp:4843