1 #include <catch2/catch_all.hpp>
13 TEST_CASE(
"Projector on positive quadrant",
"[utility][project]") {
16 SECTION(
"Real matrix") {
24 CHECK(expr(1, 1) == Approx(0));
25 CHECK(expr(3, 2) == Approx(0));
27 auto value = expr.eval();
28 CHECK(value(1, 1) == Approx(0));
29 CHECK(value(3, 2) == Approx(0));
30 value(1, 1) = input(1, 1);
31 value(3, 2) = input(3, 2);
32 CHECK(value.isApprox(input));
35 SECTION(
"Complex matrix") {
37 input.real()(1, 1) *= -1;
38 input.real()(3, 2) *= -1;
45 auto value = expr.eval();
46 CHECK(value.real()(1, 1) == Approx(0));
47 CHECK(value.real()(3, 2) == Approx(0));
48 value(1, 1) = input.real()(1, 1);
49 value(3, 2) = input.real()(3, 2);
50 CHECK(value.real().isApprox(input.real()));
51 CHECK(value.imag().isApprox(0e0 * input.real()));
59 SECTION(
"Real valued") {
61 input << 5, -6, 7, -8;
62 CHECK(
sopt::l1_norm(input, weight) == Approx(5 + 12 + 21 + 32));
64 SECTION(
"Complex valued") {
67 input << 5. + 5. * i, 6. + 6. * i, 7. + 7. * i, 8. + 8. * i;
68 CHECK(
sopt::l1_norm(input, weight) == Approx(std::sqrt(2) * (5 + 12 + 21 + 32)));
72 TEST_CASE(
"Soft threshhold",
"[utility][threshhold]") {
74 input << 1e1, 2e1, 3e1, 4e1, 1e4, 2e4;
76 SECTION(
"Single-valued threshhold") {
87 CHECK((
b -
a) == Approx(2 * (c -
b)));
90 SECTION(
"Multi-values threshhold") {
94 threshhold << 1.1e1, 1.1e1, 1e0, 4.5, 2.25, 2.26;
96 SECTION(
"Real input") {
98 CHECK(actual(0) == 0e0);
99 CHECK(actual(1) == input(1) - threshhold(1));
100 CHECK(actual(2) == input(2) + threshhold(2));
101 CHECK(actual(3) == input(3) - threshhold(3));
103 #ifdef CATCH_HAS_THROWS_AS
107 SECTION(
"Complex input") {
109 CHECK(actual(0) == 0e0);
110 CHECK(actual(1) == input(1) - threshhold(1));
111 CHECK(actual(2) == input(2) + threshhold(2));
112 CHECK(actual(3) == input(3) - threshhold(3));
114 #ifdef CATCH_HAS_THROWS_AS
123 t_Vector const input = t_Vector::Random(12);
128 sampling(down, input);
129 CHECK(down.size() == 4);
130 CHECK(down(0) == input(1));
131 CHECK(down(1) == input(3));
132 CHECK(down(2) == input(6));
133 CHECK(down(3) == input(5));
135 t_Vector up = t_Vector::Zero(input.size());
136 sampling.adjoint(up, down);
137 CHECK(up(1) == input(1));
138 CHECK(up(3) == input(3));
139 CHECK(up(6) == input(6));
140 CHECK(up(5) == input(5));
145 CHECK(up == t_Vector::Zero(up.size()));
148 TEST_CASE(
"Relative variation",
"[utility][convergence]") {
152 CHECK(not relvar(input));
153 CHECK(relvar(input));
158 TEST_CASE(
"Scalar elative variation",
"[utility][convergence]") {
161 CHECK(not relvar(input));
162 CHECK(relvar(input));
163 CHECK(not relvar(input + 0.1));
172 stddev += std::real(std::conj(input(i) - mean) * (input(i) - mean));
173 stddev = std::sqrt(stddev) / std::sqrt(
static_cast<sopt::t_real>(input.size()));
188 static_assert(
sopt::is_complex<std::complex<double>>::value,
"Testing is_complex");
189 static_assert(
sopt::is_complex<std::complex<int>>::value,
"Testing is_complex");
sopt::Vector< Scalar > t_Vector
Real tolerance() const
Allowed variation.
An operator that samples a set of measurements.
Real relative_tolerance() const
Allowed variation.
Computes inner-most element type.
TEST_CASE("Projector on positive quadrant", "[utility][project]")
real_type< typename T::Scalar >::type standard_deviation(Eigen::ArrayBase< T > const &x)
Computes the standard deviation of a vector.
Eigen::CwiseUnaryOp< const details::ProjectPositiveQuadrant< typename T::Scalar >, const T > positive_quadrant(Eigen::DenseBase< T > const &input)
Expression to create projection onto positive quadrant.
std::enable_if< std::is_arithmetic< SCALAR >::value or is_complex< SCALAR >::value, SCALAR >::type soft_threshhold(SCALAR const &x, typename real_type< SCALAR >::type const &threshhold)
abs(x) < threshhold ? 0: x - sgn(x) * threshhold
double t_real
Root of the type hierarchy for real numbers.
Eigen::Array< T, Eigen::Dynamic, Eigen::Dynamic > Image
A 2-dimensional list of elements of given type.
Eigen::Array< T, Eigen::Dynamic, 1 > Array
A 1-dimensional list of elements of given type.
Eigen::Matrix< T, Eigen::Dynamic, 1 > Vector
A vector of a given type.
std::complex< t_real > t_complex
Root of the type hierarchy for (real) complex numbers.
real_type< typename T0::Scalar >::type l1_norm(Eigen::ArrayBase< T0 > const &input, Eigen::ArrayBase< T1 > const &weights)
Computes weighted L1 norm.
True if underlying type is complex.