From 77d9e641db24dc1edb6d0e2b91b58d395761616f Mon Sep 17 00:00:00 2001 From: Vivien Maisonneuve Date: Thu, 17 Jul 2014 11:40:22 +0200 Subject: [PATCH 1/1] Add EmptyType and UniverseType --- examples/diamonds.py | 1 + pypol/polyhedra.py | 92 ++++++++++++++++++++++++++++++-------------- 2 files changed, 64 insertions(+), 29 deletions(-) diff --git a/examples/diamonds.py b/examples/diamonds.py index 572a870..d5119b4 100755 --- a/examples/diamonds.py +++ b/examples/diamonds.py @@ -44,4 +44,5 @@ cubo = Le(0, x) & Le(x, 5) & Le(0, y) & Le(y, 5) & Le(0, z) & Le(z, 5) & \ 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/pypol/polyhedra.py b/pypol/polyhedra.py index a5d9495..f8d413e 100644 --- a/pypol/polyhedra.py +++ b/pypol/polyhedra.py @@ -114,6 +114,10 @@ class Polyhedron(Domain): @classmethod def _fromislbasicset(cls, islbset, symbols): + if libisl.isl_basic_set_is_empty(islbset): + return Empty + if libisl.isl_basic_set_is_universe(islbset): + return Universe islconstraints = islhelper.isl_basic_set_constraints(islbset) equalities = [] inequalities = [] @@ -184,33 +188,23 @@ class Polyhedron(Domain): return domain def __repr__(self): - if self.isempty(): - return 'Empty' - elif self.isuniverse(): - return 'Universe' + strings = [] + for equality in self.equalities: + strings.append('Eq({}, 0)'.format(equality)) + for inequality in self.inequalities: + strings.append('Ge({}, 0)'.format(inequality)) + if len(strings) == 1: + return strings[0] else: - strings = [] - for equality in self.equalities: - strings.append('Eq({}, 0)'.format(equality)) - for inequality in self.inequalities: - strings.append('Ge({}, 0)'.format(inequality)) - if len(strings) == 1: - return strings[0] - else: - return 'And({})'.format(', '.join(strings)) + return 'And({})'.format(', '.join(strings)) def _repr_latex_(self): - if self.isempty(): - return '$\\emptyset$' - elif self.isuniverse(): - return '$\\Omega$' - else: - strings = [] - for equality in self.equalities: - strings.append('{} = 0'.format(equality._repr_latex_().strip('$'))) - for inequality in self.inequalities: - strings.append('{} \\ge 0'.format(inequality._repr_latex_().strip('$'))) - return '${}$'.format(' \\wedge '.join(strings)) + strings = [] + for equality in self.equalities: + strings.append('{} = 0'.format(equality._repr_latex_().strip('$'))) + for inequality in self.inequalities: + strings.append('{} \\ge 0'.format(inequality._repr_latex_().strip('$'))) + return '$${}$$'.format(' \\wedge '.join(strings)) @classmethod def fromsympy(cls, expr): @@ -228,6 +222,51 @@ class Polyhedron(Domain): constraints.append(sympy.Ge(inequality.tosympy(), 0)) return sympy.And(*constraints) + +class EmptyType(Polyhedron): + + __slots__ = Polyhedron.__slots__ + + def __new__(cls): + self = object().__new__(cls) + self._equalities = (Rational(1),) + self._inequalities = () + self._constraints = self._equalities + self._symbols = () + self._dimension = 0 + return self + + def __repr__(self): + return 'Empty' + + def _repr_latex_(self): + return '$$\\emptyset$$' + +Empty = EmptyType() + + +class UniverseType(Polyhedron): + + __slots__ = Polyhedron.__slots__ + + def __new__(cls): + self = object().__new__(cls) + self._equalities = () + self._inequalities = () + self._constraints = () + self._symbols = () + self._dimension = () + return self + + def __repr__(self): + return 'Universe' + + def _repr_latex_(self): + return '$$\\Omega$$' + +Universe = UniverseType() + + def _polymorphic(func): @functools.wraps(func) def wrapper(left, right): @@ -287,8 +326,3 @@ def Ge(left, right): Return true if the first set is greater than or equal the second set. """ return Polyhedron([], [left - right]) - - -Empty = Eq(1, 0) - -Universe = Polyhedron([]) -- 2.20.1