From bc7f3817f8134cb772918bb7f77d872938ea631b Mon Sep 17 00:00:00 2001 From: Hossein Moein Date: Fri, 29 Nov 2024 10:28:44 -0500 Subject: [PATCH] Fixed a bug in Matrix class --- include/DataFrame/Utils/Matrix.h | 4 ++-- test/dataframe_tester_4.cc | 8 ++++---- test/matrix_tester.cc | 25 ++++++++++++++++++++++--- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/include/DataFrame/Utils/Matrix.h b/include/DataFrame/Utils/Matrix.h index c5341d95..83c4fa9f 100644 --- a/include/DataFrame/Utils/Matrix.h +++ b/include/DataFrame/Utils/Matrix.h @@ -1113,7 +1113,7 @@ operator * (const Matrix &lhs, const Matrix &rhs) { assert(lhs_cols == rhs.rows()); #endif // HMDF_SANITY_EXCEPTIONS - Matrix result { lhs_rows, rhs_cols }; + Matrix result { lhs_rows, rhs_cols, 0 }; const long thread_level = (lhs_cols >= 400L || rhs_cols >= 400L) ? ThreadGranularity::get_thread_level() : 0; @@ -1125,7 +1125,7 @@ operator * (const Matrix &lhs, const Matrix &rhs) { for (long c = begin; c < end; ++c) for (long r = 0; r < lhs_rows; ++r) for (long k = 0; k < lhs_cols; ++k) - result(r, c) += lhs(k, r) * rhs(c, k); + result(r, c) += lhs(r, k) * rhs(k, c); }; auto row_lbd = [lhs_cols, rhs_cols, &result, diff --git a/test/dataframe_tester_4.cc b/test/dataframe_tester_4.cc index 14ba4ca3..6c134caf 100644 --- a/test/dataframe_tester_4.cc +++ b/test/dataframe_tester_4.cc @@ -1979,10 +1979,10 @@ static void test_PartialAutoCorrVisitor() { assert(pacf.get_result().size() == 50); assert(std::fabs(pacf.get_result()[0] - 1.0) < 0.000001); assert(std::fabs(pacf.get_result()[1] - 0.999915) < 0.000001); - assert(std::fabs(pacf.get_result()[10] - 0.094446) < 0.000001); - assert(std::fabs(pacf.get_result()[30] - 0.004907) < 0.000001); - assert(std::fabs(pacf.get_result()[48] - 0.004338) < 0.000001); - assert(std::fabs(pacf.get_result()[49] - 0.045952) < 0.000001); + assert(std::fabs(pacf.get_result()[10] - 0.982959) < 0.000001); + assert(std::fabs(pacf.get_result()[30] - 0.983226) < 0.000001); + assert(std::fabs(pacf.get_result()[48] - 0.98751) < 0.000001); + assert(std::fabs(pacf.get_result()[49] - 0.987886) < 0.000001); } // ---------------------------------------------------------------------------- diff --git a/test/matrix_tester.cc b/test/matrix_tester.cc index e56fae7b..b7190d74 100644 --- a/test/matrix_tester.cc +++ b/test/matrix_tester.cc @@ -46,10 +46,29 @@ int main(int, char *[]) { // ThreadGranularity::set_optimum_thread_level(); + row_mat_t row_mata { 3, 3 }; + col_mat_t col_mata { 3, 3 }; + std::size_t value { 0 }; + + for (long r = 0; r < row_mata.rows(); ++r) + for (long c = 0; c < row_mata.cols(); ++c) { + row_mata(r, c) = ++value; + col_mata(r, c) = value; + } + + row_mat_t row_matb = row_mata * row_mata; + col_mat_t col_matb = col_mata * col_mata; + + assert((row_matb(0, 0) == col_matb(0, 0) && row_matb(0, 0) == 30)); + assert((row_matb(0, 2) == col_matb(0, 2) && row_matb(0, 2) == 42)); + assert((row_matb(1, 1) == col_matb(1, 1) && row_matb(1, 1) == 81)); + assert((row_matb(2, 1) == col_matb(2, 1) && row_matb(2, 1) == 126)); + assert((row_matb(2, 2) == col_matb(2, 2) && row_matb(2, 2) == 150)); + row_mat_t row_mat { ROWS, COLS }; col_mat_t col_mat { ROWS, COLS }; - std::size_t value { 0 }; + value = 0; for (long r = 0; r < row_mat.rows(); ++r) for (long c = 0; c < row_mat.cols(); ++c) row_mat(r, c) = value++; @@ -184,8 +203,8 @@ int main(int, char *[]) { assert(big_multi_mat(0, 0) == 5050); assert(big_multi_mat(99, 99) == 505000); - assert(big_multi_mat(98, 2) == 499950); - assert(big_multi_mat(2, 5) == 15150); + assert(big_multi_mat(98, 2) == 15150); + assert(big_multi_mat(2, 5) == 30300); // // Test inverse