+
+ def __contains__(self, point):
+ if not isinstance(point, Point):
+ raise TypeError('point must be a Point instance')
+ if self.symbols != point.symbols:
+ raise ValueError('arguments must belong to the same space')
+ for equality in self.equalities:
+ if equality.subs(point.coordinates()) != 0:
+ return False
+ for inequality in self.inequalities:
+ if inequality.subs(point.coordinates()) < 0:
+ return False
+ return True
+
+ def subs(self, symbol, expression=None):
+ equalities = [equality.subs(symbol, expression)
+ for equality in self.equalities]
+ inequalities = [inequality.subs(symbol, expression)
+ for inequality in self.inequalities]
+ return Polyhedron(equalities, inequalities)
+
+ def _asinequalities(self):
+ inequalities = list(self.equalities)
+ inequalities.extend([-expression for expression in self.equalities])
+ inequalities.extend(self.inequalities)
+ return inequalities
+
+ def widen(self, other):
+ if not isinstance(other, Polyhedron):
+ raise ValueError('argument must be a Polyhedron instance')
+ inequalities1 = self._asinequalities()
+ inequalities2 = other._asinequalities()
+ inequalities = []
+ for inequality1 in inequalities1:
+ if other <= Polyhedron(inequalities=[inequality1]):
+ inequalities.append(inequality1)
+ for inequality2 in inequalities2:
+ for i in range(len(inequalities1)):
+ inequalities3 = inequalities1[:i] + inequalities[i + 1:]
+ inequalities3.append(inequality2)
+ polyhedron3 = Polyhedron(inequalities=inequalities3)
+ if self == polyhedron3:
+ inequalities.append(inequality2)
+ break
+ return Polyhedron(inequalities=inequalities)
+