Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
matrix_observers.hpp
Go to the documentation of this file.
1// parasoft-suppress AUTOSAR-A2_8_1-a "False positive: also defines arene::base::linalg::row_count"
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_OBSERVERS_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_MATRIX_OBSERVERS_HPP_
7
8// IWYU pragma: private, include "arene/base/linalg.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
14#include "arene/base/linalg/detail/mandates.hpp"
17#include "arene/base/stdlib_choice/cstddef.hpp"
18#include "arene/base/stdlib_choice/enable_if.hpp"
19#include "arene/base/stdlib_choice/integral_constant.hpp"
20// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
21
22// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
23// parasoft-begin-suppress AUTOSAR-A7_1_5-a "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
24
25namespace arene {
26namespace base {
27namespace linalg {
28
29namespace matrix_observers_detail {
30
31/// @brief Function object returning the dynamic extent of a matrix along a given axis
32/// @tparam Axis The matrix axis to query: @c 0 for rows, @c 1 for columns
33template <std::size_t Axis>
34class extent_accessor_fn {
35 public:
36 // parasoft-begin-suppress AUTOSAR-A0_1_4-a "False positive: parameter 'matrix' is used"
37 // parasoft-begin-suppress AUTOSAR-A8_4_2-a "False positive: function does return a value"
38 // parasoft-begin-suppress CERT_C-MSC37-a "False positive: function does return a value"
39 // parasoft-begin-suppress CERT_CPP-MSC52-a "False positive: function does return a value"
40 /// @brief Return the extent of @c matrix along @c Axis
41 /// @tparam Mat Matrix type; must satisfy @c is_in_matrix_v
42 /// @param matrix Matrix whose extent along @c Axis is returned
43 /// @return @c matrix.extent(Axis)
44 template <typename Mat, arene::base::constraints<std::enable_if_t<is_in_matrix_v<Mat>>> = nullptr>
45 constexpr auto operator()(Mat matrix) const noexcept -> math_index_t<Mat> {
46 return matrix.extent(Axis);
47 }
48 // parasoft-end-suppress CERT_CPP-MSC52-a
49 // parasoft-end-suppress CERT_C-MSC37-a
50 // parasoft-end-suppress AUTOSAR-A8_4_2-a
51 // parasoft-end-suppress AUTOSAR-A0_1_4-a
52};
53
54/// @brief Helper trait for implementing @c static_row_count_v and @c static_column_count_v for when @c Mat is not an
55/// in-matrix
56/// @tparam Mat The matrix type
57/// @tparam Axis The matrix axis to query: @c 0 for rows, @c 1 for columns
58/// @see static_row_count_v
59/// @see static_column_count_v
60///
61/// The primary defines no @c value, so naming it for a type that is not an in-matrix is ill-formed. A rank-1 or rank-3
62/// @c mdspan does have an extent along @c Axis, but it has no rows or columns, so the query is rejected rather than
63/// answered with @c dynamic_extent. The @c static_assert reports that in the same words on every compiler.
64template <typename Mat, std::size_t Axis, typename = arene::base::constraints<>>
65class static_extent {
66 static_assert(is_in_matrix_v<Mat>, "static_row_count/static_column_count: Mat must be an in-matrix");
67};
68
69/// @brief Helper trait for implementing @c static_row_count_v and @c static_column_count_v for when @c Mat is an
70/// in-matrix
71/// @tparam Mat The matrix type
72/// @tparam Axis The matrix axis to query: @c 0 for rows, @c 1 for columns
73/// @see static_row_count_v
74/// @see static_column_count_v
75template <typename Mat, std::size_t Axis>
76class static_extent<Mat, Axis, arene::base::constraints<std::enable_if_t<is_in_matrix_v<Mat>>>>
77 : public std::integral_constant<std::size_t, math_extents_t<Mat>::static_extent(Axis)> {};
78
79} // namespace matrix_observers_detail
80
81/// @brief Function object returning the number of rows of a matrix
82///
83/// Given an in-matrix @c A, @c row_count(A) returns <c>A.extent(0)</c>.
84/// @note Requires @c is_in_matrix_v<Mat> to be @c true.
85// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
86// object used in multiple TUs."
87// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
88// object used in multiple TUs."
89ARENE_CPP14_INLINE_VARIABLE(matrix_observers_detail::extent_accessor_fn<0U>, row_count);
90
91/// @brief Function object returning the number of columns of a matrix
92///
93/// Given an in-matrix @c A, @c column_count(A) returns <c>A.extent(1)</c>.
94/// @note Requires @c is_in_matrix_v<Mat> to be @c true.
95ARENE_CPP14_INLINE_VARIABLE(matrix_observers_detail::extent_accessor_fn<1U>, column_count);
96// parasoft-end-suppress AUTOSAR-M7_3_3-a
97// parasoft-end-suppress CERT_CPP-DCL59-a
98
99/// @brief The compile-time number of rows of a matrix
100/// @tparam Mat Matrix type; must satisfy @c is_in_matrix_v
101/// @note Ill-formed when @c Mat is not an in-matrix.
102///
103/// Equals <c>math_extents_t<Mat>::static_extent(0)</c>, which is @c dynamic_extent if the row extent is not known
104/// statically.
105template <typename Mat>
106extern constexpr std::size_t static_row_count_v{matrix_observers_detail::static_extent<Mat, 0U>::value};
107
108/// @brief The compile-time number of columns of a matrix
109/// @tparam Mat Matrix type; must satisfy @c is_in_matrix_v
110/// @note Ill-formed when @c Mat is not an in-matrix.
111///
112/// Equals <c>math_extents_t<Mat>::static_extent(1)</c>, which is @c dynamic_extent if the column extent is not known
113/// statically.
114template <typename Mat>
115extern constexpr std::size_t static_column_count_v{matrix_observers_detail::static_extent<Mat, 1U>::value};
116
117/// @brief Trait holding the compile-time number of rows of a matrix
118/// @tparam Mat Matrix type; must satisfy @c is_in_matrix_v
119/// @note Ill-formed when @c Mat is not an in-matrix.
120/// @see static_row_count_v
121template <typename Mat>
122class static_row_count : public std::integral_constant<std::size_t, static_row_count_v<Mat>> {};
123
124/// @brief Trait holding the compile-time number of columns of a matrix
125/// @tparam Mat Matrix type; must satisfy @c is_in_matrix_v
126/// @note Ill-formed when @c Mat is not an in-matrix.
127/// @see static_column_count_v
128template <typename Mat>
129class static_column_count : public std::integral_constant<std::size_t, static_column_count_v<Mat>> {};
130
131namespace matrix_observers_detail {
132
133/// @brief Class to implement @c is_square
134class is_square_fn {
135 public:
136 /// @brief Check whether @c matrix has as many rows as it has columns
137 /// @tparam Mat Matrix type; must satisfy @c possibly_square_v
138 /// @param matrix Matrix to check
139 /// @return @c true if <c>row_count(matrix) == column_count(matrix)</c>, @c false otherwise
140 template <typename Mat, arene::base::constraints<std::enable_if_t<possibly_square_v<Mat>>> = nullptr>
141 constexpr auto operator()(Mat matrix) const noexcept -> bool {
142 return row_count(matrix) == column_count(matrix);
143 }
144
145 /// @brief Check whether a matrix whose static extents cannot be equal has as many rows as it has columns
146 /// @tparam Mat Matrix type; must satisfy @c is_in_matrix_v but not @c possibly_square_v
147 /// @return @c false
148 template <
149 typename Mat,
150 arene::base::constraints<std::enable_if_t<is_in_matrix_v<Mat> && !possibly_square_v<Mat>>> = nullptr>
151 constexpr auto operator()(Mat /*matrix*/) const noexcept -> bool {
152 return false;
153 }
154};
155
156} // namespace matrix_observers_detail
157
158/// @brief Function object returning whether or not a matrix has as many rows as it has columns
159///
160/// Given an in-matrix @c A, @c is_square(A) returns <c>row_count(A) == column_count(A)</c>, except that it returns
161/// @c false without inspecting @c A when the static extents of @c A's type cannot be equal.
162/// @note Requires @c is_in_matrix_v<Mat> to be @c true.
163// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
164// object used in multiple TUs."
165// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
166// object used in multiple TUs."
167ARENE_CPP14_INLINE_VARIABLE(matrix_observers_detail::is_square_fn, is_square);
168// parasoft-end-suppress AUTOSAR-M7_3_3-a
169// parasoft-end-suppress CERT_CPP-DCL59-a
170
171} // namespace linalg
172} // namespace base
173} // namespace arene
174
175#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_MATRIX_OBSERVERS_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
Trait holding the compile-time number of columns of a matrix.
Definition matrix_observers.hpp:129
Trait holding the compile-time number of rows of a matrix.
Definition matrix_observers.hpp:122
A helper class to represent a compile-time constant with the specified type and value....
Definition integral_constant.hpp:23
Definition add.hpp:20
constexpr std::size_t static_row_count_v
The compile-time number of rows of a matrix.
constexpr bool is_in_matrix_v
Determine if the given type is an in-matrix, usable as an input matrix for linalg algorithms.
constexpr std::size_t static_column_count_v
The compile-time number of columns of a matrix.
typename math_traits< T >::index_type math_index_t
The mdspan-equivalent index type of T if T is a linalg object, or a substitution error if not.
Definition math_traits.hpp:76
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
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29