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

Using conda

Install the cdd dependency first:

$ conda install cddlib

Then install pypoman from PyPI:

$ pip install pypoman

It won't need to build cdd from source as it is installed from conda-forge.

Building from source

Install system packages for cdd and GLPK, for instance on Debian-based Linux distributions:

$ sudo apt-get install cython libcdd-dev libglpk-dev libgmp3-dev

You can then install the library from PyPI as follows. This approach will likely require building cdd from source.

$ 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

Release files for pypoman 2.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 2.0.0
File Size Uploaded
pypoman-2.0.0.tar.gz 29.1 kB Details

Built distribution (wheel)

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

Total release size: 59.4 kB

Release files / pypoman-2.0.0.tar.gz

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

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

Download URL pypoman-2.0.0-py3-none-any.whl
Size 30.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e34438ab20ac555289b45928318fbcc37da48147dccb89473664a3376ccb8ce6
BLAKE2b-256 checksum
How to use checksums
fd272435e52f4f3d71e88d0444b53ebf81d5ef6a913273809b4cf2cd8185f39b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.32.4

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.6.0

2 release files

0.5.4

1 release file

0.5.3

1 release file

0.5.2

1 release file

0.5.1

1 release file

0.5.0

1 release file

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