diff --git a/mathics/builtin/procedural.py b/mathics/builtin/procedural.py index 6aaa6c143..e15e12424 100644 --- a/mathics/builtin/procedural.py +++ b/mathics/builtin/procedural.py @@ -22,7 +22,7 @@ A_PROTECTED, A_READ_PROTECTED, ) -from mathics.core.builtin import Builtin, InfixOperator, IterationFunction +from mathics.core.builtin import Builtin, InfixOperator, IterationFunction, SympyObject from mathics.core.evaluation import Evaluation from mathics.core.expression import Expression from mathics.core.interrupt import ( @@ -37,6 +37,8 @@ from mathics.eval.datetime import eval_pause, valid_time_from_expression from mathics.eval.patterns import match +import sympy + SymbolWhich = Symbol("Which") @@ -365,7 +367,8 @@ def eval(self, start, test, incr, body, evaluation): return SymbolNull -class If(Builtin): +# note that this is not strictly speaking procedural +class If(SympyObject): """ :WMA link:https://reference.wolfram.com/language/ref/If.html @@ -443,6 +446,21 @@ def eval_with_false_and_other(self, condition, t, f, u, evaluation): else: return u.evaluate(evaluation) + def to_sympy(self, expr, **kwargs): + args = [e.to_sympy() for e in expr.elements] + if all(arg is not None for arg in args): + if len(args) == 2: + cond, t = args + return sympy.Piecewise((t, cond)) + elif len(args) == 3: + cond, t, f = args + return sympy.Piecewise((t, cond), (f, True)) + elif len(args) == 4: + cond, t, f, u = args + return sympy.Piecewise((t, cond==True), (f, cond==False), (u, True)) + + return None + class Interrupt(Builtin): r""" @@ -542,6 +560,7 @@ def eval(self, expr, evaluation: Evaluation): # pylint: disable=unused-argument raise ReturnInterrupt(expr) +# TODO: to_sympy like If class Switch(Builtin): """ :WMA link: @@ -648,6 +667,7 @@ def eval_with_tag( raise WLThrowInterrupt(value, tag) +# TODO: to_sympy like If class Which(Builtin): """