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