alpaqa 1.0.0a18
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};
168
169/// Single-precision `float` configuration.
170struct EigenConfigf : EigenConfig<float> {
171 static constexpr const char *get_name() { return "EigenConfigf"; }
172};
173/// Double-precision `double` configuration.
174struct EigenConfigd : EigenConfig<double> {
175 static constexpr const char *get_name() { return "EigenConfigd"; }
176};
177/// `long double` configuration. (Quad precision on ARM64, 80-bit x87 floats
178/// on Intel/AMD x86)
179struct EigenConfigl : EigenConfig<long double> {
180 static constexpr const char *get_name() { return "EigenConfigl"; }
181};
182#ifdef ALPAQA_WITH_QUAD_PRECISION
183/// Quad-precision `__float128` configuration.
184struct EigenConfigq : EigenConfig<__float128> {
185 static constexpr const char *get_name() { return "EigenConfigq"; }
186};
187#endif
188
189/// Global empty vector for convenience.
190template <Config Conf>
191inline const rvec<Conf> null_vec = mvec<Conf>{nullptr, 0};
192/// Global empty index vector for convenience.
193template <Config Conf>
195
196namespace vec_util {
197
198/// Get the maximum or infinity-norm of the given vector.
199/// @returns @f$ \left\|v\right\|_\infty @f$
200template <class Derived>
201 requires(Derived::ColsAtCompileTime == 1)
202auto norm_inf(const Eigen::MatrixBase<Derived> &v) {
203 return v.template lpNorm<Eigen::Infinity>();
204}
205
206/// Get the 1-norm of the given vector.
207/// @returns @f$ \left\|v\right\|_1 @f$
208template <class Derived>
209 requires(Derived::ColsAtCompileTime == 1)
210auto norm_1(const Eigen::MatrixBase<Derived> &v) {
211 return v.template lpNorm<1>();
212}
213
214} // namespace vec_util
215
216} // namespace alpaqa
217
218#ifdef ALPAQA_WITH_QUAD_PRECISION
219#define ALPAQA_IF_QUADF(...) __VA_ARGS__
220#else
221#define ALPAQA_IF_QUADF(...)
222#endif
223
224#ifdef ALPAQA_WITH_SINGLE_PRECISION
225#define ALPAQA_IF_FLOAT(...) __VA_ARGS__
226#else
227#define ALPAQA_IF_FLOAT(...)
228#endif
229
230#ifdef ALPAQA_WITH_LONG_DOUBLE
231#define ALPAQA_IF_LONGD(...) __VA_ARGS__
232#else
233#define ALPAQA_IF_LONGD(...)
234#endif
auto norm_inf(const Eigen::MatrixBase< Derived > &v)
Get the maximum or infinity-norm of the given vector.
Definition config.hpp:202
auto norm_1(const Eigen::MatrixBase< Derived > &v)
Get the 1-norm of the given vector.
Definition config.hpp:210
typename Conf::mvec mvec
Definition config.hpp:89
Eigen::MatrixX< cplx_t > cmat
Dynamic complex matrix type.
Definition config.hpp:144
typename Conf::mat mat
Definition config.hpp:93
Eigen::Ref< const vec > crvec
Reference to immutable vector.
Definition config.hpp:132
typename Conf::crcmat crcmat
Definition config.hpp:102
Eigen::Map< mat > mmat
Map of matrix type.
Definition config.hpp:136
typename Conf::crmat crmat
Definition config.hpp:97
Eigen::Ref< const mat > crmat
Reference to immutable matrix.
Definition config.hpp:142
typename Conf::rmat rmat
Definition config.hpp:96
Eigen::Map< vec > mvec
Map of vector type.
Definition config.hpp:126
typename Conf::mindexvec mindexvec
Definition config.hpp:108
typename Conf::indexvec indexvec
Definition config.hpp:105
Eigen::Ref< const indexvec > crindexvec
Reference to immutable index vector.
Definition config.hpp:162
constexpr bool is_eigen_config_v
Definition config.hpp:25
const rindexvec< Conf > null_indexvec
Global empty index vector for convenience.
Definition config.hpp:194
typename Conf::cmcmat cmcmat
Definition config.hpp:100
constexpr bool is_config_v
Definition config.hpp:17
Eigen::MatrixX< real_t > mat
Dynamic matrix type.
Definition config.hpp:134
typename Conf::cmmat cmmat
Definition config.hpp:95
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
typename Conf::real_t real_t
Definition config.hpp:86
const rvec< Conf > null_vec
Global empty vector for convenience.
Definition config.hpp:191
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
Eigen::Index index_t
Type for vector and matrix indices.
Definition config.hpp:156
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
Eigen::VectorX< real_t > vec
Dynamic vector type.
Definition config.hpp:124
typename Conf::cmvec cmvec
Definition config.hpp:90
Eigen::Ref< mat > rmat
Reference to mutable matrix.
Definition config.hpp:140
constexpr const auto inf
Definition config.hpp:112
Eigen::Index length_t
Type for lengths and sizes.
Definition config.hpp:154
typename Conf::rvec rvec
Definition config.hpp:91
Eigen::Map< indexvec > mindexvec
Map of index vector type.
Definition config.hpp:164
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
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
typename Conf::crindexvec crindexvec
Definition config.hpp:107
Double-precision double configuration.
Definition config.hpp:174
static constexpr const char * get_name()
Definition config.hpp:175
Single-precision float configuration.
Definition config.hpp:170
static constexpr const char * get_name()
Definition config.hpp:171
long double configuration.
Definition config.hpp:179
static constexpr const char * get_name()
Definition config.hpp:180