Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
matrix_vector_product.hpp
Go to the documentation of this file.
1// parasoft-suppress AUTOSAR-A2_8_1-a "False positive: also defines arene::base::linalg::column_view"
2// Copyright 2026, Toyota Motor Corporation
3//
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_MATRIX_VECTOR_PRODUCT_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_MATRIX_VECTOR_PRODUCT_HPP_
7
8// IWYU pragma: private, include "arene/base/linalg.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11// parasoft-begin-suppress AUTOSAR-A16_2_2-a "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
15#include "arene/base/detail/dynamic_extent.hpp"
16#include "arene/base/linalg/detail/constraints_for.hpp"
17#include "arene/base/linalg/detail/mandates.hpp"
18#include "arene/base/linalg/detail/preconditions.hpp"
23#include "arene/base/stdlib_choice/common_type.hpp"
24#include "arene/base/stdlib_choice/enable_if.hpp"
27// parasoft-end-suppress AUTOSAR-A16_2_2-a
28
29// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
30// parasoft-begin-suppress AUTOSAR-A7_1_5-a "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
31
32namespace arene {
33namespace base {
34namespace linalg {
35
36namespace matrix_vector_product_detail {
37
38/// @brief Type trait to check whether or not the type parameters could fail at runtime to be multipliable
39/// @tparam InMatrix Type of the matrix to multiply
40/// @tparam InVector Type of the vector to multiply
41/// @tparam OutVector Type of the vector to save the result to
42/// @note Assumes that the arguments *are* possibly-multipliable
43template <class InMatrix, class InVector, class OutVector>
44constexpr bool possibly_not_multipliable_v{
45 static_row_count_v<InMatrix> == dynamic_extent || static_column_count_v<InMatrix> == dynamic_extent ||
46 InVector::static_extent(0U) == dynamic_extent || OutVector::static_extent(0U) == dynamic_extent
47};
48
49/// @brief Type trait to check whether or not the type parameters could fail at runtime to be addable
50/// @tparam LeftVector Type of the left vector to add
51/// @tparam RightVector Type of the right vector to add
52/// @tparam OutVector Type of the vector to save the result to
53/// @note Assumes that the arguments *are* possibly-addable
54template <class LeftVector, class RightVector, class OutVector>
55constexpr bool possibly_not_addable_v{
56 LeftVector::static_extent(0U) == dynamic_extent || RightVector::static_extent(0U) == dynamic_extent ||
57 OutVector::static_extent(0U) == dynamic_extent
58};
59
60/// @brief Does a precondition check that the operands form a valid multiplication, or a no-op if this is guaranteed
61/// @tparam InMatrix Type of the matrix to multiply
62/// @tparam InVector Type of the vector to multiply
63/// @tparam OutVector Type of the vector to save the result to
64/// @param in_matrix Matrix to multiply
65/// @param in_vector Vector to multiply
66/// @param out_vector Vector to save the result to
67template <
68 class InMatrix,
69 class InVector,
70 class OutVector,
72constexpr auto precondition_actually_multipliable(InMatrix in_matrix, InVector in_vector, OutVector out_vector) noexcept
73 -> void {
74 ARENE_PRECONDITION(actually_multipliable(in_matrix, in_vector, out_vector));
75}
76
77/// @brief Does a precondition check that the operands form a valid multiplication, or a no-op if this is guaranteed
78/// @tparam InMatrix Type of the matrix to multiply
79/// @tparam InVector Type of the vector to multiply
80/// @tparam OutVector Type of the vector to save the result to
81template <
82 class InMatrix,
83 class InVector,
84 class OutVector,
86constexpr auto precondition_actually_multipliable(InMatrix, InVector, OutVector) noexcept -> void {}
87
88/// @brief Does a precondition check that the operands form a valid addition, or a no-op if this is guaranteed
89/// @tparam LeftVector Type of the left vector to add
90/// @tparam RightVector Type of the right vector to add
91/// @tparam OutVector Type of the vector to save the result to
92/// @param left_vector Left vector to add
93/// @param right_vector Right vector to add
94/// @param out_vector Vector to save the result to
95template <
96 class LeftVector,
97 class RightVector,
98 class OutVector,
100constexpr auto
101precondition_actually_addable(LeftVector left_vector, RightVector right_vector, OutVector out_vector) noexcept -> void {
102 ARENE_PRECONDITION(actually_addable(left_vector, right_vector, out_vector));
103}
104
105/// @brief Does a precondition check that the operands form a valid addition, or a no-op if this is guaranteed
106/// @tparam LeftVector Type of the left vector to add
107/// @tparam RightVector Type of the right vector to add
108/// @tparam OutVector Type of the vector to save the result to
109template <
110 class LeftVector,
111 class RightVector,
112 class OutVector,
114constexpr auto precondition_actually_addable(LeftVector, RightVector, OutVector) noexcept -> void {}
115
116/// @brief Function object implementing a matrix-vector product (matrix on left, vector on right) with optional offset
117class matrix_vector_product_impl {
118 public:
119 /// @brief Multiply a matrix with a vector on the right, saving the result to another vector
120 /// @tparam InMatrix Type of the matrix to multiply
121 /// @tparam InVector Type of the vector to multiply
122 /// @tparam OutVector Type of the vector to save the result to
123 /// @param in_matrix Matrix to multiply
124 /// @param in_vector Vector to multiply
125 /// @param out_vector Vector to save the result to
126 /// @pre The length of @c in_vector must match the width of <c>in_matrix</c>, and the length of @c out_vector must
127 /// match the height of <c>in_matrix</c>, otherwise precondition violation
128 /// @pre @c out_vector must not overlap @c in_matrix or <c>in_vector</c>, otherwise the behaviour is undefined
129 /// @note @c in_matrix and @c in_vector may overlap
130 template <
131 class InMatrix,
132 class InVector,
133 class OutVector,
138 detail::constraints_for<
139 decltype(dot),
140 invoke_result_t<decltype(row_view), InMatrix, typename InMatrix::index_type>,
141 InVector>> = nullptr>
142 constexpr auto operator()(InMatrix in_matrix, InVector in_vector, OutVector out_vector) const
143 noexcept(noexcept(out_vector(0) = dot(row_view(in_matrix, 0), in_vector))) -> void {
144 static_assert(
145 compatible_static_extents<InMatrix, InVector>(1, 0),
146 "In a matrix-vector product, the input vector's extent must match the matrix's width"
147 );
148 static_assert(
149 compatible_static_extents<InMatrix, OutVector>(0),
150 "In a matrix-vector product, the output vector's extent must match the matrix's height"
151 );
152
153 precondition_actually_multipliable(in_matrix, in_vector, out_vector);
154
156
157 for (index_type row_idx{}; cmp_less(row_idx, row_count(in_matrix)); ++row_idx) {
158 out_vector(row_idx) = dot(row_view(in_matrix, row_idx), in_vector);
159 }
160 }
161
162 /// @brief Multiply a matrix with a vector on the right and add to an offset, saving the result to another vector
163 /// @tparam InMatrix Type of the matrix to multiply
164 /// @tparam MultiplicandVector Type of the vector to multiply
165 /// @tparam OffsetVector Type of the vector to add as an offset
166 /// @tparam OutVector Type of the vector to save the result to
167 /// @param in_matrix Matrix to multiply
168 /// @param multiplicand_vector Vector to multiply
169 /// @param offset_vector Vector to add as an offset
170 /// @param out_vector Vector to save the result to
171 /// @pre The length of @c multiplicand_vector must match the width of <c>in_matrix</c>, and the lengths of both
172 /// @c offset_vector and @c out_vector must match the height of <c>in_matrix</c>, otherwise precondition violation
173 /// @pre @c out_vector must not overlap @c in_matrix or <c>multiplicand_vector</c>, otherwise the behaviour is
174 /// undefined
175 /// @pre @c out_vector must either not overlap @c offset_vector or must alias it completely, otherwise the behaviour
176 /// is undefined
177 /// @note <c>in_matrix</c>, <c>multiplicand_vector</c>, and @c offset_vector may overlap each other
178 template <
179 class InMatrix,
180 class MultiplicandVector,
181 class OffsetVector,
182 class OutVector,
188 detail::constraints_for<
189 decltype(dot),
190 invoke_result_t<decltype(row_view), InMatrix, typename InMatrix::index_type>,
191 MultiplicandVector,
192 typename OffsetVector::value_type>> = nullptr>
193 constexpr auto operator()(
194 InMatrix in_matrix,
195 MultiplicandVector multiplicand_vector,
196 OffsetVector offset_vector,
197 OutVector out_vector
198 ) const noexcept(noexcept(out_vector(0) = dot(row_view(in_matrix, 0), multiplicand_vector, offset_vector(0))))
199 -> void {
200 static_assert(
201 compatible_static_extents<InMatrix, MultiplicandVector>(1, 0),
202 "In a matrix-vector product, the input vector's extent must match the matrix's width"
203 );
204 static_assert(
205 compatible_static_extents<InMatrix, OutVector>(0),
206 "In a matrix-vector product, the output vector's extent must match the matrix's height"
207 );
208 static_assert(
209 possibly_addable_v<OffsetVector, OutVector, OutVector>,
210 "In a matrix-vector product, the offset vector (if present) must have the same extent as the output vector"
211 );
212
213 precondition_actually_multipliable(in_matrix, multiplicand_vector, out_vector);
214 precondition_actually_addable(offset_vector, out_vector, out_vector);
215
216 using index_type = std::
217 common_type_t<typename InMatrix::index_type, typename OffsetVector::index_type, typename OutVector::index_type>;
218
219 for (index_type row_idx{}; row_idx < row_count(in_matrix); ++row_idx) {
220 out_vector(row_idx) = dot(row_view(in_matrix, row_idx), multiplicand_vector, offset_vector(row_idx));
221 }
222 }
223};
224
225} // namespace matrix_vector_product_detail
226
227// parasoft-begin-suppress AUTOSAR-M7_3_3-a "Unnamed namespace creates a per-TU reference to a single global object"
228// parasoft-begin-suppress CERT_CPP-DCL59-a "Unnamed namespace creates a per-TU reference to a single global object"
229/// @brief Function object implementing a matrix-vector product with optional offset.
230ARENE_CPP14_INLINE_VARIABLE(matrix_vector_product_detail::matrix_vector_product_impl, matrix_vector_product);
231// parasoft-end-suppress AUTOSAR-M7_3_3-a
232// parasoft-end-suppress CERT_CPP-DCL59-a
233
234} // namespace linalg
235} // namespace base
236} // namespace arene
237
238// parasoft-end-suppress AUTOSAR-A7_1_5-a
239// parasoft-end-suppress AUTOSAR-M2_10_1-a-2
240
241#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_MATRIX_VECTOR_PRODUCT_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Definition add.hpp:20
constexpr std::size_t static_row_count_v
The compile-time number of rows of a matrix.
constexpr std::size_t static_column_count_v
The compile-time number of columns of a matrix.
Definition accumulate.hpp:37
typename invoke_result< Fn, Args... >::type invoke_result_t
The return type of a functor invoked with the specified arguments.
Definition is_invocable.hpp:271
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
typename common_type< Ts... >::type common_type_t
Helper alias template for common_type.
Definition common_type.hpp:99
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
Provides backports of the safe integer comparison functions from C++20.