Skip to content

Commit e0cffc0

Browse files
authored
Go over drawing.plot and ... (#1130)
add links to functional_iteration
1 parent 45e428a commit e0cffc0

6 files changed

Lines changed: 51 additions & 16 deletions

File tree

mathics/builtin/drawing/plot.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from mathics.core.list import ListExpression
2929
from mathics.core.symbols import Symbol, SymbolList, SymbolTrue
3030
from mathics.core.systemsymbols import (
31+
SymbolBlack,
3132
SymbolBlend,
3233
SymbolColorData,
3334
SymbolEdgeForm,
@@ -308,7 +309,16 @@ def eval(self, points, evaluation: Evaluation, options: dict):
308309
)
309310
)
310311

311-
all_points = eval_N(points, evaluation).to_python()
312+
# If "points" is a literal value with a Python representation,
313+
# it has a ".value" attribute with a non-None value. So here,
314+
# we don't have to eval_N().to_python().
315+
316+
all_points = (
317+
points.value
318+
if hasattr(points, "value") and points.value is not None
319+
else eval_N(points, evaluation).to_python()
320+
)
321+
312322
# FIXME: arrange for self to have a .symbolname property or attribute
313323
expr = Expression(Symbol(self.get_name()), points, *options_to_rules(options))
314324

@@ -1176,7 +1186,7 @@ def boxes():
11761186
x += 0.2
11771187

11781188
def rectangles():
1179-
yield Expression(SymbolEdgeForm, Symbol("Black"))
1189+
yield Expression(SymbolEdgeForm, SymbolBlack)
11801190

11811191
last_x1 = 0
11821192

@@ -1198,7 +1208,7 @@ def rectangles():
11981208
)
11991209

12001210
def axes():
1201-
yield Expression(SymbolFaceForm, Symbol("Black"))
1211+
yield Expression(SymbolFaceForm, SymbolBlack)
12021212

12031213
def points(x):
12041214
return ListExpression(vector2(x, 0), vector2(x, MTwoTenths))
@@ -1210,7 +1220,7 @@ def points(x):
12101220
yield Expression(SymbolLine, points(x1))
12111221

12121222
def labels(names):
1213-
yield Expression(SymbolFaceForm, Symbol("Black"))
1223+
yield Expression(SymbolFaceForm, SymbolBlack)
12141224

12151225
for (k, n), x0, x1, y in boxes():
12161226
if k <= len(names):
@@ -2362,7 +2372,7 @@ def phis(values):
23622372
phi0 = phi1
23632373

23642374
def segments():
2365-
yield Expression(SymbolEdgeForm, Symbol("Black"))
2375+
yield Expression(SymbolEdgeForm, SymbolBlack)
23662376

23672377
origin = vector2(0.0, 0.0)
23682378

@@ -2386,7 +2396,7 @@ def segments():
23862396
)
23872397

23882398
def labels(names):
2389-
yield Expression(SymbolFaceForm, Symbol("Black"))
2399+
yield Expression(SymbolFaceForm, SymbolBlack)
23902400

23912401
for values, (r0, r1) in zip(data, radii()):
23922402
for name, (phi0, phi1) in zip(names, phis(values)):

mathics/builtin/functional/functional_iteration.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@
55
Functional iteration is an elegant way to represent repeated operations that is used a lot.
66
"""
77

8-
from mathics.core.atoms import Integer1
98
from mathics.core.builtin import Builtin
109
from mathics.core.convert.python import from_python
1110
from mathics.core.evaluation import Evaluation
1211
from mathics.core.expression import Expression
1312
from mathics.core.expression_predefined import MATHICS3_INFINITY
1413
from mathics.core.symbols import Symbol, SymbolTrue
14+
from mathics.core.systemsymbols import SymbolAll
1515

1616
# This tells documentation how to sort this module
1717
sort_order = "mathics.builtin.iteratively-applying-functions"
1818

1919

2020
class FixedPoint(Builtin):
2121
"""
22+
<url>:WMA link:
23+
https://reference.wolfram.com/language/ref/FixedPoint.html</url>
24+
2225
<dl>
2326
<dt>'FixedPoint[$f$, $expr$]'
2427
<dd>starting with $expr$, iteratively applies $f$ until the result no longer changes.
@@ -84,9 +87,13 @@ def eval(self, f, expr, n, evaluation: Evaluation, options: dict):
8487

8588
class FixedPointList(Builtin):
8689
"""
90+
<url>:WMA link:
91+
https://reference.wolfram.com/language/ref/FixedPointList.html</url>
92+
8793
<dl>
8894
<dt>'FixedPointList[$f$, $expr$]'
89-
<dd>starting with $expr$, iteratively applies $f$ until the result no longer changes, and returns a list of all intermediate results.
95+
<dd>starting with $expr$, iteratively applies $f$ until the result no longer changes, \
96+
and returns a list of all intermediate results.
9097
9198
<dt>'FixedPointList[$f$, $expr$, $n$]'
9299
<dd>performs at most $n$ iterations.
@@ -100,12 +107,17 @@ class FixedPointList(Builtin):
100107
>> newton[9]
101108
= {1., 5., 3.4, 3.02353, 3.00009, 3., 3., 3.}
102109
103-
Plot the "hailstone" sequence of a number:
110+
Compute the <url>:Hailstone Number:
111+
https://mathworld.wolfram.com/HailstoneNumber.html</url>: for 14:
112+
104113
>> collatz[1] := 1;
105114
>> collatz[x_ ? EvenQ] := x / 2;
106115
>> collatz[x_] := 3 x + 1;
107116
>> list = FixedPointList[collatz, 14]
108117
= {14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, 1}
118+
119+
Plot this:
120+
109121
>> ListLinePlot[list]
110122
= -Graphics-
111123
"""
@@ -143,6 +155,9 @@ def eval(self, f, expr, n, evaluation: Evaluation):
143155

144156
class Fold(Builtin):
145157
"""
158+
<url>:WMA link:
159+
https://reference.wolfram.com/language/ref/Fold.html</url>
160+
146161
<dl>
147162
<dt>'Fold[$f$, $x$, $list$]'
148163
<dd>returns the result of iteratively applying the binary
@@ -166,6 +181,9 @@ class Fold(Builtin):
166181

167182
class FoldList(Builtin):
168183
"""
184+
<url>:WMA link:
185+
https://reference.wolfram.com/language/ref/FoldList.html</url>
186+
169187
<dl>
170188
<dt>'FoldList[$f$, $x$, $list$]'
171189
<dd>returns a list starting with $x$, where each element is
@@ -190,6 +208,9 @@ class FoldList(Builtin):
190208

191209
class Nest(Builtin):
192210
"""
211+
<url>:WMA link:
212+
https://reference.wolfram.com/language/ref/Nest.html</url>
213+
193214
<dl>
194215
<dt>'Nest[$f$, $expr$, $n$]'
195216
<dd>starting with $expr$, iteratively applies $f$ $n$ times and returns the final result.
@@ -217,6 +238,9 @@ def eval(self, f, expr, n, evaluation):
217238

218239
class NestList(Builtin):
219240
"""
241+
<url>:WMA link:
242+
https://reference.wolfram.com/language/ref/NestList.html</url>
243+
220244
<dl>
221245
<dt>'NestList[$f$, $expr$, $n$]'
222246
<dd>starting with $expr$, iteratively applies $f$ $n$ times and \
@@ -257,6 +281,9 @@ def eval(self, f, expr, n, evaluation):
257281

258282
class NestWhile(Builtin):
259283
"""
284+
<url>:WMA link:
285+
https://reference.wolfram.com/language/ref/NestWhile.html</url>
286+
260287
<dl>
261288
<dt>'NestWhile[$f$, $expr$, $test$]'
262289
<dd>applies a function $f$ repeatedly on an expression $expr$, until \
@@ -302,7 +329,7 @@ def eval(self, f, expr, test, m, evaluation: Evaluation):
302329

303330
results = [expr]
304331
while True:
305-
if m.get_name() == "System`All":
332+
if m is SymbolAll:
306333
test_elements = results
307334
else:
308335
test_elements = results[-m.value :]

mathics/core/builtin.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,7 @@ def eval_symbol(self, expr, iterator, evaluation):
830830

831831
def eval_range(self, expr, i, imax, evaluation):
832832
"%(name)s[expr_, {i_Symbol, imax_}]"
833+
833834
imax = imax.evaluate(evaluation)
834835
if imax.has_form("Range", None):
835836
# FIXME: this should work as an iterator in Python3, not
@@ -965,6 +966,7 @@ def eval_iter(self, expr, i, imin, imax, di, evaluation):
965966

966967
def eval_list(self, expr, i, items, evaluation):
967968
"%(name)s[expr_, {i_Symbol, {items___}}]"
969+
968970
items = items.evaluate(evaluation).get_sequence()
969971
result = []
970972
for item in items:

mathics/core/list.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,7 @@ def evaluate_elements(self, evaluation: Evaluation) -> Expression:
110110
return self
111111

112112
new_list = ListExpression(*elements)
113-
# TODO: we could have a specialized version of this
114-
# that keeps self.value up to date when that is
115-
# easy to do. That is left of some future time to
116-
# decide whether doing this this is warranted.
117113
new_list._build_elements_properties()
118-
new_list.value = None
119114
return new_list
120115

121116
@property

mathics/core/systemsymbols.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
SymbolAttributes = Symbol("System`Attributes")
4141
SymbolAutomatic = Symbol("System`Automatic")
4242
SymbolBaseForm = Symbol("System`BaseForm")
43+
SymbolBlack = Symbol("System`Black")
4344
SymbolBlank = Symbol("System`Blank")
4445
SymbolBlankNullSequence = Symbol("System`BlankNullSequence")
4546
SymbolBlankSequence = Symbol("System`BlankSequence")

mathics/eval/drawing/plot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def eval_ListPlot(
158158
options: miscellaneous graphics options from underlying M-Expression
159159
"""
160160

161-
if not isinstance(plot_groups, list) or len(plot_groups) == 0:
161+
if not isinstance(plot_groups, (list, tuple)) or len(plot_groups) == 0:
162162
return
163163

164164
# Classify the kind of data that "point" is, and

0 commit comments

Comments
 (0)