OpenKalman
get_stat_dimension.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) 2020-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_GET_EUCLIDEAN_DIMENSION_HPP
17 #define OPENKALMAN_GET_EUCLIDEAN_DIMENSION_HPP
18 
19 #include <functional>
20 #include "basics/basics.hpp"
25 
26 namespace OpenKalman::patterns
27 {
28  namespace detail
29  {
30 #ifndef __cpp_lib_ranges
31  template<typename T, typename = void>
32  struct range_value_has_fixed_stat_dimension : std::false_type {};
33 
34  template<typename T>
35  struct range_value_has_fixed_stat_dimension<T, std::enable_if_t<stdex::ranges::range<T>>>
36  : std::bool_constant<values::fixed_value_compares_with<decltype(internal::get_descriptor_stat_dimension(
37  std::declval<stdex::ranges::range_value_t<T>>())), 0>> {};
38 #endif
39 
40 
41  template<std::size_t i = 0, typename T>
42  static constexpr auto get_stat_dimension_fixed(const T& t)
43  {
44  if constexpr (i < collections::size_of_v<T>)
45  {
46  return values::operation(
47  std::plus{},
48  internal::get_descriptor_stat_dimension(collections::get<i>(t)),
49  get_stat_dimension_fixed<i + 1>(t));
50  }
51  else return std::integral_constant<std::size_t, 0_uz>{};
52  }
53  }
54 
55 
60 #ifdef __cpp_concepts
61  template<sized_pattern Arg> requires descriptor<Arg> or collections::sized<Arg> or
62  values::fixed_value_compares_with<decltype(internal::get_descriptor_stat_dimension(std::declval<stdex::ranges::range_value_t<Arg>>())), 0>
63  constexpr values::index auto
64 #else
65  template<typename Arg, std::enable_if_t<descriptor<Arg> or
66  (descriptor_collection<Arg> and (collections::sized<Arg> or detail::range_value_has_fixed_stat_dimension<Arg>::value)), int> = 0>
67  constexpr auto
68 #endif
69  get_stat_dimension(const Arg& arg)
70  {
71  if constexpr (descriptor<Arg>)
72  {
73  return internal::get_descriptor_stat_dimension(arg);
74  }
76  {
77  return std::integral_constant<std::size_t, 0_uz>{};
78  }
79  else if constexpr (values::fixed_value_compares_with<collections::size_of<Arg>, stdex::dynamic_extent, &stdex::is_neq>)
80  {
81  return detail::get_stat_dimension_fixed(arg);
82  }
83  else // dynamic size
84  {
85  using C = decltype(internal::get_descriptor_stat_dimension(std::declval<stdex::ranges::range_value_t<Arg>>()));
86  if constexpr (values::fixed_value_compares_with<C, 0_uz>)
87  return std::integral_constant<std::size_t, 0_uz>{};
88  else if constexpr (values::fixed<C>)
89  return values::fixed_value_of_v<C> * collections::get_size(arg);
90  else
91 #ifdef __cpp_lib_ranges_fold
92  return std::ranges::fold_left(collections::views::all(arg), 0_uz,
93  [](const auto& a, const auto& b) { return a + internal::get_descriptor_stat_dimension(b); });
94 #else
95  {
96  std::size_t ret = 0_uz;
97  for (const auto& c : collections::views::all(arg)) ret += internal::get_descriptor_stat_dimension(c);
98  return ret;
99  }
100 #endif
101  }
102  }
103 
104 
105 }
106 
107 #endif
constexpr auto get_size(Arg &&arg)
Get the size of a sized object (e.g, a collection)
Definition: get_size.hpp:224
Definition for patterns::sized_pattern.
The size of a sized object (including a collection).
Definition: size_of.hpp:33
The namespace for features relating to patterns::pattern object.
Definition: collection_compares_with.hpp:24
Definition for patterns::descriptor.
constexpr detail::all_closure all
a std::ranges::range_adaptor_closure which returns a view to all members of its collection argument...
Definition: all.hpp:70
constexpr bool fixed_value_compares_with
T has a fixed value that compares with N in a particular way based on parameter comp.
Definition: fixed_value_compares_with.hpp:74
constexpr bool index
T is an index value.
Definition: index.hpp:62
Basic definitions for OpenKalman as a whole.
Definition for collections::uniformly_gettable.
constexpr auto get_stat_dimension(const Arg &arg)
Get the vector dimension of patterns::pattern Arg when transformed into statistical space...
Definition: get_stat_dimension.hpp:69
constexpr auto operation(Operation &&op, Args &&...args)
A potentially constant-evaluated operation involving some number of values.
Definition: operation.hpp:98