SOPT
Sparse OPTimisation
power_method.cc
Go to the documentation of this file.
1 #include <numeric>
2 #include <Eigen/Eigenvalues>
3 #include "sopt/power_method.h"
4 
5 int main(int, char const **) {
6  using Scalar = sopt::t_real;
7  auto constexpr N = 10;
8 
9  // Create some kind of matrix
10  sopt::Matrix<Scalar> const A =
12 
13  // the linear transform wraps the matrix into something the power-method understands
14  auto const lt = sopt::linear_transform(A.cast<sopt::t_complex>());
15  // instanciate the power method
16  auto const pm = sopt::algorithm::PowerMethod<sopt::t_complex>().tolerance(1e-12);
17  // call it
18  auto const result = pm.AtA(lt, sopt::Vector<sopt::t_complex>::Ones(N));
19 
20  // Compute the eigen values explictly to figure out the result of the power method
21  Eigen::EigenSolver<sopt::Matrix<Scalar>> es;
22  es.compute(A.adjoint() * A, true);
23  Eigen::DenseIndex index;
24  (es.eigenvalues().transpose() * es.eigenvalues()).real().maxCoeff(&index);
25  auto const eigenvalue = es.eigenvalues()(index);
26 
27  // This should pass if the power method is correct
28  if (std::abs(result.magnitude - std::abs(eigenvalue)) > 1e-8 * std::abs(eigenvalue))
29  throw std::runtime_error("Power method did not converge to the expected value");
30 
31  return 0;
32 }
constexpr auto N
Definition: wavelets.cc:57
sopt::t_real Scalar
Eigenvalue and eigenvector for eigenvalue with largest magnitude.
Definition: power_method.h:137
DiagnosticAndResult AtA(t_LinearTransform const &A, t_Vector const &input) const
Maximum number of iterations.
Definition: power_method.h:199
int main(int, char const **)
Definition: power_method.cc:5
LinearTransform< VECTOR > linear_transform(OperatorFunction< VECTOR > const &direct, OperatorFunction< VECTOR > const &indirect, std::array< t_int, 3 > const &sizes={{1, 1, 0}})
double t_real
Root of the type hierarchy for real numbers.
Definition: types.h:17
Eigen::Matrix< T, Eigen::Dynamic, 1 > Vector
A vector of a given type.
Definition: types.h:24
std::complex< t_real > t_complex
Root of the type hierarchy for (real) complex numbers.
Definition: types.h:19
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > Matrix
A matrix of a given type.
Definition: types.h:29