source: pacpussensors/trunk/Vislab/lib3dv/eigen/doc/special_examples/Tutorial_sparse_example.cpp@ 136

Last change on this file since 136 was 136, checked in by ldecherf, 7 years ago

Doc

File size: 1.0 KB
Line 
1#include <Eigen/Sparse>
2#include <vector>
3
4typedef Eigen::SparseMatrix<double> SpMat; // declares a column-major sparse matrix type of double
5typedef Eigen::Triplet<double> T;
6
7void buildProblem(std::vector<T>& coefficients, Eigen::VectorXd& b, int n);
8void saveAsBitmap(const Eigen::VectorXd& x, int n, const char* filename);
9
10int main(int argc, char** argv)
11{
12 int n = 300; // size of the image
13 int m = n*n; // number of unknows (=number of pixels)
14
15 // Assembly:
16 std::vector<T> coefficients; // list of non-zeros coefficients
17 Eigen::VectorXd b(m); // the right hand side-vector resulting from the constraints
18 buildProblem(coefficients, b, n);
19
20 SpMat A(m,m);
21 A.setFromTriplets(coefficients.begin(), coefficients.end());
22
23 // Solving:
24 Eigen::SimplicialCholesky<SpMat> chol(A); // performs a Cholesky factorization of A
25 Eigen::VectorXd x = chol.solve(b); // use the factorization to solve for the given right hand side
26
27 // Export the result to a file:
28 saveAsBitmap(x, n, argv[1]);
29
30 return 0;
31}
32
Note: See TracBrowser for help on using the repository browser.