Skip to main content

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)))

You can try this without installing anything. numpy explore runs the tracer in your browser:

numpy explore, typing a NumPy program and watching its mathematics appear

Every formula comes as a certificate. The certificate is 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 raises an exception when it can't work something out. 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.

You can also state the formula you believe and let the trace check it, as an ordinary pytest test:

import sympy
from scipy.integrate import simpson
from skverify.testing import specifies

y = sympy.IndexedBase("y")

@specifies((y[0] + 4*y[1] + 2*y[2] + 4*y[3] + y[4]) / 3)
def test_simpson_is_the_textbook_rule():
    return (lambda y: simpson(y)), (np.array([0.7, 1.2, 2.5, 0.3, 0.4]),)

A passing test means the code computes that formula, proved symbolically, not sampled. A failing one prints both formulas with a concrete counterexample. The penalty matrix notebook is this in action on a real derivation.

The decorator checks every reachable branch by default. The Z3 solver finds inputs for the paths your test data never took, or proves no such inputs exist. A green test cannot hide an unchecked branch.

from skverify.testing import check_formula

v = sympy.IndexedBase("v")
i = sympy.Idx("i")

# meant to apply a gain of 2 to every element
def apply_gain(v):
    if v.sum() > 0:
        return v * 2.0
    return v * 3.0          # wrong gain on this branch

check_formula(apply_gain, (np.array([1.0, 2.0]),), 2 * v[i], indices=(i,), explore=True)
# verdict: differs
#   your spec:  2*v[0]
#   the code:   3.0*v[0]
#   on the path where: Sum(v[j], (j, 0, 1)) <= 0

The test input [1.0, 2.0] has a positive sum, so it only ever takes the correct branch. explore=True sends Z3 after the path the data never reached, and there the gain is wrong.

When nobody knows the closed form, state a fact about it instead. The entries sum to one, the matrix is symmetric, a null space holds:

@specifies.property(lambda F: sympy.Eq(sum(F[k] for k in range(3)), 1))
def test_softmax_normalizes():
    return (lambda v: softmax(v)), (np.array([0.7, -1.2, 2.5]),)

The fact is decided symbolically on every reachable branch, and a branch where it fails comes back with the failing input.

Measured over every public numpy function the tracer lifts, 274 of 293 get full branch coverage proven. The branch coverage notebook tells the whole story.

Correctness of numerical programs is two questions:

  1. Is the math itself correct?
  2. Is the code numerically stable?

scikit-verify answers the first question.

Installation

pip install scikit-verify

Requires Python >= 3.11, numpy, sympy, and z3-solver (a plain pip wheel, nothing to install system-wide). The import name is skverify. The companion layers install as extras:

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

The API may change while the project is young. 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).

AI policy

You can use AI for any contribution, including code, tests, and docs. The one condition is that you have read every line you submit and can explain it. You are the author and the AI is the typist. Pull requests that read like nobody looked at them will be treated as such.

License

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

Metadata

Release files for scikit-verify 0.4.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for scikit-verify 0.4.3
File Size Uploaded Provenance Actions
scikit_verify-0.4.3.tar.gz 618.4 kB Without provenance Details for scikit_verify-0.4.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for scikit-verify 0.4.3
File Interpreter ABI Platform Actions
scikit_verify-0.4.3-py3-none-any.whl Python 3 none any Details for scikit_verify-0.4.3-py3-none-any.whl

Total release size: 763.9 kB

Release files / scikit_verify-0.4.3.tar.gz

Download URL scikit_verify-0.4.3.tar.gz
Size 618.4 kB
Tags Source
SHA-256 checksum
How to use checksums
df5142105e2d788f6069ee4770ceba05b71842c801398d5a34d938fb5b5bda8e
BLAKE2b-256 checksum
How to use checksums
c3ebe7dd679d47fe6cd7eac292d637d695985bef49ff6b0740e7bf86611c165c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release files / scikit_verify-0.4.3-py3-none-any.whl

Download URL scikit_verify-0.4.3-py3-none-any.whl
Size 145.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fdbb11de64e1d676f16b7006145fce10d0bb52e9c01789ae64ff8c527152f253
BLAKE2b-256 checksum
How to use checksums
7991c3d2b59915cd7f98c49d883d2309ebf21084efdf0dba6c89c73aebf2b573
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Security for scikit-verify 0.4.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page