简体   繁体   中英

Using Eigen::LLT within a templated function

I have written the following function:

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;
  }

Basically, I want to be able to call things like:

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

However, I get the following error:

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

I cannot figure out what I have done wrong. I have a similar ApplyInverseCholesky that solves the linear system (ie, I need two functions: (i) y = A x and (ii) y = A^{-1} x) that has the the same error

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

it is parsing the < as a less than and > as greater than. Then it dies at the () .

This is because triangularView being a template or not is dependent on the type of mat . To simplify parsing, C++ states when a token could be a template, type or value depending on the type of an unbound template parameter, the parser should assume it is a value.

Programmers must use the typename and template keywords to disambiguate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM