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

  1. //$$ submat.cxx                         submatrices
  2.  
  3. // Copyright (C) 1991,2: R B Davies
  4.  
  5. #include "include.h"
  6.  
  7. #include "newmat.h"
  8. #include "newmatrc.h"
  9.  
  10.  
  11. //#define REPORT { static ExeCounter ExeCount(__LINE__,6); ExeCount++; }
  12.  
  13. #define REPORT {}
  14.  
  15.  
  16. /****************************** submatrices *********************************/
  17.  
  18. #ifdef TEMPS_DESTROYED_QUICKLY
  19. GetSubMatrix& BaseMatrix::SubMatrix(int first_row, int last_row, int first_col,
  20.    int last_col) const
  21. #else
  22. GetSubMatrix BaseMatrix::SubMatrix(int first_row, int last_row, int first_col,
  23.    int last_col) const
  24. #endif
  25. {
  26.    REPORT
  27.    Tracer tr("SubMatrix");
  28.    int a = first_row - 1; int b = last_row - first_row + 1;
  29.    int c = first_col - 1; int d = last_col - first_col + 1;
  30.    if (a<0 || b<=0 || c<0 || d<=0) Throw(SubMatrixDimensionException());
  31. #ifdef TEMPS_DESTROYED_QUICKLY
  32.    GetSubMatrix* x = new GetSubMatrix(this, a, b, c, d, Type().sub());
  33.    MatrixErrorNoSpace(x);
  34.    return *x;
  35. #else
  36.    return GetSubMatrix(this, a, b, c, d, Type().sub());
  37. #endif
  38. }
  39.  
  40. #ifdef TEMPS_DESTROYED_QUICKLY
  41. GetSubMatrix& BaseMatrix::SymSubMatrix(int first_row, int last_row) const
  42. #else
  43. GetSubMatrix BaseMatrix::SymSubMatrix(int first_row, int last_row) const
  44. #endif
  45. {
  46.    REPORT
  47.    Tracer tr("SubMatrix(symmetric)");
  48.    int a = first_row - 1; int b = last_row - first_row + 1;
  49.    if (a<0 || b<=0) Throw(SubMatrixDimensionException());
  50. #ifdef TEMPS_DESTROYED_QUICKLY
  51.    GetSubMatrix* x = new GetSubMatrix(this, a, b, a, b, Type().ssub());
  52.    MatrixErrorNoSpace(x);
  53.    return *x;
  54. #else
  55.    return GetSubMatrix( this, a, b, a, b, Type().ssub());
  56. #endif
  57. }
  58.  
  59. #ifdef TEMPS_DESTROYED_QUICKLY
  60. GetSubMatrix& BaseMatrix::Row(int first_row) const
  61. #else
  62. GetSubMatrix BaseMatrix::Row(int first_row) const
  63. #endif
  64. {
  65.    REPORT
  66.    Tracer tr("SubMatrix(row)");
  67.    int a = first_row - 1;
  68.    if (a<0) Throw(SubMatrixDimensionException());
  69. #ifdef TEMPS_DESTROYED_QUICKLY
  70.    GetSubMatrix* x = new GetSubMatrix(this, a, 1, 0, -1, MatrixType::RV);
  71.    MatrixErrorNoSpace(x);
  72.    return *x;
  73. #else
  74.    return GetSubMatrix(this, a, 1, 0, -1, MatrixType::RV);
  75. #endif
  76. }
  77.  
  78. #ifdef TEMPS_DESTROYED_QUICKLY
  79. GetSubMatrix& BaseMatrix::Rows(int first_row, int last_row) const
  80. #else
  81. GetSubMatrix BaseMatrix::Rows(int first_row, int last_row) const
  82. #endif
  83. {
  84.    REPORT
  85.    Tracer tr("SubMatrix(rows)");
  86.    int a = first_row - 1; int b = last_row - first_row + 1;
  87.    if (a<0 || b<=0) Throw(SubMatrixDimensionException());
  88. #ifdef TEMPS_DESTROYED_QUICKLY
  89.    GetSubMatrix* x = new GetSubMatrix(this, a, b, 0, -1, MatrixType::RV);
  90.    MatrixErrorNoSpace(x);
  91.    return *x;
  92. #else
  93.    return GetSubMatrix(this, a, b, 0, -1, MatrixType::RV);
  94. #endif
  95. }
  96.  
  97. #ifdef TEMPS_DESTROYED_QUICKLY
  98. GetSubMatrix& BaseMatrix::Column(int first_col) const
  99. #else
  100. GetSubMatrix BaseMatrix::Column(int first_col) const
  101. #endif
  102. {
  103.    REPORT
  104.    Tracer tr("SubMatrix(column)");
  105.    int c = first_col - 1;
  106.    if (c<0) Throw(SubMatrixDimensionException());
  107. #ifdef TEMPS_DESTROYED_QUICKLY
  108.    GetSubMatrix* x = new GetSubMatrix(this, 0, -1, c, 1, MatrixType::CV);
  109.    MatrixErrorNoSpace(x);
  110.    return *x;
  111. #else
  112.    return GetSubMatrix(this, 0, -1, c, 1, MatrixType::CV);
  113. #endif
  114. }
  115.  
  116. #ifdef TEMPS_DESTROYED_QUICKLY
  117. GetSubMatrix& BaseMatrix::Columns(int first_col, int last_col) const
  118. #else
  119. GetSubMatrix BaseMatrix::Columns(int first_col, int last_col) const
  120. #endif
  121. {
  122.    REPORT
  123.    Tracer tr("SubMatrix(columns)");
  124.    int c = first_col - 1; int d = last_col - first_col + 1;
  125.    if (c<0 || d<=0) Throw(SubMatrixDimensionException());
  126. #ifdef TEMPS_DESTROYED_QUICKLY
  127.    GetSubMatrix* x = new GetSubMatrix(this, 0, -1, c, d, MatrixType::CV);
  128.    MatrixErrorNoSpace(x);
  129.    return *x;
  130. #else
  131.    return GetSubMatrix(this, 0, -1, c, d, MatrixType::CV);
  132. #endif
  133. }
  134.  
  135. void GetSubMatrix::SetUpLHS()
  136. {
  137.    REPORT
  138.    Tracer tr("SubMatrix(LHS)");
  139.    const BaseMatrix* bm1 = bm;
  140.    GeneralMatrix* gm = ((BaseMatrix*&)bm)->Evaluate();
  141.    if ((BaseMatrix*)gm!=bm1)
  142.       Throw(ProgramException("Invalid LHS"));
  143.    if (row_number < 0) row_number = gm->Nrows();
  144.    if (col_number < 0) col_number = gm->Ncols();
  145.    if (row_skip+row_number > gm->Nrows() || col_skip+col_number > gm->Ncols())
  146.       Throw(SubMatrixDimensionException());
  147. }
  148.  
  149. void GetSubMatrix::operator<<(const BaseMatrix& bmx)
  150. {
  151.    REPORT
  152.    Tracer tr("SubMatrix(<<)");
  153.    SetUpLHS(); GeneralMatrix* gmx = ((BaseMatrix&)bmx).Evaluate();
  154.    if (row_number != gmx->Nrows() || col_number != gmx->Ncols())
  155.       Throw(IncompatibleDimensionsException());
  156.    MatrixRow mrx(gmx, LoadOnEntry); 
  157.    MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
  158.                                   // do need LoadOnEntry
  159.    MatrixRowCol sub; int i = row_number;
  160.    while (i--)
  161.    {
  162.       mr.SubRowCol(sub, col_skip, col_number);   // put values in sub
  163.       sub.Copy(mrx); mr.Next(); mrx.Next();
  164.    }
  165.    gmx->tDelete();
  166. #ifdef TEMPS_DESTROYED_QUICKLY
  167.    delete this;
  168. #endif
  169. }   
  170.  
  171. void GetSubMatrix::operator<<(const Real* r)
  172. {
  173.    REPORT
  174.    Tracer tr("SubMatrix(<<Real*)");
  175.    SetUpLHS();
  176.    if (row_skip+row_number > gm->Nrows() || col_skip+col_number > gm->Ncols())
  177.       Throw(SubMatrixDimensionException());
  178.    MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
  179.                                   // do need LoadOnEntry
  180.    MatrixRowCol sub; int i = row_number;
  181.    while (i--)
  182.    {
  183.       mr.SubRowCol(sub, col_skip, col_number);   // put values in sub
  184.       sub.Copy(r); mr.Next();
  185.    }
  186. #ifdef TEMPS_DESTROYED_QUICKLY
  187.    delete this;
  188. #endif
  189. }   
  190.  
  191. void GetSubMatrix::operator<<(Real r)
  192. {
  193.    REPORT
  194.    Tracer tr("SubMatrix(<<Real)");
  195.    SetUpLHS();
  196.    MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
  197.                                   // do need LoadOnEntry
  198.    MatrixRowCol sub; int i = row_number;
  199.    while (i--)
  200.    {
  201.       mr.SubRowCol(sub, col_skip, col_number);   // put values in sub
  202.       sub.Copy(r); mr.Next();
  203.    }
  204. #ifdef TEMPS_DESTROYED_QUICKLY
  205.    delete this;
  206. #endif
  207. }   
  208.  
  209. void GetSubMatrix::Inject(const GeneralMatrix& gmx)
  210. {
  211.    REPORT
  212.    Tracer tr("SubMatrix(inject)");
  213.    SetUpLHS();
  214.    if (row_number != gmx.Nrows() || col_number != gmx.Ncols())
  215.       Throw(IncompatibleDimensionsException());
  216.    MatrixRow mrx((GeneralMatrix*)(&gmx), LoadOnEntry);
  217.    MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
  218.                                   // do need LoadOnEntry
  219.    MatrixRowCol sub; int i = row_number;
  220.    while (i--)
  221.    {
  222.       mr.SubRowCol(sub, col_skip, col_number);   // put values in sub
  223.       sub.Inject(mrx); mr.Next(); mrx.Next();
  224.    }
  225. #ifdef TEMPS_DESTROYED_QUICKLY
  226.    delete this;
  227. #endif
  228. }
  229.  
  230.