alpaqa pi-pico
Nonconvex constrained optimization
Loading...
Searching...
No Matches
nuclear-norm.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <Eigen/SVD>
5
6namespace alpaqa::functions {
7
8#if EIGEN_VERSION_AT_LEAST(3, 4, 1)
9template <Config Conf>
10using DefaultSVD = Eigen::BDCSVD<typename Conf::mat,
11 Eigen::ComputeThinU | Eigen::ComputeThinV>;
12#else
13template <Config Conf>
14using DefaultSVD = Eigen::BDCSVD<typename Conf::mat>;
15#endif
16
17/// Nuclear norm (ℓ₁-norm of singular values).
18/// @ingroup grp_Functions
19template <Config Conf, class SVD = DefaultSVD<Conf>>
22
23 /// Construct without pre-allocation.
25 if (λ < 0 || !std::isfinite(λ))
26 throw std::invalid_argument("NuclearNorm::λ must be nonnegative");
27 }
28 /// Construct with pre-allocation.
30 : λ{λ}, rows{rows}, cols{cols},
32 svd{rows, cols},
33#else
35#endif
37 if (λ < 0 || !std::isfinite(λ))
38 throw std::invalid_argument("NuclearNorm::λ must be nonnegative");
39 }
40
42 length_t rows = 0, cols = 0;
45
46 real_t prox(crmat in, rmat out, real_t γ = 1) {
47 if (λ == 0) {
48 out = in;
49 return 0;
50 }
51 if (rows == 0 || cols == 0) { // dynamic size
52 assert(in.rows() == out.rows());
53 assert(in.cols() == out.cols());
54#if EIGEN_VERSION_AT_LEAST(3, 4, 1)
55 svd.compute(in);
56#else
57 svd.compute(in, Eigen::ComputeThinU | Eigen::ComputeThinV);
58#endif
59 } else { // fixed size
60 assert(in.size() == rows * cols);
61 assert(out.size() == rows * cols);
62#if EIGEN_VERSION_AT_LEAST(3, 4, 1)
63 svd.compute(in.reshaped(rows, cols));
64#else
65 svd.compute(in.reshaped(rows, cols),
66 Eigen::ComputeThinU | Eigen::ComputeThinV);
67#endif
68 }
69 const length_t n = svd.singularValues().size();
70 auto step = vec::Constant(n, λ * γ);
71 singular_values = vec::Zero(n).cwiseMax(svd.singularValues() - step);
72 using vec_util::norm_1;
73 real_t value = λ * norm_1(singular_values);
74 auto it0 = std::find(singular_values.begin(), singular_values.end(), 0);
75 index_t rank = it0 - singular_values.begin();
76 using Eigen::placeholders::all, Eigen::seqN;
77 auto sel = seqN(0, rank);
78 auto &&U = svd.matrixU(), &&V = svd.matrixV();
79 auto &&U1 = U(all, sel);
80 auto &&Σ1 = singular_values(sel).asDiagonal();
81 auto &&V1T = V.transpose()(sel, all);
82 out.reshaped().noalias() = (U1 * Σ1 * V1T).reshaped();
83 return value;
84 }
85
87 crmat in, rmat out, real_t γ) {
88 return self.prox(std::move(in), std::move(out), γ);
89 }
90};
91
92} // namespace alpaqa::functions
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:77
Eigen::BDCSVD< typename Conf::mat > DefaultSVD
auto norm_1(const Eigen::MatrixBase< Derived > &v)
Get the 1-norm of the given vector.
Definition config.hpp:212
std::decay_t< decltype(Tag)> tag_t
typename Conf::crmat crmat
Definition config.hpp:97
typename Conf::rmat rmat
Definition config.hpp:96
typename Conf::real_t real_t
Definition config.hpp:86
typename Conf::index_t index_t
Definition config.hpp:104
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
Nuclear norm (ℓ₁-norm of singular values).
real_t prox(crmat in, rmat out, real_t γ=1)
NuclearNorm(real_t λ=1)
Construct without pre-allocation.
NuclearNorm(real_t λ, length_t rows, length_t cols)
Construct with pre-allocation.
friend real_t alpaqa_tag_invoke(tag_t< alpaqa::prox >, NuclearNorm &self, crmat in, rmat out, real_t γ)