Re: [eigen] Returning Tensor Expressions

[ Thread Index | Date Index | More Archives ]

Hi Toby,

could you specify which C++ standard you are using?

The possible implementations for a free function that replaces your lambda depends on this.
E.g. since C++14 you should be able to just declare the function as

  auto func(...)


On 17. Dec 2019, at 13:15, Wood, Tobias <tobias.wood@xxxxxxxxx> wrote:

I am trying to write a finite difference function for Eigen::Tensors. Currently I am using a lambda:
auto diff = [](Eigen::Tensor<std::complex<float>, 3> const &a, Eigen::Index const d) {
  Dims3 const sz{a.dimension(0) - 2, a.dimension(1) - 2, a.dimension(2) - 2};
  Dims3 const st1{1, 1, 1};
  Dims3 fwd{1, 1, 1};
  Dims3 bck{1, 1, 1};
  fwd[d] = 2;
  bck[d] = 0;
  return (a.slice(fwd, sz) - a.slice(bck, sz)) / a.slice(st1, sz).constant(2.f);
This works okay. However, I would like to do two things:
1 – Change this from a lambda into a free function. What should the return type of the function be, so that it returns the _expression_/operation and does not evaluate the tensor into a temporary?
2 – I would prefer to pass in a TensorRef, so I can pass in a .chip() from a 4D tensor without a temporary. When I try to do this with the current lambda, and I am assigning to a slice, e.g.
b.chip<3>(0).slice(st1, sz) = diff(a, 0);
I get the following error:
TensorRef.h:413:51: error: cannot initialize return object of type 'Eigen::TensorEvaluator<const Eigen::TensorRef<Eigen::Tensor<std::__1::complex<float>, 3, 0, long> >, Eigen::ThreadPoolDevice>::Scalar *' (aka 'std::__1::complex<float> *') with an rvalue of type 'const Eigen::TensorRef<Eigen::Tensor<std::__1::complex<float>, 3, 0, long> >::Scalar *' (aka 'const std::__1::complex<float> *')
This appears to be complaining that I can’t assign a `const std::complex<float> *` to a `std::complex<float> *`?
Thanks in advance,

Mail converted by MHonArc 2.6.19+