From 91503c978296a36d453f87206d5eca8685c1b83b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Thu, 28 Aug 2025 20:54:09 +0200 Subject: [PATCH] fix ruff PERF in schemes --- src/sage/schemes/curves/affine_curve.py | 14 +++------ src/sage/schemes/curves/projective_curve.py | 9 ++---- src/sage/schemes/elliptic_curves/BSD.py | 10 +++--- .../schemes/elliptic_curves/ell_generic.py | 9 +++--- src/sage/schemes/generic/divisor.py | 3 +- .../hyperelliptic_finite_field.py | 9 +++--- .../hyperelliptic_curves/monsky_washnitzer.py | 10 +----- .../product_projective/rational_point.py | 25 +++++---------- .../schemes/projective/proj_bdd_height.py | 31 ++++++++----------- .../schemes/projective/projective_point.py | 10 +++--- .../projective/projective_rational_point.py | 15 +++------ .../schemes/projective/projective_space.py | 21 ++++--------- 12 files changed, 61 insertions(+), 105 deletions(-) diff --git a/src/sage/schemes/curves/affine_curve.py b/src/sage/schemes/curves/affine_curve.py index d4787972083..16824785953 100644 --- a/src/sage/schemes/curves/affine_curve.py +++ b/src/sage/schemes/curves/affine_curve.py @@ -1744,14 +1744,13 @@ def tangent_line(self, p): Tp = self.tangent_space(p) if Tp.dimension() > 1: - raise ValueError("the curve is not smooth at {}".format(p)) + raise ValueError(f"the curve is not smooth at {p}") from sage.schemes.curves.constructor import Curve # translate to p - I0 = [] - for poly in Tp.defining_polynomials(): - I0.append(poly.subs({x: x - c for x, c in zip(gens, p)})) + I0 = [poly.subs({x: x - c for x, c in zip(gens, p)}) + for poly in Tp.defining_polynomials()] return Curve(I0, A) @@ -2744,11 +2743,8 @@ def places_on(self, point): gs = [phi(g) for g in point.prime_ideal().gens()] fs = [g for g in gs if not g.is_zero()] f = fs.pop() - places = [] - for p in f.zeros(): - if all(f.valuation(p) > 0 for f in fs): - places.append(p) - return places + return [p for p in f.zeros() + if all(f.valuation(p) > 0 for f in fs)] def parametric_representation(self, place, name=None): """ diff --git a/src/sage/schemes/curves/projective_curve.py b/src/sage/schemes/curves/projective_curve.py index 4fa23344368..56871de729a 100644 --- a/src/sage/schemes/curves/projective_curve.py +++ b/src/sage/schemes/curves/projective_curve.py @@ -2862,14 +2862,11 @@ def places_on(self, point): phi = self._map_to_function_field denom = self._coordinate_functions[i] - gs = [phi(f)/denom**f.degree() for f in prime.gens()] + gs = [phi(f) / denom**f.degree() for f in prime.gens()] fs = [g for g in gs if not g.is_zero()] f = fs.pop() - places = [] - for p in f.zeros(): - if all(f.valuation(p) > 0 for f in fs): - places.append(p) - return places + return [p for p in f.zeros() + if all(f.valuation(p) > 0 for f in fs)] def jacobian(self, model, base_div=None): """ diff --git a/src/sage/schemes/elliptic_curves/BSD.py b/src/sage/schemes/elliptic_curves/BSD.py index 5ed20b13ed1..910a1dfea79 100644 --- a/src/sage/schemes/elliptic_curves/BSD.py +++ b/src/sage/schemes/elliptic_curves/BSD.py @@ -593,10 +593,12 @@ def prove_BSD(E, verbosity=0, two_desc='mwrank', proof=None, secs_hi=5, if p >= 5 and D_K % p and len(K.factor(p)) == 1: # p is inert in K BSD.primes.append(p) - for p in heegner_primes: - if p >= 5 and D_E % p and D_K % p and len(K.factor(p)) == 1: - # p is good for E and inert in K - kolyvagin_primes.append(p) + + kolyvagin_primes.extend(p for p in heegner_primes + # p is good for E and inert in K + if p >= 5 and D_E % p and D_K % p + and len(K.factor(p)) == 1) + for p in prime_divisors(BSD.sha_an): if p >= 5 and D_K % p and len(K.factor(p)) == 1: if BSD.curve.is_good(p): diff --git a/src/sage/schemes/elliptic_curves/ell_generic.py b/src/sage/schemes/elliptic_curves/ell_generic.py index 82074e11fac..49779a6d1d8 100644 --- a/src/sage/schemes/elliptic_curves/ell_generic.py +++ b/src/sage/schemes/elliptic_curves/ell_generic.py @@ -3207,16 +3207,15 @@ def montgomery_model(self, twisted=False, morphism=False): R = self.base_ring() P = PolynomialRing(R, 'v') - sols = [] - for r in P([b, a, 0, 1]).roots(multiplicities=False): - for s in P([3 * r**2 + a, 0, -1]).roots(multiplicities=False): - sols.append((r,s)) + sols = [(r, s) + for r in P([b, a, 0, 1]).roots(multiplicities=False) + for s in P([3 * r**2 + a, 0, -1]).roots(multiplicities=False)] if not sols: raise ValueError(f'{self} has no Montgomery model') # square s allows us to take B=1 - r,s = max(sols, key=lambda t: t[1].is_square()) + r, s = max(sols, key=lambda t: t[1].is_square()) A = 3 * r / s B = R.one() if s.is_square() else ~s diff --git a/src/sage/schemes/generic/divisor.py b/src/sage/schemes/generic/divisor.py index 64b28c0d70b..a54c31207c4 100644 --- a/src/sage/schemes/generic/divisor.py +++ b/src/sage/schemes/generic/divisor.py @@ -89,8 +89,7 @@ def CurvePointToIdeal(C, P): polys.append(x[i]) else: polys.append(x[i]-ai) - for i in range(m+1,n): - polys.append(x[i]) + polys.extend(x[i] for i in range(m + 1, n)) return R.ideal(polys) diff --git a/src/sage/schemes/hyperelliptic_curves/hyperelliptic_finite_field.py b/src/sage/schemes/hyperelliptic_curves/hyperelliptic_finite_field.py index 51cab0e6d9f..660507b965d 100644 --- a/src/sage/schemes/hyperelliptic_curves/hyperelliptic_finite_field.py +++ b/src/sage/schemes/hyperelliptic_curves/hyperelliptic_finite_field.py @@ -1054,16 +1054,15 @@ def count_points_exhaustive(self, n=1, naive=False): [9, 27, 108, 675, 3069, 16302] """ g = self.genus() - a = [] - for i in range(1, min(n, g) + 1): - a.append(self.cardinality_exhaustive(extension_degree=i)) + a = [self.cardinality_exhaustive(extension_degree=i) + for i in range(1, min(n, g) + 1)] if n <= g: return a if naive: - for i in range(g + 1, n + 1): - a.append(self.cardinality_exhaustive(extension_degree=i)) + a.extend(self.cardinality_exhaustive(extension_degree=i) + for i in range(g + 1, n + 1)) # let's not be too naive and compute the frobenius polynomial f = self._frobenius_polynomial_cardinalities(a=a) diff --git a/src/sage/schemes/hyperelliptic_curves/monsky_washnitzer.py b/src/sage/schemes/hyperelliptic_curves/monsky_washnitzer.py index 567cceb20d4..ac3ec459563 100644 --- a/src/sage/schemes/hyperelliptic_curves/monsky_washnitzer.py +++ b/src/sage/schemes/hyperelliptic_curves/monsky_washnitzer.py @@ -668,16 +668,8 @@ def transpose_list(input) -> list[list]: sage: transpose_list(L) [[1, 3, 5], [2, 4, 6]] """ - h = len(input) w = len(input[0]) - - output = [] - for i in range(w): - row = [] - for j in range(h): - row.append(input[j][i]) - output.append(row) - return output + return [[input_j[i] for input_j in input] for i in range(w)] def helper_matrix(Q): diff --git a/src/sage/schemes/product_projective/rational_point.py b/src/sage/schemes/product_projective/rational_point.py index 0173fdee72c..1e714998169 100644 --- a/src/sage/schemes/product_projective/rational_point.py +++ b/src/sage/schemes/product_projective/rational_point.py @@ -133,10 +133,10 @@ def enum_product_projective_rational_field(X, B): R = X.codomain().ambient_space() m = R.num_components() - iters = [ R[i].points_of_bounded_height(bound=B) for i in range(m) ] + iters = [R[i].points_of_bounded_height(bound=B) for i in range(m)] dim = [R[i].dimension_relative() + 1 for i in range(m)] - dim_prefix = [0, dim[0]] # prefixes dim list + dim_prefix = [0, dim[0]] # prefixes dim list for i in range(1, len(dim)): dim_prefix.append(dim_prefix[i] + dim[i]) @@ -144,10 +144,9 @@ def enum_product_projective_rational_field(X, B): P = [] for i in range(m): pt = next(iters[i]) - for j in range(dim[i]): - P.append(pt[j]) # initial value of P + P.extend(pt[j] for j in range(dim[i])) # initial value of P - try: # add the initial point + try: # add the initial point pts.append(X(P)) except TypeError: pass @@ -450,18 +449,12 @@ def points_modulo_primes(X, primes): Return a list of rational points modulo all `p` in primes, computed parallelly. """ - normalized_input = [] - for p in primes_list: - normalized_input.append(((X, p, ), {})) + normalized_input = [((X, p, ), {}) for p in primes_list] p_iter = p_iter_fork(ncpus()) points_pair = list(p_iter(parallel_function, normalized_input)) points_pair.sort() - modulo_points = [] - for pair in points_pair: - modulo_points.append(pair[1]) - - return modulo_points + return [pair[1] for pair in points_pair] def parallel_function_combination(point_p_max): r""" @@ -511,12 +504,10 @@ def lift_all_points(): r""" Return list of all rational points lifted parallelly. """ - normalized_input = [] - points = modulo_points.pop() # remove the list of points corresponding to largest prime + points = modulo_points.pop() # remove the list of points corresponding to largest prime len_modulo_points.pop() - for point in points: - normalized_input.append(( (point, ), {})) + normalized_input = [((point, ), {}) for point in points] p_iter = p_iter_fork(ncpus()) points_satisfying = list(p_iter(parallel_function_combination, normalized_input)) diff --git a/src/sage/schemes/projective/proj_bdd_height.py b/src/sage/schemes/projective/proj_bdd_height.py index e58922aab41..39be55337b1 100644 --- a/src/sage/schemes/projective/proj_bdd_height.py +++ b/src/sage/schemes/projective/proj_bdd_height.py @@ -186,7 +186,7 @@ def IQ_points_of_bounded_height(PS, K, dim, bound): possible_norm_set = set() for i in range(class_number): for k in range(1, floor(bound + 1)): - possible_norm_set.add(k*class_group_ideal_norms[i]) + possible_norm_set.add(k * class_group_ideal_norms[i]) coordinate_space = {} coordinate_space[0] = [K(0)] @@ -199,11 +199,9 @@ def IQ_points_of_bounded_height(PS, K, dim, bound): a_norm_bound = bound * a_norm a_coordinates = [] - for m in coordinate_space: + for m, coord_m in coordinate_space.items(): if m <= a_norm_bound: - for x in coordinate_space[m]: - if x in a: - a_coordinates.append(x) + a_coordinates.extend(x for x in coord_m if x in a) points_in_class_a = set() t = len(a_coordinates) - 1 @@ -213,7 +211,7 @@ def IQ_points_of_bounded_height(PS, K, dim, bound): if a == K.ideal(point_coordinates): for p in itertools.permutations(point_coordinates): for u in unit_tuples: - point = PS([i*j for i, j in zip(u, p)] + [p[dim]]) + point = PS([i * j for i, j in zip(u, p)] + [p[dim]]) if point not in points_in_class_a: points_in_class_a.add(point) @@ -360,21 +358,18 @@ def points_of_bounded_height(PS, K, dim, bound, prec=53): vertex = sum([coefficient_tuple[i]*fund_unit_logs[i] for i in range(r)]) fund_parallelotope_vertices.append(vertex) - D_numbers = [] - for v in range(r + 1): - D_numbers.append(max([vertex[v] for vertex in fund_parallelotope_vertices])) + D_numbers = [max(vertex[v] for vertex in fund_parallelotope_vertices) + for v in range(r + 1)] - A_numbers = [] - for v in range(r + 1): - A_numbers.append(min([pr_ideal_gen_logs[y][v] for y in pr_ideal_gen_logs])) + A_numbers = [min(pr_ideal_gen_logs[y][v] for y in pr_ideal_gen_logs) + for v in range(r + 1)] - aux_constant = (1/K_degree) * Reals(norm_bound).log() + aux_constant = (1 / K_degree) * Reals(norm_bound).log() - L_numbers = [] - for v in range(r1): - L_numbers.append(aux_constant + D_numbers[v] - A_numbers[v]) - for v in range(r1, r + 1): - L_numbers.append(2*aux_constant + D_numbers[v] - A_numbers[v]) + L_numbers = [aux_constant + D_numbers[v] - A_numbers[v] + for v in range(r1)] + L_numbers.extend(2 * aux_constant + D_numbers[v] - A_numbers[v] + for v in range(r1, r + 1)) L_numbers = vector(L_numbers).change_ring(QQ) T = column_matrix(fund_unit_logs).delete_rows([r]).change_ring(QQ) diff --git a/src/sage/schemes/projective/projective_point.py b/src/sage/schemes/projective/projective_point.py index 87858525117..60ba4628d67 100644 --- a/src/sage/schemes/projective/projective_point.py +++ b/src/sage/schemes/projective/projective_point.py @@ -676,14 +676,14 @@ def dehomogenize(self, n): ... ValueError: can...t dehomogenize at 0 coordinate """ - if self[n].is_zero(): + sn = self[n] + if sn.is_zero(): raise ValueError("can't dehomogenize at 0 coordinate") PS = self.codomain() A = PS.affine_patch(n) - Q = [] - for i in range(PS.ambient_space().dimension_relative() + 1): - if i != n: - Q.append(self[i] / self[n]) + Q = [self[i] / sn + for i in range(PS.ambient_space().dimension_relative() + 1) + if i != n] return A.point(Q) def global_height(self, prec=None): diff --git a/src/sage/schemes/projective/projective_rational_point.py b/src/sage/schemes/projective/projective_rational_point.py index 56a2d573f16..1798b402ff5 100644 --- a/src/sage/schemes/projective/projective_rational_point.py +++ b/src/sage/schemes/projective/projective_rational_point.py @@ -467,18 +467,14 @@ def parallel_function(X, p): all rational points in modulo ring. """ Xp = X.change_ring(GF(p)) - L = Xp.rational_points() - - return [list(_) for _ in L] + return [list(p) for p in Xp.rational_points()] def points_modulo_primes(X, primes): r""" Return a list of rational points modulo all `p` in primes, computed parallelly. """ - normalized_input = [] - for p in primes_list: - normalized_input.append(((X, p, ), {})) + normalized_input = [((X, p, ), {}) for p in primes_list] p_iter = p_iter_fork(ncpus()) points_pair = list(p_iter(parallel_function, normalized_input)) @@ -528,12 +524,11 @@ def lift_all_points(): r""" Return list of all rational points lifted parallelly. """ - normalized_input = [] - points = modulo_points.pop() # remove the list of points corresponding to largest prime + points = modulo_points.pop() # remove the list of points corresponding to largest prime len_modulo_points.pop() - for point in points: - normalized_input.append(( (point, ), {})) + normalized_input = [((point, ), {}) for point in points] + p_iter = p_iter_fork(ncpus()) points_satisfying = list(p_iter(parallel_function_combination, normalized_input)) diff --git a/src/sage/schemes/projective/projective_space.py b/src/sage/schemes/projective/projective_space.py index 1ff1773f07b..9da4038ee3f 100644 --- a/src/sage/schemes/projective/projective_space.py +++ b/src/sage/schemes/projective/projective_space.py @@ -2034,10 +2034,7 @@ def is_linearly_independent(self, points, n=None): all_subsets = Subsets(range(len(points)), n) linearly_independent = True for subset in all_subsets: - point_list = [] - for index in subset: - point_list.append(list(points[index])) - M = matrix(point_list) + M = matrix([list(points[index]) for index in subset]) if M.rank() != n: linearly_independent = False break @@ -2165,18 +2162,13 @@ def subscheme_from_Chow_form(self, Ch, dim): if binomial(n + 1, n - dim) != R.ngens(): raise ValueError("for given dimension, there should be %d variables in the Chow form" % binomial(n + 1, n - dim)) # create the brackets associated to variables - L1 = [] - for t in UnorderedTuples(list(range(n + 1)), dim + 1): - if all(t[i] < t[i + 1] for i in range(dim)): - L1.append(list(t)) + L1 = [list(t) for t in UnorderedTuples(range(n + 1), dim + 1) + if all(t[i] < t[i + 1] for i in range(dim))] # create the dual brackets L2 = [] signs = [] for l in L1: - s = [] - for v in range(n + 1): - if v not in l: - s.append(v) + s = [v for v in range(n + 1) if v not in l] t1 = [b + 1 for b in l] t2 = [b + 1 for b in s] perm = Permutation(t1 + t2) @@ -2190,9 +2182,8 @@ def subscheme_from_Chow_form(self, Ch, dim): else: T = PolynomialRing(R.base_ring(), n + 1, 'z') M = matrix(T, n - dim, n + 1, list(T.gens())) - coords = [] - for i in range(len(L2)): - coords.append(signs[i] * M.matrix_from_columns(L2[i]).det()) + coords = [si * M.matrix_from_columns(L2i).det() + for si, L2i in zip(signs, L2)] # substitute in dual brackets to chow form phi = R.hom(coords, T) ch = phi(Ch)