Add some more mat3 functions

This commit is contained in:
Alex Selimov 2026-10-26 23:28:32 -04:00
parent 55ba9f6cde
commit 7e5de6f6eb
3 changed files with 108 additions and 0 deletions

View file

@ -97,6 +97,44 @@ int test_mat3_add() {
struct Mat3 expected_c = {{1, 3, 3}, {0, 2, 6}, {0, 0, 2}};
return mat3_eql_tol(expected_c, mat3_add(a, b), 1e-12);
}
int test_mat3_sub() {
struct Mat3 a = {{1, 2, 3}, {0, 1, 5}, {0, 0, 0}};
struct Mat3 b = {{0, 1, 0}, {0, 1, 1}, {0, 0, 2}};
struct Mat3 expected_c = {{1, 1, 3}, {0, 0, 4}, {0, 0, -2}};
return mat3_eql_tol(expected_c, mat3_sub(a, b), 1e-12);
}
int test_mat3_mul() {
struct Mat3 a = {{1, 2, 3}, {0, 1, 5}, {0, 0, 9}};
struct Mat3 b = {{0, 1, 0}, {0, 1, 1}, {0, 0, 1}};
struct Mat3 expected_c = {{0, 3, 5}, {0, 1, 6}, {0, 0, 9}};
return mat3_eql_tol(expected_c, mat3_mul(a, b), 1e-12);
}
int test_mat3_det() {
struct Mat3 a = {{1, 2, 3}, {4, 1, 5}, {3, 1, 9}};
double expected_det = -35.0;
return fabs(mat3_det(a) - expected_det) < 1e-12;
}
int test_mat3_inv() {
struct Mat3 a = {
{1.0, 2.0, 3.0},
{4.0, 1.0, 5.0},
{3.0, 1.0, 9.0},
};
struct Mat3 expected_inv = {{-4.0 / 35.0, 3.0 / 7.0, -1.0 / 5.0},
{3.0 / 5.0, 0.0, -1.0 / 5.0},
{-1.0 / 35.0, -1.0 / 7.0, 1.0 / 5.0}};
struct Mat3 inv_a = mat3_inv(a);
return mat3_eql_tol(inv_a, expected_inv, 1e-11);
}
int main() {
int tests_total = 0;
int tests_passed = 0;
@ -110,6 +148,10 @@ int main() {
run_test(test_mat3_identity);
run_test(test_mat3_eql);
run_test(test_mat3_add);
run_test(test_mat3_sub);
run_test(test_mat3_mul);
run_test(test_mat3_det);
run_test(test_mat3_inv);
if (tests_total == tests_passed) {
printf("\033[92mAll tests passed!\033[0m");