SOPT
Sparse OPTimisation
Functions
chained_operators.cc File Reference
#include <catch2/catch_all.hpp>
#include <complex>
#include <iomanip>
#include "sopt/chained_operators.h"
#include "sopt/linear_transform.h"
+ Include dependency graph for chained_operators.cc:

Go to the source code of this file.

Functions

 TEST_CASE ("Linear Transforms", "[ops]")
 

Function Documentation

◆ TEST_CASE()

TEST_CASE ( "Linear Transforms"  ,
""  [ops] 
)

Definition at line 8 of file chained_operators.cc.

8  {
9  using namespace sopt;
10 
11  using SCALAR = int;
12  using t_Vector = Vector<SCALAR>;
13  auto constexpr N = 5;
14 
15  SECTION("1 Functions") {
16  OperatorFunction<t_Vector> const func0 = [](t_Vector &out, t_Vector const &input) {
17  out = input.array() * 2 - 1;
18  };
19  OperatorFunction<t_Vector> const func1 = [](t_Vector &out, t_Vector const &input) {
20  out = input.array() * 4 - 1;
21  };
22  t_Vector const x = t_Vector::Random(2 * N) * 5;
23  auto chain = chained_operators(func0);
24 
25  t_Vector actual;
26  t_Vector expected;
27  func0(actual, x);
28  chain(expected, x);
29  CHECK(actual == expected);
30  }
31 
32  SECTION("2 Functions") {
33  OperatorFunction<t_Vector> const func0 = [](t_Vector &out, t_Vector const &input) {
34  out = input.array() * 2 - 1;
35  };
36  OperatorFunction<t_Vector> const func1 = [](t_Vector &out, t_Vector const &input) {
37  out = input.array() * 4 - 1;
38  };
39  t_Vector const x = t_Vector::Random(2 * N) * 5;
40  auto chain = chained_operators(func0, func1);
41 
42  t_Vector actual;
43  t_Vector expected;
44  func1(expected, x);
45  func0(actual, expected);
46  chain(expected, x);
47  CHECK(actual == expected);
48  }
49 
50  SECTION("3 Functions") {
51  OperatorFunction<t_Vector> const func0 = [](t_Vector &out, t_Vector const &input) {
52  out = input.array() * 2 - 1;
53  };
54  OperatorFunction<t_Vector> const func1 = [](t_Vector &out, t_Vector const &input) {
55  out = input.array() * 4 - 1;
56  };
57  t_Vector const x = t_Vector::Random(2 * N) * 5;
58  auto chain = chained_operators(func0, func1, func0);
59 
60  t_Vector actual;
61  t_Vector expected;
62  func0(actual, x);
63  func1(expected, actual);
64  func0(actual, expected);
65  chain(expected, x);
66  CHECK(actual == expected);
67  }
68 
69  SECTION("4 Functions") {
70  OperatorFunction<t_Vector> const func0 = [](t_Vector &out, t_Vector const &input) {
71  out = input.array() * 2 - 1;
72  };
73  OperatorFunction<t_Vector> const func1 = [](t_Vector &out, t_Vector const &input) {
74  out = input.array() * 4 - 1;
75  };
76  t_Vector const x = t_Vector::Random(2 * N) * 5;
77  auto chain = chained_operators(func0, func1, func0, func0);
78 
79  t_Vector actual;
80  t_Vector expected;
81  func0(expected, x);
82  func0(actual, expected);
83  func1(expected, actual);
84  func0(actual, expected);
85  chain(expected, x);
86  CHECK(actual == expected);
87  }
88 
89  SECTION("linear transform") {
90  OperatorFunction<t_Vector> const func0 = [](t_Vector &out, t_Vector const &input) {
91  out = input.head(N - 1).array() * 2;
92  };
93  OperatorFunction<t_Vector> const func1 = [](t_Vector &out, t_Vector const &input) {
94  out = input.head(N - 1).array() * 4;
95  };
96  OperatorFunction<t_Vector> const afunc0 = [](t_Vector &out, t_Vector const &input) {
97  out = t_Vector::Zero(N);
98  out.head(N - 1) = input.head(N - 1).array() * 2;
99  };
100  OperatorFunction<t_Vector> const afunc1 = [](t_Vector &out, t_Vector const &input) {
101  out = t_Vector::Zero(N);
102  out.head(N - 1) = input.head(N - 1).array() * 4;
103  };
104  t_Vector const x = t_Vector::Random(2 * N) * 5;
105  auto chain = chained_operators(func0, func1, func0, func0);
106  auto chain_adjoint = chained_operators(afunc0, afunc0, afunc1, afunc0);
107  auto op = LinearTransform<t_Vector>{chain, chain_adjoint};
108  t_Vector actual;
109  t_Vector expected;
110  chain(actual, x);
111  expected = op * x;
112  CHECK(expected == x.head(N - 1) * 32);
113  CHECK(actual == expected);
114  chain_adjoint(actual, x.head(N - 1));
115  expected = op.adjoint() * x.head(N - 1);
116  CHECK(actual == expected);
117  }
118 }
constexpr auto N
Definition: wavelets.cc:57
sopt::Vector< Scalar > t_Vector
OperatorFunction< T0 > chained_operators(OperatorFunction< T0 > const &arg0, T const &... args)
std::function< void(VECTOR &, VECTOR const &)> OperatorFunction
Typical function out = A*x.
Definition: types.h:43
Eigen::Matrix< T, Eigen::Dynamic, 1 > Vector
A vector of a given type.
Definition: types.h:24

References sopt::chained_operators(), and N.