Replace examples by tutorial in documentation
[linpy.git] / doc / tutorial.rst
1
2 .. _tutorial:
3
4 Tutorial
5 ========
6
7 Polyhedra
8 ---------
9
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.
13
14 >>> from linpy import *
15 >>> x, y = symbols('x y')
16
17 Then, we can build the :class:`Polyhedron` object ``square1`` from its constraints:
18
19 >>> square1 = Le(0, x, 2) & Le(0, y, 2)
20 >>> square1
21 And(Ge(x, 0), Ge(-x + 2, 0), Ge(y, 0), Ge(-y + 2, 0))
22
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:
25
26 >>> square2 = Polyhedron('1 <= x <= 3, 1 <= y <= 3')
27 >>> square2
28 And(Ge(x - 1, 0), Ge(-x + 3, 0), Ge(y - 1, 0), Ge(-y + 3, 0))
29
30 The usual polyhedral operations are available, including intersection:
31
32 >>> inter = square1.intersection(square2)
33 >>> inter
34 And(Ge(x - 1, 0), Ge(-x + 2, 0), Ge(y - 1, 0), Ge(-y + 2, 0))
35
36 convex union:
37
38 >>> hull = square1.convex_union(square2)
39 >>> hull
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))
41
42 and projection:
43
44 >>> square1.project([y])
45 And(Ge(x, 0), Ge(-x + 2, 0))
46
47 Equality and inclusion tests are also provided.
48 Special values :data:`Empty` and :data:`Universe` represent the empty and universe polyhedra.
49
50 >>> inter <= square1
51 True
52 >>> inter == Empty
53 False
54
55
56 Domains
57 -------
58
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.
62
63 >>> union = square1 | square2
64 >>> union
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)))
66 >>> union <= hull
67 True
68
69 Unlike polyhedra, domains allow exact computation of union, subtraction and complementary operations.
70
71 >>> diff = square1 - square2
72 >>> diff
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)))
74 >>> ~square1
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)))
76
77
78 Plotting
79 --------
80
81 LinPy can use the :mod:`matplotlib` plotting library, if available, to plot bounded polyhedra and domains.
82
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)
90 >>> pylab.show()
91
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.
94
95 .. figure:: images/union.jpg
96 :align: center
97
98 3D plots are also supported.
99
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)
113 >>> pylab.show()
114
115 .. figure:: images/cham_cube.jpg
116 :align: center