Skip to main content

Polyhedron manipulation in Python

Build Documentation Coverage PyPI version PyPI downloads

This library interfaces common operations over convex polyhedra such as polytope projection and vertex enumeration. Check out the documentation for details.

Installation

$ pip install pypoman

This installs the core library, covering for instance polytope projection with the Bretl method, Chebyshev centers and polygon operations. The following features rely on optional dependencies:

Extra Dependency Features
cdd pycddlib Vertex and halfspace enumeration, polytope projection with method="cdd"
all All of the above, plus pyclipper and qpsolvers Everything, including polygon intersection and point-to-polytope projection

Installing cdd

The cdd and all extras depend on pycddlib, which is built from source against cddlib and GMP. The simplest way is to get these libraries from conda-forge:

$ conda install cddlib
$ pip install pypoman[cdd]

Alternatively, install them as system packages, for instance on Debian-based Linux distributions:

$ sudo apt-get install libcdd-dev libgmp3-dev
$ pip install pypoman[cdd]

Replace pypoman[cdd] with pypoman[all] to enable all optional features.

Examples

The examples can be run from a clone of the repository using uv, for instance:

$ uv run examples/polytope_projection.py

Vertex enumeration

This example requires the cdd extra. 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

This example requires the cdd extra. 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

Metadata

Release files for pypoman 3.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pypoman 3.0.0
File Size Uploaded Provenance Actions
pypoman-3.0.0.tar.gz 31.0 kB Without provenance Details for pypoman-3.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pypoman 3.0.0
File Interpreter ABI Platform Actions
pypoman-3.0.0-py3-none-any.whl Python 3 none any Details for pypoman-3.0.0-py3-none-any.whl

Total release size: 61.6 kB

Release files / pypoman-3.0.0.tar.gz

Download URL pypoman-3.0.0.tar.gz
Size 31.0 kB
Tags Source
SHA-256 checksum
How to use checksums
cf7618a7edaf39ab97a36ae56ca2d99df091e6b29e93414f45f7c2626b1804c6
BLAKE2b-256 checksum
How to use checksums
4f05d7684e93481dbcbe54d83b5576998458814bcd8d0203e8194a6dcc3ede01
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.32.4

Release files / pypoman-3.0.0-py3-none-any.whl

Download URL pypoman-3.0.0-py3-none-any.whl
Size 30.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
66f1b4bc6a3e169807f0670ae8f6954f720cd0d3a1cd5d2c26fbe7fd73c81540
BLAKE2b-256 checksum
How to use checksums
81027b9ffff7b3ebb3e602f2e1d09f62ee62ba8ff2aecb1eeb4ea373ae0e0d15
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.32.4

Security for pypoman 3.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page