Skip to content

Commit 6f63102

Browse files
author
Release Manager
committed
Trac #34847: modernize some for loops in cython files
changing things like {{{ for i from 0 <= i < n }}} to {{{ for i in range(n) }}} There remains many, as can be seen using {{{ git grep " from .*<=" src/sage/ }}} URL: https://trac.sagemath.org/34847 Reported by: chapoton Ticket author(s): Frédéric Chapoton Reviewer(s): David Coudert
2 parents 9ffabc7 + 16ef533 commit 6f63102

File tree

10 files changed

+44
-45
lines changed

10 files changed

+44
-45
lines changed

src/sage/combinat/expnums.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,17 @@ def expnums(int n, int aa):
8787
mpz_init_set_si(bell[1], aa)
8888
cdef int i
8989
cdef int k
90-
for i from 1 <= i < n:
90+
for i in range(1, n):
9191
mpz_init(bell[i+1])
9292
mpz_mul(bell[i+1], a, bell[1])
93-
for k from 0 <= k < i:
93+
for k in range(i):
9494
mpz_add(bell[i-k], bell[i-k], bell[i-k+1])
9595

9696
z = Integer.__new__(Integer)
9797
mpz_set(z.value, bell[1])
9898
r.append(z)
9999

100-
for i from 1 <= i <= n:
100+
for i in range(1, n + 1):
101101
mpz_clear(bell[i])
102102
sig_free(bell)
103103

src/sage/libs/coxeter3/coxeter.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,7 @@ cdef class CoxGroupElement:
980980

981981
cdef Length i = 0
982982
cdef CoxGroupElement res
983-
for i from 0 <= i < list.size():
983+
for i in range(list.size()):
984984
res = self._new()
985985
res.word = list[i]
986986
coatoms.append(res)

src/sage/media/channels.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ def _separate_channels(_data, _width, _nchannels):
1717
if width == 1:
1818
# handle the one byte case
1919

20-
for n from 0 <= n < l:
20+
for n in range(l):
2121
channel_data[n % nchannels].append(ord(data[n])-127)
2222

2323
elif width == 2:
2424
a = 32768
25-
for n from 0 <= n < l:
25+
for n in range(l):
2626
# compute the value as an integer
2727
x = <int> (data[2*n]) + 256 * <int>(data[2*n + 1])
2828
#x -= 65536*(x > a)

src/sage/probability/probability_distribution.pyx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,10 @@ cdef class SphericalDistribution(ProbabilityDistribution):
307307
sage: T.get_random_element() # rel tol 4e-16
308308
(0.07961564104639995, -0.05237671627581255, 0.9954486572862178)
309309
"""
310-
311310
cdef int i
312311
v = [0]*self.dimension
313312
gsl_ran_dir_nd(self.r, self.dimension, self.vec)
314-
for i from 0 <= i<self.dimension:
313+
for i in range(self.dimension):
315314
v[i] = self.vec[i]
316315
return vector(sage.rings.real_double.RDF, v) #This could be made more efficient by directly constructing the vector, TODO.
317316

src/sage/quivers/algebra_elements.pxi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ cdef int negdeglex(path_mon_t M1, path_mon_t M2) except -2:
219219
if M1.s_len < M2.s_len:
220220
return -1
221221
return 1
222-
for index from 0 <= index < M1.path.length:
222+
for index in range(M1.path.length):
223223
item1 = biseq_getitem(M1.path, index)
224224
item2 = biseq_getitem(M2.path, index)
225225
sig_check()
@@ -256,7 +256,7 @@ cdef int deglex(path_mon_t M1, path_mon_t M2) except -2:
256256
if M1.s_len < M2.s_len:
257257
return 1
258258
return -1
259-
for index from 0 <= index < M1.path.length:
259+
for index in range(M1.path.length):
260260
item1 = biseq_getitem(M1.path, index)
261261
item2 = biseq_getitem(M2.path, index)
262262
sig_check()

src/sage/schemes/elliptic_curves/descent_two_isogeny.pyx

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ r"""
22
Descent on elliptic curves over `\QQ` with a 2-isogeny
33
"""
44

5-
#*****************************************************************************
5+
# ***************************************************************************
66
# Copyright (C) 2009 Robert L. Miller <[email protected]>
77
#
88
# This program is free software: you can redistribute it and/or modify
99
# it under the terms of the GNU General Public License as published by
1010
# the Free Software Foundation, either version 2 of the License, or
1111
# (at your option) any later version.
12-
# http://www.gnu.org/licenses/
13-
#*****************************************************************************
12+
# https://www.gnu.org/licenses/
13+
# ***************************************************************************
1414

1515
from cysignals.memory cimport sig_malloc, sig_free
1616
from cysignals.signals cimport sig_on, sig_off
@@ -316,7 +316,7 @@ cdef bint Zp_soluble_siksek(mpz_t a, mpz_t b, mpz_t c, mpz_t d, mpz_t e,
316316
result = 0
317317
(<nmod_poly_factor_struct *>f_factzn)[0].num = 0 # reset data struct
318318
qq = nmod_poly_factor(f_factzn, f)
319-
for i from 0 <= i < f_factzn.num:
319+
for i in range(f_factzn.num):
320320
if f_factzn.exp[i]&1:
321321
result = 1
322322
break
@@ -327,15 +327,15 @@ cdef bint Zp_soluble_siksek(mpz_t a, mpz_t b, mpz_t c, mpz_t d, mpz_t e,
327327

328328
nmod_poly_zero(f)
329329
nmod_poly_set_coeff_ui(f, 0, 1)
330-
for i from 0 <= i < f_factzn.num:
331-
for j from 0 <= j < (f_factzn.exp[i]>>1):
330+
for i in range(f_factzn.num):
331+
for j in range(f_factzn.exp[i]>>1):
332332
nmod_poly_mul(f, f, &f_factzn.p[i])
333333

334334
(<nmod_poly_factor_struct *>f_factzn)[0].num = 0 # reset data struct
335335
nmod_poly_factor(f_factzn, f)
336336
has_roots = 0
337337
j = 0
338-
for i from 0 <= i < f_factzn.num:
338+
for i in range(f_factzn.num):
339339
if nmod_poly_degree(&f_factzn.p[i]) == 1 and 0 != nmod_poly_get_coeff_ui(&f_factzn.p[i], 1):
340340
has_roots = 1
341341
roots[j] = pp_ui - nmod_poly_get_coeff_ui(&f_factzn.p[i], 0)
@@ -393,7 +393,7 @@ cdef bint Zp_soluble_siksek(mpz_t a, mpz_t b, mpz_t c, mpz_t d, mpz_t e,
393393

394394
result = 0
395395
mpz_init(tt)
396-
for i from 0 <= i < j:
396+
for i in range(j):
397397
mpz_mul_ui(tt, aaa, roots[i])
398398
mpz_add(tt, tt, bbb)
399399
mpz_mul_ui(tt, tt, roots[i])
@@ -454,7 +454,7 @@ cdef bint Zp_soluble_siksek(mpz_t a, mpz_t b, mpz_t c, mpz_t d, mpz_t e,
454454
has_roots = 0
455455
has_single_roots = 0
456456
j = 0
457-
for i from 0 <= i < f_factzn.num:
457+
for i in range(f_factzn.num):
458458
if nmod_poly_degree(&f_factzn.p[i]) == 1 and 0 != nmod_poly_get_coeff_ui(&f_factzn.p[i], 1):
459459
has_roots = 1
460460
if f_factzn.exp[i] == 1:
@@ -473,7 +473,7 @@ cdef bint Zp_soluble_siksek(mpz_t a, mpz_t b, mpz_t c, mpz_t d, mpz_t e,
473473
mpz_init(cc)
474474
mpz_init(dd)
475475
mpz_init(ee)
476-
for i from 0 <= i < j:
476+
for i in range(j):
477477
fmpz_poly_zero(f1)
478478
fmpz_poly_zero(linear)
479479
fmpz_poly_set_coeff_mpz(f1, 0, e)
@@ -558,7 +558,7 @@ cdef bint Zp_soluble_siksek_large_p(mpz_t a, mpz_t b, mpz_t c, mpz_t d, mpz_t e,
558558

559559
f = ntl.ZZ_pX([1], P)
560560
for factor, exponent in f_factzn:
561-
for j from 0 <= j < (exponent/2):
561+
for j in range(exponent // 2):
562562
f *= factor
563563

564564
f /= f.leading_coefficient()
@@ -631,7 +631,7 @@ cdef bint Zp_soluble_siksek_large_p(mpz_t a, mpz_t b, mpz_t c, mpz_t d, mpz_t e,
631631

632632
result = 0
633633
mpz_init(tt)
634-
for i from 0 <= i < j:
634+
for i in range(j):
635635
mpz_mul(tt, aaa, roots[i])
636636
mpz_add(tt, tt, bbb)
637637
mpz_mul(tt, tt, roots[i])
@@ -712,7 +712,7 @@ cdef bint Zp_soluble_siksek_large_p(mpz_t a, mpz_t b, mpz_t c, mpz_t d, mpz_t e,
712712
mpz_init(cc)
713713
mpz_init(dd)
714714
mpz_init(ee)
715-
for i from 0 <= i < j:
715+
for i in range(j):
716716
fmpz_poly_zero(f1)
717717
fmpz_poly_zero(linear)
718718
fmpz_poly_set_coeff_mpz(f1, 0, e)
@@ -958,7 +958,7 @@ cdef int count(mpz_t c_mpz, mpz_t d_mpz, mpz_t *p_list, unsigned long p_list_len
958958

959959
# Set up coefficient array, and static variables
960960
cdef mpz_t *coeffs = <mpz_t *> sig_malloc(5 * sizeof(mpz_t))
961-
for i from 0 <= i <= 4:
961+
for i in range(5):
962962
mpz_init(coeffs[i])
963963
mpz_set_ui(coeffs[1], 0) #
964964
mpz_set(coeffs[2], c_mpz) # These never change
@@ -969,7 +969,7 @@ cdef int count(mpz_t c_mpz, mpz_t d_mpz, mpz_t *p_list, unsigned long p_list_len
969969
# local solubility over RR)
970970
cdef mpz_t *p_div_d_mpz = <mpz_t *> sig_malloc((p_list_len+1) * sizeof(mpz_t))
971971
n_primes = 0
972-
for i from 0 <= i < p_list_len:
972+
for i in range(p_list_len):
973973
if mpz_divisible_p(d_mpz, p_list[i]):
974974
mpz_init(p_div_d_mpz[n_primes])
975975
mpz_set(p_div_d_mpz[n_primes], p_list[i])
@@ -987,7 +987,7 @@ cdef int count(mpz_t c_mpz, mpz_t d_mpz, mpz_t *p_list, unsigned long p_list_len
987987
mpz_set_ui(n2, 0)
988988
while mpz_cmp(j, n_divisors) < 0:
989989
mpz_set_ui(coeffs[4], 1)
990-
for i from 0 <= i < n_primes:
990+
for i in range(n_primes):
991991
if mpz_tstbit(j, i):
992992
mpz_mul(coeffs[4], coeffs[4], p_div_d_mpz[i])
993993
if verbosity > 3:
@@ -1012,7 +1012,7 @@ cdef int count(mpz_t c_mpz, mpz_t d_mpz, mpz_t *p_list, unsigned long p_list_len
10121012
if not found_global_points:
10131013
# Test whether the quartic is everywhere locally soluble:
10141014
els = 1
1015-
for i from 0 <= i < p_list_len:
1015+
for i in range(p_list_len):
10161016
if not Qp_soluble(coeffs[4], coeffs[3], coeffs[2], coeffs[1], coeffs[0], p_list[i]):
10171017
els = 0
10181018
break
@@ -1035,11 +1035,11 @@ cdef int count(mpz_t c_mpz, mpz_t d_mpz, mpz_t *p_list, unsigned long p_list_len
10351035
print("\nDone calling ratpoints for large point search")
10361036
mpz_add_ui(j, j, 1)
10371037
mpz_clear(j)
1038-
for i from 0 <= i < n_primes:
1038+
for i in range(n_primes):
10391039
mpz_clear(p_div_d_mpz[i])
10401040
sig_free(p_div_d_mpz)
10411041
mpz_clear(n_divisors)
1042-
for i from 0 <= i <= 4:
1042+
for i in range(5):
10431043
mpz_clear(coeffs[i])
10441044
sig_free(coeffs)
10451045
return 0
@@ -1202,16 +1202,16 @@ def two_descent_by_two_isogeny_work(Integer c, Integer d,
12021202
p_list_len = 1
12031203
n_factor_init(&fact)
12041204
n_factor(&fact, mpz_get_ui(d_mpz), proof)
1205-
for i from 0 <= i < fact.num:
1205+
for i in range(fact.num):
12061206
p = fact.p[i]
12071207
if p != 2:
12081208
mpz_init_set_ui(p_list_mpz[p_list_len], p)
12091209
p_list_len += 1
12101210
n_factor(&fact, mpz_get_ui(d_prime_mpz), proof)
1211-
for i from 0 <= i < fact.num:
1211+
for i in range(fact.num):
12121212
p = fact.p[i]
12131213
found = 0
1214-
for j from 0 <= j < p_list_len:
1214+
for j in range(p_list_len):
12151215
if mpz_cmp_ui(p_list_mpz[j], p)==0:
12161216
found = 1
12171217
break
@@ -1233,7 +1233,7 @@ def two_descent_by_two_isogeny_work(Integer c, Integer d,
12331233
if P not in primes: primes.append(P)
12341234
p_list_len = len(primes)
12351235
p_list_mpz = <mpz_t *> sig_malloc(p_list_len * sizeof(mpz_t))
1236-
for i from 0 <= i < p_list_len:
1236+
for i in range(p_list_len):
12371237
P = Integer(primes[i])
12381238
mpz_init_set(p_list_mpz[i], P.value)
12391239
if d_neg:
@@ -1257,7 +1257,7 @@ def two_descent_by_two_isogeny_work(Integer c, Integer d,
12571257
global_limit_small, global_limit_large, verbosity, selmer_only,
12581258
n1_prime.value, n2_prime.value)
12591259

1260-
for i from 0 <= i < p_list_len:
1260+
for i in range(p_list_len):
12611261
mpz_clear(p_list_mpz[i])
12621262
sig_free(p_list_mpz)
12631263

src/sage/schemes/toric/divisor_class.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ def _ToricRationalDivisorClass_unpickle_v1(parent, entries,
329329
v._init(degree, parent)
330330
cdef Rational z
331331
cdef Py_ssize_t i
332-
for i from 0 <= i < degree:
332+
for i in range(degree):
333333
z = Rational(entries[i])
334334
mpq_set(v._entries[i], z.value)
335335
v._is_immutable = not is_mutable

src/sage/structure/list_clone.pyx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ cdef class ClonableIntArray(ClonableElement):
12781278
if self._list is not NULL:
12791279
raise ValueError("resizing is forbidden")
12801280
self._alloc_(len(lst))
1281-
for i from 0 <= i < self._len:
1281+
for i in range(self._len):
12821282
self._list[i] = lst[i]
12831283

12841284
self._is_immutable = immutable
@@ -1390,7 +1390,7 @@ cdef class ClonableIntArray(ClonableElement):
13901390
cdef int i
13911391
cdef list L = <list> PyList_New(self._len)
13921392
cdef object o
1393-
for i from 0<=i<self._len:
1393+
for i in range(self._len):
13941394
o = PyInt_FromLong(self._list[i])
13951395
Py_INCREF(o)
13961396
PyList_SET_ITEM(L, i, o)
@@ -1524,7 +1524,7 @@ cdef class ClonableIntArray(ClonableElement):
15241524
False
15251525
"""
15261526
cdef int i
1527-
for i from 0 <= i < self._len:
1527+
for i in range(self._len):
15281528
if item == self._list[i]:
15291529
return True
15301530
return False
@@ -1545,7 +1545,7 @@ cdef class ClonableIntArray(ClonableElement):
15451545
ValueError: list.index(x): x not in list
15461546
"""
15471547
cdef int i
1548-
for i from 0 <= i < self._len:
1548+
for i in range(self._len):
15491549
if item == self._list[i]:
15501550
return i
15511551
raise ValueError("list.index(x): x not in list")
@@ -1664,7 +1664,7 @@ cdef class ClonableIntArray(ClonableElement):
16641664
res._parent = self._parent
16651665
if self:
16661666
res._alloc_(self._len)
1667-
for i from 0 <= i < res._len:
1667+
for i in range(self._len):
16681668
res._list[i] = self._list[i]
16691669
if HAS_DICTIONARY(self):
16701670
res.__dict__ = self.__dict__.copy()

src/sage/symbolic/expression.pyx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7000,7 +7000,7 @@ cdef class Expression(Expression_abc):
70007000
# the following is a temporary fix for GiNaC bug #9505
70017001
if is_a_mul(ss._gobj): # necessarily n=1 here
70027002
res = self
7003-
for i from 0 <= i < ss._gobj.nops():
7003+
for i in range(ss._gobj.nops()):
70047004
res = res.coefficient(new_Expression_from_GEx(self._parent, ss._gobj.op(i)))
70057005
return res
70067006
sig_on()
@@ -10135,7 +10135,7 @@ cdef class Expression(Expression_abc):
1013510135
sig_off()
1013610136
return new_Expression_from_GEx(self._parent, ex)
1013710137
elif is_a_mul(self._gobj):
10138-
for i from 0 <= i < self._gobj.nops():
10138+
for i in range(self._gobj.nops()):
1013910139
oper = self._gobj.op(i)
1014010140
if not is_a_power(oper):
1014110141
vec.push_back(oper)
@@ -10225,7 +10225,7 @@ cdef class Expression(Expression_abc):
1022510225
sig_off()
1022610226
return new_Expression_from_GEx(self._parent, ex)
1022710227
elif is_a_mul(self._gobj):
10228-
for i from 0 <= i < self._gobj.nops():
10228+
for i in range(self._gobj.nops()):
1022910229
oper = self._gobj.op(i)
1023010230
if is_a_power(oper):
1023110231
ex = oper.op(0)
@@ -10318,7 +10318,7 @@ cdef class Expression(Expression_abc):
1031810318
return (new_Expression_from_GEx(self._parent, ex.op(0)),
1031910319
new_Expression_from_GEx(self._parent, ex.op(1)))
1032010320
elif is_a_mul(self._gobj):
10321-
for i from 0 <= i < self._gobj.nops():
10321+
for i in range(self._gobj.nops()):
1032210322
oper = self._gobj.op(i)
1032310323
if is_a_power(oper): # oper = ex^power
1032410324
ex = oper.op(0)

src/sage/symbolic/pynac_function_impl.pxi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ cpdef call_registered_function(unsigned serial,
4242
cdef GEx res
4343
cdef GExVector vec
4444
if nargs == 0 or nargs > 3:
45-
for i from 0 <= i < len(args):
45+
for i in range(len(args)):
4646
vec.push_back((<Expression>args[i])._gobj)
4747
res = g_function_evalv(serial, vec, hold)
4848
elif nargs == 1:

0 commit comments

Comments
 (0)