1 #include <Eigen/Sparse> 2 #include <Eigen/IterativeLinearSolvers> 3 #include <Eigen/PardisoSupport> 4 using namespace Eigen; 5 using namespace std; 6 typedef Eigen::SparseMatrix<double> SpMat; 7 typedef Eigen::PardisoLU <SpMat> PLUSolver; 8 typedef Eigen::PardisoLDLT <SpMat> PLDLTSolver; 9 10 //构造稀疏矩阵,按照行列号插入非零元素 11 SpMat A(8, 8); 12 A.insert(0, 0) = 7.0; 13 A.insert(0, 2) = 1.0; 14 …… 15 A.makeCompressed(); 16 VectorXd b(8), x(8);
//使用Eigen自带CG求解器求解对称稀疏矩阵线性方程组 ConjugateGradient<SparseMatrix<double>, Eigen::Upper> solver; x = solver.compute(A).solve(b);
//调用pardiso求解非对称稀疏矩阵线性方程组 PLUSolver solver; solver.analyzePattern(A); solver.factorize(A); x = solver.solve(b);
//调用pardiso求解对称稀疏矩阵线性方程组 PLDLTSolver solver; solver.analyzePattern(A); solver.factorize(A); x = solver.solve(b);