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