00001
00002
00003
00004
00005 #include "include.h"
00006
00007 #include "newmat.h"
00008 #include "newmatrc.h"
00009
00010 #ifdef use_namespace
00011 namespace NEWMAT {
00012 #endif
00013
00014 #ifdef DO_REPORT
00015 #define REPORT { static ExeCounter ExeCount(__LINE__,11); ++ExeCount; }
00016 #else
00017 #define REPORT {}
00018 #endif
00019
00020
00021
00022
00023 GetSubMatrix BaseMatrix::SubMatrix(int first_row, int last_row, int first_col,
00024 int last_col) const
00025 {
00026 REPORT
00027 Tracer tr("SubMatrix");
00028 int a = first_row - 1; int b = last_row - first_row + 1;
00029 int c = first_col - 1; int d = last_col - first_col + 1;
00030 if (a<0 || b<0 || c<0 || d<0) Throw(SubMatrixDimensionException());
00031
00032 return GetSubMatrix(this, a, b, c, d, false);
00033 }
00034
00035 GetSubMatrix BaseMatrix::SymSubMatrix(int first_row, int last_row) const
00036 {
00037 REPORT
00038 Tracer tr("SubMatrix(symmetric)");
00039 int a = first_row - 1; int b = last_row - first_row + 1;
00040 if (a<0 || b<0) Throw(SubMatrixDimensionException());
00041
00042 return GetSubMatrix( this, a, b, a, b, true);
00043 }
00044
00045 GetSubMatrix BaseMatrix::Row(int first_row) const
00046 {
00047 REPORT
00048 Tracer tr("SubMatrix(row)");
00049 int a = first_row - 1;
00050 if (a<0) Throw(SubMatrixDimensionException());
00051 return GetSubMatrix(this, a, 1, 0, -1, false);
00052 }
00053
00054 GetSubMatrix BaseMatrix::Rows(int first_row, int last_row) const
00055 {
00056 REPORT
00057 Tracer tr("SubMatrix(rows)");
00058 int a = first_row - 1; int b = last_row - first_row + 1;
00059 if (a<0 || b<0) Throw(SubMatrixDimensionException());
00060
00061 return GetSubMatrix(this, a, b, 0, -1, false);
00062 }
00063
00064 GetSubMatrix BaseMatrix::Column(int first_col) const
00065 {
00066 REPORT
00067 Tracer tr("SubMatrix(column)");
00068 int c = first_col - 1;
00069 if (c<0) Throw(SubMatrixDimensionException());
00070 return GetSubMatrix(this, 0, -1, c, 1, false);
00071 }
00072
00073 GetSubMatrix BaseMatrix::Columns(int first_col, int last_col) const
00074 {
00075 REPORT
00076 Tracer tr("SubMatrix(columns)");
00077 int c = first_col - 1; int d = last_col - first_col + 1;
00078 if (c<0 || d<0) Throw(SubMatrixDimensionException());
00079
00080 return GetSubMatrix(this, 0, -1, c, d, false);
00081 }
00082
00083 void GetSubMatrix::SetUpLHS()
00084 {
00085 REPORT
00086 Tracer tr("SubMatrix(LHS)");
00087 const BaseMatrix* bm1 = bm;
00088 GeneralMatrix* gm1 = (const_cast<BaseMatrix*&>(bm))->Evaluate();
00089 if ((BaseMatrix*)gm1!=bm1)
00090 Throw(ProgramException("Invalid LHS"));
00091 if (row_number < 0) row_number = gm1->Nrows();
00092 if (col_number < 0) col_number = gm1->Ncols();
00093 if (row_skip+row_number > gm1->Nrows()
00094 || col_skip+col_number > gm1->Ncols())
00095 Throw(SubMatrixDimensionException());
00096 }
00097
00098 void GetSubMatrix::operator<<(const BaseMatrix& bmx)
00099 {
00100 REPORT
00101 Tracer tr("SubMatrix(<<)"); GeneralMatrix* gmx = 0;
00102 Try
00103 {
00104 SetUpLHS(); gmx = (const_cast<BaseMatrix&>(bmx)).Evaluate();
00105 if (row_number != gmx->Nrows() || col_number != gmx->Ncols())
00106 Throw(IncompatibleDimensionsException());
00107 MatrixRow mrx(gmx, LoadOnEntry);
00108 MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
00109
00110 MatrixRowCol sub; int i = row_number;
00111 while (i--)
00112 {
00113 mr.SubRowCol(sub, col_skip, col_number);
00114 sub.Copy(mrx); mr.Next(); mrx.Next();
00115 }
00116 gmx->tDelete();
00117 }
00118
00119 CatchAll
00120 {
00121 if (gmx) gmx->tDelete();
00122 ReThrow;
00123 }
00124 }
00125
00126 void GetSubMatrix::operator=(const BaseMatrix& bmx)
00127 {
00128 REPORT
00129 Tracer tr("SubMatrix(=)"); GeneralMatrix* gmx = 0;
00130
00131 Try
00132 {
00133 SetUpLHS(); gmx = (const_cast<BaseMatrix&>(bmx)).Evaluate();
00134 if (row_number != gmx->Nrows() || col_number != gmx->Ncols())
00135 Throw(IncompatibleDimensionsException());
00136 LoadAndStoreFlag lasf =
00137 ( row_skip == col_skip
00138 && gm->Type().IsSymmetric()
00139 && gmx->Type().IsSymmetric() )
00140 ? LoadOnEntry+DirectPart
00141 : LoadOnEntry;
00142 MatrixRow mrx(gmx, lasf);
00143 MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
00144
00145 MatrixRowCol sub; int i = row_number;
00146 while (i--)
00147 {
00148 mr.SubRowCol(sub, col_skip, col_number);
00149 sub.CopyCheck(mrx); mr.Next(); mrx.Next();
00150 }
00151 gmx->tDelete();
00152 }
00153
00154 CatchAll
00155 {
00156 if (gmx) gmx->tDelete();
00157 ReThrow;
00158 }
00159 }
00160
00161 void GetSubMatrix::operator<<(const Real* r)
00162 {
00163 REPORT
00164 Tracer tr("SubMatrix(<<Real*)");
00165 SetUpLHS();
00166 if (row_skip+row_number > gm->Nrows() || col_skip+col_number > gm->Ncols())
00167 Throw(SubMatrixDimensionException());
00168 MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
00169
00170 MatrixRowCol sub; int i = row_number;
00171 while (i--)
00172 {
00173 mr.SubRowCol(sub, col_skip, col_number);
00174 sub.Copy(r); mr.Next();
00175 }
00176 }
00177
00178 void GetSubMatrix::operator<<(const int* r)
00179 {
00180 REPORT
00181 Tracer tr("SubMatrix(<<int*)");
00182 SetUpLHS();
00183 if (row_skip+row_number > gm->Nrows() || col_skip+col_number > gm->Ncols())
00184 Throw(SubMatrixDimensionException());
00185 MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
00186
00187 MatrixRowCol sub; int i = row_number;
00188 while (i--)
00189 {
00190 mr.SubRowCol(sub, col_skip, col_number);
00191 sub.Copy(r); mr.Next();
00192 }
00193 }
00194
00195 void GetSubMatrix::operator=(Real r)
00196 {
00197 REPORT
00198 Tracer tr("SubMatrix(=Real)");
00199 SetUpLHS();
00200 MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
00201
00202 MatrixRowCol sub; int i = row_number;
00203 while (i--)
00204 {
00205 mr.SubRowCol(sub, col_skip, col_number);
00206 sub.Copy(r); mr.Next();
00207 }
00208 }
00209
00210 void GetSubMatrix::Inject(const GeneralMatrix& gmx)
00211 {
00212 REPORT
00213 Tracer tr("SubMatrix(inject)");
00214 SetUpLHS();
00215 if (row_number != gmx.Nrows() || col_number != gmx.Ncols())
00216 Throw(IncompatibleDimensionsException());
00217 MatrixRow mrx(const_cast<GeneralMatrix*>(&gmx), LoadOnEntry);
00218 MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
00219
00220 MatrixRowCol sub; int i = row_number;
00221 while (i--)
00222 {
00223 mr.SubRowCol(sub, col_skip, col_number);
00224 sub.Inject(mrx); mr.Next(); mrx.Next();
00225 }
00226 }
00227
00228 void GetSubMatrix::operator+=(const BaseMatrix& bmx)
00229 {
00230 REPORT
00231 Tracer tr("SubMatrix(+=)"); GeneralMatrix* gmx = 0;
00232
00233 Try
00234 {
00235 SetUpLHS(); gmx = (const_cast<BaseMatrix&>(bmx)).Evaluate();
00236 if (row_number != gmx->Nrows() || col_number != gmx->Ncols())
00237 Throw(IncompatibleDimensionsException());
00238 MatrixRow mrx(gmx, LoadOnEntry);
00239 MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
00240
00241 MatrixRowCol sub; int i = row_number;
00242 while (i--)
00243 {
00244 mr.SubRowCol(sub, col_skip, col_number);
00245 sub.Check(mrx);
00246 sub.Add(mrx); mr.Next(); mrx.Next();
00247 }
00248 gmx->tDelete();
00249 }
00250
00251 CatchAll
00252 {
00253 if (gmx) gmx->tDelete();
00254 ReThrow;
00255 }
00256 }
00257
00258 void GetSubMatrix::operator-=(const BaseMatrix& bmx)
00259 {
00260 REPORT
00261 Tracer tr("SubMatrix(-=)"); GeneralMatrix* gmx = 0;
00262
00263 Try
00264 {
00265 SetUpLHS(); gmx = (const_cast<BaseMatrix&>(bmx)).Evaluate();
00266 if (row_number != gmx->Nrows() || col_number != gmx->Ncols())
00267 Throw(IncompatibleDimensionsException());
00268 MatrixRow mrx(gmx, LoadOnEntry);
00269 MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
00270
00271 MatrixRowCol sub; int i = row_number;
00272 while (i--)
00273 {
00274 mr.SubRowCol(sub, col_skip, col_number);
00275 sub.Check(mrx);
00276 sub.Sub(mrx); mr.Next(); mrx.Next();
00277 }
00278 gmx->tDelete();
00279 }
00280
00281 CatchAll
00282 {
00283 if (gmx) gmx->tDelete();
00284 ReThrow;
00285 }
00286 }
00287
00288 void GetSubMatrix::operator+=(Real r)
00289 {
00290 REPORT
00291 Tracer tr("SubMatrix(+= or -= Real)");
00292
00293 Try
00294 {
00295 SetUpLHS();
00296 MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
00297
00298 MatrixRowCol sub; int i = row_number;
00299 while (i--)
00300 {
00301 mr.SubRowCol(sub, col_skip, col_number);
00302 sub.Check();
00303 sub.Add(r); mr.Next();
00304 }
00305 }
00306
00307 CatchAll
00308 {
00309 ReThrow;
00310 }
00311 }
00312
00313 void GetSubMatrix::operator*=(Real r)
00314 {
00315 REPORT
00316 Tracer tr("SubMatrix(*= or /= Real)");
00317
00318 Try
00319 {
00320 SetUpLHS();
00321 MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip);
00322
00323 MatrixRowCol sub; int i = row_number;
00324 while (i--)
00325 {
00326 mr.SubRowCol(sub, col_skip, col_number);
00327 sub.Multiply(r); mr.Next();
00328 }
00329 }
00330
00331 CatchAll
00332 {
00333 ReThrow;
00334 }
00335 }
00336
00337 #ifdef use_namespace
00338 }
00339 #endif
00340