aligator  0.14.0
A primal-dual augmented Lagrangian-type solver for nonlinear 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
8#define ALIGATOR_DYNAMIC_TYPEDEFS(Scalar) \
9 using VectorXs = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>; \
10 using MatrixXs = Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>; \
11 using VectorOfVectors = std::vector<VectorXs>; \
12 using VectorRef = Eigen::Ref<VectorXs>; \
13 using MatrixRef = Eigen::Ref<MatrixXs>; \
14 using ConstVectorRef = Eigen::Ref<const VectorXs>; \
15 using ConstMatrixRef = Eigen::Ref<const MatrixXs>; \
16 using Vector3s = Eigen::Matrix<Scalar, 3, 1>; \
17 using Vector6s = Eigen::Matrix<Scalar, 6, 1>; \
18 using Matrix3Xs = Eigen::Matrix<Scalar, 3, Eigen::Dynamic>; \
19 using Matrix6Xs = Eigen::Matrix<Scalar, 6, Eigen::Dynamic>; \
20 using Matrix6s = Eigen::Matrix<Scalar, 6, 6>
21
22#define ALIGATOR_DYNAMIC_TYPEDEFS_WITH_ROW_TYPES(Scalar) \
23 ALIGATOR_DYNAMIC_TYPEDEFS(Scalar); \
24 using RowMatrixXs = typename Eigen::Transpose<MatrixXs>::PlainObject; \
25 using RowMatrixRef = Eigen::Ref<RowMatrixXs>; \
26 using ConstRowMatrixRef = Eigen::Ref<const RowMatrixXs>
27
28#ifdef ALIGATOR_EIGEN_CHECK_MALLOC
31#define ALIGATOR_EIGEN_ALLOW_MALLOC(allowed) \
32 ::Eigen::internal::set_is_malloc_allowed(allowed)
37#define ALIGATOR_NOMALLOC_SCOPED \
38 const ::aligator::internal::scoped_nomalloc ___aligator_nomalloc_zone {}
40#define ALIGATOR_NOMALLOC_RESTORE \
41 ALIGATOR_EIGEN_ALLOW_MALLOC(::aligator::internal::get_cached_malloc_status())
42#else
43#define ALIGATOR_EIGEN_ALLOW_MALLOC(allowed)
44#define ALIGATOR_NOMALLOC_SCOPED
45#define ALIGATOR_NOMALLOC_RESTORE
46#endif
47
49#define ALIGATOR_NOMALLOC_BEGIN ALIGATOR_EIGEN_ALLOW_MALLOC(false)
51#define ALIGATOR_NOMALLOC_END ALIGATOR_EIGEN_ALLOW_MALLOC(true)
52
53namespace aligator {
54
55template <typename T>
56inline constexpr bool is_eigen_dense_type =
57 std::is_base_of_v<Eigen::DenseBase<T>, T>;
58
59template <typename T>
60inline constexpr bool is_eigen_matrix_type =
61 std::is_base_of_v<Eigen::MatrixBase<T>, T>;
62
63template <typename T, typename T2 = void>
64using enable_if_eigen_dense = std::enable_if_t<is_eigen_dense_type<T>, T2>;
65
66#ifdef ALIGATOR_EIGEN_CHECK_MALLOC
67namespace internal {
68thread_local static bool g_cached_malloc_status = true;
69
70inline void set_malloc_status(bool status) { g_cached_malloc_status = status; }
71
72inline void save_malloc_status() {
73 set_malloc_status(
74#ifdef ALIGATOR_EIGEN_CHECK_MALLOC
75 ::Eigen::internal::is_malloc_allowed()
76#else
77 false
78#endif
79 );
80}
81
82inline bool get_cached_malloc_status() { return g_cached_malloc_status; }
83
84struct scoped_nomalloc {
85 scoped_nomalloc(const scoped_nomalloc &) = delete;
86 scoped_nomalloc(scoped_nomalloc &&) = delete;
87 ALIGATOR_INLINE scoped_nomalloc() {
88 save_malloc_status();
90 }
91 // reset to value from before the scope
92 ~scoped_nomalloc() { ALIGATOR_NOMALLOC_RESTORE; }
93};
94
95} // namespace internal
96#endif
97
100template <typename _Scalar> struct math_types {
101 using Scalar = _Scalar;
103};
104
108template <typename D>
109auto eigenPrintWithPreamble(const Eigen::EigenBase<D> &mat,
110 const std::string &text,
111 Eigen::IOFormat ft = EIGEN_DEFAULT_IO_FORMAT) {
112 ft.matPrefix = text;
113 ft.rowSpacer = "";
114 int i = int(text.length()) - 1;
115 while (i >= 0) {
116 if (text[size_t(i)] != '\n')
117 ft.rowSpacer += ' ';
118 i--;
119 }
120 return mat.derived().format(ft);
121}
122
124namespace math {
125
126template <typename MatType>
127typename MatType::Scalar infty_norm(const Eigen::MatrixBase<MatType> &z) {
128 if (z.rows() == 0 || z.cols() == 0) {
129 return 0.;
130 } else {
131 return z.template lpNorm<Eigen::Infinity>();
132 }
133}
134
135template <typename MatType>
136typename MatType::Scalar infty_norm(const std::vector<MatType> &z) {
137 const std::size_t n = z.size();
138 typename MatType::Scalar out = 0.;
139 for (std::size_t i = 0; i < n; i++) {
140 out = std::max(out, infty_norm(z[i]));
141 }
142 return out;
143}
144
146template <typename Scalar> inline bool check_scalar(const Scalar value) {
147 return std::isnan(value) || std::isinf(value);
148}
149
151template <typename T> bool check_value(const std::vector<T> &xs) {
152 const std::size_t n = xs.size();
153 for (std::size_t i = 0; i < n; i++) {
154 if (check_value<T>(xs[i]))
155 return true;
156 }
157 return false;
158}
159
160template <typename T, typename = std::enable_if_t<std::is_scalar<T>::value>>
161bool check_value(const T &x) {
162 static_assert(std::is_scalar<T>::value, "Parameter T should be scalar.");
163 return check_scalar(x);
164}
165
166template <typename MatrixType>
167bool check_value(const Eigen::MatrixBase<MatrixType> &x) {
168 return (x.hasNaN() || (!x.allFinite()));
169}
170
174template <typename Scalar>
175bool scalar_close(const Scalar a, const Scalar b,
176 const Scalar prec = std::numeric_limits<Scalar>::epsilon()) {
177 return std::abs(a - b) < prec * (1 + std::max(std::abs(a), std::abs(b)));
178}
179
180template <typename T> T sign(const T &x) {
181 static_assert(std::is_scalar<T>::value, "Parameter T should be scalar.");
182 return T((x > T(0)) - (x < T(0)));
183}
184
186template <typename Derived, unsigned int UpLo = Eigen::Lower>
187void make_symmetric(const Eigen::MatrixBase<Derived> &matrix) {
188 Derived &mat = matrix.const_cast_derived();
189 // symmetrize upper part
190 Eigen::SelfAdjointView<Derived, UpLo> view{mat};
191 mat = view;
192}
193
194template <typename T> void setZero(std::vector<T> &mats) {
195 for (std::size_t i = 0; i < mats.size(); i++) {
196 mats[i].setZero();
197 }
198}
199
201template <typename A, typename B, typename OutType, typename Scalar>
202void vectorMultiplyAdd(const std::vector<A> &a, const std::vector<B> &b,
203 std::vector<OutType> &c, const Scalar alpha) {
204 assert(a.size() == b.size());
205 assert(a.size() == c.size());
206 const std::size_t N = a.size();
207 for (std::size_t i = 0; i < N; i++) {
208 c[i] = a[i] + alpha * b[i];
209 }
210}
211
212} // namespace math
213} // 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:45
#define ALIGATOR_EIGEN_ALLOW_MALLOC(allowed)
Definition math.hpp:43
Math utilities.
Definition math.hpp:124
void setZero(std::vector< T > &mats)
Definition math.hpp:194
MatType::Scalar infty_norm(const Eigen::MatrixBase< MatType > &z)
Definition math.hpp:127
void vectorMultiplyAdd(const std::vector< A > &a, const std::vector< B > &b, std::vector< OutType > &c, const Scalar alpha)
Compute zi = xi + alpha * yi for all i.
Definition math.hpp:202
void make_symmetric(const Eigen::MatrixBase< Derived > &matrix)
Symmetrize a matrix using its lower triangular part.
Definition math.hpp:187
bool check_scalar(const Scalar value)
Check that a scalar is neither inf, nor NaN.
Definition math.hpp:146
bool check_value(const std::vector< T > &xs)
Check if a std::vector of numerical objects has invalid values.
Definition math.hpp:151
T sign(const T &x)
Definition math.hpp:180
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:175
Main package namespace.
constexpr bool is_eigen_dense_type
Definition math.hpp:56
std::enable_if_t< is_eigen_dense_type< T >, T2 > enable_if_eigen_dense
Definition math.hpp:64
constexpr bool is_eigen_matrix_type
Definition math.hpp:60
auto eigenPrintWithPreamble(const Eigen::EigenBase< D > &mat, const std::string &text, Eigen::IOFormat ft=EIGEN_DEFAULT_IO_FORMAT)
Definition math.hpp:109
Typedefs for math (Eigen vectors, matrices) depending on scalar type.
Definition math.hpp:100
ALIGATOR_DYNAMIC_TYPEDEFS(_Scalar)