home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / n / newmat06.zip / TMT.CXX < prev    next >
C/C++ Source or Header  |  1992-12-03  |  5KB  |  187 lines

  1. #define WANT_STREAM
  2.  
  3. #include "include.h"
  4.  
  5. #include "newmat.h"
  6.  
  7. /**************************** test program ******************************/
  8.  
  9. class PrintCounter
  10. {
  11.    int count;
  12.    char* s;
  13. public:
  14.    ~PrintCounter();
  15.    PrintCounter(char * sx) : count(0), s(sx) {}
  16.    void operator++() { count++; }
  17. };
  18.  
  19.    PrintCounter PCZ("Number of non-zero matrices = ");
  20.    PrintCounter PCN("Number of matrices tested   = ");
  21.  
  22. PrintCounter::~PrintCounter()
  23. { cout << s << count << "\n"; }
  24.  
  25.  
  26. void Print(const Matrix& X)
  27. {
  28.    ++PCN;
  29.    cout << "\nMatrix type: " << (char*)X.Type() << " (";
  30.    cout << X.Nrows() << ", ";
  31.    cout << X.Ncols() << ")\n\n";
  32.    if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; }
  33.    int nr=X.Nrows(); int nc=X.Ncols();
  34.    for (int i=1; i<=nr; i++)
  35.    {
  36.       for (int j=1; j<=nc; j++)  cout << X(i,j) << "\t";
  37.       cout << "\n";
  38.    }
  39.    cout << flush; ++PCZ;
  40. }
  41.  
  42. void Print(const UpperTriangularMatrix& X)
  43. {
  44.    ++PCN;
  45.    cout << "\nMatrix type: " << (char*)X.Type() << " (";
  46.    cout << X.Nrows() << ", ";
  47.    cout << X.Ncols() << ")\n\n";
  48.    if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; }
  49.    int nr=X.Nrows(); int nc=X.Ncols();
  50.    for (int i=1; i<=nr; i++)
  51.    {
  52.       for (int j=1; j<i; j++) cout << "\t";
  53.       for (j=i; j<=nc; j++)  cout << X(i,j) << "\t";
  54.       cout << "\n";
  55.    }
  56.    cout << flush; ++PCZ;
  57. }
  58.  
  59. void Print(const DiagonalMatrix& X)
  60. {
  61.    ++PCN;
  62.    cout << "\nMatrix type: " << (char*)X.Type() << " (";
  63.    cout << X.Nrows() << ", ";
  64.    cout << X.Ncols() << ")\n\n";
  65.    if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; }
  66.    int nr=X.Nrows(); int nc=X.Ncols();
  67.    for (int i=1; i<=nr; i++)
  68.    {
  69.       for (int j=1; j<i; j++) cout << "\t";
  70.       if (i<=nc) cout << X(i,i) << "\t";
  71.       cout << "\n";
  72.    }
  73.    cout << flush; ++PCZ;
  74. }
  75.  
  76. void Print(const SymmetricMatrix& X)
  77. {
  78.    ++PCN;
  79.    cout << "\nMatrix type: " << (char*)X.Type() << " (";
  80.    cout << X.Nrows() << ", ";
  81.    cout << X.Ncols() << ")\n\n";
  82.    if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; }
  83.    int nr=X.Nrows(); int nc=X.Ncols();
  84.    for (int i=1; i<=nr; i++)
  85.    {
  86.       for (int j=1; j<i; j++) cout << X(j,i) << "\t";
  87.       for (j=i; j<=nc; j++)  cout << X(i,j) << "\t";
  88.       cout << "\n";
  89.    }
  90.    cout << flush; ++PCZ;
  91. }
  92.  
  93. void Print(const LowerTriangularMatrix& X)
  94. {
  95.    ++PCN;
  96.    cout << "\nMatrix type: " << (char*)X.Type() << " (";
  97.    cout << X.Nrows() << ", ";
  98.    cout << X.Ncols() << ")\n\n";
  99.    if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; }
  100.    int nr=X.Nrows();
  101.    for (int i=1; i<=nr; i++)
  102.    {
  103.       for (int j=1; j<=i; j++) cout << X(i,j) << "\t";
  104.       cout << "\n";
  105.    }
  106.    cout << flush; ++PCZ;
  107. }
  108.  
  109.  
  110. void Clean(Matrix& A, Real c)
  111. {
  112.    int nr = A.Nrows(); int nc = A.Ncols();
  113.    for (int i=1; i<=nr; i++)
  114.    {
  115.       for ( int j=1; j<=nc; j++)
  116.       { Real a = A(i,j); if ((a < c) && (a > -c)) A(i,j) = 0.0; }
  117.    }
  118. }
  119.  
  120. void Clean(DiagonalMatrix& A, Real c)
  121. {
  122.    int nr = A.Nrows();
  123.    for (int i=1; i<=nr; i++)
  124.    { Real a = A(i,i); if ((a < c) && (a > -c)) A(i,i) = 0.0; }
  125. }
  126.  
  127.  
  128. void trymat1(); void trymat2(); void trymat3();
  129. void trymat4(); void trymat5(); void trymat6();
  130. void trymat7(); void trymat8(); void trymat9();
  131. void trymata(); void trymatb(); void trymatc();
  132. void trymatd(); void trymate(); void trymatf();
  133. void trymatg(); void trymath(); void trymati();
  134.  
  135. main()
  136. {
  137.    Real* s1; Real* s2; Real* s3; Real* s4;
  138.    cout << "\nBegin test\n";   // Forces cout to allocate memory at beginning
  139.    { Matrix A1(25,200); s1 = A1.Store(); }
  140.    { Matrix A1(1,1); s3 = A1.Store(); }
  141.    {
  142.       Tracer et("Matrix test program");
  143.  
  144.       Matrix A(25,150);
  145.       {
  146.      int i;
  147.      RowVector A(8);
  148.      for (i=1;i<=7;i++) A(i)=0.0; A(8)=1.0;
  149.      Print(A);
  150.       }
  151.       cout << "\n";
  152.  
  153.       TestTypeAdd(); TestTypeMult(); TestTypeOrder();
  154.  
  155.       trymat1();
  156.       trymat2();
  157.       trymat3();
  158.       trymat4();
  159.       trymat5();
  160.       trymat6();
  161.       trymat7();
  162.       trymat8();
  163.       trymat9();
  164.       trymata();
  165.       trymatb();
  166.       trymatc();
  167.       trymatd();
  168.       trymate();
  169.       trymatf();
  170.       trymatg();
  171.       trymath();
  172.       trymati();
  173.    }
  174.    { Matrix A1(25,200); s2 = A1.Store(); }
  175.    cout << "\nChecking for lost memory: "
  176.       << (unsigned long)s1 << " " << (unsigned long)s2 << " ";
  177.    if (s1 != s2) cout << " - error\n"; else cout << " - ok\n\n";
  178.    { Matrix A1(1,1); s4 = A1.Store(); }
  179.    cout << "\nChecking for lost memory: "
  180.       << (unsigned long)s3 << " " << (unsigned long)s4 << " ";
  181.    if (s3 != s4) cout << " - error\n"; else cout << " - ok\n\n";
  182. #ifdef DO_FREE_CHECK
  183.    FreeCheck::Status();
  184. #endif 
  185.    return 0;
  186. }
  187.