xtensor icon indicating copy to clipboard operation
xtensor copied to clipboard

Fix xaxis_*_iterator shape and strides types

Open Stef-Sijben opened this issue 2 years ago • 1 comments

Checklist

  • [x] The title and commit message(s) are descriptive.
  • [x] Small commits made to fix your PR have been squashed to avoid history pollution.
  • [x] Tests have been added for new features or bug fixes.
  • [x] API of new functions and classes are documented.

Description

Fixes #2116.

As described by @dchansen in #2723:

This issue for both cases is that the strides_type and shape_type in axis_slice_iterator and axis_iterator are taken from the given xexpression, which is not correct for xtensor as the resulting strided_view should anyhow be dynamically sized.

The alternative would be to assign a dynamic strides and shape_type, but it was not clear to me what the correct replacement type should be.

So I changed the type of the shape and strides members in the returned views/iterators as follows:

  • For xaxis_iterator: std::vector<T>, where T is the value type of the input expression's shape or strides type.
  • For xaxis_slice_iterator: std::array<T, 1>, since the resulting view is always one-dimensional. Note that this is different from the currently returned shape and strides type in case of xarray input, so this breaks API/ABI for those cases. If that is not desired, this case should also use std::vector<T>.

For both iterators, this loses some compile-time information in certain cases. E.g.:

  • For xtensor_fixed inputs, the resulting view could also have compile-time shape and strides, but some more metaprogramming magic would be required to correctly detect that situation and select the correct types.
  • For xtensor inputs, xaxis_iterator's view could have fixed dimension of N-1.

Someone else would have to look into that if that's desired. However, I think the above paragraph would be more of an optimization, and this fix at least makes the iterators on xtensor and xtensor_fixed functionally correct, in the sense that their views have the correct shape and contain the correct elements.

I also made the iterator test cases type parameterized, so the existing test cases check all of xarray, xtensor, and xtensor_fixed as input types.

Stef-Sijben avatar Nov 22 '23 13:11 Stef-Sijben

Also fixes #2543.

hsanzg avatar Oct 11 '24 10:10 hsanzg