Skip to main content

Multiple integration on convex polytopes.

Project description

pypolyhedralcubature

Documentation status

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.

$$\int_{-5}^4\int_{-5}^{3-x}\int_{-10}^{6-x-y} f(x, y, z) \text{d}z \text{d}y \text{d}x.$$

In other words, the domain of integration is given by a set of linear inequalities:

$$\left{\begin{matrix} -5 & \leq & x & \leq & 4 \\ -5 & \leq & y & \leq & 3-x \\ -10 & \leq & z & \leq & 6-x-y \end{matrix}\right..$$

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

$$A {(x,y,z)}' \leqslant b.$$

The matrix $A$ and the vector $b$ appear when one rewrites the linear inequalities above as:

$$\left{\begin{matrix} -x & \leq & 5 \\ x & \leq & 4 \\ -y & \leq & 5 \\ x+y & \leq & 3 \\ -z & \leq & 10 \\ x+y+z & \leq & 6 \end{matrix}\right..$$

The matrix $A$ is given by the coefficients of $x$, $y$, $z$ at the left-hand sides, and the vector $b$ is made of the upper bounds at the right-hand sides:

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 $A$ and $b$ in a user-friendly way:

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 $f(x,y,z) = x(x+1) - yz^2$. Once we have $A$ and $b$, here is how to evaluate the integral of $f$ over the convex polytope:

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 $57892.275$, so there is a slight numerical error in the procedure.

Acknowledgments

I am grateful to the StackOverflow user @Davide_sd for the help he provided regarding the getAb function.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pypolyhedralcubature-0.1.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pypolyhedralcubature-0.1.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file pypolyhedralcubature-0.1.0.tar.gz.

File metadata

  • Download URL: pypolyhedralcubature-0.1.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.10.5 Windows/10

File hashes

Hashes for pypolyhedralcubature-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d3757c89a004bed7e734a2c0c5d37a0f05caff690a4c7889bf5197550ac1c237
MD5 b1da46d26782467346de5ea88673a524
BLAKE2b-256 2fac42970dc981195178c9328c50907d5ae03a24592db97dfc8a0ee7836fc571

See more details on using hashes here.

File details

Details for the file pypolyhedralcubature-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pypolyhedralcubature-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f2e7131b52dffcfb475f6eb2fedaf754d6dbe29b92e5d11cbe4c3bbfe56b507a
MD5 9d29c2bb0ba169c28675ea6767b2b667
BLAKE2b-256 52c253b0a18a17d7354ccf1545aa638d598b79fb1da9634d9760edcbfaa863a6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page