X-Git-Url: https://svn.cri.ensmp.fr/git/linpy.git/blobdiff_plain/1d494bb187b70135df721c13306d7f26fdf33f50..1c48ba9f3483505f53962731dc58c6c02d785fc4:/pypol/domains.py diff --git a/pypol/domains.py b/pypol/domains.py index fd588b7..20493fa 100644 --- a/pypol/domains.py +++ b/pypol/domains.py @@ -1,8 +1,11 @@ +import ast import functools +import re from . import islhelper from .islhelper import mainctx, libisl, isl_set_basic_sets +from .linexprs import Expression __all__ = [ @@ -81,6 +84,12 @@ class Domain: libisl.isl_set_free(islset) return universe + def isbounded(self): + islset = self._toislset(self.polyhedra, self.symbols) + bounded = bool(libisl.isl_set_is_bounded(islset)) + libisl.isl_set_free(islset) + return bounded + def __eq__(self, other): symbols = self._xsymbols([self, other]) islset1 = self._toislset(self.polyhedra, symbols) @@ -236,9 +245,68 @@ class Domain: islset1 = libisl.isl_set_union(islset1, islset2) return islset1 + @classmethod + def _fromast(cls, node): + from .polyhedra import Polyhedron + if isinstance(node, ast.Module) and len(node.body) == 1: + return cls._fromast(node.body[0]) + elif isinstance(node, ast.Expr): + return cls._fromast(node.value) + elif isinstance(node, ast.UnaryOp): + domain = cls._fromast(node.operand) + if isinstance(node.operand, ast.invert): + return Not(domain) + elif isinstance(node, ast.BinOp): + domain1 = cls._fromast(node.left) + domain2 = cls._fromast(node.right) + if isinstance(node.op, ast.BitAnd): + return And(domain1, domain2) + elif isinstance(node.op, ast.BitOr): + return Or(domain1, domain2) + elif isinstance(node, ast.Compare): + equalities = [] + inequalities = [] + left = Expression._fromast(node.left) + for i in range(len(node.ops)): + op = node.ops[i] + right = Expression._fromast(node.comparators[i]) + if isinstance(op, ast.Lt): + inequalities.append(right - left - 1) + elif isinstance(op, ast.LtE): + inequalities.append(right - left) + elif isinstance(op, ast.Eq): + equalities.append(left - right) + elif isinstance(op, ast.GtE): + inequalities.append(left - right) + elif isinstance(op, ast.Gt): + inequalities.append(left - right - 1) + else: + break + left = right + else: + return Polyhedron(equalities, inequalities) + raise SyntaxError('invalid syntax') + @classmethod def fromstring(cls, string): - raise NotImplementedError + # remove brackets + string = re.sub(r'^\{\s*|\s*\}$', '', string) + # replace '=' by '==' + string = re.sub(r'([^<=>])=([^<=>])', r'\1==\2', string) + # replace 'and', 'or', 'not' + string = re.sub(r'\band\b|,|&&|/\\|∧|∩', r' & ', string) + string = re.sub(r'\bor\b|;|\|\||\\/|∨|∪', r' | ', string) + string = re.sub(r'\bnot\b|!|¬', r' ~', string) + tokens = re.split(r'(&|\||~)', string) + for i, token in enumerate(tokens): + if i % 2 == 0: + # add implicit multiplication operators, e.g. '5x' -> '5*x' + token = re.sub(r'(\d+|\))\s*([^\W\d_]\w*|\()', r'\1*\2', token) + token = '({})'.format(token) + tokens[i] = token + string = ''.join(tokens) + tree = ast.parse(string) + return cls._fromast(tree) def __repr__(self): assert len(self.polyhedra) >= 2