Eigen const TensorMap
Asked Answered
V

1

8

Consider the following (working) snippet:

Eigen::ArrayXd x (8);
x << 1, 2, 3, 4, 5, 6, 7, 8;
Eigen::TensorMap<Eigen::Tensor<double, 2>> y (x.data(), 2, 4);

This is also works:

const Eigen::ArrayXd const_x = x;
const Eigen::Map<const Eigen::ArrayXXd> z (const_x.data(), 2, 4);

I'm trying to figure out why I can't do this though:

const Eigen::TensorMap<const Eigen::Tensor<double, 2>> const_y (const_x.data(), 2, 4);

I'm using Eigen 3.3.3 (also tried 3.3.4)

Vere answered 24/7, 2017 at 14:41 Comment(3)
Could you add your compilation error message?Bad
Try "const Eigen::TensorMap<Eigen::Tensor<const double, 2>> const_y (const_x.data(), 2, 4);". Note the "const double" inside Eigen::Tensor.Linskey
Did you try the suggestion by @CarlodelMundo ? It seems to work, even though it is not standard Eigen-syntax -- but the Tensor-module as a whole does not follow several Eigen standards (starting from naming it TensorMap, instead of Map<Tensor> and Map<const Tensor>).Margy
A
6

You are trying to store a const tensor.

Error 2 error C2664: 'Eigen::TensorMap<PlainObjectType>::TensorMap(double *,__w64 int,__w64 int)' : impossible to convert parameter 1 from 'const double *' to 'double *'

I think you meant to have a tensor on a const double (as mentioned by @CarlodelMundo too).

const Eigen::TensorMap<Eigen::Tensor<const double, 2>> const_y(const_x.data(), 2, 4);

In https://eigen.tuxfamily.org/dox/unsupported/TensorMap_8h_source.html it seems that there is no constructor that takes a const as a parameter 1.

Accelerant answered 31/7, 2017 at 8:26 Comment(1)
I'm aware of the fix mentioned by @CarlodelMundo, but it doesn't feel Eigen-esque (as pointed out by @chtz)Vere

© 2022 - 2024 — McMap. All rights reserved.