Skip to content

Commit 59ae8fd

Browse files
author
Release Manager
committed
gh-35922: using python-style loops in plot3d/*.pyx <!-- ^^^^^ Please provide a concise, informative and self-explanatory title. Don't put issue numbers in there, do this in the PR body below. For example, instead of "Fixes #1234" use "Introduce new method to calculate 1+1" --> <!-- Describe your changes here in detail --> converting loops in plot3d cython file to use `range` <!-- Why is this change required? What problem does it solve? --> <!-- If this PR resolves an open issue, please link to it here. For example "Fixes #12345". --> <!-- If your change requires a documentation PR, please link it appropriately. --> ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> <!-- If your change requires a documentation PR, please link it appropriately --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> <!-- Feel free to remove irrelevant items. --> - [x] The title is concise, informative, and self-explanatory. - [x] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation accordingly. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on - #12345: short description why this is a dependency - #34567: ... --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> URL: #35922 Reported by: Frédéric Chapoton Reviewer(s): Matthias Köppe
2 parents 19870d2 + f8ea850 commit 59ae8fd

File tree

5 files changed

+64
-64
lines changed

5 files changed

+64
-64
lines changed

src/sage/plot/plot3d/implicit_surface.pyx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,8 @@ cdef class MarchingCubesTriangles(MarchingCubes):
493493
cdef bint has_nan
494494
cdef point_c gradients[2]
495495
cdef int i
496-
for y from 0 <= y < ny - 1:
497-
for z from 0 <= z < nz:
496+
for y in range(ny - 1):
497+
for z in range(nz):
498498
if marching_has_edge(cur[y,z], cur[y+1,z], self.contour, &frac, &has_nan):
499499
v = mk_VertexInfo(x, y+frac, z, &self.eval_min, &self.eval_scale)
500500
if self.region is not None:
@@ -507,7 +507,7 @@ cdef class MarchingCubesTriangles(MarchingCubes):
507507
self.apply_point_func(&v.gradient, self.gradient, v)
508508
else:
509509
# Use central differencing.
510-
for i from 0 <= i < 2:
510+
for i in range(2):
511511
self.get_gradient(&gradients[i],
512512
x, y+i, z,
513513
cur[y+i,z],
@@ -527,8 +527,8 @@ cdef class MarchingCubesTriangles(MarchingCubes):
527527

528528
# OK, that updated the Y vertices. Now we do almost exactly
529529
# the same thing to update Z vertices.
530-
for y from 0 <= y < ny:
531-
for z from 0 <= z < nz - 1:
530+
for y in range(ny):
531+
for z in range(nz - 1):
532532
if marching_has_edge(cur[y,z], cur[y,z+1], self.contour, &frac, &has_nan):
533533
v = mk_VertexInfo(x, y, z+frac, &self.eval_min, &self.eval_scale)
534534
if self.region is not None:
@@ -541,7 +541,7 @@ cdef class MarchingCubesTriangles(MarchingCubes):
541541
self.apply_point_func(&v.gradient, self.gradient, v)
542542
else:
543543
# Use central differencing.
544-
for i from 0 <= i < 2:
544+
for i in range(2):
545545
self.get_gradient(&gradients[i],
546546
x, y, z+i,
547547
cur[y,z+i],
@@ -594,8 +594,8 @@ cdef class MarchingCubesTriangles(MarchingCubes):
594594
cdef double frac
595595
cdef bint has_nan
596596
cdef point_c gradients[2]
597-
for y from 0 <= y < ny:
598-
for z from 0 <= z < nz:
597+
for y in range(ny):
598+
for z in range(nz):
599599
if marching_has_edge(left[y,z], right[y,z], self.contour, &frac, &has_nan):
600600
v = mk_VertexInfo(x+frac, y, z, &self.eval_min, &self.eval_scale)
601601
if self.region is not None:
@@ -744,8 +744,8 @@ cdef class MarchingCubesTriangles(MarchingCubes):
744744

745745
cdef int i
746746

747-
for y from 0 <= y < ny-1:
748-
for z from 0 <= z < nz-1:
747+
for y in range(ny - 1):
748+
for z in range(nz - 1):
749749
# For each vertex (0 to 7), set the corresponding bit
750750
# of insideMask iff the vertex is inside the surface.
751751
insideMask = 0
@@ -908,13 +908,13 @@ cpdef render_implicit(f, xrange, yrange, zrange, plot_points, cube_marchers):
908908

909909
cdef MarchingCubes marcher
910910

911-
for n from 0 <= n < nx:
911+
for n in range(nx):
912912
x = nx-1-n
913913
eval_x = eval_min.x + eval_scale.x * x
914914
slice = data[n % 4, :, :]
915-
for y from 0 <= y < ny:
915+
for y in range(ny):
916916
eval_y = eval_min.y + eval_scale.y * y
917-
for z from 0 <= z < nz:
917+
for z in range(nz):
918918
eval_z = eval_min.z + eval_scale.z * z
919919
slice[y, z] = f(eval_x, eval_y, eval_z)
920920

@@ -1217,7 +1217,7 @@ cdef class ImplicitSurface(IndexFaceSet):
12171217
int fcount = len(results)
12181218

12191219
self.realloc(fcount * 3, fcount, fcount * 3)
1220-
for i from 0 <= i < fcount:
1220+
for i in range(fcount):
12211221
dest_face = &self._faces[i]
12221222
src_face = results[i]
12231223

@@ -1233,7 +1233,7 @@ cdef class ImplicitSurface(IndexFaceSet):
12331233
# ct is the mean of the colors of vertices
12341234
dest_face.color.r, dest_face.color.g, dest_face.color.b = ct
12351235

1236-
for j from 0 <= j < 3:
1236+
for j in range(3):
12371237
dest_face.vertices[j] = (3 * i) + j
12381238
dest_vertex = &self.vs[(3 * i) + j]
12391239
dest_vertex.x = src_face[j]['x']

src/sage/plot/plot3d/index_face_set.pyx

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,15 @@ cdef inline format_pmesh_face(face_c face, int has_color):
178178
# Naive triangulation
179179
all = []
180180
if has_color == 1:
181-
for i from 1 <= i < face.n - 1:
181+
for i in range(1, face.n - 1):
182182
r = sprintf_5i(ss, "%d\n%d\n%d\n%d\n%d", has_color * 4,
183183
face.vertices[0],
184184
face.vertices[i],
185185
face.vertices[i + 1],
186186
face.vertices[0])
187187
PyList_Append(all, PyBytes_FromStringAndSize(ss, r))
188188
else:
189-
for i from 1 <= i < face.n - 1:
189+
for i in range(1, face.n - 1):
190190
r = sprintf_6i(ss, "%d\n%d\n%d\n%d\n%d\n%d", has_color * 4,
191191
face.vertices[0],
192192
face.vertices[i],
@@ -369,16 +369,16 @@ cdef class IndexFaceSet(PrimitiveObject):
369369

370370
cdef Py_ssize_t i
371371
cdef Py_ssize_t index_len = 0
372-
for i from 0 <= i < len(faces):
372+
for i in range(len(faces)):
373373
index_len += len(faces[i])
374374

375375
self.realloc(len(point_list), len(faces), index_len)
376376

377-
for i from 0 <= i < self.vcount:
377+
for i in range(self.vcount):
378378
self.vs[i].x, self.vs[i].y, self.vs[i].z = point_list[i]
379379

380380
cdef int cur_pt = 0
381-
for i from 0 <= i < self.fcount:
381+
for i in range(self.fcount):
382382
self._faces[i].n = len(faces[i])
383383
self._faces[i].vertices = &self.face_indices[cur_pt]
384384
if self.global_texture:
@@ -517,15 +517,15 @@ cdef class IndexFaceSet(PrimitiveObject):
517517
cdef int* point_counts = <int *>check_calloc(self.vcount * 2 + 1, sizeof(int))
518518
# For each vertex, get number of faces
519519
cdef int* running_point_counts = &point_counts[self.vcount]
520-
for i from 0 <= i < self.fcount:
520+
for i in range(self.fcount):
521521
face = &self._faces[i]
522522
total += face.n
523-
for j from 0 <= j < face.n:
523+
for j in range(face.n):
524524
point_counts[face.vertices[j]] += 1
525525
# Running used as index into face list
526526
cdef int running = 0
527527
cdef int max = 0
528-
for i from 0 <= i < self.vcount:
528+
for i in range(self.vcount):
529529
running_point_counts[i] = running
530530
running += point_counts[i]
531531
if point_counts[i] > max:
@@ -540,9 +540,9 @@ cdef class IndexFaceSet(PrimitiveObject):
540540
raise
541541
sig_on()
542542
memset(point_counts, 0, sizeof(int) * self.vcount)
543-
for i from 0 <= i < self.fcount:
543+
for i in range(self.fcount):
544544
face = &self._faces[i]
545-
for j from 0 <= j < face.n:
545+
for j in range(face.n):
546546
v = face.vertices[j]
547547
point_faces[running_point_counts[v]+point_counts[v]] = face
548548
point_counts[v] += 1
@@ -557,7 +557,7 @@ cdef class IndexFaceSet(PrimitiveObject):
557557
while start < self.vcount:
558558
ix = self.vcount
559559
# Find creases
560-
for i from 0 <= i < self.vcount - start:
560+
for i in range(self.vcount - start):
561561
faces = &point_faces[running_point_counts[i]]
562562
any = 0
563563
for j from point_counts[i] > j >= 1:
@@ -582,17 +582,17 @@ cdef class IndexFaceSet(PrimitiveObject):
582582
raise
583583
ix = self.vcount
584584
running = 0
585-
for i from 0 <= i < self.vcount - start:
585+
for i in range(self.vcount - start):
586586
if point_counts[i] != running_point_counts[i+1] - running_point_counts[i]:
587587
# We have a new vertex
588588
self.vs[ix] = self.vs[i+start]
589589
# Update the point_counts and point_faces arrays for the next time around.
590590
count = running_point_counts[i+1] - running_point_counts[i] - point_counts[i]
591591
faces = &point_faces[running]
592-
for j from 0 <= j < count:
592+
for j in range(count):
593593
faces[j] = point_faces[running_point_counts[i] + point_counts[i] + j]
594594
face = faces[j]
595-
for k from 0 <= k < face.n:
595+
for k in range(face.n):
596596
if face.vertices[k] == i + start:
597597
face.vertices[k] = ix
598598
point_counts[ix-self.vcount] = count
@@ -653,8 +653,8 @@ cdef class IndexFaceSet(PrimitiveObject):
653653
"""
654654
cdef Py_ssize_t i, j
655655
return [[self._faces[i].vertices[j]
656-
for j from 0 <= j < self._faces[i].n]
657-
for i from 0 <= i < self.fcount]
656+
for j in range(self._faces[i].n)]
657+
for i in range(self.fcount)]
658658

659659
def has_local_colors(self):
660660
"""
@@ -719,11 +719,11 @@ cdef class IndexFaceSet(PrimitiveObject):
719719
if self.global_texture:
720720
raise ValueError('the texture is global')
721721
return [([self._faces[i].vertices[j]
722-
for j from 0 <= j < self._faces[i].n],
722+
for j in range(self._faces[i].n)],
723723
Color(self._faces[i].color.r,
724724
self._faces[i].color.g,
725725
self._faces[i].color.b).html_color())
726-
for i from 0 <= i < self.fcount]
726+
for i in range(self.fcount)]
727727

728728
def faces(self):
729729
"""
@@ -944,10 +944,10 @@ cdef class IndexFaceSet(PrimitiveObject):
944944
cdef int *partition = <int *>check_allocarray(self.fcount, sizeof(int))
945945

946946
part_counts = {}
947-
for i from 0 <= i < self.fcount:
947+
for i in range(self.fcount):
948948
face = &self._faces[i]
949949
P = self.vs[face.vertices[0]]
950-
for j from 1 <= j < face.n:
950+
for j in range(1, face.n):
951951
point_c_add(&P, P, self.vs[face.vertices[j]])
952952
point_c_mul(&P, P, 1.0/face.n)
953953
partition[i] = part = f(P.x, P.y, P.z)
@@ -964,13 +964,13 @@ cdef class IndexFaceSet(PrimitiveObject):
964964
memcpy(face_set.vs, self.vs, sizeof(point_c) * self.vcount)
965965
face_ix = 0
966966
ix = 0
967-
for i from 0 <= i < self.fcount:
967+
for i in range(self.fcount):
968968
if partition[i] == part:
969969
face = &self._faces[i]
970970
new_face = &face_set._faces[face_ix]
971971
new_face.n = face.n
972972
new_face.vertices = &face_set.face_indices[ix]
973-
for j from 0 <= j < face.n:
973+
for j in range(face.n):
974974
new_face.vertices[j] = face.vertices[j]
975975
face_ix += 1
976976
ix += face.n
@@ -1226,7 +1226,7 @@ cdef class IndexFaceSet(PrimitiveObject):
12261226
cdef face_c face
12271227
cdef Py_ssize_t i, k
12281228
sig_on()
1229-
for i from 0 <= i < self.fcount:
1229+
for i in range(self.fcount):
12301230
face = self._faces[i]
12311231
if transform is not None:
12321232
transform.transform_point_c(&P, self.vs[face.vertices[0]])
@@ -1242,7 +1242,7 @@ cdef class IndexFaceSet(PrimitiveObject):
12421242
else:
12431243
PyList_Append(lines, format_tachyon_texture(face.color))
12441244
if face.n > 3:
1245-
for k from 3 <= k < face.n:
1245+
for k in range(3, face.n):
12461246
Q = R
12471247
if transform is not None:
12481248
transform.transform_point_c(&R, self.vs[face.vertices[k]])
@@ -1290,7 +1290,7 @@ cdef class IndexFaceSet(PrimitiveObject):
12901290
for i in range(self.vcount)))
12911291
else:
12921292
vertices_str = "["
1293-
for i from 0 <= i < self.vcount:
1293+
for i in range(self.vcount):
12941294
transform.transform_point_c(&res, self.vs[i])
12951295
if i > 0:
12961296
vertices_str += ","
@@ -1408,7 +1408,7 @@ cdef class IndexFaceSet(PrimitiveObject):
14081408
vertices = []
14091409
cdef Transformation transform = render_params.transform
14101410
cdef point_c res
1411-
for i from 0 <= i < self.vcount:
1411+
for i in range(self.vcount):
14121412
if transform is None:
14131413
res = self.vs[i]
14141414
else:
@@ -1418,16 +1418,16 @@ cdef class IndexFaceSet(PrimitiveObject):
14181418

14191419
faces = []
14201420
cdef face_c face
1421-
for i from 0 <= i < self.fcount:
1421+
for i in range(self.fcount):
14221422
face = self._faces[i]
1423-
faces.append([int(face.vertices[j]) for j from 0 <= j < face.n])
1423+
faces.append([int(face.vertices[j]) for j in range(face.n)])
14241424
surface['faces'] = faces
14251425

14261426
if self.global_texture:
14271427
surface['color'] = '#' + str(self.texture.hex_rgb())
14281428
else:
14291429
face_colors = []
1430-
for i from 0 <= i < self.fcount:
1430+
for i in range(self.fcount):
14311431
face = self._faces[i]
14321432
color = Color(face.color.r, face.color.g, face.color.b)
14331433
face_colors.append(str(color.html_color()))
@@ -1472,16 +1472,16 @@ cdef class IndexFaceSet(PrimitiveObject):
14721472

14731473
sig_on()
14741474
if transform is None:
1475-
points = [format_obj_vertex(self.vs[i]) for i from 0 <= i < self.vcount]
1475+
points = [format_obj_vertex(self.vs[i]) for i in range(self.vcount)]
14761476
else:
14771477
points = []
1478-
for i from 0 <= i < self.vcount:
1478+
for i in range(self.vcount):
14791479
transform.transform_point_c(&res, self.vs[i])
14801480
PyList_Append(points, format_obj_vertex(res))
14811481

1482-
faces = [format_obj_face(self._faces[i], off) for i from 0 <= i < self.fcount]
1482+
faces = [format_obj_face(self._faces[i], off) for i in range(self.fcount)]
14831483
if not self.enclosed:
1484-
back_faces = [format_obj_face_back(self._faces[i], off) for i from 0 <= i < self.fcount]
1484+
back_faces = [format_obj_face_back(self._faces[i], off) for i in range(self.fcount)]
14851485
else:
14861486
back_faces = []
14871487

@@ -1513,25 +1513,25 @@ cdef class IndexFaceSet(PrimitiveObject):
15131513
sig_on()
15141514
if transform is None:
15151515
points = [format_pmesh_vertex(self.vs[i])
1516-
for i from 0 <= i < self.vcount]
1516+
for i in range(self.vcount)]
15171517
else:
15181518
points = []
1519-
for i from 0 <= i < self.vcount:
1519+
for i in range(self.vcount):
15201520
transform.transform_point_c(&res, self.vs[i])
15211521
PyList_Append(points, format_pmesh_vertex(res))
15221522

15231523
# activation of coloring in jmol
15241524
if self.global_texture:
15251525
faces = [format_pmesh_face(self._faces[i], 1)
1526-
for i from 0 <= i < self.fcount]
1526+
for i in range(self.fcount)]
15271527
else:
15281528
faces = [format_pmesh_face(self._faces[i], -1)
1529-
for i from 0 <= i < self.fcount]
1529+
for i in range(self.fcount)]
15301530

15311531
# If a face has more than 4 vertices, it gets chopped up in
15321532
# format_pmesh_face
15331533
cdef Py_ssize_t extra_faces = 0
1534-
for i from 0 <= i < self.fcount:
1534+
for i in range(self.fcount):
15351535
if self._faces[i].n >= 5:
15361536
extra_faces += self._faces[i].n-3
15371537

@@ -1641,14 +1641,14 @@ cdef class IndexFaceSet(PrimitiveObject):
16411641
dual.realloc(self.fcount, self.vcount, self.icount)
16421642

16431643
# is using dicts overly-heavy?
1644-
dual_faces = [{} for i from 0 <= i < self.vcount]
1644+
dual_faces = [{} for i in range(self.vcount)]
16451645

1646-
for i from 0 <= i < self.fcount:
1646+
for i in range(self.fcount):
16471647
sig_check()
16481648
# Let the vertex be centered on the face according to a simple average
16491649
face = &self._faces[i]
16501650
dual.vs[i] = self.vs[face.vertices[0]]
1651-
for j from 1 <= j < face.n:
1651+
for j in range(1, face.n):
16521652
point_c_add(&dual.vs[i], dual.vs[i], self.vs[face.vertices[j]])
16531653
point_c_mul(&dual.vs[i], dual.vs[i], 1.0/face.n)
16541654

@@ -1676,7 +1676,7 @@ cdef class IndexFaceSet(PrimitiveObject):
16761676
face.vertices = &dual.face_indices[ix]
16771677
ff, next_ = next(iter(dd.itervalues()))
16781678
face.vertices[0] = ff
1679-
for j from 1 <= j < face.n:
1679+
for j in range(1, face.n):
16801680
ff, next_ = dd[next_]
16811681
face.vertices[j] = ff
16821682
i += 1
@@ -1764,7 +1764,7 @@ cdef class FaceIter:
17641764
raise StopIteration
17651765
else:
17661766
face = []
1767-
for j from 0 <= j < self.set._faces[self.i].n:
1767+
for j in range(self.set._faces[self.i].n):
17681768
P = self.set.vs[self.set._faces[self.i].vertices[j]]
17691769
PyList_Append(face, (P.x, P.y, P.z))
17701770
self.i += 1

0 commit comments

Comments
 (0)