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

  1. ////////////////////////////////////////////////////////////////
  2. // tstmat3.cpp: Matrix test program using sample math routines.
  3. // Copyright(c) 1993 Azarona Software. All rights reserved.
  4. ////////////////////////////////////////////////////////////////
  5. #include <iostream.h>
  6. #include <iomanip.h>
  7. #include "matrix.h"
  8.  
  9. // We'll set up the type of number we'd like to store 
  10. // in the matrices
  11.  
  12. typedef double Number;
  13.  
  14. // Choose multiply routine you would like to use for test
  15. // Test4 = Uses double subscripting approach
  16. // Test5 = Uses 2-d subscripting approach
  17. // Test6 = Uses lowest level and fastest 
  18.  
  19. #define MatMult Test6
  20.  
  21.  
  22. // Matrix multiply routines. Include these directly into
  23. // your test programs. (Not a real library-style source file.)
  24.  
  25. template<class TYPE>
  26. void Test4(Matrix<TYPE> &c, const Matrix<TYPE> &a, const Matrix<TYPE> &b)
  27. // Uses dynamically allocated Matrix objects with double-subscripting.
  28. // Really S--L--O--W.
  29. {
  30.   unsigned i, j, k;
  31.   for (i = 0; i < a.NRows(); i++) {
  32.       for (j = 0; j < b.NCols(); j++) {
  33.           TYPE sum = 0;
  34.           for (k = 0; k < b.NRows(); k++) sum += a[i][k] * b[k][j];
  35.           c[i][j] = sum;
  36.       }
  37.   }
  38. }
  39.  
  40. template<class TYPE>
  41. void Test5(Matrix<TYPE> &c, const Matrix<TYPE> &a, const Matrix<TYPE> &b)
  42. // Uses dynamically allocated Matrix objects with 2-d subscripting.
  43. // Better than Test4, but still slow.
  44. {
  45.   unsigned i, j, k;
  46.  
  47.   for (i = 0; i < a.NRows(); i++) {
  48.       for (j = 0; j < b.NCols(); j++) {
  49.           TYPE sum = 0;
  50.           for (k = 0; k < b.NRows(); k++) sum += a(i,k) * b(k,j);
  51.           c(i,j) = sum;
  52.       }
  53.   }
  54. }
  55.  
  56.  
  57. template<class TYPE>
  58. void Test6(Matrix<TYPE> &c, const Matrix<TYPE> &a, const Matrix<TYPE> &b)
  59. // Fastest routine for Matrix objects, using vector pointers.
  60. // Should be pretty decent.
  61. {
  62.   unsigned i, j, k;
  63.  
  64.   VecPtr<const TYPE> ar = a.RowPtr();
  65.   VecPtr<const TYPE> ac = a.ColPtr();
  66.   VecPtr<const TYPE> br = b.RowPtr();
  67.   VecPtr<const TYPE> bc = b.ColPtr();
  68.   VecPtr<TYPE> cr = c.RowPtr();
  69.   VecPtr<TYPE> cc = c.ColPtr();
  70.   
  71.   const TYPE *bstart = b.PtrToAll();
  72.  
  73.   for (i = 0; i < a.NRows(); i++) {
  74.       cr = cc; // Point to start of row i
  75.       br = bstart;
  76.       for (j = 0; j < b.NCols(); j++) {
  77.           TYPE sum = 0;
  78.           ar = ac; // Point to start of row i
  79.           bc = br; // Point to start of col j
  80.           for (k = 0; k < b.NRows(); k++) {
  81.                sum += *ar * *bc;
  82.                ar++; // Next col
  83.                bc++; // Next row
  84.           }
  85.           br++; // Next col
  86.           *cr = sum;
  87.           cr++; // Next col
  88.       }
  89.       ac++; // Next row
  90.       cc++; // Next row
  91.   }
  92. }
  93.  
  94. template<class TYPE>
  95. Matrix<TYPE> operator*(const Matrix<TYPE> &a, const Matrix<TYPE> &b)
  96. // Multiplies two matrices together and returns a resulting matrix.
  97. // If matrices don't conform, a message is printed and the resulting
  98. // matrix is set to all zeros.
  99. {
  100.   Matrix<TYPE> r(a.NRows(), b.NCols()); // Create resulting matrix
  101.   MatMult(r, a, b);
  102.   return r;
  103. }
  104.  
  105. // BC++ seems to need this
  106. void MatMult(Matrix<Number> &r,
  107.              const Matrix<Number> &a, const Matrix<Number> &b);
  108.  
  109. template<class TYPE>
  110. void PrtMat(const Matrix<TYPE> &m)
  111. // We'll test 2D subscripting here while we're at it.
  112. {
  113.   for (int i = 0; i<m.NRows(); i++) {
  114.       for (int j = 0; j<m.NCols(); j++) {
  115.           cout << setw(3) << m(i,j) << ' ';
  116.       }
  117.       cout << '\n';
  118.   }
  119. }
  120.  
  121. typedef Vector<Number> vec;
  122. typedef Matrix<Number> mat;
  123.  
  124. // You need one of these statements for each type of vector:
  125.  
  126. INITNULLVEC(Number);
  127.  
  128. // Create matrices out of raw data
  129.  
  130. Number arra[3*5] = {
  131.    1,  2,  3,  4,  5,
  132.    6,  7,  8,  9, 10,
  133.   11, 12, 13, 14, 15
  134. };
  135.  
  136. mat a(3, 5, arra);
  137.  
  138. Number arrb[5*2] = {
  139.   1, 2,
  140.   3, 4,
  141.   5, 6,
  142.   7, 8,
  143.   9, 10
  144. };
  145.  
  146. mat b(5, 2, arrb);
  147.  
  148.  
  149. void test()
  150. {
  151.  
  152.   cout << "Multiplying two whole matrices:\n\n";
  153.   cout << "a: \n";
  154.   PrtMat(a); cout << '\n';
  155.  
  156.   cout << "b: \n";
  157.   PrtMat(b); cout << '\n';
  158.  
  159.   mat r = a * b;
  160.  
  161.   cout << "r: \n";
  162.   PrtMat(r); cout << '\n';
  163.  
  164.   cout << "Multiplying two sub-matrices:\n\n";
  165.  
  166.   mat suba(a, SHARED, 0, 1, 2, 3);
  167.   mat subb(b, SHARED, 2, 0, 3, 2);
  168.   cout << "sub-a: \n";
  169.   PrtMat(suba); cout << '\n';
  170.  
  171.   cout << "sub-b: \n";
  172.   PrtMat(subb); cout << '\n';
  173.  
  174.   cout << "sub-r: \n";
  175.   PrtMat(suba * subb); cout << '\n';
  176. }
  177.  
  178. main()
  179. {
  180.   test();
  181.   return 0;
  182. }
  183.