Add EmptyType and UniverseType
[linpy.git] / pypol / polyhedra.py
index a5d9495..f8d413e 100644 (file)
@@ -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([])