Optional axis argument for NDArray::shape() with negative indexing - #20
Merged
Merged
Conversation
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.
This PR extends
NDArray::shape()so callers can read the full shape as today or pass an axis index (including negative indices from the last dimension) to get a single dimension length, with clear errors for out-of-range axes and zero-dimensional arrays, plus tests and documentation updates.Motivation and Context
PHP only exposes the shape as a list, so reading the last dimension or a specific axis without extra indexing gymnastics is awkward compared to tuple-style access in other ecosystems. An optional axis argument keeps the default call identical while giving a small, discoverable API that matches how axis indices already work elsewhere on the class.
What’s
Changedshape(?int $axis = null)returning the fulllist<int>when omitted or null, or one int length when an axis is given.IndexExceptionon out-of-bounds axis indices or when indexing shape on a zero-dimensional array.Breaking
ChangesNone. Existing
shape()calls without arguments behave the same.