Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
modernize some for loops in cython files
Browse files Browse the repository at this point in the history
  • Loading branch information
fchapoton committed Dec 14, 2022
1 parent bb7ee85 commit 16ef533
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 45 deletions.
6 changes: 3 additions & 3 deletions src/sage/combinat/expnums.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,17 @@ def expnums(int n, int aa):
mpz_init_set_si(bell[1], aa)
cdef int i
cdef int k
for i from 1 <= i < n:
for i in range(1, n):
mpz_init(bell[i+1])
mpz_mul(bell[i+1], a, bell[1])
for k from 0 <= k < i:
for k in range(i):
mpz_add(bell[i-k], bell[i-k], bell[i-k+1])

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

for i from 1 <= i <= n:
for i in range(1, n + 1):
mpz_clear(bell[i])
sig_free(bell)

Expand Down
2 changes: 1 addition & 1 deletion src/sage/libs/coxeter3/coxeter.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ cdef class CoxGroupElement:

cdef Length i = 0
cdef CoxGroupElement res
for i from 0 <= i < list.size():
for i in range(list.size()):
res = self._new()
res.word = list[i]
coatoms.append(res)
Expand Down
4 changes: 2 additions & 2 deletions src/sage/media/channels.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ def _separate_channels(_data, _width, _nchannels):
if width == 1:
# handle the one byte case

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

elif width == 2:
a = 32768
for n from 0 <= n < l:
for n in range(l):
# compute the value as an integer
x = <int> (data[2*n]) + 256 * <int>(data[2*n + 1])
#x -= 65536*(x > a)
Expand Down
3 changes: 1 addition & 2 deletions src/sage/probability/probability_distribution.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,10 @@ cdef class SphericalDistribution(ProbabilityDistribution):
sage: T.get_random_element() # rel tol 4e-16
(0.07961564104639995, -0.05237671627581255, 0.9954486572862178)
"""

cdef int i
v = [0]*self.dimension
gsl_ran_dir_nd(self.r, self.dimension, self.vec)
for i from 0 <= i<self.dimension:
for i in range(self.dimension):
v[i] = self.vec[i]
return vector(sage.rings.real_double.RDF, v) #This could be made more efficient by directly constructing the vector, TODO.

Expand Down
4 changes: 2 additions & 2 deletions src/sage/quivers/algebra_elements.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ cdef int negdeglex(path_mon_t M1, path_mon_t M2) except -2:
if M1.s_len < M2.s_len:
return -1
return 1
for index from 0 <= index < M1.path.length:
for index in range(M1.path.length):
item1 = biseq_getitem(M1.path, index)
item2 = biseq_getitem(M2.path, index)
sig_check()
Expand Down Expand Up @@ -256,7 +256,7 @@ cdef int deglex(path_mon_t M1, path_mon_t M2) except -2:
if M1.s_len < M2.s_len:
return 1
return -1
for index from 0 <= index < M1.path.length:
for index in range(M1.path.length):
item1 = biseq_getitem(M1.path, index)
item2 = biseq_getitem(M2.path, index)
sig_check()
Expand Down
48 changes: 24 additions & 24 deletions src/sage/schemes/elliptic_curves/descent_two_isogeny.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ r"""
Descent on elliptic curves over `\QQ` with a 2-isogeny
"""

#*****************************************************************************
# ***************************************************************************
# Copyright (C) 2009 Robert L. Miller <rlmillster@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
# https://www.gnu.org/licenses/
# ***************************************************************************

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

nmod_poly_zero(f)
nmod_poly_set_coeff_ui(f, 0, 1)
for i from 0 <= i < f_factzn.num:
for j from 0 <= j < (f_factzn.exp[i]>>1):
for i in range(f_factzn.num):
for j in range(f_factzn.exp[i]>>1):
nmod_poly_mul(f, f, &f_factzn.p[i])

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

result = 0
mpz_init(tt)
for i from 0 <= i < j:
for i in range(j):
mpz_mul_ui(tt, aaa, roots[i])
mpz_add(tt, tt, bbb)
mpz_mul_ui(tt, tt, roots[i])
Expand Down Expand Up @@ -454,7 +454,7 @@ cdef bint Zp_soluble_siksek(mpz_t a, mpz_t b, mpz_t c, mpz_t d, mpz_t e,
has_roots = 0
has_single_roots = 0
j = 0
for i from 0 <= i < f_factzn.num:
for i in range(f_factzn.num):
if nmod_poly_degree(&f_factzn.p[i]) == 1 and 0 != nmod_poly_get_coeff_ui(&f_factzn.p[i], 1):
has_roots = 1
if f_factzn.exp[i] == 1:
Expand All @@ -473,7 +473,7 @@ cdef bint Zp_soluble_siksek(mpz_t a, mpz_t b, mpz_t c, mpz_t d, mpz_t e,
mpz_init(cc)
mpz_init(dd)
mpz_init(ee)
for i from 0 <= i < j:
for i in range(j):
fmpz_poly_zero(f1)
fmpz_poly_zero(linear)
fmpz_poly_set_coeff_mpz(f1, 0, e)
Expand Down Expand Up @@ -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,

f = ntl.ZZ_pX([1], P)
for factor, exponent in f_factzn:
for j from 0 <= j < (exponent/2):
for j in range(exponent // 2):
f *= factor

f /= f.leading_coefficient()
Expand Down Expand Up @@ -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,

result = 0
mpz_init(tt)
for i from 0 <= i < j:
for i in range(j):
mpz_mul(tt, aaa, roots[i])
mpz_add(tt, tt, bbb)
mpz_mul(tt, tt, roots[i])
Expand Down Expand Up @@ -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,
mpz_init(cc)
mpz_init(dd)
mpz_init(ee)
for i from 0 <= i < j:
for i in range(j):
fmpz_poly_zero(f1)
fmpz_poly_zero(linear)
fmpz_poly_set_coeff_mpz(f1, 0, e)
Expand Down Expand Up @@ -958,7 +958,7 @@ cdef int count(mpz_t c_mpz, mpz_t d_mpz, mpz_t *p_list, unsigned long p_list_len

# Set up coefficient array, and static variables
cdef mpz_t *coeffs = <mpz_t *> sig_malloc(5 * sizeof(mpz_t))
for i from 0 <= i <= 4:
for i in range(5):
mpz_init(coeffs[i])
mpz_set_ui(coeffs[1], 0) #
mpz_set(coeffs[2], c_mpz) # These never change
Expand All @@ -969,7 +969,7 @@ cdef int count(mpz_t c_mpz, mpz_t d_mpz, mpz_t *p_list, unsigned long p_list_len
# local solubility over RR)
cdef mpz_t *p_div_d_mpz = <mpz_t *> sig_malloc((p_list_len+1) * sizeof(mpz_t))
n_primes = 0
for i from 0 <= i < p_list_len:
for i in range(p_list_len):
if mpz_divisible_p(d_mpz, p_list[i]):
mpz_init(p_div_d_mpz[n_primes])
mpz_set(p_div_d_mpz[n_primes], p_list[i])
Expand All @@ -987,7 +987,7 @@ cdef int count(mpz_t c_mpz, mpz_t d_mpz, mpz_t *p_list, unsigned long p_list_len
mpz_set_ui(n2, 0)
while mpz_cmp(j, n_divisors) < 0:
mpz_set_ui(coeffs[4], 1)
for i from 0 <= i < n_primes:
for i in range(n_primes):
if mpz_tstbit(j, i):
mpz_mul(coeffs[4], coeffs[4], p_div_d_mpz[i])
if verbosity > 3:
Expand All @@ -1012,7 +1012,7 @@ cdef int count(mpz_t c_mpz, mpz_t d_mpz, mpz_t *p_list, unsigned long p_list_len
if not found_global_points:
# Test whether the quartic is everywhere locally soluble:
els = 1
for i from 0 <= i < p_list_len:
for i in range(p_list_len):
if not Qp_soluble(coeffs[4], coeffs[3], coeffs[2], coeffs[1], coeffs[0], p_list[i]):
els = 0
break
Expand All @@ -1035,11 +1035,11 @@ cdef int count(mpz_t c_mpz, mpz_t d_mpz, mpz_t *p_list, unsigned long p_list_len
print("\nDone calling ratpoints for large point search")
mpz_add_ui(j, j, 1)
mpz_clear(j)
for i from 0 <= i < n_primes:
for i in range(n_primes):
mpz_clear(p_div_d_mpz[i])
sig_free(p_div_d_mpz)
mpz_clear(n_divisors)
for i from 0 <= i <= 4:
for i in range(5):
mpz_clear(coeffs[i])
sig_free(coeffs)
return 0
Expand Down Expand Up @@ -1202,16 +1202,16 @@ def two_descent_by_two_isogeny_work(Integer c, Integer d,
p_list_len = 1
n_factor_init(&fact)
n_factor(&fact, mpz_get_ui(d_mpz), proof)
for i from 0 <= i < fact.num:
for i in range(fact.num):
p = fact.p[i]
if p != 2:
mpz_init_set_ui(p_list_mpz[p_list_len], p)
p_list_len += 1
n_factor(&fact, mpz_get_ui(d_prime_mpz), proof)
for i from 0 <= i < fact.num:
for i in range(fact.num):
p = fact.p[i]
found = 0
for j from 0 <= j < p_list_len:
for j in range(p_list_len):
if mpz_cmp_ui(p_list_mpz[j], p)==0:
found = 1
break
Expand All @@ -1233,7 +1233,7 @@ def two_descent_by_two_isogeny_work(Integer c, Integer d,
if P not in primes: primes.append(P)
p_list_len = len(primes)
p_list_mpz = <mpz_t *> sig_malloc(p_list_len * sizeof(mpz_t))
for i from 0 <= i < p_list_len:
for i in range(p_list_len):
P = Integer(primes[i])
mpz_init_set(p_list_mpz[i], P.value)
if d_neg:
Expand All @@ -1257,7 +1257,7 @@ def two_descent_by_two_isogeny_work(Integer c, Integer d,
global_limit_small, global_limit_large, verbosity, selmer_only,
n1_prime.value, n2_prime.value)

for i from 0 <= i < p_list_len:
for i in range(p_list_len):
mpz_clear(p_list_mpz[i])
sig_free(p_list_mpz)

Expand Down
2 changes: 1 addition & 1 deletion src/sage/schemes/toric/divisor_class.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def _ToricRationalDivisorClass_unpickle_v1(parent, entries,
v._init(degree, parent)
cdef Rational z
cdef Py_ssize_t i
for i from 0 <= i < degree:
for i in range(degree):
z = Rational(entries[i])
mpq_set(v._entries[i], z.value)
v._is_immutable = not is_mutable
Expand Down
10 changes: 5 additions & 5 deletions src/sage/structure/list_clone.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@ cdef class ClonableIntArray(ClonableElement):
if self._list is not NULL:
raise ValueError("resizing is forbidden")
self._alloc_(len(lst))
for i from 0 <= i < self._len:
for i in range(self._len):
self._list[i] = lst[i]

self._is_immutable = immutable
Expand Down Expand Up @@ -1392,7 +1392,7 @@ cdef class ClonableIntArray(ClonableElement):
cdef int i
cdef list L = <list> PyList_New(self._len)
cdef object o
for i from 0<=i<self._len:
for i in range(self._len):
o = PyInt_FromLong(self._list[i])
Py_INCREF(o)
PyList_SET_ITEM(L, i, o)
Expand Down Expand Up @@ -1526,7 +1526,7 @@ cdef class ClonableIntArray(ClonableElement):
False
"""
cdef int i
for i from 0 <= i < self._len:
for i in range(self._len):
if item == self._list[i]:
return True
return False
Expand All @@ -1547,7 +1547,7 @@ cdef class ClonableIntArray(ClonableElement):
ValueError: list.index(x): x not in list
"""
cdef int i
for i from 0 <= i < self._len:
for i in range(self._len):
if item == self._list[i]:
return i
raise ValueError("list.index(x): x not in list")
Expand Down Expand Up @@ -1666,7 +1666,7 @@ cdef class ClonableIntArray(ClonableElement):
res._parent = self._parent
if self:
res._alloc_(self._len)
for i from 0 <= i < res._len:
for i in range(self._len):
res._list[i] = self._list[i]
if HAS_DICTIONARY(self):
res.__dict__ = self.__dict__.copy()
Expand Down
8 changes: 4 additions & 4 deletions src/sage/symbolic/expression.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -7000,7 +7000,7 @@ cdef class Expression(Expression_abc):
# the following is a temporary fix for GiNaC bug #9505
if is_a_mul(ss._gobj): # necessarily n=1 here
res = self
for i from 0 <= i < ss._gobj.nops():
for i in range(ss._gobj.nops()):
res = res.coefficient(new_Expression_from_GEx(self._parent, ss._gobj.op(i)))
return res
sig_on()
Expand Down Expand Up @@ -10135,7 +10135,7 @@ cdef class Expression(Expression_abc):
sig_off()
return new_Expression_from_GEx(self._parent, ex)
elif is_a_mul(self._gobj):
for i from 0 <= i < self._gobj.nops():
for i in range(self._gobj.nops()):
oper = self._gobj.op(i)
if not is_a_power(oper):
vec.push_back(oper)
Expand Down Expand Up @@ -10225,7 +10225,7 @@ cdef class Expression(Expression_abc):
sig_off()
return new_Expression_from_GEx(self._parent, ex)
elif is_a_mul(self._gobj):
for i from 0 <= i < self._gobj.nops():
for i in range(self._gobj.nops()):
oper = self._gobj.op(i)
if is_a_power(oper):
ex = oper.op(0)
Expand Down Expand Up @@ -10318,7 +10318,7 @@ cdef class Expression(Expression_abc):
return (new_Expression_from_GEx(self._parent, ex.op(0)),
new_Expression_from_GEx(self._parent, ex.op(1)))
elif is_a_mul(self._gobj):
for i from 0 <= i < self._gobj.nops():
for i in range(self._gobj.nops()):
oper = self._gobj.op(i)
if is_a_power(oper): # oper = ex^power
ex = oper.op(0)
Expand Down
2 changes: 1 addition & 1 deletion src/sage/symbolic/pynac_function_impl.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ cpdef call_registered_function(unsigned serial,
cdef GEx res
cdef GExVector vec
if nargs == 0 or nargs > 3:
for i from 0 <= i < len(args):
for i in range(len(args)):
vec.push_back((<Expression>args[i])._gobj)
res = g_function_evalv(serial, vec, hold)
elif nargs == 1:
Expand Down

0 comments on commit 16ef533

Please sign in to comment.