Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
matrix_rank_1_update.hpp
Go to the documentation of this file.
1// parasoft-suppress AUTOSAR-A2_8_1-a "False positive: also defines arene::base::linalg::matrix_rank_1_update"
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_RANK_1_UPDATE_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_MATRIX_RANK_1_UPDATE_HPP_
7
10#include "arene/base/linalg/detail/constraints_for.hpp"
11#include "arene/base/linalg/detail/mandates.hpp"
12#include "arene/base/linalg/detail/zero_object.hpp"
15#include "arene/base/stdlib_choice/declval.hpp"
16#include "arene/base/stdlib_choice/enable_if.hpp"
17#include "arene/base/stdlib_choice/is_assignable.hpp"
18#include "arene/base/stdlib_choice/is_constructible.hpp"
19#include "arene/base/stdlib_choice/multiplies.hpp"
20#include "arene/base/stdlib_choice/plus.hpp"
22
23// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
24// parasoft-begin-suppress AUTOSAR-A7_1_5-a "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
25
26namespace arene {
27namespace base {
28namespace linalg {
29
30namespace detail {
31
32/// @brief Constraints for computing <c>target = static_cast<Result>(addend + (left * right))</c>
33/// @tparam Target the type being assigned to
34/// @tparam Result the type the sum is converted to before being assigned
35/// @tparam Addend the addend that the product is added to
36/// @tparam Left the left operand of the product
37/// @tparam Right the right operand of the product
38template <typename Target, typename Result, typename Addend, typename Left, typename Right>
39using multiply_add_constraints = arene::base::constraints<
42 std::enable_if_t<std::is_constructible<
43 Result,
44 invoke_result_t<std::plus<>&, Addend, invoke_result_t<std::multiplies<>&, Left, Right>>>::value>,
45 std::enable_if_t<std::is_assignable<Target, Result>::value>>;
46
47/// @brief constraints specialization for @c matrix_rank_1_update_fn
48template <>
49class algorithm_constraints<class matrix_rank_1_update_fn> {
50 public:
51 /// @brief constraints for @c matrix_rank_1_update
52 /// @tparam InVec1 The type of the first vector (a rank-1 specialization of <c>mdspan</c>)
53 /// @tparam InVec2 The type of the second vector (a rank-1 specialization of <c>mdspan</c>)
54 /// @tparam InMat The type of the matrix the outer product is added to (a rank-2 specialization of <c>mdspan</c>)
55 /// @tparam OutMat The type of the matrix the result is written to (a rank-2 specialization of <c>mdspan</c>)
56 template <typename InVec1, typename InVec2, typename InMat, typename OutMat>
57 using type = arene::base::constraints<
65 multiply_add_constraints<
66 math_reference_t<OutMat>,
67 math_value_t<OutMat>,
68 math_reference_t<InMat>,
69 math_reference_t<InVec1>,
70 math_reference_t<InVec2>>>;
71};
72
73/// @brief Function object to compute rank-1 matrix updates
74///
75/// This class implements the rank-1 update operation, computing the outer product of two vectors and either adding
76/// it to an existing matrix or overwriting a matrix with the result. The computation corresponds to the BLAS
77/// function xGER (for real element types).
78class matrix_rank_1_update_fn {
79 public:
80 /// @brief Compute the outer product of two vectors, adding it to a given matrix
81 /// @tparam InVec1 The type of the first vector (a rank-1 specialization of <c>mdspan</c>)
82 /// @tparam InVec2 The type of the second vector (a rank-1 specialization of <c>mdspan</c>)
83 /// @tparam InMat The type of the matrix the outer product is added to (a rank-2 specialization of <c>mdspan</c>)
84 /// @tparam OutMat The type of the matrix the result is written to (a rank-2 specialization of <c>mdspan</c>)
85 /// @param x_vec The first vector
86 /// @param y_vec The second vector
87 /// @param e_mat The matrix the outer product of @c x_vec and @c y_vec is added to
88 /// @param a_mat The matrix that <c>e_mat + x_vec * y_vecT</c> is written to
89 /// @pre @c a_mat and @c e_mat have compatible extents
90 /// @pre <c>row_count(a_mat) == x_vec.size()</c> and <c>column_count(a_mat) == y_vec.size()</c>
91 /// @pre @c a_mat and @c x_vec must not overlap
92 /// @pre @c a_mat and @c y_vec must not overlap
93 /// @pre if @c a_mat and @c e_mat overlap, they must alias
94 ///
95 /// @note Complexity <br>
96 /// <c>O(x_vec.extent(0) * y_vec.extent(0))</c>.
97 ///
98 /// @note This function corresponds to the BLAS function xGER (for real element types)
99 template <
100 typename InVec1,
101 typename InVec2,
102 typename InMat,
103 typename OutMat,
104 constraints_for<matrix_rank_1_update_fn, InVec1, InVec2, InMat, OutMat> = nullptr>
105 auto operator()(InVec1 x_vec, InVec2 y_vec, InMat e_mat, OutMat a_mat) const
106 noexcept(noexcept(a_mat(0U, 0U) = static_cast<math_value_t<OutMat>>(e_mat(0U, 0U) + (x_vec(0U) * y_vec(0U)))))
107 -> void;
108
109 /// @brief Compute the outer product of two vectors, overwriting a given matrix
110 /// @tparam InVec1 The type of the first vector (a rank-1 specialization of <c>mdspan</c>)
111 /// @tparam InVec2 The type of the second vector (a rank-1 specialization of <c>mdspan</c>)
112 /// @tparam OutMat The type of the matrix the result is written to (a rank-2 specialization of <c>mdspan</c>)
113 /// @param x_vec The first vector
114 /// @param y_vec The second vector
115 /// @param a_mat The matrix that <c>x_vec * y_vecT</c> is written to
116 /// @pre <c>row_count(a_mat) == x_vec.size()</c> and <c>column_count(a_mat) == y_vec.size()</c>
117 /// @pre @c a_mat and @c x_vec must not overlap
118 /// @pre @c a_mat and @c y_vec must not overlap
119 ///
120 /// @note Complexity <br>
121 /// <c>O(x_vec.extent(0) * y_vec.extent(0))</c>.
122 ///
123 /// @note This function corresponds to the BLAS function xGER (for real element types)
124 template <
125 typename InVec1,
126 typename InVec2,
127 typename OutMat,
130 constraints_for<matrix_rank_1_update_fn, InVec1, InVec2, zero_object_t<OutMat>, OutMat>> = nullptr>
131 auto operator()(InVec1 x_vec, InVec2 y_vec, OutMat a_mat) const
135 zero_object<OutMat>(std::declval<OutMat>().extents()),
137 ))) -> void;
138};
139
140} // namespace detail
141
142// parasoft-begin-suppress AUTOSAR-M7_3_3-a "Unnamed namespace creates a per-TU reference to a single global object"
143// parasoft-begin-suppress CERT_CPP-DCL59-a "Unnamed namespace creates a per-TU reference to a single global object"
144/// @brief Compute a rank-1 matrix update of two vectors.
145ARENE_CPP14_INLINE_VARIABLE(detail::matrix_rank_1_update_fn, matrix_rank_1_update);
146// parasoft-end-suppress AUTOSAR-M7_3_3-a
147// parasoft-end-suppress CERT_CPP-DCL59-a
148
149} // namespace linalg
150} // namespace base
151} // namespace arene
152
153// parasoft-end-suppress AUTOSAR-A7_1_5-a
154// parasoft-end-suppress AUTOSAR-M2_10_1-a-2
155
156#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_MATRIX_RANK_1_UPDATE_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
Definition add.hpp:20
typename math_traits< T >::value_type math_value_t
The mdspan-equivalent value type of T if T is a linalg object, or a substitution error if not.
Definition math_traits.hpp:66
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 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
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...