Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
vector_idx_abs_max.hpp
Go to the documentation of this file.
1// parasoft-suppress AUTOSAR-A2_8_1-a "False positive: also defines arene::base::linalg::vector_idx_abs_max"
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_IDX_ABS_MAX_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_VECTOR_IDX_ABS_MAX_HPP_
7
8#include "arene/base/algorithm/detail/relative_find.hpp"
11#include "arene/base/detail/make_range_from_indexable.hpp"
12#include "arene/base/functional/detail/flip.hpp"
13#include "arene/base/linalg/detail/constraints_for.hpp"
17#include "arene/base/math/detail/less_nan_highest.hpp"
18#include "arene/base/stdlib_choice/declval.hpp"
19#include "arene/base/stdlib_choice/enable_if.hpp"
20#include "arene/base/stdlib_choice/numeric_limits.hpp"
23
24// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
25// parasoft-begin-suppress AUTOSAR-A7_1_5-a "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
26
27namespace arene {
28namespace base {
29namespace linalg {
30namespace detail {
31
32/// @brief constraints specialization for @c vector_idx_abs_max_fn
33template <>
34class algorithm_constraints<class vector_idx_abs_max_fn> {
35 /// @brief constraints requiring a type to be partially ordered
36 /// @tparam T the type to check for comparison operator support
37 /// Checks that @c T supports the full set of relational and equality
38 /// comparison operators (<c>==</c>, <c>!=</c>, <c><</c>, <c>></c>,
39 /// <c><=</c>, <c>>=</c>). This is applied to the result of @c Norm
40 /// so that the maximum element can be determined.
41 template <typename T>
42 using partially_ordered_constraints = arene::base::constraints<
49
50 public:
51 /// @brief constraints for @c vector_idx_abs_max_fn
52 /// @tparam InVec input vector type (a rank-1 specialization of @c mdspan)
53 /// @tparam Norm elementwise norm function object
54 template <typename InVec, typename Norm>
55 using type = arene::base::constraints<
58 partially_ordered_constraints<invoke_result_t<Norm&, math_reference_t<InVec>>>>;
59};
60
61/// @brief class to implement @c vector_idx_abs_max
62class vector_idx_abs_max_fn {
63 public:
64 /// @brief finds the index of maximum absolute value of vector elements
65 /// @tparam InVec vector type (a rank-1 specialization of <c>mdspan</c>)
66 /// @tparam Norm normalization function that determines the absolute value
67 /// (defaults to @c decltype(arene::base::complex_l1_norm))
68 /// @param vec input vector
69 /// @param norm normalization function used to determine the absolute value
70 ///
71 /// Finds the index corresponding to the element with the maximum absolute value
72 /// in vector @c vec, where the absolute value is determined by @c norm. By
73 /// default, <c>arene::base::complex_l1_norm</c> is used. If multiple elements
74 /// contain the maximum absolute value, returns the index of the first element
75 /// with the maximum absolute value.
76 ///
77 /// @return <c>math_size_t<InVec></c> with the index of maximum absolute value
78 /// or <c>std::numeric_limits<math_size_t<InVec>>::max()</c> if @c vec is empty
79 /// @note This function is equivalent to IxAMAX from BLAS 1 with the default
80 /// @c Norm.
81 /// @note For complex types, this function is equivalent to functions ICMAX1
82 /// and IZMAX1 from LAPACK when <c> Norm = decltype(arene::base::math::abs)
83 /// </c>
84 /// @note NaN norm results are treated as the largest value; any NaN
85 /// compares greater than every non-NaN value. When one or more elements
86 /// have a NaN norm result, the index of the first such element is
87 /// returned. This does *not* implement the IEEE 754 total ordering over
88 /// NaN, which distinguishes NaN sign and payload: in this implementation, all NaN norm results are
89 /// treated as equivalent.
90 template < //
91 typename InVec,
92 typename Norm = decltype(complex_l1_norm),
93 constraints_for<vector_idx_abs_max_fn, InVec, Norm> = nullptr>
94 constexpr auto operator()(InVec vec, Norm norm = {}) const noexcept( //
95 noexcept(algorithm_detail::relative_find( //
96 arene::base::detail::make_range_from_indexable(std::declval<InVec&>()).begin(),
97 arene::base::detail::make_range_from_indexable(std::declval<InVec&>()).end(),
98 math_detail::less_nan_highest,
100 ))
101 ) -> math_size_t<InVec> {
102 if (vec.empty()) {
103 return std::numeric_limits<math_size_t<InVec>>::max();
104 }
105
106 auto const values = arene::base::detail::make_range_from_indexable(vec);
107
108 // 'arene::base::max_element' requires LegacyForwardIterator, which will not
109 // be satisfied if 'InVec' has an AccessorPolicy that returns a value.
110 // Also note that the spec doesn't require the norm result to be copyable
111 // so we can't store the best norm result on the stack and update it.
112 auto const iter = algorithm_detail::relative_find( //
113 values.begin(),
114 values.end(),
115 functional_detail::flip(math_detail::less_nan_highest),
116 norm
117 );
118
119 return static_cast<math_size_t<InVec>>(iter - values.begin());
120 }
121};
122
123} // namespace detail
124
125// parasoft-begin-suppress AUTOSAR-M7_3_3-a "Unnamed namespace creates a per-TU reference to a single global object"
126// parasoft-begin-suppress CERT_CPP-DCL59-a "Unnamed namespace creates a per-TU reference to a single global object"
127/// @brief Find the index of a vector element with greatest absolute value.
128ARENE_CPP14_INLINE_VARIABLE(detail::vector_idx_abs_max_fn, vector_idx_abs_max);
129// parasoft-end-suppress AUTOSAR-M7_3_3-a
130// parasoft-end-suppress CERT_CPP-DCL59-a
131
132} // namespace linalg
133} // namespace base
134} // namespace arene
135
136#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_VECTOR_IDX_ABS_MAX_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 >::size_type math_size_t
The mdspan-equivalent size type of T if T is a linalg object, or a substitution error if not.
Definition math_traits.hpp:81
Definition accumulate.hpp:37
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,...
constexpr auto end(Element(&array)[Size]) noexcept -> Element *
Obtain a pointer to the one-past-the-end element of the array.
Definition end.hpp:28
constexpr auto begin(Element(&array)[Size]) noexcept -> Element *
Obtain a pointer to the first element of the array.
Definition begin.hpp:28