OpenKalman
VectorSpaceAdapter.hpp
1 /* This file is part of OpenKalman, a header-only C++ library for
2  * Kalman filters and other recursive filters.
3  *
4  * Copyright (c) 2024 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 
11 #ifndef OPENKALMAN_VECTORSPACEADAPTER_HPP
12 #define OPENKALMAN_VECTORSPACEADAPTER_HPP
13 
14 namespace OpenKalman
15 {
16 #ifdef __cpp_concepts
17  template<indexible NestedObject, pattern_collection Descriptors> requires
18  internal::not_more_fixed_than<NestedObject, Descriptors> and (not internal::less_fixed_than<NestedObject, Descriptors>) and
19  internal::maybe_same_shape_as_vector_space_descriptors<NestedObject, Descriptors>
20 #else
21  template<typename NestedObject, typename Descriptors>
22 #endif
23  struct VectorSpaceAdapter : internal::AdapterBase<VectorSpaceAdapter<NestedObject, Descriptors>, NestedObject>
24  {
25  private:
26 
27 #ifndef __cpp_concepts
28  static_assert(indexible<NestedObject>);
29  static_assert(pattern_collection<Descriptors>);
30  static_assert(internal::not_more_fixed_than<NestedObject, Descriptors>);
31  static_assert(not internal::less_fixed_than<NestedObject, Descriptors>);
32  static_assert(internal::maybe_same_shape_as_vector_space_descriptors<NestedObject, Descriptors>);
33 #endif
34 
35  using Base = internal::AdapterBase<VectorSpaceAdapter, NestedObject>;
36 
37  public:
38 
42 #ifdef __cpp_concepts
43  constexpr VectorSpaceAdapter() requires std::default_initializable<Base> and
44  fixed_pattern_tuple<Descriptors>
45 #else
47  and fixed_pattern_tuple<Descriptors>, int> = 0>
48  constexpr VectorSpaceAdapter()
49 #endif
50  : Base {}, my_descriptors{} {}
51 
52 
58 #ifdef __cpp_concepts
59  template<internal::maybe_same_shape_as_vector_space_descriptors<Descriptors> Arg> requires
60  (not internal::vector_space_adapter<Arg>) and std::constructible_from<Base, Arg&&>
61 #else
62  template<typename Arg, typename...Ds, std::enable_if_t<
63  internal::maybe_same_shape_as_vector_space_descriptors<Arg, Descriptors> and
64  (not internal::vector_space_adapter<Arg>) and std::is_constructible_v<Base, Arg&&>, int> = 0>
65 #endif
66  constexpr VectorSpaceAdapter(Arg&& arg, const std::decay_t<Descriptors>& descriptors)
67  : Base {std::forward<Arg>(arg)}, my_descriptors {descriptors} {}
68 
69 
74 #ifdef __cpp_concepts
75  template<internal::maybe_same_shape_as_vector_space_descriptors<Descriptors> Arg> requires
76  internal::vector_space_adapter<Arg> and std::constructible_from<Base, nested_object_of_t<Arg&&>>
77 #else
78  template<typename Arg, std::enable_if_t<
79  internal::maybe_same_shape_as_vector_space_descriptors<Arg, Descriptors> and
80  internal::maybe_same_shape_as_vector_space_descriptors<Arg, Descriptors> and
81  internal::vector_space_adapter<Arg> and std::is_constructible_v<Base, typename nested_object_of<Arg&&>::type>, int> = 0>
82 #endif
83  constexpr VectorSpaceAdapter(Arg&& arg, const std::decay_t<Descriptors>& descriptors)
84  : Base {nested_object(std::forward<Arg>(arg))}, my_descriptors {descriptors} {}
85 
86 
90 #ifdef __cpp_concepts
91  template<internal::maybe_same_shape_as_vector_space_descriptors<Descriptors> Arg> requires
92  (not std::is_base_of_v<VectorSpaceAdapter, std::decay_t<Arg>>) and
93  std::assignable_from<std::add_lvalue_reference_t<NestedObject>, Arg&&> and
94  requires(Arg&& arg) { {count_indices(arg)} -> values::fixed; } and
95  requires(Descriptors my_descriptors, Arg&& arg) { my_descriptors = all_vector_space_descriptors(arg); }
96 #else
97  template<typename Arg, std::enable_if_t<
98  (not std::is_base_of_v<VectorSpaceAdapter, std::decay_t<Arg>>) and
99  internal::maybe_same_shape_as_vector_space_descriptors<Arg, Descriptors> and
100  std::is_assignable_v<std::add_lvalue_reference_t<NestedObject>, Arg&&> and
101  values::fixed<decltype(count_indices(std::declval<Arg&&>()))> and
102  std::is_assignable_v<Descriptors, decltype(all_vector_space_descriptors(std::declval<Arg&&>()))>, int> = 0>
103 #endif
104  constexpr VectorSpaceAdapter& operator=(Arg&& arg)
105  {
106  my_descriptors = all_vector_space_descriptors(arg);
107  Base::operator=(std::forward<Arg>(arg));
108  return *this;
109  }
110 
111 
113 #ifdef __cpp_concepts
114  auto& operator+=(const VectorSpaceAdapter& other) requires
115  requires(Base& base, const VectorSpaceAdapter& v) { base += v.nested_object(); }
116 #else
117  template<typename B = Base, typename = std::void_t<
118  decltype(std::declval<Base&>() += std::declval<const VectorSpaceAdapter&>().nested_object())>>
119  auto& operator+=(const VectorSpaceAdapter& other)
120 #endif
121  {
122  Base::operator+=(other.nested_object());
123  return *this;
124  }
125 
126 
128 #ifdef __cpp_concepts
129  auto& operator-=(const VectorSpaceAdapter& other) requires
130  requires(Base& base, const VectorSpaceAdapter& v) { base -= v.nested_object(); }
131 #else
132  template<typename B = Base, typename = std::void_t<
133  decltype(std::declval<Base&>() -= std::declval<const VectorSpaceAdapter&>().nested_object())>>
134  auto& operator-=(const VectorSpaceAdapter& other)
135 #endif
136  {
137  Base::operator-=(other.nested_object());
138  return *this;
139  }
140 
141 
142  protected:
143 
144  std::decay_t<Descriptors> my_descriptors;
145 
146  friend struct interface::indexible_object_traits<VectorSpaceAdapter>;
147  friend struct interface::library_interface<VectorSpaceAdapter>;
148 
149  }; // struct VectorSpaceAdapter
150 
151 
152  // ------------------------------- //
153  // Deduction Guides //
154  // ------------------------------- //
155 
156 #ifdef __cpp_concepts
157  template<indexible Arg, pattern_collection Descriptors> requires (not internal::vector_space_adapter<Arg>)
158 #else
159  template<typename Arg, typename...Vs, std::enable_if_t<indexible<Arg> and pattern_collection<Descriptors> and
160  (not internal::vector_space_adapter<Arg>), int> = 0>
161 #endif
162  VectorSpaceAdapter(Arg&&, Descriptors&&)
164 
165 
166 #ifdef __cpp_concepts
167  template<internal::vector_space_adapter Arg, pattern_collection Descriptors>
168 #else
169  template<typename Arg, typename...Vs, std::enable_if_t<
170  internal::vector_space_adapter<Arg> and pattern_collection<Descriptors>, int> = 0>
171 #endif
172  VectorSpaceAdapter(Arg&&, Descriptors&&)
174 
175 
176 } // namespace OpenKalman
177 
178 
179 #endif //OPENKALMAN_VECTORSPACEADAPTER_HPP
constexpr auto count_indices(const T &t)
Get the number of indices available to address the components of an indexible object.
Definition: count_indices.hpp:33
constexpr NestedObject & nested_object() &
Get the nested object.
Definition: AdapterBase.hpp:97
An adapter that adds vector space descriptors for each index.
Definition: forward-class-declarations.hpp:301
Definition: indexible_object_traits.hpp:36
constexpr AdapterBase & operator=(Arg &&arg)
Assign from another compatible indexible object.
Definition: AdapterBase.hpp:82
Definition: tuple_reverse.hpp:103
constexpr bool value
T is numerical value or is reducible to a numerical value.
Definition: value.hpp:31
Definition: AdapterBase.hpp:36
decltype(auto) constexpr all_vector_space_descriptors(const T &t)
Return a collection of coordinates::pattern objects associated with T.
Definition: all_vector_space_descriptors.hpp:52
auto & operator+=(const VectorSpaceAdapter &other)
Increment from another VectorSpaceAdapter.
Definition: VectorSpaceAdapter.hpp:119
constexpr VectorSpaceAdapter(Arg &&arg, const std::decay_t< Descriptors > &descriptors)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: VectorSpaceAdapter.hpp:83
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:37
constexpr VectorSpaceAdapter(Arg &&arg, const std::decay_t< Descriptors > &descriptors)
Construct from a compatible indexible object.
Definition: VectorSpaceAdapter.hpp:66
auto & operator-=(const VectorSpaceAdapter &other)
Decrement from another VectorSpaceAdapter.
Definition: VectorSpaceAdapter.hpp:134
constexpr bool fixed
T is a values::value that is determinable at compile time.
Definition: fixed.hpp:60
constexpr VectorSpaceAdapter()
Default constructor.
Definition: VectorSpaceAdapter.hpp:48