Advertisement
ulfben

submatrix first version

Feb 5th, 2023
623
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. //submatrix first version
  2.  
  3. constexpr Matrix2 submatrix(const Matrix3& m, Matrix3::size_type row_, Matrix3::size_type column_) {
  4.     assert(row_ < m.rows() && column_ < m.columns() && "invalid submatrix specification. row and column must be inside the input matrix.");
  5.     using size_type = Matrix3::size_type;
  6.     Matrix2 r;
  7.     size_type i = 0;    
  8.     for (size_type k = 0; k < m.rows(); k++) {
  9.         if (k == row_) {
  10.             continue;
  11.         }
  12.         size_type j = 0;
  13.         for (size_type l = 0; l < m.columns(); l++) {
  14.             if (l == column_) {
  15.                 continue;
  16.             }
  17.             r(i, j) = m(k, l);
  18.             j++;
  19.         }
  20.         i++;
  21.     }
  22.     return r;
  23. }
  24.  
  25.  
  26. TEST(Matrix, submatrixOf3x3is2x2) {
  27.     const Matrix3 a{
  28.         1,5,0,
  29.         -3,2,7,
  30.         0,6,-3
  31.     };
  32.     const Matrix2 truth{
  33.         -3, 2,
  34.         0, 6
  35.     };
  36.     const auto b = submatrix(a, 0, 2);
  37.     EXPECT_TRUE(b == truth);
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement