gtsam
Loading...
Searching...
No Matches
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
19
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
31namespace gtsam {
32
33using Sparse = Eigen::SparseMatrix<double>;
34
57template <class Operator>
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
89 ritzVector_ = powerIteration(x0);
90 }
91
96 Vector powerIteration(const Vector &x) const {
97 Vector y = A_ * x;
98 y.normalize();
99 return y;
100 }
101
106 Vector powerIteration() const { return powerIteration(ritzVector_); }
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 if (maxIterations == 0) return isConverged;
134
135 Vector product = A_ * ritzVector_;
136
137 for (size_t i = 0; i < maxIterations && !isConverged; i++) {
138 ++nrIterations_;
139 // update the ritzVector after power iteration
140 ritzVector_ = product;
141 ritzVector_.normalize();
142
143 // Reuse this product for the Ritz value, residual, and next iteration.
144 product = A_ * ritzVector_;
145 ritzValue_ = ritzVector_.dot(product);
146 const double error = (product - ritzValue_ * ritzVector_).norm();
147 isConverged = error < tol;
148 }
149
150 return isConverged;
151 }
152
154 double eigenvalue() const { return ritzValue_; }
155
157 Vector eigenvector() const { return ritzVector_; }
158};
159
160} // namespace gtsam
typedef and functions to augment Eigen's MatrixXd
typedef and functions to augment Eigen's VectorXd
Global functions in a separate testing namespace.
Definition chartTesting.h:28
Vector powerIteration(const Vector &x) const
Run power iteration to get ritzVector with previous ritzVector x, and return A * x / || A * x ||.
Definition PowerMethod.h:96
Vector eigenvector() const
Return the eigenvector.
Definition PowerMethod.h:157
const Operator & A_
Const reference to an externally-held matrix whose minimum-eigenvalue we want to compute.
Definition PowerMethod.h:64
double eigenvalue() const
Return the eigenvalue.
Definition PowerMethod.h:154
Vector powerIteration() const
Run power iteration to get ritzVector with previous ritzVector x, and return A * x / || A * x ||.
Definition PowerMethod.h:106
bool converged(double tol) const
After Perform power iteration on a single Ritz value, check if the Ritz residual for the current Ritz...
Definition PowerMethod.h:113
PowerMethod(const Operator &A, const std::optional< Vector > initial={})
Construct from the aim matrix and intial ritz vector.
Definition PowerMethod.h:78
bool compute(size_t maxIterations, double tol)
Start the power/accelerated iteration, after performing the power/accelerated iteration,...
Definition PowerMethod.h:130
size_t nrIterations() const
Return the number of iterations.
Definition PowerMethod.h:122