home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / sources / misc / 4254 / submat.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-11  |  7.4 KB  |  277 lines

  1. //$$ submat.cxx                         submatrices
  2.  
  3. // Copyright (C) 1991,2,3: 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, FALSE);
  33.    MatrixErrorNoSpace(x);
  34.    return *x;
  35. #else
  36.    return GetSubMatrix(this, a, b, c, d, FALSE);
  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, TRUE);
  52.    MatrixErrorNoSpace(x);
  53.    return *x;
  54. #else
  55.    return GetSubMatrix( this, a, b, a, b, TRUE);
  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, FALSE);
  71.    MatrixErrorNoSpace(x);
  72.    return *x;
  73. #else
  74.    return GetSubMatrix(this, a, 1, 0, -1, FALSE);
  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, FALSE);
  90.    MatrixErrorNoSpace(x);
  91.    return *x;
  92. #else
  93.    return GetSubMatrix(this, a, b, 0, -1, FALSE);
  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, FALSE);
  109.    MatrixErrorNoSpace(x);
  110.    return *x;
  111. #else
  112.    return GetSubMatrix(this, 0, -1, c, 1, FALSE);
  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, FALSE);
  128.    MatrixErrorNoSpace(x);
  129.    return *x;
  130. #else
  131.    return GetSubMatrix(this, 0, -1, c, d, FALSE);
  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(<<)"); GeneralMatrix* gmx = 0;
  153.    Try
  154.    {
  155.       SetUpLHS(); gmx = ((BaseMatrix&)bmx).Evaluate();
  156.       if (row_number != gmx->Nrows() || col_number != gmx->Ncols())
  157.          Throw(IncompatibleDimensionsException());
  158.       MatrixRow mrx(gmx, LoadOnEntry); 
  159.       MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
  160.                                      // do need LoadOnEntry
  161.       MatrixRowCol sub; int i = row_number;
  162.       while (i--)
  163.       {
  164.          mr.SubRowCol(sub, col_skip, col_number);   // put values in sub
  165.          sub.Copy(mrx); mr.Next(); mrx.Next();
  166.       }
  167.       gmx->tDelete();
  168. #ifdef TEMPS_DESTROYED_QUICKLY
  169.       delete this;
  170. #endif
  171.    }   
  172.  
  173.    CatchAll
  174.    {
  175.       if (gmx) gmx->tDelete();
  176. #ifdef TEMPS_DESTROYED_QUICKLY
  177.       delete this;
  178. #endif
  179.       Throw();
  180.    }
  181. }
  182.  
  183. void GetSubMatrix::operator=(const BaseMatrix& bmx)
  184. {
  185.    REPORT
  186.    Tracer tr("SubMatrix(=)"); GeneralMatrix* gmx = 0;
  187.    MatrixConversionCheck mcc;         // Check for loss of info
  188.    Try
  189.    {
  190.       SetUpLHS(); gmx = ((BaseMatrix&)bmx).Evaluate();
  191.       if (row_number != gmx->Nrows() || col_number != gmx->Ncols())
  192.          Throw(IncompatibleDimensionsException());
  193.       MatrixRow mrx(gmx, LoadOnEntry); 
  194.       MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
  195.                                      // do need LoadOnEntry
  196.       MatrixRowCol sub; int i = row_number;
  197.       while (i--)
  198.       {
  199.          mr.SubRowCol(sub, col_skip, col_number);   // put values in sub
  200.          sub.CopyCheck(mrx); mr.Next(); mrx.Next();
  201.       }
  202.       gmx->tDelete();
  203. #ifdef TEMPS_DESTROYED_QUICKLY
  204.       delete this;
  205. #endif
  206.    }   
  207.  
  208.    CatchAll
  209.    {
  210.       if (gmx) gmx->tDelete();
  211. #ifdef TEMPS_DESTROYED_QUICKLY
  212.       delete this;
  213. #endif
  214.       Throw();
  215.    }
  216. }
  217.  
  218. void GetSubMatrix::operator<<(const Real* r)
  219. {
  220.    REPORT
  221.    Tracer tr("SubMatrix(<<Real*)");
  222.    SetUpLHS();
  223.    if (row_skip+row_number > gm->Nrows() || col_skip+col_number > gm->Ncols())
  224.       Throw(SubMatrixDimensionException());
  225.    MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
  226.                                   // do need LoadOnEntry
  227.    MatrixRowCol sub; int i = row_number;
  228.    while (i--)
  229.    {
  230.       mr.SubRowCol(sub, col_skip, col_number);   // put values in sub
  231.       sub.Copy(r); mr.Next();
  232.    }
  233. #ifdef TEMPS_DESTROYED_QUICKLY
  234.    delete this;
  235. #endif
  236. }   
  237.  
  238. void GetSubMatrix::operator=(Real r)
  239. {
  240.    REPORT
  241.    Tracer tr("SubMatrix(=Real)");
  242.    SetUpLHS();
  243.    MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
  244.                                   // do need LoadOnEntry
  245.    MatrixRowCol sub; int i = row_number;
  246.    while (i--)
  247.    {
  248.       mr.SubRowCol(sub, col_skip, col_number);   // put values in sub
  249.       sub.Copy(r); mr.Next();
  250.    }
  251. #ifdef TEMPS_DESTROYED_QUICKLY
  252.    delete this;
  253. #endif
  254. }   
  255.  
  256. void GetSubMatrix::Inject(const GeneralMatrix& gmx)
  257. {
  258.    REPORT
  259.    Tracer tr("SubMatrix(inject)");
  260.    SetUpLHS();
  261.    if (row_number != gmx.Nrows() || col_number != gmx.Ncols())
  262.       Throw(IncompatibleDimensionsException());
  263.    MatrixRow mrx((GeneralMatrix*)(&gmx), LoadOnEntry);
  264.    MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
  265.                                   // do need LoadOnEntry
  266.    MatrixRowCol sub; int i = row_number;
  267.    while (i--)
  268.    {
  269.       mr.SubRowCol(sub, col_skip, col_number);   // put values in sub
  270.       sub.Inject(mrx); mr.Next(); mrx.Next();
  271.    }
  272. #ifdef TEMPS_DESTROYED_QUICKLY
  273.    delete this;
  274. #endif
  275. }
  276.  
  277.