eigen error: SelfAdjointView is only for squared matrices

eigen error: SelfAdjointView is only for squared matrices

我想求解像这样的稀疏线性系统:

SparseMatrix<double> A(m, n);
VectorXd b(m);
ConjugateGradient<SparseMatrix<double>, Upper> solver;
solver.compute(A);
VectorXd X = solver.solve(b);

但是 运行 此代码出现此错误:

Assertion failed: (rows()==cols() && "SelfAdjointView is only for squared matrices"), function SparseSelfAdjointView

为什么我会遇到这个问题,如何解决?


我写了一个小例子来重现这个错误:

#include "lib/Eigen/Sparse"

using namespace Eigen;

int main()
{
    SparseMatrix<double> A(2, 3);

    A.coeffRef(0, 0) = 1;
    A.coeffRef(0, 1) = 1;
    A.coeffRef(0, 2) = 1;
    A.coeffRef(1, 0) = 1;
    A.coeffRef(1, 1) = 1;
    A.coeffRef(1, 2) = 1;

    VectorXd b(2);
    b << 3, 3;

    ConjugateGradient<SparseMatrix<double>, Upper> solver;
    solver.compute(A);
    VectorXd X = solver.solve(b);

    return 0;
}

ConjugateGradient 算法仅适用于自伴随矩阵。对于矩形矩阵,请改用 LeastSquaresConjugateGradient