From 4ef674f7b46dcbc5bfc8660ad2ad2bb82d91c7d1 Mon Sep 17 00:00:00 2001 From: Orange Date: Tue, 29 Apr 2025 20:08:27 +0300 Subject: [PATCH 1/5] fixed infinite recursion in compile time --- include/omath/mat.hpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/include/omath/mat.hpp b/include/omath/mat.hpp index 47773c6..07ae913 100644 --- a/include/omath/mat.hpp +++ b/include/omath/mat.hpp @@ -253,7 +253,8 @@ namespace omath if constexpr (Rows == 2) return At(0, 0) * At(1, 1) - At(0, 1) * At(1, 0); - else + + if constexpr (Rows > 2) { Type det = 0; for (size_t i = 0; i < Columns; ++i) @@ -263,11 +264,13 @@ namespace omath } return det; } + std::unreachable(); } [[nodiscard]] constexpr Mat Minor(const size_t row, const size_t column) const { + static_assert(Rows-1 > 0 && Columns-1 > 0); Mat result; for (size_t i = 0, m = 0; i < Rows; ++i) { From 46157696822bcce37b4c802ac7d2bb87b637315f Mon Sep 17 00:00:00 2001 From: Orange Date: Tue, 29 Apr 2025 20:10:17 +0300 Subject: [PATCH 2/5] added additional methods --- include/omath/mat.hpp | 19 ++++++++++++++++--- tests/general/unit_test_mat.cpp | 4 ++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/include/omath/mat.hpp b/include/omath/mat.hpp index 07ae913..9bb653f 100644 --- a/include/omath/mat.hpp +++ b/include/omath/mat.hpp @@ -257,9 +257,9 @@ namespace omath if constexpr (Rows > 2) { Type det = 0; - for (size_t i = 0; i < Columns; ++i) + for (size_t column = 0; column < Columns; ++column) { - const Type cofactor = (i % 2 == 0 ? 1 : -1) * At(0, i) * Minor(0, i).Determinant(); + const Type cofactor = At(0, column) * AlgComplement(0, column); det += cofactor; } return det; @@ -268,7 +268,7 @@ namespace omath } [[nodiscard]] - constexpr Mat Minor(const size_t row, const size_t column) const + constexpr Mat Strip(const size_t row, const size_t column) const { static_assert(Rows-1 > 0 && Columns-1 > 0); Mat result; @@ -288,6 +288,19 @@ namespace omath return result; } + [[nodiscard]] + constexpr Type Minor(const size_t row, const size_t column) const + { + return Strip(row, column).Determinant(); + } + + [[nodiscard]] + constexpr Type AlgComplement(const size_t row, const size_t column) const + { + const auto minor = Minor(row, column); + return (row + column + 2) % 2 == 0 ? minor: -minor; + } + [[nodiscard]] constexpr const std::array& RawArray() const { diff --git a/tests/general/unit_test_mat.cpp b/tests/general/unit_test_mat.cpp index 43b25c1..ec60eb3 100644 --- a/tests/general/unit_test_mat.cpp +++ b/tests/general/unit_test_mat.cpp @@ -180,10 +180,10 @@ TEST(UnitTestMatStandalone, Determinant_3x3) } // Test Minor for 3x3 matrix -TEST(UnitTestMatStandalone, Minor_3x3) +TEST(UnitTestMatStandalone, Strip_3x3) { constexpr Mat<3, 3> m{{3, 0, 2}, {2, 0, -2}, {0, 1, 1}}; - auto minor = m.Minor(0, 0); + auto minor = m.Strip(0, 0); EXPECT_EQ(minor.RowCount(), 2); EXPECT_EQ(minor.ColumnsCount(), 2); EXPECT_FLOAT_EQ(minor.At(0, 0), 0.0f); From 1c5c9360c80a5cbb318af45e6bcafd44da4f5823 Mon Sep 17 00:00:00 2001 From: Orange Date: Tue, 29 Apr 2025 20:33:39 +0300 Subject: [PATCH 3/5] added inverse method --- include/omath/mat.hpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/include/omath/mat.hpp b/include/omath/mat.hpp index 9bb653f..93d3771 100644 --- a/include/omath/mat.hpp +++ b/include/omath/mat.hpp @@ -359,6 +359,25 @@ namespace omath }; } + [[nodiscard]] + constexpr std::optional Inverted() const + { + const auto det = Determinant(); + + if (det == 0) + return std::nullopt; + + const auto transposed = Transposed(); + Mat result; + + for (std::size_t row = 0; row < Rows; row++) + for (std::size_t column = 0; column < Rows; column++) + result.At(row, column) = transposed.AlgComplement(row, column); + + result /= det; + + return {result}; + } private: std::array m_data; }; From a0d1dc4313a4df9618836b195ce6426c97501650 Mon Sep 17 00:00:00 2001 From: Orange Date: Tue, 29 Apr 2025 20:49:59 +0300 Subject: [PATCH 4/5] added test case --- include/omath/mat.hpp | 2 +- tests/general/unit_test_mat.cpp | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/omath/mat.hpp b/include/omath/mat.hpp index 93d3771..c418702 100644 --- a/include/omath/mat.hpp +++ b/include/omath/mat.hpp @@ -375,7 +375,7 @@ namespace omath result.At(row, column) = transposed.AlgComplement(row, column); result /= det; - + return {result}; } private: diff --git a/tests/general/unit_test_mat.cpp b/tests/general/unit_test_mat.cpp index ec60eb3..abf6f1e 100644 --- a/tests/general/unit_test_mat.cpp +++ b/tests/general/unit_test_mat.cpp @@ -206,3 +206,12 @@ TEST(UnitTestMatStandalone, Transpose_NonSquare) EXPECT_FLOAT_EQ(transposed.At(1, 1), 5.0f); EXPECT_FLOAT_EQ(transposed.At(2, 1), 6.0f); } + +TEST(UnitTestMatStandalone, Enverse) +{ + + constexpr Mat<2, 2> m{{1.0f, 3.0f}, {2.0f, 5.0f}}; + constexpr Mat<2,2> mv{{-5.0f, 3.0f}, {2.0f, -1.0f}}; + + EXPECT_EQ(mv, m.Inverted()); +} From a41526c494580bf2b3aeadf8bf221577b788b7dd Mon Sep 17 00:00:00 2001 From: Orange Date: Tue, 29 Apr 2025 20:52:41 +0300 Subject: [PATCH 5/5] style fix --- tests/general/unit_test_mat.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/general/unit_test_mat.cpp b/tests/general/unit_test_mat.cpp index abf6f1e..7fc25f6 100644 --- a/tests/general/unit_test_mat.cpp +++ b/tests/general/unit_test_mat.cpp @@ -209,7 +209,6 @@ TEST(UnitTestMatStandalone, Transpose_NonSquare) TEST(UnitTestMatStandalone, Enverse) { - constexpr Mat<2, 2> m{{1.0f, 3.0f}, {2.0f, 5.0f}}; constexpr Mat<2,2> mv{{-5.0f, 3.0f}, {2.0f, -1.0f}};