Skip to content

Commit 9d390f6

Browse files
author
Release Manager
committed
gh-35818: fully get rid of xrange() in pyx files <!-- Please provide a concise, informative and self-explanatory title. --> <!-- Don't put issue numbers in the title. Put it in the Description below. --> <!-- For example, instead of "Fixes #12345", use "Add a new method to multiply two integers" --> ### 📚 Description replace all remaining xrange by range <!-- Describe your changes here in detail. --> <!-- 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. It should be `[x]` not `[x ]`. --> - [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: #35818 Reported by: Frédéric Chapoton Reviewer(s): Matthias Köppe
2 parents 8341449 + e61fac9 commit 9d390f6

32 files changed

+94
-94
lines changed

src/sage/algebras/letterplace/free_algebra_letterplace.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ cdef class FreeAlgebra_letterplace(Algebra):
594594
cdef list tmp
595595
for i from 0<=i<nblocks:
596596
base = i*ngens
597-
tmp = [(j,E[base+j]) for j in xrange(ngens) if E[base+j]]
597+
tmp = [(j,E[base+j]) for j in range(ngens) if E[base+j]]
598598
if not tmp:
599599
continue
600600
var_ind, exp = tmp[0]
@@ -627,7 +627,7 @@ cdef class FreeAlgebra_letterplace(Algebra):
627627
cdef list names = self.latex_variable_names()
628628
for i from 0<=i<nblocks:
629629
base = i*ngens
630-
tmp = [(j,E[base+j]) for j in xrange(ngens) if E[base+j]]
630+
tmp = [(j,E[base+j]) for j in range(ngens) if E[base+j]]
631631
if not tmp:
632632
continue
633633
var_ind, exp = tmp[0]

src/sage/algebras/quatalg/quaternion_algebra_cython.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ def rational_quaternions_from_integral_matrix_and_denom(A, Matrix_integer_dense
226226
mpz_init(tmp)
227227

228228
if reverse:
229-
rng = xrange(H.nrows()-1, -1, -1)
229+
rng = range(H.nrows()-1, -1, -1)
230230
else:
231-
rng = xrange(H.nrows())
231+
rng = range(H.nrows())
232232

233233
for i in rng:
234234
x = <QuaternionAlgebraElement_rational_field> QuaternionAlgebraElement_rational_field.__new__(QuaternionAlgebraElement_rational_field)

src/sage/graphs/convexity_properties.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ cdef class ConvexityProperties:
435435
p.add_variables(self._n, 0, None, True, False, False, 1, None)
436436

437437
# We know that at least 2 vertices are required to cover the whole graph
438-
p.add_linear_constraint([(i, 1) for i in xrange(self._n)], 2, None)
438+
p.add_linear_constraint([(i, 1) for i in range(self._n)], 2, None)
439439

440440
# The set of vertices generated by the current LP solution
441441
cdef bitset_t current_hull

src/sage/graphs/generic_graph_pyx.pyx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ def length_and_string_from_graph6(s):
529529
else: # only first byte is N
530530
o = ord(s[0])
531531
if o > 126 or o < 63:
532-
raise RuntimeError("the string seems corrupt: valid characters are \n" + ''.join(chr(i) for i in xrange(63, 127)))
532+
raise RuntimeError("the string seems corrupt: valid characters are \n" + ''.join(chr(i) for i in range(63, 127)))
533533
n = o - 63
534534
s = s[1:]
535535
return n, s
@@ -568,7 +568,7 @@ def binary_string_from_graph6(s, n):
568568
for i in range(len(s)):
569569
o = ord(s[i])
570570
if o > 126 or o < 63:
571-
raise RuntimeError("the string seems corrupt: valid characters are \n" + ''.join(chr(i) for i in xrange(63, 127)))
571+
raise RuntimeError("the string seems corrupt: valid characters are \n" + ''.join(chr(i) for i in range(63, 127)))
572572
a = int_to_binary_string(o - 63)
573573
l.append('0'*(6 - len(a)) + a)
574574
m = "".join(l)
@@ -606,7 +606,7 @@ def binary_string_from_dig6(s, n):
606606
for i in range(len(s)):
607607
o = ord(s[i])
608608
if o > 126 or o < 63:
609-
raise RuntimeError("the string seems corrupt: valid characters are \n" + ''.join(chr(i) for i in xrange(63, 127)))
609+
raise RuntimeError("the string seems corrupt: valid characters are \n" + ''.join(chr(i) for i in range(63, 127)))
610610
a = int_to_binary_string(o - 63)
611611
l.append('0'*(6 - len(a)) + a)
612612
m = "".join(l)
@@ -862,14 +862,14 @@ cdef class SubgraphSearch:
862862

863863
# line_h_out[i] represents the adjacency sequence of vertex i
864864
# in h relative to vertices 0, 1, ..., i-1
865-
for i in xrange(self.nh):
865+
for i in range(self.nh):
866866
self.line_h_out[i] = self.line_h_out[0] + i*self.nh
867867
self.h.adjacency_sequence_out(i, self.vertices, i, self.line_h_out[i])
868868

869869
# Similarly in the opposite direction (only useful if the
870870
# graphs are directed)
871871
if self.directed:
872-
for i in xrange(self.nh):
872+
for i in range(self.nh):
873873
self.line_h_in[i] = self.line_h_in[0] + i*self.nh
874874
self.h.adjacency_sequence_in(i, self.vertices, i, self.line_h_in[i])
875875

@@ -935,7 +935,7 @@ cdef class SubgraphSearch:
935935
if self.active == self.nh-1:
936936
sig_off()
937937
return [self.g_vertices[self.stack[l]]
938-
for l in xrange(self.nh)]
938+
for l in range(self.nh)]
939939

940940
# We are still missing several vertices ...
941941
else:

src/sage/libs/ntl/ntl_GF2X_linkage.pxi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ cdef inline int celement_cmp(GF2X_c *a, GF2X_c *b, long parent) except -2:
180180
elif diff < 0:
181181
return -1
182182
else:
183-
for i in xrange(GF2X_NumBits(a[0])-1, -1, -1):
183+
for i in range(GF2X_NumBits(a[0])-1, -1, -1):
184184
ca = GF2_conv_to_long(GF2X_coeff(a[0], i))
185185
cb = GF2_conv_to_long(GF2X_coeff(b[0], i))
186186
if ca < cb:

src/sage/libs/ntl/ntl_mat_GF2E.pyx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -726,27 +726,27 @@ cdef class ntl_mat_GF2E():
726726

727727
if not nonzero:
728728
if _density == 1.0:
729-
for i in xrange(self.x.NumRows()):
730-
for j in xrange(self.x.NumCols()):
729+
for i in range(self.x.NumRows()):
730+
for j in range(self.x.NumCols()):
731731
tmp = GF2E_random()
732732
mat_GF2E_setitem(&self.x, i, j, &tmp)
733733
else:
734-
for i in xrange(self.x.NumRows()):
735-
for j in xrange(self.x.NumCols()):
734+
for i in range(self.x.NumRows()):
735+
for j in range(self.x.NumCols()):
736736
if rstate.c_rand_double() <= _density:
737737
tmp = GF2E_random()
738738
mat_GF2E_setitem(&self.x, i, j, &tmp)
739739
else:
740740
if _density == 1.0:
741-
for i in xrange(self.x.NumRows()):
742-
for j in xrange(self.x.NumCols()):
741+
for i in range(self.x.NumRows()):
742+
for j in range(self.x.NumCols()):
743743
tmp = GF2E_random()
744744
while GF2E_IsZero(tmp):
745745
tmp = GF2E_random()
746746
mat_GF2E_setitem(&self.x, i, j, &tmp)
747747
else:
748-
for i in xrange(self.x.NumRows()):
749-
for j in xrange(self.x.NumCols()):
748+
for i in range(self.x.NumRows()):
749+
for j in range(self.x.NumCols()):
750750
if rstate.c_rand_double() <= _density:
751751
tmp = GF2E_random()
752752
while GF2E_IsZero(tmp):

src/sage/matrix/matrix0.pyx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3760,7 +3760,7 @@ cdef class Matrix(sage.structure.element.Matrix):
37603760
else:
37613761
L.extend( L_prime )
37623762
if return_diag:
3763-
return [d[i] for i in xrange(self._nrows)]
3763+
return [d[i] for i in range(self._nrows)]
37643764
else:
37653765
return True
37663766

@@ -4685,7 +4685,7 @@ cdef class Matrix(sage.structure.element.Matrix):
46854685

46864686
X = set(self.pivots())
46874687
np = []
4688-
for j in xrange(self.ncols()):
4688+
for j in range(self.ncols()):
46894689
if j not in X:
46904690
np.append(j)
46914691
np = tuple(np)
@@ -5073,7 +5073,7 @@ cdef class Matrix(sage.structure.element.Matrix):
50735073
raise ArithmeticError("number of rows of matrix must equal degree of vector")
50745074
cdef Py_ssize_t i
50755075
return sum([v[i] * self.row(i, from_list=True)
5076-
for i in xrange(self._nrows)], M(0))
5076+
for i in range(self._nrows)], M(0))
50775077

50785078
cdef _matrix_times_vector_(self, Vector v):
50795079
"""
@@ -5107,7 +5107,7 @@ cdef class Matrix(sage.structure.element.Matrix):
51075107
raise ArithmeticError("number of columns of matrix must equal degree of vector")
51085108
cdef Py_ssize_t i
51095109
return sum([self.column(i, from_list=True) * v[i]
5110-
for i in xrange(self._ncols)], M(0))
5110+
for i in range(self._ncols)], M(0))
51115111

51125112
def iterates(self, v, n, rows=True):
51135113
r"""

src/sage/matrix/matrix2.pyx

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3171,24 +3171,24 @@ cdef class Matrix(Matrix1):
31713171
A = [R.zero()] * n
31723172

31733173
F[0] = - M.get_unsafe(0, 0)
3174-
for t in xrange(1,n):
3174+
for t in range(1,n):
31753175

31763176
# Set a(1, t) to be M(<=t, t)
31773177
#
3178-
for i in xrange(t+1):
3178+
for i in range(t+1):
31793179
a.set_unsafe(0, i, M.get_unsafe(i, t))
31803180

31813181
# Set A[1, t] to be the (t)th entry in a[1, t]
31823182
#
31833183
A[0] = M.get_unsafe(t, t)
31843184

3185-
for p in xrange(1, t):
3185+
for p in range(1, t):
31863186

31873187
# Set a(p, t) to the product of M[<=t, <=t] * a(p-1, t)
31883188
#
3189-
for i in xrange(t+1):
3189+
for i in range(t+1):
31903190
s = R.zero()
3191-
for j in xrange(t+1):
3191+
for j in range(t+1):
31923192
s = s + M.get_unsafe(i, j) * a.get_unsafe(p-1, j)
31933193
a.set_unsafe(p, i, s)
31943194

@@ -3199,13 +3199,13 @@ cdef class Matrix(Matrix1):
31993199
# Set A[t, t] to be M[t, <=t] * a(p-1, t)
32003200
#
32013201
s = R.zero()
3202-
for j in xrange(t+1):
3202+
for j in range(t+1):
32033203
s = s + M.get_unsafe(t, j) * a.get_unsafe(t-1, j)
32043204
A[t] = s
32053205

3206-
for p in xrange(t+1):
3206+
for p in range(t+1):
32073207
s = F[p]
3208-
for k in xrange(p):
3208+
for k in range(p):
32093209
s = s - A[k] * F[p-k-1]
32103210
F[p] = s - A[p]
32113211

@@ -6690,7 +6690,7 @@ cdef class Matrix(Matrix1):
66906690
warn("Using generic algorithm for an inexact ring, which will probably give incorrect results due to numerical precision issues.")
66916691

66926692
if not extend:
6693-
return Sequence(r for r,m in self.charpoly().roots() for _ in xrange(m))
6693+
return Sequence(r for r,m in self.charpoly().roots() for _ in range(m))
66946694

66956695
# now we need to find a natural algebraic closure for the base ring
66966696
K = self.base_ring()
@@ -11329,7 +11329,7 @@ cdef class Matrix(Matrix1):
1132911329
C = B*C
1133011330
ranks.append(C.rank())
1133111331
i += 1
11332-
diagram = [ranks[i]-ranks[i+1] for i in xrange(len(ranks)-1)]
11332+
diagram = [ranks[i]-ranks[i+1] for i in range(len(ranks)-1)]
1133311333
blocks.extend([(eval, i)
1133411334
for i in Partition(diagram).conjugate()])
1133511335

@@ -15474,7 +15474,7 @@ cdef class Matrix(Matrix1):
1547415474
"""
1547515475
d = self.smith_form(transformation=False)
1547615476
r = min(self.nrows(), self.ncols())
15477-
return [d[i,i] for i in xrange(r)]
15477+
return [d[i,i] for i in range(r)]
1547815478

1547915479
def smith_form(self, transformation=True, integral=None, exact=True):
1548015480
r"""
@@ -16005,7 +16005,7 @@ cdef class Matrix(Matrix1):
1600516005

1600616006

1600716007
try:
16008-
for i in xrange(1, len(pivs)):
16008+
for i in range(1, len(pivs)):
1600916009
y = a[i][pivs[i]]
1601016010
I = ideal_or_fractional(R, y)
1601116011
s = a[0][pivs[i]]
@@ -17747,7 +17747,7 @@ def _smith_diag(d, transformation=True):
1774717747
right = d.new_matrix(d.ncols(), d.ncols(), 1)
1774817748
else:
1774917749
left = right = None
17750-
for i in xrange(n):
17750+
for i in range(n):
1775117751
I = ideal_or_fractional(R, dp[i,i])
1775217752

1775317753
if I == ideal_or_fractional(R, 1):
@@ -17757,7 +17757,7 @@ def _smith_diag(d, transformation=True):
1775717757
dp[i,i] = R(1)
1775817758
continue
1775917759

17760-
for j in xrange(i+1,n):
17760+
for j in range(i+1,n):
1776117761
if dp[j,j] not in I:
1776217762
t = ideal_or_fractional(R, [dp[i,i], dp[j,j]]).gens_reduced()
1776317763
if len(t) > 1:
@@ -17842,7 +17842,7 @@ def _generic_clear_column(m):
1784217842
# is invertible over R
1784317843

1784417844
I = ideal_or_fractional(R, a[0, 0]) # need to make sure we change this when a[0,0] changes
17845-
for k in xrange(1, a.nrows()):
17845+
for k in range(1, a.nrows()):
1784617846
if a[k,0] not in I:
1784717847
try:
1784817848
v = ideal_or_fractional(R, a[0,0], a[k,0]).gens_reduced()
@@ -17889,7 +17889,7 @@ def _generic_clear_column(m):
1788917889
raise ArithmeticError
1789017890

1789117891
# now everything in column 0 is divisible by the pivot
17892-
for i in xrange(1,a.nrows()):
17892+
for i in range(1,a.nrows()):
1789317893
s = R( a[i, 0]/a[0, 0])
1789417894
a.add_multiple_of_row(i, 0, -s )
1789517895
left_mat.add_multiple_of_row(i, 0, -s)
@@ -17942,7 +17942,7 @@ def _smith_onestep(m):
1794217942

1794317943
# test if everything to the right of the pivot in row 0 is good as well
1794417944
isdone = True
17945-
for jj in xrange(j+1, a.ncols()):
17945+
for jj in range(j+1, a.ncols()):
1794617946
if a[0,jj] != 0:
1794717947
isdone = False
1794817948

src/sage/matrix/matrix_integer_dense.pyx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2454,10 +2454,10 @@ cdef class Matrix_integer_dense(Matrix_dense):
24542454
24552455
:meth:`elementary_divisors`
24562456
"""
2457-
X = self.matrix_space()([self[i,j] for i in xrange(self._nrows-1,-1,-1) for j in xrange(self._ncols-1,-1,-1)])
2457+
X = self.matrix_space()([self[i,j] for i in range(self._nrows-1,-1,-1) for j in range(self._ncols-1,-1,-1)])
24582458
v = X.__pari__().matsnf(1).sage()
24592459
# need to reverse order of rows of U, columns of V, and both of D.
2460-
D = self.matrix_space()([v[2][i,j] for i in xrange(self._nrows-1,-1,-1) for j in xrange(self._ncols-1,-1,-1)])
2460+
D = self.matrix_space()([v[2][i,j] for i in range(self._nrows-1,-1,-1) for j in range(self._ncols-1,-1,-1)])
24612461

24622462
if not transformation:
24632463
return D
@@ -2471,13 +2471,13 @@ cdef class Matrix_integer_dense(Matrix_dense):
24712471
# silly special cases for matrices with 0 columns (PARI has a unique empty matrix)
24722472
U = self.matrix_space(ncols = self._nrows)(1)
24732473
else:
2474-
U = self.matrix_space(ncols = self._nrows)([v[0][i,j] for i in xrange(self._nrows-1,-1,-1) for j in xrange(self._nrows-1,-1,-1)])
2474+
U = self.matrix_space(ncols = self._nrows)([v[0][i,j] for i in range(self._nrows-1,-1,-1) for j in range(self._nrows-1,-1,-1)])
24752475

24762476
if self._nrows == 0:
24772477
# silly special cases for matrices with 0 rows (PARI has a unique empty matrix)
24782478
V = self.matrix_space(nrows = self._ncols)(1)
24792479
else:
2480-
V = self.matrix_space(nrows = self._ncols)([v[1][i,j] for i in xrange(self._ncols-1,-1,-1) for j in xrange(self._ncols-1,-1,-1)])
2480+
V = self.matrix_space(nrows = self._ncols)([v[1][i,j] for i in range(self._ncols-1,-1,-1) for j in range(self._ncols-1,-1,-1)])
24812481

24822482
return D, U, V
24832483

@@ -3310,7 +3310,7 @@ cdef class Matrix_integer_dense(Matrix_dense):
33103310

33113311
#For any $i<d$, we have $\delta |b_i^*|^2 <= |b_{i+1}^* + mu_{i+1, i} b_i^* |^2$
33123312
norms = [G[i].norm()**2 for i in range(len(G))]
3313-
for i in xrange(1,self.nrows()):
3313+
for i in range(1,self.nrows()):
33143314
if norms[i] < (delta - mu[i,i-1]**2) * norms[i-1]:
33153315
return False
33163316
return True

src/sage/matrix/matrix_integer_sparse.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ cdef class Matrix_integer_sparse(Matrix_sparse):
348348
if copy:
349349
return list(x)
350350
return x
351-
nzc = [[] for _ in xrange(self._ncols)]
351+
nzc = [[] for _ in range(self._ncols)]
352352
cdef Py_ssize_t i, j
353353
for i from 0 <= i < self._nrows:
354354
for j from 0 <= j < self._matrix[i].num_nonzero:

0 commit comments

Comments
 (0)