home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / c / math_classes.lha / math_classes / matrix / libmatrix / determinant.cc next >
Encoding:
C/C++ Source or Header  |  1996-09-18  |  694 b   |  49 lines

  1. #include "Matrix.h"
  2.  
  3.  
  4. mtrxtype determinant(const Matrix& m1)
  5.     {
  6.     unsigned int k,i2,i,j;
  7.     unsigned int c;
  8.     unsigned int maxr;
  9.     mtrxtype maxv;
  10.     Matrix m(m1);
  11.     mtrxtype t1,t2,t3;
  12.     if(m1.rows()!=m1.cols())error("dimensions do not match");
  13.     c=1;
  14.     for(k=1;k<m.rows();k++)
  15.         {
  16.         maxr=k;
  17.         maxv=m(k,k);
  18.         for(i2=k;i2<=m.rows();i2++)
  19.             {
  20.             t3=m(k,i2);
  21.             if (abs(t3)>abs(maxv))
  22.                 {
  23.                 maxr=i2;
  24.                 maxv=t3;
  25.                 }
  26.             }
  27.         if (maxr!=k)
  28.             {
  29.             m.rowexchange(k,maxr);
  30.             c*=-1;
  31.             }
  32.         for(j=k+1;j<=m.rows();j++)
  33.             {
  34.             t1=m(j,k)/m(k,k);
  35.             for(i=k;i<=m.cols();i++)
  36.                 {
  37.                 t2=m(j,i)-t1*m(k,i);
  38.                 m(j,i)=t2;
  39.                 }
  40.             }
  41.         }
  42.     t1=1.0;
  43.     for(i=1;i<=m.cols();i++)
  44.         {
  45.         t1*=m(i,i);
  46.         }
  47.     return(t1*c);
  48.     }
  49.