Exact integral of a polynomial on a simplex
The paper Simple formula for integration of polynomials on a simplex by Jean B. Lasserre provides a method to calculate the exact value of the integral of a multivariate polynomial on a simplex (i.e. a tetrahedron in dimension three). I implemented it in Julia, Python, and R.
Integration on simplices is important, because any convex polyhedron can be decomposed into simplices, thanks to the Delaunay tessellation. Therefore one can integrate over convex polyhedra once one can integrate over simplices (I wrote an example of doing so with R).
Julia
using TypedPolynomials
using LinearAlgebra
function integratePolynomialOnSimplex(P, S)
= variables(P)
gens = length(gens)
n = S[end]
v = Array{Float64}(undef, n, 0)
B for i in 1:n
= hcat(B, S[i] - v)
B end
= P(gens => v + B * vec(gens))
Q = 0.0
s for t in terms(Q)
= TypedPolynomials.coefficient(t)
coef = TypedPolynomials.exponents(t)
powers = sum(powers)
j if j == 0
= s + coef
s continue
end
= coef * prod(factorial.(powers))
coef = s + coef / prod((n+1):(n+j))
s end
return abs(LinearAlgebra.det(B)) / factorial(n) * s
end
Julia example
We define the polynomial to be integrated as follows:
using TypedPolynomials
@polyvar x y z
= x^4 + y + 2*x*y^2 - 3*z P
Be careful. If the expression of your polynomial does not involve one of the variables, e.g. \(P(x, y, z) = x^4 + 2xy^2\), you must define a polynomial involving this variable:
= x^4 + 2*x*y^2 + 0.0*z P
Now we define the simplex as a matrix whose rows correspond to the vertices:
# simplex vertices
= [1.0, 1.0, 1.0]
v1 = [2.0, 2.0, 3.0]
v2 = [3.0, 4.0, 5.0]
v3 = [3.0, 2.0, 1.0]
v4 # simplex
= [v1, v2, v3, v4] S
And finally we run the function:
, S) integratePolynomialOnSimplex(P
Python
from math import factorial
from sympy import Poly
import numpy as np
def _term(Q, monom):
= Q.coeff_monomial(monom)
coef = list(monom)
powers = sum(powers)
j if j == 0:
return coef
= coef * np.prod(list(map(factorial, powers)))
coef = len(monom)
n return coef / np.prod(list(range(n+1, n+j+1)))
def integratePolynomialOnSimplex(P, S):
= P.gens
gens = len(gens)
n = np.asarray(S)
S = S[n,:]
v = []
columns for i in range(n):
- v)
columns.append(S[i,:] = np.column_stack(tuple(columns))
B = {}
dico for i in range(n):
= v[i]
newvar for j in range(n):
= newvar + B[i,j]*Poly(gens[j], gens, domain="RR")
newvar = newvar.as_expr()
dico[gens[i]] = P.subs(dico, simultaneous=True).as_expr().as_poly(gens)
Q print(Q)
= Q.monoms()
monoms = 0.0
s for monom in monoms:
= s + _term(Q, monom)
s return np.abs(np.linalg.det(B)) / factorial(n) * s
Python example
# simplex vertices
= [1.0, 1.0, 1.0]
v1 = [2.0, 2.0, 3.0]
v2 = [3.0, 4.0, 5.0]
v3 = [3.0, 2.0, 1.0]
v4 # simplex
= [v1, v2, v3, v4]
S
# polynomial to integrate
from sympy import Poly
from sympy.abc import x, y, z
= Poly(x**4 + y + 2*x*y**2 - 3*z, x, y, z, domain = "RR")
P
# integral
integratePolynomialOnSimplex(P, S)
R
library(spray)
function(P, S) {
integratePolynomialonSimplex <- ncol(S)
n <- S[n+1L, ]
v <- t(S[1L:n, ]) - v
B <- lapply(1L:n, function(i) lone(i, n))
gens <- vector("list", n)
newvars <-for(i in 1L:n) {
v[i]
newvar <- B[i, ]
Bi <-for(j in 1L:n) {
newvar + Bi[j] * gens[[j]]
newvar <-
} newvar
newvars[[i]] <-
} 0
Q <- P[["index"]]
exponents <- P[["value"]]
coeffs <-for(i in 1L:nrow(exponents)) {
exponents[i, ]
powers <- 1
term <-for(j in 1L:n) {
term * newvars[[j]]^powers[j]
term <-
} Q + coeffs[i] * term
Q <-
} 0
s <- Q[["index"]]
exponents <- Q[["value"]]
coeffs <-for(i in 1L:nrow(exponents)) {
coeffs[i]
coef <- exponents[i, ]
powers <- sum(powers)
d <-if(d == 0L) {
s + coef
s <-next
} coef * prod(factorial(powers))
coef <- s + coef / prod((n+1L):(n+d))
s <-
}abs(det(B)) / factorial(n) * s
}
R example
library(spray)
# variables
lone(1, 3)
x <- lone(2, 3)
y <- lone(3, 3)
z <-# polynomial
x^4 + y + 2*x*y^2 - 3*z
P <-
# simplex (tetrahedron) vertices
c(1, 1, 1)
v1 <- c(2, 2, 3)
v2 <- c(3, 4, 5)
v3 <- c(3, 2, 1)
v4 <-# simplex
rbind(v1, v2, v3, v4)
S <-
# integral
integratePolynomialonSimplex(P, S)
Note
The functions do not check whether the given matrix S
defines a non-degenerate simplex. This is equivalent to the invertibility of the matrix B
constructed in the functions.