xtensor icon indicating copy to clipboard operation
xtensor copied to clipboard

reshape view doesn't infer shape from -1

Open JosephSummerhays1 opened this issue 2 years ago • 1 comments

https://github.com/xtensor-stack/xtensor/issues/926

behold my code:

int main() {                   
    xt::xarray<float> a = xt::zeros<float>({3, 3, 3});
    auto b = xt::reshape_view(a, {3, -1});
    cout << xt::adapt(a.shape()) << endl;
    cout << xt::adapt(b.shape()) << endl;
}

and it's unexpected output

{3, 3, 3}
{                   3, 18446744073709551615}

While nowhere in the documentation does it claim to handle -1 as numpy does, and this is probably just my fault for assuming this, it would be very convenient if it did.

JosephSummerhays1 avatar Jul 22 '21 22:07 JosephSummerhays1

As method reshape seems to support having one entry -1, see docs. In my opinion reshape_view should also support this. However, in your case -1 seems to be casted to a signed integer. So I think this is a bug. But maybe others disagree?

tdegeus avatar Aug 01 '21 13:08 tdegeus

@tdegeus I agree with @JosephSummerhays1 I've done this several times and it's a nightmare to find when xt::xarray<T>::reshape does it. I also find myself doing xt::reshape_view(data, {data.size() / lastAxisLength, lastAxisLength}) this is pretty ugly.

spectre-ns avatar Nov 10 '23 16:11 spectre-ns