Fix Polyhedron.isempty()
[linpy.git] / pypol / linear.py
index 4f3aeef..5f3c559 100644 (file)
@@ -139,7 +139,7 @@ class Expression:
     @_polymorphic_method
     def __add__(self, other):
         coefficients = dict(self.coefficients())
     @_polymorphic_method
     def __add__(self, other):
         coefficients = dict(self.coefficients())
-        for symbol, coefficient in other.coefficients:
+        for symbol, coefficient in other.coefficients():
             if symbol in coefficients:
                 coefficients[symbol] += coefficient
             else:
             if symbol in coefficients:
                 coefficients[symbol] += coefficient
             else:
@@ -152,7 +152,7 @@ class Expression:
     @_polymorphic_method
     def __sub__(self, other):
         coefficients = dict(self.coefficients())
     @_polymorphic_method
     def __sub__(self, other):
         coefficients = dict(self.coefficients())
-        for symbol, coefficient in other.coefficients:
+        for symbol, coefficient in other.coefficients():
             if symbol in coefficients:
                 coefficients[symbol] -= coefficient
             else:
             if symbol in coefficients:
                 coefficients[symbol] -= coefficient
             else:
@@ -270,30 +270,30 @@ class Expression:
     def __hash__(self):
         return hash((self._coefficients, self._constant))
 
     def __hash__(self):
         return hash((self._coefficients, self._constant))
 
-    def _canonify(self):
+    def _toint(self):
         lcm = functools.reduce(lambda a, b: a*b // gcd(a, b),
                 [value.denominator for value in self.values()])
         return self * lcm
 
     @_polymorphic_method
     def _eq(self, other):
         lcm = functools.reduce(lambda a, b: a*b // gcd(a, b),
                 [value.denominator for value in self.values()])
         return self * lcm
 
     @_polymorphic_method
     def _eq(self, other):
-        return Polyhedron(equalities=[(self - other)._canonify()])
+        return Polyhedron(equalities=[(self - other)._toint()])
 
     @_polymorphic_method
     def __le__(self, other):
 
     @_polymorphic_method
     def __le__(self, other):
-        return Polyhedron(inequalities=[(other - self)._canonify()])
+        return Polyhedron(inequalities=[(other - self)._toint()])
 
     @_polymorphic_method
     def __lt__(self, other):
 
     @_polymorphic_method
     def __lt__(self, other):
-        return Polyhedron(inequalities=[(other - self)._canonify() - 1])
+        return Polyhedron(inequalities=[(other - self)._toint() - 1])
 
     @_polymorphic_method
     def __ge__(self, other):
 
     @_polymorphic_method
     def __ge__(self, other):
-        return Polyhedron(inequalities=[(self - other)._canonify()])
+        return Polyhedron(inequalities=[(self - other)._toint()])
 
     @_polymorphic_method
     def __gt__(self, other):
 
     @_polymorphic_method
     def __gt__(self, other):
-        return Polyhedron(inequalities=[(self - other)._canonify() - 1])
+        return Polyhedron(inequalities=[(self - other)._toint() - 1])
 
 
 def constant(numerator=0, denominator=None):
 
 
 def constant(numerator=0, denominator=None):
@@ -378,16 +378,6 @@ class Polyhedron:
     def inequalities(self):
         return self._inequalities
 
     def inequalities(self):
         return self._inequalities
 
-    @property
-    def constant(self):
-        return self._constant
-
-    def isconstant(self):
-        return len(self._coefficients) == 0
-
-    def isempty(self):
-        return bool(libisl.isl_basic_set_is_empty(self._bset))
-
     @property
     def constraints(self):
         return self._constraints
     @property
     def constraints(self):
         return self._constraints
@@ -411,10 +401,11 @@ class Polyhedron:
         raise NotImplementedError
 
     def isempty(self):
         raise NotImplementedError
 
     def isempty(self):
-        return self == empty
+        bset = self._to_isl()
+        return bool(libisl.isl_basic_set_is_empty(bset))
 
     def isuniverse(self):
 
     def isuniverse(self):
-        return self == universe
+        raise NotImplementedError
 
     def isdisjoint(self, other):
         # return true if the polyhedron has no elements in common with other
 
     def isdisjoint(self, other):
         # return true if the polyhedron has no elements in common with other
@@ -530,7 +521,7 @@ class Polyhedron:
         return bset
 
     @classmethod
         return bset
 
     @classmethod
-    def from_isl(cls, bset):
+    def _from_isl(cls, bset):
         '''takes basic set in isl form and puts back into python version of polyhedron
         isl example code gives isl form as:
             "{[i] : exists (a : i = 2a and i >= 10 and i <= 42)}")
         '''takes basic set in isl form and puts back into python version of polyhedron
         isl example code gives isl form as:
             "{[i] : exists (a : i = 2a and i >= 10 and i <= 42)}")
@@ -558,3 +549,5 @@ if __name__ == '__main__':
     p = Polyhedron(inequalities=[ex1, ex2])
     bs = p._to_isl()
     print(bs)
     p = Polyhedron(inequalities=[ex1, ex2])
     bs = p._to_isl()
     print(bs)
+    print('empty ?', p.isempty())
+    print('empty ?', eq(0, 1).isempty())