Skip to content

NDArray M2: multi-dimensional slicing with :: syntax#75

Merged
Quafadas merged 2 commits intomainfrom
copilot/m2-upgrade-ndarray-slicing
Apr 8, 2026
Merged

NDArray M2: multi-dimensional slicing with :: syntax#75
Quafadas merged 2 commits intomainfrom
copilot/m2-upgrade-ndarray-slicing

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 8, 2026

Extends NDArray[A] with a varargs apply(selectors: RangeExtender*) that brings the same ergonomic slicing already available on Matrix to N-dimensional arrays.

API

val cube = NDArray(data, Array(4, 5, 3))

cube(::, 1 until 3, ::)       // shape [4, 2, 3] — zero-copy view
cube(0 until 2, ::, ::)       // shape [2, 5, 3] — zero-copy view
cube(::, ::, Array(0, 2))     // shape [4, 5, 2] — gather (copy)

RangeExtender = Range | Array[Int] | ::.type is reused as-is from rangeExtender.scala.

Implementation (vecxt/src/ndarrayOps.scala)

  • Zero-copy path — when every selector is ::, a step-1 Range, or a contiguous Array[Int]: returns a strided view sharing the backing array, adjusting only offset and shape; strides are unchanged.
  • Gather path — when any selector is non-contiguous: resolves all selectors to Array[Int] and iterates output indices in col-major order to fill a fresh contiguous array.
  • Bounds validation always on (varargs precludes transparent inline): validates selector count matches ndim, range start/end within shape, and each Array[Int] element within shape.

Note: a single Array[Int] on a 1-D NDArray (arr(Array(0,3,7))) resolves via Scala's overload rules to the existing N-D element-access apply(indices: Array[Int]), not the new slicing overload. Multi-argument calls (e.g. arr(Array(0,2), ::)) are unambiguous.

Tests (vecxt/test/src/ndarraySlicing.test.scala)

Covers 1-D, 2-D, and 3-D cases across all selector types; zero-copy mutation visibility; consistency with slice; and error paths (wrong selector count, out-of-bounds range, out-of-bounds gather index).

Copilot AI linked an issue Apr 8, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Implement multi-dimensional indexing for NDArray using :: NDArray M2: multi-dimensional slicing with :: syntax Apr 8, 2026
Copilot AI requested a review from Quafadas April 8, 2026 07:55
@Quafadas Quafadas marked this pull request as ready for review April 8, 2026 07:58
@Quafadas Quafadas merged commit 1dfb75b into main Apr 8, 2026
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.

M2 upgrade

2 participants