From 3065eb63c1dacd6af678b6fd16ba4966390b29f9 Mon Sep 17 00:00:00 2001 From: Dominic Kempf Date: Fri, 8 Sep 2017 09:13:20 +0200 Subject: [PATCH 1/2] Better handling of the else branch I realized sympy.Piecewise iterates over the conditions and returns the first which evaluates to True. So, the else branch should have `True` instead of `sp.Not(cond)` for a more robust implementation. --- pymbolic/interop/sympy.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pymbolic/interop/sympy.py b/pymbolic/interop/sympy.py index 2ccb9fe..a9626cf 100644 --- a/pymbolic/interop/sympy.py +++ b/pymbolic/interop/sympy.py @@ -67,7 +67,7 @@ class SympyToPymbolicMapper(SympyLikeToPymbolicMapper): # We only handle piecewises with 2 arguments! assert len(expr.args) == 2 # We only handle if/else cases - assert expr.args[0][1] == sympy.Not(expr.args[1][1]) + assert expr.args[0][1] is True then = self.rec(expr.args[0][0]) else_ = self.rec(expr.args[1][0]) cond = self.rec(expr.args[0][1]) @@ -106,7 +106,7 @@ class PymbolicToSympyMapper(PymbolicToSympyLikeMapper): def map_if(self, expr): cond = self.rec(expr.condition) return self.sym.Piecewise((self.rec(expr.then), cond), - (self.rec(expr.else_), self.sym.Not(cond)) + (self.rec(expr.else_), True) ) def map_comparison(self, expr): -- GitLab From fa78a7a45c6b86577e99b00f5be06918b65af1b7 Mon Sep 17 00:00:00 2001 From: Dominic Kempf Date: Fri, 8 Sep 2017 09:26:44 +0200 Subject: [PATCH 2/2] Fix glitch in if/else assertion --- pymbolic/interop/sympy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pymbolic/interop/sympy.py b/pymbolic/interop/sympy.py index a9626cf..b480771 100644 --- a/pymbolic/interop/sympy.py +++ b/pymbolic/interop/sympy.py @@ -67,7 +67,7 @@ class SympyToPymbolicMapper(SympyLikeToPymbolicMapper): # We only handle piecewises with 2 arguments! assert len(expr.args) == 2 # We only handle if/else cases - assert expr.args[0][1] is True + assert expr.args[1][1].is_Boolean and bool(expr.args[1][1]) is True then = self.rec(expr.args[0][0]) else_ = self.rec(expr.args[1][0]) cond = self.rec(expr.args[0][1]) -- GitLab