Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix comparison with NumPy of slicing with negative step #1319

Merged
merged 1 commit into from Aug 6, 2023

Conversation

venkat0791
Copy link
Contributor

The existing documentation states that the behavior of slicing is the same as in NumPy except when step < -1, implying that the behavior is the same when step = -1. But this is not true:

In [1]: import numpy as np

In [2]: x = np.arange(10)

In [3]: x[2 : 5 : -1] # Analogous slice in ndarray: array![4, 3, 2]
Out[3]: array([], dtype=int32)

In [4]: x[5 : 2 : -1] # Analogous slice in ndarray: array![]
Out[4]: array([5, 4, 3])

So step < -1 should be replaced by step < 0 in the documentation.

There are some further differences in slicing behavior with negative step, having to do with the default values for start and end:

In [5]: x[: 7 : -1] # Analogous slice in ndarray: array![6, 5, 4, 3, 2, 1, 0]
Out[5]: array([9, 8])

In [6]: x[7 : : -1] # Analogous slice in ndarray: array![9, 8, 7]
Out[6]: array([7, 6, 5, 4, 3, 2, 1, 0])

The existing documentation states that the behavior of slicing is the same as
in NumPy except when `step < -1`, implying that the behavior is the same when
`step = -1`. But this is not true:

In [1]: import numpy as np

In [2]: x = np.arange(10)

In [3]: x[2 : 5 : -1]  # Analogous slice in `ndarray`: `array![4, 3, 2]`
Out[3]: array([], dtype=int32)

In [4]: x[5 : 2 : -1]  # Analogous slice in `ndarray`: `array![]`
Out[4]: array([5, 4, 3])

So `step < -1` should be replaced by `step < 0` in the documentation.

There are some further differences in slicing behavior with negative step,
having to do with the default values for `start` and `end`:

In [5]: x[: 7 : -1]  # Analogous slice in `ndarray`: `array![6, 5, 4, 3, 2, 1, 0]`
Out[5]: array([9, 8])

In [6]: x[7 : : -1]  # Analogous slice in `ndarray`: `array![9, 8, 7]`
Out[6]: array([7, 6, 5, 4, 3, 2, 1, 0])
@nilgoyette
Copy link
Collaborator

Thank you @venkat0791 for fixing this wrong information. The CI tests are failing, but it's not related to this MR so I'll merge anyway.

I don't remember using negative slicing in ndarray so I learned something :)

@nilgoyette nilgoyette merged commit e21665f into rust-ndarray:master Aug 6, 2023
2 of 7 checks passed
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.

None yet

2 participants