Skip to main content

pysimplicialcubature

Documentation status

This package is a port of a part of the R package SimplicialCubature, written by John P. Nolan, and which contains R translations of some Matlab and Fortran code written by Alan Genz. In addition it provides a function for the exact computation of the integral of a polynomial over a simplex.


A simplex is a triangle in dimension 2, a tetrahedron in dimension 3. This package provides two main functions: integrateOnSimplex, to integrate an arbitrary function on a simplex, and integratePolynomialOnSimplex, to get the exact value of the integral of a multivariate polynomial on a simplex.

Suppose for example you want to evaluate the following integral:

$$\int_0^1\int_0^x\int_0^y \exp(x + y + z) \text{d}z \text{d}y \text{d}x.$$

from pysimplicialcubature.simplicialcubature import integrateOnSimplex
from math import exp

# simplex vertices
v1 = [0.0, 0.0, 0.0] 
v2 = [1.0, 1.0, 1.0] 
v3 = [0.0, 1.0, 1.0] 
v4 = [0.0, 0.0, 1.0]
# simplex
S = [v1, v2, v3, v4]
# function to integrate
f = lambda x : exp(x[0] + x[1] + x[2])
# integral of f on S
I_f = integrateOnSimplex(f, S)
I_f["integral"]
# 0.8455356728324119

The exact value of this integral is ${(e-1)}^3/6 \approx 0.8455356852954753$.

Now let's turn to a polynomial example. You have to define the polynomial with sympy.Poly.

from pysimplicialcubature.simplicialcubature import integratePolynomialOnSimplex
from sympy import Poly
from sympy.abc import x, y, z

# simplex vertices
v1 = [1.0, 1.0, 1.0] 
v2 = [2.0, 2.0, 3.0] 
v3 = [3.0, 4.0, 5.0] 
v4 = [3.0, 2.0, 1.0]
# simplex
S = [v1, v2, v3, v4]
# polynomial to integrate
P = Poly(x**4 + y + 2*x*y**2 - 3*z, x, y, z, domain = "RR")
# integral of P on S
integratePolynomialOnSimplex(P, S)
# -0.253571428571429

We can do better: by defining the vertex coordinates as fractions, and by taking the field of rational numbers as the domain of the polynomial, we will get the exact value of the integral, without numerical error.

from pysimplicialcubature.simplicialcubature import integratePolynomialOnSimplex
from sympy import Poly
from sympy.abc import x, y, z
from fractions import Fraction

# simplex vertices
v1 = [Fraction(0), Fraction(0), Fraction(0)] 
v2 = [Fraction(1), Fraction(1), Fraction(1)] 
v3 = [Fraction(0), Fraction(1), Fraction(1)] 
v4 = [Fraction(0), Fraction(0), Fraction(1)]
# simplex
S = [v1, v2, v3, v4]
# polynomial to integrate
P = Poly(x**4 + y + 2*x*y**2 - 3*z, x, y, z, domain = "RR")
# integral of P on S
integratePolynomialOnSimplex(P, S)
# -71/280 (= -0.25357142857142856...)

References

  • A. Genz and R. Cools. An adaptive numerical cubature algorithm for simplices. ACM Trans. Math. Software 29, 297-308 (2003).

  • Jean B. Lasserre. Simple formula for the integration of polynomials on a simplex. BIT Numerical Mathematics 61, 523-533 (2021).

Metadata

Release files for pysimplicialcubature 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 pysimplicialcubature 0.2.0
File Size Uploaded
pysimplicialcubature-0.2.0.tar.gz 20.8 kB Details

Built distribution (wheel)

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

Total release size: 41.9 kB

Release files / pysimplicialcubature-0.2.0.tar.gz

Download URL pysimplicialcubature-0.2.0.tar.gz
Size 20.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e142d4b72a04b32aaf8d1e368c155af3209dd24bbef9c9119aaba49a746963ba
BLAKE2b-256 checksum
How to use checksums
f68e0557fd8fc34a70dc1a7bcbdfee33d0ec57fc7282c653c6d2baf60895d1ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.0 CPython/3.10.5 Windows/10

Release files / pysimplicialcubature-0.2.0-py3-none-any.whl

Download URL pysimplicialcubature-0.2.0-py3-none-any.whl
Size 21.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9c833574c795b650355d1bcc403b60277fdb3c6d38f65ebd2573c37f8bfc9c95
BLAKE2b-256 checksum
How to use checksums
825aec5ae8d5f25dbee7568f7fcdcd1249494dfdee653b3afa8b6b20fe0e6475
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.0 CPython/3.10.5 Windows/10

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

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