【问题标题】:Lambda Function Elementwise in EigenEigen 中的 Lambda 函数 Elementwise
【发布时间】:2018-06-26 20:36:42
【问题描述】:

这是以下先前问题的组合:Apply function to all Eigen matrix elementSet coefficients of an Eigen::Matrix according an arbitrary distribution。基本上我正在尝试生成一个特征矩阵,其系数从高斯分布中采样。

这是我执行此操作的代码(静态类方法),它返回一个相当神秘的错误消息:

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

返回错误: 错误:

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;

【问题讨论】:

  • 这看起来不像一元函数,它看起来像一个空函数。如果您不能使用给定的参数调用给定的函数,std::result_of 是未定义的。试试[&amp;](float /*unused*/) 吧?

标签: c++ eigen eigen3


【解决方案1】:

正如 o11c 所注意到的,这确实是一个空表达式,在 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";
}

【讨论】:

  • 虽然这在我的 Mac 上可以正常工作,但在 Ubuntu 16.04 上使用g++ -std=c++11 会引发错误。 /usr/include/eigen3/Eigen/src/Core/CoreEvaluators.h:348:27: 错误:不匹配调用 '(const main()::) (Eigen::Index&)' return m_functor(索引); ^ abc.cpp:8:23: 注意: 候选: main():: auto poisson = [&] () {return distribution(generator);}; ^ abc.cpp:8:23: 注意:候选人期望 0 个参数,提供 1 个
  • 你需要 Eigen 3.3.x.
猜你喜欢
  • 1970-01-01
  • 2015-04-19
  • 1970-01-01
  • 2022-01-23
  • 2023-03-10
  • 2020-07-05
  • 2022-12-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多