Skip to content

Commit fed3e09

Browse files
committed
Use max_recursion 0 in _Plot.
Also use Symbols over character strings more often in Plot options routines.
1 parent afed316 commit fed3e09

File tree

1 file changed

+35
-29
lines changed

1 file changed

+35
-29
lines changed

mathics/builtin/drawing/plot.py

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from mathics.core.symbols import Symbol, SymbolList
3030
from mathics.core.systemsymbols import (
3131
SymbolAll,
32+
SymbolAutomatic,
3233
SymbolBlack,
3334
SymbolEdgeForm,
3435
SymbolFull,
@@ -260,43 +261,47 @@ def eval(self, functions, x, start, stop, evaluation: Evaluation, options: dict)
260261

261262
# Mesh Option
262263
mesh_option = self.get_option(options, "Mesh", evaluation)
263-
mesh = mesh_option.to_python()
264-
if mesh not in ["System`None", "System`Full", "System`All"]:
264+
if mesh_option not in (SymbolNone, SymbolFull, SymbolAll):
265265
evaluation.message("Mesh", "ilevels", mesh_option)
266266
mesh = "System`None"
267+
else:
268+
mesh = mesh_option.to_python()
267269

268270
# PlotPoints Option
269271
plotpoints_option = self.get_option(options, "PlotPoints", evaluation)
270-
plotpoints = plotpoints_option.to_python()
271-
if plotpoints == "System`None":
272+
if plotpoints_option is SymbolNone:
272273
plotpoints = 57
274+
else:
275+
plotpoints = plotpoints_option.to_python()
273276
if not (isinstance(plotpoints, int) and plotpoints >= 2):
274277
evaluation.message(self.get_name(), "ppts", plotpoints)
275278
return
276279

277280
# MaxRecursion Option
278281
max_recursion_limit = 15
279282
maxrecursion_option = self.get_option(options, "MaxRecursion", evaluation)
280-
maxrecursion = maxrecursion_option.to_python()
283+
# Old default was 3. Bruce Lucas observes that
284+
# 0 using more points is faster and gives better results.
285+
maxrecursion = 0
281286
try:
282-
if maxrecursion == "System`Automatic":
283-
maxrecursion = 3
284-
elif maxrecursion == float("inf"):
285-
maxrecursion = max_recursion_limit
286-
raise ValueError
287-
elif isinstance(maxrecursion, int):
288-
if maxrecursion > max_recursion_limit:
287+
if maxrecursion_option is not SymbolAutomatic:
288+
maxrecursion = maxrecursion_option.to_python()
289+
if maxrecursion == float("inf"):
289290
maxrecursion = max_recursion_limit
290291
raise ValueError
291-
if maxrecursion < 0:
292+
elif isinstance(maxrecursion, int):
293+
if maxrecursion > max_recursion_limit:
294+
maxrecursion = max_recursion_limit
295+
raise ValueError
296+
if maxrecursion < 0:
297+
maxrecursion = 0
298+
raise ValueError
299+
else:
292300
maxrecursion = 0
293301
raise ValueError
294-
else:
295-
maxrecursion = 0
296-
raise ValueError
297302
except ValueError:
298303
evaluation.message(
299-
self.get_name(), "invmaxrec", maxrecursion, max_recursion_limit
304+
self.get_name(), "invmaxrec", maxrecursion_option, max_recursion_limit
300305
)
301306
assert isinstance(maxrecursion, int)
302307

@@ -312,22 +317,23 @@ def check_exclusion(excl):
312317
return True
313318

314319
exclusions_option = self.get_option(options, "Exclusions", evaluation)
315-
exclusions = eval_N(exclusions_option, evaluation).to_python()
316320
# TODO Turn expressions into points E.g. Sin[x] == 0 becomes 0, 2 Pi...
317321

318-
if exclusions in ["System`None", ["System`None"]]:
322+
if exclusions_option in (SymbolNone, (SymbolNone,)):
319323
exclusions = "System`None"
320-
elif not isinstance(exclusions, list):
321-
exclusions = [exclusions]
324+
else:
325+
exclusions = eval_N(exclusions_option, evaluation).to_python()
326+
if not isinstance(exclusions, list):
327+
exclusions = [exclusions]
322328

323-
if isinstance(exclusions, list) and all( # noqa
324-
check_exclusion(excl) for excl in exclusions
325-
):
326-
pass
329+
if isinstance(exclusions, list) and all( # noqa
330+
check_exclusion(excl) for excl in exclusions
331+
):
332+
pass
327333

328-
else:
329-
evaluation.message(self.get_name(), "invexcl", exclusions_option)
330-
exclusions = ["System`Automatic"]
334+
else:
335+
evaluation.message(self.get_name(), "invexcl", exclusions_option)
336+
exclusions = ["System`Automatic"]
331337

332338
# exclusions is now either 'None' or a list of reals and 'Automatic'
333339
assert exclusions == "System`None" or isinstance(exclusions, list)
@@ -381,7 +387,7 @@ def get_plotrange(self, plotrange, start, stop):
381387

382388
def process_function_and_options(
383389
self, functions, x, start, stop, evaluation: Evaluation, options: dict
384-
) -> tuple:
390+
) -> Optional[tuple]:
385391
"""Process the arguments of a plot expression."""
386392
if isinstance(functions, Symbol) and functions.name is not x.get_name():
387393
rules = evaluation.definitions.get_ownvalues(functions.name)

0 commit comments

Comments
 (0)