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>
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>
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())
43#define ALIGATOR_EIGEN_ALLOW_MALLOC(allowed)
44#define ALIGATOR_NOMALLOC_SCOPED
45#define ALIGATOR_NOMALLOC_RESTORE
49#define ALIGATOR_NOMALLOC_BEGIN ALIGATOR_EIGEN_ALLOW_MALLOC(false)
51#define ALIGATOR_NOMALLOC_END ALIGATOR_EIGEN_ALLOW_MALLOC(true)
57 std::is_base_of_v<Eigen::DenseBase<T>, T>;
61 std::is_base_of_v<Eigen::MatrixBase<T>, T>;
63template <
typename T,
typename T2 =
void>
66#ifdef ALIGATOR_EIGEN_CHECK_MALLOC
68thread_local static bool g_cached_malloc_status =
true;
70inline void set_malloc_status(
bool status) { g_cached_malloc_status = status; }
72inline void save_malloc_status() {
74#ifdef ALIGATOR_EIGEN_CHECK_MALLOC
75 ::Eigen::internal::is_malloc_allowed()
82inline bool get_cached_malloc_status() {
return g_cached_malloc_status; }
84struct scoped_nomalloc {
85 scoped_nomalloc(
const scoped_nomalloc &) =
delete;
86 scoped_nomalloc(scoped_nomalloc &&) =
delete;
110 const std::string &text,
114 int i = int(text.length()) - 1;
116 if (text[
size_t(i)] !=
'\n')
120 return mat.derived().format(ft);
126template <
typename MatType>
127typename MatType::Scalar
infty_norm(
const Eigen::MatrixBase<MatType> &z) {
128 if (z.rows() == 0 || z.cols() == 0) {
131 return z.template lpNorm<Eigen::Infinity>();
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++) {
146template <
typename Scalar>
inline bool check_scalar(
const Scalar value) {
147 return std::isnan(value) || std::isinf(value);
152 const std::size_t n = xs.size();
153 for (std::size_t i = 0; i < n; i++) {
160template <typename T, typename = std::enable_if_t<std::is_scalar<T>::value>>
162 static_assert(std::is_scalar<T>::value,
"Parameter T should be scalar.");
166template <
typename MatrixType>
168 return (x.hasNaN() || (!x.allFinite()));
174template <
typename Scalar>
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)));
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)));
186template <
typename Derived,
unsigned int UpLo = Eigen::Lower>
188 Derived &mat = matrix.const_cast_derived();
190 Eigen::SelfAdjointView<Derived, UpLo> view{mat};
194template <
typename T>
void setZero(std::vector<T> &mats) {
195 for (std::size_t i = 0; i < mats.size(); i++) {
201template <
typename A,
typename B,
typename OutType,
typename Scalar>
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];
#define EIGEN_DEFAULT_IO_FORMAT
#define ALIGATOR_NOMALLOC_RESTORE
#define ALIGATOR_EIGEN_ALLOW_MALLOC(allowed)
void setZero(std::vector< T > &mats)
MatType::Scalar infty_norm(const Eigen::MatrixBase< MatType > &z)
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.
void make_symmetric(const Eigen::MatrixBase< Derived > &matrix)
Symmetrize a matrix using its lower triangular part.
bool check_scalar(const Scalar value)
Check that a scalar is neither inf, nor NaN.
bool check_value(const std::vector< T > &xs)
Check if a std::vector of numerical objects has invalid values.
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.
constexpr bool is_eigen_dense_type
std::enable_if_t< is_eigen_dense_type< T >, T2 > enable_if_eigen_dense
constexpr bool is_eigen_matrix_type
auto eigenPrintWithPreamble(const Eigen::EigenBase< D > &mat, const std::string &text, Eigen::IOFormat ft=EIGEN_DEFAULT_IO_FORMAT)
Typedefs for math (Eigen vectors, matrices) depending on scalar type.
ALIGATOR_DYNAMIC_TYPEDEFS(_Scalar)