2
$\begingroup$

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;
solver.analyzePattern(A);
solver.factorize(A);
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?

$\endgroup$
3
  • 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$ Nov 19, 2015 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, 2015 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, 2015 at 6:46

1 Answer 1

3
$\begingroup$
Eigen::SparseLU<Eigen::SparseMatrix<double> > solverA;
A.makeCompressed();
solverA.analyzePattern(A);
solverA.factorize(A);

if(solverA.info()!=Eigen::Success) {
    std::cout << "Oh: Very bad" <<"\n";
 }
else{
     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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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