OpenKalman
diagonal_of.hpp
Go to the documentation of this file.
1 /* This file is part of OpenKalman, a header-only C++ library for
2  * Kalman filters and other recursive filters.
3  *
4  * Copyright (c) 2025-2026 Christopher Lee Ogden <ogden@gatech.edu>
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
9  */
10 
17 #ifndef OPENKALMAN_PATTERNS_DIAGONAL_OF_HPP
18 #define OPENKALMAN_PATTERNS_DIAGONAL_OF_HPP
19 
22 #include "patterns/descriptors/Any.hpp"
27 
29 {
30  namespace detail
31  {
37  {
38  constexpr diagonal_of_closure() = default;
39 
40  #ifdef __cpp_concepts
41  template<pattern_collection R> requires
45  &stdex::is_lteq, applicability::permitted> or
47  pattern_collection_element_t<1, R>,
48  pattern_collection_element_t<0, R>,
49  &stdex::is_lteq, applicability::permitted>
50  constexpr pattern_collection decltype(auto)
51  #else
52  template<typename R, std::enable_if_t<
53  pattern_collection<R> and
55  pattern_collection_element_t<0, R>,
56  pattern_collection_element_t<1, R>,
57  &stdex::is_lteq, applicability::permitted> or
59  pattern_collection_element_t<1, R>,
60  pattern_collection_element_t<0, R>,
61  &stdex::is_lteq, applicability::permitted>), int> = 0>
62  constexpr decltype(auto)
63  #endif
64  operator() (R&& r) const
65  {
66  using N2 = std::integral_constant<std::size_t, 2>;
67  using P0 = pattern_collection_element_t<0, R>;
68  using P1 = pattern_collection_element_t<1, R>;
69 
71  {
72  return std::forward<R>(r);
73  }
75  {
76  return operator()(collections::views::concat(std::forward<R>(r), std::array{Dimensions<1>{}}));
77  }
78  else if constexpr (compares_with<P0, P1, &stdex::is_lteq>)
79  {
81  std::array {get_pattern<0>(r)},
82  collections::views::slice(std::forward<R>(r), N2{}));
83  }
84  else if constexpr (compares_with<P0, P1, &stdex::is_gt>)
85  {
87  std::array {get_pattern<1>(r)},
88  collections::views::slice(std::forward<R>(r), N2{}));
89  }
90  else
91  {
92  auto p0 = get_pattern<0>(r);
93  auto p1 = get_pattern<1>(r);
94  auto ps = collections::views::slice(std::forward<R>(r), N2{});
95 
96  if (compare<&stdex::is_lteq>(p0, p1))
97  return collections::views::concat(std::array{Any{std::move(p0)}}, std::move(ps));
98  if (compare<&stdex::is_gt>(p0, p1))
99  return collections::views::concat(std::array{Any{std::move(p1)}}, std::move(ps));
100  throw (std::logic_error("Patterns for the first two ranks are not compatible for taking diagonal"));
101  }
102  }
103  };
104 
105 
107  {
108  constexpr auto
109  operator() () const
110  {
111  return diagonal_of_closure {};
112  }
113 
114 
115  #ifdef __cpp_concepts
116  template<pattern_collection R> requires
120  &stdex::is_lteq, applicability::permitted> or
122  pattern_collection_element_t<1, R>,
123  pattern_collection_element_t<0, R>,
124  &stdex::is_lteq, applicability::permitted>
125  constexpr pattern_collection decltype(auto)
126  #else
127  template<typename R, std::enable_if_t<
128  pattern_collection<R> and
129  (compares_with<
130  pattern_collection_element_t<0, R>,
131  pattern_collection_element_t<1, R>,
132  &stdex::is_lteq, applicability::permitted> or
134  pattern_collection_element_t<1, R>,
135  pattern_collection_element_t<0, R>,
136  &stdex::is_lteq, applicability::permitted>), int> = 0>
137  constexpr decltype(auto)
138  #endif
139  operator() (R&& r) const
140  {
141  return diagonal_of_closure{}(std::forward<R>(r));
142  }
143  };
144 
145  }
146 
147 
154 
155 }
156 
157 #endif
Definition for pattern_collection.
constexpr bool pattern_collection
An object describing a sized collection of /ref sized_pattern objects.
Definition: pattern_collection.hpp:66
constexpr detail::concat_adaptor concat
a std::ranges::range_adaptor_closure for a set of concatenated collection objects.
Definition: concat.hpp:209
Definition for compares_with.
The concept, trait, or restraint is permitted, but whether it applies is not necessarily known at com...
constexpr bool compares_with
Compares two patterns::pattern objects.
Definition: compares_with.hpp:472
The namespace for views for patterns::pattern object.
Definition: patterns.hpp:51
Definition of the Dimensions class.
Definition for patterns::get_pattern.
The size of a sized object (including a collection).
Definition: size_of.hpp:33
A structure representing the dimensions associated with of a particular index.
Definition: Dimensions.hpp:42
typename pattern_collection_element< i, T >::type pattern_collection_element_t
Helper template for collection_element.
Definition: pattern_collection_element.hpp:58
Inclusion file for collections.
constexpr detail::slice_adapter slice
a RangeAdapterObject associated with slice_view.
Definition: slice.hpp:423
Definition: range_adaptor_closure.hpp:34
constexpr detail::diagonal_of_adapter diagonal_of
A RangeAdapterObject that converts one pattern_collection to another corresponding to the diagonal_ma...
Definition: diagonal_of.hpp:153
constexpr bool fixed_value_compares_with
T has a fixed value that compares with N in a particular way based on parameter comp.
Definition: fixed_value_compares_with.hpp:74
Definition: Any.hpp:42
Definition for collections::pattern_collection_element.