Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
row_column_view.hpp
Go to the documentation of this file.
1// parasoft-suppress AUTOSAR-A2_8_1-a "False positive: also defines arene::base::linalg::column_view"
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_ROW_COLUMN_VIEW_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_ROW_COLUMN_VIEW_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 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
16#include "arene/base/stdlib_choice/cstddef.hpp"
17#include "arene/base/stdlib_choice/declval.hpp"
18#include "arene/base/stdlib_choice/enable_if.hpp"
19#include "arene/base/stdlib_choice/forward.hpp"
21// parasoft-end-suppress AUTOSAR-A16_2_2-a
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 row_column_view_detail {
31
32/// @brief Class implementing @c row_view and @c column_view by fixing one axis of an in-matrix
33/// @tparam Axis the matrix axis held fixed: @c 0 for rows, @c 1 for columns
34template <std::size_t Axis>
35class matrix_axis_view_fn {
36 public:
37 /// @brief Return a range over the slices along axis @c Axis of an in-matrix
38 /// @tparam InMatrix the input matrix type; must satisfy @c is_in_matrix_v
39 /// @tparam Args the types of any other arguments (e.g. indices) to be forwarded to @c axis_view from mdspan
40 /// @param matrix the rank-2 mdspan whose rows (@c Axis==0 ) or columns (@c Axis==1 ) are viewed
41 /// @param args other arguments (e.g. indices) to be forwarded to @c axis_view from mdspan
42 /// @return the range yielded by <c>arene::base::axis_view<Axis>(matrix, std::forward<Args>(args)...)</c>
43 template <
44 class InMatrix,
45 class... Args,
48 std::enable_if_t<is_invocable_v<decltype(axis_view<Axis>), InMatrix, Args...>>> = nullptr>
49 constexpr auto operator()(InMatrix const matrix, Args&&... args) const
50 noexcept(noexcept(arene::base::axis_view<Axis>(matrix, std::forward<Args>(args)...)))
51 -> decltype(arene::base::axis_view<Axis>(matrix, std::forward<Args>(args)...)) {
52 return arene::base::axis_view<Axis>(matrix, std::forward<Args>(args)...);
53 }
54};
55
56} // namespace row_column_view_detail
57
58// parasoft-begin-suppress AUTOSAR-A2_7_2-a "False positive: this is documentation, not commented-out code"
59// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
60// object used in multiple TUs."
61// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
62// object used in multiple TUs."
63/// @brief Return a range over the rows of a matrix
64///
65/// Given a matrix, @c row_view returns a range whose elements are the rows of that matrix, in order. The range has one
66/// element per row, totaling @c matrix.extent(0) elements, and its @c i-th element is a rank-1 view of the @c i-th
67/// row, holding the @c matrix.extent(1) elements of that row. The result is a range, so it may be used in a
68/// range-based for loop and with functions that operate on ranges.
69///
70/// The rows are views rather than copies, so each one refers to the elements of @c matrix. Where the element
71/// type is non-const, writing through a row writes through to the matrix.
72///
73/// ~~~{.cpp}
74/// auto const A = ...; // a 3-by-4 matrix
75/// for (auto const row : arene::base::linalg::row_view(A)) {
76/// // the loop runs 3 times, once per row of A
77/// // row is a rank-1 view of the 4 elements of that row, so row(0) is its first element
78/// }
79/// ~~~
80///
81/// The argument must be an in-matrix, that is a rank-2 mdspan whose value type is a scalar
82/// (see @c arene::base::linalg::is_in_matrix_v). The @c i -th row is exactly
83/// <tt> submdspan(matrix, i, full_extent) </tt>, so the layout of @c matrix must support slicing.
84///
85/// Can also be called with a second index parameter @c i to get a view of the @c i-th row:
86///
87/// ~~~{.cpp}
88/// auto const A = ...; // a 3-by-4 matrix
89/// auto const row = arene::base::linalg::row_view(A, std::integral_constant<int, 1>);
90/// // row is a rank-1 view of the 4 elements of row 1, so row(0) is A(1, 0)
91/// ~~~
92/// @see arene::base::linalg::column_view
93/// @see arene::base::axis_view
94ARENE_CPP14_INLINE_VARIABLE(row_column_view_detail::matrix_axis_view_fn<0U>, row_view);
95
96/// @brief Return a range over the columns of a matrix
97///
98/// Given a matrix, @c column_view returns a range whose elements are the columns of that matrix, in order. The range
99/// has one element per column, totaling @c matrix.extent(1) elements, and its @c i-th element is a rank-1 view of
100/// the @c i-th column, holding the @c matrix.extent(0) elements of that column. The result is a range, so it may
101/// be used in a range-based for loop and with functions that operate on ranges.
102///
103/// The columns are views rather than copies, so each one refers to the elements of @c matrix itself. Where the element
104/// type is non-const, writing through a column writes through to the matrix.
105///
106/// ~~~{.cpp}
107/// auto const A = ...; // a 3-by-4 matrix
108/// for (auto const column : arene::base::linalg::column_view(A)) {
109/// // the loop runs 4 times, once per column of A
110/// // column is a rank-1 view of the 3 elements of that column, so column(0) is its first element
111/// }
112/// ~~~
113///
114/// The argument must be an in-matrix, that is a rank-2 mdspan whose value type is a scalar
115/// (see @c arene::base::linalg::is_in_matrix_v). The @c i -th column is exactly
116/// <tt> submdspan(matrix, full_extent, i) </tt>, so the layout of @c matrix must support slicing.
117///
118/// Can also be called with a second index parameter @c i to get a view of the @c i-th column:
119///
120/// ~~~{.cpp}
121/// auto const A = ...; // a 3-by-4 matrix
122/// auto const col = arene::base::linalg::column_view(A, std::integral_constant<int, 1>);
123/// // col is a rank-1 view of the 3 elements of column 1, so col(0) is A(0, 1)
124/// ~~~
125/// @see arene::base::linalg::row_view
126/// @see arene::base::axis_view
127ARENE_CPP14_INLINE_VARIABLE(row_column_view_detail::matrix_axis_view_fn<1U>, column_view);
128// parasoft-end-suppress CERT_CPP-DCL59-a
129// parasoft-end-suppress AUTOSAR-M7_3_3-a
130// parasoft-end-suppress AUTOSAR-A2_7_2-a
131
132namespace detail {
133
134/// @brief column vector view type obtained from a matrix
135/// @tparam InMat input matrix type (a rank-2 specialization of @c mdspan)
136template <class InMat>
137using column_vector_of_t = decltype(column_view(std::declval<InMat>(), 0));
138
139/// @brief row vector view type obtained from a matrix
140/// @tparam InMat input matrix type (a rank-2 specialization of @c mdspan)
141template <class InMat>
142using row_vector_of_t = decltype(row_view(std::declval<InMat>(), 0));
143
144} // namespace detail
145
146} // namespace linalg
147} // namespace base
148} // namespace arene
149
150// parasoft-end-suppress AUTOSAR-A7_1_5-a
151// parasoft-end-suppress AUTOSAR-M2_10_1-a-2
152
153#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_ROW_COLUMN_VIEW_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_invocable_v
Determine if a functor type is invocable with the specified argument types.
constexpr auto axis_view
Create a range of slices along an mdspan axis.
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 forward(remove_reference_t< T > &value) noexcept -> T &&
Cast a value to an rvalue if T is an rvalue or rvalue reference, otherwise forward an lvalue referenc...
Definition forward.hpp:24