Add methods may be useful for larger polys, change format of printing
[linpy.git] / examples / squares.py
1 #!/usr/bin/env python3
2
3 from pypol import *
4
5 a, x, y, z = symbols('a x y z')
6
7 sq1 = Le(0, x) & Le(x, 2) & Le(0, y) & Le(y, 2)
8 sq2 = Le(2, x) & Le(x, 4) & Le(2, y) & Le(y, 4)
9 sq3 = Le(0, x) & Le(x, 3) & Le(0, y) & Le(y, 3)
10 sq4 = Le(1, x) & Le(x, 2) & Le(1, y) & Le(y, 2)
11 sq5 = Le(1, x) & Le(x, 2) & Le(1, y)
12 sq6 = Le(1, x) & Le(x, 2) & Le(1, y) & Eq(y, 3)
13 sq7 = Le(0, x) & Le(x, 2) & Le(0, y) & Eq(z, 2) & Le(a, 3)
14 universe = Polyhedron([])
15 q = sq1 - sq2
16 e = Empty
17
18 print('sq1 =', sq1) #print correct square
19 print('sq2 =', sq2) #print correct square
20 print('sq3 =', sq3) #print correct square
21 print('sq4 =', sq4) #print correct square
22 print('universe =', universe) #print correct square
23 print()
24 print('¬sq1 =', ~sq1) #test complement
25 print()
26 print('sq1 + sq1 =', sq1 + sq2) #test addition
27 print('sq1 + sq2 =', Polyhedron(sq1 + sq2)) #test addition
28 print()
29 print('universe + universe =', universe + universe)#test addition
30 print('universe - universe =', universe - universe) #test subtraction
31 print()
32 print('sq2 - sq1 =', sq2 - sq1) #test subtraction
33 print('sq2 - sq1 =', Polyhedron(sq2 - sq1)) #test subtraction
34 print('sq1 - sq1 =', Polyhedron(sq1 - sq1)) #test subtraction
35 print()
36 print('sq1 ∩ sq2 =', sq1 & sq2) #test intersection
37 print('sq1 ∪ sq2 =', sq1 | sq2) #test union
38 print()
39 print('sq1 ⊔ sq2 =', Polyhedron(sq1 | sq2)) # test convex union
40 print()
41 print('check if sq1 and sq2 disjoint:', sq1.isdisjoint(sq2)) #should return false
42 print()
43 print('sq1 disjoint:', sq1.disjoint()) #make disjoint
44 print('sq2 disjoint:', sq2.disjoint()) #make disjoint
45 print()
46 print('is square 1 universe?:', sq1.isuniverse()) #test if square is universe
47 print('is u universe?:', universe.isuniverse()) #test if square is universe
48 print()
49 print('is sq1 a subset of sq2?:', sq1.issubset(sq2)) #test issubset()
50 print('is sq4 less than sq3?:', sq4.__lt__(sq3)) # test lt(), must be a strict subset
51 print()
52 print('lexographic min of sq1:', sq1.lexmin()) #test lexmin()
53 print('lexographic max of sq1:', sq1.lexmax()) #test lexmin()
54 print()
55 print('lexographic min of sq2:', sq2.lexmin()) #test lexmax()
56 print('lexographic max of sq2:', sq2.lexmax()) #test lexmax()
57 print()
58 print('Polyhedral hull of sq1 + sq2 is:', q.polyhedral_hull()) #test polyhedral hull
59 print()
60 print('is sq1 bounded?', sq1.isbounded()) #unbounded should return True
61 print('is sq5 bounded?', sq5.isbounded()) #unbounded should return False
62 print()
63 print('sq6:', sq6)
64 print('sq6 simplified:', sq6.sample())
65 print()
66 print(universe.project_out([x]))
67 print('sq7 with out constraints involving y and a', sq7.project_out([a, z, x, y])) #drops dims that are passed
68 print()
69 print('sq1 has {} parameters'.format(sq1.num_parameters()))
70 print()
71 print('does sq1 constraints involve x?', sq1.involves_dims([x]))