本征中的 Lambda 函数元素

Lambda Function Elementwise in Eigen

这是以下先前问题的组合: and Set coefficients of an Eigen::Matrix according an arbitrary distribution。基本上我试图生成一个特征矩阵,其系数从高斯分布中采样。

这是我执行此操作的代码(静态 class 方法),其中 returns 一个相当神秘的错误消息:

matrix_eig EigenUtil::GaussianNoise(size_t rows, size_t cols,
                                    float mean, float std) {
  matrix_eig m(rows, cols);
  std::mt19937 rng;
  std::normal_distribution<float> nd(mean, std);
  auto sampler = [&]() { return nd(rng); };
  return matrix_eig::Zero(rows, cols).unaryExpr(sampler);
}

其中 returns 错误: 错误:

no type named 'type' in 'std::__1::result_of<(lambda at eigen_util.cpp:101:18) (const float &)>'
  typedef typename std::result_of<T>::type type1;

正如 o11c 所注意到的,这确实是一个 nullary-expression,并且 doc 中有几乎完全相同的示例。为了方便我复制了它:

#include <Eigen/Core>
#include <iostream>
#include <random>
using namespace Eigen;
int main() {
  std::default_random_engine generator;
  std::poisson_distribution<int> distribution(4.1);
  auto poisson = [&] () {return distribution(generator);};
  RowVectorXi v = RowVectorXi::NullaryExpr(10, poisson );
  std::cout << v << "\n";
}