我正在尝试根据特征矩阵进行约简。
#include <iostream>
#include <Eigen/Dense>
#include <type_traits>
template<typename T1, typename T2, int n1, int n2>
auto reduction(Eigen::Matrix<T1, n1, n2> &a1,
Eigen::Matrix<T2, n1, n2> &a2)
-> decltype(T1{}*T2{})
{
using BaseT3 =
typename std::remove_cv<typename std::remove_reference<decltype(T1{}*T2{})>::type>::type;
BaseT3 res = a1(0, 0)*a2(0, 0);
for (int i=0; i<n1; ++i)
for (int j=0; j<n2; ++j)
if (i+j)
res = res + a1(i, j)*a2(i, j);
return res;
}
int main()
{
Eigen::Matrix<double, 3, 3> m;
Eigen::Matrix<Eigen::Vector3d, 3, 3> n;
std::cout << reduction(m, n) << std::endl;
}
基本上,我试图获取 sum_{i, j} a1[i, j] * a2[i, j]
其中 a1
和 a2
是一些 eigen mathix 但我收到编译错误。我得到的错误是
error: no match for ‘operator=’ (operand types are ‘BaseT3 {aka
Eigen::CwiseUnaryOp<Eigen::internal::scalar_multiple_op<double>,
const Eigen::Matrix<double, 3, 1> >}’
and
‘const Eigen::CwiseBinaryOp<Eigen::internal::scalar_sum_op<double>,
const Eigen::CwiseUnaryOp<Eigen::internal::scalar_multiple_op<double>,
const Eigen::Matrix<double, 3, 1> >,
const Eigen::CwiseUnaryOp<Eigen::internal::scalar_multiple_op<double>,
const Eigen::Matrix<double, 3, 1> > >’)
res = res + a1(i, j)*a2(i, j);
^
如果我没记错的话,对于给定的 main
,类型 BaseT3
应该是 Eigen::Vector3d
。我还尝试进行静态转换,以便 operator=
不会失败,但随后出现其他错误。
这是c++11,我使用Eigen3,编译器是g++ 5.4.1。
最佳答案
T1 * T2 的 decltype 不是您所期望的 - Eigen 大量使用表达式模板。错误中的 CWiseUnaryOp 和 CWiseBinaryOp 类型表明了这一点。换句话说,“double * Vector3d”的结果不是您所期望的(它不是 Vector3d,而是 cwisebinaryop)。
另请参阅:Writing functions taking Eigen Types .
在这种特定情况下,您可以通过为模板函数的第一个和第二个参数创建特征基类型的部分特化来找到解决方案。
关于c++ - 减少基于特征的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48989329/