Skip to content

Commit

Permalink
Merge pull request #1124 from mathics/backend-constants
Browse files Browse the repository at this point in the history
Backend constants
  • Loading branch information
rocky committed Jan 27, 2021
2 parents b459d5b + 75af136 commit 1bd8460
Show file tree
Hide file tree
Showing 11 changed files with 1,139 additions and 1,000 deletions.
5 changes: 5 additions & 0 deletions mathics/autoload/settings.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@
Settings`$TraceGet::usage = "If this Boolean variable is set True, 'Get' traces the lines it reads that start a new expression";
Settings`$TraceGet = False
Unprotect[Settings`$TraceGet]


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";
Settings`$PreferredBackendMethod = "sympy"
Unprotect[Settings`$PreferredBackendMethod]
139 changes: 110 additions & 29 deletions mathics/builtin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,108 @@


from mathics.builtin import (
algebra, arithmetic, assignment, attributes, calculus, combinatorial, compilation,
comparison, control, datentime, diffeqns, evaluation, exptrig, functional,
graphics, graphics3d,
image, inout, integer, iohooks, linalg, lists, logic,
manipulate, quantities, numbertheory, numeric, options, patterns,
plot, physchemdata, randomnumbers, recurrence, specialfunctions, scoping,
strings, structure, system, tensors, xmlformat, optimization)
algebra,
arithmetic,
assignment,
attributes,
calculus,
combinatorial,
compilation,
comparison,
constants,
control,
datentime,
diffeqns,
evaluation,
exptrig,
functional,
graphics,
graphics3d,
image,
inout,
integer,
iohooks,
linalg,
lists,
logic,
manipulate,
quantities,
numbertheory,
numeric,
options,
patterns,
plot,
physchemdata,
randomnumbers,
recurrence,
specialfunctions,
scoping,
strings,
structure,
system,
tensors,
xmlformat,
optimization,
)

from mathics.builtin.base import (
Builtin, SympyObject, BoxConstruct, Operator, PatternObject)
Builtin,
SympyObject,
BoxConstruct,
Operator,
PatternObject,
)

from mathics.settings import ENABLE_FILES_MODULE

modules = [
algebra, arithmetic, assignment, attributes, calculus, combinatorial, compilation,
comparison, control, datentime, diffeqns, evaluation, exptrig, functional,
graphics, graphics3d,
image, inout, integer, iohooks, linalg, lists, logic,
manipulate, quantities, numbertheory, numeric, options, patterns,
plot, physchemdata, randomnumbers, recurrence, specialfunctions, scoping,
strings, structure, system, tensors, xmlformat, optimization]
algebra,
arithmetic,
assignment,
attributes,
calculus,
combinatorial,
compilation,
comparison,
constants,
control,
datentime,
diffeqns,
evaluation,
exptrig,
functional,
graphics,
graphics3d,
image,
inout,
integer,
iohooks,
linalg,
lists,
logic,
manipulate,
quantities,
numbertheory,
numeric,
options,
patterns,
plot,
physchemdata,
randomnumbers,
recurrence,
specialfunctions,
scoping,
strings,
structure,
system,
tensors,
xmlformat,
optimization,
]

if ENABLE_FILES_MODULE:
from mathics.builtin import files, importexport

modules += [files, importexport]

builtins = []
Expand All @@ -36,7 +114,7 @@
def is_builtin(var):
if var == Builtin:
return True
if hasattr(var, '__bases__'):
if hasattr(var, "__bases__"):
return any(is_builtin(base) for base in var.__bases__)
return False

Expand All @@ -46,11 +124,14 @@ def is_builtin(var):
vars = dir(module)
for name in vars:
var = getattr(module, name)
if (hasattr(var, '__module__') and
var.__module__.startswith('mathics.builtin.') and
var.__module__ != 'mathics.builtin.base' and
is_builtin(var) and not name.startswith('_') and
var.__module__ == module.__name__): # nopep8
if (
hasattr(var, "__module__")
and var.__module__.startswith("mathics.builtin.")
and var.__module__ != "mathics.builtin.base"
and is_builtin(var)
and not name.startswith("_")
and var.__module__ == module.__name__
): # nopep8

instance = var(expression=False)

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

# builtins = dict(builtins)

mathics_to_sympy = {} # here we have: name -> sympy object
mathics_to_python = {} # here we have: name -> string
mathics_to_sympy = {} # here we have: name -> sympy object
mathics_to_python = {} # here we have: name -> string
sympy_to_mathics = {}

box_constructs = {}
Expand Down Expand Up @@ -101,22 +182,22 @@ def get_module_doc(module):
doc = doc.strip()
if doc:
title = doc.splitlines()[0]
text = '\n'.join(doc.splitlines()[1:])
text = "\n".join(doc.splitlines()[1:])
else:
title = module.__name__
for prefix in ('mathics.builtin.', 'mathics.optional.'):
for prefix in ("mathics.builtin.", "mathics.optional."):
if title.startswith(prefix):
title = title[len(prefix):]
title = title[len(prefix) :]
title = title.capitalize()
text = ''
text = ""
return title, text


def contribute(definitions):
# let MakeBoxes contribute first
builtins['System`MakeBoxes'].contribute(definitions)
builtins["System`MakeBoxes"].contribute(definitions)
for name, item in builtins.items():
if name != 'System`MakeBoxes':
if name != "System`MakeBoxes":
item.contribute(definitions)

from mathics.core.expression import ensure_context
Expand Down
85 changes: 0 additions & 85 deletions mathics/builtin/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
PostfixOperator,
Test,
SympyFunction,
SympyConstant,
)

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


class Infinity(SympyConstant):
"""
<dl>
<dt>'Infinity'
<dd>represents an infinite real quantity.
</dl>
>> 1 / Infinity
= 0
>> Infinity + 100
= Infinity
Use 'Infinity' in sum and limit calculations:
>> Sum[1/x^2, {x, 1, Infinity}]
= Pi ^ 2 / 6
#> FullForm[Infinity]
= DirectedInfinity[1]
#> (2 + 3.5*I) / Infinity
= 0.
#> Infinity + Infinity
= Infinity
#> Infinity / Infinity
: Indeterminate expression 0 Infinity encountered.
= Indeterminate
"""

sympy_name = "oo"
python_equivalent = math.inf

rules = {
"Infinity": "DirectedInfinity[1]",
"MakeBoxes[Infinity, f:StandardForm|TraditionalForm]": ('"\\[Infinity]"'),
}


class ComplexInfinity(SympyConstant):
"""
<dl>
<dt>'ComplexInfinity'
<dd>represents an infinite complex quantity of undetermined direction.
</dl>
>> 1 / ComplexInfinity
= 0
>> ComplexInfinity * Infinity
= ComplexInfinity
>> FullForm[ComplexInfinity]
= DirectedInfinity[]
## Issue689
#> ComplexInfinity + ComplexInfinity
: Indeterminate expression ComplexInfinity + ComplexInfinity encountered.
= Indeterminate
#> ComplexInfinity + Infinity
: Indeterminate expression ComplexInfinity + Infinity encountered.
= Indeterminate
"""

sympy_name = "zoo"

rules = {
"ComplexInfinity": "DirectedInfinity[]",
}


class DirectedInfinity(SympyFunction):
"""
<dl>
Expand Down Expand Up @@ -1350,24 +1283,6 @@ def evaluate(self, evaluation):
return Complex(Integer(0), Integer(1))


class Indeterminate(SympyConstant):
"""
<dl>
<dt>'Indeterminate'
<dd>represents an indeterminate result.
</dl>
>> 0^0
: Indeterminate expression 0 ^ 0 encountered.
= Indeterminate
>> Tan[Indeterminate]
= Indeterminate
"""

sympy_name = "nan"


class NumberQ(Test):
"""
<dl>
Expand Down
Loading

0 comments on commit 1bd8460

Please sign in to comment.