Skip to content

Optional axis argument for NDArray::shape() with negative indexing - #20

Merged
CodeWithKyrian merged 1 commit into
mainfrom
feat/ndarray-shape-axis-accessor
May 14, 2026
Merged

CodeWithKyrian merged 1 commit into
mainfrom
feat/ndarray-shape-axis-accessor

Conversation

@CodeWithKyrian

Copy link
Copy Markdown
Contributor

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 Changed

  • shape(?int $axis = null) returning the full list<int> when omitted or null, or one int length when an axis is given.
  • Negative axis indices resolved from the last dimension, with IndexException on out-of-bounds axis indices or when indexing shape on a zero-dimensional array.
  • PHPDoc with a conditional return template for static analyzers.
  • Unit tests covering null vs omitted behavior, positive and negative axes, bounds failures, and the zero-dimensional case.
  • User-facing docs updated on the NDArray class page, API quick reference, landing comparison table, fundamentals and quick-start guides, and the NumPy migration guide where shape is discussed.

Breaking Changes
None. Existing shape() calls without arguments behave the same.

@CodeWithKyrian
CodeWithKyrian merged commit 95736b6 into main May 14, 2026
14 checks passed
@CodeWithKyrian
CodeWithKyrian deleted the feat/ndarray-shape-axis-accessor branch May 14, 2026 07:35
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.

1 participant