//submatrix second version constexpr value_type operator[](size_type i) const noexcept { assert(i < size()); return _data[i]; } constexpr reference operator[](size_type i) noexcept { assert(i < size()); return _data[i]; } constexpr size_type index_to_column(size_type index) const noexcept { assert(index < size()); return index % columns(); } constexpr size_type index_to_row(size_type index) const noexcept { assert(index < size()); return index / columns(); } constexpr auto submatrix(size_type remove_row, size_type remove_column) const noexcept { assert(remove_row < rows() && remove_column < columns() && "submatrix: row and column to remove must be inside the original matrix."); Matrix r; uint8_t ri = 0; for (size_type i = 0; i < size(); i++) { if(index_to_row(i) != remove_row && index_to_column(i) != remove_column) { r[ri++] = _data[i]; } } return r; } TEST(Matrix, getRowFromIndex) { const Matrix3 a{ 1,5,0, -3,2,7, 0,6,-3 }; EXPECT_EQ(0, a.index_to_row(0)); EXPECT_EQ(0, a.index_to_row(2)); EXPECT_EQ(2, a.index_to_row(6)); EXPECT_EQ(2, a.index_to_row(8)); } TEST(Matrix, getColumnFromIndex) { const Matrix3 a{ 1,5,0, -3,2,7, 0,6,-3 }; EXPECT_EQ(0, a.index_to_column(0)); EXPECT_EQ(1, a.index_to_column(1)); EXPECT_EQ(2, a.index_to_column(2)); EXPECT_EQ(0, a.index_to_column(6)); EXPECT_EQ(1, a.index_to_column(7)); EXPECT_EQ(2, a.index_to_column(8)); } TEST(Matrix, submatrixOf3x3is2x2) { const Matrix3 a{ 1,5,0, -3,2,7, 0,6,-3 }; const Matrix2 truth{ -3, 2, 0, 6 }; const auto b = submatrix(a, 0, 2); const auto c = a.submatrix(0, 2); EXPECT_TRUE(b == truth); EXPECT_TRUE(c == truth); }