Skip to content

Commit 1bd8460

Browse files
authored
Merge pull request #1124 from mathics/backend-constants
Backend constants
2 parents b459d5b + 75af136 commit 1bd8460

11 files changed

Lines changed: 1139 additions & 1000 deletions

File tree

mathics/autoload/settings.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@
55
Settings`$TraceGet::usage = "If this Boolean variable is set True, 'Get' traces the lines it reads that start a new expression";
66
Settings`$TraceGet = False
77
Unprotect[Settings`$TraceGet]
8+
9+
10+
Settings`$PreferredBackendMethod::usage = "Set this do whether to use mpmath, numpy or Sympy for numeric and symbolic constants and methods when there is s choice";
11+
Settings`$PreferredBackendMethod = "sympy"
12+
Unprotect[Settings`$PreferredBackendMethod]

mathics/builtin/__init__.py

Lines changed: 110 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,108 @@
33

44

55
from mathics.builtin import (
6-
algebra, arithmetic, assignment, attributes, calculus, combinatorial, compilation,
7-
comparison, control, datentime, diffeqns, evaluation, exptrig, functional,
8-
graphics, graphics3d,
9-
image, inout, integer, iohooks, linalg, lists, logic,
10-
manipulate, quantities, numbertheory, numeric, options, patterns,
11-
plot, physchemdata, randomnumbers, recurrence, specialfunctions, scoping,
12-
strings, structure, system, tensors, xmlformat, optimization)
6+
algebra,
7+
arithmetic,
8+
assignment,
9+
attributes,
10+
calculus,
11+
combinatorial,
12+
compilation,
13+
comparison,
14+
constants,
15+
control,
16+
datentime,
17+
diffeqns,
18+
evaluation,
19+
exptrig,
20+
functional,
21+
graphics,
22+
graphics3d,
23+
image,
24+
inout,
25+
integer,
26+
iohooks,
27+
linalg,
28+
lists,
29+
logic,
30+
manipulate,
31+
quantities,
32+
numbertheory,
33+
numeric,
34+
options,
35+
patterns,
36+
plot,
37+
physchemdata,
38+
randomnumbers,
39+
recurrence,
40+
specialfunctions,
41+
scoping,
42+
strings,
43+
structure,
44+
system,
45+
tensors,
46+
xmlformat,
47+
optimization,
48+
)
1349

1450
from mathics.builtin.base import (
15-
Builtin, SympyObject, BoxConstruct, Operator, PatternObject)
51+
Builtin,
52+
SympyObject,
53+
BoxConstruct,
54+
Operator,
55+
PatternObject,
56+
)
1657

1758
from mathics.settings import ENABLE_FILES_MODULE
1859

1960
modules = [
20-
algebra, arithmetic, assignment, attributes, calculus, combinatorial, compilation,
21-
comparison, control, datentime, diffeqns, evaluation, exptrig, functional,
22-
graphics, graphics3d,
23-
image, inout, integer, iohooks, linalg, lists, logic,
24-
manipulate, quantities, numbertheory, numeric, options, patterns,
25-
plot, physchemdata, randomnumbers, recurrence, specialfunctions, scoping,
26-
strings, structure, system, tensors, xmlformat, optimization]
61+
algebra,
62+
arithmetic,
63+
assignment,
64+
attributes,
65+
calculus,
66+
combinatorial,
67+
compilation,
68+
comparison,
69+
constants,
70+
control,
71+
datentime,
72+
diffeqns,
73+
evaluation,
74+
exptrig,
75+
functional,
76+
graphics,
77+
graphics3d,
78+
image,
79+
inout,
80+
integer,
81+
iohooks,
82+
linalg,
83+
lists,
84+
logic,
85+
manipulate,
86+
quantities,
87+
numbertheory,
88+
numeric,
89+
options,
90+
patterns,
91+
plot,
92+
physchemdata,
93+
randomnumbers,
94+
recurrence,
95+
specialfunctions,
96+
scoping,
97+
strings,
98+
structure,
99+
system,
100+
tensors,
101+
xmlformat,
102+
optimization,
103+
]
27104

28105
if ENABLE_FILES_MODULE:
29106
from mathics.builtin import files, importexport
107+
30108
modules += [files, importexport]
31109

32110
builtins = []
@@ -36,7 +114,7 @@
36114
def is_builtin(var):
37115
if var == Builtin:
38116
return True
39-
if hasattr(var, '__bases__'):
117+
if hasattr(var, "__bases__"):
40118
return any(is_builtin(base) for base in var.__bases__)
41119
return False
42120

@@ -46,11 +124,14 @@ def is_builtin(var):
46124
vars = dir(module)
47125
for name in vars:
48126
var = getattr(module, name)
49-
if (hasattr(var, '__module__') and
50-
var.__module__.startswith('mathics.builtin.') and
51-
var.__module__ != 'mathics.builtin.base' and
52-
is_builtin(var) and not name.startswith('_') and
53-
var.__module__ == module.__name__): # nopep8
127+
if (
128+
hasattr(var, "__module__")
129+
and var.__module__.startswith("mathics.builtin.")
130+
and var.__module__ != "mathics.builtin.base"
131+
and is_builtin(var)
132+
and not name.startswith("_")
133+
and var.__module__ == module.__name__
134+
): # nopep8
54135

55136
instance = var(expression=False)
56137

@@ -61,8 +142,8 @@ def is_builtin(var):
61142

62143
# builtins = dict(builtins)
63144

64-
mathics_to_sympy = {} # here we have: name -> sympy object
65-
mathics_to_python = {} # here we have: name -> string
145+
mathics_to_sympy = {} # here we have: name -> sympy object
146+
mathics_to_python = {} # here we have: name -> string
66147
sympy_to_mathics = {}
67148

68149
box_constructs = {}
@@ -101,22 +182,22 @@ def get_module_doc(module):
101182
doc = doc.strip()
102183
if doc:
103184
title = doc.splitlines()[0]
104-
text = '\n'.join(doc.splitlines()[1:])
185+
text = "\n".join(doc.splitlines()[1:])
105186
else:
106187
title = module.__name__
107-
for prefix in ('mathics.builtin.', 'mathics.optional.'):
188+
for prefix in ("mathics.builtin.", "mathics.optional."):
108189
if title.startswith(prefix):
109-
title = title[len(prefix):]
190+
title = title[len(prefix) :]
110191
title = title.capitalize()
111-
text = ''
192+
text = ""
112193
return title, text
113194

114195

115196
def contribute(definitions):
116197
# let MakeBoxes contribute first
117-
builtins['System`MakeBoxes'].contribute(definitions)
198+
builtins["System`MakeBoxes"].contribute(definitions)
118199
for name, item in builtins.items():
119-
if name != 'System`MakeBoxes':
200+
if name != "System`MakeBoxes":
120201
item.contribute(definitions)
121202

122203
from mathics.core.expression import ensure_context

mathics/builtin/arithmetic.py

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
PostfixOperator,
2222
Test,
2323
SympyFunction,
24-
SympyConstant,
2524
)
2625

2726
from mathics.core.expression import (
@@ -1011,72 +1010,6 @@ def apply(self, n, evaluation):
10111010
return Expression("Power", n, Expression("Divide", 1, 3))
10121011

10131012

1014-
class Infinity(SympyConstant):
1015-
"""
1016-
<dl>
1017-
<dt>'Infinity'
1018-
<dd>represents an infinite real quantity.
1019-
</dl>
1020-
1021-
>> 1 / Infinity
1022-
= 0
1023-
>> Infinity + 100
1024-
= Infinity
1025-
1026-
Use 'Infinity' in sum and limit calculations:
1027-
>> Sum[1/x^2, {x, 1, Infinity}]
1028-
= Pi ^ 2 / 6
1029-
1030-
#> FullForm[Infinity]
1031-
= DirectedInfinity[1]
1032-
#> (2 + 3.5*I) / Infinity
1033-
= 0.
1034-
#> Infinity + Infinity
1035-
= Infinity
1036-
#> Infinity / Infinity
1037-
: Indeterminate expression 0 Infinity encountered.
1038-
= Indeterminate
1039-
"""
1040-
1041-
sympy_name = "oo"
1042-
python_equivalent = math.inf
1043-
1044-
rules = {
1045-
"Infinity": "DirectedInfinity[1]",
1046-
"MakeBoxes[Infinity, f:StandardForm|TraditionalForm]": ('"\\[Infinity]"'),
1047-
}
1048-
1049-
1050-
class ComplexInfinity(SympyConstant):
1051-
"""
1052-
<dl>
1053-
<dt>'ComplexInfinity'
1054-
<dd>represents an infinite complex quantity of undetermined direction.
1055-
</dl>
1056-
1057-
>> 1 / ComplexInfinity
1058-
= 0
1059-
>> ComplexInfinity * Infinity
1060-
= ComplexInfinity
1061-
>> FullForm[ComplexInfinity]
1062-
= DirectedInfinity[]
1063-
1064-
## Issue689
1065-
#> ComplexInfinity + ComplexInfinity
1066-
: Indeterminate expression ComplexInfinity + ComplexInfinity encountered.
1067-
= Indeterminate
1068-
#> ComplexInfinity + Infinity
1069-
: Indeterminate expression ComplexInfinity + Infinity encountered.
1070-
= Indeterminate
1071-
"""
1072-
1073-
sympy_name = "zoo"
1074-
1075-
rules = {
1076-
"ComplexInfinity": "DirectedInfinity[]",
1077-
}
1078-
1079-
10801013
class DirectedInfinity(SympyFunction):
10811014
"""
10821015
<dl>
@@ -1350,24 +1283,6 @@ def evaluate(self, evaluation):
13501283
return Complex(Integer(0), Integer(1))
13511284

13521285

1353-
class Indeterminate(SympyConstant):
1354-
"""
1355-
<dl>
1356-
<dt>'Indeterminate'
1357-
<dd>represents an indeterminate result.
1358-
</dl>
1359-
1360-
>> 0^0
1361-
: Indeterminate expression 0 ^ 0 encountered.
1362-
= Indeterminate
1363-
1364-
>> Tan[Indeterminate]
1365-
= Indeterminate
1366-
"""
1367-
1368-
sympy_name = "nan"
1369-
1370-
13711286
class NumberQ(Test):
13721287
"""
13731288
<dl>

0 commit comments

Comments
 (0)