From ec633807462fb96f7ce4f96cfe494a10789a8f09 Mon Sep 17 00:00:00 2001 From: Karen Date: Fri, 16 May 2025 16:46:38 +0200 Subject: [PATCH 01/11] [TEST][ARITHMETICS] added tests for the operator+ --- test/basic/operators.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/basic/operators.cpp b/test/basic/operators.cpp index 0734aef..4a81d07 100644 --- a/test/basic/operators.cpp +++ b/test/basic/operators.cpp @@ -28,4 +28,27 @@ TTS_CASE("Check operator*") a *= 10.5; TTS_EQUAL(a, ref); +}; + +TTS_CASE("Check operator matrix + matrix") +{ + rotgen::matrix mat1(3,4); + rotgen::matrix mat2(3,4); + rotgen::matrix mat_sum(3,4); + + for(std::size_t r=0;r Date: Mon, 19 May 2025 10:24:49 +0200 Subject: [PATCH 02/11] [ARITHMETIC OPERATORS][IMPLEMENTATION] implmeented the binary, unary and compound - operator --- include/rotgen/impl/matrix_impl64.hpp | 2 ++ include/rotgen/matrix.hpp | 18 ++++++++++++++++++ src/matrix_impl64.cpp | 13 +++++++++++++ 3 files changed, 33 insertions(+) diff --git a/include/rotgen/impl/matrix_impl64.hpp b/include/rotgen/impl/matrix_impl64.hpp index e3345a7..ff9d349 100644 --- a/include/rotgen/impl/matrix_impl64.hpp +++ b/include/rotgen/impl/matrix_impl64.hpp @@ -43,6 +43,8 @@ namespace rotgen double const& operator()(std::size_t index) const; matrix_impl64& operator+=(matrix_impl64 const& rhs); + matrix_impl64& operator-=(matrix_impl64 const& rhs); + matrix_impl64& operator-(); matrix_impl64& operator*=(matrix_impl64 const& rhs); matrix_impl64& operator*=(double d); diff --git a/include/rotgen/matrix.hpp b/include/rotgen/matrix.hpp index a481c23..b7ef164 100644 --- a/include/rotgen/matrix.hpp +++ b/include/rotgen/matrix.hpp @@ -69,6 +69,17 @@ namespace rotgen return *this; } + matrix& operator-=(matrix const& rhs) + { + static_cast(*this) -= static_cast(rhs); + return *this; + } + + matrix& operator-() + { + return static_cast(*this).operator-(); + } + matrix& operator*=(matrix const& rhs) { static_cast(*this) *= static_cast(rhs); @@ -89,6 +100,13 @@ namespace rotgen return that += rhs; } + template + matrix operator-(matrix const& lhs, matrix const& rhs) + { + matrix that(lhs); + return that -= rhs; + } + template matrix operator*(matrix const& lhs, matrix const& rhs) { diff --git a/src/matrix_impl64.cpp b/src/matrix_impl64.cpp index e1fec52..515659a 100644 --- a/src/matrix_impl64.cpp +++ b/src/matrix_impl64.cpp @@ -95,6 +95,19 @@ namespace rotgen return *this; } + matrix_impl64& matrix_impl64::operator-=(matrix_impl64 const& rhs) + { + storage_->data -= rhs.storage_->data; + return *this; + } + + matrix_impl64& matrix_impl64::operator-() + { + matrix_impl64 result(*this); + result.storage_->data = -result.storage_->data; + return result; + } + matrix_impl64& matrix_impl64::operator*=(matrix_impl64 const& rhs) { storage_->data *= rhs.storage_->data; From ee3197a0a67a47268cb808c3711c3d0dd912ebbe Mon Sep 17 00:00:00 2001 From: kallore Date: Mon, 19 May 2025 10:28:10 +0200 Subject: [PATCH 03/11] [ARITHMETIC OPERATORS][IMPLEMENTATION] implmeented the binary and compound / operator --- include/rotgen/impl/matrix_impl64.hpp | 1 + include/rotgen/matrix.hpp | 13 +++++++++++++ src/matrix_impl64.cpp | 6 ++++++ 3 files changed, 20 insertions(+) diff --git a/include/rotgen/impl/matrix_impl64.hpp b/include/rotgen/impl/matrix_impl64.hpp index ff9d349..e814c7c 100644 --- a/include/rotgen/impl/matrix_impl64.hpp +++ b/include/rotgen/impl/matrix_impl64.hpp @@ -47,6 +47,7 @@ namespace rotgen matrix_impl64& operator-(); matrix_impl64& operator*=(matrix_impl64 const& rhs); matrix_impl64& operator*=(double d); + matrix_impl64& operator/=(double d); friend std::ostream& operator<<(std::ostream&,matrix_impl64 const&); friend bool operator==(matrix_impl64 const& lhs, matrix_impl64 const& rhs); diff --git a/include/rotgen/matrix.hpp b/include/rotgen/matrix.hpp index b7ef164..6018b13 100644 --- a/include/rotgen/matrix.hpp +++ b/include/rotgen/matrix.hpp @@ -91,6 +91,12 @@ namespace rotgen static_cast(*this) *= rhs; return *this; } + + matrix& operator/=(double rhs) + { + static_cast(*this) /= rhs; + return *this; + } }; template @@ -126,4 +132,11 @@ namespace rotgen { return rhs * lhs; } + + template + matrix operator/(matrix const& lhs, double rhs) + { + matrix that(lhs); + return that /= rhs; + } } \ No newline at end of file diff --git a/src/matrix_impl64.cpp b/src/matrix_impl64.cpp index 515659a..a5a69ad 100644 --- a/src/matrix_impl64.cpp +++ b/src/matrix_impl64.cpp @@ -119,4 +119,10 @@ namespace rotgen storage_->data *= s; return *this; } + + matrix_impl64& matrix_impl64::operator/=(double s) + { + storage_->data /= s; + return *this; + } } \ No newline at end of file From 721550d0f8d26421a8c0e39daec3215ce5411670 Mon Sep 17 00:00:00 2001 From: kallore Date: Mon, 19 May 2025 16:59:10 +0200 Subject: [PATCH 04/11] [ARITHMETIC OP][IMPLEMENTATION] rectifie dthe implementation of the unary - operator --- include/rotgen/impl/matrix_impl64.hpp | 2 +- include/rotgen/matrix.hpp | 6 ++++-- src/matrix_impl64.cpp | 3 +-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/include/rotgen/impl/matrix_impl64.hpp b/include/rotgen/impl/matrix_impl64.hpp index e814c7c..07a3fce 100644 --- a/include/rotgen/impl/matrix_impl64.hpp +++ b/include/rotgen/impl/matrix_impl64.hpp @@ -44,7 +44,7 @@ namespace rotgen matrix_impl64& operator+=(matrix_impl64 const& rhs); matrix_impl64& operator-=(matrix_impl64 const& rhs); - matrix_impl64& operator-(); + matrix_impl64 operator-() const; matrix_impl64& operator*=(matrix_impl64 const& rhs); matrix_impl64& operator*=(double d); matrix_impl64& operator/=(double d); diff --git a/include/rotgen/matrix.hpp b/include/rotgen/matrix.hpp index 6018b13..9c78d00 100644 --- a/include/rotgen/matrix.hpp +++ b/include/rotgen/matrix.hpp @@ -29,6 +29,8 @@ namespace rotgen if constexpr(Cols != -1) assert(c == Cols && "Mismatched between dynamic and static column size"); } + matrix(parent const& base) : parent(base) {} + matrix(std::initializer_list> init) : parent(init) { if constexpr(Rows != -1) assert(init.size() == Rows && "Mismatched between dynamic and static row size"); @@ -75,9 +77,9 @@ namespace rotgen return *this; } - matrix& operator-() + matrix operator-() const { - return static_cast(*this).operator-(); + return matrix(static_cast(*this).operator-()); } matrix& operator*=(matrix const& rhs) diff --git a/src/matrix_impl64.cpp b/src/matrix_impl64.cpp index a5a69ad..658b22c 100644 --- a/src/matrix_impl64.cpp +++ b/src/matrix_impl64.cpp @@ -101,8 +101,7 @@ namespace rotgen return *this; } - matrix_impl64& matrix_impl64::operator-() - { + matrix_impl64 matrix_impl64::operator-() const { matrix_impl64 result(*this); result.storage_->data = -result.storage_->data; return result; From 8759c7381ae5fb4e711ecaea7c41f19e4980a107 Mon Sep 17 00:00:00 2001 From: kallore Date: Mon, 19 May 2025 17:00:58 +0200 Subject: [PATCH 05/11] [ARITHMETIC OP][TEST] added tests for all arithmetic operators --- test/basic/operators.cpp | 269 +++++++++++++++++++++++++++++++++++---- 1 file changed, 241 insertions(+), 28 deletions(-) diff --git a/test/basic/operators.cpp b/test/basic/operators.cpp index 4a81d07..b3fb8f6 100644 --- a/test/basic/operators.cpp +++ b/test/basic/operators.cpp @@ -9,46 +9,259 @@ #include #include "tts.hpp" -TTS_CASE("Check operator*") +template +void test_matrix_scalar_multiplication(std::size_t rows, std::size_t cols, double scalar, + const std::function& init_fn) { - rotgen::matrix a(2,2); - rotgen::matrix ref(2,2); + MatrixType a(rows, cols); + MatrixType ref(rows, cols); - for(std::size_t r=0;r +void test_matrix_scalar_division(std::size_t rows, std::size_t cols, double scalar, + const std::function& init_fn) +{ + MatrixType a(rows, cols); + MatrixType ref(rows, cols); + + for (std::size_t r = 0; r < rows; ++r) + { + for (std::size_t c = 0; c < cols; ++c) + { + init_fn(a, r, c); + ref(r, c) = a(r, c) / scalar; + } + } + + TTS_EQUAL(a / scalar, ref); + a /= scalar; + TTS_EQUAL(a, ref); +} + +template +void test_matrix_multiplication(std::size_t n, std::size_t m, std::size_t p, InitA&& a_init_fn, InitB&& b_init_fn) +{ + MatrixType a(n, m); + MatrixType b(m, p); + MatrixType ref(n, p); + + for (std::size_t r = 0; r < n; ++r) + for (std::size_t c = 0; c < m; ++c) + a_init_fn(a, r, c); + + for (std::size_t r = 0; r < m; ++r) + for (std::size_t c = 0; c < p; ++c) + b_init_fn(b, r, c); + + for (std::size_t i = 0; i < n; ++i) + for (std::size_t j = 0; j < p; ++j) + { + ref(i, j) = 0; + for (std::size_t k = 0; k < m; ++k) + ref(i, j) += a(i, k) * b(k, j); + } + + TTS_EQUAL(a * b, ref); + a *= b; + TTS_EQUAL(a, ref); +} + +template +void test_matrix_addition(std::size_t rows, std::size_t cols, InitA&& a_init_fn, InitB&& b_init_fn) +{ + MatrixType a(rows, cols); + MatrixType b(rows, cols); + MatrixType ref(rows, cols); + + for (std::size_t r = 0; r < rows; ++r) + { + for (std::size_t c = 0; c < cols; ++c) + { + a_init_fn(a, r, c); + b_init_fn(b, r, c); + ref(r, c) = a(r,c) + b(r,c); + } + } + + TTS_EQUAL(a + b, ref); + TTS_EQUAL(b + a, ref); + a += b; + TTS_EQUAL(a, ref); +} + +template +void test_matrix_substraction(std::size_t rows, std::size_t cols, InitA&& a_init_fn, InitB&& b_init_fn) +{ + MatrixType a(rows, cols); + MatrixType b(rows, cols); + MatrixType ref(rows, cols); + MatrixType a_minus_ref(rows, cols); + MatrixType b_minus_ref(rows, cols); + + for (std::size_t r = 0; r < rows; ++r) + { + for (std::size_t c = 0; c < cols; ++c) + { + a_init_fn(a, r, c); + b_init_fn(b, r, c); + ref(r, c) = a(r,c) - b(r,c); + a_minus_ref(r, c) = -a(r,c); + b_minus_ref(r, c) = -b(r,c); + } + } + + MatrixType a_unary = -a; + MatrixType b_unary = -b; + + TTS_EQUAL(a - b, ref); + + TTS_EQUAL(a_unary, a_minus_ref); + TTS_EQUAL(-a, a_minus_ref); + TTS_EQUAL(-b, b_minus_ref); + TTS_EQUAL(-(-a), a); + TTS_EQUAL(-(-b), b); + + a -= b; TTS_EQUAL(a, ref); }; -TTS_CASE("Check operator matrix + matrix") +TTS_CASE("Check matrix * scalar and scalar * matrix with default values") { - rotgen::matrix mat1(3,4); - rotgen::matrix mat2(3,4); - rotgen::matrix mat_sum(3,4); + test_matrix_scalar_multiplication>(2, 2, 10.5, + [](auto& a, std::size_t r, std::size_t c) + { a(r, c) = (1 + c) + 10 * (1 + r); }); +}; - for(std::size_t r=0;r>(3, 2, 0.0, + [](auto& a, std::size_t r, std::size_t c) + { a(r, c) = 5 * c - r; }); +}; - TTS_EQUAL(mat1 + mat2, mat_sum); - TTS_EQUAL(mat2 + mat1, mat_sum); +TTS_CASE("Check matrix * scalar with one scalar multiplication") +{ + test_matrix_scalar_multiplication>(3, 2, 1, + [](auto& a, std::size_t r, std::size_t c) + { a(r, c) = 3.3*r - 6; }); +}; - mat1 += mat2; - TTS_EQUAL(mat1, mat_sum); -}; \ No newline at end of file +TTS_CASE("Check matrix - scalar with negative scalar multiplication") +{ + test_matrix_scalar_multiplication>(3, 2, -36.2, + [](auto& a, std::size_t r, std::size_t c) + { a(r, c) = r * r - c + 3.9; }); +}; + +TTS_CASE("Check static matrix - scalar with float scalar multiplication") +{ + test_matrix_scalar_multiplication>(3, 4, 5.6, + [](auto& a, std::size_t r, std::size_t c) + { a(r, c) = 1.2*r+4.5*c-6; }); +}; + +TTS_CASE("Check matrix / scalar with default values") +{ + test_matrix_scalar_division>(6, 7, 10.5, + [](auto& a, std::size_t r, std::size_t c) + { a(r, c) = (1 + c) + 10 * (1 + r); }); +}; + +TTS_CASE("Check matrix * scalar with one scalar multiplication") +{ + test_matrix_scalar_division>(3, 2, 1, + [](auto& a, std::size_t r, std::size_t c) + { a(r, c) = 3.3*r - 4.5*c + 1.1; }); +}; + +TTS_CASE("Check matrix - scalar with negative scalar multiplication") +{ + test_matrix_scalar_division>(2, 7, -36.2, + [](auto& a, std::size_t r, std::size_t c) + { a(r, c) = 3.4 * r * r - c + 3.9; }); +}; + +TTS_CASE("Check static matrix - scalar with float scalar multiplication") +{ + test_matrix_scalar_division>(3, 4, 5.6, + [](auto& a, std::size_t r, std::size_t c) + { a(r, c) = 1.2*r+4.5*c-6; }); +}; + +TTS_CASE("Matrix multiplication") +{ + test_matrix_multiplication>(2, 3, 4, + [](auto& mat, std::size_t r, std::size_t c) { mat(r, c) = r + c; }, + [](auto& mat, std::size_t r, std::size_t c) { mat(r, c) = r * c; }); +}; + +TTS_CASE("Matrix multiplication with floats") +{ + test_matrix_multiplication>(3, 6, 3, + [](auto& mat, std::size_t r, std::size_t c) { mat(r, c) = 3.4 * r + 5.4 * c*c - 5.2; }, + [](auto& mat, std::size_t r, std::size_t c) { mat(r, c) = r * c + 5 * r - 3.33; }); +}; + +TTS_CASE("Matrix multiplication with zero matrix") +{ + test_matrix_multiplication>(3, 4, 5, + [](auto& mat, std::size_t r, std::size_t c) { mat(r, c) = (-4) * r + 3*c; }, + [](auto& mat, std::size_t r, std::size_t c) { mat(r, c) = 0; }); +}; + +TTS_CASE("Matrix multiplication with itself") +{ + test_matrix_multiplication>(3, 3, 3, + [](auto& mat, std::size_t r, std::size_t c) { mat(r, c) = 3*r - 7*r*c + 14.4; }, + [](auto& mat, std::size_t r, std::size_t c) { mat(r, c) = 3*r - 7*r*c + 14.4; }); +}; + +TTS_CASE("Matrix multiplication with identity matrix") +{ + test_matrix_multiplication>(4, 4, 4, + [](auto& mat, std::size_t r, std::size_t c) { mat(r, c) = r*r*r - c + 12; }, + [](auto& mat, std::size_t r, std::size_t c) { mat(r, c) = (r==c); }); +}; + +TTS_CASE("Matrix addition") +{ + test_matrix_addition>(3, 4, + [](auto& mat, std::size_t r, std::size_t c) { mat(r, c) = 9.9*r*r*r - 6*c -12; }, + [](auto& mat, std::size_t r, std::size_t c) { mat(r, c) = 3.1*r + 4.2*c - 12.3; }); +}; + +TTS_CASE("Matrix addition with zero matrix") +{ + test_matrix_addition>(3, 4, + [](auto& mat, std::size_t r, std::size_t c) { mat(r, c) = 2*r*r + c + 3.4; }, + [](auto& mat, std::size_t r, std::size_t c) { mat(r, c) = 0; }); +}; + +TTS_CASE("Matrix substraction") +{ + test_matrix_substraction>(3, 4, + [](auto& mat, std::size_t r, std::size_t c) { mat(r, c) = 6.78*r - 5.2*c - 0.01; }, + [](auto& mat, std::size_t r, std::size_t c) { mat(r, c) = 3.1*r + 33.456*c*c; }); +}; + +TTS_CASE("Matrix substraction with zero matrix") +{ + test_matrix_substraction>(3, 4, + [](auto& mat, std::size_t r, std::size_t c) { mat(r, c) = r + c*c*c*c - 56.6; }, + [](auto& mat, std::size_t r, std::size_t c) { mat(r, c) = 0; }); +}; From 8aea305ad3544004a6043e5e6c506777afa38b13 Mon Sep 17 00:00:00 2001 From: kallore Date: Tue, 20 May 2025 10:23:57 +0200 Subject: [PATCH 06/11] [ARITHMETIC][FUNCTIONS] implemented and tested the unary operations transpose, adjoint, conjugate and transposeInPlace and adjointInPlace --- include/rotgen/impl/matrix_impl64.hpp | 7 +++ include/rotgen/matrix.hpp | 19 ++++++++ src/matrix_impl64.cpp | 30 +++++++++++++ test/basic/arithmetic_functions.cpp | 63 +++++++++++++++++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 test/basic/arithmetic_functions.cpp diff --git a/include/rotgen/impl/matrix_impl64.hpp b/include/rotgen/impl/matrix_impl64.hpp index 07a3fce..0764624 100644 --- a/include/rotgen/impl/matrix_impl64.hpp +++ b/include/rotgen/impl/matrix_impl64.hpp @@ -36,6 +36,13 @@ namespace rotgen void resize(std::size_t new_rows, std::size_t new_cols); void conservativeResize(std::size_t new_rows, std::size_t new_cols); + matrix_impl64 transpose() const; + matrix_impl64 conjugate() const; + matrix_impl64 adjoint() const; + + void transposeInPlace(); + void adjointInPlace(); + double& operator()(std::size_t i, std::size_t j); double const& operator()(std::size_t i, std::size_t j) const; diff --git a/include/rotgen/matrix.hpp b/include/rotgen/matrix.hpp index 9c78d00..febe15b 100644 --- a/include/rotgen/matrix.hpp +++ b/include/rotgen/matrix.hpp @@ -60,6 +60,25 @@ namespace rotgen parent::conservativeResize(new_rows, new_cols); } + matrix transpose() const + { + return matrix(static_cast(*this).transpose()); + } + + matrix conjugate() const + { + return matrix(static_cast(*this).conjugate()); + } + + matrix adjoint() const + { + return matrix(static_cast(*this).adjoint()); + } + + void transposeInPlace() { parent::transposeInPlace(); } + + void adjointInPlace() { parent::adjointInPlace(); } + friend bool operator==(matrix const& lhs, matrix const& rhs) { return static_cast(lhs) == static_cast(rhs); diff --git a/src/matrix_impl64.cpp b/src/matrix_impl64.cpp index 658b22c..40d616b 100644 --- a/src/matrix_impl64.cpp +++ b/src/matrix_impl64.cpp @@ -76,6 +76,36 @@ namespace rotgen const double* matrix_impl64::data() const { return storage_->data.data(); } + matrix_impl64 matrix_impl64::transpose() const + { + matrix_impl64 result(*this); + result.storage_->data = storage_->data.transpose(); + return result; + } + + matrix_impl64 matrix_impl64::conjugate() const + { + matrix_impl64 result(*this); + result.storage_->data = storage_->data.conjugate(); + return result; + } + + matrix_impl64 matrix_impl64::adjoint() const + { + matrix_impl64 result(*this); + result.storage_->data = storage_->data.adjoint(); + return result; + } + + void matrix_impl64::transposeInPlace() + { + storage_->data.transposeInPlace(); + } + void matrix_impl64::adjointInPlace() + { + storage_->data.adjointInPlace(); + } + //================================================================================================== // Operators //================================================================================================== diff --git a/test/basic/arithmetic_functions.cpp b/test/basic/arithmetic_functions.cpp new file mode 100644 index 0000000..8d8ad26 --- /dev/null +++ b/test/basic/arithmetic_functions.cpp @@ -0,0 +1,63 @@ +//================================================================================================== +/* + ROTGEN - Runtime Overlay for Eigen + Copyright : CODE RECKONS + SPDX-License-Identifier: BSL-1.0 +*/ +//================================================================================================== +#define TTS_MAIN +#include +#include "tts.hpp" + +template +struct MatrixDescriptor +{ + std::size_t rows, cols; + std::function init_fn; +}; + +template +void test_matrix_unary_ops(std::size_t rows, std::size_t cols, + const std::function &init_fn) +{ + + MatrixType original(rows, cols); + MatrixType transposed_matrix(cols, rows); + + for (std::size_t r = 0; r < rows; ++r) + for (std::size_t c = 0; c < cols; ++c) + init_fn(original, r, c); + + for (std::size_t r = 0; r < rows; ++r) + for (std::size_t c = 0; c < cols; ++c) + transposed_matrix(c, r) = original(r, c); + + TTS_EQUAL(original.transpose(), transposed_matrix); + TTS_EQUAL(original.conjugate(), original); + TTS_EQUAL(original.adjoint(), transposed_matrix); + + original.transposeInPlace(); + TTS_EQUAL(original, transposed_matrix); + + original.transposeInPlace(); + original.adjointInPlace(); + TTS_EQUAL(original, transposed_matrix); +} + +TTS_CASE("Matrix unary operations: transpose, adjoint, conjugate") +{ + std::vector>> test_matrices = { + {3, 3, [](auto& m, std::size_t r, std::size_t c) { m(r, c) = r + 3 * c - 2.5; }}, + {4, 9, [](auto& m, std::size_t r, std::size_t c) { m(r, c) = r*r + 3.12 * c + 6.87; }}, + {2, 7, [](auto& m, std::size_t r, std::size_t c) { m(r, c) = 1.1 * (r - c); }}, + {1, 5, [](auto &m, std::size_t r, std::size_t c) { m(r, c) = 9.99; }}, + {4, 2, [](auto& m, std::size_t r, std::size_t c) { m(r, c) = 0.0; }}, + {3, 3, [](auto& m, std::size_t r, std::size_t c) { m(r, c) = (r == c) ? 1.0 : 0.0; }}, + {2, 2, [](auto& m, std::size_t r, std::size_t c) { m(r, c) = (r + c) * 1e-10; }}, + {2, 2, [](auto& m, std::size_t r, std::size_t c) { m(r, c) = (r + 1) * 1e+10; }}, + }; + + for (auto const &desc : test_matrices) + test_matrix_unary_ops>(desc.rows, desc.cols, desc.init_fn); +}; + From 9fc9b94be769432aa1ca97e873aeee68d7abdab4 Mon Sep 17 00:00:00 2001 From: kallore Date: Tue, 20 May 2025 11:47:24 +0200 Subject: [PATCH 07/11] [TESTS][CMAKE] added the eigen library to the tests CmakeLists --- test/CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e2dfd7b..14d185c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -9,10 +9,11 @@ rotgen_setup_test_targets() ##====================================================================================================================== ## Compiler options for Unit Tests ##====================================================================================================================== +find_package (Eigen3 3.4 REQUIRED NO_MODULE) add_library(rotgen_test INTERFACE) target_compile_options(rotgen_test INTERFACE -std=c++20 -Werror -Wall -Wextra -Wshadow -Wunused-variable) target_include_directories( rotgen_test INTERFACE ${PROJECT_SOURCE_DIR}/test) -target_link_libraries(rotgen_test INTERFACE rotgen) +target_link_libraries(rotgen_test INTERFACE rotgen Eigen3::Eigen) ##====================================================================================================================== ## Unit Tests registration From a0a5a197e81f3ac4717cbff2ad74b3c29c40ffb9 Mon Sep 17 00:00:00 2001 From: kallore Date: Tue, 20 May 2025 11:49:03 +0200 Subject: [PATCH 08/11] [ARITHMETIC][REDUCTION OPERATIONS] implemented and added tests for all reduction operations --- include/rotgen/impl/matrix_impl64.hpp | 9 +++++ src/matrix_impl64.cpp | 13 ++++++ test/basic/arithmetic_functions.cpp | 58 +++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) diff --git a/include/rotgen/impl/matrix_impl64.hpp b/include/rotgen/impl/matrix_impl64.hpp index 0764624..1651f71 100644 --- a/include/rotgen/impl/matrix_impl64.hpp +++ b/include/rotgen/impl/matrix_impl64.hpp @@ -43,6 +43,15 @@ namespace rotgen void transposeInPlace(); void adjointInPlace(); + double sum() const; + double prod() const; + double mean() const; + double maxCoeff() const; + double maxCoeff(std::ptrdiff_t* row, std::ptrdiff_t* col) const; + double minCoeff() const; + double minCoeff(std::ptrdiff_t* row, std::ptrdiff_t* col) const; + double trace() const; + double& operator()(std::size_t i, std::size_t j); double const& operator()(std::size_t i, std::size_t j) const; diff --git a/src/matrix_impl64.cpp b/src/matrix_impl64.cpp index 40d616b..34cb985 100644 --- a/src/matrix_impl64.cpp +++ b/src/matrix_impl64.cpp @@ -101,11 +101,24 @@ namespace rotgen { storage_->data.transposeInPlace(); } + void matrix_impl64::adjointInPlace() { storage_->data.adjointInPlace(); } + double matrix_impl64::sum() const { return storage_->data.sum(); } + double matrix_impl64::prod() const { return storage_->data.prod(); } + double matrix_impl64::mean() const { return storage_->data.mean(); } + + double matrix_impl64::minCoeff() const { return storage_->data.minCoeff(); } + double matrix_impl64::minCoeff(std::ptrdiff_t* row, std::ptrdiff_t* col) const { return storage_->data.minCoeff(row, col); } + + double matrix_impl64::maxCoeff() const { return storage_->data.maxCoeff(); } + double matrix_impl64::maxCoeff(std::ptrdiff_t* row, std::ptrdiff_t* col) const { return storage_->data.maxCoeff(row, col); } + + double matrix_impl64::trace() const { return storage_->data.trace(); } + //================================================================================================== // Operators //================================================================================================== diff --git a/test/basic/arithmetic_functions.cpp b/test/basic/arithmetic_functions.cpp index 8d8ad26..c61f505 100644 --- a/test/basic/arithmetic_functions.cpp +++ b/test/basic/arithmetic_functions.cpp @@ -8,6 +8,7 @@ #define TTS_MAIN #include #include "tts.hpp" +#include template struct MatrixDescriptor @@ -44,6 +45,46 @@ void test_matrix_unary_ops(std::size_t rows, std::size_t cols, TTS_EQUAL(original, transposed_matrix); } +template +void test_matrix_scalar_reductions(std::size_t rows, std::size_t cols, + const std::function& init_fn) +{ + using EigenMatrix = Eigen::Matrix; + + MatrixType original(rows, cols); + EigenMatrix ref(rows, cols); + + for (std::size_t r = 0; r < rows; ++r) + for (std::size_t c = 0; c < cols; ++c) + { + init_fn(original, r, c); + ref(r, c) = original(r, c); + } + + TTS_EQUAL(original.sum(), ref.sum()); + TTS_EQUAL(original.prod(), ref.prod()); + TTS_EQUAL(original.mean(), ref.mean()); + TTS_EQUAL(original.maxCoeff(), ref.maxCoeff()); + TTS_EQUAL(original.minCoeff(), ref.minCoeff()); + TTS_EQUAL(original.trace(), ref.trace()); + + std::ptrdiff_t row, col, ref_row, ref_col; + + double cmin = original.minCoeff(&row, &col); + double rmin = ref.minCoeff(&ref_row, &ref_col); + + TTS_EQUAL(cmin, rmin); + TTS_EQUAL(row, ref_row); + TTS_EQUAL(col, ref_col); + + double cmax = original.maxCoeff(&row, &col); + double rmax = ref.maxCoeff(&ref_row, &ref_col); + + TTS_EQUAL(cmax, rmax); + TTS_EQUAL(row, ref_row); + TTS_EQUAL(col, ref_col); +} + TTS_CASE("Matrix unary operations: transpose, adjoint, conjugate") { std::vector>> test_matrices = { @@ -61,3 +102,20 @@ TTS_CASE("Matrix unary operations: transpose, adjoint, conjugate") test_matrix_unary_ops>(desc.rows, desc.cols, desc.init_fn); }; +TTS_CASE("Basic arithmetic reduction operations") +{ + std::vector>> test_matrices = { + {3, 3, [](auto& m, std::size_t r, std::size_t c) { m(r, c) = r + c; }}, + {3, 3, [](auto& m, std::size_t r, std::size_t c) { m(r, c) = 0.0; }}, + {2, 4, [](auto& m, std::size_t r, std::size_t c) { m(r, c) = -r -c*c - 1234; }}, + {4, 4, [](auto& m, std::size_t r, std::size_t c) { m(r, c) = 7.0; }}, + {1, 1, [](auto& m, std::size_t r, std::size_t c) { m(r, c) = 42.0; }}, + {4, 2, [](auto& m, std::size_t r, std::size_t c) { m(r, c) = std::sin(r + c); }}, + {1, 5, [](auto& m, std::size_t r, std::size_t c) { m(r, c) = -1.5 * r + 2.56 * c; }}, + {5, 7, [](auto& m, std::size_t r, std::size_t c) { m(r, c) = (r == c ? 1.0 : 0.0); }}, + }; + + for (const auto& [rows, cols, init_fn] : test_matrices) + test_matrix_scalar_reductions>(rows, cols, init_fn); +}; + From 2cb2724bcd25982a0cd4f853323698bb8f454b6d Mon Sep 17 00:00:00 2001 From: kallore Date: Tue, 20 May 2025 15:21:48 +0200 Subject: [PATCH 09/11] [TEST][CMAKE] removed the find eigen library --- test/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 14d185c..b9dddab 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -9,7 +9,6 @@ rotgen_setup_test_targets() ##====================================================================================================================== ## Compiler options for Unit Tests ##====================================================================================================================== -find_package (Eigen3 3.4 REQUIRED NO_MODULE) add_library(rotgen_test INTERFACE) target_compile_options(rotgen_test INTERFACE -std=c++20 -Werror -Wall -Wextra -Wshadow -Wunused-variable) target_include_directories( rotgen_test INTERFACE ${PROJECT_SOURCE_DIR}/test) From b206f40372dd5fa41536f658a3d0de6ff1c1f647 Mon Sep 17 00:00:00 2001 From: kallore Date: Tue, 20 May 2025 15:22:55 +0200 Subject: [PATCH 10/11] [ARITHMETIC][FUNCTIONS] modified teh transpose and adjoint implementations to use the in place version and avoid having a redundant copy --- src/matrix_impl64.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/matrix_impl64.cpp b/src/matrix_impl64.cpp index 34cb985..f70259a 100644 --- a/src/matrix_impl64.cpp +++ b/src/matrix_impl64.cpp @@ -79,8 +79,9 @@ namespace rotgen matrix_impl64 matrix_impl64::transpose() const { matrix_impl64 result(*this); - result.storage_->data = storage_->data.transpose(); + result.storage_->data.transposeInPlace(); return result; + } matrix_impl64 matrix_impl64::conjugate() const @@ -93,7 +94,7 @@ namespace rotgen matrix_impl64 matrix_impl64::adjoint() const { matrix_impl64 result(*this); - result.storage_->data = storage_->data.adjoint(); + result.storage_->data.adjointInPlace(); return result; } @@ -144,7 +145,8 @@ namespace rotgen return *this; } - matrix_impl64 matrix_impl64::operator-() const { + matrix_impl64 matrix_impl64::operator-() const + { matrix_impl64 result(*this); result.storage_->data = -result.storage_->data; return result; From bf343703fa8be13cbc86325e442e894e92ed8dff Mon Sep 17 00:00:00 2001 From: kallore Date: Tue, 20 May 2025 15:23:48 +0200 Subject: [PATCH 11/11] [ARITHMETICS][TESTS] used auto init_fn instead of std::function for the function called to initialize teh matrix --- test/basic/operators.cpp | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/test/basic/operators.cpp b/test/basic/operators.cpp index b3fb8f6..a3051ad 100644 --- a/test/basic/operators.cpp +++ b/test/basic/operators.cpp @@ -10,8 +10,7 @@ #include "tts.hpp" template -void test_matrix_scalar_multiplication(std::size_t rows, std::size_t cols, double scalar, - const std::function& init_fn) +void test_matrix_scalar_multiplication(std::size_t rows, std::size_t cols, double scalar, auto init_fn) { MatrixType a(rows, cols); MatrixType ref(rows, cols); @@ -33,8 +32,7 @@ void test_matrix_scalar_multiplication(std::size_t rows, std::size_t cols, doubl } template -void test_matrix_scalar_division(std::size_t rows, std::size_t cols, double scalar, - const std::function& init_fn) +void test_matrix_scalar_division(std::size_t rows, std::size_t cols, double scalar, auto init_fn) { MatrixType a(rows, cols); MatrixType ref(rows, cols); @@ -53,8 +51,8 @@ void test_matrix_scalar_division(std::size_t rows, std::size_t cols, double scal TTS_EQUAL(a, ref); } -template -void test_matrix_multiplication(std::size_t n, std::size_t m, std::size_t p, InitA&& a_init_fn, InitB&& b_init_fn) +template +void test_matrix_multiplication(std::size_t n, std::size_t m, std::size_t p, auto a_init_fn, auto b_init_fn) { MatrixType a(n, m); MatrixType b(m, p); @@ -81,8 +79,8 @@ void test_matrix_multiplication(std::size_t n, std::size_t m, std::size_t p, Ini TTS_EQUAL(a, ref); } -template -void test_matrix_addition(std::size_t rows, std::size_t cols, InitA&& a_init_fn, InitB&& b_init_fn) +template +void test_matrix_addition(std::size_t rows, std::size_t cols, auto a_init_fn, auto b_init_fn) { MatrixType a(rows, cols); MatrixType b(rows, cols); @@ -104,8 +102,8 @@ void test_matrix_addition(std::size_t rows, std::size_t cols, InitA&& a_init_fn, TTS_EQUAL(a, ref); } -template -void test_matrix_substraction(std::size_t rows, std::size_t cols, InitA&& a_init_fn, InitB&& b_init_fn) +template +void test_matrix_substraction(std::size_t rows, std::size_t cols, auto a_init_fn, auto b_init_fn) { MatrixType a(rows, cols); MatrixType b(rows, cols); @@ -252,14 +250,14 @@ TTS_CASE("Matrix addition with zero matrix") [](auto& mat, std::size_t r, std::size_t c) { mat(r, c) = 0; }); }; -TTS_CASE("Matrix substraction") +TTS_CASE("Matrix subtraction") { test_matrix_substraction>(3, 4, [](auto& mat, std::size_t r, std::size_t c) { mat(r, c) = 6.78*r - 5.2*c - 0.01; }, [](auto& mat, std::size_t r, std::size_t c) { mat(r, c) = 3.1*r + 33.456*c*c; }); }; -TTS_CASE("Matrix substraction with zero matrix") +TTS_CASE("Matrix subtraction with zero matrix") { test_matrix_substraction>(3, 4, [](auto& mat, std::size_t r, std::size_t c) { mat(r, c) = r + c*c*c*c - 56.6; },