Add rot90 method to rotate an array 90 degrees in a plane (np.rot90 equivalent)#1603
Open
teddytennant wants to merge 1 commit into
Open
Add rot90 method to rotate an array 90 degrees in a plane (np.rot90 equivalent)#1603teddytennant wants to merge 1 commit into
teddytennant wants to merge 1 commit into
Conversation
Add `ArrayBase::rot90(k, axes)`, the equivalent of NumPy's `numpy.rot90`. It rotates the array `k` times by 90 degrees in the plane spanned by the two given axes, going from `axes.0` toward `axes.1`. Like the existing axis-manipulation methods it moves no data, only adjusting the dimensions and strides via `swap_axes`/`invert_axis`. `k` is reduced modulo 4 with `rem_euclid`, so negative values rotate in the opposite direction. Closes rust-ndarray#866.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds a new public method
ArrayBase::rot90(k, axes)that rotates an array by 90 degreesktimes in the plane spanned by two axes, mirroring NumPy'snumpy.rot90.axes.0towardaxes.1(counterclockwise in that axis ordering), matching NumPy.kis reduced modulo 4 viarem_euclid, so negativekrotates in the opposite (clockwise) direction andk == -1equalsk == 3.swap_axes/invert_axis, so it is O(1). It consumesself; call it on.view()/.clone()to keep the original.#[track_caller], and panics if the two axes are equal or out of bounds.Why
There is currently no built-in equivalent to
np.rot90; users have to hand-composeswap_axes/invert_axis(or multiply by a flipped identity matrix). Issue #866 (help-wanted) requests it, and a maintainer asked for a volunteer to implement the full NumPy-equivalent including thekandaxesparameters.Testing
test_rot90intests/array.rscovering all four rotation cases against hand-verified NumPy results onarr2(&[[1,2,3],[4,5,6]]), thek == 0/k == 4identity, negative-k(clockwise) behavior, reversed-axis-plane direction, and a 3-D shape check ((1,2,3)rotated in plane(0,2)->[3,2,1]).test_rot90_same_axisandtest_rot90_oob#[should_panic]tests for the two panic conditions.cargo test --test array test_rot90-> 3 passed;cargo test --doc -- rot90-> doc-test passes.RELEASES.md.Fixes #866.