Skip to content

Commit 36c63da

Browse files
mmaterarocky
authored andcommitted
using Compile to improve the behaviour of to_python over System`Function expressions
1 parent 0263772 commit 36c63da

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

mathics/builtin/compilation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
class Compile(Builtin):
17-
"""
17+
'''
1818
<dl>
1919
<dt>'Compile[{x1, x2, ...}, expr_]'
2020
<dd>Compiles $expr$ assuming each $xi$ is a $Real$ number.

mathics/core/expression.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,16 +990,22 @@ def to_python(self, *args, **kwargs):
990990
Null -> None
991991
Symbol -> '...'
992992
String -> '"..."'
993+
Function -> python function
993994
numbers -> Python number
994995
If kwarg n_evaluation is given, apply N first to the expression.
995996
"""
996997
from mathics.builtin.base import mathics_to_python
997-
998998
n_evaluation = kwargs.get('n_evaluation')
999+
head_name = self._head.get_name()
9991000
if n_evaluation is not None:
1001+
if head_name == 'System`Function':
1002+
compiled = Expression("System`Compile", *(self._leaves))
1003+
compiled = compiled.evaluate(n_evaluation)
1004+
if compiled.get_head_name() == "System`CompiledFunction":
1005+
return compiled.leaves[2].cfunc
10001006
value = Expression('N', self).evaluate(n_evaluation)
10011007
return value.to_python()
1002-
head_name = self._head.get_name()
1008+
10031009
if head_name == 'System`DirectedInfinity' and len(self._leaves) == 1:
10041010
direction = self._leaves[0].get_int_value()
10051011
if direction == 1:
@@ -1008,6 +1014,7 @@ def to_python(self, *args, **kwargs):
10081014
return -math.inf
10091015
elif head_name == 'System`List':
10101016
return [leaf.to_python(*args, **kwargs) for leaf in self._leaves]
1017+
10111018
if head_name in mathics_to_python:
10121019
py_obj = mathics_to_python[head_name]
10131020
# Start here

0 commit comments

Comments
 (0)