Go to the documentation of this file.00001 #define WANT_STREAM
00002
00003 #include "include.h"
00004
00005 #include "newmat.h"
00006
00007 #include "tmt.h"
00008
00009 #ifdef use_namespace
00010
00011 namespace NEWMAT {
00012 #endif
00013
00014
00015
00016
00017
00018 class PrintCounter
00019 {
00020 int count;
00021 const char* s;
00022 public:
00023 ~PrintCounter();
00024 PrintCounter(const char * sx) : count(0), s(sx) {}
00025 void operator++() { count++; }
00026 };
00027
00028 PrintCounter PCZ("Number of non-zero matrices (should be 1) = ");
00029 PrintCounter PCN("Number of matrices tested = ");
00030
00031 PrintCounter::~PrintCounter()
00032 { cout << s << count << "\n"; }
00033
00034
00035 void Print(const Matrix& X)
00036 {
00037 ++PCN;
00038 cout << "\nMatrix type: " << X.Type().Value() << " (";
00039 cout << X.Nrows() << ", ";
00040 cout << X.Ncols() << ")\n\n";
00041 if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; }
00042 int nr=X.Nrows(); int nc=X.Ncols();
00043 for (int i=1; i<=nr; i++)
00044 {
00045 for (int j=1; j<=nc; j++) cout << X(i,j) << "\t";
00046 cout << "\n";
00047 }
00048 cout << flush; ++PCZ;
00049 }
00050
00051 void Print(const UpperTriangularMatrix& X)
00052 {
00053 ++PCN;
00054 cout << "\nMatrix type: " << X.Type().Value() << " (";
00055 cout << X.Nrows() << ", ";
00056 cout << X.Ncols() << ")\n\n";
00057 if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; }
00058 int nr=X.Nrows(); int nc=X.Ncols();
00059 for (int i=1; i<=nr; i++)
00060 {
00061 int j;
00062 for (j=1; j<i; j++) cout << "\t";
00063 for (j=i; j<=nc; j++) cout << X(i,j) << "\t";
00064 cout << "\n";
00065 }
00066 cout << flush; ++PCZ;
00067 }
00068
00069 void Print(const DiagonalMatrix& X)
00070 {
00071 ++PCN;
00072 cout << "\nMatrix type: " << X.Type().Value() << " (";
00073 cout << X.Nrows() << ", ";
00074 cout << X.Ncols() << ")\n\n";
00075 if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; }
00076 int nr=X.Nrows(); int nc=X.Ncols();
00077 for (int i=1; i<=nr; i++)
00078 {
00079 for (int j=1; j<i; j++) cout << "\t";
00080 if (i<=nc) cout << X(i,i) << "\t";
00081 cout << "\n";
00082 }
00083 cout << flush; ++PCZ;
00084 }
00085
00086 void Print(const SymmetricMatrix& X)
00087 {
00088 ++PCN;
00089 cout << "\nMatrix type: " << X.Type().Value() << " (";
00090 cout << X.Nrows() << ", ";
00091 cout << X.Ncols() << ")\n\n";
00092 if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; }
00093 int nr=X.Nrows(); int nc=X.Ncols();
00094 for (int i=1; i<=nr; i++)
00095 {
00096 int j;
00097 for (j=1; j<i; j++) cout << X(j,i) << "\t";
00098 for (j=i; j<=nc; j++) cout << X(i,j) << "\t";
00099 cout << "\n";
00100 }
00101 cout << flush; ++PCZ;
00102 }
00103
00104 void Print(const LowerTriangularMatrix& X)
00105 {
00106 ++PCN;
00107 cout << "\nMatrix type: " << X.Type().Value() << " (";
00108 cout << X.Nrows() << ", ";
00109 cout << X.Ncols() << ")\n\n";
00110 if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; }
00111 int nr=X.Nrows();
00112 for (int i=1; i<=nr; i++)
00113 {
00114 for (int j=1; j<=i; j++) cout << X(i,j) << "\t";
00115 cout << "\n";
00116 }
00117 cout << flush; ++PCZ;
00118 }
00119
00120
00121 void Clean(Matrix& A, Real c)
00122 {
00123 int nr = A.Nrows(); int nc = A.Ncols();
00124 for (int i=1; i<=nr; i++)
00125 {
00126 for (int j=1; j<=nc; j++)
00127 { Real a = A(i,j); if ((a < c) && (a > -c)) A(i,j) = 0.0; }
00128 }
00129 }
00130
00131 void Clean(DiagonalMatrix& A, Real c)
00132 {
00133 int nr = A.Nrows();
00134 for (int i=1; i<=nr; i++)
00135 { Real a = A(i,i); if ((a < c) && (a > -c)) A(i,i) = 0.0; }
00136 }
00137
00138 void PentiumCheck(Real N, Real D)
00139 {
00140 Real R = N / D;
00141 R = R * D - N;
00142 if ( R > 1 || R < -1)
00143 cout << "Pentium error detected: % error = " << 100 * R / N << "\n";
00144 }
00145
00146
00147 #ifdef use_namespace
00148 }
00149 using namespace NEWMAT;
00150 #endif
00151
00152
00153
00154 void TestTypeAdd();
00155 void TestTypeMult();
00156 void TestTypeConcat();
00157 void TestTypeSP();
00158 void TestTypeKP();
00159 void TestTypeOrder();
00160
00161
00162 int main()
00163 {
00164 Real* s1; Real* s2; Real* s3; Real* s4;
00165 cout << "\nBegin test\n";
00166 cout << "Now print a real number: " << 3.14159265 << endl;
00167
00168 #ifndef DisableExceptions
00169 Try { Throw(BaseException("Just a dummy\n")); }
00170 CatchAll {}
00171 #else
00172 cout << "Not doing exceptions\n";
00173 #endif
00174 { Matrix A1(40,200); s1 = A1.Store(); }
00175 { Matrix A1(1,1); s3 = A1.Store(); }
00176 {
00177 Tracer et("Matrix test program");
00178
00179 Matrix A(25,150);
00180 {
00181 int i;
00182 RowVector A(8);
00183 for (i=1;i<=7;i++) A(i)=0.0; A(8)=1.0;
00184 Print(A);
00185 }
00186 cout << "\n";
00187
00188 TestTypeAdd(); TestTypeMult(); TestTypeConcat();
00189 TestTypeSP(); TestTypeKP(); TestTypeOrder();
00190
00191
00192 Try {
00193 trymat1();
00194 trymat2();
00195 trymat3();
00196 trymat4();
00197 trymat5();
00198 trymat6();
00199 trymat7();
00200 trymat8();
00201 trymat9();
00202 trymata();
00203 trymatb();
00204 trymatc();
00205 trymatd();
00206 trymate();
00207 trymatf();
00208 trymatg();
00209 trymath();
00210 trymati();
00211 trymatj();
00212 trymatk();
00213 trymatl();
00214 trymatm();
00215
00216 cout << "\nEnd of tests\n";
00217 }
00218 CatchAll
00219 {
00220 cout << "\nTest program fails - exception generated\n\n";
00221 cout << BaseException::what();
00222 }
00223
00224
00225 }
00226
00227 { Matrix A1(40,200); s2 = A1.Store(); }
00228 cout << "\n(The following memory checks are probably not valid with all\n";
00229 cout << "compilers - see documentation)\n";
00230 cout << "\nChecking for lost memory: "
00231 << (unsigned long)s1 << " " << (unsigned long)s2 << " ";
00232 if (s1 != s2) cout << " - error\n"; else cout << " - ok\n";
00233 { Matrix A1(1,1); s4 = A1.Store(); }
00234 cout << "\nChecking for lost memory: "
00235 << (unsigned long)s3 << " " << (unsigned long)s4 << " ";
00236 if (s3 != s4) cout << " - error\n\n"; else cout << " - ok\n\n";
00237
00238
00239 PentiumCheck(4195835L,3145727L);
00240 PentiumCheck(5244795L,3932159L);
00241
00242 #ifdef DO_FREE_CHECK
00243 FreeCheck::Status();
00244 #endif
00245 return 0;
00246 }
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258 void TestTypeAdd()
00259 {
00260 MatrixType list[10];
00261 list[0] = MatrixType::UT;
00262 list[1] = MatrixType::LT;
00263 list[2] = MatrixType::Rt;
00264 list[3] = MatrixType::Sm;
00265 list[4] = MatrixType::Dg;
00266 list[5] = MatrixType::BM;
00267 list[6] = MatrixType::UB;
00268 list[7] = MatrixType::LB;
00269 list[8] = MatrixType::SB;
00270 list[9] = MatrixType::Id;
00271
00272 cout << "+ ";
00273 int i;
00274 for (i=0; i<MatrixType::nTypes(); i++) cout << list[i].Value() << " ";
00275 cout << "\n";
00276 for (i=0; i<MatrixType::nTypes(); i++)
00277 {
00278 cout << list[i].Value() << " ";
00279 for (int j=0; j<MatrixType::nTypes(); j++)
00280 cout << (list[j]+list[i]).Value() << " ";
00281 cout << "\n";
00282 }
00283 cout << "\n";
00284 }
00285
00286 void TestTypeMult()
00287 {
00288 MatrixType list[10];
00289 list[0] = MatrixType::UT;
00290 list[1] = MatrixType::LT;
00291 list[2] = MatrixType::Rt;
00292 list[3] = MatrixType::Sm;
00293 list[4] = MatrixType::Dg;
00294 list[5] = MatrixType::BM;
00295 list[6] = MatrixType::UB;
00296 list[7] = MatrixType::LB;
00297 list[8] = MatrixType::SB;
00298 list[9] = MatrixType::Id;
00299
00300 cout << "* ";
00301 int i;
00302 for (i=0; i<MatrixType::nTypes(); i++)
00303 cout << list[i].Value() << " ";
00304 cout << "\n";
00305 for (i=0; i<MatrixType::nTypes(); i++)
00306 {
00307 cout << list[i].Value() << " ";
00308 for (int j=0; j<MatrixType::nTypes(); j++)
00309 cout << (list[j]*list[i]).Value() << " ";
00310 cout << "\n";
00311 }
00312 cout << "\n";
00313 }
00314
00315 void TestTypeConcat()
00316 {
00317 MatrixType list[10];
00318 list[0] = MatrixType::UT;
00319 list[1] = MatrixType::LT;
00320 list[2] = MatrixType::Rt;
00321 list[3] = MatrixType::Sm;
00322 list[4] = MatrixType::Dg;
00323 list[5] = MatrixType::BM;
00324 list[6] = MatrixType::UB;
00325 list[7] = MatrixType::LB;
00326 list[8] = MatrixType::SB;
00327 list[9] = MatrixType::Id;
00328
00329 cout << "| ";
00330 int i;
00331 for (i=0; i<MatrixType::nTypes(); i++)
00332 cout << list[i].Value() << " ";
00333 cout << "\n";
00334 for (i=0; i<MatrixType::nTypes(); i++)
00335 {
00336 cout << list[i].Value() << " ";
00337 for (int j=0; j<MatrixType::nTypes(); j++)
00338 cout << (list[j] | list[i]).Value() << " ";
00339 cout << "\n";
00340 }
00341 cout << "\n";
00342 }
00343
00344 void TestTypeSP()
00345 {
00346 MatrixType list[10];
00347 list[0] = MatrixType::UT;
00348 list[1] = MatrixType::LT;
00349 list[2] = MatrixType::Rt;
00350 list[3] = MatrixType::Sm;
00351 list[4] = MatrixType::Dg;
00352 list[5] = MatrixType::BM;
00353 list[6] = MatrixType::UB;
00354 list[7] = MatrixType::LB;
00355 list[8] = MatrixType::SB;
00356 list[9] = MatrixType::Id;
00357
00358 cout << "SP ";
00359 int i;
00360 for (i=0; i<MatrixType::nTypes(); i++)
00361 cout << list[i].Value() << " ";
00362 cout << "\n";
00363 for (i=0; i<MatrixType::nTypes(); i++)
00364 {
00365 cout << list[i].Value() << " ";
00366 for (int j=0; j<MatrixType::nTypes(); j++)
00367 cout << (list[j].SP(list[i])).Value() << " ";
00368 cout << "\n";
00369 }
00370 cout << "\n";
00371 }
00372
00373 void TestTypeKP()
00374 {
00375 MatrixType list[10];
00376 list[0] = MatrixType::UT;
00377 list[1] = MatrixType::LT;
00378 list[2] = MatrixType::Rt;
00379 list[3] = MatrixType::Sm;
00380 list[4] = MatrixType::Dg;
00381 list[5] = MatrixType::BM;
00382 list[6] = MatrixType::UB;
00383 list[7] = MatrixType::LB;
00384 list[8] = MatrixType::SB;
00385 list[9] = MatrixType::Id;
00386
00387 cout << "KP ";
00388 int i;
00389 for (i=0; i<MatrixType::nTypes(); i++)
00390 cout << list[i].Value() << " ";
00391 cout << "\n";
00392 for (i=0; i<MatrixType::nTypes(); i++)
00393 {
00394 cout << list[i].Value() << " ";
00395 for (int j=0; j<MatrixType::nTypes(); j++)
00396 cout << (list[j].KP(list[i])).Value() << " ";
00397 cout << "\n";
00398 }
00399 cout << "\n";
00400 }
00401
00402 void TestTypeOrder()
00403 {
00404 MatrixType list[10];
00405 list[0] = MatrixType::UT;
00406 list[1] = MatrixType::LT;
00407 list[2] = MatrixType::Rt;
00408 list[3] = MatrixType::Sm;
00409 list[4] = MatrixType::Dg;
00410 list[5] = MatrixType::BM;
00411 list[6] = MatrixType::UB;
00412 list[7] = MatrixType::LB;
00413 list[8] = MatrixType::SB;
00414 list[9] = MatrixType::Id;
00415
00416 cout << ">= ";
00417 int i;
00418 for (i = 0; i<MatrixType::nTypes(); i++)
00419 cout << list[i].Value() << " ";
00420 cout << "\n";
00421 for (i=0; i<MatrixType::nTypes(); i++)
00422 {
00423 cout << list[i].Value() << " ";
00424 for (int j=0; j<MatrixType::nTypes(); j++)
00425 cout << ((list[j]>=list[i]) ? "Yes " : "No ");
00426 cout << "\n";
00427 }
00428 cout << "\n";
00429 }
00430
00431