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 object_traits<Eigen::CwiseBinaryOp<BinaryOp, LhsType, RhsType>>
24  : Eigen3::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_pattern_collection(const Arg& arg, N n)
37  {
38  if constexpr (square_shaped<LhsType> or square_shaped<RhsType>)
39  return internal::most_fixed_pattern(
40  OpenKalman::get_pattern_collection<0>(arg.lhs()),
41  OpenKalman::get_pattern_collection<0>(arg.rhs()),
42  OpenKalman::get_pattern_collection<1>(arg.lhs()),
43  OpenKalman::get_pattern_collection<1>(arg.rhs()));
44  else
45  return internal::most_fixed_pattern(
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_value {arg.rhs()};
84  else if constexpr (Traits::binary_functor_type == Eigen3::BinaryFunctorType::sum and zero<RhsType>)
85  return constant_value {arg.lhs()};
86  else if constexpr (Traits::binary_functor_type == Eigen3::BinaryFunctorType::product and zero<LhsType>)
87  return constant_value {arg.lhs()};
88  else if constexpr (Traits::binary_functor_type == Eigen3::BinaryFunctorType::product and zero<RhsType>)
89  return constant_value {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_value {arg.lhs()}, constant_value {arg.rhs()});
97  else
98  return values::operation(arg.functor(),
99  constant_value {arg.lhs()}, constant_value {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_value {arg.rhs()};
126  else if constexpr (Traits::binary_functor_type == Eigen3::BinaryFunctorType::sum and zero<RhsType>)
127  return constant_diagonal_value {arg.lhs()};
128  else if constexpr (Traits::binary_functor_type == Eigen3::BinaryFunctorType::product and zero<LhsType>)
129  return constant_value {arg.lhs()};
130  else if constexpr (Traits::binary_functor_type == Eigen3::BinaryFunctorType::product and zero<RhsType>)
131  return constant_value {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_value {arg.lhs()};
136  else return constant_diagonal_value {arg.lhs()};
137  }(arg);
138  auto c_right = [](const Arg& arg){
139  if constexpr (constant_matrix<RhsType>) return constant_value {arg.rhs()};
140  else return constant_diagonal_value {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(),
159  constant_diagonal_value {arg.lhs()}, constant_diagonal_value {arg.rhs()});
160  else
161  return values::operation(arg.functor(),
162  constant_diagonal_value {arg.lhs()}, constant_diagonal_value {arg.rhs()});
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<triangle_type t>
195  static constexpr bool triangle_type_value =
196  Traits::binary_functor_type == Eigen3::BinaryFunctorType::sum ?
197  triangular_matrix<LhsType, t> and triangular_matrix<RhsType, t> and
198  (t != triangle_type::any or triangle_type_of_v<LhsType, RhsType> != triangle_type::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, triangle_type::lower> and triangular_matrix<RhsType, triangle_type::upper>) or
202  (triangular_matrix<LhsType, triangle_type::upper> and triangular_matrix<RhsType, triangle_type::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 }
212 
213 #endif
constexpr bool one_dimensional
Specifies that a type is one-dimensional in every index.
Definition: one_dimensional.hpp:56
Definition: basics.hpp:41
The concept, trait, or restraint represents a compile-time guarantee.
Definition: eigen-comma-initializers.hpp:20
decltype(auto) constexpr get_pattern_collection(T &&t)
Get the coordinates::pattern_collection associated with indexible object T.
Definition: get_pattern_collection.hpp:59
constexpr bool value
T is a fixed or dynamic value that is reducible to a number.
Definition: value.hpp:45
Lower, upper, or diagonal matrix.
Definition: object_traits.hpp:38
Trait object providing get and set routines.
Definition: eigen-forward-declarations.hpp:502
Definition: eigen-forward-declarations.hpp:61
constexpr auto constant_value(T &&t)
The constant value associated with a constant_object or constant_diagonal_object. ...
Definition: constant_value.hpp:37
constexpr auto operation(Operation &&op, Args &&...args)
A potentially constant-evaluated operation involving some number of values.
Definition: operation.hpp:98