Skip to content

Conversation

@Arsalaan-Alam
Copy link
Contributor

@Arsalaan-Alam Arsalaan-Alam commented Nov 16, 2025

This PR fixes #151

Updated slicing logic in nx/lib/core/frontend.ml:

  • Only skip work when index lists are in true identity order: [0; 1; 2; …; n-1]
  • For any other case (reordering, duplicates), dispatch to gather so the result matches expected semantics.

Added the following tests in nx/test/test_nx_indexing.ml:

  • test_index_idx_reorder_rows
  • test_index_idx_duplicate_rows

These tests ensure correct behavior for both permutations and repeated indices.

@ghennequin
Copy link

ghennequin commented Nov 16, 2025

Thanks! There should be a more efficient way of checking whether or not indices is the [0;1;...n-1] range (your solution allocates a whole [0; 1; ...; n-1] range of indices, and then tests full equality with the provided indices; this introduces quite a bit of overhead). Can I suggest this instead:

let is_range n indices =
  let rec traverse id = function
    | [] -> id=n
    | hd :: tl when hd = id -> traverse (succ id) tl
    | _ -> false
  in
  traverse 0 indices

E.g.
is_range 3 [0;1;2] → true
is_range 3 [1;2;3] → false
is_range 3 [0;2;1] → false
...etc.

@Arsalaan-Alam
Copy link
Contributor Author

you're right! thanks for pointing this out, updated the implementation

@ghennequin
Copy link

looks good to me, thanks @Arsalaan-Alam !

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.

bug in fancy slicing

2 participants