NdArray<T>

A compact, typed, n-dimensional numeric array. Shape and layout metadata are immutable; slicing, transposing, and broadcasting retain shared storage through checked views.

Constructors and functions

fn zeros<T>(shape: [int]) -> NdArray<T>#

Allocates a writable dense ndarray filled with numeric zero. T must be a supported numeric dtype.

fn ones<T>(shape: [int]) -> NdArray<T>#

Allocates a writable dense ndarray filled with numeric one.

fn full<T>(shape: [int], value: T) -> NdArray<T>#

Allocates a writable dense ndarray filled with value; T may be inferred from the value.

fn arange(start: int, end: int, step: int = 1) -> NdArray<int>#

Builds a half-open integer progression. The step defaults to one and must be non-zero.

fn linspace(start: float, end: float, count: int) -> NdArray<float>#

Builds count evenly spaced values, including both endpoints when count is greater than one.

fn from_array<T>(values: [T], shape: [int]) -> Result<NdArray<T>, Error>#

Copies a flat row-major array after checking that its length equals the shape product.

fn from_nested<T>(values: nested [T]) -> Result<NdArray<T>, Error>#

Copies a statically nested rectangular array. Ragged or mixed-depth input is rejected.

fn from_bytes<T>(data: string, shape: [int]) -> Result<NdArray<T>, Error>#

Copies compact native-endian dtype bytes after checking their length against the shape exactly.

fn to_bytes<T>(array: NdArray<T>) -> string#

Copies compact dtype bytes in logical row-major order, materializing arbitrary strided views.

fn dot<T>(a: NdArray<T>, b: NdArray<T>) -> Result<T, Error>#

Computes a rank-one dot product after dtype promotion.

fn matmul<T>(a: NdArray<T>, b: NdArray<T>) -> Result<NdArray<T>, Error>#

Performs vector, matrix, or broadcast-batched matrix multiplication.

Methods

fn rank<T>(self: NdArray<T>) -> int#

Returns copied ndarray metadata; mutating the returned array never changes the ndarray.

fn shape<T>(self: NdArray<T>) -> [int]#

Returns copied ndarray metadata; mutating the returned array never changes the ndarray.

fn strides<T>(self: NdArray<T>) -> [int]#

Returns copied ndarray metadata; mutating the returned array never changes the ndarray.

fn len<T>(self: NdArray<T>) -> int#

Returns the checked product of all dimensions; a rank-zero ndarray has length one.

fn is_contiguous<T>(self: NdArray<T>) -> bool#

Reports whether logical row-major order maps to contiguous storage.

fn view_contiguous<T>(self: NdArray<T>) -> Result<NdSlice<T>, Error>#

Returns a zero-copy rank-one NdSlice<T> over this array's elements in row-major order, or reports NotContiguous when the layout is strided. The view shares the array's storage: writes through either are visible to both, and the view borrows the array for as long as it is live.

fn dtype<T>(self: NdArray<T>) -> string#

Returns the stable storage dtype name.

fn to_array<T>(self: NdArray<T>) -> [T]#

Copies all scalars into a flat array in logical row-major order.

fn astype<T, U>(self: NdArray<T>) -> Result<NdArray<U>, Error>#

Explicitly converts every scalar to U, reporting narrowing overflow.

fn copy<T>(self: NdArray<T>) -> NdArray<T>#

Returns an independent writable contiguous copy.

fn contiguous<T>(self: NdArray<T>) -> NdArray<T>#

Returns an independent writable row-major copy.

fn get<T>(self: NdArray<T>, indices: [int]) -> T#

Reads one scalar. Negative indices count from the corresponding dimension end.

fn get_or_none<T>(self: NdArray<T>, indices: [int]) -> Optional<T>#

Reads one scalar, returning none for rank or bounds mismatch.

fn set<T>(self: NdArray<T>, indices: [int], value: T)#

Writes one scalar through a writable view.

fn slice<T>(self: NdArray<T>, starts: [int], ends: [int], steps: [int]) -> Result<NdArray<T>, Error>#

Returns a checked zero-copy view when the layout permits it. Views retain their shared backing buffer.

fn reshape<T>(self: NdArray<T>, shape: [int]) -> Result<NdArray<T>, Error>#

Returns a view for contiguous input; otherwise first makes a contiguous copy.

fn transpose<T>(self: NdArray<T>, axes: [int]) -> Result<NdArray<T>, Error>#

Returns a checked zero-copy view when the layout permits it. Views retain their shared backing buffer.

fn swap_axes<T>(self: NdArray<T>, a: int, b: int) -> Result<NdArray<T>, Error>#

Returns a checked zero-copy view when the layout permits it. Views retain their shared backing buffer.

fn squeeze<T>(self: NdArray<T>) -> NdArray<T>#

Returns a checked zero-copy view when the layout permits it. Views retain their shared backing buffer.

fn expand_dims<T>(self: NdArray<T>, axis: int) -> Result<NdArray<T>, Error>#

Returns a checked zero-copy view when the layout permits it. Views retain their shared backing buffer.

fn broadcast_to<T>(self: NdArray<T>, shape: [int]) -> Result<NdArray<T>, Error>#

Returns a right-aligned broadcast view. Repeated-cell views are read-only.

fn transpose2<T>(self: NdArray<T>) -> Result<NdArray<T>, Error>#

Returns the zero-copy transpose of a rank-two ndarray.

fn iter<T>(self: NdArray<T>) -> Iterator<T>#

Returns a single-pass iterator in logical row-major order.

fn eq_elements<T>(self: NdArray<T>, other: T | NdArray<T>) -> NdArray<bool>#

Broadcast elementwise equality. Ordinary == remains handle identity.

fn ne_elements<T>(self: NdArray<T>, other: T | NdArray<T>) -> NdArray<bool>#

Broadcast elementwise inequality.

fn lt_elements<T>(self: NdArray<T>, other: T | NdArray<T>) -> NdArray<bool>#

Broadcast elementwise less-than comparison.

fn le_elements<T>(self: NdArray<T>, other: T | NdArray<T>) -> NdArray<bool>#

Broadcast elementwise less-than-or-equal comparison.

fn gt_elements<T>(self: NdArray<T>, other: T | NdArray<T>) -> NdArray<bool>#

Broadcast elementwise greater-than comparison.

fn ge_elements<T>(self: NdArray<T>, other: T | NdArray<T>) -> NdArray<bool>#

Broadcast elementwise greater-than-or-equal comparison.

fn add_in_place<T>(self: NdArray<T>, other: T | NdArray<T>)#

Alias-safe broadcast addition into a writable target of unchanged shape and dtype.

fn sub_in_place<T>(self: NdArray<T>, other: T | NdArray<T>)#

Alias-safe broadcast subtraction into a writable target.

fn mul_in_place<T>(self: NdArray<T>, other: T | NdArray<T>)#

Alias-safe broadcast multiplication into a writable target.

fn fill<T>(self: NdArray<T>, value: T)#

Fills every logical position of a writable injective view.

fn array_equal<T>(self: NdArray<T>, other: NdArray<T>) -> bool#

Compares shape and all scalar contents exactly.

fn all_close<T>(self: NdArray<T>, other: NdArray<T>, rel: float = 1e-5, abs: float = 1e-8) -> bool#

Compares numeric contents with explicit relative and absolute tolerances. NaN is never close.

fn sum<T>(self: NdArray<T>) -> T#

Reduces with the additive identity; empty arrays return zero.

fn product<T>(self: NdArray<T>) -> T#

Reduces with the multiplicative identity; empty arrays return one.

fn min<T>(self: NdArray<T>) -> Optional<T>#

Returns none for an empty ndarray.

fn max<T>(self: NdArray<T>) -> Optional<T>#

Returns none for an empty ndarray.

fn mean<T>(self: NdArray<T>) -> Optional<float>#

Returns an f64 mean, or none for an empty ndarray.

fn any(self: NdArray<bool>) -> bool#

Boolean reduction with false as the empty identity.

fn all(self: NdArray<bool>) -> bool#

Boolean reduction with true as the empty identity.

fn sum_axis<T>(self: NdArray<T>, axis: int, keep_dims: bool = false) -> NdArray<T>#

Reduces one normalized axis, optionally retaining it with extent one.

fn mean_axis<T>(self: NdArray<T>, axis: int, keep_dims: bool = false) -> NdArray<float>#

Computes f64 means along one axis.

fn min_axis<T>(self: NdArray<T>, axis: int, keep_dims: bool = false) -> Result<NdArray<T>, Error>#

Computes minima along a non-empty axis.

fn max_axis<T>(self: NdArray<T>, axis: int, keep_dims: bool = false) -> Result<NdArray<T>, Error>#

Computes maxima along a non-empty axis.