riccaticpp
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1#ifndef INCLUDE_RICCATI_UTILS_HPP
2#define INCLUDE_RICCATI_UTILS_HPP
3
4#include <Eigen/Dense>
5#include <type_traits>
6#ifdef RICCATI_DEBUG
7#include <iostream>
8#include <iomanip>
9#endif
10namespace riccati {
11
12template <typename T>
13constexpr Eigen::Index compile_size_v
14 = std::decay_t<T>::RowsAtCompileTime * std::decay_t<T>::ColsAtCompileTime;
15
39template <typename Scalar, typename Vector>
40inline auto scale(Vector&& x, Scalar x0, Scalar h) {
41 return (x0 + h / 2.0 + h / 2.0 * x.array()).matrix();
42}
43
47template <typename Scalar>
48using matrix_t = Eigen::Matrix<Scalar, -1, -1>;
49template <typename Scalar>
50using vector_t = Eigen::Matrix<Scalar, -1, 1>;
51
52template <typename Scalar>
53using row_vector_t = Eigen::Matrix<Scalar, 1, -1>;
54
55template <typename Scalar>
56using array2d_t = Eigen::Matrix<Scalar, -1, -1>;
57template <typename Scalar>
58using array1d_t = Eigen::Matrix<Scalar, -1, 1>;
59template <typename Scalar>
60using row_array1d_t = Eigen::Matrix<Scalar, 1, -1>;
61
62template <typename T>
63inline constexpr T pi() {
64 return static_cast<T>(3.141592653589793238462643383279);
65}
66
67inline double eval(double x) { return x; }
68template <typename T>
69inline std::complex<T>& eval(std::complex<T>& x) {
70 return x;
71}
72template <typename T>
73inline std::complex<T> eval(std::complex<T>&& x) {
74 return x;
75}
76
77template <typename T>
78inline auto eval(T&& x) {
79 return x.eval();
80}
81
82template <typename T, Eigen::Index R, Eigen::Index C>
83inline Eigen::Matrix<T, R, C> eval(Eigen::Matrix<T, R, C>&& x) {
84 return std::move(x);
85}
86
87template <typename T, Eigen::Index R, Eigen::Index C>
88inline auto& eval(Eigen::Matrix<T, R, C>& x) {
89 return x;
90}
91
92template <typename T, Eigen::Index R, Eigen::Index C>
93inline const auto& eval(const Eigen::Matrix<T, R, C>& x) {
94 return x;
95}
96
97template <typename T, Eigen::Index R, Eigen::Index C>
98inline Eigen::Array<T, R, C> eval(Eigen::Array<T, R, C>&& x) {
99 return std::move(x);
100}
101
102template <typename T, Eigen::Index R, Eigen::Index C>
103inline auto& eval(Eigen::Array<T, R, C>& x) {
104 return x;
105}
106
107template <typename T, Eigen::Index R, Eigen::Index C>
108inline const auto& eval(const Eigen::Array<T, R, C>& x) {
109 return x;
110}
111
112template <typename T, typename Scalar>
113auto get_slice(T&& x_eval, Scalar start, Scalar end) {
114 Eigen::Index i = 0;
115 Eigen::Index dense_start = 0;
116 if (start > end) {
117 std::swap(start, end);
118 }
119 for (; i < x_eval.size(); ++i) {
120 if ((x_eval[i] >= start && x_eval[i] <= end)) {
121 dense_start = i;
122 break;
123 }
124 }
125 Eigen::Index dense_size = 0;
126 for (; i < x_eval.size(); ++i) {
127 if ((x_eval[i] >= start && x_eval[i] <= end)) {
128 dense_size++;
129 } else {
130 break;
131 }
132 }
133 return std::make_pair(dense_start, dense_size);
134}
135
136template <typename T>
138 = std::enable_if_t<!std::is_floating_point<std::decay_t<T>>::value>;
139
140template <typename T>
142 = std::enable_if_t<std::is_floating_point<std::decay_t<T>>::value>;
143
144namespace internal {
145template <typename T>
146struct is_complex_impl : std::false_type {};
147template <typename T>
148struct is_complex_impl<std::complex<T>> : std::true_type {};
149} // namespace internal
151template <typename T>
152struct is_complex : internal::is_complex_impl<std::decay_t<T>> {};
154template <typename T>
156 = std::enable_if_t<std::is_floating_point<std::decay_t<T>>::value
158
159template <typename T>
161 = std::enable_if_t<!std::is_floating_point<std::decay_t<T>>::value
162 && !is_complex<std::decay_t<T>>::value>;
163
164template <typename T, require_floating_point_or_complex<T>* = nullptr>
165inline auto sin(T x) {
166 return std::sin(x);
167}
168
169template <typename T, require_not_floating_point<T>* = nullptr>
170inline auto sin(T&& x) {
171 return x.sin();
172}
173
174template <typename T, require_floating_point_or_complex<T>* = nullptr>
175inline auto cos(T x) {
176 return std::cos(x);
177}
178
179template <typename T, require_not_floating_point<T>* = nullptr>
180inline auto cos(T&& x) {
181 return x.cos();
182}
183
184template <typename T, require_floating_point_or_complex<T>* = nullptr>
185inline auto sqrt(T x) {
186 return std::sqrt(x);
187}
188
189template <typename T, require_not_floating_point<T>* = nullptr>
190inline auto sqrt(T&& x) {
191 return x.sqrt();
192}
193
194template <typename T, require_floating_point_or_complex<T>* = nullptr>
195inline auto square(T x) {
196 return x * x;
197}
198
199template <typename T, require_not_floating_point<T>* = nullptr>
200inline auto square(T&& x) {
201 return x.square();
202}
203
204template <typename T, require_floating_point_or_complex<T>* = nullptr>
205inline auto array(T x) {
206 return x;
207}
208
209template <typename T, require_not_floating_point<T>* = nullptr>
210inline auto array(T&& x) {
211 return x.array();
212}
213
214template <typename T, require_floating_point_or_complex<T>* = nullptr>
215inline auto matrix(T x) {
216 return x;
217}
218
219template <typename T, require_not_floating_point<T>* = nullptr>
220inline auto matrix(T&& x) {
221 return x.matrix();
222}
223
224template <typename T, require_floating_point_or_complex<T>* = nullptr>
225inline constexpr T zero_like(T x) {
226 return static_cast<T>(0);
227}
228
229template <typename T, require_not_floating_point<T>* = nullptr>
230inline auto zero_like(const T& x) {
231 return std::decay_t<typename T::PlainObject>::Zero(x.rows(), x.cols());
232}
233
234template <typename T1, typename T2, require_floating_point<T1>* = nullptr>
235inline auto pow(T1 x, T2 y) {
236 return std::pow(x, y);
237}
238
239template <typename T1, typename T2, require_not_floating_point<T1>* = nullptr>
240inline auto pow(T1&& x, T2 y) {
241 return x.array().pow(y);
242}
243
244template <typename T, int R, int C>
245inline void print(const char* name, const Eigen::Matrix<T, R, C>& x) {
246#ifdef RICCATI_DEBUG
247 std::cout << name << "(" << x.rows() << ", " << x.cols() << ")" << std::endl;
248 std::cout << x << std::endl;
249#endif
250}
251
252template <typename T, int R, int C>
253inline void print(const char* name, const Eigen::Array<T, R, C>& x) {
254#ifdef RICCATI_DEBUG
255 std::cout << name << "(" << x.rows() << ", " << x.cols() << ")" << std::endl;
256 std::cout << x << std::endl;
257#endif
258}
259
260template <
261 typename T,
262 std::enable_if_t<std::is_floating_point<std::decay_t<T>>::value
263 || std::is_integral<std::decay_t<T>>::value>* = nullptr>
264inline void print(const char* name, T&& x) {
265#ifdef RICCATI_DEBUG
266 std::cout << name << ": " << std::setprecision(16) << x << std::endl;
267#endif
268}
269
270template <
271 typename T,
272 std::enable_if_t<std::is_floating_point<std::decay_t<T>>::value
273 || std::is_integral<std::decay_t<T>>::value>* = nullptr>
274inline void print(const char* name, const std::complex<T>& x) {
275#ifdef RICCATI_DEBUG
276 std::cout << name << ": (" << std::setprecision(16) << x.real() << ", "
277 << x.imag() << ")" << std::endl;
278#endif
279}
280
281} // namespace riccati
282
283#endif
auto sqrt(T x)
Definition utils.hpp:181
auto array(T x)
Definition utils.hpp:201
constexpr Eigen::Index compile_size_v
Definition utils.hpp:14
std::enable_if_t<!std::is_floating_point< std::decay_t< T > >::value &&!is_complex< std::decay_t< T > >::value > require_not_floating_point_or_complex
Definition utils.hpp:157
auto sin(T x)
Definition utils.hpp:161
Eigen::Matrix< Scalar, 1, -1 > row_array1d_t
Definition utils.hpp:60
Eigen::Matrix< Scalar, 1, -1 > row_vector_t
Definition utils.hpp:53
Eigen::Matrix< Scalar, -1, -1 > array2d_t
Definition utils.hpp:56
auto eval(arena_allocator< T, arena_alloc > &arena, const Expr &expr) noexcept
auto square(T x)
Definition utils.hpp:191
Eigen::Matrix< Scalar, -1, 1 > array1d_t
Definition utils.hpp:58
std::enable_if_t< std::is_floating_point< std::decay_t< T > >::value > require_floating_point
Definition utils.hpp:140
Eigen::Matrix< Scalar, -1, -1 > matrix_t
Definition utils.hpp:48
auto get_slice(T &&x_eval, Scalar start, Scalar end)
Definition utils.hpp:113
constexpr T zero_like(T x)
Definition utils.hpp:221
auto pow(T1 x, T2 y)
Definition utils.hpp:231
std::enable_if_t<!std::is_floating_point< std::decay_t< T > >::value > require_not_floating_point
Definition utils.hpp:137
auto cos(T x)
Definition utils.hpp:171
auto scale(Vector &&x, Scalar x0, Scalar h)
Scales and shifts a vector of Chebyshev nodes.
Definition utils.hpp:40
std::enable_if_t< std::is_floating_point< std::decay_t< T > >::value||is_complex< std::decay_t< T > >::value > require_floating_point_or_complex
Definition utils.hpp:153
Eigen::Matrix< Scalar, -1, 1 > vector_t
Definition utils.hpp:50
constexpr T pi()
Definition utils.hpp:63
void print(const char *name, const arena_matrix< T > &x)
auto matrix(T x)
Definition utils.hpp:211