From 8aeb4667d7fdeacbbd767c21bdeb3a767a8261c3 Mon Sep 17 00:00:00 2001 From: Orange Date: Wed, 17 Sep 2025 17:30:57 +0300 Subject: [PATCH 1/3] decomposed mutiplication --- include/omath/linear_algebra/mat.hpp | 52 ++++++++++++++++++---------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/include/omath/linear_algebra/mat.hpp b/include/omath/linear_algebra/mat.hpp index 4f5a7ed0..2d255843 100644 --- a/include/omath/linear_algebra/mat.hpp +++ b/include/omath/linear_algebra/mat.hpp @@ -158,25 +158,10 @@ namespace omath Mat result; if constexpr (StoreType == MatStoreType::ROW_MAJOR) - for (std::size_t i = 0; i < Rows; ++i) - for (std::size_t k = 0; k < Columns; ++k) - { - const Type aik = at(i, k); - for (std::size_t j = 0; j < OtherColumns; ++j) - result.at(i, j) += aik * other.at(k, j); - } - else if constexpr (StoreType == MatStoreType::COLUMN_MAJOR) - for (std::size_t j = 0; j < OtherColumns; ++j) - for (std::size_t k = 0; k < Columns; ++k) - { - const Type bkj = other.at(k, j); - for (std::size_t i = 0; i < Rows; ++i) - result.at(i, j) += at(i, k) * bkj; - } - else - std::unreachable(); - - return result; + return cache_friendly_multiply_row_major(other); + if constexpr (StoreType == MatStoreType::COLUMN_MAJOR) + return cache_friendly_multiply_col_major(other); + std::unreachable(); } constexpr Mat& operator*=(const Type& f) noexcept @@ -378,6 +363,35 @@ namespace omath private: std::array m_data; + + template [[nodiscard]] + constexpr Mat + cache_friendly_multiply_row_major(const Mat& other) const + { + Mat result; + for (std::size_t i = 0; i < Rows; ++i) + for (std::size_t k = 0; k < Columns; ++k) + { + const Type aik = at(i, k); + for (std::size_t j = 0; j < OtherColumns; ++j) + result.at(i, j) += aik * other.at(k, j); + } + return result; + } + template [[nodiscard]] + constexpr Mat cache_friendly_multiply_col_major( + const Mat& other) const + { + Mat result; + for (std::size_t j = 0; j < OtherColumns; ++j) + for (std::size_t k = 0; k < Columns; ++k) + { + const Type bkj = other.at(k, j); + for (std::size_t i = 0; i < Rows; ++i) + result.at(i, j) += at(i, k) * bkj; + } + return result; + } }; template [[nodiscard]] From 68ec42d9ed955f1a1a30bb8f59c135a0e145ebd2 Mon Sep 17 00:00:00 2001 From: Orange Date: Wed, 17 Sep 2025 17:33:05 +0300 Subject: [PATCH 2/3] added space --- include/omath/linear_algebra/mat.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/include/omath/linear_algebra/mat.hpp b/include/omath/linear_algebra/mat.hpp index 2d255843..096345cf 100644 --- a/include/omath/linear_algebra/mat.hpp +++ b/include/omath/linear_algebra/mat.hpp @@ -378,6 +378,7 @@ namespace omath } return result; } + template [[nodiscard]] constexpr Mat cache_friendly_multiply_col_major( const Mat& other) const From 874b028e8608a5cb28b2661282d5880b4f78e56b Mon Sep 17 00:00:00 2001 From: Orange Date: Wed, 17 Sep 2025 17:38:17 +0300 Subject: [PATCH 3/3] removed unused var --- include/omath/linear_algebra/mat.hpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/include/omath/linear_algebra/mat.hpp b/include/omath/linear_algebra/mat.hpp index 096345cf..99f87757 100644 --- a/include/omath/linear_algebra/mat.hpp +++ b/include/omath/linear_algebra/mat.hpp @@ -155,8 +155,6 @@ namespace omath constexpr Mat operator*(const Mat& other) const { - Mat result; - if constexpr (StoreType == MatStoreType::ROW_MAJOR) return cache_friendly_multiply_row_major(other); if constexpr (StoreType == MatStoreType::COLUMN_MAJOR)