Skip to content

Commit 1a76c80

Browse files
author
Release Manager
committed
gh-36142: python-style loops in `sage.graphs` Convert the loops of the form `for x from 0 <= x < n` in `sage.graphs` to python-style loops. ### 📝 Checklist - [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: #36142 Reported by: David Coudert Reviewer(s): David Coudert, Frédéric Chapoton
2 parents 37377bc + a179563 commit 1a76c80

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/sage/graphs/chrompoly.pyx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def chromatic_polynomial(G, return_tree_basis=False, algorithm='C', cache=None):
185185
# Breadth first search from 0:
186186
bfs_reorder[0] = 0
187187
mpz_init(tot[0]) # sets to 0
188-
for i from 0 < i < nverts:
188+
for i in range(1, nverts):
189189
bfs_reorder[i] = -1
190190
mpz_init(tot[i]) # sets to 0
191191
mpz_init(tot[nverts]) # sets to 0
@@ -230,12 +230,12 @@ def chromatic_polynomial(G, return_tree_basis=False, algorithm='C', cache=None):
230230
for i in range(nverts):
231231
mpz_clear(tot[i])
232232
raise
233-
for i from 0 <= i <= nverts:
233+
for i in range(nverts + 1):
234234
mpz_init(coeffs[i]) # also sets them to 0
235235
mpz_init(coeff)
236236
mpz_init_set_si(m, -1)
237237
# start with the zero polynomial: f(x) = 0
238-
for i from nverts >= i > 0:
238+
for i in range(nverts, 0, -1): # nverts >= i > 0
239239
if not mpz_sgn(tot[i]):
240240
continue
241241
mpz_neg(m, m)
@@ -244,7 +244,7 @@ def chromatic_polynomial(G, return_tree_basis=False, algorithm='C', cache=None):
244244
# f += tot[i]*m*x*(x-1)**(i-1)
245245
mpz_addmul(coeffs[i], m, tot[i])
246246
mpz_set_si(coeff, 1)
247-
for j from 1 <= j < i:
247+
for j in range(1, i):
248248
# an iterative method for binomial coefficients...
249249
mpz_mul_si(coeff, coeff, j-i)
250250
mpz_divexact_ui(coeff, coeff, j)
@@ -254,13 +254,13 @@ def chromatic_polynomial(G, return_tree_basis=False, algorithm='C', cache=None):
254254
mpz_mul(coeff, coeff, m)
255255
coeffs_ZZ = []
256256
cdef Integer c_ZZ
257-
for i from 0 <= i <= nverts:
257+
for i in range(nverts + 1):
258258
c_ZZ = Integer(0)
259259
mpz_set(c_ZZ.value, coeffs[i])
260260
coeffs_ZZ.append(c_ZZ)
261261
f = R(coeffs_ZZ)
262262

263-
for i from 0 <= i <= nverts:
263+
for i in range(nverts + 1):
264264
mpz_clear(tot[i])
265265
mpz_clear(coeffs[i])
266266

src/sage/graphs/matchpoly.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,14 @@ def matching_polynomial(G, complement=True, name=None):
216216

217217
cdef int i, j, d
218218
cdef fmpz_poly_t pol
219-
cdef nverts = G.num_verts()
219+
cdef int nverts = G.num_verts()
220220

221221
# Using Godsil's duality theorem when the graph is dense
222222

223223
if complement and G.density() > 0.5: # this cutoff could probably be tuned
224224
f_comp = matching_polynomial(G.complement()).list()
225225
f = x.parent().zero()
226-
for i from 0 <= i <= nverts / 2: # implicit floor
226+
for i in range(nverts // 2 + 1):
227227
j = nverts - 2 * i
228228
f += complete_poly(j) * f_comp[j] * (-1)**i
229229
return f

0 commit comments

Comments
 (0)