Skip to content

Commit c012cc5

Browse files
authored
Merge pull request #1204 from mathics/SymbolList
Singletonize Symbols frequently used in Head
2 parents 21eb3eb + 4a3ffcc commit c012cc5

19 files changed

+331
-298
lines changed

mathics/builtin/arithmetic.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@
3131
Real,
3232
String,
3333
Symbol,
34+
SymbolComplexInfinity,
35+
SymbolDirectedInfinity,
3436
SymbolFalse,
37+
SymbolInfinity,
38+
SymbolN,
3539
SymbolNull,
40+
SymbolSequence,
3641
SymbolTrue,
37-
SymbolList,
38-
SymbolInfinity,
39-
SymbolDirectedInfinity,
40-
SymbolComplexInfinity,
41-
from_python,
4242
from_mpmath,
43+
from_python,
4344
)
4445
from mathics.core.numbers import min_prec, dps, SpecialValueError
4546

@@ -122,7 +123,7 @@ def apply(self, z, evaluation):
122123
prec = min_prec(*args)
123124
d = dps(prec)
124125
args = [
125-
Expression("N", arg, Integer(d)).evaluate(evaluation) for arg in args
126+
Expression(SymbolN, arg, Integer(d)).evaluate(evaluation) for arg in args
126127
]
127128
with mpmath.workprec(prec):
128129
mpmath_args = [x.to_mpmath() for x in args]
@@ -947,7 +948,7 @@ def apply_check(self, x, y, evaluation):
947948
if isinstance(y, Number):
948949
y_err = y
949950
else:
950-
y_err = Expression("N", y).evaluate(evaluation)
951+
y_err = Expression(SymbolN, y).evaluate(evaluation)
951952
if isinstance(y_err, Number):
952953
py_y = y_err.round_to_float(permit_complex=True).real
953954
if py_y > 0:
@@ -963,7 +964,7 @@ def apply_check(self, x, y, evaluation):
963964
factor = self.apply(Expression("Sequence", x.imag, y), evaluation)
964965
return Expression("Times", factor, Expression("Power", Integer(-1), yhalf))
965966

966-
result = self.apply(Expression("Sequence", x, y), evaluation)
967+
result = self.apply(Expression(SymbolSequence, x, y), evaluation)
967968
if result is None or result != SymbolNull:
968969
return result
969970

@@ -1418,7 +1419,7 @@ def apply(self, expr, evaluation):
14181419
result = _iszero(exprexp)
14191420
if result is None:
14201421
# Can't get exact answer, so try approximate equal
1421-
numeric_val = Expression("N", expr).evaluate(evaluation)
1422+
numeric_val = Expression(SymbolN, expr).evaluate(evaluation)
14221423
if numeric_val and hasattr(numeric_val, "is_approx_zero"):
14231424
result = numeric_val.is_approx_zero
14241425
elif (

mathics/builtin/calculus.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from mathics.version import __version__ # noqa used in loading to check consistency.
77

88
from mathics.builtin.base import Builtin, PostfixOperator, SympyFunction
9-
from mathics.core.expression import Expression, Integer, Number, SymbolTrue, SymbolFalse
9+
from mathics.core.expression import Expression, Integer, Number, SymbolTrue, SymbolFalse, SymbolList, SymbolN, SymbolRule
1010
from mathics.core.convert import sympy_symbol_prefix, SympyExpression, from_sympy
1111
from mathics.core.rules import Pattern
1212
from mathics.core.numbers import dps
@@ -190,7 +190,7 @@ def summand(leaf, index):
190190
def apply_wrong(self, expr, x, other, evaluation):
191191
"D[expr_, {x_, other___}]"
192192

193-
arg = Expression("List", x, *other.get_sequence())
193+
arg = Expression(SymbolList, x, *other.get_sequence())
194194
evaluation.message("D", "dvar", arg)
195195
return Expression("D", expr, arg)
196196

@@ -743,7 +743,7 @@ def apply(self, eqs, vars, evaluation):
743743
if eq == SymbolTrue:
744744
pass
745745
elif eq == SymbolFalse:
746-
return Expression("List")
746+
return Expression(SymbolList)
747747
elif not eq.has_form("Equal", 2):
748748
return evaluation.message("Solve", "eqf", eqs_original)
749749
else:
@@ -816,9 +816,9 @@ def transform_solution(sol):
816816
if not isinstance(result, list):
817817
result = [result]
818818
if isinstance(result, list) and len(result) == 1 and result[0] is True:
819-
return Expression("List", Expression("List"))
819+
return Expression(SymbolList, Expression(SymbolList))
820820
if result == [None]:
821-
return Expression("List")
821+
return Expression(SymbolList)
822822
results = []
823823
for sol in result:
824824
results.extend(transform_solution(sol))
@@ -842,7 +842,7 @@ def transform_solution(sol):
842842
Expression(
843843
"List",
844844
*(
845-
Expression("Rule", var, from_sympy(sol[var_sympy]))
845+
Expression(SymbolRule, var, from_sympy(sol[var_sympy]))
846846
for var, var_sympy in zip(vars, vars_sympy)
847847
if var_sympy in sol
848848
)
@@ -1098,7 +1098,7 @@ class FindRoot(Builtin):
10981098
def apply(self, f, x, x0, evaluation):
10991099
"FindRoot[f_, {x_, x0_}]"
11001100

1101-
x0 = Expression("N", x0).evaluate(evaluation)
1101+
x0 = Expression(SymbolN, x0).evaluate(evaluation)
11021102
if not isinstance(x0, Number):
11031103
evaluation.message("FindRoot", "snum", x0)
11041104
return
@@ -1134,11 +1134,11 @@ def sub(evaluation):
11341134
return
11351135
if x1 == x0:
11361136
break
1137-
x0 = Expression("N", x1).evaluate(
1137+
x0 = Expression(SymbolN, x1).evaluate(
11381138
evaluation
11391139
) # N required due to bug in sympy arithmetic
11401140
count += 1
11411141
else:
11421142
evaluation.message("FindRoot", "maxiter")
11431143

1144-
return Expression("List", Expression("Rule", x, x0))
1144+
return Expression(SymbolList, Expression(SymbolRule, x, x0))

mathics/builtin/files.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
SymbolFalse,
4343
SymbolNull,
4444
SymbolTrue,
45+
SymbolList,
4546
from_mpmath,
4647
from_python,
4748
valid_context_name,
@@ -396,7 +397,7 @@ class Path(Predefined):
396397
name = "$Path"
397398

398399
def evaluate(self, evaluation):
399-
return Expression("List", *[String(p) for p in PATH_VAR])
400+
return Expression(SymbolList, *[String(p) for p in PATH_VAR])
400401

401402

402403
class OperatingSystem(Predefined):

mathics/builtin/graphics.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
Real,
2727
String,
2828
Symbol,
29+
SymbolList,
30+
SymbolN,
31+
SymbolMakeBoxes,
2932
strip_context,
3033
system_symbols,
3134
system_symbols_dict,
@@ -290,7 +293,7 @@ def _CMC_distance(lab1, lab2, l, c):
290293

291294

292295
def _extract_graphics(graphics, format, evaluation):
293-
graphics_box = Expression("MakeBoxes", graphics).evaluate(evaluation)
296+
graphics_box = Expression(SymbolMakeBoxes, graphics).evaluate(evaluation)
294297
builtin = GraphicsBox(expression=False)
295298
elements, calc_dimensions = builtin._prepare_elements(
296299
graphics_box.leaves, {"evaluation": evaluation}, neg_y=True
@@ -398,7 +401,7 @@ def apply(self, graphics, evaluation, options):
398401

399402
for option in options:
400403
if option not in ("System`ImageSize",):
401-
options[option] = Expression("N", options[option]).evaluate(evaluation)
404+
options[option] = Expression(SymbolN, options[option]).evaluate(evaluation)
402405

403406
# The below could probably be done with graphics.filter..
404407
new_leaves = []
@@ -475,7 +478,7 @@ def convert(content):
475478
head = content.get_head_name()
476479

477480
if head == "System`List":
478-
return Expression("List", *[convert(item) for item in content.leaves])
481+
return Expression(SymbolList, *[convert(item) for item in content.leaves])
479482
elif head == "System`Style":
480483
return Expression(
481484
"StyleBox", *[convert(item) for item in content.leaves]
@@ -492,12 +495,12 @@ def convert(content):
492495
):
493496
if head == "System`Inset":
494497
n_leaves = [content.leaves[0]] + [
495-
Expression("N", leaf).evaluate(evaluation)
498+
Expression(SymbolN, leaf).evaluate(evaluation)
496499
for leaf in content.leaves[1:]
497500
]
498501
else:
499502
n_leaves = (
500-
Expression("N", leaf).evaluate(evaluation)
503+
Expression(SymbolN, leaf).evaluate(evaluation)
501504
for leaf in content.leaves
502505
)
503506
else:
@@ -507,7 +510,7 @@ def convert(content):
507510

508511
for option in options:
509512
if option not in ("System`ImageSize",):
510-
options[option] = Expression("N", options[option]).evaluate(evaluation)
513+
options[option] = Expression(SymbolN, options[option]).evaluate(evaluation)
511514
from mathics.builtin.graphics3d import Graphics3DBox, Graphics3D
512515

513516
if type(self) is Graphics:
@@ -986,9 +989,9 @@ def distance(a, b):
986989
*[distance(a, b) for a, b in zip(c1.leaves, c2.leaves)]
987990
)
988991
else:
989-
return Expression("List", *[distance(c, c2) for c in c1.leaves])
992+
return Expression(SymbolList, *[distance(c, c2) for c in c1.leaves])
990993
elif c2.get_head_name() == "System`List":
991-
return Expression("List", *[distance(c1, c) for c in c2.leaves])
994+
return Expression(SymbolList, *[distance(c1, c) for c in c2.leaves])
992995
else:
993996
return distance(c1, c2)
994997
except ColorError:
@@ -1436,7 +1439,7 @@ def do_init(self, graphics, points):
14361439
leaves = points.leaves
14371440
self.multi_parts = True
14381441
else:
1439-
leaves = [Expression("List", *points.leaves)]
1442+
leaves = [Expression(SymbolList, *points.leaves)]
14401443
self.multi_parts = False
14411444
lines = []
14421445
for leaf in leaves:
@@ -1492,7 +1495,7 @@ def init(self, graphics, style, item=None):
14921495
points = item.leaves[0]
14931496
if points.has_form("List", None) and len(points.leaves) != 0:
14941497
if all(not leaf.has_form("List", None) for leaf in points.leaves):
1495-
points = Expression("List", points)
1498+
points = Expression(SymbolList, points)
14961499
self.do_init(graphics, points)
14971500
else:
14981501
raise BoxConstructError
@@ -1890,7 +1893,7 @@ def process_option(self, name, value):
18901893
self.vertex_colors = [[black] * len(line) for line in self.lines]
18911894
colors = value.leaves
18921895
if not self.multi_parts:
1893-
colors = [Expression("List", *colors)]
1896+
colors = [Expression(SymbolList, *colors)]
18941897
for line_index, line in enumerate(self.lines):
18951898
if line_index >= len(colors):
18961899
break
@@ -2046,7 +2049,7 @@ def vertices():
20462049
)
20472050

20482051
new_item = Expression(
2049-
"RegularPolygonBox", Expression("List", *list(vertices()))
2052+
"RegularPolygonBox", Expression(SymbolList, *list(vertices()))
20502053
)
20512054
else:
20522055
raise BoxConstructError
@@ -3525,7 +3528,7 @@ def apply(self, colors, u, evaluation):
35253528
if not colors:
35263529
raise ColorError
35273530
except ColorError:
3528-
evaluation.message("Blend", "arg", Expression("List", colors_orig))
3531+
evaluation.message("Blend", "arg", Expression(SymbolList, colors_orig))
35293532
return
35303533

35313534
if u.has_form("List", None):
@@ -3546,7 +3549,7 @@ def apply(self, colors, u, evaluation):
35463549
use_list = False
35473550
if values is None:
35483551
return evaluation.message(
3549-
"Blend", "argl", u, Expression("List", colors_orig)
3552+
"Blend", "argl", u, Expression(SymbolList, colors_orig)
35503553
)
35513554

35523555
if use_list:

0 commit comments

Comments
 (0)