Fork me on GitHub

Interpolation and polynomials, in pure Go.
Measured against SciPy and NumPy.

go-approx is the approximation layer of the Go scientific stack: interpolation and splines with the semantics of scipy.interpolate, and polynomials with those of numpy.polyfit, numpy.roots and numpy.polynomial. Both libraries run SciPy and NumPy as oracles from their tests and record where they differ. No cgo.

3434/3434 polynomial values bit-identical to NumPy100% statement coverage in polyCGO=0 pure Go
main.go
s, _ := interp.NewCubicSpline(x, y, "not-a-knot") // scipy CubicSpline
v := s.At(2.5)
d := s.Derivative(1).At(2.5)
area := s.Integrate(0, 4)

fit, _ := poly.Polyfit(x, y, 3, nil) // np.polyfit
r, _ := poly.Roots(fit.Coef)         // np.roots
4 repositories — two libraries: go get github.com/go-approx/{interp,poly}
Why go-approx

Approximation, with the reference named.

Each Go function names the SciPy, NumPy or MATLAB call it corresponds to, and the deliberate differences are listed in each library's README.

📈

Interpolators

interp1d kinds, cubic splines with not-a-knot, natural, clamped and periodic conditions, Hermite, PCHIP, Akima and makima.

〰️

B-splines

make_interp_spline of any degree, make_lsq_spline, FITPACK smoothing splines, design matrices, derivatives, antiderivatives and integrals.

▦

Grids

Regular-grid interpolation (linear to quintic), tensor-product splines and bicubic interpolation.

x²

Polynomials

Evaluation by Horner or compensated Horner, arithmetic, derivatives, integrals, companion-matrix roots.

📉

Fits

Polyfit with weights, rcond and covariance, and fits in the Chebyshev, Legendre, Hermite, HermiteE and Laguerre bases.

📦

Pure Go, CGO=0

No cgo. poly's CI runs Linux, macOS, Windows, arm64, and qemu riscv64, loong64, ppc64le, s390x and arm.

🧭

Planned

Chebyshev approximation and AAA rational approximation are not written yet.

⚖️

BSD-3-Clause

Every repository of the organization. Ported code keeps its attribution in place.

What is checked

6 kinds of check, each against a recorded answer.

The fixtures are written by tests that call the reference — SciPy 1.18.1 and NumPy 2.5.4 — committed, and compared in the ordinary tests. What differs, and why, is in each library's documentation.

SciPy and NumPy as oracles

SciPy 1.18.1 and NumPy 2.5.4 are run from Go by a tag-gated harness that writes the fixtures; the ordinary tests read them and need no Python. Where SciPy raises, the Go constructor returns an error and the fixture says so. (interp)

Edge cases on purpose

1, 2 and 3 points, duplicate and unsorted x, NaN and Inf, nodes clustered over 14 orders of magnitude, offsets of 1e8, spans of 1e-9, values of 1e150 and 1e-290, and n = 1e5 and 1e6. (interp)

Polynomials, bit for bit

Polyval, Polyder, Polyint, Polyadd, Polysub, Deconv and PolyFromRoots: 3434 of 3434 values bit-identical to NumPy; each of the five bases: 10016 of 10016. (poly)

Compensated Horner

PolyvalComp: worst error over the theoretical bound is 0.00095, and it is more accurate than Horner at 677 of 780 points. (poly)

Fits judged against the exact residual

When our sum of squared residuals and NumPy’s differ by more than 1000 eps y², both are judged against the exact residual (normal equations at 256 bits). (poly)

Gates

poly holds 100.0% statement coverage and its CI runs Linux, macOS, Windows, arm64, qemu riscv64, loong64, ppc64le, s390x and arm, 386, and compiles every GOOS/GOARCH. (poly)

Repositories

What is here.

interp

library · released v0.2.0

Interpolation, splines and smoothing: interp1d, cubic splines with every boundary condition, PCHIP, Akima and makima, B-splines of any degree, FITPACK smoothing splines and the legacy splrep, least-squares splines, regular-grid and tensor-product interpolation.

poly

library · released v0.1.0

Polynomials: evaluation (Horner and compensated), arithmetic, least-squares fitting, roots, and the Chebyshev, Legendre, Hermite, HermiteE and Laguerre bases, with the semantics of numpy.polyfit, numpy.roots and numpy.polynomial.

docs

site

The documentation (Hugo + Hextra): one section per library, made of its own README, with its verification and performance. Source →

brand

assets

Logos and icons for the organization, in SVG / PNG / JPG / ICO / ICNS across colour, white and black variants.

Approximation for the Go scientific stack.

go-approx belongs to the family of go-numerics (optimisation, quadrature, special functions) and go-statistics. What is not there yet — scattered-data interpolation, periodic and parametric smoothing splines, Chebyshev and AAA approximation — is listed in the documentation.

Read the source on GitHub →