Skip to content

Add rot90 method to rotate an array 90 degrees in a plane (np.rot90 equivalent)#1603

Open
teddytennant wants to merge 1 commit into
rust-ndarray:masterfrom
teddytennant:add-rot90-method
Open

Add rot90 method to rotate an array 90 degrees in a plane (np.rot90 equivalent)#1603
teddytennant wants to merge 1 commit into
rust-ndarray:masterfrom
teddytennant:add-rot90-method

Conversation

@teddytennant

Copy link
Copy Markdown

What

Adds a new public method ArrayBase::rot90(k, axes) that rotates an array by 90 degrees k times in the plane spanned by two axes, mirroring NumPy's numpy.rot90.

  • Rotation goes from axes.0 toward axes.1 (counterclockwise in that axis ordering), matching NumPy.
  • k is reduced modulo 4 via rem_euclid, so negative k rotates in the opposite (clockwise) direction and k == -1 equals k == 3.
  • No data is copied: it only adjusts dimensions and strides via the existing swap_axes / invert_axis, so it is O(1). It consumes self; 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-compose swap_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 the k and axes parameters.

Testing

  • Added test_rot90 in tests/array.rs covering all four rotation cases against hand-verified NumPy results on arr2(&[[1,2,3],[4,5,6]]), the k == 0 / k == 4 identity, 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]).
  • Added test_rot90_same_axis and test_rot90_oob #[should_panic] tests for the two panic conditions.
  • Included a rustdoc doc-test on the method.
  • cargo test --test array test_rot90 -> 3 passed; cargo test --doc -- rot90 -> doc-test passes.
  • Added an entry to RELEASES.md.

Fixes #866.

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.
Copilot AI review requested due to automatic review settings July 8, 2026 16:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

rot90 function?

2 participants