From 7e5de6f6eb8a6c8d73f09a011e4dbf039dcb91d2 Mon Sep 17 00:00:00 2001 From: Alex Selimov Date: Mon, 26 Oct 2026 23:28:32 -0400 Subject: [PATCH] Add some more mat3 functions --- mathprimitives/mathprimitives.c | 28 ++++++++++++++++++++++ mathprimitives/mathprimitives.h | 38 +++++++++++++++++++++++++++++ mathprimitives/test.c | 42 +++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 mathprimitives/mathprimitives.c diff --git a/mathprimitives/mathprimitives.c b/mathprimitives/mathprimitives.c new file mode 100644 index 0000000..0c5820e --- /dev/null +++ b/mathprimitives/mathprimitives.c @@ -0,0 +1,28 @@ +#include "mathprimitives.h" + +struct Mat3 mat3_inv(struct Mat3 a) { + double inv_det = 1.0 / mat3_det(a); + struct Vec3 r1 = vec3_scale( + (struct Vec3){ + a.r2.y * a.r3.z - a.r2.z * a.r3.y, + a.r1.z * a.r3.y - a.r1.y * a.r3.z, + a.r1.y * a.r2.z - a.r1.z * a.r2.y, + }, + inv_det); + struct Vec3 r2 = vec3_scale( + (struct Vec3){ + a.r2.z * a.r3.x - a.r2.x * a.r3.z, + a.r1.x * a.r3.z - a.r1.z * a.r3.x, + a.r1.z * a.r2.x - a.r1.x * a.r2.z, + }, + inv_det); + struct Vec3 r3 = vec3_scale( + (struct Vec3){ + a.r2.x * a.r3.y - a.r2.y * a.r3.x, + a.r1.y * a.r3.x - a.r1.x * a.r3.y, + a.r1.x * a.r2.y - a.r1.y * a.r2.x, + }, + inv_det); + + return (struct Mat3){r1, r2, r3}; +} diff --git a/mathprimitives/mathprimitives.h b/mathprimitives/mathprimitives.h index 96470c3..86c0d88 100644 --- a/mathprimitives/mathprimitives.h +++ b/mathprimitives/mathprimitives.h @@ -62,4 +62,42 @@ static inline struct Mat3 mat3_add(struct Mat3 a, struct Mat3 b) { return (struct Mat3){r1, r2, r3}; } +static inline struct Mat3 mat3_sub(struct Mat3 a, struct Mat3 b) { + struct Vec3 r1 = vec3_sub(a.r1, b.r1); + struct Vec3 r2 = vec3_sub(a.r2, b.r2); + struct Vec3 r3 = vec3_sub(a.r3, b.r3); + + return (struct Mat3){r1, r2, r3}; +} + +static inline struct Mat3 mat3_mul(struct Mat3 a, struct Mat3 b) { + struct Vec3 r1 = { + a.r1.x * b.r1.x + a.r1.y * b.r2.x + a.r1.z * b.r3.x, + a.r1.x * b.r1.y + a.r1.y * b.r2.y + a.r1.z * b.r3.y, + a.r1.x * b.r1.z + a.r1.y * b.r2.z + a.r1.z * b.r3.z, + }; + + struct Vec3 r2 = { + a.r2.x * b.r1.x + a.r2.y * b.r2.x + a.r2.z * b.r3.x, + a.r2.x * b.r1.y + a.r2.y * b.r2.y + a.r2.z * b.r3.y, + a.r2.x * b.r1.z + a.r2.y * b.r2.z + a.r2.z * b.r3.z, + }; + + struct Vec3 r3 = { + a.r3.x * b.r1.x + a.r3.y * b.r2.x + a.r3.z * b.r3.x, + a.r3.x * b.r1.y + a.r3.y * b.r2.y + a.r3.z * b.r3.y, + a.r3.x * b.r1.z + a.r3.y * b.r2.z + a.r3.z * b.r3.z, + }; + + return (struct Mat3){r1, r2, r3}; +} + +static inline double mat3_det(struct Mat3 a) { + return a.r1.x * (a.r2.y * a.r3.z - a.r2.z * a.r3.y) - + a.r1.y * (a.r2.x * a.r3.z - a.r2.z * a.r3.x) + + a.r1.z * (a.r2.x * a.r3.y - a.r2.y * a.r3.x); +} + +struct Mat3 mat3_inv(struct Mat3 a); + #endif diff --git a/mathprimitives/test.c b/mathprimitives/test.c index 38c7ca1..a7a42cf 100644 --- a/mathprimitives/test.c +++ b/mathprimitives/test.c @@ -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");