Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将固定大小的特征矩阵作为参数传递给调用动态大小矩阵的函数_C++_Templates_Matrix_Typedef_Eigen - Fatal编程技术网

C++ 将固定大小的特征矩阵作为参数传递给调用动态大小矩阵的函数

C++ 将固定大小的特征矩阵作为参数传递给调用动态大小矩阵的函数,c++,templates,matrix,typedef,eigen,C++,Templates,Matrix,Typedef,Eigen,我正在为我的个人代码库在Eigen之上编写一个小型线性代数实用程序库。为了使它尽可能灵活,我输入了不同的特征矩阵类型作为参数。然而,我经常遇到的一个问题是,当我使用它时,我无法将固定大小(即在编译时设置)矩阵作为参数传递给具有动态大小(在运行时设置)矩阵typedef作为参数的函数。我可以理解相反的情况——由于编译时检查,无法将动态大小的矩阵传递为固定矩阵,但这似乎应该可以 一个可测试的例子是下面的pdist2函数(在Eigen API中应该有一个本机实现) 为什么这不起作用?或者更具体地说,我

我正在为我的个人代码库在Eigen之上编写一个小型线性代数实用程序库。为了使它尽可能灵活,我输入了不同的特征矩阵类型作为参数。然而,我经常遇到的一个问题是,当我使用它时,我无法将固定大小(即在编译时设置)矩阵作为参数传递给具有动态大小(在运行时设置)矩阵typedef作为参数的函数。我可以理解相反的情况——由于编译时检查,无法将动态大小的矩阵传递为固定矩阵,但这似乎应该可以

一个可测试的例子是下面的
pdist2
函数(在Eigen API中应该有一个本机实现)

为什么这不起作用?或者更具体地说,我如何使用模板化的typedef来确保我的固定大小矩阵源自
Eigen::MatrixXT


注:这都是使用Eigen 3.3.3。

问题是
矩阵
矩阵
不是同一类型,因此,将前者传递给
pdist2
的唯一方法是将其转换为
矩阵
。如果
pdist2
不是模板函数,则编译器会自动执行此操作:

MatrixXT<double> pdist2(const MatrixXT<double>&,const MatrixXT<double>&);

如果X不能表示为指向实际值的指针+跨步,则将以这种方式计算。

这是不可能的<代码>矩阵和
MatrixXt
是不同的类型。这意味着您不能将对
MatrixXt
的引用绑定到
Matrix
的实例。使用传递值,或者查看
Eigen::Ref
@HenriMenke我不认为这是问题所在,有转换正在进行(即使DIM不正确,您也会得到断言错误)。问题在于推断模板类型
T
,仍在试图找出原因。您能否清楚地说明为什么调用函数作为
pdist2(X,Y)
可以避免模板参数推断问题?在这种情况下,编译器不必为您推断任何模板参数,
pdist2
因此,其行为与我的答案的非模板函数完全相同,并且不同矩阵类型的隐式转换适用。@ggael您应该启动Kickstarter来编写权威的Eigen书籍。我会为此付出很多。
Eigen::Matrix<double, 3, 5> X;
X << 8.147236863931790, 9.133758561390193, 2.784982188670484, 9.648885351992766, 9.571669482429456,
     9.057919370756192, 6.323592462254095, 5.468815192049838, 1.576130816775483, 4.853756487228412,
     1.269868162935061, 0.975404049994095, 9.575068354342976, 9.705927817606156, 8.002804688888002;

Eigen::Matrix<double, 3, 4> Y;
Y << 1.418863386272153, 7.922073295595544, 0.357116785741896, 6.787351548577734,
     4.217612826262750, 9.594924263929030, 8.491293058687772, 7.577401305783335,
     9.157355251890671, 6.557406991565868, 9.339932477575505, 7.431324681249162;

Eigen::Matrix<double, 5, 4> D = pdist2(X, Y);
error: no matching function for call to ‘pdist2(Eigen::Matrix<double, 3, 5>&, Eigen::Matrix<double, 3, 4>&)’
  Eigen::Matrix<double, 5, 4> D = Util::Math::pdist2(X, Y);
                                                              ^
note: candidate: template<class T> Eigen::MatrixXT<T> Util::Math::pdist2(Eigen::MatrixXT<T>&, Eigen::MatrixXT<T>&)
   inline Eigen::MatrixXT<T> pdist2(const Eigen::MatrixXT<T> &X, const Eigen::MatrixXT<T> &Y)
                         ^
note:   template argument deduction/substitution failed:
note:   template argument ‘3’ does not match ‘#‘integer_cst’ not supported by dump_decl#<declaration error>’
  Eigen::Matrix<double, 5, 4> D_est = Util::Math::pdist2(X, Y);
                                                              ^
note:   ‘Eigen::Matrix<double, 3, 5>’ is not derived from ‘Eigen::MatrixXT<T>’
MatrixXT<double> pdist2(const MatrixXT<double>&,const MatrixXT<double>&);
template<typename D1,typename D2>
Matrix<typename D1::Scalar,D1::ColsAtCompileTime,D2::ColsAtCompileTime>
pdist2(const MatrixBase<D1>& _X,const MatrixBase<D2>& _Y);
Ref<const typename D1::PlainObject> X(_X);
Ref<const typename D2::PlainObject> Y(_Y);