Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
vector_abs_sum.hpp
Go to the documentation of this file.
1// parasoft-suppress AUTOSAR-A2_8_1-a "False positive: also defines arene::base::linalg::vector_abs_sum"
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_VECTOR_ABS_SUM_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_VECTOR_ABS_SUM_HPP_
7
11#include "arene/base/detail/make_range_from_indexable.hpp"
12#include "arene/base/linalg/detail/constraints_for.hpp"
13#include "arene/base/linalg/detail/zero_scalar.hpp"
17#include "arene/base/stdlib_choice/declval.hpp"
18#include "arene/base/stdlib_choice/enable_if.hpp"
19#include "arene/base/stdlib_choice/is_assignable.hpp"
20#include "arene/base/stdlib_choice/move.hpp"
21#include "arene/base/stdlib_choice/plus.hpp"
23
24// IWYU pragma: no_include "arene/base/stdlib_choice/remove_cv.hpp"
25// IWYU pragma: no_include "arene/base/stdlib_choice/remove_reference.hpp"
26
27// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
28// parasoft-begin-suppress AUTOSAR-A7_1_5-a "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
29
30namespace arene {
31namespace base {
32namespace linalg {
33namespace detail {
34
35/// @brief constraints specialization for @c vector_abs_sum_fn
36template <>
37class algorithm_constraints<class vector_abs_sum_fn> {
38 /// @brief constraints for the reduce update operation
39 /// @tparam Dest destination (accumulator) type being assigned to
40 /// @tparam Reduce binary reduction function object (e.g. @c std::plus<>)
41 /// @tparam Source1 first source operand type (typically the running accumulator)
42 /// @tparam Source2 second source operand type (typically the per-element norm result)
43 ///
44 /// Checks that @c Reduce is invocable with every combination of the two source types
45 /// and that the result of <c> Reduce(Source1, Source2) </c> is assignable to
46 /// @c Dest. This provides the constraints of GENERALIZED_SUM.
47 template <typename Dest, typename Reduce, typename Source1, typename Source2>
48 using reduce_update_constraints = arene::base::constraints<
53 std::enable_if_t<std::is_assignable< //
54 Dest, //
56 Reduce, //
57 Source1, //
58 Source2>>::value> //
59 >;
60
61 public:
62 /// @brief constraints for @c vector_abs_sum
63 /// @tparam InVec input vector type (a rank-1 specialization of @c mdspan)
64 /// @tparam Scalar scalar/accumulator type
65 /// @tparam Norm elementwise norm function object
66 template <typename InVec, typename Scalar, typename Norm>
67 using type = arene::base::constraints<
71 reduce_update_constraints<Scalar&, std::plus<>&, Scalar&&, invoke_result_t<Norm&, math_reference_t<InVec>>>>;
72};
73
74/// @brief class to implement @c vector_abs_sum
75class vector_abs_sum_fn {
76 /// @brief default elementwise norm
77 using default_norm = decltype(complex_l1_norm);
78
79 public:
80 /// @brief compute the sum of absolute values
81 /// @tparam InVec vector type (a rank-1 specialization of <c>mdspan</c>)
82 /// @tparam Scalar scalar/accumulator type
83 /// @tparam Norm elementwise normalization function that determines the absolute value
84 /// (defaults to @c decltype(arene::base::complex_l1_norm))
85 /// @param vec input vector
86 /// @param init initial value to which the sum of absolute values is added
87 /// @param norm elementwise normalization function used to determine the absolute value
88 /// @pre The sum of absolute values of @c vec would not overflow <c>init</c>, otherwise the
89 /// behaviour is undefined
90 /// @return <c> GENERALIZED_SUM(std::plus<>{}, init, norm(v(0)), ..., norm(v(v.size() - 1))) </c>
91 /// @note This function is equivalent to xASUM from BLAS 1 with the default
92 /// @c Norm.
93 /// @note For complex types, this function is equivalent to functions SCSUM1
94 /// and DZSUM1 from LAPACK when <c> Norm = decltype(arene::base::math::abs)
95 /// </c>
96 template <
97 typename InVec,
98 typename Scalar,
99 typename Norm = default_norm,
100 constraints_for<vector_abs_sum_fn, InVec, Scalar, Norm> = nullptr>
101 constexpr auto operator()(InVec vec, Scalar init, Norm norm = {}) const noexcept( //
102 noexcept(transform_reduce( //
103 arene::base::detail::make_range_from_indexable(std::declval<InVec>()),
105 std::declval<std::plus<>>(),
107 ))
108 ) -> Scalar {
109 return transform_reduce( //
110 arene::base::detail::make_range_from_indexable(std::move(vec)),
111 std::move(init),
112 std::plus<>{},
113 std::move(norm)
114 );
115 }
116
117 /// @brief compute the sum of absolute values
118 /// @tparam InVec vector type (a rank-1 specialization of <c>mdspan</c>)
119 /// @param vec input vector
120 ///
121 /// Let @c init be initialized to <c>
122 /// arene::base::numbers::zero_v<decltype(arene::base::complex_l1_norm(vec(0)))
123 /// </c>.
124 ///
125 /// @pre The sum of absolute values of @c vec would not overflow <c>init</c>, otherwise the
126 /// behaviour is undefined
127 /// @return <c> GENERALIZED_SUM(std::plus<>{}, init, norm(v(0)), ..., norm(v(v.size() - 1))) </c>
128 /// @note This function is equivalent to xASUM from BLAS 1.
129 template < //
130 typename InVec,
133 constraints_for<
134 vector_abs_sum_fn,
135 InVec,
136 zero_scalar_t<InVec, default_norm>,
137 default_norm>> = nullptr>
138 constexpr auto operator()(InVec vec) const noexcept( //
139 noexcept(std::declval<vector_abs_sum_fn const&>()(std::declval<InVec>(), zero_scalar_v<InVec, default_norm>))
140 ) -> decltype(std::declval<vector_abs_sum_fn const&>()(std::declval<InVec>(), zero_scalar_v<InVec, default_norm>)) {
141 return (*this)(std::move(vec), zero_scalar_v<InVec, default_norm>);
142 }
143};
144
145} // namespace detail
146
147// parasoft-begin-suppress AUTOSAR-M7_3_3-a "Unnamed namespace creates a per-TU reference to a single global object"
148// parasoft-begin-suppress CERT_CPP-DCL59-a "Unnamed namespace creates a per-TU reference to a single global object"
149/// @brief Compute the sum of the absolute values of a vector's elements.
150ARENE_CPP14_INLINE_VARIABLE(detail::vector_abs_sum_fn, vector_abs_sum);
151// parasoft-end-suppress AUTOSAR-M7_3_3-a
152// parasoft-end-suppress CERT_CPP-DCL59-a
153
154} // namespace linalg
155} // namespace base
156} // namespace arene
157
158#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_VECTOR_ABS_SUM_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
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
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
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,...