| 1 | #define WANT_STREAM |
|---|
| 2 | |
|---|
| 3 | #include "include.h" |
|---|
| 4 | |
|---|
| 5 | #include "newmat.h" |
|---|
| 6 | |
|---|
| 7 | #include "tmt.h" |
|---|
| 8 | |
|---|
| 9 | #ifdef use_namespace |
|---|
| 10 | using namespace NEWMAT; |
|---|
| 11 | #endif |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | /**************************** test program ******************************/ |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | class PrintCounter |
|---|
| 18 | { |
|---|
| 19 | int count; |
|---|
| 20 | const char* s; |
|---|
| 21 | public: |
|---|
| 22 | ~PrintCounter(); |
|---|
| 23 | PrintCounter(const char * sx) : count(0), s(sx) {} |
|---|
| 24 | void operator++() { count++; } |
|---|
| 25 | }; |
|---|
| 26 | |
|---|
| 27 | PrintCounter PCZ("Number of non-zero matrices (should be 1) = "); |
|---|
| 28 | PrintCounter PCN("Number of matrices tested = "); |
|---|
| 29 | |
|---|
| 30 | PrintCounter::~PrintCounter() |
|---|
| 31 | { cout << s << count << "\n"; } |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | void Print(const Matrix& X) |
|---|
| 35 | { |
|---|
| 36 | ++PCN; |
|---|
| 37 | cout << "\nMatrix type: " << X.Type().Value() << " ("; |
|---|
| 38 | cout << X.Nrows() << ", "; |
|---|
| 39 | cout << X.Ncols() << ")\n\n"; |
|---|
| 40 | if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; } |
|---|
| 41 | int nr=X.Nrows(); int nc=X.Ncols(); |
|---|
| 42 | for (int i=1; i<=nr; i++) |
|---|
| 43 | { |
|---|
| 44 | for (int j=1; j<=nc; j++) cout << X(i,j) << "\t"; |
|---|
| 45 | cout << "\n"; |
|---|
| 46 | } |
|---|
| 47 | cout << flush; ++PCZ; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | void Print(const UpperTriangularMatrix& X) |
|---|
| 51 | { |
|---|
| 52 | ++PCN; |
|---|
| 53 | cout << "\nMatrix type: " << X.Type().Value() << " ("; |
|---|
| 54 | cout << X.Nrows() << ", "; |
|---|
| 55 | cout << X.Ncols() << ")\n\n"; |
|---|
| 56 | if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; } |
|---|
| 57 | int nr=X.Nrows(); int nc=X.Ncols(); |
|---|
| 58 | for (int i=1; i<=nr; i++) |
|---|
| 59 | { |
|---|
| 60 | int j; |
|---|
| 61 | for (j=1; j<i; j++) cout << "\t"; |
|---|
| 62 | for (j=i; j<=nc; j++) cout << X(i,j) << "\t"; |
|---|
| 63 | cout << "\n"; |
|---|
| 64 | } |
|---|
| 65 | cout << flush; ++PCZ; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | void Print(const DiagonalMatrix& X) |
|---|
| 69 | { |
|---|
| 70 | ++PCN; |
|---|
| 71 | cout << "\nMatrix type: " << X.Type().Value() << " ("; |
|---|
| 72 | cout << X.Nrows() << ", "; |
|---|
| 73 | cout << X.Ncols() << ")\n\n"; |
|---|
| 74 | if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; } |
|---|
| 75 | int nr=X.Nrows(); int nc=X.Ncols(); |
|---|
| 76 | for (int i=1; i<=nr; i++) |
|---|
| 77 | { |
|---|
| 78 | for (int j=1; j<i; j++) cout << "\t"; |
|---|
| 79 | if (i<=nc) cout << X(i,i) << "\t"; |
|---|
| 80 | cout << "\n"; |
|---|
| 81 | } |
|---|
| 82 | cout << flush; ++PCZ; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | void Print(const SymmetricMatrix& X) |
|---|
| 86 | { |
|---|
| 87 | ++PCN; |
|---|
| 88 | cout << "\nMatrix type: " << X.Type().Value() << " ("; |
|---|
| 89 | cout << X.Nrows() << ", "; |
|---|
| 90 | cout << X.Ncols() << ")\n\n"; |
|---|
| 91 | if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; } |
|---|
| 92 | int nr=X.Nrows(); int nc=X.Ncols(); |
|---|
| 93 | for (int i=1; i<=nr; i++) |
|---|
| 94 | { |
|---|
| 95 | int j; |
|---|
| 96 | for (j=1; j<i; j++) cout << X(j,i) << "\t"; |
|---|
| 97 | for (j=i; j<=nc; j++) cout << X(i,j) << "\t"; |
|---|
| 98 | cout << "\n"; |
|---|
| 99 | } |
|---|
| 100 | cout << flush; ++PCZ; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | void Print(const LowerTriangularMatrix& X) |
|---|
| 104 | { |
|---|
| 105 | ++PCN; |
|---|
| 106 | cout << "\nMatrix type: " << X.Type().Value() << " ("; |
|---|
| 107 | cout << X.Nrows() << ", "; |
|---|
| 108 | cout << X.Ncols() << ")\n\n"; |
|---|
| 109 | if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; } |
|---|
| 110 | int nr=X.Nrows(); |
|---|
| 111 | for (int i=1; i<=nr; i++) |
|---|
| 112 | { |
|---|
| 113 | for (int j=1; j<=i; j++) cout << X(i,j) << "\t"; |
|---|
| 114 | cout << "\n"; |
|---|
| 115 | } |
|---|
| 116 | cout << flush; ++PCZ; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | void Clean(Matrix& A, Real c) |
|---|
| 121 | { |
|---|
| 122 | int nr = A.Nrows(); int nc = A.Ncols(); |
|---|
| 123 | for (int i=1; i<=nr; i++) |
|---|
| 124 | { |
|---|
| 125 | for (int j=1; j<=nc; j++) |
|---|
| 126 | { Real a = A(i,j); if ((a < c) && (a > -c)) A(i,j) = 0.0; } |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | void Clean(DiagonalMatrix& A, Real c) |
|---|
| 131 | { |
|---|
| 132 | int nr = A.Nrows(); |
|---|
| 133 | for (int i=1; i<=nr; i++) |
|---|
| 134 | { Real a = A(i,i); if ((a < c) && (a > -c)) A(i,i) = 0.0; } |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | void PentiumCheck(Real N, Real D) |
|---|
| 138 | { |
|---|
| 139 | Real R = N / D; |
|---|
| 140 | R = R * D - N; |
|---|
| 141 | if ( R > 1 || R < -1) |
|---|
| 142 | cout << "Pentium error detected: % error = " << 100 * R / N << "\n"; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | //*************************** main program ********************************** |
|---|
| 148 | |
|---|
| 149 | void TestTypeAdd(); // test + |
|---|
| 150 | void TestTypeMult(); // test * |
|---|
| 151 | void TestTypeConcat(); // test | |
|---|
| 152 | void TestTypeSP(); // test SP |
|---|
| 153 | void TestTypeKP(); // test KP |
|---|
| 154 | void TestTypeOrder(); // test >= |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | int main() |
|---|
| 158 | { |
|---|
| 159 | Real* s1; Real* s2; Real* s3; Real* s4; |
|---|
| 160 | cout << "\nBegin test\n"; // Forces cout to allocate memory at beginning |
|---|
| 161 | cout << "Now print a real number: " << 3.14159265 << endl; |
|---|
| 162 | // Throw exception to set up exception buffer |
|---|
| 163 | #ifndef DisableExceptions |
|---|
| 164 | Try { Throw(Exception("Just a dummy\n")); } |
|---|
| 165 | CatchAll {} |
|---|
| 166 | #else |
|---|
| 167 | cout << "Not doing exceptions\n"; |
|---|
| 168 | #endif |
|---|
| 169 | { Matrix A1(40,200); s1 = A1.Store(); } |
|---|
| 170 | { Matrix A1(1,1); s3 = A1.Store(); } |
|---|
| 171 | { |
|---|
| 172 | Tracer et("Matrix test program"); |
|---|
| 173 | |
|---|
| 174 | Matrix A(25,150); |
|---|
| 175 | { |
|---|
| 176 | int i; |
|---|
| 177 | RowVector A(8); |
|---|
| 178 | for (i=1;i<=7;i++) A(i)=0.0; A(8)=1.0; |
|---|
| 179 | Print(A); |
|---|
| 180 | } |
|---|
| 181 | cout << "\n"; |
|---|
| 182 | |
|---|
| 183 | TestTypeAdd(); TestTypeMult(); TestTypeConcat(); |
|---|
| 184 | TestTypeSP(); TestTypeKP(); TestTypeOrder(); |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | Try { |
|---|
| 188 | trymat1(); |
|---|
| 189 | trymat2(); |
|---|
| 190 | trymat3(); |
|---|
| 191 | trymat4(); |
|---|
| 192 | trymat5(); |
|---|
| 193 | trymat6(); |
|---|
| 194 | trymat7(); |
|---|
| 195 | trymat8(); |
|---|
| 196 | trymat9(); |
|---|
| 197 | trymata(); |
|---|
| 198 | trymatb(); |
|---|
| 199 | trymatc(); |
|---|
| 200 | trymatd(); |
|---|
| 201 | trymate(); |
|---|
| 202 | trymatf(); |
|---|
| 203 | trymatg(); |
|---|
| 204 | trymath(); |
|---|
| 205 | trymati(); |
|---|
| 206 | trymatj(); |
|---|
| 207 | trymatk(); |
|---|
| 208 | trymatl(); |
|---|
| 209 | trymatm(); |
|---|
| 210 | |
|---|
| 211 | cout << "\nEnd of tests\n"; |
|---|
| 212 | } |
|---|
| 213 | CatchAll |
|---|
| 214 | { |
|---|
| 215 | cout << "\nTest program fails - exception generated\n\n"; |
|---|
| 216 | cout << Exception::what(); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | { Matrix A1(40,200); s2 = A1.Store(); } |
|---|
| 223 | cout << "\n(The following memory checks are probably not valid with all\n"; |
|---|
| 224 | cout << "compilers - see documentation)\n"; |
|---|
| 225 | cout << "\nChecking for lost memory: " |
|---|
| 226 | << (unsigned long)s1 << " " << (unsigned long)s2 << " "; |
|---|
| 227 | if (s1 != s2) cout << " - error\n"; else cout << " - ok\n"; |
|---|
| 228 | { Matrix A1(1,1); s4 = A1.Store(); } |
|---|
| 229 | cout << "\nChecking for lost memory: " |
|---|
| 230 | << (unsigned long)s3 << " " << (unsigned long)s4 << " "; |
|---|
| 231 | if (s3 != s4) cout << " - error\n\n"; else cout << " - ok\n\n"; |
|---|
| 232 | |
|---|
| 233 | // check for Pentium bug |
|---|
| 234 | PentiumCheck(4195835L,3145727L); |
|---|
| 235 | PentiumCheck(5244795L,3932159L); |
|---|
| 236 | |
|---|
| 237 | #ifdef DO_FREE_CHECK |
|---|
| 238 | FreeCheck::Status(); |
|---|
| 239 | #endif |
|---|
| 240 | return 0; |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | //************************ test type manipulation **************************/ |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | // These functions may cause problems for Glockenspiel 2.0c; they are used |
|---|
| 250 | // only for testing so you can delete them |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | void TestTypeAdd() |
|---|
| 254 | { |
|---|
| 255 | MatrixType list[10]; |
|---|
| 256 | list[0] = MatrixType::UT; |
|---|
| 257 | list[1] = MatrixType::LT; |
|---|
| 258 | list[2] = MatrixType::Rt; |
|---|
| 259 | list[3] = MatrixType::Sm; |
|---|
| 260 | list[4] = MatrixType::Dg; |
|---|
| 261 | list[5] = MatrixType::BM; |
|---|
| 262 | list[6] = MatrixType::UB; |
|---|
| 263 | list[7] = MatrixType::LB; |
|---|
| 264 | list[8] = MatrixType::SB; |
|---|
| 265 | list[9] = MatrixType::Id; |
|---|
| 266 | |
|---|
| 267 | cout << "+ "; |
|---|
| 268 | int i; |
|---|
| 269 | for (i=0; i<MatrixType::nTypes(); i++) cout << list[i].Value() << " "; |
|---|
| 270 | cout << "\n"; |
|---|
| 271 | for (i=0; i<MatrixType::nTypes(); i++) |
|---|
| 272 | { |
|---|
| 273 | cout << list[i].Value() << " "; |
|---|
| 274 | for (int j=0; j<MatrixType::nTypes(); j++) |
|---|
| 275 | cout << (list[j]+list[i]).Value() << " "; |
|---|
| 276 | cout << "\n"; |
|---|
| 277 | } |
|---|
| 278 | cout << "\n"; |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | void TestTypeMult() |
|---|
| 282 | { |
|---|
| 283 | MatrixType list[10]; |
|---|
| 284 | list[0] = MatrixType::UT; |
|---|
| 285 | list[1] = MatrixType::LT; |
|---|
| 286 | list[2] = MatrixType::Rt; |
|---|
| 287 | list[3] = MatrixType::Sm; |
|---|
| 288 | list[4] = MatrixType::Dg; |
|---|
| 289 | list[5] = MatrixType::BM; |
|---|
| 290 | list[6] = MatrixType::UB; |
|---|
| 291 | list[7] = MatrixType::LB; |
|---|
| 292 | list[8] = MatrixType::SB; |
|---|
| 293 | list[9] = MatrixType::Id; |
|---|
| 294 | |
|---|
| 295 | cout << "* "; |
|---|
| 296 | int i; |
|---|
| 297 | for (i=0; i<MatrixType::nTypes(); i++) |
|---|
| 298 | cout << list[i].Value() << " "; |
|---|
| 299 | cout << "\n"; |
|---|
| 300 | for (i=0; i<MatrixType::nTypes(); i++) |
|---|
| 301 | { |
|---|
| 302 | cout << list[i].Value() << " "; |
|---|
| 303 | for (int j=0; j<MatrixType::nTypes(); j++) |
|---|
| 304 | cout << (list[j]*list[i]).Value() << " "; |
|---|
| 305 | cout << "\n"; |
|---|
| 306 | } |
|---|
| 307 | cout << "\n"; |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | void TestTypeConcat() |
|---|
| 311 | { |
|---|
| 312 | MatrixType list[10]; |
|---|
| 313 | list[0] = MatrixType::UT; |
|---|
| 314 | list[1] = MatrixType::LT; |
|---|
| 315 | list[2] = MatrixType::Rt; |
|---|
| 316 | list[3] = MatrixType::Sm; |
|---|
| 317 | list[4] = MatrixType::Dg; |
|---|
| 318 | list[5] = MatrixType::BM; |
|---|
| 319 | list[6] = MatrixType::UB; |
|---|
| 320 | list[7] = MatrixType::LB; |
|---|
| 321 | list[8] = MatrixType::SB; |
|---|
| 322 | list[9] = MatrixType::Id; |
|---|
| 323 | |
|---|
| 324 | cout << "| "; |
|---|
| 325 | int i; |
|---|
| 326 | for (i=0; i<MatrixType::nTypes(); i++) |
|---|
| 327 | cout << list[i].Value() << " "; |
|---|
| 328 | cout << "\n"; |
|---|
| 329 | for (i=0; i<MatrixType::nTypes(); i++) |
|---|
| 330 | { |
|---|
| 331 | cout << list[i].Value() << " "; |
|---|
| 332 | for (int j=0; j<MatrixType::nTypes(); j++) |
|---|
| 333 | cout << (list[j] | list[i]).Value() << " "; |
|---|
| 334 | cout << "\n"; |
|---|
| 335 | } |
|---|
| 336 | cout << "\n"; |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | void TestTypeSP() |
|---|
| 340 | { |
|---|
| 341 | MatrixType list[10]; |
|---|
| 342 | list[0] = MatrixType::UT; |
|---|
| 343 | list[1] = MatrixType::LT; |
|---|
| 344 | list[2] = MatrixType::Rt; |
|---|
| 345 | list[3] = MatrixType::Sm; |
|---|
| 346 | list[4] = MatrixType::Dg; |
|---|
| 347 | list[5] = MatrixType::BM; |
|---|
| 348 | list[6] = MatrixType::UB; |
|---|
| 349 | list[7] = MatrixType::LB; |
|---|
| 350 | list[8] = MatrixType::SB; |
|---|
| 351 | list[9] = MatrixType::Id; |
|---|
| 352 | |
|---|
| 353 | cout << "SP "; |
|---|
| 354 | int i; |
|---|
| 355 | for (i=0; i<MatrixType::nTypes(); i++) |
|---|
| 356 | cout << list[i].Value() << " "; |
|---|
| 357 | cout << "\n"; |
|---|
| 358 | for (i=0; i<MatrixType::nTypes(); i++) |
|---|
| 359 | { |
|---|
| 360 | cout << list[i].Value() << " "; |
|---|
| 361 | for (int j=0; j<MatrixType::nTypes(); j++) |
|---|
| 362 | cout << (list[j].SP(list[i])).Value() << " "; |
|---|
| 363 | cout << "\n"; |
|---|
| 364 | } |
|---|
| 365 | cout << "\n"; |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | void TestTypeKP() |
|---|
| 369 | { |
|---|
| 370 | MatrixType list[10]; |
|---|
| 371 | list[0] = MatrixType::UT; |
|---|
| 372 | list[1] = MatrixType::LT; |
|---|
| 373 | list[2] = MatrixType::Rt; |
|---|
| 374 | list[3] = MatrixType::Sm; |
|---|
| 375 | list[4] = MatrixType::Dg; |
|---|
| 376 | list[5] = MatrixType::BM; |
|---|
| 377 | list[6] = MatrixType::UB; |
|---|
| 378 | list[7] = MatrixType::LB; |
|---|
| 379 | list[8] = MatrixType::SB; |
|---|
| 380 | list[9] = MatrixType::Id; |
|---|
| 381 | |
|---|
| 382 | cout << "KP "; |
|---|
| 383 | int i; |
|---|
| 384 | for (i=0; i<MatrixType::nTypes(); i++) |
|---|
| 385 | cout << list[i].Value() << " "; |
|---|
| 386 | cout << "\n"; |
|---|
| 387 | for (i=0; i<MatrixType::nTypes(); i++) |
|---|
| 388 | { |
|---|
| 389 | cout << list[i].Value() << " "; |
|---|
| 390 | for (int j=0; j<MatrixType::nTypes(); j++) |
|---|
| 391 | cout << (list[j].KP(list[i])).Value() << " "; |
|---|
| 392 | cout << "\n"; |
|---|
| 393 | } |
|---|
| 394 | cout << "\n"; |
|---|
| 395 | } |
|---|
| 396 | |
|---|
| 397 | void TestTypeOrder() |
|---|
| 398 | { |
|---|
| 399 | MatrixType list[10]; |
|---|
| 400 | list[0] = MatrixType::UT; |
|---|
| 401 | list[1] = MatrixType::LT; |
|---|
| 402 | list[2] = MatrixType::Rt; |
|---|
| 403 | list[3] = MatrixType::Sm; |
|---|
| 404 | list[4] = MatrixType::Dg; |
|---|
| 405 | list[5] = MatrixType::BM; |
|---|
| 406 | list[6] = MatrixType::UB; |
|---|
| 407 | list[7] = MatrixType::LB; |
|---|
| 408 | list[8] = MatrixType::SB; |
|---|
| 409 | list[9] = MatrixType::Id; |
|---|
| 410 | |
|---|
| 411 | cout << ">= "; |
|---|
| 412 | int i; |
|---|
| 413 | for (i = 0; i<MatrixType::nTypes(); i++) |
|---|
| 414 | cout << list[i].Value() << " "; |
|---|
| 415 | cout << "\n"; |
|---|
| 416 | for (i=0; i<MatrixType::nTypes(); i++) |
|---|
| 417 | { |
|---|
| 418 | cout << list[i].Value() << " "; |
|---|
| 419 | for (int j=0; j<MatrixType::nTypes(); j++) |
|---|
| 420 | cout << ((list[j]>=list[i]) ? "Yes " : "No "); |
|---|
| 421 | cout << "\n"; |
|---|
| 422 | } |
|---|
| 423 | cout << "\n"; |
|---|
| 424 | } |
|---|
| 425 | |
|---|
| 426 | |
|---|