在模板化函数中使用Eigen :: LLT

时间:2017-11-09 21:53:40

标签: c++ c++11 templates eigen boost-any

我写了以下函数:

template<typename mattype, typename vectype>
    inline static boost::any ApplyCholesky(boost::any const& A, boost::any const& x) {
    const Eigen::LLT<mattype>& chol = boost::any_cast<Eigen::LLT<mattype> const&>(A);

    const mattype& mat = chol.matrixL();
    const vectype& vec = boost::any_cast<vectype const&>(x);
    assert(mat.cols()==vec.size());

    vectype soln = mat.triangularView<Eigen::Lower>()*mat.transpose()*vec;

    return soln;
  }

基本上,我希望能够调用以下内容:

ApplyCholesky<Eigen::MatrixXd, Eigen::VectorXd>(A, x);
ApplyCholesky<Eigen::Matrix4d, Eigen::Vector4d>(A, x);
ApplyCholesky<Eigen::Matrix2f, Eigen::Vector2f>(A, x);

但是,我收到以下错误:

error: expected expression
    vectype soln = mat.triangularView<Eigen::Lower>()*mat.transpose()*vec;

我无法弄清楚我做错了什么。我有一个类似的ApplyInverseCholesky来解决线性系统(即,我需要两个函数:(i)y = A x和(ii)y = A ^ { - 1} x)具有相同的误差

1 个答案:

答案 0 :(得分:3)

vectype soln = mat.template triangularView<Eigen::Lower>()*mat.transpose()*vec;

它将<解析为小于和>大于。()。然后它在triangularView处死亡。

这是因为mat是否为模板取决于typename的类型。为了简化解析,C ++说明了令牌可以是模板,类型或值,具体取决于未绑定模板参数的类型,解析器应该假定它是一个值。

程序员必须使用template// ... $this->request = $this->request->withParam( 'paging', $paginator->getPagingParams() + (array)$this->request->getParam('paging') ); $this->set(compact('pokemons')); 关键字来消除歧义。