numpy for Go
NumPy-style n-dimensional arrays in Go.
A from-scratch, standard-library-only Go library modeled on the core of Python's NumPy. Everything is built on a single dense NDArray of float64, backed by one flat slice with row-major shape and strides — no cgo, no third-party dependencies. You get creation helpers, zero-copy views, NumPy-rule broadcasting element-wise math, whole-array and per-axis reductions, basic linear algebra and boolean masking. Transpose, Slice and Reshape return views that share the parent's buffer, while arithmetic and reductions always return fresh contiguous arrays. All shape and dimension validation failures panic with a message prefixed by "numpy:", keeping the arithmetic API free of error returns so operations can be chained.
Install
$ go get github.com/malcolmston/numpyQuick start
import np "github.com/malcolmston/numpy"
a := np.Arange(0, 6, 1).Reshape(2, 3) // [[0 1 2] [3 4 5]]
b := np.FromSlice([]float64{10, 20, 30}) // broadcast (2,3) + (3,)
fmt.Println(a.Add(b).Data()) // [10 21 32 13 24 35]
fmt.Println(a.SumAxis(0, false).Data()) // [3 5 7]
fmt.Println(a.MatMul(a.T()).Data()) // [5 14 14 50]
mask := a.GreaterScalar(2)
fmt.Println(a.MaskSelect(mask).Data()) // [3 4 5]Features
NDArraycore — a dense row-major array offloat64with cachedShape,Strides,NdimandSize- Creation —
FromSlice,FromData,FromNested,Zeros,Ones,Full,Arange,Linspace,Eye,Identity - Zero-copy views —
Transpose/Tpermute strides,Sliceadjusts offset+shape,Reshapere-views the same buffer - Broadcasting — NumPy's trailing-axis rules via
BroadcastTo, with size-1 dimensions expanded through a zero stride (no copy) - Element-wise math —
Add,Sub,Mul,Div,Powplus*Scalarvariants, andNeg/Abs/Sqrt/Exp/Log/Sin/Cos - Reductions — whole-array
Sum,Mean,Max,Min,Std,Var,Prodand per-axisSumAxis/MeanAxis/MaxAxis/… with optionalkeepdims - Linear algebra —
Dot(1-D dot / 2-D matmul) andMatMulfor matrix products - Comparison & masking —
Greater,Less,EqualMask(and scalar variants),MaskSelect,Where,Any,All - Indexing & combining —
At/Setby multi-index (negatives allowed), plusConcatenateandStack - Panic-based errors — every shape or dimension failure panics with a
numpy:prefix, so the arithmetic API stays return-value clean and chainable - Zero dependencies — pure Go standard library, nothing to audit but the toolchain