File: DavidsonSymEigs_example.cpp

package info (click to toggle)
spectra 1.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,788 kB
  • sloc: cpp: 23,044; ansic: 175; fortran: 131; makefile: 90
file content (41 lines) | stat: -rw-r--r-- 1,112 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

#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;
    }

    DenseSymMatProd<double> op_dense(mat1);  // Create the Matrix Product operation

    Eigen::Index num_of_eigenvalues = 5;
    DavidsonSymEigsSolver<DenseSymMatProd<double>> solver(op_dense, num_of_eigenvalues);  // Create Solver
    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;
}