From cf4079e5727529d05145cdeb01217418da13337b Mon Sep 17 00:00:00 2001 From: Chad Cotton Date: Thu, 8 May 2025 10:38:16 -0500 Subject: [PATCH] Remove matrix dot product in place of element wise multiplication. --- include/LinearLib/Matrix.hpp | 15 +++++---------- tests/MatrixTests.cpp | 27 --------------------------- 2 files changed, 5 insertions(+), 37 deletions(-) diff --git a/include/LinearLib/Matrix.hpp b/include/LinearLib/Matrix.hpp index 0e43e64..67f9a83 100644 --- a/include/LinearLib/Matrix.hpp +++ b/include/LinearLib/Matrix.hpp @@ -288,20 +288,15 @@ namespace LinearLib { } /** - * Matrix dot product + * Element-wise multiplication */ - template - Matrix operator*(const Matrix& other) const { + Matrix operator*(const Matrix& other) const { - Matrix res; + Matrix res; for (std::size_t i = 0; i < R; i++) { - for (std::size_t j = 0; j < I; j++) { - T sum = T{}; - for (std::size_t k = 0; k < C; k++) { - sum += data[i][k] * other[k][j]; - } - res[i][j] = sum; + for (std::size_t j = 0; j < C; j++) { + res.data[i][j] = data[i][j] * other[i][j]; } } diff --git a/tests/MatrixTests.cpp b/tests/MatrixTests.cpp index 27316d4..bfd08d0 100644 --- a/tests/MatrixTests.cpp +++ b/tests/MatrixTests.cpp @@ -35,33 +35,6 @@ TEST_CASE("Matrix operations", "[vector]") { REQUIRE(result[1][1] == 2); } - SECTION("Matrix dot product") { - const Matrix<2, 2, int> m1 {{1, 2},{3,4}}; - const Matrix<2, 2, int> m2 {{5, 6},{7,8}}; - - const Matrix<2, 2, int> result1 = m1 * m2; - - REQUIRE(result1[0][0] == 19); - REQUIRE(result1[0][1] == 22); - REQUIRE(result1[1][0] == 43); - REQUIRE(result1[1][1] == 50); - - const Matrix<3, 3, int> m3 {{1, 1, 3}, - {-1, 4, 1}, - {0, 2, -2}}; - const Matrix<3, 1, int> m4 {{1}, - {0}, - {-1}}; - - const Matrix<3, 1, int> result2 = m3 * m4; - - REQUIRE(result2[0][0] == -2); - REQUIRE(result2[1][0] == -2); - REQUIRE(result2[2][0] == 2); - - - } - SECTION("Matrix multiplication") { const Matrix<2, 4, int> m1 {{5, 3, 3, 4}, {2, 4, 4, 3}};