[英]Using Eigen::LLT within a templated 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;
}
基本上,我希望能夠調用以下內容:
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)具有相同的誤差
vectype soln = mat.template triangularView<Eigen::Lower>()*mat.transpose()*vec;
它會將<
解析為小於和>
解析為大於。 然后它死於()
。
這是因為triangularView
是否為模板取決於mat
的類型。 為了簡化解析,C ++聲明令牌何時可以是模板,類型或值(取決於未綁定模板參數的類型),解析器應假定它是一個值。
程序員必須使用typename
和template
關鍵字來消除歧義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.