Skip to content

Commit 1f5dbb2

Browse files
mmaterammatera
andauthored
Convert results just if the condition is valid in If.to_sympy (#1537)
Just a small detail about converting conditional expressions to sympy. --------- Co-authored-by: mmatera <[email protected]>
1 parent c71c4da commit 1f5dbb2

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

mathics/builtin/procedural.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,6 @@ def eval_with_false_and_other(self, condition, t, f, u, evaluation):
453453
def to_sympy(self, expr, **kwargs):
454454
if len(expr.elements) == 3:
455455
sympy_cond = expr.elements[0].to_sympy(**kwargs)
456-
sympy_true = expr.elements[1].to_sympy(**kwargs)
457-
sympy_false = expr.elements[2].to_sympy(**kwargs)
458456
if sympy_cond is None:
459457
return
460458
# sympy.Piecewise is is picky as to what type of conds it will accept,
@@ -463,12 +461,16 @@ def to_sympy(self, expr, **kwargs):
463461
# Seems an attempt at early typechecking, but maybe too restrictive -
464462
# what about Exprs in general that might or might not evaluate to Boolean?
465463
# See similar code in mathics.builtin.arithmetic.ConditionalExpression.
466-
if (
464+
if not (
467465
sympy_cond.is_Boolean
468466
or sympy_cond.is_Relational
469467
or sympy_cond.is_Symbol
470468
):
471-
return sympy.Piecewise((sympy_true, sympy_cond), (sympy_false, True))
469+
return
470+
sympy_true = expr.elements[1].to_sympy(**kwargs)
471+
sympy_false = expr.elements[2].to_sympy(**kwargs)
472+
473+
return sympy.Piecewise((sympy_true, sympy_cond), (sympy_false, True))
472474

473475

474476
class Interrupt(Builtin):

0 commit comments

Comments
 (0)