在Eigen中使用Cholesky方法求解大型稀疏线性系统

时间:2019-04-06 09:42:29

标签: c++ sparse-matrix eigen

我实际上正在尝试使用Eigen中的Cholesky方法解决大型稀疏线性系统。 我已经从this site下载了一个稀疏矩阵(cfd1)。我试图求解如下定义的线性系统Ax = b:A是矩阵cfd1,b = A * xe其中xe是矩阵尺寸相同的行向量大小的矢量。总之,我想使用Matlab表示法来解决:x = A\b。这里的代码:

#include <iostream>
#include <Eigen/Dense>
#include <unsupported/Eigen/SparseExtra>
#include<Eigen/SparseCholesky>

using namespace std;
using namespace Eigen;
int main()
{

   SparseMatrix<double> mat;
   VectorXd x;
   loadMarket(mat, "Path of downloaded matrix");

   cout << "Number of Rows:\n" << mat.rows() << endl;

   ArrayXd xe = ArrayXd::Constant(mat.rows(), 1);
   cout << xe << endl;
   SparseVector<double> b = mat*xe;

   SimplicialLLT<SparseMatrix<double> > solver;
   x = solver.compute(mat).solve(b);
   cout << x << endl;



}

问题在于,当我进行编译时,我得到了当前错误:

error: invalid operands to binary expression
      ('SparseMatrix<double>' and 'Eigen::ArrayXd' (aka 'Array<double, Dynamic,
      1>'))
   SparseVector<double> b = mat*xe;
                            ~~~^~~
/Users/anto/Desktop/example/eigen-eigen-323c052e1731/Eigen/src/SparseCore/../plugins/CommonCwiseBinaryOps.h:50:29:
note: 
      candidate function template not viable: no known conversion from
      'Eigen::ArrayXd' (aka 'Array<double, Dynamic, 1>') to 'const
      Eigen::SparseMatrixBase<Eigen::SparseMatrix<double, 0, int>
      >::StorageBaseType' (aka 'const
      Eigen::SparseMatrixBase<Eigen::SparseMatrix<double, 0, int> >') for 2nd
      argument

有人可以帮我修复它吗?

1 个答案:

答案 0 :(得分:0)

两件事:

  • 您不能在产品表达式中混用ArrayMatrix,即用ArrayXd代替VectorXd
  • 稀疏矩阵与密集向量的乘积通常是密集的,因此您需要将乘积分配给密集的VectorXd而不是SparseVector

以下编译

SparseMatrix<double> mat;
loadMarket(mat, "Path of downloaded matrix");

cout << "Number of Rows:\n" << mat.rows() << endl;

VectorXd xe = VectorXd::Constant(mat.rows(), 1);
cout << xe << endl;
VectorXd b = mat*xe;

SimplicialLLT<SparseMatrix<double> > solver;
VectorXd x = solver.compute(mat).solve(b);
cout << x << endl;