[FEAT] Add extra matrix tests

This commit is contained in:
NADAL Jean-Baptiste
2024-02-05 14:28:40 +01:00
parent f847550686
commit 1dac519cf8
3 changed files with 80 additions and 18 deletions

View File

@@ -119,7 +119,10 @@ bool Matrix::operator==(const Matrix &a_matrix) const
++the_elem1, ++the_elem2) ++the_elem1, ++the_elem2)
{ {
if (!double_equal(*the_elem1, *the_elem2)) if (!double_equal(*the_elem1, *the_elem2))
{
printf("Elem: %f != %f\n", *the_elem1, *the_elem2);
return false; return false;
}
} }
} }
return true; return true;
@@ -201,7 +204,7 @@ bool Matrix::transpose(void)
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
double Matrix::determinant(void) double Matrix::determinant(void) const
{ {
double the_det = 0; double the_det = 0;
@@ -218,7 +221,7 @@ double Matrix::determinant(void)
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
Matrix Matrix::sub_matrix(uint8_t a_row, uint8_t a_col) Matrix Matrix::sub_matrix(uint8_t a_row, uint8_t a_col) const
{ {
Matrix the_sub = *this; Matrix the_sub = *this;
@@ -236,14 +239,14 @@ Matrix Matrix::sub_matrix(uint8_t a_row, uint8_t a_col)
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
double Matrix::minor(uint8_t a_row, uint8_t a_col) double Matrix::minor(uint8_t a_row, uint8_t a_col) const
{ {
return sub_matrix(a_row, a_col).determinant(); return sub_matrix(a_row, a_col).determinant();
} }
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
double Matrix::cofactor(uint8_t a_row, uint8_t a_col) double Matrix::cofactor(uint8_t a_row, uint8_t a_col) const
{ {
int8_t an_inverter = 1; int8_t an_inverter = 1;
@@ -257,7 +260,7 @@ double Matrix::cofactor(uint8_t a_row, uint8_t a_col)
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
bool Matrix::invertible(void) bool Matrix::invertible(void) const
{ {
if (determinant() == 0) if (determinant() == 0)
return false; return false;
@@ -267,7 +270,7 @@ bool Matrix::invertible(void)
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
std::tuple<Matrix, bool> Matrix::inverse(void) Matrix Matrix::inverse(void) const
{ {
Matrix the_result(m_rows, m_cols); Matrix the_result(m_rows, m_cols);
bool the_status = false; bool the_status = false;
@@ -275,7 +278,7 @@ std::tuple<Matrix, bool> Matrix::inverse(void)
if (!invertible()) if (!invertible())
{ {
return {the_result, false}; return the_result;
} }
the_determinant = determinant(); the_determinant = determinant();
@@ -290,7 +293,7 @@ std::tuple<Matrix, bool> Matrix::inverse(void)
} }
} }
return {the_result, true}; return the_result;
} }
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */

View File

@@ -61,14 +61,14 @@ namespace Raytracer
Matrix &operator=(const Matrix &a_matrix); Matrix &operator=(const Matrix &a_matrix);
bool transpose(void); bool transpose(void);
double determinant(void); double determinant(void) const;
Matrix sub_matrix(uint8_t a_row, uint8_t a_col); Matrix sub_matrix(uint8_t a_row, uint8_t a_col) const;
double minor(uint8_t a_row, uint8_t a_col); double minor(uint8_t a_row, uint8_t a_col) const;
double cofactor(uint8_t a_row, uint8_t a_col); double cofactor(uint8_t a_row, uint8_t a_col) const;
bool invertible(void); bool invertible(void) const;
std::tuple<Matrix, bool> inverse(void); Matrix inverse(void) const;
static Matrix identity(void); static Matrix identity(void);

View File

@@ -385,20 +385,79 @@ TEST_CASE("[Matrix] Calculating the inverse of a matrix", "[Matrix]")
{ 1, -3, 7, 4} { 1, -3, 7, 4}
}; };
Matrix b_inverted = { Matrix a_inverted = {
{ 0.21805, 0.45113, 0.24060, -0.04511}, { 0.21805, 0.45113, 0.24060, -0.04511},
{-0.80827, -1.45677, -0.44361, 0.52068}, {-0.80827, -1.45677, -0.44361, 0.52068},
{-0.07895, -0.22368, -0.05263, 0.19737}, {-0.07895, -0.22368, -0.05263, 0.19737},
{-0.52256, -0.81391, -0.30075, 0.30639} {-0.52256, -0.81391, -0.30075, 0.30639}
}; };
auto [b, the_status] = a.inverse(); Matrix b = a.inverse();
REQUIRE(the_status == true);
REQUIRE(a.determinant() == 532); REQUIRE(a.determinant() == 532);
REQUIRE(a.cofactor(2, 3) == -160); REQUIRE(a.cofactor(2, 3) == -160);
REQUIRE(b[3][2] == -160.0 / 532.0); REQUIRE(b[3][2] == -160.0 / 532.0);
REQUIRE(a.cofactor(3, 2) == 105); REQUIRE(a.cofactor(3, 2) == 105);
REQUIRE(b[2][3] == 105.0 / 532.0); REQUIRE(b[2][3] == 105.0 / 532.0);
REQUIRE(b == b_inverted); REQUIRE(b == a_inverted);
}
/* ------------------------------------------------------------------------- */
TEST_CASE("[Matrix] Calculating the inverse of another matrix", "[Matrix]")
{
Matrix a = {
{ 8, -5, 9, 2},
{ 7, 5, 6, 1},
{-6, 0, 9, 6},
{-3, 0, -9, -4}
};
Matrix a_inverted = {
{-0.15385, -0.15385, -0.28205, -0.53846},
{-0.07692, 0.12308, 0.02564, 0.03077},
{ 0.35897, 0.35897, 0.43590, 0.92308},
{-0.69231, -0.69231, -0.76923, -1.92308}
};
REQUIRE(a.inverse() == a_inverted);
}
/* ------------------------------------------------------------------------- */
TEST_CASE("[Matrix] Calculating the inverse of third matrix", "[Matrix]")
{
Matrix a = {
{ 9, 3, 0, 9},
{-5, -2, -6, -3},
{-4, 9, 6, 4},
{-7, 6, 6, 2}
};
Matrix a_inverted = {
{-0.04074, -0.07778, 0.14444, -0.22222},
{-0.07778, 0.03333, 0.36667, -0.33333},
{-0.02901, -0.14630, -0.10926, 0.12963},
{ 0.17778, 0.06667, -0.26667, 0.33333}
};
REQUIRE(a.inverse() == a_inverted);
}
/* ------------------------------------------------------------------------- */
TEST_CASE("[Matrix] Multiplying a product by its inverse", "[Matrix]")
{
Matrix a = {
{ 3, -9, 7, 3},
{ 3, -8, 2, -9},
{-4, 4, 4, 1},
{-6, 5, -1, 1}
};
Matrix b = {
{8, 2, 2, 2},
{3, -1, 7, 0},
{7, 0, 5, 4},
{6, -2, 0, 5}
};
Matrix c = a * b;
REQUIRE(c * b.inverse() == a);
} }