# Solving Linear Equations using Eigen

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?

• 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. Nov 19 '15 at 1:47
• 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. Nov 19 '15 at 2:12
• 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 Nov 19 '15 at 6:46

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;