xtensor-stack / xtensor-python

Python bindings for xtensor
BSD 3-Clause "New" or "Revised" License
347 stars 58 forks source link

Can't use immediate evaluation strategy w/ reductions with xt::pyarray #145

Closed iamthebot closed 6 years ago

iamthebot commented 6 years ago

Attempting to compile the following code results in an error (using xtensor 0.15.9 but the bug was still present when trying against 0.15.2):

mean_ = xt::sum(xt::cast<double>(x), xt::evaluation_strategy::immediate()) / x.size();
.../xtensor/include/xtensor/xreducer.hpp:84:28: error: 'class xt::xfunction<xt::detail::cast<double>::functor<float>, double, const xt::pyarray<float>&>' has no member named 'data'; did you mean 'at'?
             auto begin = e.data().begin();
wolfv commented 6 years ago

The issue is that xt::cast(x) is a xfunction, but the immediate evaluation expects a container.

The fastest solution would be to change the argument so that it reads xt::eval(xt::cast<double>(x))

Or, before calling the function, assigning to a x-container of doubles. Also, not that the result of the reducers is already promoted to a bigger type. E.g. for float -> double, for char -> long long etc. (using the rules in xtensor/xutils big_promote_type.

wolfv commented 6 years ago

Hey, I am going to close this issue here, and reopen on xtensor. I think we could/should run xt::eval inside the immediate reducers, to fix this problem!