From b370c2e4dab9b035db6c0ada2387af5a11069586 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Fri, 8 Jul 2011 00:51:49 -0400 Subject: [PATCH] Support tuples in expressions in a few places, to be able to compile tuple generation. --- pymbolic/compiler.py | 21 ++++++++++++++------- pymbolic/mapper/__init__.py | 5 +++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/pymbolic/compiler.py b/pymbolic/compiler.py index 3600e02..858cca7 100644 --- a/pymbolic/compiler.py +++ b/pymbolic/compiler.py @@ -24,7 +24,7 @@ def _constant_mapper(c): class CompileMapper(StringifyMapper): def __init__(self): - StringifyMapper.__init__(self, + StringifyMapper.__init__(self, constant_mapper=_constant_mapper) def map_polynomial(self, expr, enclosing_prec): @@ -46,7 +46,7 @@ class CompileMapper(StringifyMapper): next_exp = rev_data[i+1][0] else: next_exp = 0 - result = "(%s+%s)%s" % (result, self(coeff, PREC_SUM), + result = "(%s+%s)%s" % (result, self(coeff, PREC_SUM), stringify_exp(exp-next_exp)) #print "A", result #print "B", expr @@ -68,16 +68,23 @@ class CompileMapper(StringifyMapper): return "numpy.array(%s)" % stringify_leading_dimension(expr) + def map_foreign(self, expr, enclosing_prec): + if isinstance(expr, tuple): + return "(%s)" % (", ".join(self.rec(child, PREC_NONE) for child in expr)) + else: + return StringifyMapper.map_foreign(self, expr, enclosing_prec) + + class CompiledExpression: """This class encapsulates a compiled expression. - + The main reason for its existence is the fact that a dynamically-constructed lambda function is not picklable. """ - + def __init__(self, expression, variables = []): import pymbolic.primitives as primi @@ -105,13 +112,13 @@ class CompiledExpression: all_variables = self._Variables + used_variables expr_s = CompileMapper()(self._Expression, PREC_NONE) - func_s = "lambda %s:%s" % (",".join(str(v) for v in all_variables), + func_s = "lambda %s: %s" % (",".join(str(v) for v in all_variables), expr_s) self.__call__ = eval(func_s, ctx) - + def __getinitargs__(self): return self._Expression, self._Variables - + def __getstate__(self): return None diff --git a/pymbolic/mapper/__init__.py b/pymbolic/mapper/__init__.py index 0a33771..ffde8f0 100644 --- a/pymbolic/mapper/__init__.py +++ b/pymbolic/mapper/__init__.py @@ -142,6 +142,11 @@ class CombineMapper(RecursiveMapper): self.rec(expr.then), self.rec(expr.else_)]) + def map_foreign(self, expr, *args): + if isinstance(expr, tuple): + return self.combine([self.rec(child) for child in expr]) + else: + return RecursiveMapper.map_foreign(self, expr, *args) -- GitLab