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

标签 c++ templates matrix typedef eigen

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

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

#include <Eigen/Core>

namespace Eigen
{
    template <typename T>
    using MatrixXT = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>;
}

// X is M x N
// Y is M x K
// Output is N x K
template <typename T>
inline Eigen::MatrixXT<T> pdist2(const Eigen::MatrixXT<T> &X, const Eigen::MatrixXT<T> &Y)
{
    // ASSERT(X.rows() == Y.rows(), "Input observations must have same number of rows (" + 
    //  std::to_string(X.rows()) + "!=" + std::to_string(Y.rows()) + ")");

    Eigen::MatrixXT<T> dists = X.colwise().squaredNorm().transpose() * Eigen::MatrixXT<T>::Ones(1, Y.cols()) +
        Eigen::MatrixXT<T>::Ones(X.cols(), 1) * Y.colwise().squaredNorm() -
        2 * X.transpose() * Y;

    return dists;
}

此代码无法编译:

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

上述函数已经过单元测试并正确评估,但只有当 XYEigen::MatrixXd 类型时它才会起作用。看来一定是我的模板 typedef 导致了这个问题,但它只是一个具有模板类型的动态(即在运行时)大小的矩阵。

错误如下:

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>’

为什么这不起作用?或者更具体地说,我如何使用模板化类型定义来确保我的固定大小矩阵Eigen::MatrixXT<T> 派生?

注意:这都是使用 Eigen 3.3.3。

最佳答案

问题是 Matrix<double, 3, 5>MatrixXT<double>不是同一类型,因此,将前者传递给 pdist2 的唯一方法是将其转换为 MatrixXT<double> .如果pdist2,这将由编译器自动完成不是模板函数:

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

但由于 pdist2 是模板化的并且MatrixXT<double>不是 Matrix<double, 3, 5> 的基类, 在 C++ 中,不允许编译器自动推导出模板参数来实例化 pdist2。您的解决方案是进一步推广您的功能以采取任何 MatrixBase<>作为输入:

template<typename D1,typename D2>
Matrix<typename D1::Scalar,D1::ColsAtCompileTime,D2::ColsAtCompileTime>
pdist2(const MatrixBase<D1>& _X,const MatrixBase<D2>& _Y);

由于 X 和 Y 将被多次使用,并且它们现在可以是任意表达式(包括昂贵的矩阵乘积),您可能希望在需要时让 Eigen 评估参数,为此,您可以使用Ref :

Ref<const typename D1::PlainObject> X(_X);
Ref<const typename D2::PlainObject> Y(_Y);

如果 X 不能表示为指向实际值的指针+步长,则将通过这种方式对其求值。

关于c++ - 将固定大小的特征矩阵作为参数传递给调用动态大小矩阵的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44790593/

相关文章:

c++ - 单独对象的单一模板实例化

java - 带有 Freemarker 的 Netbeans 模板 : built-in with argument fails

Python Numpy 矩阵乘法使用循环将多个矩阵相乘

matlab - 如何创建自定义非线性滤波器?

c++ - GCC 和 binutils build - C 编译器无法创建可执行文件

c++ - c++多个if语句。为什么其他语句也会执行?

c++ - 在另一个类中初始化指针

c++ - 用于编译 .idls 的 Makefile

c++ - 显式模板实例化是否在 cpp 或头文件中?

python - 如何计算最近的半正定矩阵?