OpenKalman
Reshaped.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-2023 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_EIGEN_TRAITS_RESHAPED_HPP
17 #define OPENKALMAN_EIGEN_TRAITS_RESHAPED_HPP
18 
19 #include <type_traits>
20 
21 
22 namespace OpenKalman::interface
23 {
24  namespace detail
25  {
26  template<typename XprType, int Rows, int Cols, int Order, bool HasDirectAccess>
27  struct ReshapedNested { using type = typename Eigen::Reshaped<XprType, Rows, Cols, Order>::MatrixTypeNested; };
28 
29  template<typename XprType, int Rows, int Cols, int Order>
30  struct ReshapedNested<XprType, Rows, Cols, Order, true>
31  {
32  using type = typename Eigen::internal::ref_selector<XprType>::non_const_type;
33  };
34  }
35 
36 
37  template<typename XprType, int Rows, int Cols, int Order>
38  struct indexible_object_traits<Eigen::Reshaped<XprType, Rows, Cols, Order>>
39  : Eigen3::indexible_object_traits_base<Eigen::Reshaped<XprType, Rows, Cols, Order>>
40  {
41  private:
42 
44 
45  static constexpr std::size_t nested_components = has_dynamic_dimensions<XprType> ? dynamic_size :
46  index_dimension_of_v<XprType, 0> * index_dimension_of_v<XprType, 1>;
47 
48  static constexpr std::size_t xprtypemax = dynamic_index_count_v<XprType> < 2 ? std::max(
49  dynamic_dimension<XprType, 0> ? 0 : index_dimension_of_v<XprType, 0>,
50  dynamic_dimension<XprType, 1> ? 0 : index_dimension_of_v<XprType, 1>) : dynamic_size;
51 
52  static constexpr bool HasDirectAccess = Eigen::internal::traits<Eigen::Reshaped<XprType, Rows, Cols, Order>>::HasDirectAccess;
53 
54  using Nested_t = typename detail::ReshapedNested<XprType, Rows, Cols, Order, HasDirectAccess>::type;
55 
56  public:
57 
58  template<typename Arg, typename N>
59  static constexpr auto get_vector_space_descriptor(const Arg& arg, N n)
60  {
61  if constexpr (values::fixed<N>)
62  {
63  constexpr auto dim = n == 0_uz ? Rows : Cols;
64  constexpr auto other_dim = n == 0_uz ? Cols : Rows;
65  constexpr std::size_t dimension =
66  dim != Eigen::Dynamic ? dim :
67  other_dim == Eigen::Dynamic or other_dim == 0 ? dynamic_size :
68  other_dim == index_dimension_of_v<XprType, 0> ? index_dimension_of_v<XprType, 1> :
69  other_dim == index_dimension_of_v<XprType, 1> ? index_dimension_of_v<XprType, 0> :
70  nested_components != dynamic_size and nested_components % other_dim == 0 ? nested_components / other_dim :
72 
73  if constexpr (dimension == dynamic_size)
74  {
75  if constexpr (n == 0_uz) return static_cast<std::size_t>(arg.rows());
76  else return static_cast<std::size_t>(arg.cols());
77  }
78  else return Dimensions<dimension>{};
79  }
80  else
81  {
82  if (n == 0_uz) return static_cast<std::size_t>(arg.rows());
83  else return static_cast<std::size_t>(arg.cols());
84  }
85  }
86 
87 
88  template<typename Arg>
89  static decltype(auto) nested_object(Arg&& arg)
90  {
91  return std::forward<Arg>(arg).nestedExpression();
92  }
93 
94 
95  template<typename Arg>
96  static constexpr auto get_constant(const Arg& arg)
97  {
98  return constant_coefficient {arg.nestedExpression()};
99  }
100 
101 
102  template<typename Arg>
103  static constexpr auto get_constant_diagonal(const Arg& arg)
104  {
105  if constexpr ((Rows != Eigen::Dynamic and Rows == XprType::RowsAtCompileTime) or (Cols != Eigen::Dynamic and Cols == XprType::ColsAtCompileTime))
106  return constant_diagonal_coefficient {arg.nestedExpression()};
107  else
108  return std::monostate{};
109  }
110 
111 
112  template<Applicability b>
113  static constexpr bool one_dimensional =
114  (Rows == 1 and Cols == 1 and OpenKalman::one_dimensional<XprType, Applicability::permitted>) or
115  ((Rows == 1 or Rows == Eigen::Dynamic) and (Cols == 1 or Cols == Eigen::Dynamic) and OpenKalman::one_dimensional<XprType, b>);
116 
117 
118  template<Applicability b>
119  static constexpr bool is_square =
120  (b != Applicability::guaranteed or (Rows != Eigen::Dynamic and Cols != Eigen::Dynamic) or
121  ((Rows != Eigen::Dynamic or Cols != Eigen::Dynamic) and not has_dynamic_dimensions<XprType>)) and
122  (Rows == Eigen::Dynamic or Cols == Eigen::Dynamic or Rows == Cols) and
123  (nested_components == dynamic_size or (
124  values::internal::near(nested_components, values::sqrt(nested_components) * values::sqrt(nested_components)) and
125  (Rows == Eigen::Dynamic or Rows * Rows == nested_components))) and
126  (Rows == Eigen::Dynamic or xprtypemax == dynamic_size or (Rows * Rows) % xprtypemax == 0) and
127  (Cols == Eigen::Dynamic or xprtypemax == dynamic_size or (Cols * Cols) % xprtypemax == 0);
128 
129 
130  template<TriangleType t>
131  static constexpr bool is_triangular = triangular_matrix<XprType, t> and
132  ((Rows != Eigen::Dynamic and Rows == XprType::RowsAtCompileTime) or (Cols != Eigen::Dynamic and Cols == XprType::ColsAtCompileTime));
133 
134 
135  static constexpr bool is_triangular_adapter = false;
136 
137 
138  static constexpr bool is_hermitian = hermitian_matrix<XprType, Applicability::permitted> and
139  ((XprType::RowsAtCompileTime == Eigen::Dynamic and XprType::ColsAtCompileTime == Eigen::Dynamic) or
140  (Rows == Eigen::Dynamic or Rows == XprType::RowsAtCompileTime or Rows == XprType::ColsAtCompileTime) and
141  (Cols == Eigen::Dynamic or Cols == XprType::ColsAtCompileTime or Cols == XprType::RowsAtCompileTime));
142  };
143 
144 } // namespace OpenKalman::interface
145 
146 #endif //OPENKALMAN_EIGEN_TRAITS_RESHAPED_HPP
constexpr bool one_dimensional
Specifies that a type is one-dimensional in every index.
Definition: one_dimensional.hpp:83
Definition: indexible_object_traits.hpp:36
Definition: basics.hpp:41
Trait object providing get and set routines.
Definition: eigen-forward-declarations.hpp:502
Definition: eigen-comma-initializers.hpp:20
The constant associated with T, assuming T is a constant_matrix.
Definition: constant_coefficient.hpp:36
The constant associated with T, assuming T is a constant_diagonal_matrix.
Definition: constant_diagonal_coefficient.hpp:32
constexpr auto sqrt(const Arg &arg)
A constexpr alternative to std::sqrt.
Definition: sqrt.hpp:46
constexpr bool near(const Arg1 &arg1, const Arg2 &arg2)
Determine whether two numbers are within a rounding tolerance.
Definition: near.hpp:36
constexpr std::size_t dynamic_size
A constant indicating that a size or index is dynamic.
Definition: global-definitions.hpp:33
decltype(auto) constexpr nested_object(Arg &&arg)
Retrieve a nested object of Arg, if it exists.
Definition: nested_object.hpp:34
The concept, trait, or restraint represents a compile-time guarantee.
constexpr auto get_vector_space_descriptor(const T &t, const N &n)
Get the coordinates::pattern object for index N of indexible object T.
Definition: get_vector_space_descriptor.hpp:56