[FEAT] Add Submatrix

This commit is contained in:
NADAL Jean-Baptiste
2024-02-02 17:41:51 +01:00
parent 7d52592cd5
commit a584bf54d8
3 changed files with 57 additions and 0 deletions

View File

@@ -208,6 +208,24 @@ double Matrix::determinant(void)
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
Matrix Matrix::sub_matrix(uint8_t a_rows, uint8_t a_cols)
{
Matrix the_sub = *this;
the_sub.m_data.erase(the_sub.m_data.begin() + a_rows);
the_sub.m_rows--;
for (int the_row = 0; the_row < the_sub.m_rows; the_row++)
{
the_sub.m_data[the_row].erase(the_sub.m_data[the_row].begin() + a_cols);
}
the_sub.m_cols--;
return the_sub;
}
/* ------------------------------------------------------------------------- */
Matrix Matrix::identity(void) Matrix Matrix::identity(void)
{ {
Matrix the_identity = { Matrix the_identity = {

View File

@@ -61,6 +61,7 @@ namespace Raytracer
bool transpose(void); bool transpose(void);
double determinant(void); double determinant(void);
Matrix sub_matrix(uint8_t a_rows, uint8_t a_cols);
static Matrix identity(void); static Matrix identity(void);

View File

@@ -239,3 +239,41 @@ TEST_CASE("[Matrix] Calculating the determinant of a 2x2 matrix", "[Matrix]")
REQUIRE(a.determinant() == 17); REQUIRE(a.determinant() == 17);
} }
/* ------------------------------------------------------------------------- */
TEST_CASE("[Matrix] A submatrix of a 3x3 matrix is a 2x2 matrix", "[Matrix]")
{
Matrix a = {
{ 1, 5, 0},
{-3, 2, 7},
{ 0, 6, -3}
};
Matrix b = {
{-3, 2},
{ 0, 6}
};
REQUIRE(a.sub_matrix(0, 2) == b);
}
/* ------------------------------------------------------------------------- */
TEST_CASE("[Matrix] A submatrix of a 4x4 matrix is a 3x3 matrix", "[Matrix]")
{
Matrix a = {
{-6, 1, 1, 6},
{-8, 5, 8, 6},
{-1, 0, 8, 2},
{-7, 1, -1, 1}
};
Matrix b = {
{-6, 1, 6},
{-8, 8, 6},
{-7, -1, 1}
};
REQUIRE(a.sub_matrix(2, 1) == b);
}