Skip to content

Commit 4b4e548

Browse files
committed
Fix xview::element when a newaxis precedes an integral slice
make_index maps each dimension i of the underlying expression to its slice k, then to the position of that slice in the view index: k minus the number of integral slices before it. It counted the integral slices before i instead of before k. The two differ when a newaxis comes before an integral slice, so the wrong index was read: xt::xarray<int> a = {{0, 1, 2}, {3, 4, 5}}; auto v = xt::view(a, xt::newaxis(), 1, xt::all()); v[{0, 2}]; // returned 3 instead of 5 operator() already used the slice position. element(), and the operator[] and at-style accesses built on it, now do too.
1 parent 57f3d07 commit 4b4e548

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎include/xtensor/views/xview.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1707,7 +1707,7 @@ namespace xt
17071707

17081708
// need to advance captured `first`
17091709
first = first_copy;
1710-
std::advance(first, static_cast<diff_type>(k - xt::integral_count_before<S...>(i)));
1710+
std::advance(first, static_cast<diff_type>(k - xt::integral_count_before<S...>(k)));
17111711

17121712
if (first < last)
17131713
{

‎test/test_xview.cpp‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,28 @@ namespace xt
17361736
EXPECT_EQ(res, 2);
17371737
}
17381738

1739+
TEST(xview, element_newaxis_before_integral)
1740+
{
1741+
xarray<int> a = xt::arange<int>(24).reshape({2, 3, 4});
1742+
1743+
// The newaxis shifts the view dimensions that follow the integral slice.
1744+
auto v = view(a, newaxis(), 1, all());
1745+
auto w = view(a, newaxis(), keep(1), 0, newaxis(), drop(0, 3));
1746+
for (std::size_t j = 0; j < 3; ++j)
1747+
{
1748+
for (std::size_t k = 0; k < 4; ++k)
1749+
{
1750+
xindex idx = {0, j, k};
1751+
EXPECT_EQ(v[idx], a(1, j, k));
1752+
}
1753+
}
1754+
for (std::size_t k = 0; k < 2; ++k)
1755+
{
1756+
xindex idx = {0, 0, 0, k};
1757+
EXPECT_EQ(w[idx], a(1, 0, k + 1));
1758+
}
1759+
}
1760+
17391761
TEST(xview, view_reshape_view)
17401762
{
17411763
xtensor<int, 1> a = {0, 1, 2};

0 commit comments

Comments
 (0)