977b6978b0febc0543fe81cdc8bc1962e45944ec
[linpy.git] / doc / tutorial.rst
1
2 .. _tutorial:
3
4 Tutorial
5 ========
6
7 This section a short introduction to some of LinPy's features.
8 For a comprehensive description of its functionalities, please consult the :ref:`reference` section.
9
10
11 Polyhedra
12 ---------
13
14 The following example shows how we can manipulate polyhedra using LinPy.
15 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}``.
16 First, we need define the symbols used, for instance with the :func:`symbols` function.
17
18 >>> from linpy import *
19 >>> x, y = symbols('x y')
20
21 Then, we can build the :class:`Polyhedron` object ``square1`` from its constraints:
22
23 >>> square1 = Le(0, x, 2) & Le(0, y, 2)
24 >>> square1
25 And(Ge(x, 0), Ge(-x + 2, 0), Ge(y, 0), Ge(-y + 2, 0))
26
27 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.
28 Alternatively, a polyhedron can be built from a string:
29
30 >>> square2 = Polyhedron('1 <= x <= 3, 1 <= y <= 3')
31 >>> square2
32 And(Ge(x - 1, 0), Ge(-x + 3, 0), Ge(y - 1, 0), Ge(-y + 3, 0))
33
34 The usual polyhedral operations are available, including intersection:
35
36 >>> inter = square1.intersection(square2)
37 >>> inter
38 And(Ge(x - 1, 0), Ge(-x + 2, 0), Ge(y - 1, 0), Ge(-y + 2, 0))
39
40 convex union:
41
42 >>> hull = square1.convex_union(square2)
43 >>> hull
44 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))
45
46 and projection:
47
48 >>> square1.project([y])
49 And(Ge(x, 0), Ge(-x + 2, 0))
50
51 Equality and inclusion tests are also provided.
52 Special values :data:`Empty` and :data:`Universe` represent the empty and universe polyhedra.
53
54 >>> inter <= square1
55 True
56 >>> inter == Empty
57 False
58
59
60 Domains
61 -------
62
63 LinPy is also able to manipulate polyhedral *domains*, that is, unions of polyhedra.
64 An example of domain is the set union (as opposed to convex union) of polyhedra ``square1`` and ``square2``.
65 The result is a :class:`Domain` object.
66
67 >>> union = square1 | square2
68 >>> union
69 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)))
70 >>> union <= hull
71 True
72
73 Unlike polyhedra, domains allow exact computation of union, subtraction and complementary operations.
74
75 >>> diff = square1 - square2
76 >>> diff
77 Or(And(Eq(x, 0), Ge(y, 0), Ge(-y + 2, 0)), And(Eq(y, 0), Ge(x - 1, 0), Ge(-x + 2, 0)))
78 >>> ~square1
79 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)))
80
81
82 Plotting
83 --------
84
85 LinPy can use the :mod:`matplotlib` plotting library, if available, to plot bounded polyhedra and domains.
86
87 >>> import matplotlib.pyplot as plt
88 >>> from matplotlib import pylab
89 >>> fig = plt.figure()
90 >>> plot = fig.add_subplot(1, 1, 1, aspect='equal')
91 >>> square1.plot(plot, facecolor='red', alpha=0.3)
92 >>> square2.plot(plot, facecolor='blue', alpha=0.3)
93 >>> hull.plot(plot, facecolor='blue', alpha=0.3)
94 >>> pylab.show()
95
96 Note that you can pass a plot object to the :meth:`Domain.plot` method, which provides great flexibility.
97 Also, keyword arguments can be passed such as color and the degree of transparency of a polygon.
98
99 .. figure:: images/union.jpg
100 :align: center
101
102 3D plots are also supported:
103
104 >>> import matplotlib.pyplot as plt
105 >>> from matplotlib import pylab
106 >>> from mpl_toolkits.mplot3d import Axes3D
107 >>> from linpy import *
108 >>> x, y, z = symbols('x y z')
109 >>> fig = plt.figure()
110 >>> plot = fig.add_subplot(1, 1, 1, projection='3d', aspect='equal')
111 >>> plot.set_title('Chamfered cube')
112 >>> poly = Le(0, x, 3) & Le(0, y, 3) & Le(0, z, 3) & \
113 Le(z - 2, x) & Le(x, z + 2) & Le(1 - z, x) & Le(x, 5 - z) & \
114 Le(z - 2, y) & Le(y, z + 2) & Le(1 - z, y) & Le(y, 5 - z) & \
115 Le(y - 2, x) & Le(x, y + 2) & Le(1 - y, x) & Le(x, 5 - y)
116 >>> poly.plot(plot, facecolor='red', alpha=0.75)
117 >>> pylab.show()
118
119 .. figure:: images/cham_cube.jpg
120 :align: center