Skip to content

Fix xview::element when a newaxis precedes an integral slice - #2958

Open
gouarin wants to merge 1 commit into
xtensor-stack:masterfrom
gouarin:fix-xview-element-newaxis
Open

gouarin wants to merge 1 commit into
xtensor-stack:masterfrom
gouarin:fix-xview-element-newaxis

Conversation

@gouarin

@gouarin gouarin commented Oct 29, 2026

Copy link
Copy Markdown
Member

Checklist

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

Description

xview::element returned the wrong value when a newaxis comes before an integral slice:

xt::xarray<int> a = {{0, 1, 2}, {3, 4, 5}};
auto v = xt::view(a, xt::newaxis(), 1, xt::all());
v[{0, 2}];  // 3 instead of 5

make_index finds the slice k of each underlying dimension i, then 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 k. operator() already did it right.

xview.element_newaxis_before_integral fails on master and passes with the fix. I found this while writing a differential test for the xview_stepper fix, which used element() as its reference.

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.
@codspeed

codspeed Bot commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ Unable to measure the execution of 1 benchmark

This benchmark measured no execution time, so its result is not comparable and counts as unchanged. This usually happens when the compiler removes the code under test.

Prevent compiler optimizations

See affected benchmarks

✅ 255 untouched benchmarks


Comparing gouarin:fix-xview-element-newaxis (4b4e548) with master (57f3d07)

Open in CodSpeed

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant