forked from EPCCed/APT-CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparse.cpp
42 lines (30 loc) · 911 Bytes
/
sparse.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
#include <Eigen/Dense>
#include <Eigen/Sparse>
#include <iostream>
int main()
{
int n = 10;
Eigen::SparseMatrix<double> A;
A.resize(n, n);
double delta = 0.4;
std::vector<Eigen::Triplet<double>> fill;
fill.reserve(n * 4);
for (int i = 0; i < n - 1; ++i)
{
fill.push_back(Eigen::Triplet<double>(i + 1, i, delta));
fill.push_back(Eigen::Triplet<double>(i + 1, i + 1, -delta));
fill.push_back(Eigen::Triplet<double>(i, i, 1.0 - delta));
fill.push_back(Eigen::Triplet<double>(i, i + 1, delta));
}
fill.push_back(Eigen::Triplet<double>(n - 1, n - 1, 1.0));
A.setFromTriplets(fill.begin(), fill.end());
std::cout << A << std::endl;
Eigen::VectorXd b(n);
b.head(n / 2).array() = 1.0;
b.tail(n / 2).array() = 0.0;
std::cout << b.transpose() << std::endl;
for (int i = 0; i < 100; ++i)
b = A * b;
std::cout << b.transpose() << std::endl;
return 0;
}