Skip to content

Indexing in operator() wrong for xview with xt::newaxis()? #2228

Open

Description

@emmenlau

I have a small problem with assigning to an xview that was created from an xtensor with xt::newaxis(). The code is given below, and also in a separate PR for review. I'm under the impression that the indexing is somehow incorrect. Here is the code sample:

    xt::xtensor<uint8_t, 2> aTensor = xt::zeros<uint8_t>({4, 3});
    auto aView = xt::view(aTensor, xt::newaxis(), xt::newaxis(), xt::newaxis(), xt::all(), xt::all());
    uint8_t vIndex = 0;
    // Counting only until (vRow < 3) here, otherwise we get an out-of-bounds exception:
    for (size_t vRow = 0; vRow < 3; ++vRow) {
        for (size_t vCol = 0; vCol < 3; ++vCol) {
            aView(size_t{0}, size_t{0}, size_t{0}, vRow, vCol) = vIndex;
            EXPECT_EQ(aTensor(vRow, vCol), vIndex);        ///< This finds the elements are *not* equal
            ++vIndex;
        }
    }

Activity

  1. zYeoman commented on Nov 26, 2020

    @zYeoman
    Contributor

    Similar issue here.

    The pic is a xt::xarray<uint16_t>

    auto&& patch = xt::view(pic, xt::range(i, i + tile_height), xt::range(j, j + tile_width));

    The follow code is correct.

    // Code1
    for (uint k = 0; k < tile_height; ++k) {
        for (uint l = 0; l < tile_width; ++l) {
            patch.at(k, l) = payload.get();
        }
    }

    But these code is wrong

    // Code2
    for (uint k = 0; k < tile_height * tile_width; ++k) {
        patch(k) = payload.get();
    }
  2. emmenlau commented on Nov 26, 2020

    @emmenlau
    ContributorAuthor

    For what its worth, here is what I could see in the debugger:

    • in xt::xview in operator() everything seems correct and indexes are checked correctly.
    • then the view seems to call the operator() of the underlying memory holding container (in my case a 2D xtensor)
      • in this different-dimensional operator(), the indexes seem messed up. It looks like they would be in reverse order, or maybe some padding is missing or too much.
  3. emmenlau commented on Jan 22, 2021

    @emmenlau
    ContributorAuthor

    This would be nice to be fixed, esp. because its a "silent" error where the code actually seems to work but gives wrong results.

  4. emmenlau commented on Mar 17, 2023

    @emmenlau
    ContributorAuthor

    Kindly pinging this issue...

  5. emmenlau commented on Jun 4, 2024

    @emmenlau
    ContributorAuthor

    Kindly pinging this issue...

  6. emmenlau commented on Mar 31, 2025

    @emmenlau
    ContributorAuthor

    Kindly pinging this issue, that seems to perform wrong indexing (could be a bad bug?)

  7. Alex-PLACET commented on May 4, 2026

    @Alex-PLACET
    Contributor

    Fixed in #2896

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions