ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
matrix_vector_product.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19#ifndef UTILS_MATRIX_VECTOR_PRODUCT_HPP
20#define UTILS_MATRIX_VECTOR_PRODUCT_HPP
21
22#include "utils/get.hpp"
23
24#include <array>
25#include <cstddef>
26#include <utility>
27
28namespace Utils {
29
30namespace detail {
31
32template <int c, typename T> struct mul {
33 constexpr T operator()(const T a) const { return c * a; }
34};
35
36template <typename T> struct mul<0, T> {
37 constexpr T operator()(const T) const { return T{}; }
38};
39
40template <typename T> struct mul<1, T> {
41 constexpr T operator()(const T a) const { return a; }
42};
43
44template <int I, typename T, typename Container, std::size_t N, int c,
45 int... cs>
46struct inner_product_template_impl {
47 constexpr T operator()(const Container &vec) const {
48 return mul<c, T>{}(get<I>(vec)) +
49 inner_product_template_impl<I + 1, T, Container, N, cs...>{}(vec);
50 }
51};
52
53template <int I, typename T, typename Container, std::size_t N, int c>
54struct inner_product_template_impl<I, T, Container, N, c> {
55 constexpr T operator()(const Container &vec) const {
56 return mul<c, T>{}(get<I>(vec));
57 }
58};
59
60template <typename T, std::size_t N,
61 const std::array<std::array<int, N>, N> &matrix, typename Container,
62 std::size_t row_index, std::size_t... column_indices>
63constexpr T inner_product_helper(const Container &vec,
64 std::index_sequence<column_indices...>) {
65 return inner_product_template_impl<
66 0, T, Container, N, get<column_indices>(get<row_index>(matrix))...>{}(
67 vec);
68}
69
70template <typename T, std::size_t N,
71 const std::array<std::array<int, N>, N> &matrix, typename Container,
72 std::size_t row_index>
73constexpr T inner_product_template(const Container &vec) {
74 return detail::inner_product_helper<T, N, matrix, Container, row_index>(
75 vec, std::make_index_sequence<N>{});
76}
77
78template <typename T, std::size_t N,
79 const std::array<std::array<int, N>, N> &matrix, typename Container,
80 std::size_t... column_indices>
81constexpr std::array<T, N>
82matrix_vector_product_helper(const Container &vec,
83 std::index_sequence<column_indices...>) {
84 return std::array<T, N>{
85 {inner_product_template<T, N, matrix, Container, column_indices>(
86 vec)...}};
87}
88
89} // namespace detail
90
91/**
92 * @brief Calculate the matrix-vector product for a statically given (square)
93 * matrix.
94 * @tparam T data type for the vector.
95 * @tparam N size of the vector.
96 * @tparam matrix const reference to a static integer array (size N) of arrays
97 * (each of size N).
98 * @tparam Container container type for the vector.
99 * @param vec Container with data of type T and length N with the vector data.
100 * @retval An array with the result of the matrix-vector product.
101 */
102template <typename T, std::size_t N,
103 const std::array<std::array<int, N>, N> &matrix, typename Container>
104constexpr std::array<T, N> matrix_vector_product(const Container &vec) {
105 return detail::matrix_vector_product_helper<T, N, matrix, Container>(
106 vec, std::make_index_sequence<N>{});
107}
108
109} // namespace Utils
110
111#endif
float N[3]
constexpr std::array< T, N > matrix_vector_product(const Container &vec)
Calculate the matrix-vector product for a statically given (square) matrix.