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