From ab1e21cda6ef4f7402f271519281814c50e773c9 Mon Sep 17 00:00:00 2001 From: Joana Niermann Date: Wed, 20 Nov 2024 14:48:17 +0100 Subject: [PATCH] Implement row and column getter --- .../include/algebra/array_cmath.hpp | 3 +- .../include/algebra/eigen_eigen.hpp | 3 +- .../include/algebra/eigen_generic.hpp | 3 +- .../include/algebra/fastor_fastor.hpp | 3 +- .../include/algebra/smatrix_generic.hpp | 3 +- .../include/algebra/smatrix_smatrix.hpp | 3 +- frontend/vc_aos/include/algebra/vc_aos.hpp | 3 +- .../include/algebra/vc_aos_generic.hpp | 3 +- frontend/vc_soa/include/algebra/vc_soa.hpp | 3 +- .../include/algebra/vecmem_cmath.hpp | 3 +- .../algebra/storage/impl/cmath_getter.hpp | 41 ++++++++++++++-- .../include/algebra/storage/matrix_getter.hpp | 48 +++++++++++++++++-- .../algebra/storage/impl/eigen_getter.hpp | 36 ++++++++++++-- .../algebra/storage/impl/fastor_getter.hpp | 29 +++++++++-- .../algebra/storage/impl/smatrix_getter.hpp | 27 +++++++++-- .../algebra/storage/impl/vc_aos_getter.hpp | 12 +---- .../algebra/storage/impl/vc_soa_getter.hpp | 12 +---- tests/common/test_host_basics.hpp | 12 ++--- 18 files changed, 192 insertions(+), 55 deletions(-) diff --git a/frontend/array_cmath/include/algebra/array_cmath.hpp b/frontend/array_cmath/include/algebra/array_cmath.hpp index ff4680d9..affa00a0 100644 --- a/frontend/array_cmath/include/algebra/array_cmath.hpp +++ b/frontend/array_cmath/include/algebra/array_cmath.hpp @@ -29,9 +29,10 @@ namespace getter { /// @{ using cmath::storage::block; +using cmath::storage::column; using cmath::storage::element; +using cmath::storage::row; using cmath::storage::set_block; -using cmath::storage::vector; /// @} diff --git a/frontend/eigen_eigen/include/algebra/eigen_eigen.hpp b/frontend/eigen_eigen/include/algebra/eigen_eigen.hpp index db285330..fc1169ab 100644 --- a/frontend/eigen_eigen/include/algebra/eigen_eigen.hpp +++ b/frontend/eigen_eigen/include/algebra/eigen_eigen.hpp @@ -29,9 +29,10 @@ namespace getter { /// @{ using eigen::storage::block; +using eigen::storage::column; using eigen::storage::element; +using eigen::storage::row; using eigen::storage::set_block; -using eigen::storage::vector; /// @} diff --git a/frontend/eigen_generic/include/algebra/eigen_generic.hpp b/frontend/eigen_generic/include/algebra/eigen_generic.hpp index 864e9996..070d3f08 100644 --- a/frontend/eigen_generic/include/algebra/eigen_generic.hpp +++ b/frontend/eigen_generic/include/algebra/eigen_generic.hpp @@ -32,9 +32,10 @@ namespace getter { /// @{ using eigen::storage::block; +using eigen::storage::column; using eigen::storage::element; +using eigen::storage::row; using eigen::storage::set_block; -using eigen::storage::vector; /// @} diff --git a/frontend/fastor_fastor/include/algebra/fastor_fastor.hpp b/frontend/fastor_fastor/include/algebra/fastor_fastor.hpp index 34ba3714..0f776766 100644 --- a/frontend/fastor_fastor/include/algebra/fastor_fastor.hpp +++ b/frontend/fastor_fastor/include/algebra/fastor_fastor.hpp @@ -29,9 +29,10 @@ namespace getter { /// @{ using fastor::storage::block; +using fastor::storage::column; using fastor::storage::element; +using fastor::storage::row; using fastor::storage::set_block; -using fastor::storage::vector; /// @} diff --git a/frontend/smatrix_generic/include/algebra/smatrix_generic.hpp b/frontend/smatrix_generic/include/algebra/smatrix_generic.hpp index f9a933fe..9627f7f3 100644 --- a/frontend/smatrix_generic/include/algebra/smatrix_generic.hpp +++ b/frontend/smatrix_generic/include/algebra/smatrix_generic.hpp @@ -23,9 +23,10 @@ namespace getter { /// @{ using smatrix::storage::block; +using smatrix::storage::column; using smatrix::storage::element; +using smatrix::storage::row; using smatrix::storage::set_block; -using smatrix::storage::vector; /// @} diff --git a/frontend/smatrix_smatrix/include/algebra/smatrix_smatrix.hpp b/frontend/smatrix_smatrix/include/algebra/smatrix_smatrix.hpp index f6946946..8490130c 100644 --- a/frontend/smatrix_smatrix/include/algebra/smatrix_smatrix.hpp +++ b/frontend/smatrix_smatrix/include/algebra/smatrix_smatrix.hpp @@ -20,9 +20,10 @@ namespace getter { /// @{ using smatrix::storage::block; +using smatrix::storage::column; using smatrix::storage::element; +using smatrix::storage::row; using smatrix::storage::set_block; -using smatrix::storage::vector; /// @} diff --git a/frontend/vc_aos/include/algebra/vc_aos.hpp b/frontend/vc_aos/include/algebra/vc_aos.hpp index 66947c85..b506630f 100644 --- a/frontend/vc_aos/include/algebra/vc_aos.hpp +++ b/frontend/vc_aos/include/algebra/vc_aos.hpp @@ -24,9 +24,10 @@ namespace getter { /// @{ using vc_aos::storage::block; +using vc_aos::storage::column; using vc_aos::storage::element; +using vc_aos::storage::row; using vc_aos::storage::set_block; -using vc_aos::storage::vector; /// @} diff --git a/frontend/vc_aos_generic/include/algebra/vc_aos_generic.hpp b/frontend/vc_aos_generic/include/algebra/vc_aos_generic.hpp index f12ce1c2..654dc7ca 100644 --- a/frontend/vc_aos_generic/include/algebra/vc_aos_generic.hpp +++ b/frontend/vc_aos_generic/include/algebra/vc_aos_generic.hpp @@ -31,9 +31,10 @@ namespace getter { /// @{ using vc_aos::storage::block; +using vc_aos::storage::column; using vc_aos::storage::element; +using vc_aos::storage::row; using vc_aos::storage::set_block; -using vc_aos::storage::vector; /// @} diff --git a/frontend/vc_soa/include/algebra/vc_soa.hpp b/frontend/vc_soa/include/algebra/vc_soa.hpp index 09b5c0ef..46d1aced 100644 --- a/frontend/vc_soa/include/algebra/vc_soa.hpp +++ b/frontend/vc_soa/include/algebra/vc_soa.hpp @@ -35,9 +35,10 @@ namespace getter { /// @{ using vc_soa::storage::block; +using vc_soa::storage::column; using vc_soa::storage::element; +using vc_soa::storage::row; using vc_soa::storage::set_block; -using vc_soa::storage::vector; /// @} diff --git a/frontend/vecmem_cmath/include/algebra/vecmem_cmath.hpp b/frontend/vecmem_cmath/include/algebra/vecmem_cmath.hpp index 914c95f1..1a3ff17c 100644 --- a/frontend/vecmem_cmath/include/algebra/vecmem_cmath.hpp +++ b/frontend/vecmem_cmath/include/algebra/vecmem_cmath.hpp @@ -29,9 +29,10 @@ namespace getter { /// @{ using cmath::storage::block; +using cmath::storage::column; using cmath::storage::element; +using cmath::storage::row; using cmath::storage::set_block; -using cmath::storage::vector; /// @} diff --git a/storage/cmath/include/algebra/storage/impl/cmath_getter.hpp b/storage/cmath/include/algebra/storage/impl/cmath_getter.hpp index 79ddd6d7..b857fd60 100644 --- a/storage/cmath/include/algebra/storage/impl/cmath_getter.hpp +++ b/storage/cmath/include/algebra/storage/impl/cmath_getter.hpp @@ -165,11 +165,31 @@ struct block_getter { return submatrix; } - /// Operator producing a vector out of a const matrix + /// Operator producing a row vector out of a const matrix template class array_t> - ALGEBRA_HOST_DEVICE inline array_t vector( + ALGEBRA_HOST_DEVICE inline array_t row( + const array_t, COLS> &m, std::size_t row, + std::size_t col) { + + assert(col + SIZE <= COLS); + assert(row < ROWS); + + array_t subvector{}; + + for (std::size_t icol = col; icol < col + SIZE; ++icol) { + subvector[icol - col] = m[icol][row]; + } + + return subvector; + } + + /// Operator producing a column vector out of a const matrix + template class array_t> + ALGEBRA_HOST_DEVICE inline array_t column( const array_t, COLS> &m, std::size_t row, std::size_t col) { @@ -196,15 +216,26 @@ ALGEBRA_HOST_DEVICE decltype(auto) block(const input_matrix_type &m, return block_getter().template operator()(m, row, col); } -/// Function extracting a vector from a matrix +/// Function extracting a row vector from a matrix +template class array_t> +ALGEBRA_HOST_DEVICE inline array_t row( + const array_t, COLS> &m, std::size_t row, + std::size_t col) { + + return block_getter().template row(m, row, col); +} + +/// Function extracting a column vector from a matrix template class array_t> -ALGEBRA_HOST_DEVICE inline array_t vector( +ALGEBRA_HOST_DEVICE inline array_t column( const array_t, COLS> &m, std::size_t row, std::size_t col) { - return block_getter().template vector(m, row, col); + return block_getter().template column(m, row, col); } /// Sets a matrix of dimension @tparam ROW and @tparam COL as submatrix of diff --git a/storage/common/include/algebra/storage/matrix_getter.hpp b/storage/common/include/algebra/storage/matrix_getter.hpp index 53582880..4404a457 100644 --- a/storage/common/include/algebra/storage/matrix_getter.hpp +++ b/storage/common/include/algebra/storage/matrix_getter.hpp @@ -178,18 +178,42 @@ struct block_getter { return res_m; } - /// Get a vector of a const matrix + /// Get a row vector of a const matrix template class array_t> - ALGEBRA_HOST_DEVICE constexpr auto vector( + ALGEBRA_HOST_DEVICE constexpr auto row( + const matrix &m, const std::size_t row, + const std::size_t col) noexcept { + + static_assert(SIZE <= ROWS); + static_assert(SIZE <= COLS); + assert(row < ROWS); + assert(col + SIZE <= COLS); + + using vector_t = algebra::storage::vector; + + vector_t res_v{}; + + for (std::size_t i = col; i < col + SIZE; ++i) { + res_v[i - col] = m[i][row]; + } + + return res_v; + } + + /// Get a column vector of a const matrix + template class array_t> + ALGEBRA_HOST_DEVICE constexpr auto column( const matrix &m, const std::size_t row, const std::size_t col) noexcept { static_assert(SIZE <= ROWS); static_assert(SIZE <= COLS); assert(row + SIZE <= ROWS); - assert(col <= COLS); + assert(col < COLS); using input_matrix_t = matrix; using vector_t = algebra::storage::vector; @@ -283,4 +307,22 @@ ALGEBRA_HOST_DEVICE constexpr void set_block( } } +template class array_t> +ALGEBRA_HOST_DEVICE constexpr auto row( + const algebra::storage::matrix &m, + const std::size_t row, const std::size_t col) noexcept { + return algebra::storage::block_getter{}.template row(m, row, col); +} + +template class array_t> +ALGEBRA_HOST_DEVICE constexpr auto column( + const algebra::storage::matrix &m, + const std::size_t row, const std::size_t col) noexcept { + return algebra::storage::block_getter{}.template column(m, row, col); +} + } // namespace algebra::storage diff --git a/storage/eigen/include/algebra/storage/impl/eigen_getter.hpp b/storage/eigen/include/algebra/storage/impl/eigen_getter.hpp index 58a5493a..64ed31ed 100644 --- a/storage/eigen/include/algebra/storage/impl/eigen_getter.hpp +++ b/storage/eigen/include/algebra/storage/impl/eigen_getter.hpp @@ -139,7 +139,25 @@ struct block_getter { template - ALGEBRA_HOST_DEVICE decltype(auto) vector(Eigen::MatrixBase &m, + ALGEBRA_HOST_DEVICE decltype(auto) row(Eigen::MatrixBase &m, + size_type_1 row, + size_type_2 col) const { + + return m.template block<1, SIZE>(row, col); + } + + template + ALGEBRA_HOST_DEVICE decltype(auto) row( + const Eigen::MatrixBase &m, size_type_1 row, + size_type_2 col) const { + + return m.template block<1, SIZE>(row, col); + } + + template + ALGEBRA_HOST_DEVICE decltype(auto) column(Eigen::MatrixBase &m, size_type_1 row, size_type_2 col) const { @@ -148,7 +166,7 @@ struct block_getter { template - ALGEBRA_HOST_DEVICE decltype(auto) vector( + ALGEBRA_HOST_DEVICE decltype(auto) column( const Eigen::MatrixBase &m, size_type_1 row, size_type_2 col) const { @@ -176,11 +194,21 @@ ALGEBRA_HOST_DEVICE decltype(auto) block(Eigen::MatrixBase &m, /// Function extracting a slice from the matrix template -ALGEBRA_HOST_DEVICE inline decltype(auto) vector( +ALGEBRA_HOST_DEVICE inline decltype(auto) row( + const Eigen::MatrixBase &m, std::size_t row, + std::size_t col) { + + return block_getter{}.template row(m, static_cast(row), + static_cast(col)); +} + +/// Function extracting a slice from the matrix +template +ALGEBRA_HOST_DEVICE inline decltype(auto) column( const Eigen::MatrixBase &m, std::size_t row, std::size_t col) { - return block_getter{}.template vector(m, static_cast(row), + return block_getter{}.template column(m, static_cast(row), static_cast(col)); } diff --git a/storage/fastor/include/algebra/storage/impl/fastor_getter.hpp b/storage/fastor/include/algebra/storage/impl/fastor_getter.hpp index c01f45c7..e1cb0c60 100644 --- a/storage/fastor/include/algebra/storage/impl/fastor_getter.hpp +++ b/storage/fastor/include/algebra/storage/impl/fastor_getter.hpp @@ -143,7 +143,18 @@ struct block_getter { template - ALGEBRA_HOST_DEVICE Fastor::Tensor vector( + ALGEBRA_HOST_DEVICE Fastor::Tensor row( + const Fastor::Tensor &m, std::size_t row, + std::size_t col) const { + + return Fastor::Tensor( + m(static_cast(row), + Fastor::seq(static_cast(col), static_cast(col + SIZE)))); + } + + template + ALGEBRA_HOST_DEVICE Fastor::Tensor column( const Fastor::Tensor &m, std::size_t row, std::size_t col) const { @@ -163,14 +174,24 @@ ALGEBRA_HOST_DEVICE decltype(auto) block( return block_getter{}.template operator()(m, row, col); } -/// Function extracting a slice from the matrix +/// Function extracting a row vector from the matrix +template +ALGEBRA_HOST_DEVICE inline decltype(auto) row( + const Fastor::Tensor &m, std::size_t row, + std::size_t col) { + + return block_getter{}.template row(m, row, col); +} + +/// Function extracting a column vector from the matrix template -ALGEBRA_HOST_DEVICE inline decltype(auto) vector( +ALGEBRA_HOST_DEVICE inline decltype(auto) column( const Fastor::Tensor &m, std::size_t row, std::size_t col) { - return block_getter{}.template vector(m, row, col); + return block_getter{}.template column(m, row, col); } /// Operator setting a block with a matrix diff --git a/storage/smatrix/include/algebra/storage/impl/smatrix_getter.hpp b/storage/smatrix/include/algebra/storage/impl/smatrix_getter.hpp index e25c1de0..3c6c6c72 100644 --- a/storage/smatrix/include/algebra/storage/impl/smatrix_getter.hpp +++ b/storage/smatrix/include/algebra/storage/impl/smatrix_getter.hpp @@ -133,7 +133,16 @@ struct block_getter { template - ALGEBRA_HOST_DEVICE ROOT::Math::SVector vector( + ALGEBRA_HOST_DEVICE ROOT::Math::SVector row( + const ROOT::Math::SMatrix &m, unsigned int row, + unsigned int col) const { + + return m.template SubRow>(row, col); + } + + template + ALGEBRA_HOST_DEVICE ROOT::Math::SVector column( const ROOT::Math::SMatrix &m, unsigned int row, unsigned int col) const { @@ -167,11 +176,23 @@ ALGEBRA_HOST_DEVICE ROOT::Math::SMatrix block( /// @c algebra::smatrix::transform3 template -ALGEBRA_HOST_DEVICE inline auto vector( +ALGEBRA_HOST_DEVICE inline auto row( + const ROOT::Math::SMatrix &m, std::size_t row, + std::size_t col) { + + return block_getter{}.template row(m, static_cast(row), + static_cast(col)); +} + +/// Function extracting a slice from the matrix used by +/// @c algebra::smatrix::transform3 +template +ALGEBRA_HOST_DEVICE inline auto column( const ROOT::Math::SMatrix &m, std::size_t row, std::size_t col) { - return block_getter{}.template vector(m, static_cast(row), + return block_getter{}.template column(m, static_cast(row), static_cast(col)); } diff --git a/storage/vc_aos/include/algebra/storage/impl/vc_aos_getter.hpp b/storage/vc_aos/include/algebra/storage/impl/vc_aos_getter.hpp index 3bc7bbb2..ccf49f3d 100644 --- a/storage/vc_aos/include/algebra/storage/impl/vc_aos_getter.hpp +++ b/storage/vc_aos/include/algebra/storage/impl/vc_aos_getter.hpp @@ -15,17 +15,9 @@ namespace algebra::vc_aos::storage { using algebra::storage::block; +using algebra::storage::column; using algebra::storage::element; +using algebra::storage::row; using algebra::storage::set_block; -/// Get a vector of a const matrix -template class array_t> -ALGEBRA_HOST_DEVICE constexpr decltype(auto) vector( - const algebra::storage::matrix &m, - const std::size_t row, const std::size_t col) noexcept { - return algebra::storage::block_getter{}.template vector(m, row, col); -} - } // namespace algebra::vc_aos::storage diff --git a/storage/vc_soa/include/algebra/storage/impl/vc_soa_getter.hpp b/storage/vc_soa/include/algebra/storage/impl/vc_soa_getter.hpp index 98cf14ee..ed75fd43 100644 --- a/storage/vc_soa/include/algebra/storage/impl/vc_soa_getter.hpp +++ b/storage/vc_soa/include/algebra/storage/impl/vc_soa_getter.hpp @@ -15,17 +15,9 @@ namespace algebra::vc_soa::storage { using algebra::storage::block; +using algebra::storage::column; using algebra::storage::element; +using algebra::storage::row; using algebra::storage::set_block; -/// Get a vector of a const matrix -template class array_t> -ALGEBRA_HOST_DEVICE constexpr auto vector( - const algebra::storage::matrix &m, - const std::size_t row, const std::size_t col) noexcept { - return algebra::storage::block_getter{}.template vector(m, row, col); -} - } // namespace algebra::vc_soa::storage diff --git a/tests/common/test_host_basics.hpp b/tests/common/test_host_basics.hpp index cae958ae..23e4b412 100644 --- a/tests/common/test_host_basics.hpp +++ b/tests/common/test_host_basics.hpp @@ -981,17 +981,17 @@ TYPED_TEST_P(test_host_basics_transform, transform3) { test_vector; // we need to declare a variable in order to use the // [[maybe_unused]] attribute here - test_vector = algebra::getter::vector<3>(m44, 0, 0); - test_vector = algebra::getter::vector<3>(m44, 0, 1); - test_vector = algebra::getter::vector<3>(m44, 0, 2); + test_vector = algebra::getter::column<3>(m44, 0, 0); + test_vector = algebra::getter::column<3>(m44, 0, 1); + test_vector = algebra::getter::column<3>(m44, 0, 2); // Test constructor from inverse matrix auto m44_inv = trf2.matrix_inverse(); // Make sure that algebra::getter:vector can be called. - test_vector = algebra::getter::vector<3>(m44_inv, 0, 0); - test_vector = algebra::getter::vector<3>(m44_inv, 0, 1); - test_vector = algebra::getter::vector<3>(m44_inv, 0, 2); + test_vector = algebra::getter::column<3>(m44_inv, 0, 0); + test_vector = algebra::getter::column<3>(m44_inv, 0, 1); + test_vector = algebra::getter::column<3>(m44_inv, 0, 2); // Re-evaluate rot and trn auto rotm = trfm.rotation();