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