0

I have a (3D) Eigen Tensor (Eigen::TensorFixedSize<double, Eigen::Sizes<nx, ny, nz>>) and I'd want to pass a slice of it to a function, which modifies the original tensor. The original tensor and the slice have always the same shape (i.e. the slice is always a Eigen::TensorFixedSize<double, Eigen::Sizes<nx, 1, 1>>).

I can do something like

#include <unsupported/Eigen/CXX11/Tensor>
#include <Eigen/Dense>

int main() {
    const nx = 10;
    const ny = 10;
    const nz = 10;
    Eigen::TensorFixedSize<double, Eigen::Sizes<nx, ny, nz>> x;
    Eigen::TensorFixedSize<double, Eigen::Sizes<nx, 1, 1>> myslice;
    
    x.SetZero();
    
    Eigen::array<Eigen::Index, 3> offsets = {0, 0, 0};
    Eigen::array<Eigen::Index, 3> extents = {nx, 1, 1};
    
    myslice = x.slice(offsets, extents);

    do_something(myslice);

    x.slice(offsets, extents) = myslice;

    //now the slice of x is modified

    return 0;
}


void do_something(Eigen::TensorFixedSize<double, Eigen::Sizes<nx, 1, 1>> vector) {

    //do something on the slice
    vector.setConstant(123.0);

    return;

}

But I'd like to directly pass the slice (do_something(x.slice(...,...))) and have the full array modified as a result.

I tried the things suggested by the Eigen documentation regarding passing Eigen objects as parameters (which is for Eigen::Matrix) but I did not manage to find the right way to do this. I'm not even sure this is possible.

  • Dupe: [Function that takes VectorXf and can modify its values](https://stackoverflow.com/questions/73320722/function-that-takes-vectorxf-and-can-modify-its-values). – Jason Nov 04 '22 at 10:46
  • Got it, if I `const_cast` the slice I can modify it inside the function. The problem is that the slice of the full array is not modified in the outer scope. – davideperrone Nov 04 '22 at 11:17

0 Answers0