numpy for Go

github.com/malcolmston/numpy

NumPy-style n-dimensional arrays in Go.

 GitHubports numpy/numpy

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

shell
$ go get github.com/malcolmston/numpy

Quick start

main.go
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

  • NDArray core — a dense row-major array of float64 with cached Shape, Strides, Ndim and Size
  • Creation — FromSlice, FromData, FromNested, Zeros, Ones, Full, Arange, Linspace, Eye, Identity
  • Zero-copy views — Transpose/T permute strides, Slice adjusts offset+shape, Reshape re-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, Pow plus *Scalar variants, and Neg/Abs/Sqrt/Exp/Log/Sin/Cos
  • Reductions — whole-array Sum, Mean, Max, Min, Std, Var, Prod and per-axis SumAxis/MeanAxis/MaxAxis/… with optional keepdims
  • Linear algebra — Dot (1-D dot / 2-D matmul) and MatMul for matrix products
  • Comparison & masking — Greater, Less, EqualMask (and scalar variants), MaskSelect, Where, Any, All
  • Indexing & combining — At/Set by multi-index (negatives allowed), plus Concatenate and Stack
  • 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
numpy