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