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