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>
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>
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())
44#define ALIGATOR_EIGEN_ALLOW_MALLOC(allowed)
45#define ALIGATOR_NOMALLOC_SCOPED
46#define ALIGATOR_NOMALLOC_RESTORE
50#define ALIGATOR_NOMALLOC_BEGIN ALIGATOR_EIGEN_ALLOW_MALLOC(false)
52#define ALIGATOR_NOMALLOC_END ALIGATOR_EIGEN_ALLOW_MALLOC(true)
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> *);
65 decltype(
f(std::declval<std::remove_reference_t<Derived> *>()))
::value;
68template <
template <
class>
class Base,
typename Derived>
72struct is_eigen : std::bool_constant<is_tpl_base_of_v<Eigen::EigenBase, T>> {};
82template <
typename T,
typename T2 =
void>
89#ifdef ALIGATOR_EIGEN_CHECK_MALLOC
91thread_local static bool g_cached_malloc_status =
true;
93inline void set_malloc_status(
bool status) { g_cached_malloc_status = status; }
95inline void save_malloc_status() {
97#ifdef ALIGATOR_EIGEN_CHECK_MALLOC
98 ::Eigen::internal::is_malloc_allowed()
105inline bool get_cached_malloc_status() {
return g_cached_malloc_status; }
107struct scoped_nomalloc {
108 scoped_nomalloc(
const scoped_nomalloc &) =
delete;
109 scoped_nomalloc(scoped_nomalloc &&) =
delete;
111 save_malloc_status();
133 std::string_view text,
137 int i = int(text.length()) - 1;
139 if (text[
size_t(i)] !=
'\n')
143 return mat.derived().format(ft);
149template <
typename MatType>
150typename MatType::Scalar
infty_norm(
const Eigen::MatrixBase<MatType> &z) {
151 if (z.rows() == 0 || z.cols() == 0) {
154 return z.template lpNorm<Eigen::Infinity>();
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++) {
169template <
typename Scalar>
inline bool check_scalar(
const Scalar value) {
170 return std::isnan(value) || std::isinf(value);
175 const std::size_t n = xs.size();
176 for (std::size_t i = 0; i < n; i++) {
183template <typename T, typename = std::enable_if_t<std::is_scalar<T>::value>>
185 static_assert(std::is_scalar<T>::value,
"Parameter T should be scalar.");
189template <
typename MatrixType>
191 return (x.hasNaN() || (!x.allFinite()));
197template <
typename Scalar>
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)));
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)));
209template <
typename Derived,
unsigned int UpLo = Eigen::Lower>
211 Derived &mat = matrix.const_cast_derived();
213 Eigen::SelfAdjointView<Derived, UpLo> view{mat};
217template <
typename T>
void setZero(std::vector<T> &mats) {
218 for (std::size_t i = 0; i < mats.size(); i++) {
224template <
typename TA,
typename AA,
typename TB,
typename BA,
typename OutType,
225 typename AOut,
typename Scalar>
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];
#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 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.
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.
auto eigenPrintWithPreamble(const Eigen::EigenBase< D > &mat, std::string_view text, Eigen::IOFormat ft=EIGEN_DEFAULT_IO_FORMAT)
constexpr bool is_eigen_dense_v
constexpr bool is_eigen_v
constexpr bool is_tpl_base_of_v
std::enable_if_t< is_eigen_dense_v< T >, T2 > enable_if_eigen_dense_t
std::enable_if_t< is_eigen_v< std::decay_t< T > > > enable_if_eigen_t
constexpr bool is_eigen_matrix_v
static constexpr std::false_type f(const void *)
static constexpr bool value
static constexpr std::true_type f(const Base< OtherDerived > *)
Typedefs for math (Eigen vectors, matrices) depending on scalar type.
ALIGATOR_DYNAMIC_TYPEDEFS(_Scalar)