alpaqa 1.0.0a15
Nonconvex constrained optimization
Loading...
Searching...
No Matches
l1-norm.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <cassert>
6#include <cmath>
7#include <stdexcept>
8
10
11/// ℓ₁-norm.
12/// @ingroup grp_Functions
13/// @tparam Weight
14/// Type of weighting factors. Either scalar or vector.
15template <Config Conf, class Weight = typename Conf::real_t>
16 requires(std::is_same_v<Weight, typename Conf::real_t> ||
17 std::is_same_v<Weight, typename Conf::vec> ||
18 std::is_same_v<Weight, typename Conf::rvec> ||
19 std::is_same_v<Weight, typename Conf::crvec>)
20struct L1Norm {
23 static constexpr bool scalar_weight = std::is_same_v<weight_t, real_t>;
24
25 L1Norm(weight_t λ) : λ{std::move(λ)} {
26 const char *msg = "L1Norm::λ must be nonnegative";
27 if constexpr (scalar_weight) {
28 if (λ < 0 || !std::isfinite(λ))
29 throw std::invalid_argument(msg);
30 } else {
31 if ((λ.array() < 0).any() || !λ.allFinite())
32 throw std::invalid_argument(msg);
33 }
34 }
35
37 requires(scalar_weight)
38 : λ{1} {}
40 requires(!scalar_weight)
41 = default;
42
44
46 assert(in.cols() == 1);
47 assert(out.cols() == 1);
48 assert(in.size() == out.size());
49 const length_t n = in.size();
50 if constexpr (scalar_weight) {
51 assert(λ >= 0);
52 if (λ == 0) {
53 out = in;
54 return 0;
55 }
56 auto step = vec::Constant(n, λ * γ);
57 out = vec::Zero(n).cwiseMax(in - step).cwiseMin(in + step);
58 return λ * out.template lpNorm<1>();
59 } else {
60 if constexpr (std::is_same_v<weight_t, vec>)
61 if (λ.size() == 0)
62 λ = weight_t::Ones(n);
63 assert(λ.cols() == 1);
64 assert(in.size() == λ.size());
65 assert((λ.array() >= 0).all());
66 auto step = λ * γ;
67 out = vec::Zero(n).cwiseMax(in - step).cwiseMin(in + step);
68 return out.cwiseProduct(λ).template lpNorm<1>();
69 }
70 }
71
73 rmat out, real_t γ) {
74 return self.prox(std::move(in), std::move(out), γ);
75 }
76};
77
78} // namespace alpaqa::functions
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:56
std::decay_t< decltype(Tag)> tag_t
typename Conf::crmat crmat
Definition config.hpp:75
typename Conf::rmat rmat
Definition config.hpp:74
typename Conf::real_t real_t
Definition config.hpp:65
typename Conf::length_t length_t
Definition config.hpp:76
constexpr const auto inf
Definition config.hpp:85
real_t prox(crmat in, rmat out, real_t γ=1)
Definition l1-norm.hpp:45
friend real_t alpaqa_tag_invoke(tag_t< alpaqa::prox >, L1Norm &self, crmat in, rmat out, real_t γ)
Definition l1-norm.hpp:72