More robust implementation of isl_version
[linpy.git] / doc / examples.rst
1
2 .. _examples:
3
4 Examples
5 ========
6
7 Basic Examples
8 --------------
9
10 To create any polyhedron, first define the symbols used.
11 Then use the polyhedron functions to define the constraints.
12 The following is a simple running example illustrating some different operations and properties that can be performed by LinPy with two squares.
13
14 >>> from linpy import *
15 >>> x, y = symbols('x y')
16 >>> # define the constraints of the polyhedron
17 >>> square1 = Le(0, x) & Le(x, 2) & Le(0, y) & Le(y, 2)
18 >>> square1
19 And(Ge(x, 0), Ge(-x + 2, 0), Ge(y, 0), Ge(-y + 2, 0))
20
21 Binary operations and properties examples:
22
23 >>> # create a polyhedron from a string
24 >>> square2 = Polyhedron('1 <= x') & Polyhedron('x <= 3') & \
25 Polyhedron('1 <= y') & Polyhedron('y <= 3')
26 >>> #test equality
27 >>> square1 == square2
28 False
29 >>> # compute the union of two polyhedra
30 >>> square1 | square2
31 Or(And(Ge(x, 0), Ge(-x + 2, 0), Ge(y, 0), Ge(-y + 2, 0)), \
32 And(Ge(x - 1, 0), Ge(-x + 3, 0), Ge(y - 1, 0), Ge(-y + 3, 0)))
33 >>> # check if square1 and square2 are disjoint
34 >>> square1.disjoint(square2)
35 False
36 >>> # compute the intersection of two polyhedra
37 >>> square1 & square2
38 And(Ge(x - 1, 0), Ge(-x + 2, 0), Ge(y - 1, 0), Ge(-y + 2, 0))
39 >>> # compute the convex union of two polyhedra
40 >>> Polyhedron(square1 | sqaure2)
41 And(Ge(x, 0), Ge(y, 0), Ge(-y + 3, 0), Ge(-x + 3, 0), \
42 Ge(x - y + 2, 0), Ge(-x + y + 2, 0))
43
44 Unary operation and properties examples:
45
46 >>> square1.isempty()
47 False
48 >>> # compute the complement of square1
49 >>> ~square1
50 Or(Ge(-x - 1, 0), Ge(x - 3, 0), And(Ge(x, 0), Ge(-x + 2, 0), \
51 Ge(-y - 1, 0)), And(Ge(x, 0), Ge(-x + 2, 0), Ge(y - 3, 0)))
52 >>> square1.symbols()
53 (x, y)
54 >>> square1.inequalities
55 (x, -x + 2, y, -y + 2)
56 >>> # project out the variable x
57 >>> square1.project([x])
58 And(Ge(-y + 2, 0), Ge(y, 0))
59
60 Plot Examples
61 -------------
62
63 LinPy can use the matplotlib plotting library to plot 2D and 3D polygons.
64 This can be a useful tool to visualize and compare polygons.
65 The user has the option to pass plot objects to the :meth:`Domain.plot` method, which provides great flexibility.
66 Also, keyword arguments can be passed such as color and the degree of transparency of a polygon.
67
68 >>> import matplotlib.pyplot as plt
69 >>> from matplotlib import pylab
70 >>> from mpl_toolkits.mplot3d import Axes3D
71 >>> from linpy import *
72 >>> # define the symbols
73 >>> x, y, z = symbols('x y z')
74 >>> fig = plt.figure()
75 >>> cham_plot = fig.add_subplot(1, 1, 1, projection='3d', aspect='equal')
76 >>> cham_plot.set_title('Chamfered cube')
77 >>> cham = Le(0, x) & Le(x, 3) & Le(0, y) & Le(y, 3) & Le(0, z) & \
78 Le(z, 3) & Le(z - 2, x) & Le(x, z + 2) & Le(1 - z, x) & \
79 Le(x, 5 - z) & Le(z - 2, y) & Le(y, z + 2) & Le(1 - z, y) & \
80 Le(y, 5 - z) & Le(y - 2, x) & Le(x, y + 2) & Le(1 - y, x) & Le(x, 5 - y)
81 >>> cham.plot(cham_plot, facecolor='red', alpha=0.75)
82 >>> pylab.show()
83
84 .. figure:: images/cham_cube.jpg
85 :align: center
86
87 LinPy can also inspect a polygon's vertices and the integer points included in the polygon.
88
89 >>> diamond = Ge(y, x - 1) & Le(y, x + 1) & Ge(y, -x - 1) & Le(y, -x + 1)
90 >>> diamond.vertices()
91 [Point({x: Fraction(0, 1), y: Fraction(1, 1)}), \
92 Point({x: Fraction(-1, 1), y: Fraction(0, 1)}), \
93 Point({x: Fraction(1, 1), y: Fraction(0, 1)}), \
94 Point({x: Fraction(0, 1), y: Fraction(-1, 1)})]
95 >>> diamond.points()
96 [Point({x: -1, y: 0}), Point({x: 0, y: -1}), Point({x: 0, y: 0}), \
97 Point({x: 0, y: 1}), Point({x: 1, y: 0})]
98
99 The user also can pass another plot to the :meth:`Domain.plot` method.
100 This can be useful to compare two polyhedra on the same axis.
101 This example illustrates the union of two squares.
102
103 >>> from linpy import *
104 >>> import matplotlib.pyplot as plt
105 >>> from matplotlib import pylab
106 >>> x, y = symbols('x y')
107 >>> square1 = Le(0, x) & Le(x, 2) & Le(0, y) & Le(y, 2)
108 >>> square2 = Le(1, x) & Le(x, 3) & Le(1, y) & Le(y, 3)
109 >>> fig = plt.figure()
110 >>> plot = fig.add_subplot(1, 1, 1, aspect='equal')
111 >>> square1.plot(plot, facecolor='red', alpha=0.3)
112 >>> square2.plot(plot, facecolor='blue', alpha=0.3)
113 >>> squares = Polyhedron(square1 + square2)
114 >>> squares.plot(plot, facecolor='blue', alpha=0.3)
115 >>> pylab.show()
116
117 .. figure:: images/union.jpg
118 :align: center