home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / n / newmat06.zip / NEWMAT1.CXX < prev    next >
C/C++ Source or Header  |  1992-11-29  |  5KB  |  176 lines

  1. //$$ newmat1.cxx   Matrix type functions
  2.  
  3. // Copyright (C) 1991,2: R B Davies
  4.  
  5. //#define WANT_STREAM                     // to include stream package
  6.  
  7. #include "include.h"
  8.  
  9. #include "newmat.h"
  10.  
  11.  
  12.  
  13. /************************* MatrixType functions *****************************/
  14.  
  15.  
  16. int MatrixType::BestFit() const
  17. // given attributes find a matrix type that best fits them
  18. {
  19.  
  20.    if (!(attribute & Valid)) return USX;
  21.  
  22.    if (attribute & (LUDeco + OneRow + OneCol))
  23.    {
  24.       if (attribute & OneCol) return CVX;
  25.       if (attribute & OneRow) return RVX;
  26.       if (attribute & LUDeco) return (attribute & Band) ? BCX : CtX;
  27.    }
  28.  
  29.    if (attribute & Band)
  30.    {
  31.       if (attribute & Lower)
  32.      return (attribute & (Symmetric + Upper)) ? DgX : LBX;
  33.       if (attribute & Upper)
  34.      return (attribute & (Symmetric + Lower)) ? DgX : UBX;
  35.       if (attribute & Symmetric) return SBX;
  36.       return BMX;
  37.    }
  38.  
  39.    if (attribute & (Lower + Upper + Symmetric))
  40.    {
  41.       if (attribute & Lower)
  42.      return (attribute & (Symmetric + Upper)) ? DgX : LTX;
  43.       if (attribute & Upper)
  44.      return (attribute & (Symmetric + Lower)) ? DgX : UTX;
  45.       return SmX;
  46.    }
  47.  
  48.    return RtX;   
  49.  
  50. }
  51.  
  52. Boolean MatrixType::operator>=(const MatrixType& t) const
  53. {
  54.    return ( attribute & (t.attribute | (OneRow + OneCol)) ) == attribute;
  55. }
  56.  
  57. MatrixType MatrixType::operator+(const MatrixType& mt) const
  58. { return attribute & mt.attribute; }
  59.  
  60. MatrixType MatrixType::operator*(const MatrixType& mt) const
  61. {
  62.    if ( ((attribute & (Upper+Lower)) == (Upper+Lower))
  63.       && ((mt.attribute & (Upper+Lower)) == (Upper+Lower)) )
  64.          return Dg;
  65.    else return (attribute & mt.attribute & ~Symmetric)
  66.       | (attribute & OneRow) | (mt.attribute & OneCol);
  67. }
  68.  
  69. MatrixType MatrixType::i() const
  70. { return attribute & ~(Band+LUDeco) ; }
  71.  
  72. MatrixType MatrixType::t() const
  73. {
  74.    int a = attribute & ~(Upper+Lower+OneRow+OneCol);
  75.    if (attribute & Upper) a |= Lower;
  76.    if (attribute & Lower) a |= Upper;
  77.    if (attribute & OneRow) a |= OneCol;
  78.    if (attribute & OneCol) a |= OneRow;
  79.    return a;
  80. }
  81.  
  82. MatrixType MatrixType::AddEqualEl() const
  83. { return attribute & (Valid + Symmetric + OneRow + OneCol); }
  84.  
  85. MatrixType MatrixType::MultRHS() const
  86. {
  87.    if (attribute & (Upper + Lower)) return attribute;
  88.    else return attribute & ~Symmetric;
  89. }
  90.  
  91. MatrixType MatrixType::sub() const
  92. { return attribute & (Valid + OneRow + OneCol); }
  93.  
  94.  
  95. MatrixType MatrixType::ssub() const
  96. {
  97.    return *this;                  // won't work for selection matrix
  98. }
  99.  
  100. MatrixType::operator char*() const
  101. {
  102.    switch (BestFit())
  103.    {
  104.    case USX:  return "UnSp ";
  105.    case UTX:  return "UT   ";
  106.    case LTX:  return "LT   ";
  107.    case RtX:  return "Rect ";
  108.    case SmX:  return "Sym  ";
  109.    case DgX:  return "Diag ";
  110.    case RVX:  return "RowV ";
  111.    case CVX:  return "ColV ";
  112.    case BMX:  return "Band ";
  113.    case UBX:  return "UpBnd";
  114.    case LBX:  return "LwBnd";
  115.    case SBX:  return "SmBnd";
  116.    case CtX:  return "Crout";
  117.    case BCX:  return "BndLU";
  118.    }
  119.    return "?????";
  120. }
  121.  
  122. GeneralMatrix* MatrixType::New() const
  123. {
  124.    GeneralMatrix* gm;
  125.    switch (BestFit())
  126.    {
  127.    case USX:  Throw(CannotBuildException("UnSp"));
  128.    case UTX:  gm = new UpperTriangularMatrix; break;
  129.    case LTX:  gm = new LowerTriangularMatrix; break;
  130.    case RtX:  gm = new Matrix; break;
  131.    case SmX:  gm = new SymmetricMatrix; break;
  132.    case DgX:  gm = new DiagonalMatrix; break;
  133.    case RVX:  gm = new RowVector; break;
  134.    case CVX:  gm = new ColumnVector; break;
  135.    case BMX:  gm = new BandMatrix; break;
  136.    case UBX:  gm = new UpperBandMatrix; break;
  137.    case LBX:  gm = new LowerBandMatrix; break;
  138.    case SBX:  gm = new SymmetricBandMatrix; break;
  139.    case CtX:  Throw(CannotBuildException("Crout"));
  140.    case BCX:  Throw(CannotBuildException("Band LU"));
  141.    }
  142.    MatrixErrorNoSpace(gm);
  143.    gm->Protect(); return gm;
  144. }
  145.  
  146. GeneralMatrix* MatrixType::New(int nr, int nc, BaseMatrix* bm) const
  147. {
  148.    Tracer tr("New");
  149.    GeneralMatrix* gm;
  150.    switch (BestFit())
  151.    {
  152.    case USX:  Throw(CannotBuildException("UnSp"));
  153.    case UTX:  gm = new UpperTriangularMatrix(nr); break;
  154.    case LTX:  gm = new LowerTriangularMatrix(nr); break;
  155.    case RtX:  gm = new Matrix(nr, nc); break;
  156.    case SmX:  gm = new SymmetricMatrix(nr); break;
  157.    case DgX:  gm = new DiagonalMatrix(nr); break;
  158.    case RVX:  if (nr!=1) Throw(VectorException());
  159.            gm = new RowVector(nc); break;
  160.    case CVX:  if (nc!=1) Throw(VectorException());
  161.            gm = new ColumnVector(nr); break;
  162.    case BMX:  {
  163.                  MatrixBandWidth bw = bm->BandWidth();
  164.                  gm = new BandMatrix(nr,bw.lower,bw.upper); break;
  165.               }
  166.    case UBX:  gm = new UpperBandMatrix(nr,bm->BandWidth().upper); break;
  167.    case LBX:  gm = new LowerBandMatrix(nr,bm->BandWidth().lower); break;
  168.    case SBX:  gm = new SymmetricBandMatrix(nr,bm->BandWidth().lower); break;
  169.    case CtX:  Throw(CannotBuildException("Crout"));
  170.    case BCX:  Throw(CannotBuildException("Band LU"));
  171.    }
  172.    MatrixErrorNoSpace(gm);
  173.    gm->Protect(); return gm;
  174. }
  175.  
  176.