One buffer, many views
A slice moves an offset. A transpose swaps two strides. A reshape changes the shape and nothing else. Nothing is copied until you ask for a copy.
moonbitstack / numerical arrays
An array is a dtype, a shape, a stride per axis and one flat buffer. Slice it, transpose it, broadcast two shapes together, reduce along an axis, or hand it to a solver — in pure MoonBit, on every target.
Fourteen dtypes · wasm, wasm-gc, js and native · Apache-2.0
[0] — What it is
Not a wrapper around someone else's memory. One buffer type, one stride model, and every operation written against them.
A slice moves an offset. A transpose swaps two strides. A reshape changes the shape and nothing else. Nothing is copied until you ask for a copy.
Shapes align from the right. The dtype of the answer comes from a promotion table, so a result never changes type halfway through an array.
Elementwise work goes through four methods over flat runs with explicit steps. The CPU implementation has a scalar path and a vector path; another backend is four methods away.
[1] — Layout
Numbers sit in one flat run of bytes, in the dtype's own encoding. The array adds three things: a shape, a stride per axis, and an offset into the buffer.
That is why a slice of a matrix, a transposed view and a broadcast operand can all be the same bytes seen differently. It is also why writing through a view changes the original: there is only one original.
A copy happens for take, mask, astype,
flatten, copy, and whenever an operation has to produce new
values.
[2] — Space
Three axes do not change the buffer. An element at [k, r, c] of a
[2, 3, 4] array lives at 12·k + 4·r + c, because the last axis
moves fastest: one layer of twelve, three rows of four.
Point at a node and the cell it occupies lights up below. The lattice is the shape; the strip is the memory. A view is the arithmetic between them.
Strides count elements, not bytes. k steps by twelve,
r by four, c by one.
shape [2, 3, 4] · 24 elements · one buffer c · r · space k
layer k = 1 · row by rowbuffer[12 … 23]
[1, 2, 3] 12·k + 4·r + c = 23
a · [3, 1]
b · [1, 4]
a + b · [3, 4]
each shape aligns from the right; a size-one axis reads with stride zero
[3] — Broadcasting
A size-one axis takes stride zero. Reading it again for every row is not a bug; it is the definition. The loop plan merges the axes that every operand walks at a constant step, so a pair of contiguous arrays runs as one tight loop.
A shape that cannot broadcast is refused with both shapes in the message, before any element is read.
[4] — Surface
Arrays and elementwise work in the root package; factorisations and solvers beside it; layout algebra for tiling in a third.
The array, fourteen dtypes, broadcasting, universal functions, eleven reductions with
axis and keepdims, slicing, joining, sorting, matrix products.
Solving, inversion, determinants, Cholesky, Householder QR, one-sided Jacobi SVD, symmetric eigenvalues, least squares, norms, matrix powers, Kronecker products.
Layouts as nested modes: a length and a stride, composed into tuples. Coalesce adjacent modes, divide one into a tile and its index, and hand the offset to an array view.
[5] — First result
A column and a row are different shapes. Adding them gives every element of the column to every element of the row, without materialising either operand first.
The printed form names the values, the shape and the dtype, so a result can be read back without guessing what it is.
let column = @moonvec.from_doubles([1.0, 2.0, 3.0], [3, 1])
let row = @moonvec.from_doubles([10.0, 20.0, 30.0, 40.0], [1, 4])
let table = @moonvec.add(column, row)
println(table.to_string())
outputarray([[11.0, 21.0, 31.0, 41.0],
[12.0, 22.0, 32.0, 42.0],
[13.0, 23.0, 33.0, 43.0]], dtype=float64)