如何将ProductReturnType转换为矩阵

How do I convert a ProductReturnType into a matrix?

本文关键字:转换 ProductReturnType      更新时间:2023-10-16

我使用c++线性代数库的特征。我尝试了两个矩阵相乘:

static void do_stuff_with_matrix(Eigen::MatrixXf& mat) {
  return;
}
Eigen::MatrixXf a(3, 4);
Eigen::MatrixXf b(4, 5);
Eigen::MatrixXf c = a * b;
do_stuff_with_matrix(c);

不幸的是,我得到一个编译器错误,说明ProductReturnType (c是)不能转换为Eigen::MatrixXf&。如何执行这种转换?

Eigen使用延迟求值是为了防止不必要的临时和其他事情。因此,c本质上是ProductReturnType,是矩阵乘积的优化结构:

template<typename Lhs, typename Rhs, int ProductType>
class Eigen::ProductReturnType< Lhs, Rhs, ProductType >

Helper类,以获得operator*的正确和优化的返回类型。[另见2]

为了从形式为A * B的表达式创建一个实矩阵,您需要直接对其求值:

Eigen::MatrixXf c = (a * b).eval();
do_stuff_with_matrix(c);

有关Eigen的延迟求值和混叠的更多信息,请参阅本页