Integration on simplices.
Project description
pysimplicialcubature
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).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pysimplicialcubature-0.2.0.tar.gz.
File metadata
- Download URL: pysimplicialcubature-0.2.0.tar.gz
- Upload date:
- Size: 20.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.10.5 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e142d4b72a04b32aaf8d1e368c155af3209dd24bbef9c9119aaba49a746963ba
|
|
| MD5 |
2566f71c13cc28c1c35ba0b19b0bccf8
|
|
| BLAKE2b-256 |
f68e0557fd8fc34a70dc1a7bcbdfee33d0ec57fc7282c653c6d2baf60895d1ae
|
File details
Details for the file pysimplicialcubature-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pysimplicialcubature-0.2.0-py3-none-any.whl
- Upload date:
- Size: 21.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.10.5 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c833574c795b650355d1bcc403b60277fdb3c6d38f65ebd2573c37f8bfc9c95
|
|
| MD5 |
e5fe579d9e7e6ee31d67abc1b30e2daa
|
|
| BLAKE2b-256 |
825aec5ae8d5f25dbee7568f7fcdcd1249494dfdee653b3afa8b6b20fe0e6475
|