home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / code / bcpp / file19 / tstsmat.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  3.7 KB  |  143 lines

  1. /////////////////////////////////////////////////////////////
  2. // tstsmat.cpp: A test program for the SimpleMatrix class.
  3. // Copyright(c) 1993 Azarona Software. All rights reserved.
  4. /////////////////////////////////////////////////////////////
  5. #include <iostream.h>
  6. #include <iomanip.h>
  7. #include "simpmat.h"
  8.  
  9. template<class TYPE>
  10. void PrtMat(SimpleMatrix<TYPE> &m)
  11. // Prints out all elements of a matrix. 
  12. // Tests subscripting operation as well.
  13. {
  14.   for (int i = 0; i<m.NRows(); i++) {
  15.       for (int j = 0; j<m.NCols(); j++) {
  16.           cout << setw(3) << m[i][j] << ' ';
  17.       }
  18.       cout << '\n';
  19.   }
  20. }
  21.  
  22. template<class TYPE>
  23. void SetVec(VecPtr<TYPE> &v, unsigned n, const TYPE &x)
  24. // Sets all elements of a vector to x. Assumes
  25. // TYPE as '=' defined. Uses fastest form of vector
  26. // element access.
  27. {
  28.   unsigned i;
  29.  
  30.   for (i = 0; i<n; i++) {
  31.       *v = x;
  32.       v++;
  33.   }
  34. }
  35.  
  36. template<class TYPE>
  37. void MatMult(SimpleMatrix<TYPE> &c, const SimpleMatrix<TYPE> &a, const SimpleMatrix<TYPE> &b)
  38. // Does a matrix multiply.
  39. {
  40.   unsigned i, j, k;
  41.  
  42.   if (a.NCols() == b.NRows() &&   // A & B must conform
  43.       c.NCols() == b.NCols() &&   // C must be right size too
  44.       c.NRows() == a.NRows()) {
  45.     for (i = 0; i < a.NRows(); i++) {
  46.         for (j = 0; j < b.NCols(); j++) {
  47.             TYPE sum = 0;
  48.             for (k = 0; k < b.NRows(); k++) sum += a[i][k] * b[k][j];
  49.             c[i][j] = sum;
  50.         }
  51.     }
  52.   }
  53.   else {
  54.     cout << "Matrices don't conform\n";
  55.   }
  56. }
  57.  
  58. // BC++ seems to need this prototype
  59.  
  60. void MatMult(SimpleMatrix<int> &c, const SimpleMatrix<int> &a, const SimpleMatrix<int> &b);
  61.  
  62. template<class TYPE>
  63. SimpleMatrix<TYPE> operator*(const SimpleMatrix<TYPE> &a, const SimpleMatrix<TYPE> &b)
  64. // Multiplies two matrices together and returns a resulting matrix.
  65. // If matrices don't conform, a message is printed and the resulting
  66. // matrix is set to all zeros.
  67. {
  68.   SimpleMatrix<TYPE> r(a.NRows(), b.NCols()); // Create resulting matrix
  69.   MatMult(r, a, b);
  70.   return r; // Copy constructor called. Bad news.
  71. }
  72.  
  73.  
  74. template<class TYPE>
  75. SimpleMatrix<TYPE> Transpose(const SimpleMatrix<TYPE> &m)
  76. // Returns the transpose of a matrix by interchanging
  77. // rows and columns.
  78. {
  79.   SimpleMatrix<TYPE> t(m.NCols(), m.NRows());
  80.  
  81.   for (unsigned i = 0; i<m.NRows(); i++)
  82.       for (unsigned j = 0; j<m.NCols(); j++) t[j][i] = m[i][j];
  83.  
  84.   return t; // Copy constructor called.
  85. }
  86.  
  87.  
  88. template<class TYPE>
  89. void TRYIT(const SimpleMatrix<TYPE> &m)
  90. // Try updating a constant matrix.
  91. {
  92.   unsigned i;
  93.   VecPtr<const TYPE> v = m.ColPtr(0);
  94.   
  95.   for (i = 0; i<m.NRows(); i++) {
  96.       // *v = 99; Can't do this
  97.       v++;
  98.   }
  99. }
  100.  
  101.  
  102. main()
  103. {
  104.   SimpleMatrix<int> m(3, 3);
  105.  
  106.   cout << "Setting all elements to 4's\n";
  107.   SetVec(m.PtrToAll(), m.NElems(), 4);
  108.   cout << "m:\n"; PrtMat(m); cout << '\n';
  109.  
  110.   cout << "Setting row 1 to all 2's\n";
  111.   SetVec(m.RowPtr(1), m.NCols(), 2);
  112.   cout << "m:\n"; PrtMat(m); cout << '\n';
  113.  
  114.   cout << "Setting column 1 to all 3's\n";
  115.   SetVec(m.ColPtr(3), m.NRows(), 3);
  116.   cout << "m:\n"; PrtMat(m); cout << '\n';
  117.  
  118.   cout << "Setting diagonal to 1's\n";
  119.   SetVec(m.DiagPtr(), m.NRows(), 1);
  120.   cout << "m:\n"; PrtMat(m); cout << '\n';
  121.  
  122.   // Do a matrix multiply with a 3x2 matrix
  123.  
  124.   SimpleMatrix<int> m2(3, 2), c(3,2); // Result is 3x2 as well
  125.  
  126.   unsigned k = 0;
  127.   for(unsigned i = 0; i<m2.NRows(); i++)
  128.      for (unsigned j = 0; j<m2.NCols(); j++) m2[i][j] = k++;
  129.  
  130.   cout << "m2:\n"; PrtMat(m2); cout << '\n';
  131.  
  132.   cout << "Multiplying m by m2\n";
  133.   c = m * m2;
  134.   cout << "c = m x m2:\n"; PrtMat(c); cout << '\n';
  135.  
  136.   // Transpose c
  137.   cout << "Transpose(c):\n"; PrtMat(Transpose(c)); cout << '\n';
  138.  
  139.   TRYIT(m); // Try updating a constant matrix
  140.  
  141.   return 0;
  142. }
  143.