【问题标题】:error: no matching function for call to 'conjugate_gradient' in Eigen Library错误:没有匹配函数调用特征库中的“conjugate_gradient”
【发布时间】:2018-04-25 19:02:04
【问题描述】:

编译具有 Eigen 库的 c++ 编码时出现以下错误

错误:在调用“conjugate_gradient”时没有匹配的函数 特征库

下面是代码:

SparseMatrix<double> A(truncatedSize,truncatedSize);
for(int i=0;i<truncatedSize;i++)
{
    for(int j=0;j<truncatedSize;j++)
    {
        A.insert(i,j)=TruncatedGlMatrix[i][j];
    }
}

VectorXf V(truncatedSize);
for(int i=0;i<truncatedSize;i++)
{       
    V(i)=TruncatedForce[i][1];
}

// solve Ax = b
ConjugateGradient<SparseMatrix<double>, Lower|Upper> cg;
cg.compute(A);

VectorXf xa(truncatedSize);
xa = cg.solve(V);

【问题讨论】:

    标签: c++ sparse-matrix eigen


    【解决方案1】:

    您确实需要将此显示为MCVE。如果您对它进行了更多的模拟,那么您的结果可能如下:

    #include <Eigen/Core>
    #include <Eigen/Sparse>
    
    using namespace Eigen;
    
    int main()
    {
        int truncatedSize = 50;
        SparseMatrix<double> A(truncatedSize, truncatedSize);
    
    //  We have no idea what TruncatedGlMatrix or TruncatedForce are...
    //  for (int i = 0; i < truncatedSize; i++)
    //  {
    //      for (int j = 0; j < truncatedSize; j++)
    //      {
    //          A.insert(i, j) = TruncatedGlMatrix[i][j];
    //      }
    //  }
    
        VectorXf V(truncatedSize);
    //  for (int i = 0; i < truncatedSize; i++)
    //  {
    //      V(i) = TruncatedForce[i][1];
    //  }
    
        // solve Ax = b
        ConjugateGradient<SparseMatrix<double>, Lower | Upper> cg;
        cg.compute(A);
    
        VectorXf xa(truncatedSize);
        xa = cg.solve(V);
        return 0;
    }
    

    我从您那里得到一个不同的错误,但这可能是因为我不得不对您实际查看的内容进行一些猜测。在上面的代码中,问题在于您混合了doublefloat 标量类型。即,

    xa = cg.solve(V);
    

    xaVfloats 的向量,而cgA 具有double 作为它们的标量类型。您必须在这些之间显式转换,因此将该行替换为

    xa = cg.solve(V.cast<double>()).cast<float>();
    

    将解决我的 MCVE 遇到的问题(这也可能是您的问题,我不知道如何判断)。

    【讨论】:

    • 是的!现在可以了。问题在于数据类型( float 和 double )。我将所有向量转换为双倍。现在它工作正常。感谢您的意见!
    猜你喜欢
    • 2013-05-05
    • 2012-12-28
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    相关资源
    最近更新 更多