src/third_party/spectra/examples/DavidsonSymEigs_example.md
This is an example of how to use the Jacobi-Davidson Symmetric Eigenvalue Solver with DPR correction method. This test can also be found as a full file in the example/DavidsonSymEigs_example.cpp file and can be compiled with cmake and run afterwards
mkdir build && cd build && cmake ../
make DavidsonSymEigs_example
./example/DavidsonSymEigs_example
Suppose we want to find the 2 eigenpairs with the Largest value from a 1000x1000 Matrix A, then we could use this solver to quickly find them.
Note: The Matrix has to be diagonally dominant otherwise the method will not converge
#include <Eigen/Dense>
#include <Spectra/DavidsonSymEigsSolver.h>
#include <Spectra/MatOp/DenseSymMatProd.h>
#include <iostream>
using namespace Spectra;
int main()
{
Eigen::Index n = 1000;
Eigen::MatrixXd mat = 0.03 * Eigen::MatrixXd::Random(n, n);
Eigen::MatrixXd mat1 = mat + mat.transpose();
for (Eigen::Index i=0; i<n; i++) {
mat1(i,i) += i+1;
}
Note: For the solver only a Matrix product operation is required, thus you can specify a custom one without underlying matrix if you wish
DenseSymMatProd<double> op(mat); // Create the Matrix Product operation
Eigen::Index num_of_eigenvalues = 5;
DavidsonSymEigsSolver<DenseSymMatProd<double>> solver(op_dense, num_of_eigenvalues); //Create Solver
compute() method. compute() returns the number of converged eigenvalues.
Eigen::Index iterations = 100;
double tolerance = 1e-3;
int nconv = solver.compute(SortRule::LargestAlge, iterations, tolerance);
// Retrieve results
Eigen::VectorXd evalues;
if (solver.info() == CompInfo::Successful){
evalues = solver.eigenvalues();
std::cout <<nconv<< " Eigenvalues found:\n"
<< evalues << std::endl;
}else{
std::cout <<"Calculation failed"<<std::endl;
}
return 0;
}
compute_with_guess method, which takes an additional Eigen::Matrix as an input. The guess is expected to be normalized.