Fix warnings in documentation generation
[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 >>> square2 = Le(1, x) & Le(x, 3) & Le(1, y) & Le(y, 3)
19 >>> #test equality
20 >>> square1 == square2
21 False
22 >>> # compute the union of two polyhedrons
23 >>> square1 | square2
24 Or(And(Ge(x, 0), Ge(-x + 2, 0), Ge(y, 0), Ge(-y + 2, 0)), And(Ge(x - 1, 0), Ge(-x + 3, 0), Ge(y - 1, 0), Ge(-y + 3, 0)))
25 >>> # check if square1 and square2 are disjoint
26 >>> square1.disjoint(square2)
27 False
28 >>> # compute the intersection of two polyhedrons
29 >>> square1 & square2
30 And(Ge(x - 1, 0), Ge(-x + 2, 0), Ge(y - 1, 0), Ge(-y + 2, 0))
31 >>> # compute the convex union of two polyhedrons
32 >>> Polyhedron(square1 | sqaure2)
33 And(Ge(x, 0), Ge(y, 0), Ge(-y + 3, 0), Ge(-x + 3, 0), Ge(x - y + 2, 0), Ge(-x + y + 2, 0))
34
35 Unary operation and properties examples:
36
37 >>> square1.isempty()
38 False
39 >>> square1.symbols()
40 (x, y)
41 >>> square1.inequalities
42 (x, -x + 2, y, -y + 2)
43 >>> # project out the variable x
44 >>> square1.project([x])
45 And(Ge(-y + 2, 0), Ge(y, 0))
46
47 Plot Examples
48 -------------
49
50 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.
51
52 >>> import matplotlib.pyplot as plt
53 >>> from matplotlib import pylab
54 >>> from mpl_toolkits.mplot3d import Axes3D
55 >>> from linpy import *
56 >>> # define the symbols
57 >>> x, y, z = symbols('x y z')
58 >>> fig = plt.figure()
59 >>> cham_plot = fig.add_subplot(1, 1, 1, projection='3d', aspect='equal')
60 >>> cham_plot.set_title('Chamfered cube')
61 >>> cham = Le(0, x) & Le(x, 3) & Le(0, y) & Le(y, 3) & Le(0, z) & \
62 Le(z, 3) & Le(z - 2, x) & Le(x, z + 2) & Le(1 - z, x) & \
63 Le(x, 5 - z) & Le(z - 2, y) & Le(y, z + 2) & Le(1 - z, y) & \
64 Le(y, 5 - z) & Le(y - 2, x) & Le(x, y + 2) & Le(1 - y, x) & Le(x, 5 - y)
65 >>> cham.plot(cham_plot, facecolor='red', alpha=0.75)
66 >>> pylab.show()
67
68 .. figure:: images/cham_cube.jpg
69 :align: center
70
71 LinPy can also inspect a polygon's vertices and the integer points included in the polygon.
72
73 >>> diamond = Ge(y, x - 1) & Le(y, x + 1) & Ge(y, -x - 1) & Le(y, -x + 1)
74 >>> diamond.vertices()
75 [Point({x: Fraction(0, 1), y: Fraction(1, 1)}), \
76 Point({x: Fraction(-1, 1), y: Fraction(0, 1)}), \
77 Point({x: Fraction(1, 1), y: Fraction(0, 1)}), \
78 Point({x: Fraction(0, 1), y: Fraction(-1, 1)})]
79 >>> diamond.points()
80 [Point({x: -1, y: 0}), Point({x: 0, y: -1}), Point({x: 0, y: 0}), \
81 Point({x: 0, y: 1}), Point({x: 1, y: 0})]
82
83 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.
84
85 >>> from linpy import *
86 >>> import matplotlib.pyplot as plt
87 >>> from matplotlib import pylab
88 >>> x, y = symbols('x y')
89 >>> square1 = Le(0, x) & Le(x, 2) & Le(0, y) & Le(y, 2)
90 >>> square2 = Le(1, x) & Le(x, 3) & Le(1, y) & Le(y, 3)
91 >>> fig = plt.figure()
92 >>> plot = fig.add_subplot(1, 1, 1, aspect='equal')
93 >>> square1.plot(plot, facecolor='red', alpha=0.3)
94 >>> square2.plot(plot, facecolor='blue', alpha=0.3)
95 >>> squares = Polyhedron(square1 + square2)
96 >>> squares.plot(plot, facecolor='blue', alpha=0.3)
97 >>> pylab.show()
98
99 .. figure:: images/union.jpg
100 :align: center
101
102
103
104