Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
Spectra::SymEigsShiftSolver< OpType > Class Template Reference

#include <SymEigsShiftSolver.h>

Inheritance diagram for Spectra::SymEigsShiftSolver< OpType >:
Inheritance graph
[legend]

Public Member Functions

 SymEigsShiftSolver (OpType &op, Index nev, Index ncv, const Scalar &sigma)
 
- Public Member Functions inherited from Spectra::HermEigsBase< DenseSymShiftSolve< double >, IdentityBOp >
Index compute (SortRule selection=SortRule::LargestMagn, Index maxit=1000, RealScalar tol=1e-10, SortRule sorting=SortRule::LargestAlge)
 
RealVector eigenvalues () const
 
virtual Matrix eigenvectors () const
 
virtual Matrix eigenvectors (Index nvec) const
 
CompInfo info () const
 
void init ()
 
void init (const Scalar *init_resid)
 
Index num_iterations () const
 
Index num_operations () const
 

Private Types

using Array = Eigen::Array< Scalar, Eigen::Dynamic, 1 >
 
using Base = HermEigsBase< OpType, IdentityBOp >
 
using Index = Eigen::Index
 
using Scalar = typename OpType::Scalar
 

Private Member Functions

void sort_ritzpair (SortRule sort_rule) override
 

Private Attributes

const Index m_nev
 
RealVector m_ritz_val
 
const Scalar m_sigma
 

Additional Inherited Members

- Protected Attributes inherited from Spectra::HermEigsBase< DenseSymShiftSolve< double >, IdentityBOp >
LanczosFac m_fac
 
const Index m_n
 
const Index m_ncv
 
const Index m_nev
 
Index m_niter
 
Index m_nmatop
 
const DenseSymShiftSolve< double > & m_op
 
std::vector< DenseSymShiftSolve< double > > m_op_container
 
RealVector m_ritz_val
 

Detailed Description

template<typename OpType = DenseSymShiftSolve<double>>
class Spectra::SymEigsShiftSolver< OpType >

This class implements the eigen solver for real symmetric matrices using the shift-and-invert mode. The background information of the symmetric eigen solver is documented in the SymEigsSolver class. Here we focus on explaining the shift-and-invert mode.

The shift-and-invert mode is based on the following fact: If $\lambda$ and $x$ are a pair of eigenvalue and eigenvector of matrix $A$, such that $Ax=\lambda x$, then for any $\sigma$, we have

\[(A-\sigma I)^{-1}x=\nu x\]

where

\[\nu=\frac{1}{\lambda-\sigma}\]

which indicates that $(\nu, x)$ is an eigenpair of the matrix $(A-\sigma I)^{-1}$.

Therefore, if we pass the matrix operation $(A-\sigma I)^{-1}y$ (rather than $Ay$) to the eigen solver, then we would get the desired values of $\nu$, and $\lambda$ can also be easily obtained by noting that $\lambda=\sigma+\nu^{-1}$.

The reason why we need this type of manipulation is that the algorithm of Spectra (and also ARPACK) is good at finding eigenvalues with large magnitude, but may fail in looking for eigenvalues that are close to zero. However, if we really need them, we can set $\sigma=0$, find the largest eigenvalues of $A^{-1}$, and then transform back to $\lambda$, since in this case largest values of $\nu$ implies smallest values of $\lambda$.

To summarize, in the shift-and-invert mode, the selection rule will apply to $\nu=1/(\lambda-\sigma)$ rather than $\lambda$. So a selection rule of LARGEST_MAGN combined with shift $\sigma$ will find eigenvalues of $A$ that are closest to $\sigma$. But note that the eigenvalues() method will always return the eigenvalues in the original problem (i.e., returning $\lambda$ rather than $\nu$), and eigenvectors are the same for both the original problem and the shifted-and-inverted problem.

Template Parameters
OpTypeThe name of the matrix operation class. Users could either use the wrapper classes such as DenseSymShiftSolve and SparseSymShiftSolve, or define their own that implements the type definition Scalar and all the public member functions as in DenseSymShiftSolve.

Below is an example that illustrates the use of the shift-and-invert mode:

#include <Eigen/Core>
// <Spectra/MatOp/DenseSymShiftSolve.h> is implicitly included
#include <iostream>
using namespace Spectra;
int main()
{
// A size-10 diagonal matrix with elements 1, 2, ..., 10
Eigen::MatrixXd M = Eigen::MatrixXd::Zero(10, 10);
for (int i = 0; i < M.rows(); i++)
M(i, i) = i + 1;
// Construct matrix operation object using the wrapper class
// Construct eigen solver object with shift 0
// This will find eigenvalues that are closest to 0
eigs.init();
eigs.compute(SortRule::LargestMagn);
if (eigs.info() == CompInfo::Successful)
{
Eigen::VectorXd evalues = eigs.eigenvalues();
// Will get (3.0, 2.0, 1.0)
std::cout << "Eigenvalues found:\n" << evalues << std::endl;
}
return 0;
}

Also an example for user-supplied matrix shift-solve operation class:

#include <Eigen/Core>
#include <iostream>
using namespace Spectra;
// M = diag(1, 2, ..., 10)
class MyDiagonalTenShiftSolve
{
private:
double sigma_;
public:
using Scalar = double; // A typedef named "Scalar" is required
int rows() const { return 10; }
int cols() const { return 10; }
void set_shift(double sigma) { sigma_ = sigma; }
// y_out = inv(A - sigma * I) * x_in
// inv(A - sigma * I) = diag(1/(1-sigma), 1/(2-sigma), ...)
void perform_op(double *x_in, double *y_out) const
{
for (int i = 0; i < rows(); i++)
{
y_out[i] = x_in[i] / (i + 1 - sigma_);
}
}
};
int main()
{
MyDiagonalTenShiftSolve op;
// Find three eigenvalues that are closest to 3.14
eigs.init();
eigs.compute(SortRule::LargestMagn);
if (eigs.info() == CompInfo::Successful)
{
Eigen::VectorXd evalues = eigs.eigenvalues();
// Will get (4.0, 3.0, 2.0)
std::cout << "Eigenvalues found:\n" << evalues << std::endl;
}
return 0;
}

Definition at line 149 of file SymEigsShiftSolver.h.

Member Typedef Documentation

◆ Array

template<typename OpType = DenseSymShiftSolve<double>>
using Spectra::SymEigsShiftSolver< OpType >::Array = Eigen::Array<Scalar, Eigen::Dynamic, 1>
private

Definition at line 154 of file SymEigsShiftSolver.h.

◆ Base

template<typename OpType = DenseSymShiftSolve<double>>
using Spectra::SymEigsShiftSolver< OpType >::Base = HermEigsBase<OpType, IdentityBOp>
private

Definition at line 156 of file SymEigsShiftSolver.h.

◆ Index

template<typename OpType = DenseSymShiftSolve<double>>
using Spectra::SymEigsShiftSolver< OpType >::Index = Eigen::Index
private

Definition at line 153 of file SymEigsShiftSolver.h.

◆ Scalar

template<typename OpType = DenseSymShiftSolve<double>>
using Spectra::SymEigsShiftSolver< OpType >::Scalar = typename OpType::Scalar
private

Definition at line 152 of file SymEigsShiftSolver.h.

Constructor & Destructor Documentation

◆ SymEigsShiftSolver()

template<typename OpType = DenseSymShiftSolve<double>>
Spectra::SymEigsShiftSolver< OpType >::SymEigsShiftSolver ( OpType &  op,
Index  nev,
Index  ncv,
const Scalar sigma 
)
inline

Constructor to create a eigen solver object using the shift-and-invert mode.

Parameters
opThe matrix operation object that implements the shift-solve operation of $A$: calculating $(A-\sigma I)^{-1}v$ for any vector $v$. Users could either create the object from the wrapper class such as DenseSymShiftSolve, or define their own that implements all the public members as in DenseSymShiftSolve.
nevNumber of eigenvalues requested. This should satisfy $1\le nev \le n-1$, where $n$ is the size of matrix.
ncvParameter that controls the convergence speed of the algorithm. Typically a larger ncv_ means faster convergence, but it may also result in greater memory use and more matrix operations in each iteration. This parameter must satisfy $nev < ncv \le n$, and is advised to take $ncv \ge 2\cdot nev$.
sigmaThe value of the shift.

Definition at line 190 of file SymEigsShiftSolver.h.

Member Function Documentation

◆ sort_ritzpair()

template<typename OpType = DenseSymShiftSolve<double>>
void Spectra::SymEigsShiftSolver< OpType >::sort_ritzpair ( SortRule  sort_rule)
inlineoverrideprivatevirtual

Member Data Documentation

◆ m_nev

template<typename OpType = DenseSymShiftSolve<double>>
const Index Spectra::HermEigsBase< OpType, BOpType >::m_nev
private

Definition at line 78 of file HermEigsBase.h.

◆ m_ritz_val

template<typename OpType = DenseSymShiftSolve<double>>
RealVector Spectra::HermEigsBase< OpType, BOpType >::m_ritz_val
private

Definition at line 84 of file HermEigsBase.h.

◆ m_sigma

template<typename OpType = DenseSymShiftSolve<double>>
const Scalar Spectra::SymEigsShiftSolver< OpType >::m_sigma
private

Definition at line 160 of file SymEigsShiftSolver.h.


The documentation for this class was generated from the following file:
Spectra::DenseSymShiftSolve< double >
Spectra::CompInfo::Successful
@ Successful
Computation was successful.
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
main
int main(int argc, char **argv)
Definition: cmake/example_cmake_find_gtsam/main.cpp:63
sampling::sigma
static const double sigma
Definition: testGaussianBayesNet.cpp:170
SymEigsShiftSolver.h
Spectra::SymEigsShiftSolver
Definition: SymEigsShiftSolver.h:149
Spectra
Definition: LOBPCGSolver.h:19
Spectra::SortRule::LargestMagn
@ LargestMagn
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
M
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:51


gtsam
Author(s):
autogenerated on Fri Mar 28 2025 03:16:39