alpaqa 1.0.0a9
Nonconvex constrained optimization
Loading...
Searching...
No Matches
config.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <limits>
7#include <type_traits>
8
9#include <Eigen/Core>
10
11namespace alpaqa {
12
13template <class T>
14struct is_config : std::false_type {};
15template <class T>
16inline constexpr bool is_config_v = is_config<T>::value;
17
18template <class Conf>
19concept Config = is_config_v<Conf>;
20
21struct EigenConfigd;
22struct EigenConfigf;
23struct EigenConfigl;
24struct EigenConfigq;
26
27template <>
28struct is_config<EigenConfigf> : std::true_type {};
29template <>
30struct is_config<EigenConfigd> : std::true_type {};
31template <>
32struct is_config<EigenConfigl> : std::true_type {};
33template <>
34struct is_config<EigenConfigq> : std::true_type {};
35
36#define USING_ALPAQA_CONFIG_NO_TYPENAME(Conf) \
37 using real_t [[maybe_unused]] = Conf::real_t; \
38 using vec [[maybe_unused]] = Conf::vec; \
39 using mvec [[maybe_unused]] = Conf::mvec; \
40 using cmvec [[maybe_unused]] = Conf::cmvec; \
41 using rvec [[maybe_unused]] = Conf::rvec; \
42 using crvec [[maybe_unused]] = Conf::crvec; \
43 using mat [[maybe_unused]] = Conf::mat; \
44 using mmat [[maybe_unused]] = Conf::mmat; \
45 using cmmat [[maybe_unused]] = Conf::cmmat; \
46 using rmat [[maybe_unused]] = Conf::rmat; \
47 using crmat [[maybe_unused]] = Conf::crmat; \
48 using length_t [[maybe_unused]] = Conf::length_t; \
49 using index_t [[maybe_unused]] = Conf::index_t; \
50 using indexvec [[maybe_unused]] = Conf::indexvec; \
51 using rindexvec [[maybe_unused]] = Conf::rindexvec; \
52 using crindexvec [[maybe_unused]] = Conf::crindexvec
53
54#define USING_ALPAQA_CONFIG(Conf) /** @cond CONFIG_TYPES */ \
55 using config_t [[maybe_unused]] = Conf; \
56 USING_ALPAQA_CONFIG_NO_TYPENAME(typename Conf) /** @endcond */
57
58#define USING_ALPAQA_CONFIG_TEMPLATE(Conf) /** @cond CONFIG_TYPES */ \
59 using config_t [[maybe_unused]] = typename Conf; \
60 USING_ALPAQA_CONFIG_NO_TYPENAME(typename Conf) /** @endcond */
61
62// clang-format off
63template <Config Conf = DefaultConfig> using real_t = typename Conf::real_t;
64template <Config Conf = DefaultConfig> using vec = typename Conf::vec;
65template <Config Conf = DefaultConfig> using mvec = typename Conf::mvec;
66template <Config Conf = DefaultConfig> using cmvec = typename Conf::cmvec;
67template <Config Conf = DefaultConfig> using rvec = typename Conf::rvec;
68template <Config Conf = DefaultConfig> using crvec = typename Conf::crvec;
69template <Config Conf = DefaultConfig> using mat = typename Conf::mat;
70template <Config Conf = DefaultConfig> using mmat = typename Conf::mmat;
71template <Config Conf = DefaultConfig> using cmmat = typename Conf::cmmat;
72template <Config Conf = DefaultConfig> using rmat = typename Conf::rmat;
73template <Config Conf = DefaultConfig> using crmat = typename Conf::crmat;
74template <Config Conf = DefaultConfig> using length_t = typename Conf::length_t;
75template <Config Conf = DefaultConfig> using index_t = typename Conf::index_t;
76template <Config Conf = DefaultConfig> using indexvec = typename Conf::indexvec;
77template <Config Conf = DefaultConfig> using rindexvec = typename Conf::rindexvec;
78template <Config Conf = DefaultConfig> using crindexvec = typename Conf::crindexvec;
79
80template <Config Conf>
81constexpr const auto inf = std::numeric_limits<real_t<Conf>>::infinity();
82template <Config Conf>
83constexpr const auto NaN = std::numeric_limits<real_t<Conf>>::quiet_NaN();
84// clang-format on
85
86template <class RealT>
88 /// Real scalar element type.
89 using real_t = RealT;
90 /// Dynamic vector type.
91 using vec = Eigen::VectorX<real_t>;
92 /// Map of vector type.
93 using mvec = Eigen::Map<vec>;
94 /// Immutable map of vector type.
95 using cmvec = Eigen::Map<const vec>;
96 /// Reference to mutable vector.
97 using rvec = Eigen::Ref<vec>;
98 /// Reference to immutable vector.
99 using crvec = Eigen::Ref<const vec>;
100 /// Dynamic matrix type.
101 using mat = Eigen::MatrixX<real_t>;
102 /// Map of matrix type.
103 using mmat = Eigen::Map<mat>;
104 /// Immutable map of matrix type.
105 using cmmat = Eigen::Map<const mat>;
106 /// Reference to mutable matrix.
107 using rmat = Eigen::Ref<mat>;
108 /// Reference to immutable matrix.
109 using crmat = Eigen::Ref<const mat>;
110 /// Type for lengths and sizes.
111 using length_t = Eigen::Index;
112 /// Type for vector and matrix indices.
113 using index_t = Eigen::Index;
114 /// Dynamic vector of indices.
115 using indexvec = Eigen::VectorX<index_t>;
116 /// Reference to mutable index vector.
117 using rindexvec = Eigen::Ref<indexvec>;
118 /// Reference to immutable index vector.
119 using crindexvec = Eigen::Ref<const indexvec>;
120};
121
122/// Single-precision `float` configuration.
123struct EigenConfigf : EigenConfig<float> {
124 static constexpr const char *get_name() { return "EigenConfigf"; }
125};
126/// Double-precision `double` configuration.
127struct EigenConfigd : EigenConfig<double> {
128 static constexpr const char *get_name() { return "EigenConfigd"; }
129};
130/// `long double` configuration. (Quad precision on ARM64, 80-bit x87 floats
131/// on Intel/AMD x86)
132struct EigenConfigl : EigenConfig<long double> {
133 static constexpr const char *get_name() { return "EigenConfigl"; }
134};
135#ifdef ALPAQA_WITH_QUAD_PRECISION
136/// Quad-precision `__float128` configuration.
137struct EigenConfigq : EigenConfig<__float128> {
138 static constexpr const char *get_name() { return "EigenConfigq"; }
139};
140#endif
141
142/// Global empty vector for convenience.
143template <Config Conf>
144inline const rvec<Conf> null_vec = mvec<Conf>{nullptr, 0};
145
146namespace vec_util {
147
148/// Get the maximum or infinity-norm of the given vector.
149/// @returns @f$ \left\|v\right\|_\infty @f$
150template <class Derived>
151 requires(Derived::ColsAtCompileTime == 1)
152auto norm_inf(const Eigen::MatrixBase<Derived> &v) {
153 return v.template lpNorm<Eigen::Infinity>();
154}
155
156/// Get the 1-norm of the given vector.
157/// @returns @f$ \left\|v\right\|_1 @f$
158template <class Derived>
159 requires(Derived::ColsAtCompileTime == 1)
160auto norm_1(const Eigen::MatrixBase<Derived> &v) {
161 return v.template lpNorm<1>();
162}
163
164} // namespace vec_util
165
166} // namespace alpaqa
167
168#ifdef ALPAQA_WITH_QUAD_PRECISION
169#define ALPAQA_IF_QUADF(...) __VA_ARGS__
170#else
171#define ALPAQA_IF_QUADF(...)
172#endif
173
174#ifdef ALPAQA_WITH_SINGLE_PRECISION
175#define ALPAQA_IF_FLOAT(...) __VA_ARGS__
176#else
177#define ALPAQA_IF_FLOAT(...)
178#endif
179
180#ifdef ALPAQA_WITH_LONG_DOUBLE
181#define ALPAQA_IF_LONGD(...) __VA_ARGS__
182#else
183#define ALPAQA_IF_LONGD(...)
184#endif
auto norm_inf(const Eigen::MatrixBase< Derived > &v)
Get the maximum or infinity-norm of the given vector.
Definition: config.hpp:152
auto norm_1(const Eigen::MatrixBase< Derived > &v)
Get the 1-norm of the given vector.
Definition: config.hpp:160
typename Conf::mvec mvec
Definition: config.hpp:65
typename Conf::mat mat
Definition: config.hpp:69
Eigen::Ref< const vec > crvec
Reference to immutable vector.
Definition: config.hpp:99
Eigen::Map< mat > mmat
Map of matrix type.
Definition: config.hpp:103
typename Conf::crmat crmat
Definition: config.hpp:73
Eigen::Ref< const mat > crmat
Reference to immutable matrix.
Definition: config.hpp:109
typename Conf::rmat rmat
Definition: config.hpp:72
Eigen::Map< vec > mvec
Map of vector type.
Definition: config.hpp:93
typename Conf::indexvec indexvec
Definition: config.hpp:76
Eigen::Ref< const indexvec > crindexvec
Reference to immutable index vector.
Definition: config.hpp:119
constexpr bool is_config_v
Definition: config.hpp:16
Eigen::MatrixX< real_t > mat
Dynamic matrix type.
Definition: config.hpp:101
typename Conf::cmmat cmmat
Definition: config.hpp:71
Eigen::VectorX< index_t > indexvec
Dynamic vector of indices.
Definition: config.hpp:115
typename Conf::real_t real_t
Definition: config.hpp:63
const rvec< Conf > null_vec
Global empty vector for convenience.
Definition: config.hpp:144
constexpr const auto NaN
Definition: config.hpp:83
typename Conf::rindexvec rindexvec
Definition: config.hpp:77
typename Conf::index_t index_t
Definition: config.hpp:75
Eigen::Index index_t
Type for vector and matrix indices.
Definition: config.hpp:113
typename Conf::mmat mmat
Definition: config.hpp:70
typename Conf::length_t length_t
Definition: config.hpp:74
Eigen::VectorX< real_t > vec
Dynamic vector type.
Definition: config.hpp:91
typename Conf::cmvec cmvec
Definition: config.hpp:66
Eigen::Ref< mat > rmat
Reference to mutable matrix.
Definition: config.hpp:107
constexpr const auto inf
Definition: config.hpp:81
Eigen::Index length_t
Type for lengths and sizes.
Definition: config.hpp:111
typename Conf::rvec rvec
Definition: config.hpp:67
typename Conf::crvec crvec
Definition: config.hpp:68
typename Conf::vec vec
Definition: config.hpp:64
RealT real_t
Real scalar element type.
Definition: config.hpp:89
Eigen::Map< const vec > cmvec
Immutable map of vector type.
Definition: config.hpp:95
Eigen::Map< const mat > cmmat
Immutable map of matrix type.
Definition: config.hpp:105
Eigen::Ref< indexvec > rindexvec
Reference to mutable index vector.
Definition: config.hpp:117
Eigen::Ref< vec > rvec
Reference to mutable vector.
Definition: config.hpp:97
typename Conf::crindexvec crindexvec
Definition: config.hpp:78
Double-precision double configuration.
Definition: config.hpp:127
static constexpr const char * get_name()
Definition: config.hpp:128
Single-precision float configuration.
Definition: config.hpp:123
static constexpr const char * get_name()
Definition: config.hpp:124
long double configuration.
Definition: config.hpp:132
static constexpr const char * get_name()
Definition: config.hpp:133