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