ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
f1b2lndt.cpp
Go to the documentation of this file.
00001 
00007 #include <df1b2fun.h>
00008 
00009 #ifdef ISZERO
00010   #undef ISZERO
00011 #endif
00012 #define ISZERO(d) ((d)==0.0)
00013 
00014 class df1b2matrix_pair
00015 {
00016   df1b2matrix a;
00017   df1b2matrix b;
00018   ivector index;
00019 public:
00020   df1b2matrix_pair(const df1b2matrix & _a,const df1b2matrix & _b);
00021   df1b2matrix get_a(void){return a;}
00022   df1b2matrix get_b(void){return b;}
00023 };
00024 
00025 dmatrix ludcmp(const dmatrix& M,int kludge);
00026 df1b2matrix reorder(const df1b2matrix& M,const ivector& indx);
00027 df1b2vector reorder(const df1b2vector& M,const ivector& indx);
00028 void ludcmp(const df1b2matrix& a,int k);
00029 df1b2matrix_pair ludcmp(const df1b2matrix& a);
00030 
00041 df1b2vector lubksb(const df1b2matrix&  alpha, const df1b2matrix& beta,
00042   ivector & ,const df1b2vector& b)
00043 {
00044   int i,ii=0,j;
00045   df1b2variable sum;
00046   int mmin=b.indexmin();
00047   int mmax=b.indexmax();
00048   if (mmin !=1)
00049   {
00050     cerr << "not implemented" << endl;
00051     ad_exit(1);
00052   }
00053   int n=mmax;
00054   df1b2vector c(mmin,mmax);
00055   c=b;
00056 
00057   for (i=1;i<=n;i++)
00058   {
00059     sum=c(i);
00060     if (ii)
00061     {
00062       df1b2vector bt=c(ii,i-1);
00063       df1b2vector at=alpha(i)(ii,i-1);
00064       sum-=at*bt;
00065     }
00066     else
00067     {
00068       if (!ISZERO(value(sum))) ii=i;
00069     }
00070     c(i)=sum;
00071   }
00072   for (i=n;i>=1;i--)
00073   {
00074     sum=c(i);
00075     for (j=i+1;j<=n;j++) sum -= beta(j)(i)*c(j);
00076     c(i)=sum/beta(i)(i);
00077   }
00078   return c;
00079 }
00080 
00081 df1b2variable get_ln_det(const df1b2matrix& b,int& sgn)
00082 {
00083   // determinant of a lower triangular matrix
00084   int i;
00085   int mmin=b.indexmin();
00086   int mmax=b.indexmax();
00087   sgn=1;
00088   df1b2variable ln_det;
00089   ln_det=0.0;
00090   for (i=mmin;i<=mmax;i++)
00091   {
00092     if (value(b(i,i))<0.0)
00093     {
00094       ln_det+=log(-b(i,i));
00095       sgn=-sgn;
00096     }
00097     else
00098     {
00099       ln_det+=log(b(i,i));
00100     }
00101   }
00102   return ln_det;
00103 }
00104 
00105 df1b2matrix lubksb(const df1b2matrix&  alpha, const df1b2matrix& beta,
00106   ivector & ,const df1b2matrix& B)
00107 {
00108   int i,ii=0,j;
00109   int rmin=B.indexmin();
00110   int rmax=B.indexmin();
00111   df1b2matrix C(rmin,rmax);
00112   for (int k=rmin;k<=rmax;k++)
00113   {
00114     df1b2variable sum;
00115     int mmin=B(k).indexmin();
00116     int mmax=B(k).indexmax();
00117     if (mmin !=1)
00118     {
00119       cerr << "not implemented" << endl;
00120       ad_exit(1);
00121     }
00122     int n=mmax;
00123     df1b2vector c(mmin,mmax);
00124     c=B(k);
00125 
00126     for (i=1;i<=n;i++) {
00127       sum=c(i);
00128       if (ii)
00129       {
00130         df1b2vector bt=c(ii,i-1);
00131         df1b2vector at=alpha(i)(ii,i-1);
00132         sum-=at*bt;
00133       }
00134       else
00135       {
00136         if (!ISZERO(value(sum))) ii=i;
00137       }
00138       c(i)=sum;
00139     }
00140     for (i=n;i>=1;i--)
00141     {
00142       sum=c(i);
00143       for (j=i+1;j<=n;j++) sum -= beta(j)(i)*c(j);
00144       c(i)=sum/beta(i)(i);
00145     }
00146     C(i)=c;
00147   }
00148   return C;
00149 }
00150 
00151 df1b2matrix_pair::df1b2matrix_pair(const df1b2matrix& _a,
00152   const df1b2matrix& _b) : a(_a), b(_b)
00153 {}
00154 
00155 void ludcmp(const df1b2matrix& M,int kludge)
00156 {
00157   // do lu decomp once to get ordering
00158   int mmin=M.indexmin();
00159   int mmax=M.indexmax();
00160 
00161   ivector indx(mmin,mmax);
00162   ivector indx2(mmin,mmax);
00163   double d;
00164   dmatrix MC=value(M);
00165   ludcmp(MC,indx,d);
00166 
00167   df1b2matrix RM=reorder(M,indx);
00168 
00169   ludcmp(RM);
00170 }
00171 
00172 df1b2matrix reorder(const df1b2matrix& M,const ivector& indx)
00173 {
00174   int i;
00175   int mmin=M.indexmin();
00176   int mmax=M.indexmax();
00177   if ( (indx.indexmin() != mmin) || (indx.indexmax() != mmax)
00178     || (M(mmin).indexmin() != mmin)
00179     || (M(mmin).indexmax() != mmax) )
00180   {
00181     cerr << " Inconsistent sizes in "
00182       " dmatrix reorder(const dmatrix& M,const ivector& indx) "
00183       << endl;
00184     ad_exit(1);
00185   }
00186   df1b2matrix RM(mmin,mmax);
00187   ivector ir(mmin,mmax);
00188   ir.fill_seqadd(mmin,1);
00189 
00190   for (i=mmin;i<=mmax;i++)
00191   {
00192     int tmp=ir(i);
00193     ir(i)=ir(indx(i));
00194     ir(indx(i))=tmp;
00195   }
00196   //cout << ir << endl;
00197   for (i=mmin;i<=mmax;i++)
00198   {
00199     RM(i)=M(ir(i));
00200   }
00201   return RM;
00202 }
00203 df1b2vector reorder(const df1b2vector& M,const ivector& indx)
00204 {
00205   int i;
00206   int mmin=M.indexmin();
00207   int mmax=M.indexmax();
00208   if ( (indx.indexmin() != mmin) || (indx.indexmax() != mmax) )
00209   {
00210     cerr << " Inconsistent sizes in "
00211       " dvector reorder(const dvector& M,const ivector& indx) "
00212       << endl;
00213     ad_exit(1);
00214   }
00215   df1b2vector RM(mmin,mmax);
00216   ivector ir(mmin,mmax);
00217   ir.fill_seqadd(mmin,1);
00218 
00219   for (i=mmin;i<=mmax;i++)
00220   {
00221     int tmp=ir(i);
00222     ir(i)=ir(indx(i));
00223     ir(indx(i))=tmp;
00224   }
00225   //cout << ir << endl;
00226   for (i=mmin;i<=mmax;i++)
00227   {
00228     RM(i)=M(ir(i));
00229   }
00230   return RM;
00231 }
00232 
00233 df1b2matrix_pair ludcmp(const df1b2matrix& a)
00234 {
00235   int i,j;
00236 
00237   int n=a.indexmax();
00238   ivector ishape(1,n);
00239   ishape.fill_seqadd(1,1);
00240   df1b2matrix alpha(1,n,1,ishape);
00241   df1b2matrix beta(1,n,1,ishape);
00242   //df1b2matrix delta(1,n,1,n);
00243   df1b2vector gamma(1,n);
00244   alpha.initialize();
00245   beta.initialize();
00246   gamma.initialize();
00247 
00248   df1b2variable sum;
00249   for (j=1;j<=n;j++)
00250   {
00251     for (i=1;i<j;i++)
00252     {
00253       df1b2vector& ai=alpha(i);
00254       df1b2vector& bj=beta(j);
00255       sum=a(i,j);
00256       //for (k=1;k<i;k++) { sum -= ai(k)*bj(k); }
00257       sum-=ai(1,i)*bj(1,i);
00258       beta(j,i)=sum;
00259     }
00260     for (i=j;i<=n;i++)
00261     {
00262       df1b2vector& ai=alpha(i);
00263       df1b2vector& bj=beta(j);
00264       sum=a(i,j);
00265       //for (k=1;k<j;k++) { sum -= ai(k)*bj(k); }
00266       sum-=ai(1,j)*bj(1,j);
00267       if (i==j)
00268       {
00269         beta(i,j)=sum;
00270       }
00271       else
00272       {
00273         if (j !=n)
00274         {
00275           //delta(i,j)=sum;
00276           alpha(i,j)=sum;
00277         }
00278         else
00279         {
00280           alpha(i,j)=sum;
00281         }
00282       }
00283     }
00284     if (j != n)
00285     {
00286       gamma(j)=1.0/beta(j,j);
00287       for (i=j+1;i<=n;i++)
00288       {
00289         alpha(i,j)*=gamma(j);
00290         //alpha(i,j) = delta(i,j)*gamma(j);
00291       }
00292     }
00293   }
00294   return df1b2matrix_pair(alpha,beta);
00295 }
00296 dmatrix reorder(const dmatrix& CM,ivector & indx);
00297 ivector getreindex(ivector & indx);
00298 
00299 df1b2variable ln_det(const df1b2matrix& M,int & sgn)
00300 {
00301   int mmin=M.indexmin();
00302   int mmax=M.indexmax();
00303   if (mmin !=1)
00304   {
00305     cerr << "not implemented" << endl;
00306     ad_exit(1);
00307   }
00308   int ssgn=0;
00309   dmatrix CM=value(M);
00310   double cld=ln_det(CM,ssgn);
00311   ivector indx(1,mmax);
00312   double dd;
00313   ludcmp(CM,indx,dd);
00314   ivector reindex=getreindex(indx);
00315   df1b2matrix RM=reorder(M,indx);
00316   df1b2matrix_pair p=ludcmp(RM);
00317   int isg=0;
00318   df1b2variable ld=get_ln_det(p.get_b(),isg);
00319   cout << setprecision(16) << cld-value(ld) << "  ";
00320   //cout << dd << " " << isg << "  "  << dd*isg << endl;
00321   return ld;
00322 }
00323 
00324 df1b2variable ln_det(const df1b2matrix& M)
00325 {
00326   int sgn;
00327   return ln_det(M,sgn);
00328 }
00329 
00330 dmatrix reorder(const dmatrix& CM,ivector & indx)
00331 {
00332   int mmin=CM.indexmin();
00333   int mmax=CM.indexmax();
00334   dmatrix M(mmin,mmax,mmin,mmax);
00335   dvector tmp(mmin,mmax);
00336   dvector in(mmin,mmax);
00337   in.fill_seqadd(1,1);
00338   for (int i=mmin;i<=mmax;i++)
00339   {
00340     M(i)=CM(indx(i));
00341   }
00342   return M;
00343 }
00344 void xswitch(int & i1,int & i2)
00345 {
00346   int tmp=i1;
00347   i1=i2;
00348   i2=tmp;
00349 }
00350 
00351 ivector getreindex(ivector & indx)
00352 {
00353   int mmin=indx.indexmin();
00354   int mmax=indx.indexmax();
00355   ivector in1(mmin,mmax);
00356   ivector in2(mmin,mmax);
00357   in1.fill_seqadd(1,1);
00358   in2.fill_seqadd(1,1);
00359   for (int i=mmin;i<=mmax;i++)
00360   {
00361     xswitch(in1(i),in1(indx(i)));
00362   }
00363  return in1;
00364 }
00365 
00366 df1b2vector solve(df1b2matrix& M,df1b2vector& v)
00367 {
00368   int mmin=M.indexmin();
00369   int mmax=M.indexmax();
00370   if (mmin !=1)
00371   {
00372     cerr << "not implemented" << endl;
00373     ad_exit(1);
00374   }
00375   dmatrix CM=value(M);
00376   ivector indx(1,mmax);
00377   double dd;
00378   ludcmp(CM,indx,dd);
00379   df1b2matrix RM=reorder(M,indx);
00380   df1b2vector rb=reorder(v,indx);
00381   df1b2matrix_pair p=ludcmp(RM);
00382   return lubksb(p.get_a(),p.get_b(),indx,rb);
00383 }
00384 
00385 df1b2vector solve(df1b2matrix& M,df1b2vector& v,const df1b2variable& _ln_det)
00386 {
00387   df1b2variable& ln_det = (df1b2variable&) _ln_det;
00388   int mmin=M.indexmin();
00389   int mmax=M.indexmax();
00390   if (mmin !=1)
00391   {
00392     cerr << "not implemented" << endl;
00393     ad_exit(1);
00394   }
00395   dmatrix CM=value(M);
00396   ivector indx(1,mmax);
00397   double dd;
00398   ludcmp(CM,indx,dd);
00399   df1b2matrix RM=reorder(M,indx);
00400   df1b2vector rb=reorder(v,indx);
00401   df1b2matrix_pair p=ludcmp(RM);
00402   int isg;
00403   ln_det=get_ln_det(p.get_b(),isg);
00404   return lubksb(p.get_a(),p.get_b(),indx,rb);
00405 }
00406 
00407 df1b2vector solve(df1b2matrix& M,df1b2vector& v,const df1b2variable& _ln_det,
00408   const int& sgn)
00409 {
00410   df1b2variable& ln_det = (df1b2variable&) _ln_det;
00411   int mmin=M.indexmin();
00412   int mmax=M.indexmax();
00413   if (mmin !=1)
00414   {
00415     cerr << "not implemented" << endl;
00416     ad_exit(1);
00417   }
00418   dmatrix CM=value(M);
00419   ivector indx(1,mmax);
00420   double dd;
00421   ludcmp(CM,indx,dd);
00422   df1b2matrix RM=reorder(M,indx);
00423   df1b2vector rb=reorder(v,indx);
00424   df1b2matrix_pair p=ludcmp(RM);
00425   int isg;
00426   ln_det=get_ln_det(p.get_b(),isg);
00427   return lubksb(p.get_a(),p.get_b(),indx,rb);
00428 }