Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
matrix_one_norm.hpp
Go to the documentation of this file.
1// parasoft-suppress AUTOSAR-A2_8_1-a "False positive: also defines arene::base::linalg::matrix_one_norm"
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_ONE_NORM_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_MATRIX_ONE_NORM_HPP_
7
8// IWYU pragma: private, include "arene/base/linalg.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
15#include "arene/base/functional/detail/max_fn.hpp"
16#include "arene/base/linalg/detail/constraints_for.hpp"
17#include "arene/base/linalg/detail/zero_scalar.hpp"
24#include "arene/base/math/detail/less_nan_highest.hpp"
25#include "arene/base/stdlib_choice/declval.hpp"
26#include "arene/base/stdlib_choice/enable_if.hpp"
27#include "arene/base/stdlib_choice/is_convertible.hpp"
28#include "arene/base/stdlib_choice/move.hpp"
31
32// IWYU pragma: no_include "arene/base/stdlib_choice/remove_cv.hpp"
33// IWYU pragma: no_include "arene/base/stdlib_choice/remove_reference.hpp"
34
35// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
36// parasoft-begin-suppress AUTOSAR-A7_1_5-a "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
37
38namespace arene {
39namespace base {
40namespace linalg {
41namespace detail {
42
43/// @brief constraints specialization for @c matrix_one_norm_fn
44template <>
45class algorithm_constraints<class matrix_one_norm_fn> {
46 /// @brief constraints of the underlying algorithms used by @c matrix_one_norm
47 /// @tparam InVec column vector slice of the input matrix
48 /// @tparam Scalar scalar/accumulator type
49 /// @tparam Norm elementwise norm function object
50 template <typename InVec, typename Scalar, typename Norm>
51 using underlying_constraints = arene::base::constraints<
52 constraints_for<decltype(vector_abs_sum), InVec, Scalar, Norm>,
53 constraints_for<decltype(vector_idx_abs_max), InVec, auto(math_reference_t<InVec>)->Scalar>>;
54
55 public:
56 /// @brief constraints for @c matrix_one_norm
57 /// @tparam InMat input matrix type (a rank-2 specialization of @c mdspan)
58 /// @tparam Scalar scalar/accumulator type
59 /// @tparam Norm elementwise norm function object that determines the
60 /// absolute value
61 template <typename InMat, typename Scalar, typename Norm>
62 using type = arene::base::constraints<
66 underlying_constraints<column_vector_of_t<InMat>, Scalar, Norm>>;
67};
68
69/// @brief class to implement @c matrix_one_norm
70class matrix_one_norm_fn {
71 /// @brief default elementwise norm
72 using default_norm = decltype(math::abs);
73
74 public:
75 /// @brief compute the L1 (maximum absolute column sum) norm of a matrix
76 /// @tparam InMat matrix type (a rank-2 specialization of @c mdspan)
77 /// @tparam Scalar scalar/accumulator type
78 /// @tparam Norm elementwise normalization function that determines the absolute value
79 /// (defaults to @c decltype(arene::base::math::abs))
80 /// @param mat input matrix
81 /// @param init initial value to which the L1 norm is added
82 /// @param norm elementwise norm function object determining absolute value
83 /// @return the sum of @c init and the L1 norm of @c mat
84 /// @note If @c mat has zero columns, returns @c init
85 /// @note This function is equivalent to xLANGE from LAPACK when xLANGE is
86 /// specified with @c norm1 and when <c> Norm = decltype(arene::base::math::abs) </c>.
87 template < //
88 typename InMat,
89 typename Scalar,
90 typename Norm = default_norm,
91 constraints_for<matrix_one_norm_fn, InMat, Scalar, Norm> = nullptr>
92 constexpr auto operator()(InMat mat, Scalar init, Norm norm = {}) const noexcept( //
94 is_nothrow_invocable_v<decltype(vector_abs_sum), column_vector_of_t<InMat>, Scalar, Norm> //
95 ) -> Scalar {
96 // NOTE: it is semantically invalid for the result of 'abs_column_sum' to
97 // ever be ordered before 'init'. The result of a norm can never be ordered
98 // below "zero".
99 auto const abs_column_sum = bind_back(vector_abs_sum, init, std::move(norm));
100
101 return transform_reduce(
102 column_view(std::move(mat)),
103 std::move(init),
104 functional_detail::max_fn(math_detail::less_nan_highest),
105 abs_column_sum
106 );
107 }
108
109 /// @brief compute the L1 (maximum absolute column sum) norm of a matrix
110 /// @tparam InMat matrix type (a rank-2 specialization of @c mdspan)
111 /// @param mat input matrix
112 /// @return the L1 norm of @c mat
113 /// @note This function is equivalent to xLANGE from LAPACK when xLANGE is
114 /// specified with @c norm1.
115 ///
116 /// Expression-equivalent to <c> matrix_one_norm(mat, init) </c> where @c init is
117 /// initialized as <c>
118 /// arene::base::numbers::zero_v<decltype(arene::base::math::abs(mat(0, 0)))
119 /// </c>.
120 template <
121 typename InMat,
124 constraints_for<
125 matrix_one_norm_fn,
126 InMat,
127 zero_scalar_t<InMat, default_norm>,
128 default_norm>> = nullptr>
129 constexpr auto operator()(InMat mat) const noexcept( //
130 noexcept(std::declval<matrix_one_norm_fn const&>()(std::declval<InMat>(), zero_scalar_v<InMat, default_norm>))
131 ) -> decltype(std::declval<matrix_one_norm_fn const&>()(std::declval<InMat>(), zero_scalar_v<InMat, default_norm>)) {
132 return (*this)(std::move(mat), zero_scalar_v<InMat, default_norm>);
133 }
134};
135
136} // namespace detail
137
138// parasoft-begin-suppress AUTOSAR-M7_3_3-a "Unnamed namespace creates a per-TU reference to a single global object"
139// parasoft-begin-suppress CERT_CPP-DCL59-a "Unnamed namespace creates a per-TU reference to a single global object"
140/// @brief Compute the maximum absolute column sum of a matrix.
141ARENE_CPP14_INLINE_VARIABLE(detail::matrix_one_norm_fn, matrix_one_norm);
142// parasoft-end-suppress AUTOSAR-M7_3_3-a
143// parasoft-end-suppress CERT_CPP-DCL59-a
144
145} // namespace linalg
146} // namespace base
147} // namespace arene
148
149#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_MATRIX_ONE_NORM_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
constexpr bool is_nothrow_invocable_v
Determine if a functor type is no-throw invocable with the specified argument types.
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
constexpr bool is_nothrow_less_than_comparable_v
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,...