Skip to main content

symintegral

This is my personal library for symbolic integration. SymPy is my go-to python package for symbolic computing, but unfortunately slow on multidimensional integrals and often fails to evaluate them. I started looking for lightweight packages to operate on sympy integrals, but found only large general purpose symbolic packages like SAGE. Thus I created
this package for my personal use as fusion of routines created in other projects, such as topoptlab.

A word of caution: this is not meant to be a complete or bullet-proof integration library. It covers the integrals I need in my own work, and it is developed as far as that requires. Expect gaps, and check results that matter to you (e.g. with check_definite). Known failures are collected in benchmarks/LIBRARY_OF_SHAME.md.

symintegral tries to find closed-form evaluation of multidimensional SymPy integrals by taking an unevaluated sympy.Integral and evaluating it as far as possible in closed form. Several simplification strategies are in place:

  • rewrite the integral and splits it into small one-variable problems
  • closed-form rules for specific integrals for speed
  • use SymPy, Maxima or FriCAS to handle complex problems.

Anything that cannot be solved stays a SymPy Integral as a partial result. numpy_function can turn that partial result into NumPy/SciPy code that uses quadrature for the remaining integrals.

The core package is general-purpose, but code tied to particular applications live in context subpackages, which you enable by importing them:

Subpackage Adds
symintegral.qc Quantum chemistry: the "azimuthal" rule, closed forms for the azimuthal part of 1/r12 kernels (Bessel I₀, elliptic K).
import symintegral.qc   # registers the "azimuthal" rule for all later integrate() calls

Installation

Install from PyPI:

pip install symintegral               # SymPy + passagemath (Maxima/FriCAS)
pip install "symintegral[numeric]"    # + NumPy/SciPy, to run generated code

From a clone of the repository:

pip install -e ".[test]"      # development install with pytest

passagemath provides the default Maxima and FriCAS back ends. Sage is imported only when a CAS step actually runs, so pure-rule calls (backends=()) stay fast to start.

Quick start

import sympy as sp
from symintegral import integrate, simplex_integral, numpy_function, compile_function

x, y = sp.symbols("x y", real=True)
a = sp.Symbol("a", positive=True)

integrate(sp.Integral(x**2 * sp.exp(-a * x**2), (x, -sp.oo, sp.oo)),
          backends=("sympy",))
# sqrt(pi)/(2*a**(3/2))

integrate(simplex_integral(x**2 * y, (x, y)), backends=())   # reference triangle
# 1/60

leftover = integrate(sp.Integral(sp.exp(-a * x**4), (x, 0, 1)), backends=())
f = compile_function(numpy_function("f", leftover, [a]))         # needs [numeric]
f(1.0)  # 0.8448385947...

Pass log=[] to integrate to see which rule or back end handled each step. You can add your own closed-form rules with register_rule. The documentation (see below, or help(symintegral)) describes the full pipeline, the domains and the back ends.

Checking results

check_antiderivative(F, f, x) checks dF/dx = f. It sorts the residual into groups by its transcendental factors (exponentials split, radicals reduced) and cancels each group's rational coefficient separately, so exact cancellations are found without a full simplify. Only what survives is simplified, and as a last resort tested numerically. The result says which stage decided ("groups", "simplify", "numeric"). check_definite(result, integral) compares a definite result with numerical quadrature.

Examples and benchmark

examples/ contains runnable scripts:

  • Gaussian moments
  • FEM element matrices with code generation
  • the Coulomb integral of two Gaussians,
  • partial results evaluated by quadrature
  • creation/application of a user-defined rule
  • result checking
  • quantum-chemistry specific rules.

benchmarks/known_integrals.py runs symintegral on integrals with known closed forms and reports for each whether the result is exact, only numerically equal, partial, or wrong.

Running tests

pytest

This runs the tests in tests/ and the doctests in the package.

Documentation

The documentation is built with Sphinx:

pip install -e ".[docs]"
cd docs
sphinx-apidoc -o source/ ../symintegral --force --no-toc --separate
make html
xdg-open build/html/index.html

Building and uploading to PyPI

pip install -e ".[pypi]"
rm -rf dist/
python -m build
python -m twine check dist/*

Upload to TestPyPI first and test the install from there:

python -m twine upload --repository testpypi dist/*
pip install --index-url https://test.pypi.org/simple/ \
    --extra-index-url https://pypi.org/simple/ symintegral

then to PyPI:

python -m twine upload dist/*

License

GPL-3.0-or-later, see LICENSE.

Metadata

Release files for symintegral 0.1.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 symintegral 0.1.0
File Size Uploaded Provenance Actions
symintegral-0.1.0.tar.gz 63.5 kB Without provenance Details for symintegral-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for symintegral 0.1.0
File Interpreter ABI Platform Actions
symintegral-0.1.0-py3-none-any.whl Python 3 none any Details for symintegral-0.1.0-py3-none-any.whl

Total release size: 132.3 kB

Release files / symintegral-0.1.0.tar.gz

Download URL symintegral-0.1.0.tar.gz
Size 63.5 kB
Tags Source
SHA-256 checksum
How to use checksums
78b0005463defffcf923c063ae2d0a0bae7857cf77f06edee6259e52d99a9fa1
BLAKE2b-256 checksum
How to use checksums
4d12f0f594bc4d2af11398728b5afdf5b712169c3a41a8b15f0f0a52992640a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.10

Release files / symintegral-0.1.0-py3-none-any.whl

Download URL symintegral-0.1.0-py3-none-any.whl
Size 68.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7b64d5f2c6effe911742df1e6f562a60def81b639e61141fc7d3f72411938344
BLAKE2b-256 checksum
How to use checksums
20a62593c812126648f83d388ce5d3ada3354266e5fd4f850c59c85c99c6f0d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.10

Security for symintegral 0.1.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.1.0 This release

2 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