A C++ library for sparse matrix eigenproblems, utilized in quantum chemistry packages for solving sparse hamiltonian matrices.
It is built on top of eigen and uses its types, and have no other dependencies.
Its home page can be found here and a quick start guide is here.
Its main purpose is to calculate some number of eigenvalues of a large matrix , where usually .
We git pull the source code files and putting the core code and header files.
git clone https://github.com/yixuan/spectra.git
sudo cp -r spectra/include/Spectra /usr/local/include/
afterwards, it will be automatically included whenever we use a C++ compiler.
g++ solve_01.cpp -o solve_01
./solve_01
Include the library as a target in the Makefile.
This is also how the official PyCI repository does it.
deps/spectra:
[ -d $@ ] || git clone https://github.com/yixuan/spectra.git $@
The biggest benefit of spectra is that one need only supply a class that perform the matrix operation , and spectra's solver class will handle the eigensolving by calling the methods in an object of this class supplied to the solver object.
solve_01.cpp
#include <Eigen/Core>
#include <Spectra/SymEigsSolver.h>
#include <iostream>
using namespace Spectra;
// M = diag(1, 2, ..., 10)
class MyDiagonal
{
public:
using Scalar = double; // A typedef named "Scalar" is required
int rows() const { return 10; }
int cols() const { return 10; }
// y_out = M * x_in
void perform_op(const double *x_in, double *y_out) const
{
for(int i = 0; i < rows(); i++)
{
y_out[i] = x_in[i] * (i + 1);
}
}
};
int main()
{
MyDiagonal op;
// solve for the single largest eigenvalue
SymEigsSolver<MyDiagonal> eigs(op, 1, 6);
eigs.init();
eigs.compute(SortRule::LargestAlge);
//which should give 10
if(eigs.info() == CompInfo::Successful)
{
Eigen::VectorXd evalues = eigs.eigenvalues();
std::cout << "Eigenvalues found:\n" << evalues << std::endl;
}
return 0;
}
The custom class above essentially create an ad hoc diagonal matrix:
Compile and run:
g++ solve_01.cpp -o solve_01
./solve_01
And you can see the trivial answer largest eigenvalue of 10.
According to the header file, PyCI only uses 2 classes from Spectra:
#include <Spectra/MatOp/SparseSymMatProd.h>
#include <Spectra/SymEigsSolver.h>
And are only used in the SparseOp class, which happens in the solve_ci() method, to find eigenvalues of smallest algebraic value (most negative) of a sparse symmetric matrix (reference)
Thus, efforts in parallelizing the method will have a focus in parallelizing eigenvalue finding algorithm of such types of sparse matrices.