Python module for polyhedral geometry.
Project description
Polyhedron manipulation in Python
This library allows common operations over convex polyhedra such as polytope projection and vertex enumeration. Check out the API documentation for details.
Installation
Install system packages for Python and GLPK, for instance for Debian-based Linux distributions:
$ sudo apt-get install cython libglpk-dev python python-dev python-pip
Then, install the library by:
$ pip install pypoman
Some functions, such as point-polytope projection and polygon intersection, are optional and not installed by default. To enable all of them, run:
$ pip install pypoman[all]
Examples
Vertex enumeration
We can compute the list of vertices of a polytope described in halfspace representation by $A x \leq b$:
import numpy as np
from pypoman import compute_polytope_vertices
A = np.array([
[-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1],
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
[1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0],
[0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0],
[0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1]])
b = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 2, 2, 1, 2, 3])
vertices = compute_polytope_vertices(A, b)
Halfspace enumeration
The other way round, assume we know the vertices of a polytope, and want to get its halfspace representation $A x \leq b$.
import numpy as np
from pypoman import compute_polytope_halfspaces
vertices = map(
np.array,
[[1, 0, 0], [0, 1, 0], [1, 1, 0], [0, 0, 1], [0, 1, 1]],
)
A, b = compute_polytope_halfspaces(vertices)
Polytope projection
Let us project an $n$-dimensional polytope $A x \leq b$ over $x = [x_1\ \ldots\ x_n]$ onto its first two coordinates $proj(x) = [x_1 x_2]$:
from numpy import array, eye, ones, vstack, zeros
from pypoman import plot_polygon, project_polytope
n = 10 # dimension of the original polytope
p = 2 # dimension of the projected polytope
# Original polytope:
# - inequality constraints: \forall i, |x_i| <= 1
# - equality constraint: sum_i x_i = 0
A = vstack([+eye(n), -eye(n)])
b = ones(2 * n)
C = ones(n).reshape((1, n))
d = array([0])
ineq = (A, b) # A * x <= b
eq = (C, d) # C * x == d
# Projection is proj(x) = [x_0 x_1]
E = zeros((p, n))
E[0, 0] = 1.
E[1, 1] = 1.
f = zeros(p)
proj = (E, f) # proj(x) = E * x + f
vertices = project_polytope(proj, ineq, eq, method='bretl')
We can then plot the projected polytope:
import pylab
pylab.ion()
pylab.figure()
plot_polygon(vertices)
See also
- A short introduction to Polyhedra and polytopes
- Komei Fukuda's Frequently Asked Questions in Polyhedral Computation
- The Polyhedron class in Sage
- StabiliPy: a Python package implementing a more general recursive method for polytope projection
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
Built Distribution
File details
Details for the file pypoman-1.1.0.tar.gz
.
File metadata
- Download URL: pypoman-1.1.0.tar.gz
- Upload date:
- Size: 34.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea038bdc3c91fcdb6b74effc9962cac69b64e95c87e8309286d511c1ccd60a67 |
|
MD5 | 6c6361ae873103899009b44325c6eb02 |
|
BLAKE2b-256 | bf6f189f88aae6bba212836db70f42719c4c5fe8c27376add79e55dfd3c121d7 |
File details
Details for the file pypoman-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: pypoman-1.1.0-py3-none-any.whl
- Upload date:
- Size: 29.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8893d7dd7212a43c88c08cf3b11ca260f2e4e7d7d20f0183afb3645a91eeedc4 |
|
MD5 | 5a9b9dc1d43e31e05a973248202bd1e8 |
|
BLAKE2b-256 | f49fdd2a2014e519f83a3f4616bcc1400ce34bba21fb497477ac5ef9a1249fb1 |