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