Skip to main content

Pysic-rs

Mathematical physics engine in Rust with Python bindings — graded against closed forms.

Pysic-rs — Hofstadter butterfly logo

Docs PyPI Crate License: MIT Python Rust

📖 Documentation · 📦 PyPI · 🦀 crates.io · 🐙 GitHub · 📝 Changelog


Why Pysic-rs?

  • Graded, not asserted. Every routine is compared against a quantity known independently of it — a closed form, a tabulated value, or a conserved invariant. Measured accuracy is published per function (table below), not rounded off.
  • CPU-only, by design. No GPU backend and none planned. Nothing in the core depends on Python, on BLAS or LAPACK, or on a GPU runtime.
  • Lean. Pure-Rust core with no mandatory dependencies beyond ndarray / num-* / rustfft; the PyO3 layer is optional and feature-gated. Installs from source on a bare machine with only a Rust toolchain.
  • Fractal, on purpose. The logo is Hofstadter's butterfly — the spectrum of electrons in a magnetic field. pysic-rs computes that very spectrum.

A note on validation, and on 0.1.0

Earlier revisions of this README claimed that "every solver is checked against closed-form results — graduating from a should be right to an is right guarantee." That claim was false. Building a reproduction notebook for the companion paper and running the documented API against SciPy for the first time exposed eight incorrect routines in 0.1.0: J0(7.98) returned +50.6 for a function bounded by 1, Ai(0) returned NaN, E_1(1) returned 54.23 where the value is 0.2194, a vacuum spacetime reported non-zero Ricci curvature, an eigensolver returned the matrix diagonal instead of the spectrum, and a Dirac propagator's norm grew without bound. The special-function suite was not reachable from Python at all.

In every case a test existed or could trivially have existed. The tests that were written could not have failed: bessel_j0 was checked only at x = 0 and x = 1, never near its broken branch switch, and zeta(2) was asserted with a tolerance of 1e-3 — loose enough to pass a routine wrong in the fourth digit. There was no Airy test and no exponential-integral test.

All eight are repaired in 0.2.0, each held by a test that fails on the previous code. 0.1.0 should not be used. The discriminating property of a useful numerical test is not coverage but falsifiability against an independently known quantity — see CHANGELOG.md for the full post-mortem.


Measured accuracy

Maximum deviation against SciPy on dense grids, measured by the companion reproduction notebook (which ships with the paper artifact and regenerates this table):

Function Domain Measured error Method
gamma (0, 170] 2.7e-13 rel. Lanczos
ln_gamma (0, 200] 1.8e-15 rel. Lanczos (log form)
beta a, b ≥ 0.3 7.3e-15 rel. via ΓΓ
erf, erfc (0, 6] 1.4e-7 abs. A&S 7.1.26 †
bessel_j0, bessel_j1 (−30, 30) 9.9e-12 abs. series + Hankel asym.
bessel_y0, bessel_y1 (0, 30] 3.4e-11 abs. series + Hankel asym.
legendre_p, legendre_plm (−1, 1) 7.2e-15 abs. recurrence
chebyshev_t, chebyshev_u (−1, 1) 2.9e-15 abs. recurrence
airy_ai (−20, 10] 7.7e-10 abs. series + asymptotic †
expint_en, n ≤ 6 (0, 10] 1.1e-14 rel. series + Lentz CF
zeta (1, 30] 1.1e-15 rel. Euler–Maclaurin

† Limited by the formula, not the port: A&S 7.1.26 is 1.5e-7 by construction, and airy_ai is machine-precision where its series is used, the quoted figure coming from the asymptotic branch beyond. gamma overflows past x ≈ 171.6, as the type requires. Relative error for ln_gamma is reported where |lnΓ| > 1, since lnΓ vanishes at x = 1 and x = 2.

Three classes of invariant do most of the grading, because each is falsifiable without knowing the answer: unitarity (a norm that must not change), vanishing on a known solution (Schwarzschild is a vacuum, so R ≡ 0), and integrality (a winding number of 2.9997 is not imprecise, it is broken).


The mathematics

Schrödinger equation (pde — split-step Fourier, unitary by construction)

$$i\hbar\frac{\partial}{\partial t}\psi(\mathbf{r},t)=\left[-\frac{\hbar^{2}}{2m}\nabla^{2}+V(\mathbf{r})\right]\psi(\mathbf{r},t)$$

Dirac equation (pde — split-step; the mass factor is an exact rotation)

$$\left(i\gamma^{\mu}\partial_{\mu}-m\right)\psi=0, \qquad e^{-i\theta\sigma_x/2}=\cos(\theta/2),\mathbf{1}-i\sin(\theta/2),\sigma_x$$

Harper equation → Hofstadter butterfly (topology — Chern numbers, Berry phase)

$$\psi_{n+1}+\psi_{n-1}+2\cos(2\pi n\alpha),\psi_{n}=E\psi_{n}$$

Schwarzschild metric (general_relativity — Christoffel, Riemann, geodesics, ADM)

$$ds^{2}=-\left(1-\frac{2GM}{c^{2}r}\right)c^{2}dt^{2}+\left(1-\frac{2GM}{c^{2}r}\right)^{-1}dr^{2}+r^{2}d\Omega^{2}$$

Gauss linking number (topology::chern_simons — an integer invariant)

$$\mathrm{lk}(\gamma_1,\gamma_2)=\frac{1}{4\pi}\oint_{\gamma_1}!\oint_{\gamma_2} \frac{(\mathbf r_1-\mathbf r_2)\cdot(d\mathbf r_1\times d\mathbf r_2)}{|\mathbf r_1-\mathbf r_2|^{3}}\in\mathbb Z$$

Casimir force (casimir — parallel plates, Lifshitz theory)

$$F=-\frac{\pi^{2}\hbar c}{240,a^{4}}A$$

Note on rendering: GitHub renders a subset of LaTeX. For fully typeset math open the documentation site.


Modules

Module Description
special_functions Gamma, Bessel (J/Y), Legendre, erf, zeta, Airy, Chebyshev, exponential integrals — all 18 reachable from Python since 0.2.0
linalg Cholesky, LU, inverse, determinant, tensor ops, Lie brackets
calculus gradient, Hessian, Simpson/Romberg integration, interpolation
ode RK4, RK45 (Dormand–Prince, FSAL with embedded error control), backward Euler, symplectic
pde Schrödinger (split-step), Dirac, heat, wave, Poisson, Maxwell FDTD
fourier FFT/IFFT, spectral derivatives, Welch PSD
quantum Pauli algebra, density matrices, path integrals, Feynman propagators
general_relativity Schwarzschild/Kerr/FLRW metrics, Christoffel/Riemann, geodesics, ADM constraints
gauge SU(2)/SU(3) structure constants, Yang–Mills, connections, curvature
classical Hamiltonian/Lagrangian, rigid body (Euler equations), rotations
topology Berry phase, Chern numbers, winding numbers, skyrmion charge, Chern–Simons: Gauss linking numbers, sl(2,R) holonomy, BTZ mass/spin
em Green's functions (static, retarded, Helmholtz), radiation formulas
casimir Parallel plates, sphere, Lifshitz theory, Polder potential

Quick Start

import numpy as np
import pysicrs as ps

# Special functions are elementwise and shape-preserving: a float returns a
# float, any sequence returns a list. Two-argument functions broadcast, and a
# length mismatch raises rather than truncating.
x = np.linspace(0.1, 25, 800)
ps.bessel_j0(x)        # -> list of 800 floats
ps.bessel_j0(2.5)      # -> -0.04838377646...
ps.beta(x, 2.0)        # scalar broadcasts against the sequence

# Closed forms hold exactly where the physics says they must
ps.zeta(2.0)           # pi^2 / 6
ps.zeta(-1.0)          # -1/12, via the functional equation
ps.airy_ai(0.0)        # 3^(-2/3) / Gamma(2/3)

# A vacuum solution has identically zero Ricci scalar
ps.ricci_scalar([0.0, 6.0, np.pi / 2, 0.0], 1e-4)    # ~5e-9 (FD residual)

# Topological invariants are integers
theta = np.linspace(0, 2 * np.pi, 2001)[:-1]
z = np.exp(3j * theta)
ps.winding_number(z.real.tolist(), z.imag.tolist())  # exactly 3.0

# The BTZ black hole as a flat SL(2,R) x SL(2,R) Chern-Simons connection
ps.btz_mass_spin(r_plus=2.1, r_minus=0.8, ell=1.3)   # (M, J)

Installation

From PyPI (Python 3.8+, prebuilt abi3 wheels):

pip install pysic-rs

From crates.io (Rust):

cargo add pysic-rs

From source — needs only a Rust toolchain (rustup); no BLAS, LAPACK or GPU runtime:

git clone https://github.com/ThotDjehuty/pysic-rs.git
cd pysic-rs
pip install maturin
maturin develop --release

Development

cargo test                      # Rust test suite (62 tests)
python -m pytest tests/         # Python binding tests
cd docs && make html            # Sphinx / Furo docs
maturin build --release         # build the abi3 wheel

Reproducing the published results

The companion paper's reproduction artifact builds an environment, registers a Jupyter kernel and executes a notebook that regenerates the accuracy table above, re-checks all eight repaired defects against closed forms, and reconciles every number against the paper.


License

MIT — see LICENSE.

Author

HFThot Research Lab — contact@hfthot-lab.eu


CPU-only mathematical physics, graded against closed forms — powered by Rust, bound to Python.

Metadata

Release files for pysic-rs 0.2.0

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

Source distribution (sdist)

Source distribution for pysic-rs 0.2.0
File Size Uploaded Provenance Actions
pysic_rs-0.2.0.tar.gz 85.2 kB Without provenance Details for pysic_rs-0.2.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for pysic-rs 0.2.0
File Interpreter ABI Platform Actions
pysic_rs-0.2.0-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.8 abi3 Linux glibc 2.17+ x86-64 Details for pysic_rs-0.2.0-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
pysic_rs-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl CPython 3.8 abi3 macOS 10.12+ x86-64 Details for pysic_rs-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl

Total release size: 1.4 MB

Release files / pysic_rs-0.2.0.tar.gz

Download URL pysic_rs-0.2.0.tar.gz
Size 85.2 kB
Tags Source
SHA-256 checksum
How to use checksums
4114be1a8859cce4b77f50e975ae35f631308ac8925b475b3c4ca30ba80aa7d0
BLAKE2b-256 checksum
How to use checksums
2744995156aae8cb35c593a02714c66786d05e28f0fd2547d1c7585fb3d95709
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.15

Release files / pysic_rs-0.2.0-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL pysic_rs-0.2.0-cp38-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 702.1 kB
Tags CPython 3.8 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
9009a87a771bc54e493880e77f91cc4cbad686c0b6b160a743a7ef68046a0609
BLAKE2b-256 checksum
How to use checksums
2b3d89c870b061cb70d9f6877c7784e2554eae1b5a115805544b305df5f3544d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.15

Release files / pysic_rs-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl

Download URL pysic_rs-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl
Size 626.3 kB
Tags CPython 3.8 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
52b6fb5da1731801d0ae6a100b5a02709414cc1492d4e9a95762c261f4ee4dee
BLAKE2b-256 checksum
How to use checksums
4cefd054c2fd55f67f9ac0ccd3a709e89cfe9b389228a0d0b06baccfcf8478c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.15

Security for pysic-rs 0.2.0

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.

Release history Release notifications | RSS feed

This release

0.2.0 This release

3 release files No provenance

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