forked from beam2d/arpaca
/
really_simple_example.cpp
52 lines (47 loc) · 1.56 KB
/
really_simple_example.cpp
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
42
43
44
45
46
47
48
49
50
51
52
/*
* Copyright (c) 2011 Seiya Tokui <beam.web@gmail.com>
* Copyright (c) 2014 Burkhard Ritter <burkhard@ualberta.ca>
* This code is distributed under the MIT license.
*
* A very simple example program demonstrating how to use Arpaca.
*
* To compile this program:
* g++ \
* -I [/path/to/eigen] \
* really_simple_example.cpp \
* -L [/path/to/libarpack.a] \
* -larpack \
* -o really_simple_example
*
* This assumes that arpaca.hpp is in the same directory.
*/
#include <iostream>
#include <Eigen/Dense>
#include <Eigen/Sparse>
#include "arpaca.hpp"
using namespace Eigen;
using namespace arpaca;
int main()
{
// Matrix dimension
const int n_dim = 4;
// Number of eigenvalues to compute
const int n_ev = 3;
// Which eigenvalues to compute
const EigenvalueType type = ALGEBRAIC_SMALLEST;
// A self-adjoint random dense matrix of size n_dim x n_dim and the
// corresponding sparse matrix
MatrixXd dm = MatrixXd::Random(n_dim,n_dim).selfadjointView<Upper>();
SparseMatrix<double> sm = dm.sparseView();
// Solve for the eigenvalues
SymmetricEigenSolver<double> s = Solve(sm, n_ev, type);
// Output
std::cout << "Matrix: " << std::endl << std::endl
<< dm << std::endl << std::endl << std::endl
<< "Its " << n_ev << " smallest eigenvalues: "
<< std::endl << std::endl
<< s.eigenvalues() << std::endl << std::endl
<< "And the corresponding eigenvectos: "
<< std::endl << std::endl
<< s.eigenvectors() << std::endl;
}