Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
identity_matrix.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_IDENTITY_MATRIX_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_IDENTITY_MATRIX_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"
15#include "arene/base/linalg/detail/mandates.hpp"
22#include "arene/base/stdlib_choice/cstddef.hpp"
23#include "arene/base/stdlib_choice/enable_if.hpp"
24#include "arene/base/stdlib_choice/forward.hpp"
26// parasoft-end-suppress AUTOSAR-A16_2_2-a
27
28// IWYU pragma: no_include "arene/base/stdlib_choice/remove_cv.hpp"
29// IWYU pragma: no_include "arene/base/stdlib_choice/remove_reference.hpp"
30
31// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
32// parasoft-begin-suppress AUTOSAR-A7_1_5-a "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
33
34namespace arene {
35namespace base {
36namespace linalg {
37
38namespace identity_matrix_detail {
39
40/// @brief Statically assert that the type parameter is an @c extents object
41/// @tparam Extents Type to assert
42/// @return @c true so that it can be used in a constraints list
43/// @note This is in a helper function so it can be called from the constraints list before entering the function body
44template <typename Extents>
45constexpr auto mandate_is_extents() noexcept -> bool {
46 static_assert(
48 "The argument to linalg::identity_matrix should be an instance of arene::base::extents"
49 );
50 return true;
51}
52
53/// @brief A compile-time constant pair of elements for an identity matrix to refer to: a one and a zero
54/// @tparam ElementType The element type to hold a one and a zero of
55template <typename ElementType>
56extern constexpr array<ElementType, 2U> identity_elements{numbers::one_v<ElementType>, numbers::zero_v<ElementType>};
57
58/// @brief Class implementing @c identity_matrix
59/// @tparam ElementType The element type to use for the returned matrix; must be a linalg scalar
60template <typename ElementType>
61class identity_matrix_impl {
62 static_assert(is_scalar_v<ElementType>, "To create a valid matrix, the ElementType must be a linalg scalar");
63
64 /// @brief Type of the @c mdspan to return when calling this @c identity_matrix function object
65 /// @tparam Extents Type of the extents given as an argument to @c identity_matrix
66 /// @note Adds @c const to the element type since the backing data is a single compile-time-constant buffer
67 template <typename Extents>
68 using identity_mdspan = mdspan<ElementType const, Extents, layout_constant_diagonal<extents<std::size_t, 0U, 0U>>>;
69
70 public:
71 /// @brief Return an @c mdspan representing an identity matrix with the same shape as @c extents
72 /// @tparam Extents Type of the extents to use for the identity matrix; constrained to be possibly square
73 /// @param extents Extents to use for the identity span
74 /// @return An identity mdspan with element type <c>ElementType const</c> and extents constructed from @c extents
75 /// @pre @c extents must actually be square (width == height), otherwise @c ARENE_PRECONDITION violation
76 template <
77 class Extents,
80 std::enable_if_t<linalg_detail::possibly_square_extents_v<remove_cvref_t<Extents>>>> = nullptr>
81 constexpr auto operator()(Extents&& extents) const noexcept -> identity_mdspan<remove_cvref_t<Extents>> {
82 // parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
83 identity_mdspan<remove_cvref_t<Extents>> const result{
84 identity_elements<ElementType>.data(),
86 };
87 // parasoft-end-suppress AUTOSAR-A7_1_3-a
88
89 ARENE_PRECONDITION(is_square(result));
90
91 return result;
92 }
93};
94
95} // namespace identity_matrix_detail
96
97/// @brief Return an @c mdspan representing an identity matrix with the same shape as the passed-in extents
98/// @tparam ElementType The element type to use for the returned matrix; must be a linalg scalar
99template <typename ElementType>
100extern constexpr identity_matrix_detail::identity_matrix_impl<ElementType> identity_matrix{};
101
102} // namespace linalg
103} // namespace base
104} // namespace arene
105
106// parasoft-end-suppress AUTOSAR-A7_1_5-a
107// parasoft-end-suppress AUTOSAR-M2_10_1-a-2
108
109#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_LINALG_IDENTITY_MATRIX_HPP_
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Definition add.hpp:20
constexpr identity_matrix_detail::identity_matrix_impl< ElementType > identity_matrix
Return an mdspan representing an identity matrix with the same shape as the passed-in extents.
constexpr bool is_scalar_v
Determine if the given type is a scalar for the purposes of linalg algorithms.
constexpr const & one_v
constexpr reference to the multiplicative identity value one<T>::value .
Definition arithmetic_identities.hpp:105
constexpr const & zero_v
constexpr reference to the additive identity value zero<T>::value .
Definition arithmetic_identities.hpp:58
Definition accumulate.hpp:37
constexpr bool is_extents_v
Helper variable to check if a provided type is an instantiation of extents.
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
remove_cv_t< remove_reference_t< T > > remove_cvref_t
Backport of std::remove_cvref_t.
Definition remove_cvref.hpp:35
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
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