Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions mathics/builtin/procedural.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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")


Expand Down Expand Up @@ -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):
"""
<url>:WMA link:https://reference.wolfram.com/language/ref/If.html</url>

Expand Down Expand Up @@ -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"""
Expand Down Expand Up @@ -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):
"""
<url>:WMA link:
Expand Down Expand Up @@ -648,6 +667,7 @@ def eval_with_tag(
raise WLThrowInterrupt(value, tag)


# TODO: to_sympy like If
class Which(Builtin):
"""
<url>
Expand Down