After the assembly of the sparse matrix mat, I used one of the sparse solvers (https://eigen.tuxfamily.org/dox/group__TopicSparseSystems.html) to inverse it. The following code took about 2s to inverse a 500,000*500,000 sparse matrix:
Eigen::SparseMatrix<double> I(n,n);
I.setIdentity();
Eigen::SimplicialLLT<Eigen::SparseMatrix<double> > solver;
solver.compute(mat);
if (solver.info() != Eigen::Success){
cout<<"not invertible"<<endl;
}
else{
cout<<"invertible "<<endl;
solver.solve(I);
}
But the following took more than 3 hours
Eigen::SparseMatrix<double> I(n,n);
I.setIdentity();
Eigen::SimplicialLLT<Eigen::SparseMatrix<double> > solver;
solver.compute(mat);
if (solver.info() != Eigen::Success){
cout<<"not invertible"<<endl;
}
else{
cout<<"invertible "<<endl;
Eigen::SparseMatrix<double> invMat;
invMat.reserve(250000 * round(250000/10));
invMat=solver.solve(I);
}
It was fast to run line invMat.reserve(250000 * round(250000/10)); But it took forever to run line invMat=solver.solve(I);
Are there any ways to reduce the speed?
I also tried to reserve large memory: invMat.reserve(n*n); //n is the same size of the matrix to be inverted. But it still ran for ever.
In your first example, only the decomposition is computed.
solver.solve(I)just returns a proxy object -- the actual inversion happens when you assign it to something.You should (almost) never explicitly need to calculate the inverse of any matrix -- especially inverting a sparse matrix will in general result in a mostly dense matrix (this depends on the structure of the input matrix of course).