ulfben icon

submatrix first version

ulfben | PRO | 02/05/23 07:50:46 AM UTC | 0 ⭐ | 1693 👁️ | Never ⏰ | []
C++ |

950 B

|

None

|

0 👍

/

0 👎

//submatrix first version
 
constexpr Matrix2 submatrix(const Matrix3& m, Matrix3::size_type row_, Matrix3::size_type column_) {
    assert(row_ < m.rows() && column_ < m.columns() && "invalid submatrix specification. row and column must be inside the input matrix.");
    using size_type = Matrix3::size_type;
    Matrix2 r;
    size_type i = 0;    
    for (size_type k = 0; k < m.rows(); k++) {
        if (k == row_) {
            continue;
        }
        size_type j = 0;
        for (size_type l = 0; l < m.columns(); l++) {
            if (l == column_) {
                continue;
            }
            r(i, j) = m(k, l);
            j++;
        }
        i++;
    }
    return r;
}
 
 
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);
    EXPECT_TRUE(b == truth);
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎