OpenKalman
make_constant.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-2025 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_MAKE_CONSTANT_HPP
17 #define OPENKALMAN_MAKE_CONSTANT_HPP
18 
21 #include "linear-algebra/adapters/pattern_adapter.hpp"
23 
24 namespace OpenKalman
25 {
32 #ifdef __cpp_concepts
33  template<values::value C, typename IndexType, std::size_t...Extents>
34  constexpr constant_object auto
35 #else
36  template<typename C, typename IndexType, std::size_t...Extents, std::enable_if_t<values::value<C>, int> = 0>
37  constexpr auto
38 #endif
40  {
41  using ElementType = C;
42  using E = stdex::extents<IndexType, Extents...>;
43  auto mapping = typename internal::layout_constant::mapping<E> {extents};
44  auto accessor = internal::accessor_constant<ElementType> {std::move(c)};
45  return stdex::mdspan {accessor.data_handle(), mapping, accessor};
46  }
47 
48 
49  namespace detail
50  {
51  template<std::size_t N, std::size_t i = 0, std::size_t...SDs, typename P, typename...Ds>
52  constexpr auto
53  derive_extents(const P& p, Ds...ds)
54  {
55  if constexpr (i < N)
56  {
57  auto d = coordinates::get_dimension(collections::get<i>(p));
58  if constexpr (values::fixed<decltype(d)>)
59  return derive_extents<N, i + 1, SDs..., values::fixed_value_of_v<decltype(d)>>(p, std::move(ds)...);
60  else
61  return derive_extents<N, i + 1, SDs..., stdex::dynamic_extent>(p, std::move(ds)..., std::move(d));
62  }
63  else return stdex::extents<std::size_t, SDs...>{std::move(ds)...};
64  }
65  }
66 
67 
72 #ifdef __cpp_concepts
73  template<values::value C, coordinates::pattern_collection P> requires values::fixed<collections::size_of<P>>
74  constexpr constant_object auto
75 #else
76  template<typename C, typename P, std::enable_if_t<
77  values::value<C> and
78  coordinates::pattern_collection<P> and
79  values::fixed<collections::size_of<P>>, int> = 0>
80  constexpr auto
81 #endif
82  make_constant(C c, P&& p)
83  {
84  return attach_pattern(
85  make_constant(std::move(c), detail::derive_extents<collections::size_of_v<P>>(p)),
86  std::forward<P>(p));
87  }
88 
89 
94 #ifdef __cpp_concepts
95  template<values::value C, coordinates::pattern...Ps>
96  constexpr constant_object auto
97 #else
98  template<typename C, typename...Ps, std::enable_if_t<values::value<C> and (... and coordinates::pattern<Ps>), int> = 0>
99  constexpr auto
100 #endif
101  make_constant(C c, Ps&&...ps)
102  {
103  return make_constant(std::move(c), std::tuple{std::forward<Ps>(ps)...});
104  }
105 
106 
111 #ifdef __cpp_concepts
112  template<coordinates::pattern_collection P, values::value C> requires
113  std::default_initializable<P> and
114  values::fixed<collections::size_of<P>>
115  constexpr constant_object auto
116 #else
117  template<typename C, typename P, std::enable_if_t<
118  coordinates::pattern_collection<P> and
119  values::value<C> and
120  values::fixed<collections::size_of<P>>, int> = 0>
121  constexpr auto
122 #endif
124  {
125  return make_constant(std::move(c), P{});
126  }
127 
128 
129 }
130 
131 #endif
constexpr auto attach_pattern(Arg &&arg, P &&p)
Attach a pattern_collection to an indexible object.
Definition: attach_pattern.hpp:36
Definition for constant_object.
Definition: constant_mdspan_policies.hpp:31
constexpr bool pattern
An object describing the set of coordinates associated with a tensor index.
Definition: pattern.hpp:31
constexpr bool value
T is a fixed or dynamic value that is reducible to a number.
Definition: value.hpp:45
constexpr auto get_dimension(const Arg &arg)
Get the vector dimension of coordinates::pattern Arg.
Definition: get_dimension.hpp:54
Definition: mdspan.hpp:34
The root namespace for OpenKalman.
Definition: basics.hpp:34
Definition: constant_mdspan_policies.hpp:91
Definitions for attach_pattern.
constexpr bool constant_object
Specifies that all elements of an object are known at compile time to be the same constant value...
Definition: constant_object.hpp:54
constexpr auto make_constant(C c, stdex::extents< IndexType, Extents... > extents)
Make an indexible object in which every element is a constant value.
Definition: make_constant.hpp:39
constexpr bool fixed
T is a value that is determinable at compile time.
Definition: fixed.hpp:66
Definition: extents.hpp:372