Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getting rid of many uses of xrange in pyx files #35772

Merged
merged 1 commit into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/sage/calculus/interpolators.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -225,21 +225,21 @@ cdef class CCSpline:
cdef int N, i, k
N = len(pts)
yvec = np.zeros(N, dtype=np.complex128)
for i in xrange(N):
for i in range(N):
yvec[i] = 3 * (pts[(i - 1) % N] - 2*pts[i] + pts[(i + 1) % N])
bmat = np.zeros([N, N], dtype=np.complex128)
for i in xrange(N):
for i in range(N):
bmat[i, i] = 4
bmat[(i - 1) % N, i] = 1
bmat[(i + 1) % N, i] = 1
bvec = (np.linalg.solve(bmat, yvec))
cvec = np.zeros(N, dtype=np.complex128)
for i in xrange(N):
for i in range(N):
cvec[i] = (pts[(i + 1) % N] - pts[i] - 1.0/3.0 *
bvec[(i + 1) % N] - 2./3. * bvec[i])
dvec = np.array(pts, dtype=np.complex128)
avec = np.zeros(N, dtype=np.complex128)
for i in xrange(N):
for i in range(N):
avec[i] = 1.0/3.0 * (bvec[(i + 1) % N] - bvec[i])
self.avec = avec
self.bvec = bvec
Expand Down
68 changes: 34 additions & 34 deletions src/sage/calculus/riemann.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ cdef class Riemann_Map:
self.tk = np.array(np.arange(N) * TWOPI / N + 0.001 / N,
dtype=FLOAT)
self.tk2 = np.zeros(N + 1, dtype=FLOAT)
for i in xrange(N):
for i in range(N):
self.tk2[i] = self.tk[i]
self.tk2[N] = TWOPI
self.B = len(fs) # number of boundaries of the figure
Expand All @@ -246,14 +246,14 @@ cdef class Riemann_Map:
dtype=COMPLEX)
# Find the points on the boundaries and their derivatives.
if self.exterior:
for k in xrange(self.B):
for i in xrange(N):
for k in range(self.B):
for i in range(N):
fk = fs[k](self.tk[N-i-1])
cps[k, i] = complex(1/fk)
dps[k, i] = complex(1/fk**2*fprimes[k](self.tk[N-i-1]))
else:
for k in xrange(self.B):
for i in xrange(N):
for k in range(self.B):
for i in range(N):
cps[k, i] = complex(fs[k](self.tk[i]))
dps[k, i] = complex(fprimes[k](self.tk[i]))
if self.exterior:
Expand Down Expand Up @@ -327,7 +327,7 @@ cdef class Riemann_Map:
(normalized_dp[t]/(cp-cp[t])).conjugate())
for t in np.arange(NB)], dtype=np.complex128)
np.seterr(divide=errdivide,invalid=errinvalid) # resets the error handling
for i in xrange(NB):
for i in range(NB):
K[i, i] = 1
# Nystrom Method for solving 2nd kind integrals
phi = np.linalg.solve(K, g) / NB * TWOPI
Expand All @@ -339,22 +339,22 @@ cdef class Riemann_Map:
# regions.
if B != 1:
theta_array = np.zeros([1, NB])
for i in xrange(NB):
for i in range(NB):
theta_array[0, i] = phase(-I * np.power(phi[i], 2) * dp[i])
self.theta_array = np.concatenate(
[theta_array.reshape([B, N]), np.zeros([B, 1])], axis=1)
for k in xrange(B):
for k in range(B):
self.theta_array[k, N] = self.theta_array[k, 0] + TWOPI
# Finding the theta correspondence using abs. Well behaved, but
# doesn't work on multiply connected domains.
else:
phi2 = phi.reshape([self.B, N])
theta_array = np.zeros([B, N + 1], dtype=np.float64)
for k in xrange(B):
for k in range(B):
phik = phi2[k]
saa = (np.dot(abs(phi), abs(phi))) * TWOPI / NB
theta_array[k, 0] = 0
for i in xrange(1, N):
for i in range(1, N):
theta_array[k, i] = (
theta_array[k, i - 1] +
((TWOPI / NB * TWOPI *
Expand All @@ -368,7 +368,7 @@ cdef class Riemann_Map:
t0 = theta_array[k, tmax] + phase(phimax)
else:
t0 = theta_array[k, tmax] - phase(phimax)
for i in xrange(N):
for i in range(N):
theta_array[k, i] = theta_array[k, i] - t0
theta_array[k, N] = TWOPI + theta_array[k, 0]
self.theta_array = theta_array
Expand Down Expand Up @@ -432,7 +432,7 @@ cdef class Riemann_Map:
cdef int k, B
if boundary < 0:
temptk = self.tk
for i in xrange(self.B - 1):
for i in range(self.B - 1):
temptk = np.concatenate([temptk, self.tk])
if absolute_value:
return np.column_stack(
Expand Down Expand Up @@ -504,7 +504,7 @@ cdef class Riemann_Map:
"""
if boundary < 0:
temptk = self.tk2
for i in xrange(self.B - 1):
for i in range(self.B - 1):
temptk = np.concatenate([temptk, self.tk2])
return np.column_stack(
[temptk, self.theta_array.flatten()]).tolist()
Expand Down Expand Up @@ -532,8 +532,8 @@ cdef class Riemann_Map:
[self.B, N + 1], dtype=np.complex128)
cdef int k, i
# Lots of setup for Simpson's method of integration.
for k in xrange(self.B):
for i in xrange(N // 3):
for k in range(self.B):
for i in range(N // 3):
p_vector[k, 3*i] = (2*coeff * dps[k, 3*i] *
exp(I * theta_array[k, 3*i]))
p_vector[k, 3*i + 1] = (3*coeff * dps[k, 3*i + 1] *
Expand Down Expand Up @@ -636,21 +636,21 @@ cdef class Riemann_Map:
self.p_vector_inverse = np.zeros([B, N], dtype=np.complex128)
# Setup for trapezoid integration because integration points are
# not equally spaced.
for k in xrange(B):
for i in xrange(N):
for k in range(B):
for i in range(N):
di = theta_array[k, (i + 1) % N] - theta_array[k, (i - 1) % N]
if di > PI:
di = di - TWOPI
elif di < -PI:
di = di + TWOPI
self.p_vector_inverse[k, i] = di / 2
self.sinalpha = np.zeros([B, N], dtype=np.float64)
for k in xrange(B):
for i in xrange(N):
for k in range(B):
for i in range(N):
self.sinalpha[k, i] = sin(-theta_array[k, i])
self.cosalpha = np.zeros([B, N], dtype=np.float64)
for k in xrange(B):
for i in xrange(N):
for k in range(B):
for i in range(N):
self.cosalpha[k, i] = cos(-theta_array[k, i])

cpdef inverse_riemann_map(self, COMPLEX_T pt):
Expand Down Expand Up @@ -748,7 +748,7 @@ cdef class Riemann_Map:
from sage.plot.all import list_plot

plots = list(range(self.B))
for k in xrange(self.B):
for k in range(self.B):
# This conditional should be eliminated when the thickness/pointsize
# issue is resolved later. Same for the others in plot_spiderweb().
if plotjoined:
Expand Down Expand Up @@ -814,13 +814,13 @@ cdef class Riemann_Map:
cdef np.ndarray[COMPLEX_T, ndim=2] z_values = np.empty(
[y_points, x_points], dtype=np.complex128)
if self.exterior:
for i in xrange(x_points):
for j in xrange(y_points):
for i in range(x_points):
for j in range(y_points):
pt = 1/(xmin + 0.5*xstep + i*xstep + I*(ymin + 0.5*ystep + j*ystep))
z_values[j, i] = 1/(-np.dot(p_vector,1/(pre_q_vector - pt)))
else:
for i in xrange(x_points):
for j in xrange(y_points):
for i in range(x_points):
for j in range(y_points):
pt = xmin + 0.5*xstep + i*xstep + I*(ymin + 0.5*ystep + j*ystep)
z_values[j, i] = -np.dot(p_vector,1/(pre_q_vector - pt))
return z_values, xmin, xmax, ymin, ymax
Expand Down Expand Up @@ -949,9 +949,9 @@ cdef class Riemann_Map:
s = spline(np.column_stack([self.theta_array[0], self.tk2]).tolist())
tmax = self.theta_array[0, self.N]
tmin = self.theta_array[0, 0]
for k in xrange(circles):
for k in range(circles):
temp = list(range(pts*2))
for i in xrange(2*pts):
for i in range(2*pts):
temp[i] = self.inverse_riemann_map(
(k + 1) / (circles + 1.0) * exp(I*i * TWOPI / (2*pts)))
if plotjoined:
Expand All @@ -961,14 +961,14 @@ cdef class Riemann_Map:
circle_list[k] = list_plot(comp_pt(temp, 1),
rgbcolor=rgbcolor, pointsize=thickness)
line_list = list(range(spokes))
for k in xrange(spokes):
for k in range(spokes):
temp = list(range(pts))
angle = (k*1.0) / spokes * TWOPI
if angle >= tmax:
angle -= TWOPI
elif angle <= tmin:
angle += TWOPI
for i in xrange(pts - 1):
for i in range(pts - 1):
temp[i] = self.inverse_riemann_map(
(i * 1.0) / (pts * 1.0) * exp(I * angle) * linescale)
temp[pts - 1] = complex(
Expand Down Expand Up @@ -1238,8 +1238,8 @@ cpdef complex_to_spiderweb(np.ndarray[COMPLEX_T, ndim = 2] z_values,
spoke_angles = srange(-PI,PI+TWOPI/spokes,TWOPI/spokes)
else:
spoke_angles = []
for i in xrange(imax-2): # the d arrays are 1 smaller on each side
for j in xrange(jmax-2):
for i in range(imax-2): # the d arrays are 1 smaller on each side
for j in range(jmax-2):
z = z_values[i+1,j+1]
mag = abs(z)
arg = phase(z)
Expand Down Expand Up @@ -1309,9 +1309,9 @@ cpdef complex_to_rgb(np.ndarray[COMPLEX_T, ndim = 2] z_values):
dtype=FLOAT, shape=(imax, jmax, 3))

sig_on()
for i in xrange(imax):
for i in range(imax):
row = z_values[i]
for j in xrange(jmax):
for j in range(jmax):
z = row[j]
mag = abs(z)
arg = phase(z)
Expand Down
14 changes: 7 additions & 7 deletions src/sage/coding/binary_code.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@ def test_word_perms(t_limit=5.0):
raise MemoryError("Error allocating memory.")
from sage.misc.prandom import randint
from sage.combinat.permutation import Permutations
S = Permutations(list(xrange(n)))
S = Permutations(list(range(n)))
t = cputime()
while cputime(t) < t_limit:
word = [randint(0, 1) for _ in xrange(n)]
word = [randint(0, 1) for _ in range(n)]
cw1 = 0
for j from 0 <= j < n:
cw1 += (<codeword>word[j]) << (<codeword>j)
Expand Down Expand Up @@ -298,7 +298,7 @@ cdef WordPermutation *create_word_perm(object list_perm):
word_perm.chunk_num = num_chunks
words_per_chunk = 1 << chunk_size
word_perm.gate = ( (<codeword>1) << chunk_size ) - 1
list_perm += list(xrange(len(list_perm), chunk_size*num_chunks))
list_perm += list(range(len(list_perm), chunk_size*num_chunks))
word_perm.chunk_words = words_per_chunk
for i from 0 <= i < num_chunks:
images_i = <codeword *> sig_malloc(words_per_chunk * sizeof(codeword))
Expand Down Expand Up @@ -661,15 +661,15 @@ cdef codeword *expand_to_ortho_basis(BinaryCode B, int n):
for j from i <= j < n:
basis[j] = 0
# now basis is length i
perm = list(xrange(B.nrows))
perm = list(range(B.nrows))
perm_c = []
for j from B.nrows <= j < B.ncols:
if (<codeword>1 << j) & pivots:
perm.append(j)
else:
perm_c.append(j)
perm.extend(perm_c)
perm.extend(list(xrange(B.ncols, n)))
perm.extend(list(range(B.ncols, n)))
perm_c = [0]*n
for j from 0 <= j < n:
perm_c[perm[j]] = j
Expand Down Expand Up @@ -4017,7 +4017,7 @@ cdef class BinaryCodeClassifier:


for i from 0 <= i < len(aut_gp_gens):
parent_generators[i] = create_word_perm(aut_gp_gens[i] + list(xrange(B.ncols, n)))
parent_generators[i] = create_word_perm(aut_gp_gens[i] + list(range(B.ncols, n)))

word = 0
while ortho_basis[k] & (((<codeword>1) << B.ncols) - 1):
Expand Down Expand Up @@ -4116,7 +4116,7 @@ cdef class BinaryCodeClassifier:
aut_B_aug = libgap(PermutationGroup([PermutationGroupElement([a+1 for a in g]) for g in aug_aut_gp_gens]))
H = libgap(aut_m).Intersection2(aut_B_aug)
rt_transversal = [[int(a) - 1 for a in g.ListPerm(n)] for g in aut_B_aug.RightTransversal(H) if not g.IsOne()]
rt_transversal.append(list(xrange(n)))
rt_transversal.append(list(range(n)))

bingo2 = 0
for coset_rep in rt_transversal:
Expand Down
24 changes: 12 additions & 12 deletions src/sage/coding/codecan/autgroup_can_label.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ def _cyclic_shift(n, p):
sage: p.action(t)
[0, 2, 7, 3, 1, 5, 6, 4, 8, 9]
"""
x = list(xrange(1, n + 1))
for i in xrange(1, len(p)):
x = list(range(1, n + 1))
for i in range(1, len(p)):
x[p[i - 1]] = p[i] + 1
x[p[len(p) - 1]] = p[0] + 1
return Permutation(x)
Expand Down Expand Up @@ -229,17 +229,17 @@ class LinearCodeAutGroupCanLabel:
S = SemimonomialTransformationGroup(F, mat.ncols())

if P is None:
P = [list(xrange(mat.ncols()))]
P = [list(range(mat.ncols()))]

pos2P = [-1] * mat.ncols()
for i in xrange(len(P)):
for i in range(len(P)):
P[i].sort(reverse=True)
for x in P[i]:
pos2P[x] = i

col_list = mat.columns()
nz = [i for i in xrange(mat.ncols()) if not col_list[i].is_zero()]
z = [(pos2P[i], i) for i in xrange(mat.ncols()) if col_list[i].is_zero()]
nz = [i for i in range(mat.ncols()) if not col_list[i].is_zero()]
z = [(pos2P[i], i) for i in range(mat.ncols()) if col_list[i].is_zero()]
z.sort()
z = [i for (p, i) in z]

Expand All @@ -259,7 +259,7 @@ class LinearCodeAutGroupCanLabel:
col2pos = []
col2P = []
for c in col_set:
X = [(pos2P[y], y) for y in xrange(mat.ncols()) if col_list[y] == c ]
X = [(pos2P[y], y) for y in range(mat.ncols()) if col_list[y] == c ]
X.sort()
col2pos.append([b for (a, b) in X ])
col2P.append([a for (a, b) in X ])
Expand All @@ -272,7 +272,7 @@ class LinearCodeAutGroupCanLabel:
P_refined = []
p = [0]
act_qty = col2P[0]
for i in xrange(1, len(col_set)):
for i in range(1, len(col_set)):
if act_qty == col2P[i]:
p.append(i)
else:
Expand Down Expand Up @@ -357,7 +357,7 @@ class LinearCodeAutGroupCanLabel:
perm = [-1] * mat.ncols()
mult = [F.one()] * mat.ncols()

for i in xrange(len(can_col_set)):
for i in range(len(can_col_set)):
img = can_transp.get_perm()(i + 1)
for j in col2pos[img - 1]:
pos = P[ pos2P[j] ].pop()
Expand All @@ -378,7 +378,7 @@ class LinearCodeAutGroupCanLabel:
self._full_autom_order *= a


for i in xrange(len(col2P)):
for i in range(len(col2P)):
if len(col2P[i]) > 1:
A, a = self._compute_trivial_automs(normalization,
normalization_inverse, col2pos[i], col2P[i])
Expand Down Expand Up @@ -508,11 +508,11 @@ class LinearCodeAutGroupCanLabel:
n = S.degree()
A = []
for g in gens:
perm = list(xrange(1, n + 1))
perm = list(range(1, n + 1))
mult = [S.base_ring().one()] * n
short_perm = g.get_perm()
short_mult = g.get_v()
for i in xrange(len(col2pos)):
for i in range(len(col2pos)):
c = col2pos[i]
img_iter = iter(col2pos[short_perm(i + 1) - 1])
for x in c:
Expand Down
5 changes: 3 additions & 2 deletions src/sage/crypto/boolean_function.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ and also algebraic immunity.

EXAMPLES::

sage: R.<x>=GF(2^8,'a')[]
sage: R.<x> = GF(2^8,'a')[]
sage: from sage.crypto.boolean_function import BooleanFunction
sage: B = BooleanFunction( x^254 ) # the Boolean function Tr(x^254)
sage: B
Expand Down Expand Up @@ -900,7 +900,8 @@ cdef class BooleanFunction(SageObject):
temp[i] = W[i]*W[i]

walsh_hadamard(temp, self._nvariables)
self._autocorrelation = tuple([temp[i] >> self._nvariables for i in xrange(n)])
self._autocorrelation = tuple([temp[i] >> self._nvariables
for i in range(n)])
sig_free(temp)

return self._autocorrelation
Expand Down
Loading