Skip to content

Commit 79e1369

Browse files
mmaterarocky
authored andcommitted
Singletonize N
1 parent 4286b32 commit 79e1369

11 files changed

Lines changed: 50 additions & 40 deletions

File tree

mathics/builtin/arithmetic.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
Real,
3232
String,
3333
Symbol,
34+
SymbolN,
3435
SymbolFalse,
3536
SymbolNull,
3637
SymbolTrue,
@@ -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:
@@ -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: 3 additions & 3 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, SymbolList
9+
from mathics.core.expression import Expression, Integer, Number, SymbolTrue, SymbolFalse, SymbolList, SymbolN
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
@@ -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,7 +1134,7 @@ 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

mathics/builtin/graphics.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
String,
2828
Symbol,
2929
SymbolList,
30+
SymbolN,
3031
strip_context,
3132
system_symbols,
3233
system_symbols_dict,
@@ -399,7 +400,7 @@ def apply(self, graphics, evaluation, options):
399400

400401
for option in options:
401402
if option not in ("System`ImageSize",):
402-
options[option] = Expression("N", options[option]).evaluate(evaluation)
403+
options[option] = Expression(SymbolN, options[option]).evaluate(evaluation)
403404

404405
# The below could probably be done with graphics.filter..
405406
new_leaves = []
@@ -493,12 +494,12 @@ def convert(content):
493494
):
494495
if head == "System`Inset":
495496
n_leaves = [content.leaves[0]] + [
496-
Expression("N", leaf).evaluate(evaluation)
497+
Expression(SymbolN, leaf).evaluate(evaluation)
497498
for leaf in content.leaves[1:]
498499
]
499500
else:
500501
n_leaves = (
501-
Expression("N", leaf).evaluate(evaluation)
502+
Expression(SymbolN, leaf).evaluate(evaluation)
502503
for leaf in content.leaves
503504
)
504505
else:
@@ -508,7 +509,7 @@ def convert(content):
508509

509510
for option in options:
510511
if option not in ("System`ImageSize",):
511-
options[option] = Expression("N", options[option]).evaluate(evaluation)
512+
options[option] = Expression(SymbolN, options[option]).evaluate(evaluation)
512513
from mathics.builtin.graphics3d import Graphics3DBox, Graphics3D
513514

514515
if type(self) is Graphics:

mathics/builtin/lists.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
Symbol,
3333
SymbolFailed,
3434
SymbolNull,
35+
SymbolN,
3536
Integer,
3637
Number,
3738
Real,
@@ -4932,7 +4933,7 @@ class _PrecomputedDistances(PrecomputedDistances):
49324933

49334934
def __init__(self, df, p, evaluation):
49344935
distances_form = [df(p[i], p[j]) for i in range(len(p)) for j in range(i)]
4935-
distances = Expression("N", Expression(SymbolList, *distances_form)).evaluate(
4936+
distances = Expression(SymbolN, Expression(SymbolList, *distances_form)).evaluate(
49364937
evaluation
49374938
)
49384939
mpmath_distances = [_to_real_distance(d) for d in distances.leaves]
@@ -4950,7 +4951,7 @@ def __init__(self, df, p, evaluation):
49504951

49514952
def _compute_distance(self, i, j):
49524953
p = self._p
4953-
d = Expression("N", self._df(p[i], p[j])).evaluate(self._evaluation)
4954+
d = Expression(SymbolN, self._df(p[i], p[j])).evaluate(self._evaluation)
49544955
return _to_real_distance(d)
49554956

49564957

mathics/builtin/numbertheory.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from mathics.version import __version__ # noqa used in loading to check consistency.
1111
from mathics.builtin.base import Builtin, Test, SympyFunction
12-
from mathics.core.expression import Expression, Integer, Rational, Symbol, from_python
12+
from mathics.core.expression import Expression, Integer, Rational, Symbol, from_python, SymbolN
1313
from mathics.core.convert import from_sympy, SympyPrime
1414
import mpmath
1515

@@ -528,13 +528,13 @@ def apply(self, n, b, evaluation):
528528
return expr
529529

530530
if n_sympy.is_constant():
531-
temp_n = Expression("N", n).evaluate(evaluation)
531+
temp_n = Expression(SymbolN, n).evaluate(evaluation)
532532
py_n = temp_n.to_python()
533533
else:
534534
return expr
535535

536536
if b_sympy.is_constant():
537-
temp_b = Expression("N", b).evaluate(evaluation)
537+
temp_b = Expression(SymbolN, b).evaluate(evaluation)
538538
py_b = temp_b.to_python()
539539
else:
540540
return expr
@@ -559,7 +559,7 @@ def apply_2(self, n, evaluation):
559559
return expr
560560
# Handle Input with special cases such as PI and E
561561
if n_sympy.is_constant():
562-
temp_n = Expression("N", n).evaluate(evaluation)
562+
temp_n = Expression(SymbolN, n).evaluate(evaluation)
563563
py_n = temp_n.to_python()
564564
else:
565565
return expr

mathics/builtin/numeric.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
SymbolFalse,
4747
SymbolTrue,
4848
SymbolList,
49+
SymbolN,
4950
from_python,
5051
)
5152
from mathics.core.convert import from_sympy
@@ -234,13 +235,13 @@ def apply_with_prec(self, expr, prec, evaluation):
234235

235236
name = expr.get_lookup_name()
236237
if name != "":
237-
nexpr = Expression("N", expr, prec)
238+
nexpr = Expression(SymbolN, expr, prec)
238239
result = evaluation.definitions.get_value(
239240
name, "System`NValues", nexpr, evaluation
240241
)
241242
if result is not None:
242243
if not result.same(nexpr):
243-
result = Expression("N", result, prec).evaluate(evaluation)
244+
result = Expression(SymbolN, result, prec).evaluate(evaluation)
244245
return result
245246

246247
if expr.is_atom():
@@ -258,10 +259,10 @@ def apply_with_prec(self, expr, prec, evaluation):
258259
eval_range = ()
259260
else:
260261
eval_range = range(len(expr.leaves))
261-
head = Expression("N", expr.head, prec).evaluate(evaluation)
262+
head = Expression(SymbolN, expr.head, prec).evaluate(evaluation)
262263
leaves = expr.get_mutable_leaves()
263264
for index in eval_range:
264-
leaves[index] = Expression("N", leaves[index], prec).evaluate(
265+
leaves[index] = Expression(SymbolN, leaves[index], prec).evaluate(
265266
evaluation
266267
)
267268
return Expression(head, *leaves)
@@ -650,8 +651,8 @@ def apply_with_func_domain(self, func, domain, evaluation, options):
650651
(lambda u: a - z + z / u, lambda u: z * u ** (-2.0))
651652
)
652653
elif a.is_numeric() and b.is_numeric():
653-
a = Expression("N", a).evaluate(evaluation).value
654-
b = Expression("N", b).evaluate(evaluation).value
654+
a = Expression(SymbolN, a).evaluate(evaluation).value
655+
b = Expression(SymbolN, b).evaluate(evaluation).value
655656
subdomain2.append([a, b])
656657
coordtransform.append(None)
657658
else:
@@ -1665,7 +1666,7 @@ def apply_with_base(self, n, b, evaluation, nr_elements=None, pos=None):
16651666
).evaluate(evaluation)
16661667
else:
16671668
if rational_no:
1668-
n = Expression("N", n).evaluate(evaluation)
1669+
n = Expression(SymbolN, n).evaluate(evaluation)
16691670
else:
16701671
return evaluation.message("RealDigits", "ndig", expr)
16711672
py_n = abs(n.value)

mathics/builtin/plot.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
Integer,
2121
from_python,
2222
SymbolList,
23+
SymbolN,
2324
)
2425
from mathics.builtin.base import Builtin
2526
from mathics.builtin.scoping import dynamic_scoping
@@ -383,7 +384,7 @@ def quiet_f(*args):
383384

384385
return quiet_f
385386

386-
expr = Expression("N", expr)
387+
expr = Expression(SymbolN, expr)
387388
quiet_expr = Expression(
388389
"Quiet",
389390
expr,
@@ -1005,7 +1006,7 @@ def _draw(self, data, color, evaluation, options):
10051006
sector_origin = self.get_option(options, "SectorOrigin", evaluation)
10061007
if not sector_origin.has_form("List", 2):
10071008
return
1008-
sector_origin = Expression("N", sector_origin).evaluate(evaluation)
1009+
sector_origin = Expression(SymbolN, sector_origin).evaluate(evaluation)
10091010

10101011
orientation = sector_origin.leaves[0]
10111012
if (

mathics/core/convert.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,12 @@ def from_sympy(expr):
123123
Expression,
124124
MachineReal,
125125
SymbolNull,
126+
SymbolList,
126127
)
127128
from mathics.core.numbers import machine_precision
128129

129130
if isinstance(expr, (tuple, list)):
130-
return Expression("List", *[from_sympy(item) for item in expr])
131+
return Expression(SymbolList, *[from_sympy(item) for item in expr])
131132
if isinstance(expr, int):
132133
return Integer(expr)
133134
if isinstance(expr, float):
@@ -143,11 +144,11 @@ def from_sympy(expr):
143144
# This is a vector (only one column)
144145
# Transpose and select first row to get result equivalent to Mathematica
145146
return Expression(
146-
"List", *[from_sympy(item) for item in expr.T.tolist()[0]]
147+
SymbolList, *[from_sympy(item) for item in expr.T.tolist()[0]]
147148
)
148149
else:
149150
return Expression(
150-
"List", *[[from_sympy(item) for item in row] for row in expr.tolist()]
151+
SymbolList, *[[from_sympy(item) for item in row] for row in expr.tolist()]
151152
)
152153
if isinstance(expr, sympy.MatPow):
153154
return Expression("MatrixPower", from_sympy(expr.base), from_sympy(expr.exp))
@@ -226,9 +227,9 @@ def from_sympy(expr):
226227
return Expression(
227228
"Piecewise",
228229
Expression(
229-
"List",
230+
SymbolList,
230231
*[
231-
Expression("List", from_sympy(case), from_sympy(cond))
232+
Expression(SymbolList, from_sympy(case), from_sympy(cond))
232233
for case, cond in args
233234
]
234235
),
@@ -316,7 +317,7 @@ def from_sympy(expr):
316317
return Expression(Symbol(name), *args)
317318

318319
elif isinstance(expr, sympy.Tuple):
319-
return Expression("List", *[from_sympy(arg) for arg in expr.args])
320+
return Expression(SymbolList, *[from_sympy(arg) for arg in expr.args])
320321

321322
# elif isinstance(expr, sympy.Sum):
322323
# return Expression('Sum', )

mathics/core/definitions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def clear_pymathics_modules(self):
171171
for key in list(builtins_by_module.keys()):
172172
if not key.startswith("mathics."):
173173
del builtins_by_module[key]
174-
for key in pymathics:
174+
for key in self.pymathics:
175175
del self.pymathics[key]
176176

177177
self.pymathics = {}

mathics/core/evaluation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from mathics_scanner import TranslateError
1414

1515
from mathics import settings
16-
from mathics.core.expression import ensure_context, KeyComparable, SymbolAborted
16+
from mathics.core.expression import ensure_context, KeyComparable, SymbolAborted, SymbolList, SymbolNull
1717

1818
FORMATS = [
1919
"StandardForm",
@@ -250,7 +250,7 @@ def __init__(
250250
self.format = format
251251
self.catch_interrupt = catch_interrupt
252252

253-
self.SymbolNull = Symbol("Null")
253+
self.SymbolNull = SymbolNull
254254

255255
# status of last evaluate
256256
self.exc_result = self.SymbolNull
@@ -458,7 +458,7 @@ def format_output(self, expr, format=None):
458458
def set_quiet_messages(self, messages) -> None:
459459
from mathics.core.expression import Expression
460460

461-
value = Expression("List", *messages)
461+
value = Expression(SymbolList, *messages)
462462
self.definitions.set_ownvalue("Internal`$QuietMessages", value)
463463

464464
def get_quiet_messages(self):

0 commit comments

Comments
 (0)