PowerMethod.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010-2019, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
20 #pragma once
21 
22 #include <gtsam/base/Matrix.h>
23 #include <gtsam/base/Vector.h>
24 
25 #include <Eigen/Core>
26 #include <Eigen/Sparse>
27 #include <random>
28 #include <vector>
29 #include <optional>
30 
31 namespace gtsam {
32 
34 
57 template <class Operator>
58 class PowerMethod {
59  protected:
64  const Operator &A_;
65 
66  const int dim_; // dimension of Matrix A
67 
68  size_t nrIterations_; // number of iterations
69 
70  double ritzValue_; // Ritz eigenvalue
71  Vector ritzVector_; // Ritz eigenvector
72 
73  public:
76 
78  explicit PowerMethod(const Operator &A,
79  const std::optional<Vector> initial = {})
80  : A_(A), dim_(A.rows()), nrIterations_(0) {
81  Vector x0;
82  x0 = initial ? *initial : Vector::Random(dim_);
83  x0.normalize();
84 
85  // initialize Ritz eigen value
86  ritzValue_ = 0.0;
87 
88  // initialize Ritz eigen vector
90  }
91 
96  Vector powerIteration(const Vector &x) const {
97  Vector y = A_ * x;
98  y.normalize();
99  return y;
100  }
101 
107 
113  bool converged(double tol) const {
114  const Vector x = ritzVector_;
115  // store the Ritz eigen value
116  const double ritzValue = x.dot(A_ * x);
117  const double error = (A_ * x - ritzValue * x).norm();
118  return error < tol;
119  }
120 
122  size_t nrIterations() const { return nrIterations_; }
123 
130  bool compute(size_t maxIterations, double tol) {
131  // Starting
132  bool isConverged = false;
133 
134  for (size_t i = 0; i < maxIterations && !isConverged; i++) {
135  ++nrIterations_;
136  // update the ritzVector after power iteration
138  // update the ritzValue
140  isConverged = converged(tol);
141  }
142 
143  return isConverged;
144  }
145 
147  double eigenvalue() const { return ritzValue_; }
148 
150  Vector eigenvector() const { return ritzVector_; }
151 };
152 
153 } // namespace gtsam
Eigen::SparseMatrix< double >
Vector.h
typedef and functions to augment Eigen's VectorXd
gtsam::PowerMethod::nrIterations
size_t nrIterations() const
Return the number of iterations.
Definition: PowerMethod.h:122
gtsam::PowerMethod
Compute maximum Eigenpair with power method.
Definition: PowerMethod.h:58
Matrix.h
typedef and functions to augment Eigen's MatrixXd
initial
Values initial
Definition: OdometryOptimize.cpp:2
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
gtsam::Vector
Eigen::VectorXd Vector
Definition: Vector.h:38
A
Definition: test_numpy_dtypes.cpp:298
gtsam::PowerMethod::powerIteration
Vector powerIteration() const
Definition: PowerMethod.h:106
gtsam::PowerMethod::dim_
const int dim_
Definition: PowerMethod.h:66
gtsam::PowerMethod::eigenvalue
double eigenvalue() const
Return the eigenvalue.
Definition: PowerMethod.h:147
x0
static Symbol x0('x', 0)
gtsam::PowerMethod::nrIterations_
size_t nrIterations_
Definition: PowerMethod.h:68
gtsam::PowerMethod::converged
bool converged(double tol) const
Definition: PowerMethod.h:113
y
Scalar * y
Definition: level1_cplx_impl.h:124
gtsam::PowerMethod::A_
const Operator & A_
Definition: PowerMethod.h:64
gtsam::PowerMethod::eigenvector
Vector eigenvector() const
Return the eigenvector.
Definition: PowerMethod.h:150
gtsam::PowerMethod::powerIteration
Vector powerIteration(const Vector &x) const
Definition: PowerMethod.h:96
gtsam
traits
Definition: SFMdata.h:40
error
static double error
Definition: testRot3.cpp:37
gtsam::Sparse
Eigen::SparseMatrix< double > Sparse
Definition: AcceleratedPowerMethod.h:26
gtsam::tol
const G double tol
Definition: Group.h:79
initial
Definition: testScenarioRunner.cpp:148
gtsam::PowerMethod::ritzVector_
Vector ritzVector_
Definition: PowerMethod.h:71
gtsam::PowerMethod::compute
bool compute(size_t maxIterations, double tol)
Definition: PowerMethod.h:130
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
gtsam::PowerMethod::PowerMethod
PowerMethod(const Operator &A, const std::optional< Vector > initial={})
Construct from the aim matrix and intial ritz vector.
Definition: PowerMethod.h:78
gtsam::PowerMethod::ritzValue_
double ritzValue_
Definition: PowerMethod.h:70


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:34:25