Skip to content

Commit f5f876a

Browse files
author
Release Manager
committed
gh-40720: fix ruff PERF in schemes fix all suggestions of `ruff check --select=PERF src/sage/schemes` about some loops ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. URL: #40720 Reported by: Frédéric Chapoton Reviewer(s): David Coudert
2 parents 273073b + 91503c9 commit f5f876a

File tree

12 files changed

+61
-105
lines changed

12 files changed

+61
-105
lines changed

src/sage/schemes/curves/affine_curve.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,14 +1744,13 @@ def tangent_line(self, p):
17441744
Tp = self.tangent_space(p)
17451745

17461746
if Tp.dimension() > 1:
1747-
raise ValueError("the curve is not smooth at {}".format(p))
1747+
raise ValueError(f"the curve is not smooth at {p}")
17481748

17491749
from sage.schemes.curves.constructor import Curve
17501750

17511751
# translate to p
1752-
I0 = []
1753-
for poly in Tp.defining_polynomials():
1754-
I0.append(poly.subs({x: x - c for x, c in zip(gens, p)}))
1752+
I0 = [poly.subs({x: x - c for x, c in zip(gens, p)})
1753+
for poly in Tp.defining_polynomials()]
17551754

17561755
return Curve(I0, A)
17571756

@@ -2744,11 +2743,8 @@ def places_on(self, point):
27442743
gs = [phi(g) for g in point.prime_ideal().gens()]
27452744
fs = [g for g in gs if not g.is_zero()]
27462745
f = fs.pop()
2747-
places = []
2748-
for p in f.zeros():
2749-
if all(f.valuation(p) > 0 for f in fs):
2750-
places.append(p)
2751-
return places
2746+
return [p for p in f.zeros()
2747+
if all(f.valuation(p) > 0 for f in fs)]
27522748

27532749
def parametric_representation(self, place, name=None):
27542750
"""

src/sage/schemes/curves/projective_curve.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2862,14 +2862,11 @@ def places_on(self, point):
28622862

28632863
phi = self._map_to_function_field
28642864
denom = self._coordinate_functions[i]
2865-
gs = [phi(f)/denom**f.degree() for f in prime.gens()]
2865+
gs = [phi(f) / denom**f.degree() for f in prime.gens()]
28662866
fs = [g for g in gs if not g.is_zero()]
28672867
f = fs.pop()
2868-
places = []
2869-
for p in f.zeros():
2870-
if all(f.valuation(p) > 0 for f in fs):
2871-
places.append(p)
2872-
return places
2868+
return [p for p in f.zeros()
2869+
if all(f.valuation(p) > 0 for f in fs)]
28732870

28742871
def jacobian(self, model, base_div=None):
28752872
"""

src/sage/schemes/elliptic_curves/BSD.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -593,10 +593,12 @@ def prove_BSD(E, verbosity=0, two_desc='mwrank', proof=None, secs_hi=5,
593593
if p >= 5 and D_K % p and len(K.factor(p)) == 1:
594594
# p is inert in K
595595
BSD.primes.append(p)
596-
for p in heegner_primes:
597-
if p >= 5 and D_E % p and D_K % p and len(K.factor(p)) == 1:
598-
# p is good for E and inert in K
599-
kolyvagin_primes.append(p)
596+
597+
kolyvagin_primes.extend(p for p in heegner_primes
598+
# p is good for E and inert in K
599+
if p >= 5 and D_E % p and D_K % p
600+
and len(K.factor(p)) == 1)
601+
600602
for p in prime_divisors(BSD.sha_an):
601603
if p >= 5 and D_K % p and len(K.factor(p)) == 1:
602604
if BSD.curve.is_good(p):

src/sage/schemes/elliptic_curves/ell_generic.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3207,16 +3207,15 @@ def montgomery_model(self, twisted=False, morphism=False):
32073207
R = self.base_ring()
32083208
P = PolynomialRing(R, 'v')
32093209

3210-
sols = []
3211-
for r in P([b, a, 0, 1]).roots(multiplicities=False):
3212-
for s in P([3 * r**2 + a, 0, -1]).roots(multiplicities=False):
3213-
sols.append((r,s))
3210+
sols = [(r, s)
3211+
for r in P([b, a, 0, 1]).roots(multiplicities=False)
3212+
for s in P([3 * r**2 + a, 0, -1]).roots(multiplicities=False)]
32143213

32153214
if not sols:
32163215
raise ValueError(f'{self} has no Montgomery model')
32173216

32183217
# square s allows us to take B=1
3219-
r,s = max(sols, key=lambda t: t[1].is_square())
3218+
r, s = max(sols, key=lambda t: t[1].is_square())
32203219

32213220
A = 3 * r / s
32223221
B = R.one() if s.is_square() else ~s

src/sage/schemes/generic/divisor.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ def CurvePointToIdeal(C, P):
8989
polys.append(x[i])
9090
else:
9191
polys.append(x[i]-ai)
92-
for i in range(m+1,n):
93-
polys.append(x[i])
92+
polys.extend(x[i] for i in range(m + 1, n))
9493
return R.ideal(polys)
9594

9695

src/sage/schemes/hyperelliptic_curves/hyperelliptic_finite_field.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,16 +1054,15 @@ def count_points_exhaustive(self, n=1, naive=False):
10541054
[9, 27, 108, 675, 3069, 16302]
10551055
"""
10561056
g = self.genus()
1057-
a = []
1058-
for i in range(1, min(n, g) + 1):
1059-
a.append(self.cardinality_exhaustive(extension_degree=i))
1057+
a = [self.cardinality_exhaustive(extension_degree=i)
1058+
for i in range(1, min(n, g) + 1)]
10601059

10611060
if n <= g:
10621061
return a
10631062

10641063
if naive:
1065-
for i in range(g + 1, n + 1):
1066-
a.append(self.cardinality_exhaustive(extension_degree=i))
1064+
a.extend(self.cardinality_exhaustive(extension_degree=i)
1065+
for i in range(g + 1, n + 1))
10671066

10681067
# let's not be too naive and compute the frobenius polynomial
10691068
f = self._frobenius_polynomial_cardinalities(a=a)

src/sage/schemes/hyperelliptic_curves/monsky_washnitzer.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -668,16 +668,8 @@ def transpose_list(input) -> list[list]:
668668
sage: transpose_list(L)
669669
[[1, 3, 5], [2, 4, 6]]
670670
"""
671-
h = len(input)
672671
w = len(input[0])
673-
674-
output = []
675-
for i in range(w):
676-
row = []
677-
for j in range(h):
678-
row.append(input[j][i])
679-
output.append(row)
680-
return output
672+
return [[input_j[i] for input_j in input] for i in range(w)]
681673

682674

683675
def helper_matrix(Q):

src/sage/schemes/product_projective/rational_point.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,20 @@ def enum_product_projective_rational_field(X, B):
133133

134134
R = X.codomain().ambient_space()
135135
m = R.num_components()
136-
iters = [ R[i].points_of_bounded_height(bound=B) for i in range(m) ]
136+
iters = [R[i].points_of_bounded_height(bound=B) for i in range(m)]
137137
dim = [R[i].dimension_relative() + 1 for i in range(m)]
138138

139-
dim_prefix = [0, dim[0]] # prefixes dim list
139+
dim_prefix = [0, dim[0]] # prefixes dim list
140140
for i in range(1, len(dim)):
141141
dim_prefix.append(dim_prefix[i] + dim[i])
142142

143143
pts = []
144144
P = []
145145
for i in range(m):
146146
pt = next(iters[i])
147-
for j in range(dim[i]):
148-
P.append(pt[j]) # initial value of P
147+
P.extend(pt[j] for j in range(dim[i])) # initial value of P
149148

150-
try: # add the initial point
149+
try: # add the initial point
151150
pts.append(X(P))
152151
except TypeError:
153152
pass
@@ -450,18 +449,12 @@ def points_modulo_primes(X, primes):
450449
Return a list of rational points modulo all `p` in primes,
451450
computed parallelly.
452451
"""
453-
normalized_input = []
454-
for p in primes_list:
455-
normalized_input.append(((X, p, ), {}))
452+
normalized_input = [((X, p, ), {}) for p in primes_list]
456453
p_iter = p_iter_fork(ncpus())
457454

458455
points_pair = list(p_iter(parallel_function, normalized_input))
459456
points_pair.sort()
460-
modulo_points = []
461-
for pair in points_pair:
462-
modulo_points.append(pair[1])
463-
464-
return modulo_points
457+
return [pair[1] for pair in points_pair]
465458

466459
def parallel_function_combination(point_p_max):
467460
r"""
@@ -511,12 +504,10 @@ def lift_all_points():
511504
r"""
512505
Return list of all rational points lifted parallelly.
513506
"""
514-
normalized_input = []
515-
points = modulo_points.pop() # remove the list of points corresponding to largest prime
507+
points = modulo_points.pop() # remove the list of points corresponding to largest prime
516508
len_modulo_points.pop()
517509

518-
for point in points:
519-
normalized_input.append(( (point, ), {}))
510+
normalized_input = [((point, ), {}) for point in points]
520511
p_iter = p_iter_fork(ncpus())
521512
points_satisfying = list(p_iter(parallel_function_combination, normalized_input))
522513

src/sage/schemes/projective/proj_bdd_height.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def IQ_points_of_bounded_height(PS, K, dim, bound):
186186
possible_norm_set = set()
187187
for i in range(class_number):
188188
for k in range(1, floor(bound + 1)):
189-
possible_norm_set.add(k*class_group_ideal_norms[i])
189+
possible_norm_set.add(k * class_group_ideal_norms[i])
190190

191191
coordinate_space = {}
192192
coordinate_space[0] = [K(0)]
@@ -199,11 +199,9 @@ def IQ_points_of_bounded_height(PS, K, dim, bound):
199199
a_norm_bound = bound * a_norm
200200
a_coordinates = []
201201

202-
for m in coordinate_space:
202+
for m, coord_m in coordinate_space.items():
203203
if m <= a_norm_bound:
204-
for x in coordinate_space[m]:
205-
if x in a:
206-
a_coordinates.append(x)
204+
a_coordinates.extend(x for x in coord_m if x in a)
207205

208206
points_in_class_a = set()
209207
t = len(a_coordinates) - 1
@@ -213,7 +211,7 @@ def IQ_points_of_bounded_height(PS, K, dim, bound):
213211
if a == K.ideal(point_coordinates):
214212
for p in itertools.permutations(point_coordinates):
215213
for u in unit_tuples:
216-
point = PS([i*j for i, j in zip(u, p)] + [p[dim]])
214+
point = PS([i * j for i, j in zip(u, p)] + [p[dim]])
217215

218216
if point not in points_in_class_a:
219217
points_in_class_a.add(point)
@@ -360,21 +358,18 @@ def points_of_bounded_height(PS, K, dim, bound, prec=53):
360358
vertex = sum([coefficient_tuple[i]*fund_unit_logs[i] for i in range(r)])
361359
fund_parallelotope_vertices.append(vertex)
362360

363-
D_numbers = []
364-
for v in range(r + 1):
365-
D_numbers.append(max([vertex[v] for vertex in fund_parallelotope_vertices]))
361+
D_numbers = [max(vertex[v] for vertex in fund_parallelotope_vertices)
362+
for v in range(r + 1)]
366363

367-
A_numbers = []
368-
for v in range(r + 1):
369-
A_numbers.append(min([pr_ideal_gen_logs[y][v] for y in pr_ideal_gen_logs]))
364+
A_numbers = [min(pr_ideal_gen_logs[y][v] for y in pr_ideal_gen_logs)
365+
for v in range(r + 1)]
370366

371-
aux_constant = (1/K_degree) * Reals(norm_bound).log()
367+
aux_constant = (1 / K_degree) * Reals(norm_bound).log()
372368

373-
L_numbers = []
374-
for v in range(r1):
375-
L_numbers.append(aux_constant + D_numbers[v] - A_numbers[v])
376-
for v in range(r1, r + 1):
377-
L_numbers.append(2*aux_constant + D_numbers[v] - A_numbers[v])
369+
L_numbers = [aux_constant + D_numbers[v] - A_numbers[v]
370+
for v in range(r1)]
371+
L_numbers.extend(2 * aux_constant + D_numbers[v] - A_numbers[v]
372+
for v in range(r1, r + 1))
378373
L_numbers = vector(L_numbers).change_ring(QQ)
379374

380375
T = column_matrix(fund_unit_logs).delete_rows([r]).change_ring(QQ)

src/sage/schemes/projective/projective_point.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -676,14 +676,14 @@ def dehomogenize(self, n):
676676
...
677677
ValueError: can...t dehomogenize at 0 coordinate
678678
"""
679-
if self[n].is_zero():
679+
sn = self[n]
680+
if sn.is_zero():
680681
raise ValueError("can't dehomogenize at 0 coordinate")
681682
PS = self.codomain()
682683
A = PS.affine_patch(n)
683-
Q = []
684-
for i in range(PS.ambient_space().dimension_relative() + 1):
685-
if i != n:
686-
Q.append(self[i] / self[n])
684+
Q = [self[i] / sn
685+
for i in range(PS.ambient_space().dimension_relative() + 1)
686+
if i != n]
687687
return A.point(Q)
688688

689689
def global_height(self, prec=None):

0 commit comments

Comments
 (0)