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_
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"
36namespace matrix_vector_product_detail {
43template <
class InMatrix,
class InVector,
class OutVector>
44constexpr bool possibly_not_multipliable_v{
46 InVector::static_extent(0U) == dynamic_extent || OutVector::static_extent(0U) == dynamic_extent
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
72constexpr auto precondition_actually_multipliable(InMatrix in_matrix, InVector in_vector, OutVector out_vector)
noexcept
86constexpr auto precondition_actually_multipliable(InMatrix, InVector, OutVector)
noexcept ->
void {}
101precondition_actually_addable(LeftVector left_vector, RightVector right_vector, OutVector out_vector)
noexcept ->
void {
114constexpr auto precondition_actually_addable(LeftVector, RightVector, OutVector)
noexcept ->
void {}
117class matrix_vector_product_impl {
138 detail::constraints_for<
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 {
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"
149 compatible_static_extents<InMatrix, OutVector>(0),
150 "In a matrix-vector product, the output vector's extent must match the matrix's height"
153 precondition_actually_multipliable(in_matrix, in_vector, out_vector);
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);
180 class MultiplicandVector,
188 detail::constraints_for<
190 invoke_result_t<
decltype(row_view), InMatrix,
typename InMatrix::index_type>,
192 typename OffsetVector::value_type>> =
nullptr>
193 constexpr auto operator()(
195 MultiplicandVector multiplicand_vector,
196 OffsetVector offset_vector,
198 )
const noexcept(
noexcept(out_vector(0) = dot(row_view(in_matrix, 0), multiplicand_vector, offset_vector(0))))
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"
205 compatible_static_extents<InMatrix, OutVector>(0),
206 "In a matrix-vector product, the output vector's extent must match the matrix's height"
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"
213 precondition_actually_multipliable(in_matrix, multiplicand_vector, out_vector);
214 precondition_actually_addable(offset_vector, out_vector, out_vector);
216 using index_type = std::
217 common_type_t<typename InMatrix::index_type, typename OffsetVector::index_type, typename OutVector::index_type>;
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));
#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
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.