From: Vivien Maisonneuve Date: Fri, 11 Jul 2014 14:18:39 +0000 (+0200) Subject: Implement methods Polyhedron.__contains__(), Domain.__contains__() X-Git-Tag: 1.0~127 X-Git-Url: https://scm.cri.mines-paristech.fr/git/linpy.git/commitdiff_plain/663316ddc03c19cf06e95bad67fd5ac2bb5e1dfc?hp=b3c0d23e54fe4362097a2b77c34f7bc5b492e2a0 Implement methods Polyhedron.__contains__(), Domain.__contains__() --- diff --git a/pypol/domains.py b/pypol/domains.py index 3ab7d44..255c995 100644 --- a/pypol/domains.py +++ b/pypol/domains.py @@ -312,6 +312,12 @@ class Domain: points.append(Point(coordinates)) return points + def __contains__(self, point): + for polyhedron in self.polyhedra: + if point in polyhedron: + return True + return False + def subs(self, symbol, expression=None): polyhedra = [polyhedron.subs(symbol, expression) for polyhedron in self.polyhedra] diff --git a/pypol/polyhedra.py b/pypol/polyhedra.py index 3db8f40..a08213d 100644 --- a/pypol/polyhedra.py +++ b/pypol/polyhedra.py @@ -6,6 +6,7 @@ import numbers from . import islhelper from .islhelper import mainctx, libisl +from .coordinates import Point from .linexprs import Expression, Symbol, Rational from .domains import Domain @@ -87,6 +88,19 @@ class Polyhedron(Domain): def aspolyhedron(self): return self + 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]