如何使Eigen :: Tensor代码更简洁?

问题描述

我对Eigen/Core很满意,但需要一些更高维的数组。因此,我正在学习Eigen::Tensor。我可以获取以下代码进行编译,但发现语法不干净。我可以做得更好吗?评论中有两个问题。

  Eigen::Tensor<float,3> t_3d(2,3,4);
  t_3d.setZero();
  // Is there a shorter and less repetitive way to write this? e.g. like this
  // t_3d.chip(/*offset=*/1,/*dim=*/0) *= 0.5;
  // auto seems to work:
  // auto c = t_3d.chip(/*offset=*/1,/*dim=*/0);
  // c *= c.constant(0.5);
  t_3d.chip(/*offset=*/1,/*dim=*/0) *=
      t_3d.chip(/*offset=*/1,/*dim=*/0).constant(0.5);

  Eigen::Tensor<float,3> expected(2,4);
  expected.setZero();

  // Is there a shorter way to write this? e.g. by not declaring eq and using
  // 1 line.
  // Maybe this is the best we can do as doc also does that?
  // doc: https://eigen.tuxfamily.org/dox/unsupported/eigen_tensors.html#title7
  // related: https://stackoverflow.com/questions/52392465/how-to-multiply-an-eigen-tensor-by-the-scalar-sum-of-another-eigen-tensor-in-c/52403691
  Eigen::Tensor<bool,0> eq = (t_3d == expected).all();
  EXPECT_TRUE(eq(0));

此外,由于不支持Eigen::Tensor,有没有很好的选择? (我已经在代码库中使用了Eigen::Matrix等。)谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)