From: Vivien Maisonneuve Date: Wed, 20 Aug 2014 09:31:24 +0000 (+0200) Subject: Reformat examples X-Git-Tag: 1.0~14 X-Git-Url: https://scm.cri.mines-paristech.fr/git/linpy.git/commitdiff_plain/a8257bb17a1098f69625387a467170cac4b9f483 Reformat examples --- diff --git a/examples/bac2014.py b/examples/bac2014.py index 775be66..cc02126 100755 --- a/examples/bac2014.py +++ b/examples/bac2014.py @@ -1,9 +1,15 @@ #!/usr/bin/env python3 +# This example is inspired from a math question in the French baccalauréat 2014, +# consisting in computing the intersection of a plane with a line. + from linpy import * x, y, z = symbols('x y z') -DF = Eq(x, y) & Eq(z, 6 - 2*x) -P = Eq(x + y - 2*z, 0) +plane = Eq(x, y) & Eq(z, 6 - 2*x) +line = Eq(x + y - 2*z, 0) -print('DF ∩ P =', DF & P) +if __name__ == '__main__': + print('plane: ', plane) + print('line: ', line) + print('intersection:', plane & line) diff --git a/examples/diamonds.py b/examples/diamonds.py deleted file mode 100755 index 0978d4c..0000000 --- a/examples/diamonds.py +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/env python3 - -import matplotlib.pyplot as plt - -from matplotlib import pylab -from mpl_toolkits.mplot3d import Axes3D - -from linpy import * - -x, y, z = symbols('x y z') - -fig = plt.figure(facecolor='white') - -diam_plot = fig.add_subplot(2, 2, 1, aspect='equal') -diam_plot.set_title('Diamond') -diam = Ge(y, x - 1) & Le(y, x + 1) & Ge(y, -x - 1) & Le(y, -x + 1) -diam.plot(diam_plot, fill=True, edgecolor='red', facecolor='yellow') - -cham_plot = fig.add_subplot(2, 2, 2, projection='3d', aspect='equal') -cham_plot.set_title('Chamfered cube') -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) -cham.plot(cham_plot, facecolors=(1, 0, 0, 0.75)) - -rhom_plot = fig.add_subplot(2, 2, 3, projection='3d', aspect='equal') -rhom_plot.set_title('Rhombicuboctahedron') -rhom = cham & \ - Le(x + y + z, 7) & Ge(-2, -x - y - z) & \ - Le(-1, x + y - z) & Le(x + y - z, 4) & \ - Le(-1, x - y + z) & Le(x - y + z, 4) & \ - Le(-1, -x + y + z) & Le(-x + y + z, 4) -rhom.plot(rhom_plot, facecolors=(0, 1, 0, 0.75)) - -cubo_plot = fig.add_subplot(2, 2, 4, projection='3d', aspect='equal') -cubo_plot.set_title('Truncated cuboctahedron') -cubo = Le(0, x) & Le(x, 5) & Le(0, y) & Le(y, 5) & Le(0, z) & Le(z, 5) & \ - Le(x -4, y) & Le(y, x + 4) & Le(-x + 1, y) & Le(y, -x + 9) & \ - Le(y -4, z) & Le(z, y + 4) & Le(-y + 1, z) & Le(z, -y + 9) & \ - Le(z -4, x) & Le(x, z + 4) & Le(-z + 1, x) & Le(x, -z + 9) & \ - Le(3, x + y + z) & Le(x + y + z, 12) & \ - Le(-2, x - y + z) & Le(x - y + z, 7) & \ - Le(-2, -x + y + z) & Le(-x + y + z, 7) & \ - Le(-2, x + y - z) & Le(x + y - z, 7) -cubo.plot(cubo_plot, facecolors=(0, 0, 1, 0.75)) - -pylab.show() diff --git a/examples/menger.py b/examples/menger.py index d8a74d4..dcba976 100755 --- a/examples/menger.py +++ b/examples/menger.py @@ -1,5 +1,18 @@ #!/usr/bin/env python3 +# Plot a Menger sponge. +# +# The construction of a Menger sponge can be described as follows: +# +# 1. Begin with a cube. +# 2. Divide every face of the cube into 9 squares, like a Rubik's Cube. This +# will sub-divide the cube into 27 smaller cubes. +# 3. Remove the smaller cube in the middle of each face, and remove the smaller +# cube in the very center of the larger cube, leaving 20 smaller cubes. This +# is a level-1 Menger sponge (resembling a Void Cube). +# 4. Repeat steps 2 and 3 for each of the remaining smaller cubes, and continue +# to iterate. + import argparse import matplotlib.pyplot as plt @@ -11,6 +24,7 @@ from mpl_toolkits.mplot3d import Axes3D from linpy import * + x, y, z = symbols('x y z') _x, _y, _z = x.asdummy(), y.asdummy(), z.asdummy() @@ -56,8 +70,8 @@ def menger(domain, count=1, cut=False): if __name__ == '__main__': parser = argparse.ArgumentParser( description='Compute a Menger sponge.') - parser.add_argument('-n', '--iterations', type=int, default=1, - help='number of iterations (default: 1)') + parser.add_argument('-n', '--iterations', type=int, default=2, + help='number of iterations (default: 2)') parser.add_argument('-c', '--cut', action='store_true', default=False, help='cut the sponge') args = parser.parse_args() diff --git a/examples/nsad2010.py b/examples/nsad2010.py index 9359315..4b73eef 100755 --- a/examples/nsad2010.py +++ b/examples/nsad2010.py @@ -1,5 +1,13 @@ #!/usr/bin/env python3 +# This is an implementation of the algorithm described in +# +# [ACI10] C. Ancourt, F. Coelho and F. Irigoin, A modular static analysis +# approach to affine loop invariants detection (2010), pp. 3 - 16, NSAD 2010. +# +# to compute the transitive closure of an affine transformer. A refined version +# of this algorithm is implemented in PIPS. + from linpy import * @@ -39,8 +47,8 @@ class Transformer: if __name__ == '__main__': - i, iprime, j, jprime = symbols("i i' j j'") - transformer = Transformer(Eq(iprime, i + 2) & Eq(jprime, j + 1), - [i, j], [iprime, jprime]) + i0, i, j0, j = symbols('i0 i j0 j') + transformer = Transformer(Eq(i, i0 + 2) & Eq(j, j0 + 1), + [i0, j0], [i, j]) print('T =', transformer.polyhedron) print('T* =', transformer.star().polyhedron) diff --git a/examples/plots.py b/examples/plots.py new file mode 100755 index 0000000..d172835 --- /dev/null +++ b/examples/plots.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 + +# This program plots several 2D and 3D polyhedra on the same figure, +# illustrating some of the possible plot options. + +import matplotlib.pyplot as plt + +from matplotlib import pylab +from mpl_toolkits.mplot3d import Axes3D + +from linpy import * + + +x, y, z = symbols('x y z') + +diam = Ge(y, x - 1) & Le(y, x + 1) & Ge(y, -x - 1) & Le(y, -x + 1) + +cham = Le(0, x, 3) & Le(0, y, 3) & Le(0, z, 3) & \ + Le(z - 2, x, z + 2) & Le(1 - z, x, 5 - z) & \ + Le(z - 2, y, z + 2) & Le(1 - z, y, 5 - z) & \ + Le(y - 2, x, y + 2) & Le(1 - y, x, 5 - y) + +rhom = cham & \ + Le(x + y + z, 7) & Ge(-2, -x - y - z) & \ + Le(-1, x + y - z, 4) & Le(-1, x - y + z, 4) & Le(-1, -x + y + z, 4) + +cubo = Le(0, x, 5) & Le(0, y, 5) & Le(0, z, 5) & \ + Le(x -4, y, x + 4) & Le(-x + 1, y, -x + 9) & \ + Le(y -4, z, y + 4) & Le(-y + 1, z, -y + 9) & \ + Le(z -4, x, z + 4) & Le(-z + 1, x, -z + 9) & \ + Le(3, x + y + z, 12) & Le(-2, x - y + z, 7) & \ + Le(-2, -x + y + z, 7) & Le(-2, x + y - z, 7) + + +if __name__ == '__main__': + + fig = plt.figure(facecolor='white') + + diam_plot = fig.add_subplot(2, 2, 1, aspect='equal') + diam_plot.set_title('Diamond') + diam.plot(diam_plot, fill=True, edgecolor='red', facecolor='yellow') + + cham_plot = fig.add_subplot(2, 2, 2, projection='3d', aspect='equal') + cham_plot.set_title('Chamfered cube') + cham.plot(cham_plot, facecolors=(1, 0, 0, 0.75)) + + rhom_plot = fig.add_subplot(2, 2, 3, projection='3d', aspect='equal') + rhom_plot.set_title('Rhombicuboctahedron') + rhom.plot(rhom_plot, facecolors=(0, 1, 0, 0.75)) + + cubo_plot = fig.add_subplot(2, 2, 4, projection='3d', aspect='equal') + cubo_plot.set_title('Truncated cuboctahedron') + cubo.plot(cubo_plot, facecolors=(0, 0, 1, 0.75)) + + pylab.show() diff --git a/examples/squares.py b/examples/squares.py index 98e2ca8..15aed16 100755 --- a/examples/squares.py +++ b/examples/squares.py @@ -1,87 +1,56 @@ #!/usr/bin/env python3 -from linpy import * -import matplotlib.pyplot as plt -from matplotlib import pylab - -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) & Le(y, 3) -sq7 = Le(0, x) & Le(x, 2) & Le(0, y) & Eq(z, 2) & Le(a, 3) -p = Le(2*x+1, y) & Le(-2*x-1, y) & Le(y, 1) - -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()) #bounded should return True -print('is sq5 bounded?', sq5.isbounded()) #unbounded should return False -print() -print('sq6:', sq6) -print('sample Polyhedron from sq6:', sq6.sample()) -print() -print('sq7 with out constraints involving y and a', sq7.project([a, z, x, y])) -print() -print('the verticies for s are:', p.vertices()) - - -# plotting the intersection of two squares -square1 = Le(0, x) & Le(x, 2) & Le(0, y) & Le(y, 2) -square2 = Le(1, x) & Le(x, 3) & Le(1, y) & Le(y, 3) - -fig = plt.figure() -plot = fig.add_subplot(1, 1, 1, aspect='equal') -square1.plot(plot, facecolor='red', alpha=0.3) -square2.plot(plot, facecolor='blue', alpha=0.3) - -squares = Polyhedron(square1 + square2) -squares.plot(plot, facecolor='blue', alpha=0.3) - -pylab.show() +# 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)') + shell.push('inter') + shell.push() + + shell.push('hull = square1.convex_union(square2)') + shell.push('hull') + shell.push() + + shell.push('square1.project([y])') + shell.push() + + shell.push('inter <= square1') + shell.push('inter == Empty') + shell.push() + + shell.push('union = square1 | square2') + shell.push('union') + shell.push('union <= hull') + shell.push() + + shell.push('diff = square1 - square2') + shell.push('diff') + shell.push('~square1') diff --git a/examples/tesseract.py b/examples/tesseract.py index 0a57188..383d7bd 100755 --- a/examples/tesseract.py +++ b/examples/tesseract.py @@ -1,23 +1,25 @@ #!/usr/bin/env python3 +# In geometry, the tesseract is the four-dimensional analog of the cube; the +# tesseract is to the cube as the cube is to the square. Just as the surface of +# the cube consists of 6 square faces, the hypersurface of the tesseract +# consists of 8 cubical cells. + from linpy import * + x, y, z, t = symbols('x y z t') -tesseract = \ - Le(0, x) & Le(x, 1) & \ - Le(0, y) & Le(y, 1) & \ - Le(0, z) & Le(z, 1) & \ - Le(0, t) & Le(t, 1) +tesseract = Le(0, x, 1) & Le(0, y, 1) & Le(0, z, 1) & Le(0, t, 1) def faces(polyhedron): for points in polyhedron.faces(): face = points[0].aspolyhedron() - face = face.union(*[point.aspolyhedron() for point in points[1:]]) - face = face.aspolyhedron() + face = face.convex_union(*[point.aspolyhedron() for point in points[1:]]) yield face -print('Faces of tesseract\n\n {}\n\nare:\n'.format(tesseract)) -for face in faces(tesseract): - assert(len(face.vertices()) == 8) - print(' {}'.format(face)) +if __name__ == '__main__': + print('Faces of tesseract\n\n {}\n\nare:\n'.format(tesseract)) + for face in faces(tesseract): + assert(len(face.vertices()) == 8) + print(' {}'.format(face))