[4565] | 1 | //$$ bandmat.cpp Band matrix definitions |
---|
| 2 | |
---|
| 3 | // Copyright (C) 1991,2,3,4,9: R B Davies |
---|
| 4 | |
---|
| 5 | #define WANT_MATH // include.h will get math fns |
---|
| 6 | |
---|
| 7 | //#define WANT_STREAM |
---|
| 8 | |
---|
| 9 | #include "include.h" |
---|
| 10 | |
---|
| 11 | #include "newmat.h" |
---|
| 12 | #include "newmatrc.h" |
---|
| 13 | |
---|
| 14 | #ifdef use_namespace |
---|
| 15 | namespace NEWMAT { |
---|
| 16 | #endif |
---|
| 17 | |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | #ifdef DO_REPORT |
---|
| 21 | #define REPORT { static ExeCounter ExeCount(__LINE__,10); ++ExeCount; } |
---|
| 22 | #else |
---|
| 23 | #define REPORT {} |
---|
| 24 | #endif |
---|
| 25 | |
---|
| 26 | static inline int my_min(int x, int y) { return x < y ? x : y; } |
---|
| 27 | static inline int my_max(int x, int y) { return x > y ? x : y; } |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | BandMatrix::BandMatrix(const BaseMatrix& M) |
---|
| 31 | { |
---|
| 32 | REPORT // CheckConversion(M); |
---|
| 33 | // MatrixConversionCheck mcc; |
---|
| 34 | GeneralMatrix* gmx=((BaseMatrix&)M).Evaluate(MatrixType::BM); |
---|
| 35 | GetMatrix(gmx); CornerClear(); |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | void BandMatrix::SetParameters(const GeneralMatrix* gmx) |
---|
| 39 | { |
---|
| 40 | REPORT |
---|
| 41 | MatrixBandWidth bw = gmx->BandWidth(); |
---|
| 42 | lower = bw.lower; upper = bw.upper; |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | void BandMatrix::ReSize(int n, int lb, int ub) |
---|
| 46 | { |
---|
| 47 | REPORT |
---|
| 48 | Tracer tr("BandMatrix::ReSize"); |
---|
| 49 | if (lb<0 || ub<0) Throw(ProgramException("Undefined bandwidth")); |
---|
| 50 | lower = (lb<=n) ? lb : n-1; upper = (ub<=n) ? ub : n-1; |
---|
| 51 | GeneralMatrix::ReSize(n,n,n*(lower+1+upper)); CornerClear(); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | // SimpleAddOK shows when we can add etc two matrices by a simple vector add |
---|
| 55 | // and when we can add one matrix into another |
---|
| 56 | // *gm must be the same type as *this |
---|
| 57 | // return 0 if simple add is OK |
---|
| 58 | // return 1 if we can add into *gm only |
---|
| 59 | // return 2 if we can add into *this only |
---|
| 60 | // return 3 if we can't add either way |
---|
| 61 | // For SP this will still be valid if we swap 1 and 2 |
---|
| 62 | |
---|
| 63 | short BandMatrix::SimpleAddOK(const GeneralMatrix* gm) |
---|
| 64 | { |
---|
| 65 | const BandMatrix* bm = (const BandMatrix*)gm; |
---|
| 66 | if (bm->lower == lower && bm->upper == upper) { REPORT return 0; } |
---|
| 67 | else if (bm->lower >= lower && bm->upper >= upper) { REPORT return 1; } |
---|
| 68 | else if (bm->lower <= lower && bm->upper <= upper) { REPORT return 2; } |
---|
| 69 | else { REPORT return 3; } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | short SymmetricBandMatrix::SimpleAddOK(const GeneralMatrix* gm) |
---|
| 73 | { |
---|
| 74 | const SymmetricBandMatrix* bm = (const SymmetricBandMatrix*)gm; |
---|
| 75 | if (bm->lower == lower) { REPORT return 0; } |
---|
| 76 | else if (bm->lower > lower) { REPORT return 1; } |
---|
| 77 | else { REPORT return 2; } |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | void UpperBandMatrix::ReSize(int n, int lb, int ub) |
---|
| 81 | { |
---|
| 82 | REPORT |
---|
| 83 | if (lb != 0) |
---|
| 84 | { |
---|
| 85 | Tracer tr("UpperBandMatrix::ReSize"); |
---|
| 86 | Throw(ProgramException("UpperBandMatrix with non-zero lower band" )); |
---|
| 87 | } |
---|
| 88 | BandMatrix::ReSize(n, lb, ub); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | void LowerBandMatrix::ReSize(int n, int lb, int ub) |
---|
| 92 | { |
---|
| 93 | REPORT |
---|
| 94 | if (ub != 0) |
---|
| 95 | { |
---|
| 96 | Tracer tr("LowerBandMatrix::ReSize"); |
---|
| 97 | Throw(ProgramException("LowerBandMatrix with non-zero upper band" )); |
---|
| 98 | } |
---|
| 99 | BandMatrix::ReSize(n, lb, ub); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | void BandMatrix::ReSize(const GeneralMatrix& A) |
---|
| 103 | { |
---|
| 104 | REPORT |
---|
| 105 | int n = A.Nrows(); |
---|
| 106 | if (n != A.Ncols()) |
---|
| 107 | { |
---|
| 108 | Tracer tr("BandMatrix::ReSize(GM)"); |
---|
| 109 | Throw(NotSquareException(*this)); |
---|
| 110 | } |
---|
| 111 | MatrixBandWidth mbw = A.BandWidth(); |
---|
| 112 | ReSize(n, mbw.Lower(), mbw.Upper()); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | bool BandMatrix::SameStorageType(const GeneralMatrix& A) const |
---|
| 116 | { |
---|
| 117 | if (Type() != A.Type()) { REPORT return false; } |
---|
| 118 | REPORT |
---|
| 119 | return BandWidth() == A.BandWidth(); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | void BandMatrix::ReSizeForAdd(const GeneralMatrix& A, const GeneralMatrix& B) |
---|
| 123 | { |
---|
| 124 | REPORT |
---|
| 125 | Tracer tr("BandMatrix::ReSizeForAdd"); |
---|
| 126 | MatrixBandWidth A_BW = A.BandWidth(); MatrixBandWidth B_BW = B.BandWidth(); |
---|
| 127 | if ((A_BW.Lower() < 0) | (A_BW.Upper() < 0) | (B_BW.Lower() < 0) |
---|
| 128 | | (A_BW.Upper() < 0)) |
---|
| 129 | Throw(ProgramException("Can't ReSize to BandMatrix" )); |
---|
| 130 | // already know A and B are square |
---|
| 131 | ReSize(A.Nrows(), my_max(A_BW.Lower(), B_BW.Lower()), |
---|
| 132 | my_max(A_BW.Upper(), B_BW.Upper())); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | void BandMatrix::ReSizeForSP(const GeneralMatrix& A, const GeneralMatrix& B) |
---|
| 136 | { |
---|
| 137 | REPORT |
---|
| 138 | Tracer tr("BandMatrix::ReSizeForSP"); |
---|
| 139 | MatrixBandWidth A_BW = A.BandWidth(); MatrixBandWidth B_BW = B.BandWidth(); |
---|
| 140 | if ((A_BW.Lower() < 0) | (A_BW.Upper() < 0) | (B_BW.Lower() < 0) |
---|
| 141 | | (A_BW.Upper() < 0)) |
---|
| 142 | Throw(ProgramException("Can't ReSize to BandMatrix" )); |
---|
| 143 | // already know A and B are square |
---|
| 144 | ReSize(A.Nrows(), my_min(A_BW.Lower(), B_BW.Lower()), |
---|
| 145 | my_min(A_BW.Upper(), B_BW.Upper())); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | void BandMatrix::operator=(const BaseMatrix& X) |
---|
| 150 | { |
---|
| 151 | REPORT // CheckConversion(X); |
---|
| 152 | // MatrixConversionCheck mcc; |
---|
| 153 | Eq(X,MatrixType::BM); CornerClear(); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | void BandMatrix::CornerClear() const |
---|
| 157 | { |
---|
| 158 | // set unused parts of BandMatrix to zero |
---|
| 159 | REPORT |
---|
| 160 | int i = lower; Real* s = store; int bw = lower + 1 + upper; |
---|
| 161 | while (i) |
---|
| 162 | { int j = i--; Real* sj = s; s += bw; while (j--) *sj++ = 0.0; } |
---|
| 163 | i = upper; s = store + storage; |
---|
| 164 | while (i) |
---|
| 165 | { int j = i--; Real* sj = s; s -= bw; while (j--) *(--sj) = 0.0; } |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | MatrixBandWidth MatrixBandWidth::operator+(const MatrixBandWidth& bw) const |
---|
| 169 | { |
---|
| 170 | REPORT |
---|
| 171 | int l = bw.lower; int u = bw.upper; |
---|
| 172 | l = (lower < 0 || l < 0) ? -1 : (lower > l) ? lower : l; |
---|
| 173 | u = (upper < 0 || u < 0) ? -1 : (upper > u) ? upper : u; |
---|
| 174 | return MatrixBandWidth(l,u); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | MatrixBandWidth MatrixBandWidth::operator*(const MatrixBandWidth& bw) const |
---|
| 178 | { |
---|
| 179 | REPORT |
---|
| 180 | int l = bw.lower; int u = bw.upper; |
---|
| 181 | l = (lower < 0 || l < 0) ? -1 : lower+l; |
---|
| 182 | u = (upper < 0 || u < 0) ? -1 : upper+u; |
---|
| 183 | return MatrixBandWidth(l,u); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | MatrixBandWidth MatrixBandWidth::minimum(const MatrixBandWidth& bw) const |
---|
| 187 | { |
---|
| 188 | REPORT |
---|
| 189 | int l = bw.lower; int u = bw.upper; |
---|
| 190 | if ((lower >= 0) && ( (l < 0) || (l > lower) )) l = lower; |
---|
| 191 | if ((upper >= 0) && ( (u < 0) || (u > upper) )) u = upper; |
---|
| 192 | return MatrixBandWidth(l,u); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | UpperBandMatrix::UpperBandMatrix(const BaseMatrix& M) |
---|
| 196 | { |
---|
| 197 | REPORT // CheckConversion(M); |
---|
| 198 | // MatrixConversionCheck mcc; |
---|
| 199 | GeneralMatrix* gmx=((BaseMatrix&)M).Evaluate(MatrixType::UB); |
---|
| 200 | GetMatrix(gmx); CornerClear(); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | void UpperBandMatrix::operator=(const BaseMatrix& X) |
---|
| 204 | { |
---|
| 205 | REPORT // CheckConversion(X); |
---|
| 206 | // MatrixConversionCheck mcc; |
---|
| 207 | Eq(X,MatrixType::UB); CornerClear(); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | LowerBandMatrix::LowerBandMatrix(const BaseMatrix& M) |
---|
| 211 | { |
---|
| 212 | REPORT // CheckConversion(M); |
---|
| 213 | // MatrixConversionCheck mcc; |
---|
| 214 | GeneralMatrix* gmx=((BaseMatrix&)M).Evaluate(MatrixType::LB); |
---|
| 215 | GetMatrix(gmx); CornerClear(); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | void LowerBandMatrix::operator=(const BaseMatrix& X) |
---|
| 219 | { |
---|
| 220 | REPORT // CheckConversion(X); |
---|
| 221 | // MatrixConversionCheck mcc; |
---|
| 222 | Eq(X,MatrixType::LB); CornerClear(); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | BandLUMatrix::BandLUMatrix(const BaseMatrix& m) |
---|
| 226 | { |
---|
| 227 | REPORT |
---|
| 228 | Tracer tr("BandLUMatrix"); |
---|
| 229 | storage2 = 0; store2 = 0; // in event of exception during build |
---|
| 230 | GeneralMatrix* gm = ((BaseMatrix&)m).Evaluate(MatrixType::BM); |
---|
| 231 | m1 = ((BandMatrix*)gm)->lower; m2 = ((BandMatrix*)gm)->upper; |
---|
| 232 | GetMatrix(gm); |
---|
| 233 | if (nrows!=ncols) Throw(NotSquareException(*this)); |
---|
| 234 | d = true; sing = false; |
---|
| 235 | indx = new int [nrows]; MatrixErrorNoSpace(indx); |
---|
| 236 | MONITOR_INT_NEW("Index (BndLUMat)",nrows,indx) |
---|
| 237 | storage2 = nrows * m1; |
---|
| 238 | store2 = new Real [storage2]; MatrixErrorNoSpace(store2); |
---|
| 239 | MONITOR_REAL_NEW("Make (BandLUMat)",storage2,store2) |
---|
| 240 | ludcmp(); |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | BandLUMatrix::~BandLUMatrix() |
---|
| 244 | { |
---|
| 245 | REPORT |
---|
| 246 | MONITOR_INT_DELETE("Index (BndLUMat)",nrows,indx) |
---|
| 247 | MONITOR_REAL_DELETE("Delete (BndLUMt)",storage2,store2) |
---|
| 248 | delete [] indx; delete [] store2; |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | MatrixType BandLUMatrix::Type() const { REPORT return MatrixType::BC; } |
---|
| 252 | |
---|
| 253 | |
---|
| 254 | LogAndSign BandLUMatrix::LogDeterminant() const |
---|
| 255 | { |
---|
| 256 | REPORT |
---|
| 257 | if (sing) return 0.0; |
---|
| 258 | Real* a = store; int w = m1+1+m2; LogAndSign sum; int i = nrows; |
---|
| 259 | // while (i--) { sum *= *a; a += w; } |
---|
| 260 | if (i) for (;;) { sum *= *a; if (!(--i)) break; a += w; } |
---|
| 261 | if (!d) sum.ChangeSign(); return sum; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | GeneralMatrix* BandMatrix::MakeSolver() |
---|
| 265 | { |
---|
| 266 | REPORT |
---|
| 267 | GeneralMatrix* gm = new BandLUMatrix(*this); |
---|
| 268 | MatrixErrorNoSpace(gm); gm->ReleaseAndDelete(); return gm; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | |
---|
| 272 | void BandLUMatrix::ludcmp() |
---|
| 273 | { |
---|
| 274 | REPORT |
---|
| 275 | Real* a = store2; int i = storage2; |
---|
| 276 | // clear store2 - so unused locations are always zero - |
---|
| 277 | // required by operator== |
---|
| 278 | while (i--) *a++ = 0.0; |
---|
| 279 | a = store; |
---|
| 280 | i = m1; int j = m2; int k; int n = nrows; int w = m1 + 1 + m2; |
---|
| 281 | while (i) |
---|
| 282 | { |
---|
| 283 | Real* ai = a + i; |
---|
| 284 | k = ++j; while (k--) *a++ = *ai++; |
---|
| 285 | k = i--; while (k--) *a++ = 0.0; |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | a = store; int l = m1; |
---|
| 289 | for (k=0; k<n; k++) |
---|
| 290 | { |
---|
| 291 | Real x = *a; i = k; Real* aj = a; |
---|
| 292 | if (l < n) l++; |
---|
| 293 | for (j=k+1; j<l; j++) |
---|
| 294 | { aj += w; if (fabs(x) < fabs(*aj)) { x = *aj; i = j; } } |
---|
| 295 | indx[k] = i; |
---|
| 296 | if (x==0) { sing = true; return; } |
---|
| 297 | if (i!=k) |
---|
| 298 | { |
---|
| 299 | d = !d; Real* ak = a; Real* ai = store + i * w; j = w; |
---|
| 300 | while (j--) { x = *ak; *ak++ = *ai; *ai++ = x; } |
---|
| 301 | } |
---|
| 302 | aj = a + w; Real* m = store2 + m1 * k; |
---|
| 303 | for (j=k+1; j<l; j++) |
---|
| 304 | { |
---|
| 305 | *m++ = x = *aj / *a; i = w; Real* ak = a; |
---|
| 306 | while (--i) { Real* aj1 = aj++; *aj1 = *aj - x * *(++ak); } |
---|
| 307 | *aj++ = 0.0; |
---|
| 308 | } |
---|
| 309 | a += w; |
---|
| 310 | } |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | void BandLUMatrix::lubksb(Real* B, int mini) |
---|
| 314 | { |
---|
| 315 | REPORT |
---|
| 316 | Tracer tr("BandLUMatrix::lubksb"); |
---|
| 317 | if (sing) Throw(SingularException(*this)); |
---|
| 318 | int n = nrows; int l = m1; int w = m1 + 1 + m2; |
---|
| 319 | |
---|
| 320 | for (int k=0; k<n; k++) |
---|
| 321 | { |
---|
| 322 | int i = indx[k]; |
---|
| 323 | if (i!=k) { Real x=B[k]; B[k]=B[i]; B[i]=x; } |
---|
| 324 | if (l<n) l++; |
---|
| 325 | Real* m = store2 + k*m1; Real* b = B+k; Real* bi = b; |
---|
| 326 | for (i=k+1; i<l; i++) *(++bi) -= *m++ * *b; |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | l = -m1; |
---|
| 330 | for (int i = n-1; i>=mini; i--) |
---|
| 331 | { |
---|
| 332 | Real* b = B + i; Real* bk = b; Real x = *bk; |
---|
| 333 | Real* a = store + w*i; Real y = *a; |
---|
| 334 | int k = l+m1; while (k--) x -= *(++a) * *(++bk); |
---|
| 335 | *b = x / y; |
---|
| 336 | if (l < m2) l++; |
---|
| 337 | } |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | void BandLUMatrix::Solver(MatrixColX& mcout, const MatrixColX& mcin) |
---|
| 341 | { |
---|
| 342 | REPORT |
---|
| 343 | int i = mcin.skip; Real* el = mcin.data-i; Real* el1=el; |
---|
| 344 | while (i--) *el++ = 0.0; |
---|
| 345 | el += mcin.storage; i = nrows - mcin.skip - mcin.storage; |
---|
| 346 | while (i--) *el++ = 0.0; |
---|
| 347 | lubksb(el1, mcout.skip); |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | // Do we need check for entirely zero output? |
---|
| 351 | |
---|
| 352 | |
---|
| 353 | void UpperBandMatrix::Solver(MatrixColX& mcout, |
---|
| 354 | const MatrixColX& mcin) |
---|
| 355 | { |
---|
| 356 | REPORT |
---|
| 357 | int i = mcin.skip-mcout.skip; Real* elx = mcin.data-i; |
---|
| 358 | while (i-- > 0) *elx++ = 0.0; |
---|
| 359 | int nr = mcin.skip+mcin.storage; |
---|
| 360 | elx = mcin.data+mcin.storage; Real* el = elx; |
---|
| 361 | int j = mcout.skip+mcout.storage-nr; i = nr-mcout.skip; |
---|
| 362 | while (j-- > 0) *elx++ = 0.0; |
---|
| 363 | |
---|
| 364 | Real* Ael = store + (upper+1)*(i-1)+1; j = 0; |
---|
| 365 | if (i > 0) for(;;) |
---|
| 366 | { |
---|
| 367 | elx = el; Real sum = 0.0; int jx = j; |
---|
| 368 | while (jx--) sum += *(--Ael) * *(--elx); |
---|
| 369 | elx--; *elx = (*elx - sum) / *(--Ael); |
---|
| 370 | if (--i <= 0) break; |
---|
| 371 | if (j<upper) Ael -= upper - (++j); else el--; |
---|
| 372 | } |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | void LowerBandMatrix::Solver(MatrixColX& mcout, |
---|
| 376 | const MatrixColX& mcin) |
---|
| 377 | { |
---|
| 378 | REPORT |
---|
| 379 | int i = mcin.skip-mcout.skip; Real* elx = mcin.data-i; |
---|
| 380 | while (i-- > 0) *elx++ = 0.0; |
---|
| 381 | int nc = mcin.skip; i = nc+mcin.storage; elx = mcin.data+mcin.storage; |
---|
| 382 | int nr = mcout.skip+mcout.storage; int j = nr-i; i = nr-nc; |
---|
| 383 | while (j-- > 0) *elx++ = 0.0; |
---|
| 384 | |
---|
| 385 | Real* el = mcin.data; Real* Ael = store + (lower+1)*nc + lower; j = 0; |
---|
| 386 | if (i > 0) for(;;) |
---|
| 387 | { |
---|
| 388 | elx = el; Real sum = 0.0; int jx = j; |
---|
| 389 | while (jx--) sum += *Ael++ * *elx++; |
---|
| 390 | *elx = (*elx - sum) / *Ael++; |
---|
| 391 | if (--i <= 0) break; |
---|
| 392 | if (j<lower) Ael += lower - (++j); else el++; |
---|
| 393 | } |
---|
| 394 | } |
---|
| 395 | |
---|
| 396 | |
---|
| 397 | LogAndSign BandMatrix::LogDeterminant() const |
---|
| 398 | { |
---|
| 399 | REPORT |
---|
| 400 | BandLUMatrix C(*this); return C.LogDeterminant(); |
---|
| 401 | } |
---|
| 402 | |
---|
| 403 | LogAndSign LowerBandMatrix::LogDeterminant() const |
---|
| 404 | { |
---|
| 405 | REPORT |
---|
| 406 | int i = nrows; LogAndSign sum; Real* s = store + lower; int j = lower + 1; |
---|
| 407 | // while (i--) { sum *= *s; s += j; } |
---|
| 408 | if (i) for (;;) { sum *= *s; if (!(--i)) break; s += j; } |
---|
| 409 | ((GeneralMatrix&)*this).tDelete(); return sum; |
---|
| 410 | } |
---|
| 411 | |
---|
| 412 | LogAndSign UpperBandMatrix::LogDeterminant() const |
---|
| 413 | { |
---|
| 414 | REPORT |
---|
| 415 | int i = nrows; LogAndSign sum; Real* s = store; int j = upper + 1; |
---|
| 416 | // while (i--) { sum *= *s; s += j; } |
---|
| 417 | if (i) for (;;) { sum *= *s; if (!(--i)) break; s += j; } |
---|
| 418 | ((GeneralMatrix&)*this).tDelete(); return sum; |
---|
| 419 | } |
---|
| 420 | |
---|
| 421 | GeneralMatrix* SymmetricBandMatrix::MakeSolver() |
---|
| 422 | { |
---|
| 423 | REPORT |
---|
| 424 | GeneralMatrix* gm = new BandLUMatrix(*this); |
---|
| 425 | MatrixErrorNoSpace(gm); gm->ReleaseAndDelete(); return gm; |
---|
| 426 | } |
---|
| 427 | |
---|
| 428 | SymmetricBandMatrix::SymmetricBandMatrix(const BaseMatrix& M) |
---|
| 429 | { |
---|
| 430 | REPORT // CheckConversion(M); |
---|
| 431 | // MatrixConversionCheck mcc; |
---|
| 432 | GeneralMatrix* gmx=((BaseMatrix&)M).Evaluate(MatrixType::SB); |
---|
| 433 | GetMatrix(gmx); |
---|
| 434 | } |
---|
| 435 | |
---|
| 436 | GeneralMatrix* SymmetricBandMatrix::Transpose(TransposedMatrix*, MatrixType mt) |
---|
| 437 | { REPORT return Evaluate(mt); } |
---|
| 438 | |
---|
| 439 | LogAndSign SymmetricBandMatrix::LogDeterminant() const |
---|
| 440 | { |
---|
| 441 | REPORT |
---|
| 442 | BandLUMatrix C(*this); return C.LogDeterminant(); |
---|
| 443 | } |
---|
| 444 | |
---|
| 445 | void SymmetricBandMatrix::SetParameters(const GeneralMatrix* gmx) |
---|
| 446 | { REPORT lower = gmx->BandWidth().lower; } |
---|
| 447 | |
---|
| 448 | void SymmetricBandMatrix::ReSize(int n, int lb) |
---|
| 449 | { |
---|
| 450 | REPORT |
---|
| 451 | Tracer tr("SymmetricBandMatrix::ReSize"); |
---|
| 452 | if (lb<0) Throw(ProgramException("Undefined bandwidth")); |
---|
| 453 | lower = (lb<=n) ? lb : n-1; |
---|
| 454 | GeneralMatrix::ReSize(n,n,n*(lower+1)); |
---|
| 455 | } |
---|
| 456 | |
---|
| 457 | void SymmetricBandMatrix::ReSize(const GeneralMatrix& A) |
---|
| 458 | { |
---|
| 459 | REPORT |
---|
| 460 | int n = A.Nrows(); |
---|
| 461 | if (n != A.Ncols()) |
---|
| 462 | { |
---|
| 463 | Tracer tr("SymmetricBandMatrix::ReSize(GM)"); |
---|
| 464 | Throw(NotSquareException(*this)); |
---|
| 465 | } |
---|
| 466 | MatrixBandWidth mbw = A.BandWidth(); int b = mbw.Lower(); |
---|
| 467 | if (b != mbw.Upper()) |
---|
| 468 | { |
---|
| 469 | Tracer tr("SymmetricBandMatrix::ReSize(GM)"); |
---|
| 470 | Throw(ProgramException("Upper and lower band-widths not equal")); |
---|
| 471 | } |
---|
| 472 | ReSize(n, b); |
---|
| 473 | } |
---|
| 474 | |
---|
| 475 | bool SymmetricBandMatrix::SameStorageType(const GeneralMatrix& A) const |
---|
| 476 | { |
---|
| 477 | if (Type() != A.Type()) { REPORT return false; } |
---|
| 478 | REPORT |
---|
| 479 | return BandWidth() == A.BandWidth(); |
---|
| 480 | } |
---|
| 481 | |
---|
| 482 | void SymmetricBandMatrix::ReSizeForAdd(const GeneralMatrix& A, |
---|
| 483 | const GeneralMatrix& B) |
---|
| 484 | { |
---|
| 485 | REPORT |
---|
| 486 | Tracer tr("SymmetricBandMatrix::ReSizeForAdd"); |
---|
| 487 | MatrixBandWidth A_BW = A.BandWidth(); MatrixBandWidth B_BW = B.BandWidth(); |
---|
| 488 | if ((A_BW.Lower() < 0) | (B_BW.Lower() < 0)) |
---|
| 489 | Throw(ProgramException("Can't ReSize to SymmetricBandMatrix" )); |
---|
| 490 | // already know A and B are square |
---|
| 491 | ReSize(A.Nrows(), my_max(A_BW.Lower(), B_BW.Lower())); |
---|
| 492 | } |
---|
| 493 | |
---|
| 494 | void SymmetricBandMatrix::ReSizeForSP(const GeneralMatrix& A, |
---|
| 495 | const GeneralMatrix& B) |
---|
| 496 | { |
---|
| 497 | REPORT |
---|
| 498 | Tracer tr("SymmetricBandMatrix::ReSizeForSP"); |
---|
| 499 | MatrixBandWidth A_BW = A.BandWidth(); MatrixBandWidth B_BW = B.BandWidth(); |
---|
| 500 | if ((A_BW.Lower() < 0) | (B_BW.Lower() < 0)) |
---|
| 501 | Throw(ProgramException("Can't ReSize to SymmetricBandMatrix" )); |
---|
| 502 | // already know A and B are square |
---|
| 503 | ReSize(A.Nrows(), my_min(A_BW.Lower(), B_BW.Lower())); |
---|
| 504 | } |
---|
| 505 | |
---|
| 506 | |
---|
| 507 | void SymmetricBandMatrix::operator=(const BaseMatrix& X) |
---|
| 508 | { |
---|
| 509 | REPORT // CheckConversion(X); |
---|
| 510 | // MatrixConversionCheck mcc; |
---|
| 511 | Eq(X,MatrixType::SB); |
---|
| 512 | } |
---|
| 513 | |
---|
| 514 | void SymmetricBandMatrix::CornerClear() const |
---|
| 515 | { |
---|
| 516 | // set unused parts of BandMatrix to zero |
---|
| 517 | REPORT |
---|
| 518 | int i = lower; Real* s = store; int bw = lower + 1; |
---|
| 519 | if (i) for(;;) |
---|
| 520 | { |
---|
| 521 | int j = i; |
---|
| 522 | Real* sj = s; |
---|
| 523 | while (j--) *sj++ = 0.0; |
---|
| 524 | if (!(--i)) break; |
---|
| 525 | s += bw; |
---|
| 526 | } |
---|
| 527 | } |
---|
| 528 | |
---|
| 529 | MatrixBandWidth SymmetricBandMatrix::BandWidth() const |
---|
| 530 | { REPORT return MatrixBandWidth(lower,lower); } |
---|
| 531 | |
---|
| 532 | inline Real square(Real x) { return x*x; } |
---|
| 533 | |
---|
| 534 | |
---|
| 535 | Real SymmetricBandMatrix::SumSquare() const |
---|
| 536 | { |
---|
| 537 | REPORT |
---|
| 538 | CornerClear(); |
---|
| 539 | Real sum1=0.0; Real sum2=0.0; Real* s=store; int i=nrows; int l=lower; |
---|
| 540 | while (i--) |
---|
| 541 | { int j = l; while (j--) sum2 += square(*s++); sum1 += square(*s++); } |
---|
| 542 | ((GeneralMatrix&)*this).tDelete(); return sum1 + 2.0 * sum2; |
---|
| 543 | } |
---|
| 544 | |
---|
| 545 | Real SymmetricBandMatrix::SumAbsoluteValue() const |
---|
| 546 | { |
---|
| 547 | REPORT |
---|
| 548 | CornerClear(); |
---|
| 549 | Real sum1=0.0; Real sum2=0.0; Real* s=store; int i=nrows; int l=lower; |
---|
| 550 | while (i--) |
---|
| 551 | { int j = l; while (j--) sum2 += fabs(*s++); sum1 += fabs(*s++); } |
---|
| 552 | ((GeneralMatrix&)*this).tDelete(); return sum1 + 2.0 * sum2; |
---|
| 553 | } |
---|
| 554 | |
---|
| 555 | Real SymmetricBandMatrix::Sum() const |
---|
| 556 | { |
---|
| 557 | REPORT |
---|
| 558 | CornerClear(); |
---|
| 559 | Real sum1=0.0; Real sum2=0.0; Real* s=store; int i=nrows; int l=lower; |
---|
| 560 | while (i--) |
---|
| 561 | { int j = l; while (j--) sum2 += *s++; sum1 += *s++; } |
---|
| 562 | ((GeneralMatrix&)*this).tDelete(); return sum1 + 2.0 * sum2; |
---|
| 563 | } |
---|
| 564 | |
---|
| 565 | |
---|
| 566 | #ifdef use_namespace |
---|
| 567 | } |
---|
| 568 | #endif |
---|
| 569 | |
---|