API Reference

polyglint2d: Computing two-dimensional bounded linear integrals.

integrate(c, d, A, b)

Compute the integral (2D) of a linear function over a convex polygon:

\[\int_{ Ax \leq b } c'x \, {\rm d} x,\]

i.e., the integral of the linear function \(c'x\), over the 2-dimensional polygon described by \(Ax \leq b\). (The method assumes that the polygon is closed.)

Parameters:

Name Type Description Default
c array

Integrand coefficients; \({\mathbb R}^2\)

required
d Real

Integrand bias; \({\mathbb R}\)

required
A array

Constraint matrix; \({\mathbb R}^{n \times 2}\)

required
b array

Constraint vector; \({\mathbb R}^n\)

required

Returns:

Name Type Description
value Real

The value of the integral.

Example:

Area of the triangle \(x + y \leq 1\), \(x, y \geq 0\):

>>> Ab = np.array([[1, 1, 1], [-1, 0, 0], [0, -1, 0]])
>>> A, b = Ab[:, :-1], Ab[:, -1]
>>> integrate([0, 0], 1, A, b.T)
np.float64(0.5)

integrate_over_convexhull(c, d, vertices)

Compute the 2D integral of a linear function over the convex hull of given points.

Parameters:

Name Type Description Default
c array

Integrand coefficients; \({\mathbb R}^2\)

required
d Real

Integrand bias; \({\mathbb R}\)

required
vertices Collection

Collection of vertices; [\({\mathbb R}^2\)]

required

Returns:

Name Type Description
value Real

The value of the integral.

Example:

Calculating the volume contained in \(0 \leq x, y \leq 1\), and between the \(z=0\) and \(z=x\) planes:

>>> corners = [(0, 0), (0, 1), (1, 0), (1, 1)]
>>> integrate_over_convexhull([1, 0], 0, corners)
np.float64(0.5)

lint_trap(cx, cy, d, a, b, m1, b1, m2, b2)

Compute the integral of a linear function over a horizontal trapezoid:

\[\int_{x=a}^b \int_{y=m_1 x + b_1}^{m_2 x + b_2} ( c_x x + c_y y + d ) \, {\rm d}y \, {\rm d}x.\]

The closed form given by Wolfram Alpha is surprisingly ugly, so this method uses a semi-symbolic approach: It uses numpy.{polyint, polymul} to evaluate intermediate polynomials.

Returns:

Name Type Description
value Real

The value of the integral.

Examples:

>>> lint_trap(0, 0, 1, 0, 1, 0, 0, -1, 1)
np.float64(0.5)

enumerate_vertices_2d(A, b, **kwargs)

Enumerate the vertices of \(Ax \leq b\).

This is currently an inefficient implementation of 2-dimensional vertex enumeration. It simply enumerates all bases (row pairs) and checks for containment/feasibility.

(Assumes a bounded polygon, and doesn't bother to check.)

Parameters:

Name Type Description Default
A array

Constraint matrix; \({\mathbb R}^{n \times 2}\)

required
b array

Constraint vector; \({\mathbb R}^n\)

required

Returns:

Name Type Description
vertices Tuple

The vertices of the convex region bounded.

Example:

Enumerate the vertices of the unit square.

>>> Ab = np.array([[-1, 0, 0], [0, -1, 0], [1, 0, 1], [0, 1, 1]])
>>> A, b = Ab[:, :-1], Ab[:, -1]
>>> set(enumerate_vertices_2d(A, b)) == set((x, y) for x in [0, 1] for y in [0, 1])
True