Change pypol to linpy
[linpy.git] / doc / examples.rst
1 Linpy Examples
2 ==============
3
4 Creating a Polyhedron
5 -----------------
6 To create any polyhedron, first define the symbols used. Then use the polyhedron functions to define the constraints for the polyhedron. This example creates a square.
7
8 >>> from pypol import *
9 >>> x, y = symbols('x y')
10 >>> # define the constraints of the polyhedron
11 >>> square1 = Le(0, x) & Le(x, 2) & Le(0, y) & Le(y, 2)
12 >>> print(square1)
13 And(Ge(x, 0), Ge(-x + 2, 0), Ge(y, 0), Ge(-y + 2, 0))
14
15 Urnary Operations
16 -----------------
17
18 >>> square1.isempty()
19 False
20 >>> square1.isbounded()
21 True
22
23 Binary Operations
24 -----------------
25
26 >>> square2 = Le(2, x) & Le(x, 4) & Le(2, y) & Le(y, 4)
27 >>> square1 + square2
28 Or(And(Ge(x, 0), Ge(-x + 2, 0), Ge(y, 0), Ge(-y + 2, 0)), And(Ge(x - 2, 0), Ge(-x + 4, 0), Ge(y - 2, 0), Ge(-y + 4, 0)))
29 >>> # check if square1 and square2 are disjoint
30 >>> square1.disjoint(square2)
31 False
32
33 Plot Examples
34 -------------
35
36 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.
37
38 >>> import matplotlib.pyplot as plt
39 >>> from matplotlib import pylab
40 >>> from mpl_toolkits.mplot3d import Axes3D
41 >>> from pypol import *
42 >>> # define the symbols
43 >>> x, y, z = symbols('x y z')
44 >>> fig = plt.figure()
45 >>> cham_plot = fig.add_subplot(2, 2, 3, projection='3d')
46 >>> cham_plot.set_title('Chamfered cube')
47 >>> cham = Le(0, x) & Le(x, 3) & Le(0, y) & Le(y, 3) & Le(0, z) & Le(z, 3) & Le(z - 2, x) & Le(x, z + 2) & Le(1 - z, x) & Le(x, 5 - z) & Le(z - 2, y) & Le(y, z + 2) & Le(1 - z, y) & Le(y, 5 - z) & Le(y - 2, x) & Le(x, y + 2) & Le(1 - y, x) & Le(x, 5 - y)
48 >>> cham.plot(cham_plot, facecolors=(1, 0, 0, 0.75))
49 >>> pylab.show()
50
51 .. figure:: images/cube.jpg
52 :align: center
53
54 The user can also inspect a polygon's vertices and the integer points included in the polygon.
55
56 >>> diamond = Ge(y, x - 1) & Le(y, x + 1) & Ge(y, -x - 1) & Le(y, -x + 1)
57 >>> diamond.vertices()
58 [Point({x: Fraction(0, 1), y: Fraction(1, 1)}), Point({x: Fraction(-1, 1), y: Fraction(0, 1)}), Point({x: Fraction(1, 1), y: Fraction(0, 1)}), Point({x: Fraction(0, 1), y: Fraction(-1, 1)})]
59 >>> diamond.points()
60 [Point({x: -1, y: 0}), Point({x: 0, y: -1}), Point({x: 0, y: 0}), Point({x: 0, y: 1}), Point({x: 1, y: 0})]
61
62
63
64
65
66
67