Fix docstrings of And(), Or(), Not()
[linpy.git] / linpy / polyhedra.py
index b486be1..b88cfd1 100644 (file)
@@ -44,7 +44,6 @@ class Polyhedron(Domain):
     __slots__ = (
         '_equalities',
         '_inequalities',
     __slots__ = (
         '_equalities',
         '_inequalities',
-        '_constraints',
         '_symbols',
         '_dimension',
     )
         '_symbols',
         '_dimension',
     )
@@ -88,22 +87,20 @@ class Polyhedron(Domain):
             if inequalities is not None:
                 raise TypeError('too many arguments')
             return equalities.aspolyhedron()
             if inequalities is not None:
                 raise TypeError('too many arguments')
             return equalities.aspolyhedron()
-        if equalities is None:
-            equalities = []
-        else:
-            for i, equality in enumerate(equalities):
+        sc_equalities = []
+        if equalities is not None:
+            for equality in equalities:
                 if not isinstance(equality, LinExpr):
                     raise TypeError('equalities must be linear expressions')
                 if not isinstance(equality, LinExpr):
                     raise TypeError('equalities must be linear expressions')
-                equalities[i] = equality.scaleint()
-        if inequalities is None:
-            inequalities = []
-        else:
-            for i, inequality in enumerate(inequalities):
+                sc_equalities.append(equality.scaleint())
+        sc_inequalities = []
+        if inequalities is not None:
+            for inequality in inequalities:
                 if not isinstance(inequality, LinExpr):
                     raise TypeError('inequalities must be linear expressions')
                 if not isinstance(inequality, LinExpr):
                     raise TypeError('inequalities must be linear expressions')
-                inequalities[i] = inequality.scaleint()
-        symbols = cls._xsymbols(equalities + inequalities)
-        islbset = cls._toislbasicset(equalities, inequalities, symbols)
+                sc_inequalities.append(inequality.scaleint())
+        symbols = cls._xsymbols(sc_equalities + sc_inequalities)
+        islbset = cls._toislbasicset(sc_equalities, sc_inequalities, symbols)
         return cls._fromislbasicset(islbset, symbols)
 
     @property
         return cls._fromislbasicset(islbset, symbols)
 
     @property
@@ -128,7 +125,7 @@ class Polyhedron(Domain):
         The tuple of constraints, i.e., equalities and inequalities. This is
         semantically equivalent to: equalities + inequalities.
         """
         The tuple of constraints, i.e., equalities and inequalities. This is
         semantically equivalent to: equalities + inequalities.
         """
-        return self._constraints
+        return self._equalities + self._inequalities
 
     @property
     def polyhedra(self):
 
     @property
     def polyhedra(self):
@@ -147,6 +144,15 @@ class Polyhedron(Domain):
     def aspolyhedron(self):
         return self
 
     def aspolyhedron(self):
         return self
 
+    def convex_union(self, *others):
+        """
+        Return the convex union of two or more polyhedra.
+        """
+        for other in others:
+            if not isinstance(other, Polyhedron):
+                raise TypeError('arguments must be Polyhedron instances')
+        return Polyhedron(self.union(*others))
+
     def __contains__(self, point):
         if not isinstance(point, Point):
             raise TypeError('point must be a Point instance')
     def __contains__(self, point):
         if not isinstance(point, Point):
             raise TypeError('point must be a Point instance')
@@ -167,7 +173,11 @@ class Polyhedron(Domain):
             for inequality in self.inequalities]
         return Polyhedron(equalities, inequalities)
 
             for inequality in self.inequalities]
         return Polyhedron(equalities, inequalities)
 
-    def _asinequalities(self):
+    def asinequalities(self):
+        """
+        Express the polyhedron using inequalities, given as a list of
+        expressions greater or equal to 0.
+        """
         inequalities = list(self.equalities)
         inequalities.extend([-expression for expression in self.equalities])
         inequalities.extend(self.inequalities)
         inequalities = list(self.equalities)
         inequalities.extend([-expression for expression in self.equalities])
         inequalities.extend(self.inequalities)
@@ -176,11 +186,14 @@ class Polyhedron(Domain):
     def widen(self, other):
         """
         Compute the standard widening of two polyhedra, à la Halbwachs.
     def widen(self, other):
         """
         Compute the standard widening of two polyhedra, à la Halbwachs.
+
+        In its current implementation, this method is slow and should not be
+        used on large polyhedra.
         """
         if not isinstance(other, Polyhedron):
         """
         if not isinstance(other, Polyhedron):
-            raise ValueError('argument must be a Polyhedron instance')
-        inequalities1 = self._asinequalities()
-        inequalities2 = other._asinequalities()
+            raise TypeError('argument must be a Polyhedron instance')
+        inequalities1 = self.asinequalities()
+        inequalities2 = other.asinequalities()
         inequalities = []
         for inequality1 in inequalities1:
             if other <= Polyhedron(inequalities=[inequality1]):
         inequalities = []
         for inequality1 in inequalities1:
             if other <= Polyhedron(inequalities=[inequality1]):
@@ -219,8 +232,7 @@ class Polyhedron(Domain):
         self = object().__new__(Polyhedron)
         self._equalities = tuple(equalities)
         self._inequalities = tuple(inequalities)
         self = object().__new__(Polyhedron)
         self._equalities = tuple(equalities)
         self._inequalities = tuple(inequalities)
-        self._constraints = tuple(equalities + inequalities)
-        self._symbols = cls._xsymbols(self._constraints)
+        self._symbols = cls._xsymbols(self.constraints)
         self._dimension = len(self._symbols)
         return self
 
         self._dimension = len(self._symbols)
         return self
 
@@ -307,13 +319,10 @@ class EmptyType(Polyhedron):
     The empty polyhedron, whose set of constraints is not satisfiable.
     """
 
     The empty polyhedron, whose set of constraints is not satisfiable.
     """
 
-    __slots__ = Polyhedron.__slots__
-
     def __new__(cls):
         self = object().__new__(cls)
         self._equalities = (Rational(1),)
         self._inequalities = ()
     def __new__(cls):
         self = object().__new__(cls)
         self._equalities = (Rational(1),)
         self._inequalities = ()
-        self._constraints = self._equalities
         self._symbols = ()
         self._dimension = 0
         return self
         self._symbols = ()
         self._dimension = 0
         return self
@@ -338,13 +347,10 @@ class UniverseType(Polyhedron):
     i.e. is empty.
     """
 
     i.e. is empty.
     """
 
-    __slots__ = Polyhedron.__slots__
-
     def __new__(cls):
         self = object().__new__(cls)
         self._equalities = ()
         self._inequalities = ()
     def __new__(cls):
         self = object().__new__(cls)
         self._equalities = ()
         self._inequalities = ()
-        self._constraints = ()
         self._symbols = ()
         self._dimension = ()
         return self
         self._symbols = ()
         self._dimension = ()
         return self
@@ -406,15 +412,15 @@ def Ne(left, right):
     return ~Eq(left, right)
 
 @_polymorphic
     return ~Eq(left, right)
 
 @_polymorphic
-def Gt(left, right):
+def Ge(left, right):
     """
     """
-    Create the polyhedron with constraints expr1 > expr2 > expr3 ...
+    Create the polyhedron with constraints expr1 >= expr2 >= expr3 ...
     """
     """
-    return Polyhedron([], [left - right - 1])
+    return Polyhedron([], [left - right])
 
 @_polymorphic
 
 @_polymorphic
-def Ge(left, right):
+def Gt(left, right):
     """
     """
-    Create the polyhedron with constraints expr1 >= expr2 >= expr3 ...
+    Create the polyhedron with constraints expr1 > expr2 > expr3 ...
     """
     """
-    return Polyhedron([], [left - right])
+    return Polyhedron([], [left - right - 1])