Advertisement
Nicolas_Janin

Stupid Matrix class test

Dec 8th, 2013
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. // Copyright Nicolas Janin 2004 - 20013.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5.  
  6.  
  7. // Test case for http://pastebin.com/UE80Dpm1
  8.  
  9. #include <cmath>
  10. #include <iostream>
  11. #include <iomanip>
  12. #include <cstdlib>
  13. #include <ctime>
  14. #include "SMatrix.hpp"
  15.  
  16. using namespace std;
  17.  
  18.  
  19. typedef SMatrix<double> dmat;
  20.  
  21. // matrix display
  22. template <typename T>
  23. ostream& operator<<(ostream &os, SMatrix<T> &mat) {
  24. os << "(" << mat.rows() << "," << mat.cols() << ") =\n";
  25. os.setf(ios::fixed, ios::floatfield);
  26. os << setprecision(4);
  27. size_t L = mat.rows(), C = mat.cols();
  28. for(size_t l = 0; l < L; ++l) {
  29. os << "[ ";
  30. size_t c = 0;
  31. for(; c < C-1; ++c) os << setw(5) << mat(l, c) << '\t';
  32. os << setw(5) << mat(l, c) << " ]" << endl;
  33. }
  34. return os;
  35. }
  36.  
  37. double f1(double x){ return sqrt(fabs(x)); }
  38. double f2(double x, int i){ return (x * i); }
  39.  
  40.  
  41. int main(int argc, char* argv[])
  42. {
  43.  
  44. {
  45. dmat M(5, 5);
  46. cout << "M" << M << endl;
  47. M.fill(-1);
  48. cout << "M" << M << endl;
  49.  
  50. cout << "Divers acces en ecriture" << endl;
  51. for(size_t i = 1; i < M.rows(); ++i)
  52. for(size_t j = 0; j < M.cols(); ++j) M(i-1,j) = i / (j+1.);
  53. cout << "M" << M << endl;
  54.  
  55. cout << "M.tr()" << M.tr() << endl;
  56.  
  57. M.apply(&f1);
  58. cout << "M.apply(sqrt(fabs(elem))) " << M << endl;
  59. M.apply(&f2, 2);
  60. cout << "M x 2 " << M << endl;
  61. }
  62.  
  63. // Test dot product
  64. {
  65. double a[] = {1,2,0, 4,3,-1};
  66. double b[] = {5,1, 2,3, 3,4};
  67. dmat A(2,3,a);
  68. dmat AA(A);
  69. dmat B(3,2,b);
  70. cout << "A" << A << endl;
  71. cout << "B" << B << endl;
  72. A.dot(B);
  73. cout << "A.dot(B) = \n" << A << endl;
  74. B.dot(AA);
  75. cout << "B.dot(A) = \n" << B << endl;
  76. }
  77.  
  78. {
  79. dmat A(4,4); dmat B(4,4);
  80. for (int i = 0; i < A.rows(); i++)
  81. for (int j = 0; j < A.cols(); j++) {
  82. A(i,j) = i + j;
  83. B(i,j) = i - j;
  84. }
  85. cout << "Check the formula tr(AB) = tr(B).tr(A), with (" << A.rows() << "," << A.cols() << ") matrices" << endl;
  86. cout << "A" << A;
  87. cout << "B" << B;
  88. int t = clock();
  89. dmat AA(A);
  90. (A.dot(B)).tr();
  91. (B.tr()).dot(AA.tr());
  92. cout << "A" << A;
  93. cout << "B" << B;
  94. // check equality
  95. for (size_t i = 0; i < A.rows(); i++)
  96. for (size_t j = 0; j < A.cols(); j++)
  97. assert(A(i,j) == B(i,j));
  98. cout << "Done in " << (clock() - t)/1000 << " ms" <<endl;
  99. }
  100.  
  101. {
  102. cout << "Test arithmetic operations with scalars" << endl;
  103. double a[] = {1,2,3, 4,5,6};
  104. double b[] = {1,2, 3,4, 5,6};
  105. dmat A(2,3,a);
  106. dmat B(3,2,b);
  107.  
  108. cout << "A + 3" << A+3 << endl;
  109. cout << "A - 3" << A-3 << endl;
  110. cout << "B x 2" << B*2 << endl;
  111. cout << "B / 2" << B/2 << endl;
  112. }
  113.  
  114. {
  115. cout << "Test submatrix" << endl;
  116. double a[] = {1,2,3,4, 1,2,3,4, 1,2,3,4, 1,2,3,4};
  117. dmat A(4,4,a);
  118. dmat B(3,3);
  119.  
  120. A.submatrix(B,1,1,3,3);
  121. cout << "B" << B << endl;
  122.  
  123. cout << "Extract row and column" << endl;
  124. dmat row(1,4);
  125. dmat col(4,1);
  126. A.row(row, 2);
  127. A.column(col, 3);
  128. cout << "A.row(2) " << row << endl;
  129. cout << "A.col(3) " << col << endl;
  130. }
  131.  
  132. return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement