Skip to content

NDArray M4: Reduction Operations#73

Merged
Quafadas merged 6 commits intomainfrom
copilot/implement-ndarray-reduction-operations
Apr 8, 2026
Merged

NDArray M4: Reduction Operations#73
Quafadas merged 6 commits intomainfrom
copilot/implement-ndarray-reduction-operations

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 8, 2026

Implements Milestone 4 — reduction operations for NDArray[Double]: full reductions, axis reductions, arg reductions, and linear algebra (dot/matmul), as specified in the design doc.

New: vecxt/src/ndarrayReductions.scala

Full reductions — return Double, col-major fast path delegates to existing SIMD/BLAS Array[Double] extensions:

  • sum, mean, min, max, product, variance, norm
  • argmax, argmin (flat col-major index)

Axis reductions — output shape removes the reduced dimension (NumPy keepdims=False semantics):

  • sum(axis), mean(axis), min(axis), max(axis), product(axis), argmax(axis), argmin(axis)

Linear algebra:

  • dot — 1-D only, delegates to BLAS ddot on col-major inputs
  • matmul / @@ — 2-D × 2-D, materialises non-contiguous inputs then delegates to BLAS dgemm via Matrix
val a = NDArray(Array(1.0, 2.0, 3.0, 4.0, 5.0, 6.0), Array(2, 3))

a.sum          // 21.0
a.mean         // 3.5
a.argmax       // 5
a.sum(axis = 0)  // NDArray shape [3]: [3.0, 7.0, 11.0]
a.sum(axis = 1)  // NDArray shape [2]: [9.0, 12.0]

val b = NDArray(Array(7.0, 8.0, 9.0, 10.0, 11.0, 12.0), Array(3, 2))
a @@ b  // NDArray shape [2,2]: [[76, 103], [100, 136]]

Implementation notes

  • General strided kernel iterates in column-major coordinate order — same pattern as NDArrayDoubleOps.
  • All SIMD acceleration is inherited by delegating col-major full reductions to the existing arrays.* extensions; no platform-specific code required.
  • NDArrayReductionHelpers is a private top-level object (not nested inside NDArrayReductions) to work around a Scala 3 resolution issue where extension methods defined in an object shadow same-named extensions from imports, even for different receiver types.

Other changes

  • vecxt/src/all.scala — exports NDArrayReductions.*
  • vecxt/test/src/helpers.forTests.scala — adds assertNDArrayShapeAndClose
  • vecxt/test/src/ndarrayReductions.test.scala — cross-platform tests covering all operations, 2-D and 3-D axis reductions (with hand-verified values from the design doc), strided/transposed paths, and error cases

Copilot AI linked an issue Apr 8, 2026 that may be closed by this pull request
Agent-Logs-Url: https://github.com/Quafadas/vecxt/sessions/8f0a084a-c95f-4aa4-9af2-91115cebfc98

Co-authored-by: Quafadas <24899792+Quafadas@users.noreply.github.com>
@Quafadas Quafadas marked this pull request as ready for review April 8, 2026 08:12
Agent-Logs-Url: https://github.com/Quafadas/vecxt/sessions/8f0a084a-c95f-4aa4-9af2-91115cebfc98

Co-authored-by: Quafadas <24899792+Quafadas@users.noreply.github.com>
Copilot AI changed the title [WIP] Add NDArray reduction operations for M4 NDArray M4: Reduction Operations Apr 8, 2026
Copilot AI requested a review from Quafadas April 8, 2026 08:14
@Quafadas Quafadas merged commit d3b6f1d 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.

NDArray M4

2 participants