alpaqa 1.0.0a9
Nonconvex constrained optimization
Loading...
Searching...
No Matches
box.hpp
Go to the documentation of this file.
1#pragma once
2
5
6namespace alpaqa {
7
8template <Config Conf = DefaultConfig>
9struct Box {
11
12 Box() : Box{0} {}
14 : lowerbound{vec::Constant(n, -inf<config_t>)},
15 upperbound{vec::Constant(n, +inf<config_t>)} {}
16
17 static Box NaN(length_t n) {
18 return Box{vec::Constant(n, alpaqa::NaN<config_t>),
19 vec::Constant(n, alpaqa::NaN<config_t>)};
20 }
21 static Box from_lower_upper(vec lower, vec upper) {
22 return Box{std::move(lower), std::move(upper)};
23 }
24
27
28 private:
29 Box(vec lower, vec upper) : lowerbound{std::move(lower)}, upperbound{std::move(upper)} {}
30};
31
32/// Project a vector onto a box.
33/// @f[ \Pi_C(v) @f]
34template <Config Conf>
35inline auto project(const auto &v, ///< [in] The vector to project
36 const Box<Conf> &box ///< [in] The box to project onto
37) {
38 return v.cwiseMax(box.lowerbound).cwiseMin(box.upperbound);
39}
40
41/// Get the difference between the given vector and its projection.
42/// @f[ v - \Pi_C(v) @f]
43/// @warning Beware catastrophic cancellation!
44template <Config Conf>
45inline auto //
46projecting_difference(const auto &v, ///< [in] The vector to project
47 const Box<Conf> &box ///< [in] The box to project onto
48) {
49 return v - project(v, box);
50}
51
52/// Get the distance squared between the given vector and its projection.
53/// @f[ \left\| v - \Pi_C(v) \right\|_2^2 @f]
54/// @warning Beware catastrophic cancellation!
55
56template <Config Conf>
57inline auto dist_squared(const auto &v, ///< [in] The vector to project
58 const Box<Conf> &box ///< [in] The box to project onto
59) {
60 return projecting_difference(v, box).squaredNorm();
61}
62
63/// Get the distance squared between the given vector and its projection in the
64/// Σ norm.
65/// @f[ \left\| v - \Pi_C(v) \right\|_\Sigma^2
66/// = \left(v - \Pi_C(v)\right)^\top \Sigma \left(v - \Pi_C(v)\right) @f]
67/// @warning Beware catastrophic cancellation!
68template <Config Conf>
69inline auto dist_squared(const auto &v, ///< [in] The vector to project
70 const Box<Conf> &box, ///< [in] The box to project onto
71 const auto///< [in] Diagonal matrix defining norm
72 ) -> real_t<Conf> {
73 // TODO: Does this allocate?
74 // Does it have dangling references to temporaries?
75 auto d = v - project(v, box);
76 return d.dot(Σ.asDiagonal() * d);
77}
78
79} // namespace alpaqa
#define USING_ALPAQA_CONFIG(Conf)
Definition: config.hpp:54
auto projecting_difference(const auto &v, const Box< Conf > &box)
Get the difference between the given vector and its projection.
Definition: box.hpp:46
typename Conf::real_t real_t
Definition: config.hpp:63
typename Conf::length_t length_t
Definition: config.hpp:74
constexpr const auto inf
Definition: config.hpp:81
typename Conf::vec vec
Definition: config.hpp:64
auto project(const auto &v, const Box< Conf > &box)
Project a vector onto a box.
Definition: box.hpp:35
auto dist_squared(const auto &v, const Box< Conf > &box)
Get the distance squared between the given vector and its projection.
Definition: box.hpp:57
static Box from_lower_upper(vec lower, vec upper)
Definition: box.hpp:21
Box(vec lower, vec upper)
Definition: box.hpp:29
vec upperbound
Definition: box.hpp:26
static Box NaN(length_t n)
Definition: box.hpp:17
vec lowerbound
Definition: box.hpp:25
Box(length_t n)
Definition: box.hpp:13