NumPy semantics. Go ergonomics. Zero dependencies.

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

A NumPy you can ship inside a Go binary

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.

Zero dependencies

go.mod contains only the Go standard library. LAPACK, FFT, random and the math kernels are implemented in-tree — no cgo, no BLAS.

Reproducible random streams

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.

Opt-in GOEXPERIMENT=simd

Two 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

Where numgo wins — measured, not claimed

  • Faster than NumPy on small-matrix Solve (24×) and Inv (4.6×), StandardNormal, StandardGamma, and element-level At/Set (12×).
  • Near parity on einsum, cumsum, and masked-array arithmetic.
  • Single-threaded elementwise and reductions currently trail NumPy's vectorized kernels by 3–21×.
  • The opt-in GOEXPERIMENT=simd build turns Matmul 4.2× faster and makes SumAll/SumAxis0 faster than NumPy.
Selected numgo vs NumPy benchmark results, single-threaded pure Go vs NumPy 2.5.1 with OpenBLAS
OperationSizenumgoNumPyRatio
Solve100×1000.24 ms5.730 ms24× faster
Inv100×1001.98 ms9.114 ms4.6× faster
At/Set per element1e69.38 ms114.0 ms12× faster
StandardNormal1e64.53 ms6.563 ms0.69× · faster
StandardGamma1e611.1 ms12.093 ms0.92× · parity
einsum ij,jk→ik100×100×1000.29 ms0.168 ms1.7× · near parity
CumSum (last axis)1000×10002.82 ms2.302 ms1.2× · near parity
ma.Add half-masked1000×10002.59 ms0.888 ms2.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

From go get to .npy in one file

package 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

Nine packages, NumPy module by module

numgo

Array[T] views & constructors, ufuncs, reductions, sorting, manipulation, dot products, einsum, IO (.npy/.npz/txt), interop

numgo/linalg

numpy.linalg: solve, least squares, SVD/QR/Cholesky/eigen decompositions, norms — pure-Go LAPACK kernels

numgo/fft

numpy.fft: mixed-radix FFT/inverse/rfft families + fftshift, three norm modes

numgo/random

numpy.random: SeedSequence + MT19937/PCG64 + Generator distributions; streams match NumPy (libm-free paths bit-identical)

numgo/polynomial

numpy.polynomial: Polynomial, Chebyshev, Legendre, Laguerre, Hermite, HermiteE

numgo/strings

numpy.strings: vectorized string operations on StrArray (upper/split/replace/…)

numgo/ma

numpy.ma: MaskedArray = data + mask with mask-propagating operations

numgo/emath

numpy.emath: scimath functions that promote to complex outside the real domain

numgo/assert

numpy.testing: AssertAllClose/AssertArrayEqual and friends, for downstream tests