Multiple integration over a convex polytope.
This package allows to evaluate a multiple integral whose integration bounds are some linear combinations of the variables, e.g.
In other words, the domain of integration is given by a set of linear inequalities:
These linear inequalities define a convex polytope (in dimension 3, a polyhedron). In order to use the package, one has to get the matrix-vector representation of these inequalities, of the form
The matrix
The matrix
import numpy as np
A = np.array([
[-1, 0, 0], # -x
[ 1, 0, 0], # x
[ 0,-1, 0], # -y
[ 1, 1, 0], # x+y
[ 0, 0,-1], # -z
[ 1, 1, 1] # x+y+z
])
b = np.array([5, 4, 5, 3, 10, 6])
The function getAb
provided by the package allows to get
from pypolyhedralcubature.polyhedralcubature import getAb
from sympy.abc import x, y, z
# linear inequalities defining the integral bounds
i1 = (x >= -5) & (x <= 4)
i2 = (y >= -5) & (y <= 3 - x)
i3 = (z >= -10) & (z <= 6 - x - y)
# get the matrix-vector representation of these inequalities
A, b = getAb([i1, i2, i3], [x, y, z])
Now assume for example that
from pypolyhedralcubature.polyhedralcubature import integrateOnPolytope
# function to integrate
f = lambda x, y, z : x*(x+1) - y*z**2
# integral of f over the polytope defined by the linear inequalities
g = lambda v : f(v[0], v[1], v[2])
I_f = integrateOnPolytope(g, A, b)
I_f["integral"]
# 57892.275000000016
In the case when the function to be integrated is, as in our current example,
a polynomial function, it is better to use the integratePolynomialOnPolytope
function provided by the package. This function implements a procedure
calculating the exact value of the integral. Here is how to use it:
from pypolyhedralcubature.polyhedralcubature import integratePolynomialOnPolytope
from sympy import Poly
from sympy.abc import x, y, z
# polynomial to integrate
P = Poly(x*(x+1) - y*z**2, domain = "RR")
# integral of P over the polytope
integratePolynomialOnPolytope(P, A, b)
# 57892.2750000001
Actually the exact value of the integral is
# polynomial to integrate
P = Poly(x*(x+1) - y*z**2, domain = "QQ")
# integral of P over the polytope
integratePolynomialOnPolytope(P, A, b)
# 2315691/40
I am grateful to the StackOverflow user @Davide_sd for the help he provided
regarding the getAb
function.