From 1433579296349af627330f0c9463b752a8f46163 Mon Sep 17 00:00:00 2001 From: Tyagiquamar Date: Sun, 4 Oct 2026 21:36:54 +0530 Subject: [PATCH] Fix squeeze offset for sliced views --- include/xtensor/misc/xmanipulation.hpp | 16 ++++++++++++++-- test/test_xmanipulation.cpp | 13 +++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/include/xtensor/misc/xmanipulation.hpp b/include/xtensor/misc/xmanipulation.hpp index 56a78ca4b..db4125e01 100644 --- a/include/xtensor/misc/xmanipulation.hpp +++ b/include/xtensor/misc/xmanipulation.hpp @@ -536,7 +536,13 @@ namespace xt } ); - return strided_view(std::forward(e), std::move(new_shape), std::move(new_strides), 0, e.layout()); + return strided_view( + std::forward(e), + std::move(new_shape), + std::move(new_strides), + detail::get_offset(e), + e.layout() + ); } namespace detail @@ -559,7 +565,13 @@ namespace xt } } - return strided_view(std::forward(e), std::move(new_shape), std::move(new_strides), 0, e.layout()); + return strided_view( + std::forward(e), + std::move(new_shape), + std::move(new_strides), + detail::get_offset(e), + e.layout() + ); } template diff --git a/test/test_xmanipulation.cpp b/test/test_xmanipulation.cpp index bbc2663a2..32f5d4370 100644 --- a/test/test_xmanipulation.cpp +++ b/test/test_xmanipulation.cpp @@ -300,6 +300,19 @@ namespace xt EXPECT_EQ(sq3(2, 2, 0, 1, 0, 2), b(2, 2, 0, 0, 1, 0, 2)); } + TEST(xmanipulation, squeeze_offset) + { + xtensor a = {{1, 2, 3}}; + xtensor expected = {2, 3}; + + EXPECT_EQ(squeeze(view(a, all(), range(1, 3))), expected); + EXPECT_EQ(squeeze(view(a, all(), range(1, 3)), 0, check_policy::full()), expected); + + auto expression = view(a, all(), range(1, 3)) + 0; + EXPECT_EQ(squeeze(expression), expected); + EXPECT_EQ(squeeze(expression, 0, check_policy::full()), expected); + } + TEST(xmanipulation, expand_dims) { auto b = xt::xarray::from_shape({3, 3});