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

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