Skip to content

Commit 07d5552

Browse files
committed
Add Method opton to N[]
and some other small tweaks
1 parent ad80dc7 commit 07d5552

3 files changed

Lines changed: 31 additions & 13 deletions

File tree

mathics/builtin/assignment.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ def _get_usage_string(symbol, evaluation, htmlout=False):
848848
bio = pymathics.get(definition.name)
849849
if bio is None:
850850
bio = builtins.get(definition.name)
851-
851+
852852
if bio is not None:
853853
from mathics.doc.doc import Doc
854854
docstr = bio.builtin.__class__.__doc__
@@ -892,7 +892,7 @@ class Information(PrefixOperator):
892892
893893
894894
#> ? Table
895-
|
895+
|
896896
. 'Table[expr, {i, n}]'
897897
. evaluates expr with i ranging from 1 to n, returning
898898
. a list of the results.
@@ -906,7 +906,7 @@ class Information(PrefixOperator):
906906
= Null
907907
908908
#> Information[Table]
909-
|
909+
|
910910
. 'Table[expr, {i, n}]'
911911
. evaluates expr with i ranging from 1 to n, returning
912912
. a list of the results.

mathics/builtin/constants.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ def mp_constant(fn: str, d=None) -> mpmath.ctx_mp_python.mpf:
2929
if d is None:
3030
return getattr(mpmath, fn)()
3131
else:
32+
# TODO: In some function like Pi, you can
33+
# ask for a certain number of digits, but the
34+
# accuracy will be less than that. Figure out
35+
# what's up and compensate somehow.
3236
mpmath.mp.dps = int_d = int(d)
3337
return getattr(mpmath, fn)(prec=int_d)
3438

@@ -43,6 +47,9 @@ def mp_convert_constant(obj, **kwargs):
4347

4448

4549
def numpy_constant(name: str, d=None) -> float:
50+
# TODO: although numpy doesn't support arbitrary precision,
51+
# if d is smaller than the precision given we could *reduce* the
52+
# float returned.
4653
return getattr(numpy, name)
4754

4855

@@ -54,10 +61,17 @@ class _Constant_Common(Predefined):
5461

5562
attributes = ("Constant", "Protected", "ReadProtected")
5663
nargs = 0
64+
options = {"Method": "sympy"}
5765

58-
def apply_N(self, precision, evaluation):
59-
"N[%(name)s, precision_]"
60-
return self.get_constant(precision, evaluation)
66+
def apply_N(self, precision, evaluation, options={}):
67+
"N[%(name)s, precision_?NumericQ, OptionsPattern[%(name)s]]"
68+
69+
preference = self.get_option(options, "Method", evaluation).get_string_value()
70+
return self.get_constant(precision, evaluation, preference)
71+
72+
def apply_N2(self, evaluation, options={}):
73+
"N[%(name)s, OptionsPattern[%(name)s]]"
74+
return self.apply_N(None, evaluation, options)
6175

6276
def is_constant(self) -> bool:
6377
return True
@@ -71,10 +85,12 @@ def get_constant(self, precision, evaluation, preference=None):
7185
.get_string_value()
7286
)
7387
# TODO: validate PreferredBackendMethod is in "mpmath", "numpy", "sympy"
74-
try:
75-
d = get_precision(precision, evaluation)
76-
except PrecisionValueError:
77-
d = None
88+
d = None
89+
if precision:
90+
try:
91+
d = get_precision(precision, evaluation)
92+
except PrecisionValueError:
93+
pass
7894

7995
conversion_fn = MachineReal if d is None else PrecisionReal
8096

@@ -85,13 +101,15 @@ def get_constant(self, precision, evaluation, preference=None):
85101
elif preference == "mpmath" and hasattr(self, "mpmath_name"):
86102
value = mp_constant(self.mpmath_name, d)
87103
elif preference == "numpy" and hasattr(self, "numpy_name"):
88-
value = numpy_constant(self.numpy_name)
104+
# Note numpy doesn't support arbitarary precision
105+
value = numpy_constant(self.numpy_name, d)
89106
elif hasattr(self, "mpmath_name"):
90107
value = mp_constant(self.mpmath_name, d)
91108
elif hasattr(self, "sympy_name"):
92109
value = sympy_constant(self.sympy_name, d)
93110
elif hasattr(self, "numpy_name"):
94-
value = numpy_constant(self.numpy_name)
111+
# Note numpy doesn't support arbitarary precision
112+
value = numpy_constant(self.numpy_name, d)
95113
return conversion_fn(value)
96114

97115

mathics/builtin/linalg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ def mp_eig(mp_matrix) -> Expression:
739739
return Expression("List", *eigenvalues)
740740

741741
options = {
742-
"Method": "Sympy",
742+
"Method": "sympy",
743743
}
744744

745745
def apply(self, m, evaluation, options={}) -> Expression:

0 commit comments

Comments
 (0)