Skip to content

Repository files navigation

scikit-verify

Translate Python and NumPy programs to symbolic mathematics

weighted_rms in numpy maps to the square root of the ratio of weighted sums

CI

scikit-verify is a tracer for numerical Python. It runs your NumPy function once and returns the formula it computed, as an ordinary SymPy expression you can read, simplify, compare against a paper, or evaluate at any other input. Your code is not modified or annotated. For example:

import numpy as np
from skverify import to_sympy

def weighted_rms(x, w):
    return np.sqrt(np.sum(w * x**2) / np.sum(w))

out = to_sympy(weighted_rms, np.array([1.0, 2.0, 3.0]), np.array([0.5, 0.3, 0.2]))

out.formula
# sqrt(Sum(w[j]*x[j]**2, (j, 0, 2))/Sum(w[j], (j, 0, 2)))

Every formula comes as a certificate: the expression, plus the assumptions it was derived under. When code branches on your data, the branch taken becomes a stated hypothesis instead of a hidden one:

out = to_sympy(np.median, np.array([3.0, 1.0, 4.0, 1.5]))
print(out.pretty())

# formula    = a[0]/2 + a[3]/2
# assumes[0] = a[0] <= a[2]
# assumes[1] = a[1] <= a[3]
# assumes[2] = a[3] <= a[0]

The contract is exact-or-refuse. If an operation has no faithful symbolic form, scikit-verify raises instead of guessing:

to_sympy(lambda a: a.astype(int).mean(), np.array([1.4, 2.6]))
# NotImplementedError: astype to non-float would change the math

Tested against numpy, scipy, scikit-learn, statsmodels, cvxpy and random research code from GitHub; the boards in coverage regenerate every number.

A certificate is also the statement a proof assistant consumes: the formula with its hypotheses. A Lean 4 / Mathlib bridge for the rational-arithmetic fragment is on the roadmap, upgrading certificates from solver-checked to kernel-checked.

Installation

pip install scikit-verify

Requires Python >= 3.11, numpy, and sympy. The import name is skverify. The companion layers install as extras:

pip install "scikit-verify[mcp]"          # MCP server for coding agents
pip install "scikit-verify[hypothesis]"   # testing helpers

Pre-alpha; the API may change. Iterative solvers at real sizes can be slow to trace (minutes, not wrong); the boards in coverage/ carry timings.

Lineage

The ideas here are old and good. Pairing a concrete execution with a symbolic one is King's symbolic execution (CACM 1976), run in the concolic style of Cadar and Sen. Checking a compiled routine's answer against its defining equation, instead of trusting its name, is Blum and Kannan's result checking (1989). Folding a long trace back into its loop structure follows Larus's whole-program paths (PLDI 1999), with templates recovered by Plotkin's anti-unification (1970). The stance that code verification means checking code against the mathematics it claims to implement is Oberkampf and Roy's (2010). Verified lifting of stencils to summaries was developed by Kamil et al. (PLDI 2016) for performance; scikit-verify lifts for correctness. Converting NumPy to SymPy was wished for in sympy#2810 (2014).

License

BSD-3-Clause. scikit-verify is an independent project and is not affiliated with the SciPy developers.

If this is useful to you, a star helps others find it ⭐

About

Translate Python + NumPy programs to SymPy Symbolic Math Expressions

Resources

Contributing

Stars

20 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages