LeviPy
A pure-Python, SymPy-backed library for symbolic differential geometry and general relativity computations — the Levi-Civita connection, curvature tensors, geodesics, and more, all computed exactly as closed-form symbolic expressions.
What sets LeviPy apart is its Lean 4 bridge: it doesn't just compute geometric quantities, it can export them as machine-checkable proof obligations in Lean 4 against Mathlib — turning a symbolic curvature calculation into a formal theorem a proof assistant can verify. See Lean 4 formal verification.
Features
- Metric tensor
g_{ij}with automatic inverseg^{ij} - Christoffel symbols
Γ^k_{ij}of the Levi-Civita connection - Riemann curvature tensor
R^l_{ijk} - Ricci tensor
R_{ij}and Ricci scalarR - Einstein tensor
G_{ij} - Geodesic equations for an affinely parametrised curve
- Parallel transport of a vector field along a curve
- Covariant derivatives of vectors, covectors, and (1,1)-tensors
- Lie bracket
[X, Y]of two vector fields - Lean 4 bridge — translate SymPy expressions to Lean 4 terms, emit theorem stubs, and link to relevant Mathlib lemmas for formal verification
All tensors derived from the metric are computed lazily and cached.
Requirements
- Python ≥ 3.12
- SymPy ≥ 1.14
This project uses uv for environment and dependency management.
Installation
# Clone, then sync the environment
uv sync
# Build a wheel / sdist into dist/
uv build
Quick start
import sympy as sp
from levipy import Manifold
# Coordinates for the 2-sphere
theta, phi = sp.symbols('theta phi', real=True)
r = sp.Symbol('r', positive=True)
# Build a manifold and attach a metric g_{ij}
M = Manifold('S2', coords=[theta, phi])
g = M.metric([[r**2, 0],
[0, r**2 * sp.sin(theta)**2]])
# Derived geometric quantities
g.christoffel() # Christoffel symbols Γ^k_ij
g.riemann() # Riemann tensor R^l_ijk
g.ricci_tensor() # Ricci tensor R_ij
g.ricci_scalar() # Ricci scalar R
g.einstein_tensor() # Einstein tensor G_ij
Most objects provide a .display() method for pretty-printed / LaTeX output.
Geodesics
geo = g.geodesic_equations()
geo.display()
Parallel transport
from levipy import ParallelTransport
pt = ParallelTransport(g)
pt.equations(curve=[sp.cos(pt.lam), sp.sin(pt.lam)])
Lie bracket
from levipy import lie_bracket
X = [theta, 0]
Y = [0, phi]
lie_bracket(X, Y, coords=[theta, phi])
Lean 4 formal verification bridge
Symbolic computation tells you what the curvature of a space is. It does not tell you the result is correct — a typo in a metric or a SymPy simplification quirk can silently produce a wrong tensor. LeviPy's Lean 4 bridge closes that gap by translating your computed geometry into formal statements that the Lean 4 proof assistant can check against Mathlib.
This is a proof-obligation layer, not an automated prover. Given a fully
computed metric (with its Christoffel symbols, Riemann/Ricci tensors, etc.),
TheoremBuilder emits a .lean source file containing:
- The necessary Mathlib imports
- Coordinate symbols declared as
(x : ℝ) - The metric as a
noncomputable def - One theorem per nonzero Christoffel symbol
- Riemann flatness / curvature theorems
- The Ricci scalar theorem
- Geodesic equation statements
Each theorem comes with a sorry-based proof skeleton and tactic hints that
point at real Mathlib lemmas — so a human (or Lean's automation) can fill in
the proofs. The output is a set of checkable obligations: if the geometry is
wrong, the Lean statements won't close.
Three components make up the bridge:
| Component | Role |
|---|---|
SymPyToLean4 |
Translates a SymPy expression into a Lean 4 term string |
TheoremBuilder |
Emits a full .lean file of proof obligations from a metric |
MathlibLinker |
Suggests relevant Mathlib 4 lemmas/tactics for an expression |
import sympy as sp
from levipy import Manifold, TheoremBuilder
# Name the symbols in Greek so the generated Lean uses θ, φ identifiers
theta, phi = sp.symbols('θ φ', real=True)
M = Manifold('S2', coords=[theta, phi])
g = M.metric([[1, 0], [0, sp.sin(theta)**2]])
builder = TheoremBuilder(
g,
manifold_name='Sphere2',
extra_hypotheses=['(hth : 0 < θ)', '(hth2 : θ < π)'], # must match the symbol names
)
builder.save('Sphere2.lean') # writes a Lean 4 source file
Note for
pip installusers: the PyPI package ships the Python bridge (which generates.leanfiles), but not the companion Lean project itself. To build and check the generated proofs, clone the GitHub repository — the Lean Lake project lives inlean/(pinned to a Mathlib toolchain).
Lower-level pieces are also available:
from levipy import SymPyToLean4, MathlibLinker
SymPyToLean4().translate(sp.sin(theta)**2) # SymPy expr → Lean 4 term string
MathlibLinker() # map expressions to Mathlib lemmas
Package layout
levipy/
├── geometry/ Manifold, covariant derivative, Lie bracket
├── tensors/ Metric, Christoffel, Riemann, Ricci, Einstein
├── gr/ General-relativity tools (geodesics, parallel transport)
└── lean4/ Lean 4 bridge (translator, theorem builder, Mathlib linker)
lean/ Companion Lean 4 / Lake project (Mathlib-backed proofs)
Development
# Run the test suite (pytest is a dev dependency)
uv run pytest
# Verbose
uv run pytest -v
License
Released under the MIT License. © 2026 Miraj Samarakkody.
Metadata
Release files for levipy 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| levipy-0.1.1.tar.gz | 31.2 kB | Without provenance | Details for levipy-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| levipy-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for levipy-0.1.1-py3-none-any.whl |
Total release size: 62.0 kB
Release files / levipy-0.1.1.tar.gz
| Download URL | levipy-0.1.1.tar.gz |
|---|---|
| Size | 31.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ac0a0a53a292f9fa0cd08d6edb4c07d5a5ee5c911258494cfd6322472727e539
|
|
BLAKE2b-256 checksum How to use checksums |
ce57709f02175e507dbbbc1471479e7095c20426408b8de83c33cc6238e4f9a4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / levipy-0.1.1-py3-none-any.whl
| Download URL | levipy-0.1.1-py3-none-any.whl |
|---|---|
| Size | 30.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2bfce6fd66701241641eebdf51e23a14cd84b8ae526dfcf840b862e69a435fd4
|
|
BLAKE2b-256 checksum How to use checksums |
7b559fe5bbd5fc745783628397d1738f25c334db88c6933b6c5d1855fdfa078a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for levipy 0.1.1
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.