Skip to content

Commit c096a31

Browse files
authored
More plot compile functions (#1525)
Improve plot_compile evaluation and expand tests * Added pre-evaluation of expressions in plot_compile to handle cases where functions are rewritten before sympy conversion. * Expanded test coverage in test_plot_compile.py to include additional numeric functions such as CubeRoot, Divide, Log10, Log2, Minus, and Subtract.
1 parent a1d40e1 commit c096a31

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

mathics/eval/drawing/plot_compile.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ def plot_compile(evaluation, expr, names, debug=0):
5050
print("=== compiling expr")
5151
print_expression_tree(expr)
5252

53+
# Evaluate the expr first in case it hasn't been already,
54+
# because some functions are not themselves sympy-enabled
55+
# if they always get rewritten to one that is.
56+
try:
57+
new_expr = expr.evaluate(evaluation)
58+
if new_expr:
59+
expr = new_expr
60+
except Exception:
61+
pass
62+
if debug >= 2:
63+
print("post-eval", expr)
64+
5365
# Ask the expr Expression to generate a sympy expression and handle errors
5466
try:
5567
sympy_expr = expr.to_sympy()

test/builtin/drawing/test_plot_compile.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143
# dict(name="KelvinKer", args=[0]), # sympy_name is ''
144144
# dict(name="KroneckerProduct", args=[0]), # args are matrices
145145
# dict(name="LaguerreL", args=[1,0]), # SympyFunction.to_sympy silent TypeError
146-
# dict(name="LambertW", args=[0]), # not registered with mathics_to_sympy
146+
dict(name="LambertW", args=[1]),
147147
dict(name="LegendreP", args=[1, 1]),
148148
# dict(name="LegendreQ", args=[1,0]), # sympy_name is ''
149149
# dict(name="LerchPhi", args=[1,2,0.25]), # sympy expects lerchphi()
@@ -208,6 +208,32 @@
208208
# dict(name="Glaisher", args=None), # to_sympy() fails
209209
# dict(name="Khinchin", args=None), # to_sympy() fails
210210
#
211+
# Following have none of the above but do have A_NUMERIC_FUNCTION set
212+
#
213+
# dict(name="AiryAiZero", args=[1,1]),
214+
# dict(name="AiryBiZero", args=[0]),
215+
# dict(name="BernsteinBasis", args=[4, 3, 0.5]),
216+
dict(name="CubeRoot", args=[3]),
217+
dict(name="Divide", args=[1, 1]),
218+
# dict(name="FractionalPart", args=[3.5]),
219+
# dict(name="IntegerPart", args=[1.2]),
220+
dict(name="Log10", args=[10]),
221+
dict(name="Log2", args=[10]),
222+
# dict(name="LogisticSigmoid", args=[0]),
223+
# dict(name="Max", args=[0,1]),
224+
# dict(name="Min", args=[0,1]),
225+
dict(name="Minus", args=[5]),
226+
# dict(name="Mod", args=[10,2]),
227+
# dict(name="Multinomial", args=[0]),
228+
# dict(name="PolygonalNumber", args=[0]),
229+
# dict(name="Quotient", args=[5,3]),
230+
# dict(name="QuotientRemainder", args=[5,3]),
231+
# dict(name="RealAbs", args=[-1]),
232+
# dict(name="RealSign", args=[0]),
233+
# dict(name="Round", args=[1.2]),
234+
dict(name="Subtract", args=[5, 3]),
235+
# dict(name="UnitStep", args=[0]),
236+
#
211237
# Following have no sympy_name and no mpmath_name,
212238
# but do have "number" or "numeric" in their path.
213239
# Some may be possible candidates for building out compilation
@@ -258,14 +284,12 @@
258284
# dict(name="FindMinimum", args=[0]),
259285
# dict(name="FindRoot", args=[0]),
260286
# dict(name="FittedModel", args=[0]),
261-
# dict(name="FractionalPart", args=[0]),
262287
# dict(name="FromDigits", args=[0]),
263288
# dict(name="FullSimplify", args=[0]),
264289
# dict(name="Gudermannian", args=[0]),
265290
# dict(name="IntegerDigits", args=[0]),
266291
# dict(name="IntegerExponent", args=[0]),
267292
# dict(name="IntegerLength", args=[0]),
268-
# dict(name="IntegerPart", args=[0]),
269293
# dict(name="IntegerPartitions", args=[0]),
270294
# dict(name="IntegerReverse", args=[0]),
271295
# dict(name="IntegerString", args=[0]),
@@ -278,9 +302,6 @@
278302
# dict(name="Limit", args=[0]),
279303
# dict(name="LinearModelFit", args=[0]),
280304
# dict(name="LinearSolve", args=[0]),
281-
# dict(name="Log10", args=[0]),
282-
# dict(name="Log2", args=[0]),
283-
# dict(name="LogisticSigmoid", args=[0]),
284305
# dict(name="MachinePrecision", args=[0]),
285306
# dict(name="ManhattanDistance", args=[0]),
286307
# dict(name="MantissaExponent", args=[0]),
@@ -313,11 +334,8 @@
313334
# dict(name="RandomReal", args=[0]),
314335
# dict(name="RandomSample", args=[0]),
315336
# dict(name="Rationalize", args=[0]),
316-
# dict(name="RealAbs", args=[0]),
317337
# dict(name="RealDigits", args=[0]),
318-
# dict(name="RealSign", args=[0]),
319338
# dict(name="Reals", args=[0]),
320-
# dict(name="Round", args=[0]),
321339
# dict(name="RowReduce", args=[0]),
322340
# dict(name="SeedRandom", args=[0]),
323341
# dict(name="Series", args=[0]),
@@ -332,7 +350,6 @@
332350
# dict(name="Tr", args=[0]),
333351
# dict(name="Undefined", args=[0]),
334352
# dict(name="Underflow", args=[0]),
335-
# dict(name="UnitStep", args=[0]),
336353
# dict(name="Variables", args=[0]),
337354
]
338355

0 commit comments

Comments
 (0)