Skip to content

Commit e0400a9

Browse files
committed
Formatting, comments
1 parent 5c53483 commit e0400a9

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

mathics/eval/drawing/plot3d_vectorized.py

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from mathics.core.evaluation import Evaluation
1212
from mathics.core.symbols import strip_context
13-
from mathics.core.systemsymbols import SymbolRGBColor, SymbolNone, SymbolEdgeForm
13+
from mathics.core.systemsymbols import SymbolNone, SymbolRGBColor
1414
from mathics.timing import Timer
1515

1616
from .plot_compile import plot_compile
@@ -36,24 +36,35 @@ def eval_Plot3D(
3636
elif plot_options.mesh is not SymbolNone:
3737
nmesh = 20
3838

39-
# https://davidmathlogic.com/colorblind
39+
# color-blind friendly palette from https://davidmathlogic.com/colorblind
4040
palette = [
41-
(255, 176, 0), # orange
42-
(100, 143, 255), # blue
43-
(220, 38, 127), # red
44-
(50, 150, 140), # green
45-
(120, 94, 240), # purple
46-
#(240, 228, 66), # yellow
47-
(254, 97, 0), # dark orange
48-
(0, 114, 178), # dark blue
49-
#(0, 0, 0), # black
41+
(255, 176, 0), # orange
42+
(100, 143, 255), # blue
43+
(220, 38, 127), # red
44+
(50, 150, 140), # green
45+
(120, 94, 240), # purple
46+
(254, 97, 0), # dark orange
47+
(0, 114, 178), # dark blue
5048
]
5149

5250
# compile the functions
5351
with Timer("compile"):
54-
compiled_functions = [plot_compile(evaluation, function, names) for function in plot_options.functions]
52+
compiled_functions = [
53+
plot_compile(evaluation, function, names)
54+
for function in plot_options.functions
55+
]
5556

5657
def compute_over_grid(nx, ny):
58+
"""
59+
For each function, computes an (nx*ny, 3) array of coordinates (xyzs),
60+
and an (nx, ny) array of indices (inxs) into the coordinate array representing
61+
the index into the coordinate orray of the corresponding position in the grid.
62+
Returns an iterator over xyzs,inxs pairs, one for each function.
63+
64+
This is used for computing the full grid of quads representing the
65+
surface defined by each function, and also for computing a sparse
66+
grid used to display a mesh of lines on the surface.
67+
"""
5768

5869
# compute (nx, ny) grids of xs and ys for corresponding vertexes
5970
xs = np.linspace(xmin, xmax, nx)
@@ -66,7 +77,6 @@ def compute_over_grid(nx, ny):
6677
inxs = np.arange(math.prod(xs.shape)).reshape(xs.shape) + 1
6778

6879
for function in compiled_functions:
69-
7080
# compute zs from xs and ys using compiled function
7181
with Timer("compute zs"):
7282
zs = function(**{str(names[0]): xs, str(names[1]): ys})
@@ -89,33 +99,28 @@ def compute_over_grid(nx, ny):
8999

90100
# generate the quads and emit a GraphicsComplex containing them
91101
for i, (xyzs, inxs) in enumerate(compute_over_grid(*plot_options.plotpoints)):
92-
93102
# shift inxs array four different ways and stack to form
94103
# (4, nx-1, ny-1) array of quads represented as indexes into xyzs array
95-
quads = np.stack(
96-
[inxs[:-1, :-1], inxs[:-1, 1:], inxs[1:, 1:], inxs[1:, :-1]]
97-
)
104+
quads = np.stack([inxs[:-1, :-1], inxs[:-1, 1:], inxs[1:, 1:], inxs[1:, :-1]])
98105

99106
# transpose and flatten to ((nx-1)*(ny-1), 4) array, suitable for use in GraphicsComplex
100107
quads = quads.T.reshape(-1, 4)
101108

102109
# choose a color
103-
rgb = palette[i%len(palette)]
104-
rgb = [c/255.0 for c in rgb]
105-
#graphics.add_color(SymbolRGBColor, rgb)
110+
rgb = palette[i % len(palette)]
111+
rgb = [c / 255.0 for c in rgb]
112+
# graphics.add_color(SymbolRGBColor, rgb)
106113
graphics.add_directives([SymbolRGBColor, *rgb])
107114

108-
# add a GraphicsComplex for this function
115+
# add a GraphicsComplex displaying a surface for this function
109116
graphics.add_complex(xyzs, lines=None, polys=quads)
110117

111118
# if requested by the Mesh attribute create a mesh of lines covering the surfaces
112119
if nmesh:
113-
114120
# meshes are black for now
115-
graphics.add_directives([SymbolRGBColor, 0,0,0])
121+
graphics.add_directives([SymbolRGBColor, 0, 0, 0])
116122

117123
with Timer("Mesh"):
118-
nmesh = 20 # TODO: use supplied option
119124
nx, ny = plot_options.plotpoints
120125
# Do nmesh lines in each direction.
121126
# Each mesh line has high res (nx or ny) so it follows

mathics/eval/drawing/util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
from mathics.core.atoms import NumericArray
7-
from mathics.core.convert.expression import to_mathics_list, to_expression
7+
from mathics.core.convert.expression import to_expression, to_mathics_list
88
from mathics.core.expression import Expression
99
from mathics.core.list import ListExpression
1010
from mathics.core.symbols import Symbol
@@ -70,10 +70,10 @@ def cvt(d):
7070
return expr
7171
else:
7272
return d
73+
7374
for d in ds:
7475
expr = cvt(d)
7576
self.graphics.append(expr)
76-
7777

7878
def add_complex(self, xyzs, lines=None, polys=None):
7979
complex = [NumericArray(xyzs)]

0 commit comments

Comments
 (0)