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

  1. /////////////////////////////////////////////////////////////
  2. // tstmat.cpp: Matrix test program using integers.
  3. // Copyright(c) 1993 Azarona Software. All rights reserved.
  4. /////////////////////////////////////////////////////////////
  5. #include <iostream.h>
  6. #include <iomanip.h>
  7. #include "matrix.h"
  8.  
  9. template<class TYPE>
  10. void PrtMat(const Matrix<TYPE> &m)
  11. // We'll test 2D subscripting here while we're at it.
  12. {
  13.   for (int i = 0; i<m.NRows(); i++) {
  14.       for (int j = 0; j<m.NCols(); j++) {
  15.           cout << setw(3) << m(i,j) << ' ';
  16.       }
  17.       cout << '\n';
  18.   }
  19. }
  20.  
  21. template<class TYPE>
  22. void PrtVec(const Vector<TYPE> &v)
  23. {
  24.   VecPtr<const TYPE> elemptr(v.PtrToAll());
  25.   unsigned i, len = v.Length();
  26.   for (i = 0; i<len; i++) {
  27.       cout << *elemptr << ' ';
  28.       elemptr++;
  29.   }
  30.   cout << '\n';
  31. }
  32.  
  33. // BC++ needs the next statements
  34.  
  35. void PrtMat(const Matrix<int> &m);
  36. void PrtVec(const Vector<int> &v);
  37.  
  38. // You need one of these statements for each type of vector:
  39.  
  40. INITNULLVEC(int);
  41.  
  42. #ifdef ONE_WAY
  43. // A low-level C array we'll copy and transform into matrix
  44. const int arr_size = 56;
  45. int arr[arr_size];
  46. #endif
  47.      
  48. void test()
  49. {
  50.   int i;
  51.  
  52. #ifdef ONE_WAY
  53.   // Set up low level array to be transformed to a matrix
  54.   for (i = 0; i<arr_size; i++) arr[i] = i;
  55. #endif
  56.  
  57.   cout << "Creating a matrix\n";
  58.  
  59. #ifdef ONE_WAY
  60.   Matrix<int> mymat(7, 8, arr); 
  61. #else
  62.   Matrix<int> mymat(7, 8);
  63.   Vector<int> myvec(mymat.All());
  64.   // Testing 1D vector subscripting too
  65.   for(i = 0; i<myvec.Length(); i++) myvec[i] = i;
  66. #endif
  67.   cout << "mymat: \n"; 
  68.   PrtMat(mymat); cout << '\n';
  69.  
  70.   cout << "Creating copy of the matrix\n";
  71.  
  72. #ifdef ONE_WAY
  73.   Matrix<int> matii(mymat, COPIED);
  74. #else
  75.   /// ANOTHER WAY: Matrix<int> matii(mymat.Clone());
  76.   Matrix<int> matii; // Create a null matrix
  77.   matii = mymat.Clone();  
  78. #endif
  79.   cout << "matii: \n"; 
  80.   PrtMat(matii); cout << '\n';
  81.  
  82.  
  83.   cout << "Creating a share of the diagonal\n";
  84.  
  85.   Vector<int> Diag = mymat.Diag();
  86.   PrtVec(Diag);
  87.  
  88.   cout << "Setting shared diagonal = 1\n";
  89.   Diag = 1;
  90.   cout << "mymat: \n"; 
  91.   PrtMat(mymat); cout << '\n';
  92.  
  93.   cout << "Creating a copy of the diagonal\n";
  94.  
  95.   Vector<int> Diag2 = mymat.Diag(COPIED);
  96.   PrtVec(Diag2);
  97.  
  98.   cout << "Setting copied diagonal = 99\n";
  99.   Diag2 = 99;
  100.   PrtVec(Diag2); cout << '\n';
  101.   cout << "mymat: \n"; 
  102.   PrtMat(mymat); cout << '\n';
  103.  
  104.  
  105.   cout << "Creating a share of row 3\n";
  106.   Vector<int> Row = mymat.Row(3);
  107.   PrtVec(Row);
  108.  
  109.   cout << "Setting row 3 to 17's\n";
  110.   Row = 17;
  111.   cout << "mymat: \n"; 
  112.   PrtMat(mymat); cout << '\n';
  113.  
  114.   cout << "Creating a share of col 5\n";
  115.   Vector<int> Col = mymat.Col(5);
  116.   PrtVec(Col);
  117.  
  118.   cout << "Setting col 5 to 42's\n";
  119.   Col = 42;
  120.   cout << "mymat: \n"; 
  121.   PrtMat(mymat);
  122.  
  123.   cout << "\nUsing the copy of the original matrix\n";
  124.   cout << "matii: \n"; 
  125.   PrtMat(matii); cout << '\n';
  126.  
  127.   cout << "Transposing this copy\n";
  128.   matii = matii.Transpose();
  129.   cout << "matii: \n"; 
  130.   PrtMat(matii); cout << '\n';
  131.  
  132.   cout << "Set row 2 to 55's\n";
  133.   matii.Row(2) = 55;
  134.   cout << "matii: \n"; 
  135.   PrtMat(matii); cout << '\n';
  136.  
  137.   cout << "Set col 3 to 77's\n";
  138.   matii.Col(3) = 77;
  139.   cout << "matii: \n"; 
  140.   PrtMat(matii); cout << '\n';
  141.  
  142.   cout << "Creating shared submatrix of transpose\n";
  143.  
  144.   Matrix<int> submat(matii, SHARED, 2, 1, 3, 4);
  145.   cout << "submat: \n"; 
  146.   PrtMat(submat); cout << '\n';
  147.  
  148.   cout << "Transposing the submatrix\n";
  149.   submat = submat.Transpose();
  150.   cout << "submat: \n"; 
  151.   PrtMat(submat); cout << '\n';
  152.  
  153.   cout << "\nTesting a[][] form of subscripting\n";
  154.  
  155.   cout << "submat[3][2] = " << submat[3][2] << "\n\n";
  156.  
  157.   cout << "Setting diagonal of submatrix to 0\n";
  158.   submat.Diag() = 0;
  159.   cout << "submat: \n"; 
  160.   PrtMat(submat); cout << '\n';
  161.  
  162.   cout << "Setting second row of submatrix to 99's\n";
  163.   submat.Row(1) = 99;
  164.   cout << "submat: \n"; 
  165.   PrtMat(submat); cout << '\n';
  166.  
  167.   cout << "matii: \n"; 
  168.   PrtMat(matii); cout << '\n';
  169.  
  170.   cout << "Creating sub-sub-matrix\n";
  171.  
  172.   Matrix<int> subsubmat(submat, SHARED, 1, 1, 3, 2);
  173.   cout << "subsubmat: \n"; 
  174.   PrtMat(subsubmat); cout << '\n';
  175.  
  176.   cout << "Set rows of subsubmatrix to 91's, 92's and 93's\n";
  177.  
  178.   subsubmat.Row(0) = 91;
  179.   subsubmat.Row(1) = 92;
  180.   subsubmat.Row(2) = 93;
  181.   cout << "subsubmat: \n"; 
  182.   PrtMat(subsubmat); cout << '\n';
  183.   cout << "matii: \n"; 
  184.   PrtMat(matii); cout << '\n';
  185.  
  186.   cout << "Set diagonal of subsubmatrix to 77's\n";
  187.  
  188.   subsubmat.Diag() = 77;
  189.   cout << "subsubmat: \n"; 
  190.   PrtMat(subsubmat); cout << '\n';
  191.   cout << "matii: \n"; 
  192.   PrtMat(matii); cout << '\n';
  193.  
  194.   cout << "Testing sub-sub-matrix transpose\n";
  195.  
  196.   subsubmat = subsubmat.Transpose();
  197.   cout << "subsubmat transposed: \n";
  198.   PrtMat(subsubmat); cout << '\n';
  199.  
  200.   cout << "Set all elements of sub-sub-matrix to 42's\n";
  201.   subsubmat = 42;
  202.   cout << "subsubmat: \n"; 
  203.   PrtMat(subsubmat); cout << '\n';
  204.   cout << "matii: \n"; 
  205.   PrtMat(matii); cout << '\n';
  206. }
  207.  
  208. main()
  209. {
  210.   test();
  211.   return 0;
  212. }
  213.  
  214.