15aed1699bb7eda9f1d8659041ebf4a4a049ba0a
[linpy.git] / examples / squares.py
1 #!/usr/bin/env python3
2
3 # This is the code example used in the tutorial. It shows how to define and
4 # manipulate polyhedra.
5
6 import code
7
8
9 class InteractiveConsole(code.InteractiveConsole):
10 def push(self, line=''):
11 if line:
12 print('>>>', line)
13 return super().push(line)
14 else:
15 print()
16
17
18 if __name__ == '__main__':
19
20 shell = InteractiveConsole()
21
22 shell.push('from linpy import *')
23 shell.push("x, y = symbols('x y')")
24 shell.push()
25
26 shell.push('square1 = Le(0, x, 2) & Le(0, y, 2)')
27 shell.push('square1')
28 shell.push()
29
30 shell.push("square2 = Polyhedron('1 <= x <= 3, 1 <= y <= 3')")
31 shell.push('square2')
32 shell.push()
33
34 shell.push('inter = square1.intersection(square2)')
35 shell.push('inter')
36 shell.push()
37
38 shell.push('hull = square1.convex_union(square2)')
39 shell.push('hull')
40 shell.push()
41
42 shell.push('square1.project([y])')
43 shell.push()
44
45 shell.push('inter <= square1')
46 shell.push('inter == Empty')
47 shell.push()
48
49 shell.push('union = square1 | square2')
50 shell.push('union')
51 shell.push('union <= hull')
52 shell.push()
53
54 shell.push('diff = square1 - square2')
55 shell.push('diff')
56 shell.push('~square1')