Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions vecxt/src/NDArrayCheck.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,12 @@ object dimNDArrayCheck:
end if
end dimNDArrayCheck

/** shapeCheck validates that the shape is non-empty and all dimensions are > 0. */
/** shapeCheck validates that all dimensions are > 0. A 0-length shape (0-d array) is valid. */
object shapeCheck:
inline def apply(
shape: Array[Int]
)(using inline doCheck: BoundsCheck): Unit =
inline if doCheck then
if shape.length == 0 then throw InvalidNDArray("Shape must be non-empty")
end if
var i = 0
while i < shape.length do
if shape(i) <= 0 then
Expand Down
7 changes: 7 additions & 0 deletions vecxt/src/ndarray.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ object ndarray:
end while
result && data.length == numel

/** True if this is a 0-dimensional (scalar) NDArray. */
lazy val isScalar: Boolean = shape.length == 0

lazy val layout: String =
s"ndim: $ndim, shape: [${shape.mkString(",")}], strides: [${strides.mkString(",")}], offset: $offset, data length: ${data.length}"

Expand Down Expand Up @@ -93,6 +96,10 @@ object ndarray:
)(using inline boundsCheck: BoundsCheck): NDArray[A] =
new NDArray(data, Array(data.length), Array(1), 0)

/** Create a 0-dimensional (scalar) NDArray holding a single value. */
inline def scalar[A](value: A)(using ct: scala.reflect.ClassTag[A]): NDArray[A] =
new NDArray(Array(value), Array.emptyIntArray, Array.emptyIntArray, 0)

inline def zeros[A](
shape: Array[Int]
)(using inline boundsCheck: BoundsCheck, oz: OneAndZero[A], ct: scala.reflect.ClassTag[A]): NDArray[A] =
Expand Down
Loading
Loading