ADMB Documentation  11.1.2192
 All Classes Files Functions Variables Typedefs Friends Defines
dmat38.cpp
Go to the documentation of this file.
00001 
00007 #include <fvar.hpp>
00008 
00009 #ifdef __TURBOC__
00010   #pragma hdrstop
00011   #include <iostream.h>
00012 #endif
00013 
00014 #if defined (__WAT32__)
00015   #include <iostream.h>
00016   #include <strstrea.h>
00017 #endif
00018 
00019 #ifdef __ZTC__
00020   #include <iostream.hpp>
00021 #endif
00022 
00023 #define TINY 1.0e-20;
00024 
00025 dmatrix solve(const dmatrix& aa,const dmatrix& tz,
00026   const double& ln_unsigned_det,double& sign);
00027 
00032 dmatrix solve(const dmatrix& aa, const dmatrix& tz)
00033 {
00034   double ln = 0;
00035   double sgn = 0;
00036   return solve(aa,tz,ln,sgn);
00037 }
00038 
00045 dmatrix solve(const dmatrix& aa,const dmatrix& tz,
00046   const double& _ln_unsigned_det,double& sign)
00047 {
00048   double& ln_unsigned_det = (double&)_ln_unsigned_det;
00049   int i,imax,j,k,n;
00050   n=aa.colsize();
00051   int lb=aa.colmin();
00052   int ub=aa.colmax();
00053   if (lb!=aa.rowmin()||ub!=aa.colmax())
00054   {
00055     cerr << "Error matrix not square in solve()"<<endl;
00056     ad_exit(1);
00057   }
00058   dmatrix bb(lb,ub,lb,ub);
00059   bb=aa;
00060   ivector indx(lb,ub);
00061   int One=1;
00062   indx.fill_seqadd(lb,One);
00063   double d;
00064   double big,dum,sum,temp;
00065   dvector vv(lb,ub);
00066 
00067   d=1.0;
00068   for (i=lb;i<=ub;i++)
00069   {
00070     big=0.0;
00071     for (j=lb;j<=ub;j++)
00072     {
00073       temp=fabs(bb(i,j));
00074       if (temp > big)
00075       {
00076         big=temp;
00077       }
00078     }
00079     if (big == 0.0)
00080     {
00081       cerr << "Error in matrix inverse -- matrix singular in inv(dmatrix)\n";
00082     }
00083     vv[i]=1.0/big;
00084   }
00085 
00086   for (j=lb;j<=ub;j++)
00087   {
00088     for (i=lb;i<j;i++)
00089     {
00090       sum=bb(i,j);
00091       for (k=lb;k<i;k++)
00092       {
00093         sum -= bb(i,k)*bb(k,j);
00094       }
00095       //a[i][j]=sum;
00096       bb(i,j)=sum;
00097     }
00098     big=0.0;
00099     for (i=j;i<=ub;i++)
00100     {
00101       sum=bb(i,j);
00102       for (k=lb;k<j;k++)
00103       {
00104         sum -= bb(i,k)*bb(k,j);
00105       }
00106       bb(i,j)=sum;
00107       dum=vv[i]*fabs(sum);
00108       if ( dum >= big)
00109       {
00110         big=dum;
00111         imax=i;
00112       }
00113     }
00114     if (j != imax)
00115     {
00116       for (k=lb;k<=ub;k++)
00117       {
00118         dum=bb(imax,k);
00119         bb(imax,k)=bb(j,k);
00120         bb(j,k)=dum;
00121       }
00122       d = -1.*d;
00123       vv[imax]=vv[j];
00124 
00125       //if (j<ub)
00126       {
00127         int itemp=indx(imax);
00128         indx(imax)=indx(j);
00129         indx(j)=itemp;
00130       }
00131       //cout << "indx= " <<indx<<endl;
00132     }
00133 
00134     if (bb(j,j) == 0.0)
00135     {
00136       bb(j,j)=TINY;
00137     }
00138 
00139     if (j != n)
00140     {
00141       dum=1.0/bb(j,j);
00142       for (i=j+1;i<=ub;i++)
00143       {
00144         bb(i,j) = bb(i,j) * dum;
00145       }
00146     }
00147   }
00148 
00149   // get the determinant
00150   sign=d;
00151   dvector part_prod(lb,ub);
00152   part_prod(lb)=log(fabs(bb(lb,lb)));
00153   if (bb(lb,lb)<0) sign=-sign;
00154   for (j=lb+1;j<=ub;j++)
00155   {
00156     if (bb(j,j)<0) sign=-sign;
00157     part_prod(j)=part_prod(j-1)+log(fabs(bb(j,j)));
00158   }
00159   ln_unsigned_det=part_prod(ub);
00160 
00161   dmatrix z=trans(tz);
00162   int mmin=z.indexmin();
00163   int mmax=z.indexmax();
00164   dmatrix x(mmin,mmax,lb,ub);
00165   //dvector x(lb,ub);
00166 
00167   dvector y(lb,ub);
00168   //int lb=rowmin;
00169   //int ub=rowmax;
00170   dmatrix& b=bb;
00171   ivector indxinv(lb,ub);
00172   for (i=lb;i<=ub;i++)
00173   {
00174     indxinv(indx(i))=i;
00175   }
00176   for (int kk=mmin;kk<=mmax;kk++)
00177   {
00178     for (i=lb;i<=ub;i++)
00179     {
00180       y(indxinv(i))=z(kk)(i);
00181     }
00182 
00183     for (i=lb;i<=ub;i++)
00184     {
00185       sum=y(i);
00186       for (int j=lb;j<=i-1;j++)
00187       {
00188         sum-=b(i,j)*y(j);
00189       }
00190       y(i)=sum;
00191     }
00192     for (i=ub;i>=lb;i--)
00193     {
00194       sum=y(i);
00195       for (int j=i+1;j<=ub;j++)
00196       {
00197         sum-=b(i,j)*x(kk)(j);
00198       }
00199       x(kk)(i)=sum/b(i,i);
00200     }
00201   }
00202   return trans(x);
00203 }
00204 
00205 double ln_det_choleski(
00206   const banded_symmetric_dmatrix& MM, const int& _ierr)
00207 {
00208   banded_lower_triangular_dmatrix tmp=choleski_decomp(MM,_ierr);
00209 
00210   int mmin=tmp.indexmin();
00211   int mmax=tmp.indexmax();
00212   double ld=0.0;
00213   for (int i=mmin;i<=mmax;i++)
00214   {
00215     ld+=log(tmp(i,i));
00216   }
00217   return 2.0*ld;
00218 }
00219 
00220 double norm(const banded_symmetric_dmatrix& B)
00221 {
00222   return sqrt(norm2(B));
00223 }
00224 
00225 double norm2(const banded_symmetric_dmatrix& B)
00226 {
00227   double nm=0.0;
00228   for (int i=1;i<=B.bw-1;i++)
00229   {
00230     nm+=norm2(B.d(i));
00231   }
00232   nm*=2;
00233   nm+=norm2(B.d(0));
00234   return nm;
00235 }
00236 
00237 #undef TINY
00238