OpenKalman
CwiseBinaryOp.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) 2023-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_EIGEN_TRAITS_CWISEBINARYOP_HPP
17 #define OPENKALMAN_EIGEN_TRAITS_CWISEBINARYOP_HPP
18 
19 
20 namespace OpenKalman::interface
21 {
22  template<typename BinaryOp, typename LhsType, typename RhsType>
23  struct indexible_object_traits<Eigen::CwiseBinaryOp<BinaryOp, LhsType, RhsType>>
24  : Eigen3::indexible_object_traits_base<Eigen::CwiseBinaryOp<BinaryOp, LhsType, RhsType>>
25  {
26  private:
27 
28  using Xpr = Eigen::CwiseBinaryOp<BinaryOp, LhsType, RhsType>;
31 
32  public:
33 
34  template<typename Arg, typename N>
35  static constexpr auto
36  get_vector_space_descriptor(const Arg& arg, N n)
37  {
38  if constexpr (square_shaped<LhsType> or square_shaped<RhsType>)
39  return internal::best_vector_space_descriptor(
40  OpenKalman::get_vector_space_descriptor<0>(arg.lhs()),
41  OpenKalman::get_vector_space_descriptor<0>(arg.rhs()),
42  OpenKalman::get_vector_space_descriptor<1>(arg.lhs()),
43  OpenKalman::get_vector_space_descriptor<1>(arg.rhs()));
44  else
45  return internal::best_vector_space_descriptor(
48  }
49 
50 
51  // nested_object() not defined
52 
53  private:
54 
55 #ifndef __cpp_concepts
56  template<typename T = Traits, typename = void>
57  struct custom_get_constant_defined : std::false_type {};
58 
59  template<typename T>
60  struct custom_get_constant_defined<T, std::void_t<decltype(T::template get_constant<LhsType, RhsType>(std::declval<const Xpr&>()))>>
61  : std::true_type {};
62 
63  template<typename T = Traits, typename = void>
64  struct constexpr_operation_defined : std::false_type {};
65 
66  template<typename T>
67  struct constexpr_operation_defined<T, std::void_t<decltype(T::constexpr_operation())>>
68  : std::true_type {};
69 #endif
70 
71  public:
72 
73  template<typename Arg>
74  static constexpr auto get_constant(const Arg& arg)
75  {
76 #ifdef __cpp_concepts
77  if constexpr (requires { Traits::template get_constant<LhsType, RhsType>(arg); })
78 #else
80 #endif
81  return Traits::template get_constant<LhsType, RhsType>(arg);
82  else if constexpr (Traits::binary_functor_type == Eigen3::BinaryFunctorType::sum and zero<LhsType>)
83  return constant_coefficient {arg.rhs()};
84  else if constexpr (Traits::binary_functor_type == Eigen3::BinaryFunctorType::sum and zero<RhsType>)
85  return constant_coefficient {arg.lhs()};
86  else if constexpr (Traits::binary_functor_type == Eigen3::BinaryFunctorType::product and zero<LhsType>)
87  return constant_coefficient {arg.lhs()};
88  else if constexpr (Traits::binary_functor_type == Eigen3::BinaryFunctorType::product and zero<RhsType>)
89  return constant_coefficient {arg.rhs()};
90 #ifdef __cpp_concepts
91  else if constexpr (requires { Traits::constexpr_operation(); })
92 #else
93  else if constexpr (constexpr_operation_defined<>::value)
94 #endif
95  return values::operation {Traits::constexpr_operation(),
96  constant_coefficient {arg.lhs()}, constant_coefficient {arg.rhs()}};
97  else
98  return values::operation {arg.functor(),
99  constant_coefficient {arg.lhs()}, constant_coefficient {arg.rhs()}};
100  }
101 
102  private:
103 
104 #ifndef __cpp_concepts
105  template<typename T = Traits, typename = void>
106  struct custom_get_constant_diagonal_defined : std::false_type {};
107 
108  template<typename T>
109  struct custom_get_constant_diagonal_defined<T, std::void_t<decltype(T::template get_constant_diagonal<LhsType, RhsType>(std::declval<const Xpr&>()))>>
110  : std::true_type {};
111 #endif
112 
113  public:
114 
115  template<typename Arg>
116  static constexpr auto get_constant_diagonal(const Arg& arg)
117  {
118 #ifdef __cpp_concepts
119  if constexpr (requires { Traits::template get_constant_diagonal<LhsType, RhsType>(arg); })
120 #else
122 #endif
123  return Traits::template get_constant_diagonal<LhsType, RhsType>(arg);
124  else if constexpr (Traits::binary_functor_type == Eigen3::BinaryFunctorType::sum and zero<LhsType>)
125  return constant_diagonal_coefficient {arg.rhs()};
126  else if constexpr (Traits::binary_functor_type == Eigen3::BinaryFunctorType::sum and zero<RhsType>)
127  return constant_diagonal_coefficient {arg.lhs()};
128  else if constexpr (Traits::binary_functor_type == Eigen3::BinaryFunctorType::product and zero<LhsType>)
129  return constant_coefficient {arg.lhs()};
130  else if constexpr (Traits::binary_functor_type == Eigen3::BinaryFunctorType::product and zero<RhsType>)
131  return constant_coefficient {arg.rhs()};
132  else if constexpr (Traits::binary_functor_type == Eigen3::BinaryFunctorType::product)
133  {
134  auto c_left = [](const Arg& arg){
135  if constexpr (constant_matrix<LhsType>) return constant_coefficient {arg.lhs()};
136  else return constant_diagonal_coefficient {arg.lhs()};
137  }(arg);
138  auto c_right = [](const Arg& arg){
139  if constexpr (constant_matrix<RhsType>) return constant_coefficient {arg.rhs()};
140  else return constant_diagonal_coefficient {arg.rhs()};
141  }(arg);
142 #ifdef __cpp_concepts
143  if constexpr (requires { Traits::constexpr_operation(); })
144 #else
146 #endif
147  return values::operation {Traits::constexpr_operation(), c_left, c_right};
148  else
149  return values::operation {arg.functor(), c_left, c_right};
150  }
151  else if constexpr (Traits::binary_functor_type == Eigen3::BinaryFunctorType::sum or Traits::preserves_constant_diagonal)
152  {
153 #ifdef __cpp_concepts
154  if constexpr (requires { Traits::constexpr_operation(); })
155 #else
157 #endif
158  return values::operation {Traits::constexpr_operation(),
160  else
161  return values::operation {arg.functor(),
163  }
164  else
165  {
166  return std::monostate{};
167  }
168  }
169 
170 
171  template<Applicability b>
172  static constexpr bool one_dimensional =
173  OpenKalman::one_dimensional<LhsType, Applicability::permitted> and
174  OpenKalman::one_dimensional<RhsType, Applicability::permitted> and
176  not has_dynamic_dimensions<Xpr> or
177  OpenKalman::one_dimensional<LhsType, b> or
178  OpenKalman::one_dimensional<RhsType, b>);
179 
180 
181  template<Applicability b>
182  static constexpr bool is_square =
183  square_shaped<LhsType, Applicability::permitted> and
184  square_shaped<RhsType, Applicability::permitted> and
186  not has_dynamic_dimensions<Xpr> or
187  square_shaped<LhsType, b> or
188  square_shaped<RhsType, b>);
189 
190 
191  static constexpr bool is_triangular_adapter = false;
192 
193 
194  template<TriangleType t>
195  static constexpr bool is_triangular =
196  Traits::binary_functor_type == Eigen3::BinaryFunctorType::sum ?
197  triangular_matrix<LhsType, t> and triangular_matrix<RhsType, t> and
198  (t != TriangleType::any or triangle_type_of_v<LhsType, RhsType> != TriangleType::any) :
199  Traits::binary_functor_type == Eigen3::BinaryFunctorType::product and
200  (triangular_matrix<LhsType, t> or triangular_matrix<RhsType, t> or
201  (triangular_matrix<LhsType, TriangleType::lower> and triangular_matrix<RhsType, TriangleType::upper>) or
202  (triangular_matrix<LhsType, TriangleType::upper> and triangular_matrix<RhsType, TriangleType::lower>));
203 
204 
205  static constexpr bool is_hermitian = Traits::preserves_hermitian and
206  hermitian_matrix<LhsType, Applicability::permitted> and hermitian_matrix<RhsType, Applicability::permitted>;;
207 
208  };
209 
210 
211 } // namespace OpenKalman::interface
212 
213 #endif //OPENKALMAN_EIGEN_TRAITS_CWISEBINARYOP_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
An operation involving some number of values.
Definition: operation.hpp:69
Trait object providing get and set routines.
Definition: eigen-forward-declarations.hpp:502
Lower, upper, or diagonal matrix.
Definition: eigen-comma-initializers.hpp:20
constexpr bool value
T is numerical value or is reducible to a numerical value.
Definition: value.hpp:31
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
Definition: eigen-forward-declarations.hpp:61
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