proxsuite-nlp  0.10.0
A primal-dual augmented Lagrangian-type solver for nonlinear programming on manifolds.
Loading...
Searching...
No Matches
math.hpp
Go to the documentation of this file.
1
3#pragma once
4
5#include <Eigen/Core>
6#include <limits>
7#include <vector>
8
9namespace proxsuite {
10namespace nlp {
11
12template <typename T>
13constexpr bool is_eigen_dense_type =
14 std::is_base_of<Eigen::DenseBase<T>, T>::value;
15
16template <typename T>
17constexpr bool is_eigen_matrix_type =
18 std::is_base_of<Eigen::MatrixBase<T>, T>::value;
19
20template <typename T, typename T2 = void>
21using enable_if_eigen_dense = std::enable_if_t<is_eigen_dense_type<T>, T2>;
22
26#define PROXSUITE_NLP_DYNAMIC_TYPEDEFS(Scalar) \
27 using VectorXs = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>; \
28 using MatrixXs = Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>; \
29 using VectorOfVectors = std::vector<VectorXs>; \
30 using VectorRef = Eigen::Ref<VectorXs>; \
31 using MatrixRef = Eigen::Ref<MatrixXs>; \
32 using ConstVectorRef = Eigen::Ref<const VectorXs>; \
33 using ConstMatrixRef = Eigen::Ref<const MatrixXs>; \
34 using VectorOfRef = std::vector<VectorRef>; \
35 using Vector3s = Eigen::Matrix<Scalar, 3, 1>; \
36 using Vector6s = Eigen::Matrix<Scalar, 6, 1>; \
37 using Matrix3Xs = Eigen::Matrix<Scalar, 3, Eigen::Dynamic>; \
38 using Matrix6Xs = Eigen::Matrix<Scalar, 6, Eigen::Dynamic>; \
39 using Matrix6s = Eigen::Matrix<Scalar, 6, 6>
40
43template <typename _Scalar> struct math_types {
44 using Scalar = _Scalar;
46};
47
49namespace math {
50template <typename MatType>
51typename MatType::Scalar infty_norm(const Eigen::MatrixBase<MatType> &z) {
52 if (z.rows() == 0 || z.cols() == 0) {
53 return 0.;
54 } else {
55 return z.template lpNorm<Eigen::Infinity>();
56 }
57}
58
59template <typename MatType>
60typename MatType::Scalar infty_norm(const std::vector<MatType> &z) {
61 const std::size_t n = z.size();
62 typename MatType::Scalar out = 0.;
63 for (std::size_t i = 0; i < n; i++) {
64 out = std::max(out, infty_norm(z[i]));
65 }
66 return out;
67}
68
70template <typename Scalar> inline bool check_scalar(const Scalar value) {
71 return std::isnan(value) || std::isinf(value);
72}
73
78template <typename Scalar>
79bool scalar_close(const Scalar a, const Scalar b,
80 const Scalar prec = std::numeric_limits<Scalar>::epsilon()) {
81 return std::abs(a - b) < prec * (1 + std::max(std::abs(a), std::abs(b)));
82}
83
84template <typename T, typename = std::enable_if_t<std::is_scalar<T>::value>>
85bool check_value(const T &x) {
86 static_assert(std::is_scalar<T>::value, "Parameter T should be scalar.");
87 return check_scalar(x);
88}
89
90template <typename MatrixType>
91bool check_value(const Eigen::MatrixBase<MatrixType> &x) {
92 return (x.hasNaN() || (!x.allFinite()));
93}
94
95template <typename T> T sign(const T &x) {
96 static_assert(std::is_scalar<T>::value, "Parameter T should be scalar.");
97 return T((x > T(0)) - (x < T(0)));
98}
99
100} // namespace math
101} // namespace nlp
102} // namespace proxsuite
#define PROXSUITE_NLP_DYNAMIC_TYPEDEFS(Scalar)
Definition math.hpp:26
T sign(const T &x)
Definition math.hpp:95
bool check_scalar(const Scalar value)
Check that a scalar is neither inf, nor NaN.
Definition math.hpp:70
MatType::Scalar infty_norm(const Eigen::MatrixBase< MatType > &z)
Definition math.hpp:51
bool check_value(const T &x)
Definition math.hpp:85
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:79
std::enable_if_t< is_eigen_dense_type< T >, T2 > enable_if_eigen_dense
Definition math.hpp:21
constexpr bool is_eigen_matrix_type
Definition math.hpp:17
constexpr bool is_eigen_dense_type
Definition math.hpp:13
Main package namespace.
Definition bcl-params.hpp:5
Typedefs for math (Eigen vectors, matrices) depending on scalar type.
Definition math.hpp:43