I am trying to solve linear equations in c++ using the Eigen library. For the simple equation Ax=b, I have a sparse matrix A that is n x n, and known values for b which is n x 1, but I need to find the values for x. I tried using

x = A.colPivHouseholderQr().solve(b);

but this solves it for a dense matrix and since my "n" value is quite large, I need to find a way to do this using an implementation that is specific to sparse matrices. I looked into using SparseLU but I've been having trouble trying figure out the syntax behind it. Eigen online says to write something like this :

SparseLU<SparseMatrix<scalar>, COLAMDOrdering<Index>> solver;
x = solver.solve(b);

but when I run this, I get errors saying that they don't know what scalar and Index are. Has anyone used SparseLU and know the syntax behind implementing it?

  • 6
    $\begingroup$ I'm not sure where you found this example. But instead of "scalar" you would need some floating point type, typically, double. Instead of "Index" you would typically use int. $\endgroup$ – Bill Greene Nov 19 '15 at 1:47
  • 1
    $\begingroup$ This seems like a programming issue, rather than a computational science one, and it would be impossible to answer it without you saying what exactly the "errors" were. $\endgroup$ – Kirill Nov 19 '15 at 2:12
  • $\begingroup$ Please post the errors as the solution might be as simple as taking Bill Greene's suggestion of using double's and int's instead of scalars and index $\endgroup$ – James Nov 19 '15 at 6:46
Eigen::SparseLU<Eigen::SparseMatrix<double> > solverA;

if(solverA.info()!=Eigen::Success) {
    std::cout << "Oh: Very bad" <<"\n";
     std::cout<<"okay computed"<<"\n";
Eigen::VectorXd solnew = solverA.solve(b);

You don't need to mention the COLAMDOrdering but if you want you can declare the solver as :

Eigen::SparseLU<Eigen::SparseMatrix<double>, Eigen::COLAMDOrdering<int> > solver;

PS: More info about COLAMDOrdring can be found here.

| cite | improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.