moonvec numerical arrays

moonbitstack / numerical arrays

Numerical MoonBit Library

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.

moon add moonbitstack/moonvec

Fourteen dtypes · wasm, wasm-gc, js and native · Apache-2.0

column + row  /  broadcast a + b
loading the engine float64 · [3, 4] · strides [4, 1]

[0] — What it is

One array, and the arithmetic that goes with it.

Not a wrapper around someone else's memory. One buffer type, one stride model, and every operation written against them.

01

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.

02

Broadcasting, decided before the loop

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.

03

A kernel seam, not a monoculture

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

An array is a window onto a buffer.

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.

buffer · 12 elements · float64 view · offset 2

[2] — Space

Space folds into a line.

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.

d0 d2 d1

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

Two shapes meet without either being copied.

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

Three packages, one vocabulary.

Arrays and elementwise work in the root package; factorisations and solvers beside it; layout algebra for tiling in a third.

moonbitstack/moonvec

The array, fourteen dtypes, broadcasting, universal functions, eleven reductions with axis and keepdims, slicing, joining, sorting, matrix products.

  • from_doubles
  • slice
  • add
  • sqrt
  • sum
  • matmul
  • argsort
moonbitstack/moonvec/linalg

Solving, inversion, determinants, Cholesky, Householder QR, one-sided Jacobi SVD, symmetric eigenvalues, least squares, norms, matrix powers, Kronecker products.

  • solve
  • inv
  • det
  • qr
  • svd
  • eigh
  • lstsq
  • norm
moonbitstack/moonvec/cute

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.

  • compact
  • mode
  • coalesce
  • divide
  • product
  • offset
14dtypes
264public items
3packages
4build targets

[5] — First result

Four lines, and the shape does the rest.

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.

moonbit
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)

Start with one array. The rest follows the shape.