Skip to content

Add 0-d NDArray support#84

Merged
Quafadas merged 3 commits into
mainfrom
copilot/add-0d-ndarray-support
Apr 10, 2026
Merged

Add 0-d NDArray support#84
Quafadas merged 3 commits into
mainfrom
copilot/add-0d-ndarray-support

Conversation

Copilot AI commented Apr 10, 2026

Copy link
Copy Markdown
Contributor

NDArray[A] previously rejected empty shapes, precluding 0-dimensional arrays needed for scalar-tensor bridge patterns (e.g., MathTrig[NDArray[Double]] where zero/one/fromDouble must produce shape-agnostic values that broadcast universally).

Core changes

  • NDArrayCheck.scala: Remove the shape.length == 0 rejection from shapeCheck. 0-d arrays are now valid; the dimension loop simply doesn't execute.
  • ndarray.scala: Add isScalar: Boolean property; add NDArray.scalar[A](value) factory producing shape=[], strides=[], numel=1.
  • ndarrayOps.scala: Add scalar (0-arity read) and setScalar (0-arity write) extension methods.
  • ndarrayDoubleOps.scala / ndarrayFloatOps.scala: Add 0-d guards to all binary ops, in-place ops, and comparison ops. A 0-d operand is auto-promoted by delegating to the existing scalar extension — this is the only implicit broadcast relaxation; all other shape mismatches still require explicit broadcastTo/broadcastPair.

Semantics (NumPy-aligned)

val s = NDArray.scalar(3.0)                  // shape=[], ndim=0, numel=1
val v = NDArray(Array(1.0, 2.0, 3.0), Array(3))

s + v                                        // Array(4.0, 5.0, 6.0) — 0-d broadcasts to any shape
s - v                                        // Array(2.0, 1.0, 0.0)
NDArray.scalar(2.0) + NDArray.scalar(3.0)    // scalar(5.0) — 0-d + 0-d = 0-d

val a = v.reshape(Array())                   // 1-element 1-d → 0-d
a.scalar                                     // 1.0
a.setScalar(99.0)

NDArray.zeros[Double](Array())               // scalar(0.0)
NDArray.ones[Double](Array())                // scalar(1.0)

All existing broadcast machinery (broadcastShape, broadcastTo, broadcastPair) already handles 0-d correctly without modification.

Copilot AI linked an issue Apr 10, 2026 that may be closed by this pull request
…n-place/comparison op 0-d guards, tests

Agent-Logs-Url: https://github.com/Quafadas/vecxt/sessions/34025804-0357-4276-a13e-a022f28af110

Co-authored-by: Quafadas <24899792+Quafadas@users.noreply.github.com>
Copilot AI changed the title [WIP] Add support for 0-dimensional NDArray Add 0-d NDArray support Apr 10, 2026
Copilot AI requested a review from Quafadas April 10, 2026 13:29
@Quafadas Quafadas marked this pull request as ready for review April 10, 2026 13:32
@Quafadas Quafadas merged commit ff77045 into main Apr 10, 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.

Size 0 NDArrays

2 participants