OpenKalman
to_stat_space.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) 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_COLLECTIONS_TO_STAT_SPACE_HPP
17 #define OPENKALMAN_COLLECTIONS_TO_STAT_SPACE_HPP
18 
19 #include <functional>
21 #include "coordinates/interfaces/coordinate_descriptor_traits.hpp"
28 
30 {
37 #ifdef __cpp_concepts
38  template<descriptor T, collections::collection R>
39  constexpr collections::collection decltype(auto)
40 #else
41  template<typename T, typename R, std::enable_if_t<descriptor<T> and collections::collection<R>, int> = 0>
42  constexpr decltype(auto)
43 #endif
44  to_stat_space(const T& t, R&& data_view)
45  {
46  if constexpr (dimension_of_v<T> != stdex::dynamic_extent and collections::size_of_v<R> != stdex::dynamic_extent)
47  static_assert(dimension_of_v<T> == collections::size_of_v<R>);
48 
49  if constexpr (euclidean_pattern<T>)
50  {
51  return std::forward<R>(data_view);
52  }
53  else
54  {
55  using U = std::decay_t<stdex::unwrap_ref_decay_t<T>>;
57  if constexpr (std::is_same_v<U, T>)
58  return stdex::invoke(Traits::to_stat_space, t, std::forward<R>(data_view));
59  else
60  return stdex::invoke(Traits::to_stat_space, t.get(), std::forward<R>(data_view));
61  }
62  }
63 
64 
65  namespace detail
66  {
67  template<std::size_t t_i = 0, std::size_t data_view_i = 0, typename T, typename R, typename...Out>
68  static constexpr auto
69  to_stat_space_tuple(const T& t, const R& data_view, Out...out)
70  {
71  if constexpr (t_i < collections::size_of_v<T>)
72  {
73  decltype(auto) t_elem = collections::get<t_i>(t);
74  auto dim = get_dimension(t_elem);
75  auto data_view_sub = collections::views::slice(data_view, std::integral_constant<std::size_t, data_view_i>{}, dim);
76  auto o = collections::views::all(to_stat_space(t_elem, std::move(data_view_sub)));
77  return to_stat_space_tuple<t_i + 1, data_view_i + values::fixed_value_of_v<decltype(dim)>>(
78  t, data_view, std::move(out)..., std::move(o));
79  }
80  else
81  {
82  return collections::views::concat(std::move(out)...);
83  }
84  }
85  }
86 
87 
95 #ifdef __cpp_concepts
96  template<descriptor_collection T, collections::collection R>
97  constexpr collections::collection decltype(auto)
98 #else
99  template<typename T, typename R, std::enable_if_t<descriptor_collection<T> and collections::collection<R>, int> = 0>
100  constexpr decltype(auto)
101 #endif
102  to_stat_space(const T& t, R&& data_view)
103  {
104  if constexpr (dimension_of_v<T> != stdex::dynamic_extent and collections::size_of_v<R> != stdex::dynamic_extent)
105  static_assert(dimension_of_v<T> == collections::size_of_v<R>);
106 
107  if constexpr (dimension_of_v<T> == stdex::dynamic_extent)
108  {
109  std::vector<collections::common_collection_type_t<R>> stat_data;
110  stat_data.reserve(get_stat_dimension(t));
111  std::size_t i = 0;
112  for (auto& d : t)
113  {
114  auto dim = get_dimension(d);
115  auto sd = to_stat_space(d, collections::views::slice(data_view, i, dim));
116  stat_data.insert(stat_data.end(), sd.cbegin(), sd.cend());
117  i += values::to_value_type(dim);
118  }
119  return stat_data;
120  }
121  else //if constexpr (fixed_pattern<T>)
122  {
123  return detail::to_stat_space_tuple(t, data_view);
124  }
125  }
126 
127 }
128 
129 
130 #endif
Definition for coordinates::euclidean_pattern.
constexpr bool collection
An object describing a collection of objects.
Definition: collection.hpp:32
constexpr detail::concat_adaptor concat
a std::ranges::range_adaptor_closure for a set of concatenated collection objects.
Definition: concat.hpp:196
constexpr auto get_stat_dimension(const Arg &arg)
Get the vector dimension of coordinates::pattern Arg when transformed into statistical space...
Definition: get_stat_dimension.hpp:69
decltype(auto) constexpr to_stat_space(const T &t, R &&data_view)
Maps a range reflecting vector-space data to a corresponding range in a vector space for directional ...
Definition: to_stat_space.hpp:44
decltype(auto) constexpr to_value_type(Arg &&arg)
Convert, if necessary, a fixed or dynamic value to its underlying base type.
Definition: to_value_type.hpp:28
constexpr auto get_dimension(const Arg &arg)
Get the vector dimension of coordinates::pattern Arg.
Definition: get_dimension.hpp:54
Definition for coordinates::descriptor.
The namespace for features relating to coordinates::pattern object.
Definition: compares_with.hpp:25
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:72
Inclusion file for collections.
Definition for coordinates::get_stat_dimension.
constexpr detail::slice_adapter slice
a RangeAdapterObject associated with slice_view.
Definition: slice.hpp:364
Traits for coordinates::pattern objects.
Definition: coordinate_descriptor_traits.hpp:36
Definition for coordinates::get_dimension.
Definition for coordinates::descriptor_collection.
Definition for coordinates::dimension_of.