aligator 0.19.0
A versatile and efficient C++ library for real-time constrained trajectory optimization.
Loading...
Searching...
No Matches
math.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#include <Eigen/Core>
7#include <cassert>
8
9#define ALIGATOR_DYNAMIC_TYPEDEFS(Scalar) \
10 using VectorXs = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>; \
11 using MatrixXs = Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>; \
12 using VectorOfVectors = std::vector<VectorXs>; \
13 using VectorRef = Eigen::Ref<VectorXs>; \
14 using MatrixRef = Eigen::Ref<MatrixXs>; \
15 using ConstVectorRef = Eigen::Ref<const VectorXs>; \
16 using ConstMatrixRef = Eigen::Ref<const MatrixXs>; \
17 using Vector3s = Eigen::Matrix<Scalar, 3, 1>; \
18 using Vector6s = Eigen::Matrix<Scalar, 6, 1>; \
19 using Matrix3Xs = Eigen::Matrix<Scalar, 3, Eigen::Dynamic>; \
20 using Matrix6Xs = Eigen::Matrix<Scalar, 6, Eigen::Dynamic>; \
21 using Matrix6s = Eigen::Matrix<Scalar, 6, 6>
22
23#define ALIGATOR_DYNAMIC_TYPEDEFS_WITH_ROW_TYPES(Scalar) \
24 ALIGATOR_DYNAMIC_TYPEDEFS(Scalar); \
25 using RowMatrixXs = typename Eigen::Transpose<MatrixXs>::PlainObject; \
26 using RowMatrixRef = Eigen::Ref<RowMatrixXs>; \
27 using ConstRowMatrixRef = Eigen::Ref<const RowMatrixXs>
28
29#ifdef ALIGATOR_EIGEN_CHECK_MALLOC
32#define ALIGATOR_EIGEN_ALLOW_MALLOC(allowed) \
33 ::Eigen::internal::set_is_malloc_allowed(allowed)
38#define ALIGATOR_NOMALLOC_SCOPED \
39 const ::aligator::internal::scoped_nomalloc ___aligator_nomalloc_zone {}
41#define ALIGATOR_NOMALLOC_RESTORE \
42 ALIGATOR_EIGEN_ALLOW_MALLOC(::aligator::internal::get_cached_malloc_status())
43#else
44#define ALIGATOR_EIGEN_ALLOW_MALLOC(allowed)
45#define ALIGATOR_NOMALLOC_SCOPED
46#define ALIGATOR_NOMALLOC_RESTORE
47#endif
48
50#define ALIGATOR_NOMALLOC_BEGIN ALIGATOR_EIGEN_ALLOW_MALLOC(false)
52#define ALIGATOR_NOMALLOC_END ALIGATOR_EIGEN_ALLOW_MALLOC(true)
53
54namespace aligator {
55
60template <template <class> class Base, typename Derived> struct is_tpl_base_of {
61 static constexpr std::false_type f(const void *);
62 template <typename OtherDerived>
63 static constexpr std::true_type f(const Base<OtherDerived> *);
64 static constexpr bool value =
65 decltype(f(std::declval<std::remove_reference_t<Derived> *>()))::value;
66};
67
68template <template <class> class Base, typename Derived>
70
71template <typename T>
72struct is_eigen : std::bool_constant<is_tpl_base_of_v<Eigen::EigenBase, T>> {};
73
74template <typename T> inline constexpr bool is_eigen_v = is_eigen<T>::value;
75
76template <typename T>
77using enable_if_eigen_t = std::enable_if_t<is_eigen_v<std::decay_t<T>>>;
78
79template <typename T>
81
82template <typename T, typename T2 = void>
83using enable_if_eigen_dense_t = std::enable_if_t<is_eigen_dense_v<T>, T2>;
84
85template <typename T>
86inline constexpr bool is_eigen_matrix_v =
88
89#ifdef ALIGATOR_EIGEN_CHECK_MALLOC
90namespace internal {
91thread_local static bool g_cached_malloc_status = true;
92
93inline void set_malloc_status(bool status) { g_cached_malloc_status = status; }
94
95inline void save_malloc_status() {
96 set_malloc_status(
97#ifdef ALIGATOR_EIGEN_CHECK_MALLOC
98 ::Eigen::internal::is_malloc_allowed()
99#else
100 false
101#endif
102 );
103}
104
105inline bool get_cached_malloc_status() { return g_cached_malloc_status; }
106
107struct scoped_nomalloc {
108 scoped_nomalloc(const scoped_nomalloc &) = delete;
109 scoped_nomalloc(scoped_nomalloc &&) = delete;
110 ALIGATOR_INLINE scoped_nomalloc() {
111 save_malloc_status();
113 }
114 // reset to value from before the scope
115 ~scoped_nomalloc() { ALIGATOR_NOMALLOC_RESTORE; }
116};
117
118} // namespace internal
119#endif
120
123template <typename _Scalar> struct math_types {
124 using Scalar = _Scalar;
126};
127
131template <typename D>
132auto eigenPrintWithPreamble(const Eigen::EigenBase<D> &mat,
133 std::string_view text,
134 Eigen::IOFormat ft = EIGEN_DEFAULT_IO_FORMAT) {
135 ft.matPrefix = text;
136 ft.rowSpacer = "";
137 int i = int(text.length()) - 1;
138 while (i >= 0) {
139 if (text[size_t(i)] != '\n')
140 ft.rowSpacer += ' ';
141 i--;
142 }
143 return mat.derived().format(ft);
144}
145
147namespace math {
148
149template <typename MatType>
150typename MatType::Scalar infty_norm(const Eigen::MatrixBase<MatType> &z) {
151 if (z.rows() == 0 || z.cols() == 0) {
152 return 0.;
153 } else {
154 return z.template lpNorm<Eigen::Infinity>();
155 }
156}
157
158template <typename MatType>
159typename MatType::Scalar infty_norm(const std::vector<MatType> &z) {
160 const std::size_t n = z.size();
161 typename MatType::Scalar out = 0.;
162 for (std::size_t i = 0; i < n; i++) {
163 out = std::max(out, infty_norm(z[i]));
164 }
165 return out;
166}
167
169template <typename Scalar> inline bool check_scalar(const Scalar value) {
170 return std::isnan(value) || std::isinf(value);
171}
172
174template <typename T> bool check_value(const std::vector<T> &xs) {
175 const std::size_t n = xs.size();
176 for (std::size_t i = 0; i < n; i++) {
177 if (check_value<T>(xs[i]))
178 return true;
179 }
180 return false;
181}
182
183template <typename T, typename = std::enable_if_t<std::is_scalar<T>::value>>
184bool check_value(const T &x) {
185 static_assert(std::is_scalar<T>::value, "Parameter T should be scalar.");
186 return check_scalar(x);
187}
188
189template <typename MatrixType>
190bool check_value(const Eigen::MatrixBase<MatrixType> &x) {
191 return (x.hasNaN() || (!x.allFinite()));
192}
193
197template <typename Scalar>
198bool scalar_close(const Scalar a, const Scalar b,
199 const Scalar prec = std::numeric_limits<Scalar>::epsilon()) {
200 return std::abs(a - b) < prec * (1 + std::max(std::abs(a), std::abs(b)));
201}
202
203template <typename T> T sign(const T &x) {
204 static_assert(std::is_scalar<T>::value, "Parameter T should be scalar.");
205 return T((x > T(0)) - (x < T(0)));
206}
207
209template <typename Derived, unsigned int UpLo = Eigen::Lower>
210void make_symmetric(const Eigen::MatrixBase<Derived> &matrix) {
211 Derived &mat = matrix.const_cast_derived();
212 // symmetrize upper part
213 Eigen::SelfAdjointView<Derived, UpLo> view{mat};
214 mat = view;
215}
216
217template <typename T> void setZero(std::vector<T> &mats) {
218 for (std::size_t i = 0; i < mats.size(); i++) {
219 mats[i].setZero();
220 }
221}
222
224template <typename TA, typename AA, typename TB, typename BA, typename OutType,
225 typename AOut, typename Scalar>
226void vectorMultiplyAdd(const std::vector<TA, AA> &a,
227 const std::vector<TB, BA> &b,
228 std::vector<OutType, AOut> &c, const Scalar alpha) {
229 assert(a.size() == b.size());
230 assert(a.size() == c.size());
231 const std::size_t N = a.size();
232 for (std::size_t i = 0; i < N; i++) {
233 c[i] = a[i] + alpha * b[i];
234 }
235}
236
237} // namespace math
238} // namespace aligator
#define ALIGATOR_INLINE
Definition fwd.hpp:27
#define EIGEN_DEFAULT_IO_FORMAT
Definition fwd.hpp:9
#define ALIGATOR_NOMALLOC_RESTORE
Definition math.hpp:46
#define ALIGATOR_EIGEN_ALLOW_MALLOC(allowed)
Definition math.hpp:44
Math utilities.
Definition math.hpp:147
void setZero(std::vector< T > &mats)
Definition math.hpp:217
MatType::Scalar infty_norm(const Eigen::MatrixBase< MatType > &z)
Definition math.hpp:150
void make_symmetric(const Eigen::MatrixBase< Derived > &matrix)
Symmetrize a matrix using its lower triangular part.
Definition math.hpp:210
bool check_scalar(const Scalar value)
Check that a scalar is neither inf, nor NaN.
Definition math.hpp:169
bool check_value(const std::vector< T > &xs)
Check if a std::vector of numerical objects has invalid values.
Definition math.hpp:174
T sign(const T &x)
Definition math.hpp:203
bool scalar_close(const Scalar a, const Scalar b, const Scalar prec=std::numeric_limits< Scalar >::epsilon())
Tests whether a and b are close, within absolute and relative precision prec.
Definition math.hpp:198
void vectorMultiplyAdd(const std::vector< TA, AA > &a, const std::vector< TB, BA > &b, std::vector< OutType, AOut > &c, const Scalar alpha)
Compute zi = xi + alpha * yi for all i.
Definition math.hpp:226
Main package namespace.
auto eigenPrintWithPreamble(const Eigen::EigenBase< D > &mat, std::string_view text, Eigen::IOFormat ft=EIGEN_DEFAULT_IO_FORMAT)
Definition math.hpp:132
constexpr bool is_eigen_dense_v
Definition math.hpp:80
constexpr bool is_eigen_v
Definition math.hpp:74
constexpr bool is_tpl_base_of_v
Definition math.hpp:69
std::enable_if_t< is_eigen_dense_v< T >, T2 > enable_if_eigen_dense_t
Definition math.hpp:83
std::enable_if_t< is_eigen_v< std::decay_t< T > > > enable_if_eigen_t
Definition math.hpp:77
constexpr bool is_eigen_matrix_v
Definition math.hpp:86
static constexpr std::false_type f(const void *)
static constexpr bool value
Definition math.hpp:64
static constexpr std::true_type f(const Base< OtherDerived > *)
Typedefs for math (Eigen vectors, matrices) depending on scalar type.
Definition math.hpp:123
ALIGATOR_DYNAMIC_TYPEDEFS(_Scalar)