00001
00002
00003
00004
00005 #define WANT_MATH // include.h will get math fns
00006
00007
00008
00009 #include "include.h"
00010
00011 #include "newmat.h"
00012 #include "newmatrc.h"
00013
00014 #ifdef use_namespace
00015 namespace NEWMAT {
00016 #endif
00017
00018
00019
00020 #ifdef DO_REPORT
00021 #define REPORT { static ExeCounter ExeCount(__LINE__,10); ++ExeCount; }
00022 #else
00023 #define REPORT {}
00024 #endif
00025
00026 static inline int my_min(int x, int y) { return x < y ? x : y; }
00027 static inline int my_max(int x, int y) { return x > y ? x : y; }
00028
00029
00030 BandMatrix::BandMatrix(const BaseMatrix& M)
00031 {
00032 REPORT
00033
00034 GeneralMatrix* gmx=((BaseMatrix&)M).Evaluate(MatrixType::BM);
00035 GetMatrix(gmx); CornerClear();
00036 }
00037
00038 void BandMatrix::SetParameters(const GeneralMatrix* gmx)
00039 {
00040 REPORT
00041 MatrixBandWidth bw = gmx->BandWidth();
00042 lower = bw.lower; upper = bw.upper;
00043 }
00044
00045 void BandMatrix::ReSize(int n, int lb, int ub)
00046 {
00047 REPORT
00048 Tracer tr("BandMatrix::ReSize");
00049 if (lb<0 || ub<0) Throw(ProgramException("Undefined bandwidth"));
00050 lower = (lb<=n) ? lb : n-1; upper = (ub<=n) ? ub : n-1;
00051 GeneralMatrix::ReSize(n,n,n*(lower+1+upper)); CornerClear();
00052 }
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 short BandMatrix::SimpleAddOK(const GeneralMatrix* gm)
00064 {
00065 const BandMatrix* bm = (const BandMatrix*)gm;
00066 if (bm->lower == lower && bm->upper == upper) { REPORT return 0; }
00067 else if (bm->lower >= lower && bm->upper >= upper) { REPORT return 1; }
00068 else if (bm->lower <= lower && bm->upper <= upper) { REPORT return 2; }
00069 else { REPORT return 3; }
00070 }
00071
00072 short SymmetricBandMatrix::SimpleAddOK(const GeneralMatrix* gm)
00073 {
00074 const SymmetricBandMatrix* bm = (const SymmetricBandMatrix*)gm;
00075 if (bm->lower == lower) { REPORT return 0; }
00076 else if (bm->lower > lower) { REPORT return 1; }
00077 else { REPORT return 2; }
00078 }
00079
00080 void UpperBandMatrix::ReSize(int n, int lb, int ub)
00081 {
00082 REPORT
00083 if (lb != 0)
00084 {
00085 Tracer tr("UpperBandMatrix::ReSize");
00086 Throw(ProgramException("UpperBandMatrix with non-zero lower band" ));
00087 }
00088 BandMatrix::ReSize(n, lb, ub);
00089 }
00090
00091 void LowerBandMatrix::ReSize(int n, int lb, int ub)
00092 {
00093 REPORT
00094 if (ub != 0)
00095 {
00096 Tracer tr("LowerBandMatrix::ReSize");
00097 Throw(ProgramException("LowerBandMatrix with non-zero upper band" ));
00098 }
00099 BandMatrix::ReSize(n, lb, ub);
00100 }
00101
00102 void BandMatrix::ReSize(const GeneralMatrix& A)
00103 {
00104 REPORT
00105 int n = A.Nrows();
00106 if (n != A.Ncols())
00107 {
00108 Tracer tr("BandMatrix::ReSize(GM)");
00109 Throw(NotSquareException(*this));
00110 }
00111 MatrixBandWidth mbw = A.BandWidth();
00112 ReSize(n, mbw.Lower(), mbw.Upper());
00113 }
00114
00115 bool BandMatrix::SameStorageType(const GeneralMatrix& A) const
00116 {
00117 if (Type() != A.Type()) { REPORT return false; }
00118 REPORT
00119 return BandWidth() == A.BandWidth();
00120 }
00121
00122 void BandMatrix::ReSizeForAdd(const GeneralMatrix& A, const GeneralMatrix& B)
00123 {
00124 REPORT
00125 Tracer tr("BandMatrix::ReSizeForAdd");
00126 MatrixBandWidth A_BW = A.BandWidth(); MatrixBandWidth B_BW = B.BandWidth();
00127 if ((A_BW.Lower() < 0) | (A_BW.Upper() < 0) | (B_BW.Lower() < 0)
00128 | (A_BW.Upper() < 0))
00129 Throw(ProgramException("Can't ReSize to BandMatrix" ));
00130
00131 ReSize(A.Nrows(), my_max(A_BW.Lower(), B_BW.Lower()),
00132 my_max(A_BW.Upper(), B_BW.Upper()));
00133 }
00134
00135 void BandMatrix::ReSizeForSP(const GeneralMatrix& A, const GeneralMatrix& B)
00136 {
00137 REPORT
00138 Tracer tr("BandMatrix::ReSizeForSP");
00139 MatrixBandWidth A_BW = A.BandWidth(); MatrixBandWidth B_BW = B.BandWidth();
00140 if ((A_BW.Lower() < 0) | (A_BW.Upper() < 0) | (B_BW.Lower() < 0)
00141 | (A_BW.Upper() < 0))
00142 Throw(ProgramException("Can't ReSize to BandMatrix" ));
00143
00144 ReSize(A.Nrows(), my_min(A_BW.Lower(), B_BW.Lower()),
00145 my_min(A_BW.Upper(), B_BW.Upper()));
00146 }
00147
00148
00149 void BandMatrix::operator=(const BaseMatrix& X)
00150 {
00151 REPORT
00152
00153 Eq(X,MatrixType::BM); CornerClear();
00154 }
00155
00156 void BandMatrix::CornerClear() const
00157 {
00158
00159 REPORT
00160 int i = lower; Real* s = store; int bw = lower + 1 + upper;
00161 while (i)
00162 { int j = i--; Real* sj = s; s += bw; while (j--) *sj++ = 0.0; }
00163 i = upper; s = store + storage;
00164 while (i)
00165 { int j = i--; Real* sj = s; s -= bw; while (j--) *(--sj) = 0.0; }
00166 }
00167
00168 MatrixBandWidth MatrixBandWidth::operator+(const MatrixBandWidth& bw) const
00169 {
00170 REPORT
00171 int l = bw.lower; int u = bw.upper;
00172 l = (lower < 0 || l < 0) ? -1 : (lower > l) ? lower : l;
00173 u = (upper < 0 || u < 0) ? -1 : (upper > u) ? upper : u;
00174 return MatrixBandWidth(l,u);
00175 }
00176
00177 MatrixBandWidth MatrixBandWidth::operator*(const MatrixBandWidth& bw) const
00178 {
00179 REPORT
00180 int l = bw.lower; int u = bw.upper;
00181 l = (lower < 0 || l < 0) ? -1 : lower+l;
00182 u = (upper < 0 || u < 0) ? -1 : upper+u;
00183 return MatrixBandWidth(l,u);
00184 }
00185
00186 MatrixBandWidth MatrixBandWidth::minimum(const MatrixBandWidth& bw) const
00187 {
00188 REPORT
00189 int l = bw.lower; int u = bw.upper;
00190 if ((lower >= 0) && ( (l < 0) || (l > lower) )) l = lower;
00191 if ((upper >= 0) && ( (u < 0) || (u > upper) )) u = upper;
00192 return MatrixBandWidth(l,u);
00193 }
00194
00195 UpperBandMatrix::UpperBandMatrix(const BaseMatrix& M)
00196 {
00197 REPORT
00198
00199 GeneralMatrix* gmx=((BaseMatrix&)M).Evaluate(MatrixType::UB);
00200 GetMatrix(gmx); CornerClear();
00201 }
00202
00203 void UpperBandMatrix::operator=(const BaseMatrix& X)
00204 {
00205 REPORT
00206
00207 Eq(X,MatrixType::UB); CornerClear();
00208 }
00209
00210 LowerBandMatrix::LowerBandMatrix(const BaseMatrix& M)
00211 {
00212 REPORT
00213
00214 GeneralMatrix* gmx=((BaseMatrix&)M).Evaluate(MatrixType::LB);
00215 GetMatrix(gmx); CornerClear();
00216 }
00217
00218 void LowerBandMatrix::operator=(const BaseMatrix& X)
00219 {
00220 REPORT
00221
00222 Eq(X,MatrixType::LB); CornerClear();
00223 }
00224
00225 BandLUMatrix::BandLUMatrix(const BaseMatrix& m)
00226 {
00227 REPORT
00228 Tracer tr("BandLUMatrix");
00229 storage2 = 0; store2 = 0;
00230 GeneralMatrix* gm = ((BaseMatrix&)m).Evaluate(MatrixType::BM);
00231 m1 = ((BandMatrix*)gm)->lower; m2 = ((BandMatrix*)gm)->upper;
00232 GetMatrix(gm);
00233 if (nrows!=ncols) Throw(NotSquareException(*this));
00234 d = true; sing = false;
00235 indx = new int [nrows]; MatrixErrorNoSpace(indx);
00236 MONITOR_INT_NEW("Index (BndLUMat)",nrows,indx)
00237 storage2 = nrows * m1;
00238 store2 = new Real [storage2]; MatrixErrorNoSpace(store2);
00239 MONITOR_REAL_NEW("Make (BandLUMat)",storage2,store2)
00240 ludcmp();
00241 }
00242
00243 BandLUMatrix::~BandLUMatrix()
00244 {
00245 REPORT
00246 MONITOR_INT_DELETE("Index (BndLUMat)",nrows,indx)
00247 MONITOR_REAL_DELETE("Delete (BndLUMt)",storage2,store2)
00248 delete [] indx; delete [] store2;
00249 }
00250
00251 MatrixType BandLUMatrix::Type() const { REPORT return MatrixType::BC; }
00252
00253
00254 LogAndSign BandLUMatrix::LogDeterminant() const
00255 {
00256 REPORT
00257 if (sing) return 0.0;
00258 Real* a = store; int w = m1+1+m2; LogAndSign sum; int i = nrows;
00259
00260 if (i) for (;;) { sum *= *a; if (!(--i)) break; a += w; }
00261 if (!d) sum.ChangeSign(); return sum;
00262 }
00263
00264 GeneralMatrix* BandMatrix::MakeSolver()
00265 {
00266 REPORT
00267 GeneralMatrix* gm = new BandLUMatrix(*this);
00268 MatrixErrorNoSpace(gm); gm->ReleaseAndDelete(); return gm;
00269 }
00270
00271
00272 void BandLUMatrix::ludcmp()
00273 {
00274 REPORT
00275 Real* a = store2; int i = storage2;
00276
00277
00278 while (i--) *a++ = 0.0;
00279 a = store;
00280 i = m1; int j = m2; int k; int n = nrows; int w = m1 + 1 + m2;
00281 while (i)
00282 {
00283 Real* ai = a + i;
00284 k = ++j; while (k--) *a++ = *ai++;
00285 k = i--; while (k--) *a++ = 0.0;
00286 }
00287
00288 a = store; int l = m1;
00289 for (k=0; k<n; k++)
00290 {
00291 Real x = *a; i = k; Real* aj = a;
00292 if (l < n) l++;
00293 for (j=k+1; j<l; j++)
00294 { aj += w; if (fabs(x) < fabs(*aj)) { x = *aj; i = j; } }
00295 indx[k] = i;
00296 if (x==0) { sing = true; return; }
00297 if (i!=k)
00298 {
00299 d = !d; Real* ak = a; Real* ai = store + i * w; j = w;
00300 while (j--) { x = *ak; *ak++ = *ai; *ai++ = x; }
00301 }
00302 aj = a + w; Real* m = store2 + m1 * k;
00303 for (j=k+1; j<l; j++)
00304 {
00305 *m++ = x = *aj / *a; i = w; Real* ak = a;
00306 while (--i) { Real* aj1 = aj++; *aj1 = *aj - x * *(++ak); }
00307 *aj++ = 0.0;
00308 }
00309 a += w;
00310 }
00311 }
00312
00313 void BandLUMatrix::lubksb(Real* B, int mini)
00314 {
00315 REPORT
00316 Tracer tr("BandLUMatrix::lubksb");
00317 if (sing) Throw(SingularException(*this));
00318 int n = nrows; int l = m1; int w = m1 + 1 + m2;
00319
00320 for (int k=0; k<n; k++)
00321 {
00322 int i = indx[k];
00323 if (i!=k) { Real x=B[k]; B[k]=B[i]; B[i]=x; }
00324 if (l<n) l++;
00325 Real* m = store2 + k*m1; Real* b = B+k; Real* bi = b;
00326 for (i=k+1; i<l; i++) *(++bi) -= *m++ * *b;
00327 }
00328
00329 l = -m1;
00330 for (int i = n-1; i>=mini; i--)
00331 {
00332 Real* b = B + i; Real* bk = b; Real x = *bk;
00333 Real* a = store + w*i; Real y = *a;
00334 int k = l+m1; while (k--) x -= *(++a) * *(++bk);
00335 *b = x / y;
00336 if (l < m2) l++;
00337 }
00338 }
00339
00340 void BandLUMatrix::Solver(MatrixColX& mcout, const MatrixColX& mcin)
00341 {
00342 REPORT
00343 int i = mcin.skip; Real* el = mcin.data-i; Real* el1=el;
00344 while (i--) *el++ = 0.0;
00345 el += mcin.storage; i = nrows - mcin.skip - mcin.storage;
00346 while (i--) *el++ = 0.0;
00347 lubksb(el1, mcout.skip);
00348 }
00349
00350
00351
00352
00353 void UpperBandMatrix::Solver(MatrixColX& mcout,
00354 const MatrixColX& mcin)
00355 {
00356 REPORT
00357 int i = mcin.skip-mcout.skip; Real* elx = mcin.data-i;
00358 while (i-- > 0) *elx++ = 0.0;
00359 int nr = mcin.skip+mcin.storage;
00360 elx = mcin.data+mcin.storage; Real* el = elx;
00361 int j = mcout.skip+mcout.storage-nr; i = nr-mcout.skip;
00362 while (j-- > 0) *elx++ = 0.0;
00363
00364 Real* Ael = store + (upper+1)*(i-1)+1; j = 0;
00365 if (i > 0) for(;;)
00366 {
00367 elx = el; Real sum = 0.0; int jx = j;
00368 while (jx--) sum += *(--Ael) * *(--elx);
00369 elx--; *elx = (*elx - sum) / *(--Ael);
00370 if (--i <= 0) break;
00371 if (j<upper) Ael -= upper - (++j); else el--;
00372 }
00373 }
00374
00375 void LowerBandMatrix::Solver(MatrixColX& mcout,
00376 const MatrixColX& mcin)
00377 {
00378 REPORT
00379 int i = mcin.skip-mcout.skip; Real* elx = mcin.data-i;
00380 while (i-- > 0) *elx++ = 0.0;
00381 int nc = mcin.skip; i = nc+mcin.storage; elx = mcin.data+mcin.storage;
00382 int nr = mcout.skip+mcout.storage; int j = nr-i; i = nr-nc;
00383 while (j-- > 0) *elx++ = 0.0;
00384
00385 Real* el = mcin.data; Real* Ael = store + (lower+1)*nc + lower; j = 0;
00386 if (i > 0) for(;;)
00387 {
00388 elx = el; Real sum = 0.0; int jx = j;
00389 while (jx--) sum += *Ael++ * *elx++;
00390 *elx = (*elx - sum) / *Ael++;
00391 if (--i <= 0) break;
00392 if (j<lower) Ael += lower - (++j); else el++;
00393 }
00394 }
00395
00396
00397 LogAndSign BandMatrix::LogDeterminant() const
00398 {
00399 REPORT
00400 BandLUMatrix C(*this); return C.LogDeterminant();
00401 }
00402
00403 LogAndSign LowerBandMatrix::LogDeterminant() const
00404 {
00405 REPORT
00406 int i = nrows; LogAndSign sum; Real* s = store + lower; int j = lower + 1;
00407
00408 if (i) for (;;) { sum *= *s; if (!(--i)) break; s += j; }
00409 ((GeneralMatrix&)*this).tDelete(); return sum;
00410 }
00411
00412 LogAndSign UpperBandMatrix::LogDeterminant() const
00413 {
00414 REPORT
00415 int i = nrows; LogAndSign sum; Real* s = store; int j = upper + 1;
00416
00417 if (i) for (;;) { sum *= *s; if (!(--i)) break; s += j; }
00418 ((GeneralMatrix&)*this).tDelete(); return sum;
00419 }
00420
00421 GeneralMatrix* SymmetricBandMatrix::MakeSolver()
00422 {
00423 REPORT
00424 GeneralMatrix* gm = new BandLUMatrix(*this);
00425 MatrixErrorNoSpace(gm); gm->ReleaseAndDelete(); return gm;
00426 }
00427
00428 SymmetricBandMatrix::SymmetricBandMatrix(const BaseMatrix& M)
00429 {
00430 REPORT
00431
00432 GeneralMatrix* gmx=((BaseMatrix&)M).Evaluate(MatrixType::SB);
00433 GetMatrix(gmx);
00434 }
00435
00436 GeneralMatrix* SymmetricBandMatrix::Transpose(TransposedMatrix*, MatrixType mt)
00437 { REPORT return Evaluate(mt); }
00438
00439 LogAndSign SymmetricBandMatrix::LogDeterminant() const
00440 {
00441 REPORT
00442 BandLUMatrix C(*this); return C.LogDeterminant();
00443 }
00444
00445 void SymmetricBandMatrix::SetParameters(const GeneralMatrix* gmx)
00446 { REPORT lower = gmx->BandWidth().lower; }
00447
00448 void SymmetricBandMatrix::ReSize(int n, int lb)
00449 {
00450 REPORT
00451 Tracer tr("SymmetricBandMatrix::ReSize");
00452 if (lb<0) Throw(ProgramException("Undefined bandwidth"));
00453 lower = (lb<=n) ? lb : n-1;
00454 GeneralMatrix::ReSize(n,n,n*(lower+1));
00455 }
00456
00457 void SymmetricBandMatrix::ReSize(const GeneralMatrix& A)
00458 {
00459 REPORT
00460 int n = A.Nrows();
00461 if (n != A.Ncols())
00462 {
00463 Tracer tr("SymmetricBandMatrix::ReSize(GM)");
00464 Throw(NotSquareException(*this));
00465 }
00466 MatrixBandWidth mbw = A.BandWidth(); int b = mbw.Lower();
00467 if (b != mbw.Upper())
00468 {
00469 Tracer tr("SymmetricBandMatrix::ReSize(GM)");
00470 Throw(ProgramException("Upper and lower band-widths not equal"));
00471 }
00472 ReSize(n, b);
00473 }
00474
00475 bool SymmetricBandMatrix::SameStorageType(const GeneralMatrix& A) const
00476 {
00477 if (Type() != A.Type()) { REPORT return false; }
00478 REPORT
00479 return BandWidth() == A.BandWidth();
00480 }
00481
00482 void SymmetricBandMatrix::ReSizeForAdd(const GeneralMatrix& A,
00483 const GeneralMatrix& B)
00484 {
00485 REPORT
00486 Tracer tr("SymmetricBandMatrix::ReSizeForAdd");
00487 MatrixBandWidth A_BW = A.BandWidth(); MatrixBandWidth B_BW = B.BandWidth();
00488 if ((A_BW.Lower() < 0) | (B_BW.Lower() < 0))
00489 Throw(ProgramException("Can't ReSize to SymmetricBandMatrix" ));
00490
00491 ReSize(A.Nrows(), my_max(A_BW.Lower(), B_BW.Lower()));
00492 }
00493
00494 void SymmetricBandMatrix::ReSizeForSP(const GeneralMatrix& A,
00495 const GeneralMatrix& B)
00496 {
00497 REPORT
00498 Tracer tr("SymmetricBandMatrix::ReSizeForSP");
00499 MatrixBandWidth A_BW = A.BandWidth(); MatrixBandWidth B_BW = B.BandWidth();
00500 if ((A_BW.Lower() < 0) | (B_BW.Lower() < 0))
00501 Throw(ProgramException("Can't ReSize to SymmetricBandMatrix" ));
00502
00503 ReSize(A.Nrows(), my_min(A_BW.Lower(), B_BW.Lower()));
00504 }
00505
00506
00507 void SymmetricBandMatrix::operator=(const BaseMatrix& X)
00508 {
00509 REPORT
00510
00511 Eq(X,MatrixType::SB);
00512 }
00513
00514 void SymmetricBandMatrix::CornerClear() const
00515 {
00516
00517 REPORT
00518 int i = lower; Real* s = store; int bw = lower + 1;
00519 if (i) for(;;)
00520 {
00521 int j = i;
00522 Real* sj = s;
00523 while (j--) *sj++ = 0.0;
00524 if (!(--i)) break;
00525 s += bw;
00526 }
00527 }
00528
00529 MatrixBandWidth SymmetricBandMatrix::BandWidth() const
00530 { REPORT return MatrixBandWidth(lower,lower); }
00531
00532 inline Real square(Real x) { return x*x; }
00533
00534
00535 Real SymmetricBandMatrix::SumSquare() const
00536 {
00537 REPORT
00538 CornerClear();
00539 Real sum1=0.0; Real sum2=0.0; Real* s=store; int i=nrows; int l=lower;
00540 while (i--)
00541 { int j = l; while (j--) sum2 += square(*s++); sum1 += square(*s++); }
00542 ((GeneralMatrix&)*this).tDelete(); return sum1 + 2.0 * sum2;
00543 }
00544
00545 Real SymmetricBandMatrix::SumAbsoluteValue() const
00546 {
00547 REPORT
00548 CornerClear();
00549 Real sum1=0.0; Real sum2=0.0; Real* s=store; int i=nrows; int l=lower;
00550 while (i--)
00551 { int j = l; while (j--) sum2 += fabs(*s++); sum1 += fabs(*s++); }
00552 ((GeneralMatrix&)*this).tDelete(); return sum1 + 2.0 * sum2;
00553 }
00554
00555 Real SymmetricBandMatrix::Sum() const
00556 {
00557 REPORT
00558 CornerClear();
00559 Real sum1=0.0; Real sum2=0.0; Real* s=store; int i=nrows; int l=lower;
00560 while (i--)
00561 { int j = l; while (j--) sum2 += *s++; sum1 += *s++; }
00562 ((GeneralMatrix&)*this).tDelete(); return sum1 + 2.0 * sum2;
00563 }
00564
00565
00566 #ifdef use_namespace
00567 }
00568 #endif
00569