From: Danielle Bolan Date: Tue, 8 Jul 2014 16:05:53 +0000 (+0200) Subject: Add plot method X-Git-Tag: 1.0~135 X-Git-Url: https://scm.cri.ensmp.fr/git/linpy.git/commitdiff_plain/ce985e833f88aed8957c73434198e6bac6bd234e Add plot method --- diff --git a/examples/squares.py b/examples/squares.py index 4389d87..e00821e 100755 --- a/examples/squares.py +++ b/examples/squares.py @@ -9,8 +9,11 @@ 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) +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 @@ -69,3 +72,7 @@ print() print('sq1 has {} parameters'.format(sq1.num_parameters())) print() print('does sq1 constraints involve x?', sq1.involves_dims([x])) +print() +print('the verticies for s are:', p.vertices()) +print() +print(p.plot()) diff --git a/pypol/domains.py b/pypol/domains.py index 44c38e7..375be85 100644 --- a/pypol/domains.py +++ b/pypol/domains.py @@ -268,24 +268,20 @@ class Domain: points = {} num = 0 vertices_points = [] - symbols = list(self.symbols) for vertex in vertices: - expr = libisl.isl_vertex_get_expr(vertex); #make vertices a bset if islhelper.isl_version < '0.13': + expr = libisl.isl_vertex_get_expr(vertex) constraints = islhelper.isl_basic_set_constraints(expr) #get bset constraints - for dim in symbols: - index = symbols.index(dim) - for c in constraints: #for each constraint - constant = libisl.isl_constraint_get_constant_val(c) #get contant value + for index, dim in enumerate(self.symbols): + for c in constraints: #for each constraint + constant = libisl.isl_constraint_get_constant_val(c) #get constant value constant = islhelper.isl_val_to_int(constant) coefficient = libisl.isl_constraint_get_coefficient_val(c,libisl.isl_dim_set, index) coefficient = islhelper.isl_val_to_int(coefficient) #get coefficient if coefficient != 0: - num = Fraction(constant, coefficient) - points.update({dim:num}) - vertices_points.append(points) - print(points) - + num = -Fraction(constant, coefficient) + points[dim]= float(num) + vertices_points.append(points.copy()) else: points = [] string = islhelper.isl_multi_aff_to_str(expr) diff --git a/pypol/polyhedra.py b/pypol/polyhedra.py index 63ecb64..db99753 100644 --- a/pypol/polyhedra.py +++ b/pypol/polyhedra.py @@ -1,10 +1,11 @@ + import functools import numbers from . import islhelper from .islhelper import mainctx, libisl -from .linexprs import Expression, Rational +from .linexprs import Expression, Symbol, Rational from .domains import Domain @@ -194,6 +195,45 @@ class Polyhedron(Domain): for inequality in self.inequalities: constraints.append(sympy.Ge(inequality.tosympy(), 0)) return sympy.And(*constraints) + + def plot(self): + import matplotlib.pyplot as plt + from matplotlib.path import Path + import matplotlib.patches as patches + + if len(self.symbols)> 3: + raise TypeError + + elif len(self.symbols) == 2: + verts = self.vertices() + points = [] + codes = [Path.MOVETO] + for vert in verts: + pairs = () + for sym in sorted(vert, key=Symbol.sortkey): + num = vert.get(sym) + pairs = pairs + (num,) + points.append(pairs) + points.append((0.0, 0.0)) + num = len(points) + while num > 2: + codes.append(Path.LINETO) + num = num - 1 + else: + codes.append(Path.CLOSEPOLY) + path = Path(points, codes) + fig = plt.figure() + ax = fig.add_subplot(111) + patch = patches.PathPatch(path, facecolor='blue', lw=2) + ax.add_patch(patch) + ax.set_xlim(-5,5) + ax.set_ylim(-5,5) + plt.show() + + elif len(self.symbols)==3: + return 0 + + return points def _polymorphic(func):