OpenKalman
CovarianceBase1.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) 2020-2021 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_COVARIANCEBASE1_HPP
18 #define OPENKALMAN_COVARIANCEBASE1_HPP
19 
20 namespace OpenKalman::internal
21 {
22 
29 #ifdef __cpp_concepts
30  template<typename Derived, typename NestedMatrix>
31 #else
32  template<typename Derived, typename NestedMatrix, typename = void>
33 #endif
35 
36 
41  template<typename Cov, typename Nested = nested_object_of_t<Cov>>
42 #ifdef __cpp_concepts
43  concept case1or2 = (self_adjoint_covariance<Cov> and hermitian_matrix<Nested>) or
44  (triangular_covariance<Cov> and triangular_matrix<Nested>);
45 #else
46  static constexpr bool case1or2 = (self_adjoint_covariance<Cov> and hermitian_matrix<Nested>) or
47  (triangular_covariance<Cov> and triangular_matrix<Nested>);
48 #endif
49 
50 
51  // =====================================CASE 1=======================================
61 #ifdef __cpp_concepts
62  template<typename Derived, typename NestedMatrix> requires
63  case1or2<Derived, NestedMatrix> and self_contained<NestedMatrix>
64  struct CovarianceBase<Derived, NestedMatrix>
65 #else
66  template<typename Derived, typename NestedMatrix>
67  struct CovarianceBase<Derived, NestedMatrix, std::enable_if_t<
68  case1or2<Derived, NestedMatrix> and self_contained<NestedMatrix>>>
69 #endif
70  : AdapterBase<Derived, NestedMatrix>
71  {
72  private:
73 
75 
76  using Scalar = scalar_type_of_t<NestedMatrix>;
77 
78 
79 #ifdef __cpp_concepts
80  template<typename, typename>
81 #else
82  template<typename, typename, typename>
83 #endif
84  friend struct CovarianceBase;
85 
86  protected:
87 
88  using CholeskyNestedMatrix = std::conditional_t<diagonal_matrix<NestedMatrix>,
89  typename MatrixTraits<std::decay_t<NestedMatrix>>::template DiagonalMatrixFrom<>,
90  std::conditional_t<triangular_matrix<NestedMatrix>,
91  typename MatrixTraits<std::decay_t<NestedMatrix>>::template SelfAdjointMatrixFrom<>,
92  typename MatrixTraits<std::decay_t<NestedMatrix>>::template TriangularAdapterFrom<>>>;
93 
94 
100  decltype(auto) cholesky_nested_matrix() &
101  {
102  if constexpr (triangular_covariance<Derived>) return cholesky_square(this->nested_object());
103  else return cholesky_factor(this->nested_object());
104  }
105 
106 
108  decltype(auto) cholesky_nested_matrix() const &
109  {
110  if constexpr (triangular_covariance<Derived>) return cholesky_square(this->nested_object());
111  else return cholesky_factor(this->nested_object());
112  }
113 
114 
116  decltype(auto) cholesky_nested_matrix() &&
117  {
118  if constexpr (triangular_covariance<Derived>) return cholesky_square(std::move(*this).nested_object());
119  else return cholesky_factor(std::move(*this).nested_object());
120  }
121 
122 
124  decltype(auto) cholesky_nested_matrix() const &&
125  {
126  if constexpr (triangular_covariance<Derived>) return cholesky_square(std::move(*this).nested_object());
127  else return cholesky_factor(std::move(*this).nested_object());
128  }
129 
130 
135  constexpr static int synchronization_direction() { return 0; }
136 
137 
143  constexpr static void synchronize_forward() {};
144 
145 
151  constexpr static void synchronize_reverse() {};
152 
153 
159  constexpr static void mark_nested_matrix_changed() {};
160 
161 
167  constexpr static void mark_cholesky_nested_matrix_changed() {};
168 
169 
175  constexpr static void mark_synchronized() {};
176 
177 
178  public:
180 #ifdef __cpp_concepts
181  CovarianceBase() requires std::default_initializable<NestedMatrix>
182 #else
183  template<typename T = NestedMatrix, std::enable_if_t<std::is_default_constructible_v<T>, int> = 0>
185 #endif
186  : Base {} {}
187 
188 
192 #ifdef __cpp_concepts
193  template<covariance Arg> requires (not std::derived_from<std::decay_t<Arg>, CovarianceBase>)
194 #else
195  template<typename Arg, std::enable_if_t<covariance<Arg> and
196  (not std::is_base_of_v<CovarianceBase, std::decay_t<Arg>>), int> = 0>
197 #endif
198  CovarianceBase(Arg&& arg) : Base {to_covariance_nestable<NestedMatrix>(std::forward<Arg>(arg))} {}
199 
200 
204 #ifdef __cpp_concepts
205  template<covariance_nestable Arg>
206 #else
207  template<typename Arg, std::enable_if_t<covariance_nestable<Arg>, int> = 0>
208 #endif
209  explicit CovarianceBase(Arg&& arg) : Base {to_covariance_nestable<NestedMatrix>(std::forward<Arg>(arg))} {}
210 
211 
216 #ifdef __cpp_concepts
217  template<typename Arg> requires (covariance<Arg> or covariance_nestable<Arg>) and
218  (not std::derived_from<std::decay_t<Arg>, CovarianceBase>)
219 #else
220  template<typename Arg, std::enable_if_t<(covariance<Arg> or covariance_nestable<Arg>) and
221  (not std::is_base_of_v<CovarianceBase, std::decay_t<Arg>>), int> = 0>
222 #endif
223  auto& operator=(Arg&& arg)
224  {
225  Base::operator=(to_covariance_nestable<NestedMatrix>(std::forward<Arg>(arg)));
226  return *this;
227  }
228 
229 
236  auto operator() (std::size_t i, std::size_t j)
237  {
238  return ElementAccessor(Base::nested_object(), i, j);
239  }
240 
241 
243  auto operator() (std::size_t i, std::size_t j) const
244  {
245  return ElementAccessor(Base::nested_object(), i, j);
246  }
247 
248 
254  auto operator[] (std::size_t i)
255  {
257  }
258 
259 
261  auto operator[] (std::size_t i) const
262  {
264  }
265 
266 
270  void set_component(const Scalar s, const std::size_t i, const std::size_t j)
271  {
273  }
274 
275 
279  void set_component(const Scalar s, const std::size_t i)
280  {
282  }
283 
284  };
285 
286 
287 }
288 
289 #endif //OPENKALMAN_COVARIANCEBASE1_HPP
Definition: ElementAccessor.hpp:34
Arg && set_component(Arg &&arg, const scalar_type_of_t< Arg > &s, const Indices &indices)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: set_component.hpp:51
typename scalar_type_of< T >::type scalar_type_of_t
helper template for scalar_type_of.
Definition: scalar_type_of.hpp:54
Definition: tuple_reverse.hpp:103
Definition: AdapterBase.hpp:36
decltype(auto) constexpr cholesky_square(A &&a)
Take the Cholesky square of a triangular_matrix.
Definition: cholesky_square.hpp:33
Definition: CovarianceBase1.hpp:34
void set_component(const Scalar s, const std::size_t i)
Set an element of the cholesky nested matrix.
Definition: CovarianceBase1.hpp:279
decltype(auto) constexpr nested_object(Arg &&arg)
Retrieve a nested object of Arg, if it exists.
Definition: nested_object.hpp:34
void set_component(const Scalar s, const std::size_t i, const std::size_t j)
Set an element of the cholesky nested matrix.
Definition: CovarianceBase1.hpp:270
Definition: basics.hpp:48
decltype(auto) constexpr cholesky_factor(A &&a)
Take the Cholesky factor of a matrix.
Definition: cholesky_factor.hpp:38