From 79f8fe278654451196a24b7d4f775852e319166f Mon Sep 17 00:00:00 2001 From: dcoudert Date: Sat, 26 Aug 2023 15:38:52 +0200 Subject: [PATCH 1/2] python-style loops in sage.graphs --- src/sage/graphs/chrompoly.pyx | 12 ++++++------ src/sage/graphs/matchpoly.pyx | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/sage/graphs/chrompoly.pyx b/src/sage/graphs/chrompoly.pyx index 441ba58643d..d0611a3df84 100644 --- a/src/sage/graphs/chrompoly.pyx +++ b/src/sage/graphs/chrompoly.pyx @@ -185,7 +185,7 @@ def chromatic_polynomial(G, return_tree_basis=False, algorithm='C', cache=None): # Breadth first search from 0: bfs_reorder[0] = 0 mpz_init(tot[0]) # sets to 0 - for i from 0 < i < nverts: + for i in range(1, nverts): bfs_reorder[i] = -1 mpz_init(tot[i]) # sets to 0 mpz_init(tot[nverts]) # sets to 0 @@ -230,12 +230,12 @@ def chromatic_polynomial(G, return_tree_basis=False, algorithm='C', cache=None): for i in range(nverts): mpz_clear(tot[i]) raise - for i from 0 <= i <= nverts: + for i in range(nverts + 1): mpz_init(coeffs[i]) # also sets them to 0 mpz_init(coeff) mpz_init_set_si(m, -1) # start with the zero polynomial: f(x) = 0 - for i from nverts >= i > 0: + for i in range(nverts, 0, -1): # nverts >= i > 0 if not mpz_sgn(tot[i]): continue mpz_neg(m, m) @@ -244,7 +244,7 @@ def chromatic_polynomial(G, return_tree_basis=False, algorithm='C', cache=None): # f += tot[i]*m*x*(x-1)**(i-1) mpz_addmul(coeffs[i], m, tot[i]) mpz_set_si(coeff, 1) - for j from 1 <= j < i: + for j in range(1, i): # an iterative method for binomial coefficients... mpz_mul_si(coeff, coeff, j-i) mpz_divexact_ui(coeff, coeff, j) @@ -254,13 +254,13 @@ def chromatic_polynomial(G, return_tree_basis=False, algorithm='C', cache=None): mpz_mul(coeff, coeff, m) coeffs_ZZ = [] cdef Integer c_ZZ - for i from 0 <= i <= nverts: + for i in range(nverts + 1): c_ZZ = Integer(0) mpz_set(c_ZZ.value, coeffs[i]) coeffs_ZZ.append(c_ZZ) f = R(coeffs_ZZ) - for i from 0 <= i <= nverts: + for i in range(nverts + 1): mpz_clear(tot[i]) mpz_clear(coeffs[i]) diff --git a/src/sage/graphs/matchpoly.pyx b/src/sage/graphs/matchpoly.pyx index e98cc8d6b63..c50acec5152 100644 --- a/src/sage/graphs/matchpoly.pyx +++ b/src/sage/graphs/matchpoly.pyx @@ -223,7 +223,7 @@ def matching_polynomial(G, complement=True, name=None): if complement and G.density() > 0.5: # this cutoff could probably be tuned f_comp = matching_polynomial(G.complement()).list() f = x.parent().zero() - for i from 0 <= i <= nverts / 2: # implicit floor + for i in range(nverts / 2 + 1): # implicit floor j = nverts - 2 * i f += complete_poly(j) * f_comp[j] * (-1)**i return f From f4609f6cee4041abe4abf4cdc7e633d21b5ef938 Mon Sep 17 00:00:00 2001 From: dcoudert Date: Sun, 27 Aug 2023 09:22:01 +0200 Subject: [PATCH 2/2] review comment --- src/sage/graphs/matchpoly.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sage/graphs/matchpoly.pyx b/src/sage/graphs/matchpoly.pyx index c50acec5152..22361379291 100644 --- a/src/sage/graphs/matchpoly.pyx +++ b/src/sage/graphs/matchpoly.pyx @@ -216,14 +216,14 @@ def matching_polynomial(G, complement=True, name=None): cdef int i, j, d cdef fmpz_poly_t pol - cdef nverts = G.num_verts() + cdef int nverts = G.num_verts() # Using Godsil's duality theorem when the graph is dense if complement and G.density() > 0.5: # this cutoff could probably be tuned f_comp = matching_polynomial(G.complement()).list() f = x.parent().zero() - for i in range(nverts / 2 + 1): # implicit floor + for i in range(nverts // 2 + 1): j = nverts - 2 * i f += complete_poly(j) * f_comp[j] * (-1)**i return f