Skip to content

Commit d4706f9

Browse files
author
Release Manager
committed
Trac #34019: minor code details in combinat
URL: https://trac.sagemath.org/34019 Reported by: chapoton Ticket author(s): Frédéric Chapoton Reviewer(s): David Coudert
2 parents 3d336f0 + facaba4 commit d4706f9

12 files changed

+38
-63
lines changed

src/sage/arith/misc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ def primes(start, stop=None, proof=None):
967967
argument proof controls whether the numbers returned are
968968
guaranteed to be prime or not.
969969
970-
This command is like the Python 2 ``xrange`` command, except it only iterates
970+
This command is like the Python 3 ``range`` command, except it only iterates
971971
over primes. In some cases it is better to use primes than
972972
``prime_range``, because primes does not build a list of all primes in
973973
the range in memory all at once. However, it is potentially much

src/sage/combinat/binary_recurrence_sequences.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,7 @@ def period(self, m):
415415
#the identity mod p is of order (p^{e-1})^4. So we compute the period mod p^e by successively
416416
#multiplying the period mod p by powers of p.
417417

418-
for i in Fac:
419-
p = i[0]
420-
e = i[1]
418+
for p, e in Fac:
421419
#first compute the period mod p
422420
if p in self._period_dict:
423421
perp = self._period_dict[p]

src/sage/combinat/cluster_algebra_quiver/cluster_seed.py

+10-18
Original file line numberDiff line numberDiff line change
@@ -2074,39 +2074,34 @@ def highest_degree_denominator(self, filter=None):
20742074
5
20752075
"""
20762076
if filter is None:
2077-
filter = list(range(len(self.cluster())))
2077+
filter = range(len(self.cluster()))
20782078
degree = 0
20792079
vertex_to_mutate = []
20802080

20812081
# if we have d vectors use those, else see if we have clusters
20822082
if self._use_d_vec:
2083-
for i in list(enumerate(self.d_matrix().columns())):
2084-
if i[0] not in filter:
2083+
for vertex, col in enumerate(self.d_matrix().columns()):
2084+
if vertex not in filter:
20852085
continue
2086-
col = i[1]
2087-
vertex = i[0]
20882086
cur_vertex_degree = sum(col)
20892087
if degree == cur_vertex_degree:
20902088
vertex_to_mutate.append(vertex)
2091-
if degree < cur_vertex_degree:
2089+
elif degree < cur_vertex_degree:
20922090
degree = cur_vertex_degree
20932091
vertex_to_mutate = [vertex]
20942092
elif self._use_fpolys:
2095-
for i in list(enumerate(self.cluster())):
2096-
if i[0] not in filter:
2093+
for vertex, vari in enumerate(self.cluster()):
2094+
if vertex not in filter:
20972095
continue
2098-
vari = i[1]
2099-
vertex = i[0]
21002096
denom = vari.denominator()
21012097
cur_vertex_degree = denom.degree()
21022098
if degree == cur_vertex_degree:
21032099
vertex_to_mutate.append(vertex)
2104-
if degree < cur_vertex_degree:
2100+
elif degree < cur_vertex_degree:
21052101
degree = cur_vertex_degree
21062102
vertex_to_mutate = [vertex]
21072103

2108-
2109-
return_key = randint(0,len(vertex_to_mutate) - 1)
2104+
return_key = randint(0, len(vertex_to_mutate) - 1)
21102105
return vertex_to_mutate[return_key]
21112106

21122107
def smallest_c_vector(self):
@@ -2848,14 +2843,11 @@ def mutation_analysis(self, options=['all'], filter=None):
28482843
'sources_diff': {'added': [], 'removed': [5]},
28492844
'urban_renewals': [],
28502845
'urban_renewals_diff': {'added': [], 'removed': []}}}
2851-
28522846
"""
2853-
2854-
V = list(range(self._n))
2855-
2847+
V = range(self._n)
28562848
if filter is None:
28572849
filter = V
2858-
if filter in V:
2850+
elif filter in V:
28592851
filter = [filter]
28602852

28612853
# setup our initial information for differences later on

src/sage/combinat/crystals/letters.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,12 @@ class ClassicalCrystalOfLetters(UniqueRepresentation, Parent):
187187
self._element_constructor_(-1)]
188188
else:
189189
self._list = [self._element_constructor_(i)
190-
for i in xrange(1, cartan_type.rank() + 1)]
190+
for i in range(1, cartan_type.rank() + 1)]
191191
if cartan_type.type() == 'B':
192192
self._list.append(self._element_constructor_(0))
193193
if cartan_type.type() != 'A':
194194
self._list += [self._element_constructor_(-i)
195-
for i in xrange(cartan_type.rank(), 0, -1)]
195+
for i in range(cartan_type.rank(), 0, -1)]
196196
else:
197197
self._list.append(self._element_constructor_(cartan_type.rank() + 1))
198198
self._element_print_style = element_print_style

src/sage/combinat/designs/orthogonal_arrays_find_recursive.pyx

+9-7
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,17 @@ cpdef find_wilson_decomposition_with_two_truncated_groups(int k,int n):
231231
sage: _ = f(*args)
232232
"""
233233
cdef int r,m_min,m_max,m,r1_min,r1_max,r1,r2,r1_p_r2
234-
for r in [1] + list(xrange(k+1, n-2)): # as r*1+1+1 <= n and because we need
235-
# an OA(k+2,r), necessarily r=1 or r >= k+1
234+
for r in [1] + list(range(k+1, n-2)):
235+
# as r*1+1+1 <= n and because we need
236+
# an OA(k+2,r), necessarily r=1 or r >= k+1
236237
if not is_available(k+2,r):
237238
continue
238239
m_min = (n - (2*r-2))/r
239240
m_max = (n - 2)/r
240241
if m_min > 1:
241-
m_values = list(xrange(max(m_min, k - 1), m_max + 1))
242+
m_values = list(range(max(m_min, k - 1), m_max + 1))
242243
else:
243-
m_values = [1] + list(xrange(k - 1, m_max + 1))
244+
m_values = [1] + list(range(k - 1, m_max + 1))
244245
for m in m_values:
245246
r1_p_r2 = n-r*m # the sum of r1+r2
246247
# it is automatically >= 2 since m <= m_max
@@ -253,9 +254,9 @@ cpdef find_wilson_decomposition_with_two_truncated_groups(int k,int n):
253254
r1_min = r1_p_r2 - (r-1)
254255
r1_max = min(r-1, r1_p_r2)
255256
if r1_min > 1:
256-
r1_values = list(xrange(max(k - 1, r1_min), r1_max + 1))
257+
r1_values = range(max(k - 1, r1_min), r1_max + 1)
257258
else:
258-
r1_values = [1] + list(xrange(k-1, r1_max + 1))
259+
r1_values = [1] + list(range(k-1, r1_max + 1))
259260
for r1 in r1_values:
260261
if not is_available(k,r1):
261262
continue
@@ -589,7 +590,8 @@ cpdef find_thwart_lemma_3_5(int k,int N):
589590
continue
590591

591592
NN = N - n*m
592-
for a in range(max(0, (NN-n+2)/3), min(n, NN)+1): # (NN-n+2)/3 <==> ceil((NN-n)/3)
593+
for a in range(max(0, (NN-n+2)/3), min(n, NN)+1):
594+
# (NN-n+2)/3 <==> ceil((NN-n)/3)
593595
if not is_available(k,a):
594596
continue
595597
na = n-a

src/sage/combinat/permutation_cython.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ cpdef bint next_perm(array l):
248248
#mset_list = mset_list[:two] + [x for x in reversed(mset_list[two:])]
249249
n -= 1 # In the loop, we only need n-1, so just do it once here
250250
cdef Py_ssize_t i
251-
for i in xrange((n+1 - two) // 2 - 1, -1, -1):
251+
for i in range((n + 1 - two) // 2 - 1, -1, -1):
252252
t = l.data.as_uints[i + two]
253253
l.data.as_uints[i + two] = l.data.as_uints[n - i]
254254
l.data.as_uints[n - i] = t

src/sage/combinat/root_system/reflection_group_element.pyx

+5-4
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ cdef class ComplexReflectionGroupElement(PermutationGroupElement):
657657
cdef list M_gals = [x.galois_conjugates(m) if hasattr(x,"galois_conjugates") else [x] for x in M]
658658
cdef list conjugates = []
659659
cdef int i
660-
for i in xrange(len(M_gals[0])):
660+
for i in range(len(M_gals[0])):
661661
conjugates.append(Matrix(rk, [X[i] for X in M_gals]))
662662
return conjugates
663663

@@ -977,12 +977,12 @@ cdef class RealReflectionGroupElement(ComplexReflectionGroupElement):
977977
cdef int j
978978
ret = Phi[0].parent().zero()
979979
if on_space == "primal":
980-
for j in xrange(n):
980+
for j in range(n):
981981
ret += vec[j] * Phi[w.perm[j]]
982982
return ret
983983
elif on_space == "dual":
984984
w = <RealReflectionGroupElement>(~w)
985-
for j in xrange(n):
985+
for j in range(n):
986986
ret += Phi[w.perm[j]] * vec[j]
987987
return ret
988988
else:
@@ -1122,7 +1122,8 @@ cdef class RealReflectionGroupElement(ComplexReflectionGroupElement):
11221122
self = <RealReflectionGroupElement>(~self)
11231123
elif side != "right":
11241124
raise ValueError('side must be "left" or "right"')
1125-
return [Phi[i] for i in xrange(N) if self.perm[i] >= N]
1125+
return [Phi[i] for i in range(N) if self.perm[i] >= N]
1126+
11261127

11271128
def _gap_factorization(w, gens):
11281129
r"""

src/sage/combinat/subword_complex.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,8 @@ def __contains__(self, F):
12421242
"""
12431243
W = self.group()
12441244
Q = self.word()
1245-
if not all(i in list(range(len(Q))) for i in F):
1245+
r = range(len(Q))
1246+
if not all(i in r for i in F):
12461247
return False
12471248
return W.from_reduced_word(Qi for i, Qi in enumerate(Q) if i not in F) == self.pi()
12481249

src/sage/combinat/subword_complex_c.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ cpdef int _flip_c(W, set positions, list extended_root_conf_indices,
4242
nr_ref = W.number_of_reflections()
4343
r_minus = (r + nr_ref) % (2 * nr_ref) # get the negative root -r
4444
j = i
45-
for k in xrange(len(extended_root_conf_indices)):
45+
for k in range(len(extended_root_conf_indices)):
4646
if extended_root_conf_indices[k] == r_minus and k not in positions and side != "positive":
4747
j = k
4848
break
@@ -54,7 +54,7 @@ cpdef int _flip_c(W, set positions, list extended_root_conf_indices,
5454
R = list(W.reflections())
5555
if j != i:
5656
t = R[min(r, r_minus)]
57-
for k in xrange(min(i, j) + 1, max(i, j) + 1):
57+
for k in range(min(i, j) + 1, max(i, j) + 1):
5858
extended_root_conf_indices[k] = t.action_on_root_indices(extended_root_conf_indices[k],side="left")
5959
return j
6060

@@ -89,7 +89,7 @@ cpdef list _construct_facets_c(tuple Q, w, int n=-1, int pos=0, int l=-1):
8989
first = False
9090

9191
if l == 0:
92-
return [list(xrange(pos, n))]
92+
return [list(range(pos, n))]
9393
elif n < l + pos:
9494
return []
9595

src/sage/combinat/tutorial.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -1243,9 +1243,6 @@
12431243
12441244
sage: import itertools
12451245
1246-
The behaviour of this library has changed a lot between Python 2 and
1247-
Python 3. What follows is mostly written for Python 2.
1248-
12491246
We will demonstrate some applications, taking as a starting point the
12501247
permutations of `3`::
12511248
@@ -1268,12 +1265,12 @@
12681265
12691266
To apply a function to all the elements, one can do::
12701267
1271-
sage: list(z.cycle_type() for z in Permutations(3))
1268+
sage: [z.cycle_type() for z in Permutations(3)]
12721269
[[1, 1, 1], [2, 1], [2, 1], [3], [3], [2, 1]]
12731270
12741271
and similarly to select the elements satisfying a certain condition::
12751272
1276-
sage: list(z for z in Permutations(3) if z.has_pattern([1,2]))
1273+
sage: [z for z in Permutations(3) if z.has_pattern([1,2])]
12771274
[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2]]
12781275
12791276
Implementation of new iterators

src/sage/rings/polynomial/pbori/pbori.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1851,7 +1851,7 @@ def get_var_mapping(ring, other):
18511851
"""
18521852
my_names = list(ring._names) # we need .index(.)
18531853
if isinstance(other, (Parent, BooleanMonomialMonoid)):
1854-
indices = list(range(other.ngens()))
1854+
indices = range(other.ngens())
18551855
ovar_names = other._names
18561856
else:
18571857
ovar_names = other.parent().variable_names()
@@ -1862,7 +1862,7 @@ def get_var_mapping(ring, other):
18621862
else:
18631863
t = other.variables()
18641864
ovar_names = list(ovar_names)
1865-
indices = [ovar_names.index(str(var)) for var in t]
1865+
indices = (ovar_names.index(str(var)) for var in t)
18661866

18671867
var_mapping = [None] * len(ovar_names)
18681868
for idx in indices:

src/sage/rings/polynomial/weil/weil_polynomials.pyx

-16
Original file line numberDiff line numberDiff line change
@@ -367,22 +367,6 @@ class WeilPolynomials_iter():
367367
raise StopIteration
368368
return self.pol(self.ans.pop())
369369

370-
def next(self): # For Python2 backward compatibility
371-
r"""
372-
Step the iterator forward.
373-
374-
Included for Python2 backward compatibility.
375-
376-
EXAMPLES::
377-
378-
sage: from sage.rings.polynomial.weil.weil_polynomials import WeilPolynomials
379-
sage: w = WeilPolynomials(10,1,sign=1,lead=[3,1,1])
380-
sage: it = iter(w)
381-
sage: next(it)
382-
3*x^10 + x^9 + x^8 + 7*x^7 + 5*x^6 + 2*x^5 + 5*x^4 + 7*x^3 + x^2 + x + 3
383-
"""
384-
return self.__next__()
385-
386370
def node_count(self):
387371
r"""
388372
Return the number of terminal nodes found in the tree, excluding

0 commit comments

Comments
 (0)