SOPT
Sparse OPTimisation
conjugate_gradient.cc
Go to the documentation of this file.
1 #include <random>
2 #include "catch2/catch_all.hpp"
3 
5 
6 TEST_CASE("Conjugate gradient", "[cg]") {
7  using namespace sopt;
8 
9  ConjugateGradient const cg(std::numeric_limits<t_uint>::max(), 1e-12);
10  SECTION("Real valued") {
11  auto const A = Image<>::Random(10, 10).eval();
12  auto const AtA = (A.transpose().matrix() * A.matrix()).eval();
13  auto const expected = Array<>::Random(A.rows()).eval();
14 
15  auto const actual = cg(AtA, (A.transpose().matrix() * expected.matrix()).eval());
16 
17  CHECK(actual.niters > 0);
18  CHECK(std::abs(actual.residual) < 1e-6);
19  CAPTURE(actual.residual);
20  CAPTURE((A.matrix() * actual.result).transpose());
21  CAPTURE(expected.transpose());
22  CHECK((A.matrix() * actual.result).isApprox(expected.matrix(), 1e-6));
23  }
24 
25  SECTION("Complex valued") {
26  auto const A = Image<t_complex>::Random(10, 10).eval();
27  auto const AhA = (A.conjugate().transpose().matrix() * A.matrix()).eval();
28  auto const expected = Array<t_complex>::Random(A.rows()).eval();
29 
30  auto const actual = cg(AhA, (A.conjugate().transpose().matrix() * expected.matrix()).eval());
31 
32  CHECK(actual.niters > 0);
33  CHECK(std::abs(actual.residual) < 1e-6);
34  CAPTURE(actual.residual);
35  CAPTURE((A.matrix() * actual.result).transpose());
36  CAPTURE(expected.transpose());
37  CHECK((A.matrix() * actual.result).isApprox(expected.matrix(), 1e-6));
38  }
39 }
Solves $Ax = b$ for $x$, given $A$ and $b$.
Eigen::Array< T, Eigen::Dynamic, Eigen::Dynamic > Image
A 2-dimensional list of elements of given type.
Definition: types.h:39
Eigen::Array< T, Eigen::Dynamic, 1 > Array
A 1-dimensional list of elements of given type.
Definition: types.h:34
TEST_CASE("Conjugate gradient", "[cg]")