OpenKalman
constant_diagonal_coefficient.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) 2019-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 
16 #ifndef OPENKALMAN_CONSTANT_DIAGONAL_COEFFICIENT_HPP
17 #define OPENKALMAN_CONSTANT_DIAGONAL_COEFFICIENT_HPP
18 
19 #include <type_traits>
20 
21 namespace OpenKalman::values
22 {
27 #ifdef __cpp_concepts
28  template<indexible T>
29 #else
30  template<typename T, typename = void>
31 #endif
33  {
34  explicit constexpr constant_diagonal_coefficient(const std::decay_t<T>&) {};
35  };
36 
37 
41  template<typename T>
43 
44 
46 #ifdef __cpp_concepts
47  template<indexible T>
48 #else
49  template<typename T>
50 #endif
52 
53 
54  namespace detail
55  {
56  template<typename T>
57  constexpr auto const_value =
58  std::decay_t<decltype(interface::indexible_object_traits<std::decay_t<T>>::get_constant(std::declval<T>()))>::value;
59 
60 
61 #ifdef __cpp_concepts
62  template<typename T>
63  concept has_static_constant_diagonal =
64  values::fixed<typename interface::get_constant_diagonal_return_type<T>::type> or
65  (values::fixed<typename interface::get_constant_return_type<T>::type> and
66  (one_dimensional<T> or values::internal::near(const_value<T>, 0)));
67 #else
68  template<typename T, typename = void>
69  struct has_static_constant_diagonal_impl : std::false_type {};
70 
71  template<typename T>
72  struct has_static_constant_diagonal_impl<T, std::enable_if_t<values::fixed<typename interface::get_constant_return_type<T>::type>>>
73  : std::bool_constant<one_dimensional<T> or values::internal::near(const_value<T>, 0)> {};
74 
75 
76  template<typename T>
77  constexpr bool has_static_constant_diagonal =
78  values::fixed<typename interface::get_constant_diagonal_return_type<T>::type> or
80 #endif
81  } // namespace detail
82 
83 
88 #ifdef __cpp_concepts
89  template<indexible T> requires detail::has_static_constant_diagonal<T>
91 #else
92  template<typename T>
93  struct constant_diagonal_coefficient<T, std::enable_if_t<indexible<T> and detail::has_static_constant_diagonal<T>>>
94 #endif
95  {
96  private:
97 
99 
100  public:
101 
102  constexpr constant_diagonal_coefficient() = default;
103 
104  explicit constexpr constant_diagonal_coefficient(const std::decay_t<T>&) {};
105 
106  using value_type = scalar_type_of_t<T>;
107 
109 
110  static constexpr value_type value = []{
111  if constexpr (values::scalar<typename interface::get_constant_diagonal_return_type<T>::type>)
112  return std::decay_t<decltype(Trait::get_constant_diagonal(std::declval<T>()))>::value;
113  else
114  return std::decay_t<decltype(Trait::get_constant(std::declval<T>()))>::value;
115  }();
116 
117  constexpr operator value_type() const { return value; }
118 
119  constexpr value_type operator()() const { return value; }
120  };
121 
122 
127 #ifdef __cpp_concepts
128  template<indexible T> requires (not detail::has_static_constant_diagonal<T>) and
129  (values::dynamic<typename interface::get_constant_diagonal_return_type<T>::type> or one_dimensional<T>)
131 #else
132  template<typename T>
133  struct constant_diagonal_coefficient<T, std::enable_if_t<indexible<T> and (not detail::has_static_constant_diagonal<T>) and
134  (values::dynamic<typename interface::get_constant_diagonal_return_type<T>::type> or one_dimensional<T>)>>
135 #endif
136  {
137  private:
138 
140 
141  public:
142 
143  explicit constexpr constant_diagonal_coefficient(const std::decay_t<T>& t) : m_value {[](const auto& t){
144  if constexpr (values::scalar<typename interface::get_constant_diagonal_return_type<T>::type>)
145  return values::to_number(Trait::get_constant_diagonal(t));
146  else if constexpr (values::scalar<typename interface::get_constant_return_type<T>::type>)
147  return values::to_number(Trait::get_constant(t));
148  else
149  return internal::get_singular_component(t);
150  }(t)} {};
151 
152  using value_type = scalar_type_of_t<T>;
153 
154  using type = constant_diagonal_coefficient;
155 
156  constexpr operator value_type() const { return m_value; }
157 
158  constexpr value_type operator()() const { return m_value; }
159 
160  private:
161 
162  value_type m_value;
163  };
164 
165 
166 } // namespace OpenKalman::values
167 
168 
169 namespace OpenKalman
170 {
173 }
174 
175 
176 #endif //OPENKALMAN_CONSTANT_DIAGONAL_COEFFICIENT_HPP
Definition: indexible_object_traits.hpp:36
typename scalar_type_of< T >::type scalar_type_of_t
helper template for scalar_type_of.
Definition: scalar_type_of.hpp:54
constexpr auto constant_diagonal_coefficient_v
Helper template for constant_diagonal_coefficient.
Definition: constant_diagonal_coefficient.hpp:51
Definition: tuple_reverse.hpp:103
constexpr bool value
T is numerical value or is reducible to a numerical value.
Definition: value.hpp:31
constexpr auto to_number(Arg arg)
Convert any values::value to a values::number.
Definition: to_number.hpp:34
Definition: constant_diagonal_coefficient.hpp:69
The root namespace for OpenKalman.
Definition: basics.hpp:34
The constant associated with T, assuming T is a constant_diagonal_matrix.
Definition: constant_diagonal_coefficient.hpp:32
Definition for values::abs.
Definition: constant_coefficient.hpp:25
constexpr bool dynamic
T is a values::value that is not determinable at compile time.
Definition: dynamic.hpp:48
constexpr bool near(const Arg1 &arg1, const Arg2 &arg2)
Determine whether two numbers are within a rounding tolerance.
Definition: near.hpp:36
constexpr bool fixed
T is a values::value that is determinable at compile time.
Definition: fixed.hpp:60
constant_diagonal_coefficient(T &&) -> constant_diagonal_coefficient< std::decay_t< T >>
Deduction guide for constant_diagonal_coefficient.