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