10 The following example shows how we can manipulate polyhedra using LinPy.
11 Let us define two square polyhedra, corresponding to the sets ``square1 = {(x, y) | 0 <= x <= 2, 0 <= y <= 2}`` and ``square2 = {(x, y) | 2 <= x <= 4, 2 <= y <= 4}``.
12 First, we need define the symbols used, for instance with the :func:`symbols` function.
14 >>> from linpy import *
15 >>> x, y = symbols('x y')
17 Then, we can build the :class:`Polyhedron` object ``square1`` from its constraints:
19 >>> square1 = Le(0, x, 2) & Le(0, y, 2)
21 And(Ge(x, 0), Ge(-x + 2, 0), Ge(y, 0), Ge(-y + 2, 0))
23 LinPy provides comparison functions :func:`Lt`, :func:`Le`, :func:`Eq`, :func:`Ne`, :func:`Ge` and :func:`Gt` to build constraints, and logical operators :func:`And`, :func:`Or`, :func:`Not` to combine them.
24 Alternatively, a polyhedron can be built from a string:
26 >>> square2 = Polyhedron('1 <= x <= 3, 1 <= y <= 3')
28 And(Ge(x - 1, 0), Ge(-x + 3, 0), Ge(y - 1, 0), Ge(-y + 3, 0))
30 The usual polyhedral operations are available, including intersection:
32 >>> inter = square1.intersection(square2)
34 And(Ge(x - 1, 0), Ge(-x + 2, 0), Ge(y - 1, 0), Ge(-y + 2, 0))
38 >>> hull = square1.convex_union(square2)
40 And(Ge(x, 0), Ge(y, 0), Ge(-x + y + 2, 0), Ge(x - y + 2, 0), Ge(-x + 3, 0), Ge(-y + 3, 0))
44 >>> square1.project([y])
45 And(Ge(x, 0), Ge(-x + 2, 0))
47 Equality and inclusion tests are also provided.
48 Special values :data:`Empty` and :data:`Universe` represent the empty and universe polyhedra.
59 LinPy is also able to manipulate polyhedral *domains*, that is, unions of polyhedra.
60 An example of domain is the set union (as opposed to convex union) of polyhedra ``square1`` and ``square2``.
61 The result is a :class:`Domain` object.
63 >>> union = square1 | square2
65 Or(And(Ge(-x + 2, 0), Ge(x, 0), Ge(-y + 2, 0), Ge(y, 0)), And(Ge(-x + 3, 0), Ge(x - 1, 0), Ge(-y + 3, 0), Ge(y - 1, 0)))
69 Unlike polyhedra, domains allow exact computation of union, subtraction and complementary operations.
71 >>> diff = square1 - square2
73 Or(And(Eq(x, 0), Ge(y, 0), Ge(-y + 2, 0)), And(Eq(y, 0), Ge(x - 1, 0), Ge(-x + 2, 0)))
75 Or(Ge(-x - 1, 0), Ge(x - 3, 0), And(Ge(x, 0), Ge(-x + 2, 0), Ge(-y - 1, 0)), And(Ge(x, 0), Ge(-x + 2, 0), Ge(y - 3, 0)))
81 LinPy can use the :mod:`matplotlib` plotting library, if available, to plot bounded polyhedra and domains.
83 >>> import matplotlib.pyplot as plt
84 >>> from matplotlib import pylab
85 >>> fig = plt.figure()
86 >>> plot = fig.add_subplot(1, 1, 1, aspect='equal')
87 >>> square1.plot(plot, facecolor='red', alpha=0.3)
88 >>> square2.plot(plot, facecolor='blue', alpha=0.3)
89 >>> hull.plot(plot, facecolor='blue', alpha=0.3)
92 Note that you can pass a plot object to the :meth:`Domain.plot` method, which provides great flexibility.
93 Also, keyword arguments can be passed such as color and the degree of transparency of a polygon.
95 .. figure:: images/union.jpg
98 3D plots are also supported.
100 >>> import matplotlib.pyplot as plt
101 >>> from matplotlib import pylab
102 >>> from mpl_toolkits.mplot3d import Axes3D
103 >>> from linpy import *
104 >>> x, y, z = symbols('x y z')
105 >>> fig = plt.figure()
106 >>> plot = fig.add_subplot(1, 1, 1, projection='3d', aspect='equal')
107 >>> plot.set_title('Chamfered cube')
108 >>> poly = Le(0, x, 3) & Le(0, y, 3) & Le(0, z, 3) & \
109 Le(z - 2, x) & Le(x, z + 2) & Le(1 - z, x) & Le(x, 5 - z) & \
110 Le(z - 2, y) & Le(y, z + 2) & Le(1 - z, y) & Le(y, 5 - z) & \
111 Le(y - 2, x) & Le(x, y + 2) & Le(1 - y, x) & Le(x, 5 - y)
112 >>> poly.plot(plot, facecolor='red', alpha=0.75)
115 .. figure:: images/cham_cube.jpg