NumPy semantics, Go ergonomics
Broadcasting, type promotion, NaN/Inf propagation, axis handling — even error-message wording — match NumPy, behind a Go-idiomatic API: generics for dtype dispatch, explicit conversion, views as first-class values.
N-dimensional arrays with NumPy semantics for Go
Broadcasting rules, type promotion, NaN/Inf propagation and axis handling match NumPy — while the API stays Go-idiomatic: generics for dtype dispatch, explicit conversion, views as first-class values. LAPACK, FFT, random and the math kernels are implemented in-tree, on the Go standard library alone.
go get github.com/aixfoundry/numgo
Requires Go 1.27 or later.
why numgo
Broadcasting, type promotion, NaN/Inf propagation, axis handling — even error-message wording — match NumPy, behind a Go-idiomatic API: generics for dtype dispatch, explicit conversion, views as first-class values.
go.mod contains only the Go standard library. LAPACK, FFT, random and the
math kernels are implemented in-tree — no cgo, no BLAS.
SeedSequence + MT19937/PCG64 are exact ports: for identical seeds,
libm-free distributions produce bit-identical streams to NumPy; libm-sensitive ones stay
ULP-close via Go's stdlib math.
GOEXPERIMENT=simdTwo build modes: a default pure-Go build, and an opt-in SIMD build that accelerates hot paths (with documented last-ULP notes). Results contracts hold in both.
performance
Solve (24×) and Inv (4.6×),
StandardNormal, StandardGamma, and element-level
At/Set (12×).GOEXPERIMENT=simd build turns Matmul 4.2× faster and makes
SumAll/SumAxis0 faster than NumPy.| Operation | Size | numgo | NumPy | Ratio |
|---|---|---|---|---|
Solve | 100×100 | 0.24 ms | 5.730 ms | 24× faster |
Inv | 100×100 | 1.98 ms | 9.114 ms | 4.6× faster |
At/Set per element | 1e6 | 9.38 ms | 114.0 ms | 12× faster |
StandardNormal | 1e6 | 4.53 ms | 6.563 ms | 0.69× · faster |
StandardGamma | 1e6 | 11.1 ms | 12.093 ms | 0.92× · parity |
einsum ij,jk→ik | 100×100×100 | 0.29 ms | 0.168 ms | 1.7× · near parity |
CumSum (last axis) | 1000×1000 | 2.82 ms | 2.302 ms | 1.2× · near parity |
ma.Add half-masked | 1000×1000 | 2.59 ms | 0.888 ms | 2.9× · near parity |
Single-threaded pure Go vs NumPy 2.5.1 / OpenBLAS · ratio = numgo ÷ NumPy, lower is better · full methodology and all benchmarks
quick start
go get to .npy in one filepackage main
import (
"os"
"github.com/aixfoundry/numgo"
)
func main() {
a := numgo.Arange(0.0, 6.0, 1.0).Reshape(2, 3) // [[0 1 2], [3 4 5]]
b := numgo.Ones[float64](1, 3) // broadcast add (2,3)+(1,3)
c := a.Add(b) // pure operation: new data
m := numgo.Ones[float64](3, 2)
p := c.Matmul(m) // (2,3) @ (3,2) → (2,2)
numgo.SaveFile("a.npy", a) // .npy write/read
if v, err := numgo.LoadFile("a.npy"); err == nil {
arr := v.AsFloat64()
_ = arr.Sum() // 15
}
_ = p
os.Remove("a.npy")
}
NumPy 2.x semantics are verified against golden fixtures generated from
NumPy; some surfaces are intentionally skipped (recarrays, np.matrix, legacy
RandomState) — see the parity notes.
packages
Array[T] views & constructors, ufuncs, reductions, sorting, manipulation,
dot products, einsum, IO (.npy/.npz/txt), interop
numpy.linalg: solve, least squares, SVD/QR/Cholesky/eigen decompositions,
norms — pure-Go LAPACK kernels
numpy.fft: mixed-radix FFT/inverse/rfft families + fftshift, three
norm modes
numpy.random: SeedSequence + MT19937/PCG64 + Generator distributions;
streams match NumPy (libm-free paths bit-identical)
numpy.polynomial: Polynomial, Chebyshev, Legendre, Laguerre, Hermite,
HermiteE
numpy.strings: vectorized string operations on StrArray
(upper/split/replace/…)
numpy.ma: MaskedArray = data + mask with mask-propagating operations
numpy.emath: scimath functions that promote to complex outside the real
domain
numpy.testing: AssertAllClose/AssertArrayEqual and friends, for downstream
tests