特征:稀疏v传统矩阵

时间:2017-02-03 05:05:33

标签: matrix sparse-matrix eigen

为了加快计算时间,我第一次尝试使用稀疏矩阵。我有一个现有的传统矩阵工作,Ax = b。我想使用稀疏矩阵,AA * sparse_x = b。要查看是否一切正常,请将x与sparse_x进行比较,它们应该相同......但它们不是......我如何使用稀疏矩阵并获得与传统矩阵相同的答案?

Eigen::MatrixXd A(1000,1000);

//Fill A

Eigen::VectorXd b(1000);

//Fill b

Eigen::VectorXd x(1000);

x = A.colPivHouseholderQr().solve(b);

    

//Sparcity is 98% for matrix A…How do I do use sparse matrix to speed things up?

//I am following: https://eigen.tuxfamily.org/dox-devel/group__TutorialSparse.html

std::vector<Eigen::Triplet<double>> coefficients;

for(auto i = 0; i < A.rows(); ++i) {

    for(auto j = 0; j < Acols(); ++j) {

        const auto ij_val{ A(i, j) };

        if(!CApproximately::equal(ij_val, 0.0)) {

            Eigen::Triplet<double> trip(i, j, ij_val);

            coefficients.push_back(trip);

        }

    }

}

//Unknows

auto m = b_.rows();

Eigen::SparseMatrix<double> A(m, m);

A.setFromTriplets(coefficients.begin(), coefficients.end());

Eigen::SimplicialCholesky<Eigen::SparseMatrix<double>> chol(A);

Eigen::VectorXd sparse_x = chol.solve(b);

0 个答案:

没有答案