X-Git-Url: https://scm.cri.mines-paristech.fr/git/linpy.git/blobdiff_plain/cd2197879049a836b02a331adf0a00c0b87fe043..2b13a146860ac116ce0388d8f7551044c09c55f7:/examples/squares.py?ds=inline diff --git a/examples/squares.py b/examples/squares.py index 4389d87..1a0cedb 100755 --- a/examples/squares.py +++ b/examples/squares.py @@ -1,71 +1,57 @@ #!/usr/bin/env python3 -from pypol import * - -a, x, y, z = symbols('a x y z') - -sq1 = Le(0, x) & Le(x, 2) & Le(0, y) & Le(y, 2) -sq2 = Le(2, x) & Le(x, 4) & Le(2, y) & Le(y, 4) -sq3 = Le(0, x) & Le(x, 3) & Le(0, y) & Le(y, 3) -sq4 = Le(1, x) & Le(x, 2) & Le(1, y) & Le(y, 2) -sq5 = Le(1, x) & Le(x, 2) & Le(1, y) -sq6 = Le(1, x) & Le(x, 2) & Le(1, y) & Eq(y, 3) -sq7 = Le(0, x) & Le(x, 2) & Le(0, y) & Eq(z, 2) & Le(a, 3) -universe = Polyhedron([]) -q = sq1 - sq2 -e = Empty - -print('sq1 =', sq1) #print correct square -print('sq2 =', sq2) #print correct square -print('sq3 =', sq3) #print correct square -print('sq4 =', sq4) #print correct square -print('universe =', universe) #print correct square -print() -print('¬sq1 =', ~sq1) #test complement -print() -print('sq1 + sq1 =', sq1 + sq2) #test addition -print('sq1 + sq2 =', Polyhedron(sq1 + sq2)) #test addition -print() -print('universe + universe =', universe + universe)#test addition -print('universe - universe =', universe - universe) #test subtraction -print() -print('sq2 - sq1 =', sq2 - sq1) #test subtraction -print('sq2 - sq1 =', Polyhedron(sq2 - sq1)) #test subtraction -print('sq1 - sq1 =', Polyhedron(sq1 - sq1)) #test subtraction -print() -print('sq1 ∩ sq2 =', sq1 & sq2) #test intersection -print('sq1 ∪ sq2 =', sq1 | sq2) #test union -print() -print('sq1 ⊔ sq2 =', Polyhedron(sq1 | sq2)) # test convex union -print() -print('check if sq1 and sq2 disjoint:', sq1.isdisjoint(sq2)) #should return false -print() -print('sq1 disjoint:', sq1.disjoint()) #make disjoint -print('sq2 disjoint:', sq2.disjoint()) #make disjoint -print() -print('is square 1 universe?:', sq1.isuniverse()) #test if square is universe -print('is u universe?:', universe.isuniverse()) #test if square is universe -print() -print('is sq1 a subset of sq2?:', sq1.issubset(sq2)) #test issubset() -print('is sq4 less than sq3?:', sq4.__lt__(sq3)) # test lt(), must be a strict subset -print() -print('lexographic min of sq1:', sq1.lexmin()) #test lexmin() -print('lexographic max of sq1:', sq1.lexmax()) #test lexmin() -print() -print('lexographic min of sq2:', sq2.lexmin()) #test lexmax() -print('lexographic max of sq2:', sq2.lexmax()) #test lexmax() -print() -print('Polyhedral hull of sq1 + sq2 is:', q.aspolyhedron()) #test polyhedral hull -print() -print('is sq1 bounded?', sq1.isbounded()) #unbounded should return True -print('is sq5 bounded?', sq5.isbounded()) #unbounded should return False -print() -print('sq6:', sq6) -print('sq6 simplified:', sq6.sample()) -print() -print(universe.project([x])) -print('sq7 with out constraints involving y and a', sq7.project([a, z, x, y])) #drops dims that are passed -print() -print('sq1 has {} parameters'.format(sq1.num_parameters())) -print() -print('does sq1 constraints involve x?', sq1.involves_dims([x])) +# This is the code example used in the tutorial. It shows how to define and +# manipulate polyhedra. + +import code + + +class InteractiveConsole(code.InteractiveConsole): + def push(self, line=''): + if line: + print('>>>', line) + return super().push(line) + else: + print() + + +if __name__ == '__main__': + + shell = InteractiveConsole() + + shell.push('from linpy import *') + shell.push("x, y = symbols('x y')") + shell.push() + + shell.push('square1 = Le(0, x, 2) & Le(0, y, 2)') + shell.push('square1') + shell.push() + + shell.push("square2 = Polyhedron('1 <= x <= 3, 1 <= y <= 3')") + shell.push('square2') + shell.push() + + shell.push('inter = square1.intersection(square2) # or square1 & square2') + shell.push('inter') + shell.push() + + shell.push('hull = square1.convex_union(square2)') + shell.push('hull') + shell.push() + + shell.push('proj = square1.project([y])') + shell.push('proj') + shell.push() + + shell.push('inter <= square1') + shell.push('inter == Empty') + shell.push() + + shell.push('union = square1.union(square2) # or square1 | square2') + shell.push('union') + shell.push('union <= hull') + shell.push() + + shell.push('diff = square1.difference(square2) # or square1 - square2') + shell.push('diff') + shell.push('~square1')