OpenKalman
to_diagonal.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) 2022-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 
16 #ifndef OPENKALMAN_TO_DIAGONAL_HPP
17 #define OPENKALMAN_TO_DIAGONAL_HPP
18 
19 #include "patterns/patterns.hpp"
27 #include "linear-algebra/functions/internal/make_wrapped_mdspan.hpp"
29 
30 namespace OpenKalman
31 {
37 #ifdef __cpp_concepts
38  template<indexible Arg, patterns::pattern_collection P> requires
39  pattern_collection_for<decltype(patterns::views::diagonal_of(patterns::to_extents(std::declval<P>()))), Arg>
40  constexpr diagonal_matrix decltype(auto)
41 #else
42  template<typename Arg, typename P, std::enable_if_t<
43  pattern_collection_for<decltype(patterns::views::diagonal_of(patterns::to_extents(std::declval<P>()))), Arg>, int> = 0>
44  constexpr decltype(auto)
45 #endif
46  to_diagonal(Arg&& arg, P&& p)
47  {
48  using extents_type = decltype(patterns::to_extents(p));
49  if constexpr (one_dimensional<Arg> and
51  {
52  return attach_patterns(std::forward<Arg>(arg), std::forward<P>(p));
53  }
54  else if constexpr (zero<Arg>)
55  {
56  return make_zero<values::value_type_of_t<element_type_of_t<Arg>>>(std::forward<P>(p));
57  }
58  else
59  {
60  decltype(auto) n = get_mdspan(arg);
61  using N = std::decay_t<decltype(n)>;
62  using nested_extents_type = typename N::extents_type;
63  using nested_layout = typename N::layout_type;
64  using nested_accessor = typename N::accessor_type;
65  auto nested_m = n.mapping();
66 
68  using mapping_type = typename layout_type::template mapping<extents_type>;
69  std::size_t off_diag_index = nested_m.required_span_size();
70  auto m = mapping_type {nested_m, patterns::to_extents(p)};
71 
73  using data_handle_type = typename accessor_type::data_handle_type;
74  auto a = accessor_type {n.accessor()};
75 
76  return internal::make_wrapped_mdspan(
77  std::forward<Arg>(arg),
78  [off_diag_index](auto&& h) { return data_handle_type {std::forward<decltype(h)>(h), off_diag_index}; },
79  std::move(m),
80  std::move(a),
81  std::forward<P>(p));
82  }
83  }
84 
85 
92 #ifdef __cpp_concepts
93  template<indexible Arg>
94  constexpr diagonal_matrix decltype(auto)
95 #else
96  template<typename Arg, std::enable_if_t<indexible<Arg>, int> = 0>
97  constexpr decltype(auto)
98 #endif
99  to_diagonal(Arg&& arg)
100  {
101  if constexpr (compares_with_pattern_collection<Arg, Dimensions<1>>)
102  {
103  return std::forward<Arg>(arg);
104  }
105  else if constexpr (interface::to_diagonal_defined_for<Arg&&>)
106  {
108  }
109  else
110  {
111  return to_diagonal(
112  std::forward<Arg>(arg),
114  }
115  }
116 
117 
118 }
119 
120 #endif
constexpr bool diagonal_matrix
Specifies that a type is a diagonal matrix or tensor.
Definition: diagonal_matrix.hpp:32
Definitions for attach_patterns.
decltype(auto) constexpr attach_patterns(Arg &&arg, P &&p)
Attach a pattern_collection to an indexible object.
Definition: attach_patterns.hpp:74
Definition: to_diagonal_mdspan_policies.hpp:29
decltype(auto) constexpr get_pattern_collection(T &&t)
Get the patterns::pattern_collection associated with indexible object T.
Definition: get_pattern_collection.hpp:36
constexpr bool collection_compares_with
Compares two pattern_collection objects.
Definition: collection_compares_with.hpp:132
Definition for diagonal_matrix.
constexpr detail::to_diagonal_adapter to_diagonal
A RangeAdapterObject that converts one pattern_collection to another that is equivalent to duplicatin...
Definition: to_diagonal.hpp:81
constexpr bool compares_with_pattern_collection
Compares the associated pattern collection of indexible T with pattern_collection D...
Definition: compares_with_pattern_collection.hpp:50
The root namespace for OpenKalman.
Definition: basics.hpp:34
An interface to various routines from the linear algebra library associated with indexible object T...
Definition: library_interface.hpp:42
Definition of get_mdspan function.
Definition for pattern_collection_for.
Definitions for make_zero.
Definition: to_diagonal_mdspan_policies.hpp:177
Definition for indexible.
Definition for compares_with_pattern_collection.
decltype(auto) constexpr to_diagonal(Arg &&arg, P &&p)
Convert a column vector (or any other array with a 1D second index) into a diagonal_matrix.
Definition: to_diagonal.hpp:46
constexpr auto to_extents(P &&p)
Convert a pattern_collection to std::extents.
Definition: to_extents.hpp:78
Definition: extents.hpp:372
decltype(auto) constexpr get_mdspan(T &&t)
Get the mdspan associated with indexible object T.
Definition: get_mdspan.hpp:35