Skip to content

Adapting std::array and using in expression results in xtensor, not xtensor_fixed #2337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
vakokako opened this issue Mar 23, 2021 · 3 comments

Comments

@vakokako
Copy link

Take a look at this example:

std::array shape{3, 4, 5};
auto shape_adapt = xt::adapt(shape);
auto tensor = xt::eval(2 * shape_adapt);

The resulting type of tensor is xtensor_container, which is vector underneath. So we went from array to vector.

This is a very common use case for working with tensor shapes and would optimize quite a bit. Currently we go for std::transform and xtl::make_sequence in these cases, which is not so nice. Adapting shapes would be much nicer))

@tdegeus
Copy link
Member

tdegeus commented Mar 29, 2021

I think this is a good idea to have. I do think though that using shape as an example is confusing. xt::adapt transforms data into an xtensor object (of the appropriate shape). It does not allocate a tensor of a certain shape (as the example is suggesting).

Furthermore, there is a question about defaults. In particular, I do think that xtensor_fixed should get a bit of attention before this could be the default: I have been having many problems on Windows, so personally I have temporarily stopped using xtensor_fixed altogether.

In that light, it may make sense to allow an option or template argument for xt::adapt?

@vakokako
Copy link
Author

vakokako commented Apr 3, 2021

The example was about adapting the shape of the container (usually std::array or std::vector) to perform some arithmetic on it (get the shape that is twice as big).

Sorry for the confusion.

Also a nice example is working with multidimensional indices: e.g. getting the central element:

auto& center = tensor[xt::adapt(tensor.shape()) / 2];

@tdegeus
Copy link
Member

tdegeus commented Apr 3, 2021

@vakokako OK, that clarifies things. But then isn't the wanted extension more that some of xtensor's function should work on xtensor objects too (if they now only work on std::array or std::vector)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants