200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > Eigen求解大型稀疏对称矩阵(Cholesky分解)

Eigen求解大型稀疏对称矩阵(Cholesky分解)

时间:2022-05-07 18:55:47

相关推荐

Eigen求解大型稀疏对称矩阵(Cholesky分解)

参考自Eigen文档

代码如下:

#include <Eigen/Sparse>typedef Eigen::SparseMatrix<double> SpMat;typedef Eigen::Triplet<double> Trip;int main(){//设置Triplet列表,该列表可以表示稀疏矩阵std::vector<Trip> tripletList(9);tripletList.push_back(Trip(0, 0, 10));tripletList.push_back(Trip(0, 1, -2));tripletList.push_back(Trip(0, 2, -2));tripletList.push_back(Trip(1, 0, -2));tripletList.push_back(Trip(1, 1, 10));tripletList.push_back(Trip(1, 2, -1));tripletList.push_back(Trip(2, 0, -2));tripletList.push_back(Trip(2, 1, -1));tripletList.push_back(Trip(2, 2, 3));//用Triplet列表构造稀疏矩阵SpMat mat(3, 3);mat.setFromTriplets(tripletList.begin(), tripletList.end());//使用Cholesky分解矩阵mat(LDLT分解)Eigen::SimplicialLDLT<SpMat> chol(mat); //构造右端项Eigen::VectorXd b(3);b.coeffRef(0) = 1.0;b.coeffRef(1) = 0.5;b.coeffRef(2) = 1.0;//求解方程,回代Eigen::VectorXd x = chol.solve(b);//输出结果for (int i = 0; i < 3; i++){std::cout << x.coeffRef(i) << std::endl;}return 0;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。