4 import matplotlib
.pyplot
as plt
5 from matplotlib
import pylab
7 a
, x
, y
, z
= symbols('a x y z')
9 sq1
= Le(0, x
) & Le(x
, 2) & Le(0, y
) & Le(y
, 2)
10 sq2
= Le(2, x
) & Le(x
, 4) & Le(2, y
) & Le(y
, 4)
11 sq3
= Le(0, x
) & Le(x
, 3) & Le(0, y
) & Le(y
, 3)
12 sq4
= Le(1, x
) & Le(x
, 2) & Le(1, y
) & Le(y
, 2)
13 sq5
= Le(1, x
) & Le(x
, 2) & Le(1, y
)
14 sq6
= Le(1, x
) & Le(x
, 2) & Le(1, y
) & Le(y
, 3)
15 sq7
= Le(0, x
) & Le(x
, 2) & Le(0, y
) & Eq(z
, 2) & Le(a
, 3)
16 p
= Le(2*x
+1, y
) & Le(-2*x
-1, y
) & Le(y
, 1)
18 universe
= Polyhedron([])
22 print('sq1 =', sq1
) #print correct square
23 print('sq2 =', sq2
) #print correct square
24 print('sq3 =', sq3
) #print correct square
25 print('sq4 =', sq4
) #print correct square
26 print('universe =', universe
) #print correct square
28 print('¬sq1 =', ~sq1
) #test complement
30 print('sq1 + sq1 =', sq1
+ sq2
) #test addition
31 print('sq1 + sq2 =', Polyhedron(sq1
+ sq2
)) #test addition
33 print('universe + universe =', universe
+ universe
)#test addition
34 print('universe - universe =', universe
- universe
) #test subtraction
36 print('sq2 - sq1 =', sq2
- sq1
) #test subtraction
37 print('sq2 - sq1 =', Polyhedron(sq2
- sq1
)) #test subtraction
38 print('sq1 - sq1 =', Polyhedron(sq1
- sq1
)) #test subtraction
40 print('sq1 ∩ sq2 =', sq1
& sq2
) #test intersection
41 print('sq1 ∪ sq2 =', sq1 | sq2
) #test union
43 print('sq1 ⊔ sq2 =', Polyhedron(sq1 | sq2
)) # test convex union
45 print('check if sq1 and sq2 disjoint:', sq1
.isdisjoint(sq2
)) #should return false
47 print('sq1 disjoint:', sq1
.disjoint()) #make disjoint
48 print('sq2 disjoint:', sq2
.disjoint()) #make disjoint
50 print('is square 1 universe?:', sq1
.isuniverse()) #test if square is universe
51 print('is u universe?:', universe
.isuniverse()) #test if square is universe
53 print('is sq1 a subset of sq2?:', sq1
.issubset(sq2
)) #test issubset()
54 print('is sq4 less than sq3?:', sq4
.__lt__(sq3
)) # test lt(), must be a strict subset
56 print('lexographic min of sq1:', sq1
.lexmin()) #test lexmin()
57 print('lexographic max of sq1:', sq1
.lexmax()) #test lexmin()
59 print('lexographic min of sq2:', sq2
.lexmin()) #test lexmax()
60 print('lexographic max of sq2:', sq2
.lexmax()) #test lexmax()
62 print('Polyhedral hull of sq1 + sq2 is:', q
.aspolyhedron()) #test polyhedral hull
64 print('is sq1 bounded?', sq1
.isbounded()) #bounded should return True
65 print('is sq5 bounded?', sq5
.isbounded()) #unbounded should return False
68 print('sample Polyhedron from sq6:', sq6
.sample())
70 print('sq7 with out constraints involving y and a', sq7
.project([a
, z
, x
, y
]))
72 print('the verticies for s are:', p
.vertices())
75 # plotting the intersection of two squares
76 square1
= Le(0, x
) & Le(x
, 2) & Le(0, y
) & Le(y
, 2)
77 square2
= Le(1, x
) & Le(x
, 3) & Le(1, y
) & Le(y
, 3)
80 plot
= fig
.add_subplot(1, 1, 1, aspect
='equal')
81 square1
.plot(plot
, facecolor
='red', alpha
=0.3)
82 square2
.plot(plot
, facecolor
='blue', alpha
=0.3)
84 squares
= Polyhedron(square1
+ square2
)
85 squares
.plot(plot
, facecolor
='blue', alpha
=0.3)