Skip to main content

Python module for polyhedral geometry.

Project description

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

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

pypoman-2.0.0.tar.gz (29.1 kB view details)

Uploaded Source

Built Distribution

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

pypoman-2.0.0-py3-none-any.whl (30.3 kB view details)

Uploaded Python 3

File details

Details for the file pypoman-2.0.0.tar.gz.

File metadata

  • Download URL: pypoman-2.0.0.tar.gz
  • Upload date:
  • Size: 29.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.4

File hashes

Hashes for pypoman-2.0.0.tar.gz
Algorithm Hash digest
SHA256 d34d094f914fc10ef9ae7dce96d96e23dda3c43e23792a0ef2ed99aed9f4feeb
MD5 3a4722009c53b316b8812018f0d33a18
BLAKE2b-256 c6ff0254450117183f67d514d36f991326b995c09d5f926089073c1b4de119b4

See more details on using hashes here.

File details

Details for the file pypoman-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: pypoman-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 30.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.4

File hashes

Hashes for pypoman-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e34438ab20ac555289b45928318fbcc37da48147dccb89473664a3376ccb8ce6
MD5 de22b7e29666ff5c7a731624445e884d
BLAKE2b-256 fd272435e52f4f3d71e88d0444b53ebf81d5ef6a913273809b4cf2cd8185f39b

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