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

some list comprehensions in rings (ruff PERF401) #37468

Merged
merged 3 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -2647,10 +2647,8 @@ def is_convenient_multiple_point(self, p):

# Test 4: Is p convenient?
M = matrix(self.log_grads(p))
convenient_coordinates = []
for j in range(d):
if 0 not in M.columns()[j]:
convenient_coordinates.append(j)
convenient_coordinates = [j for j in range(d)
if 0 not in M.columns()[j]]
fchapoton marked this conversation as resolved.
Show resolved Hide resolved
if not convenient_coordinates:
return (False, 'multiple point but not convenient')

Expand Down Expand Up @@ -2746,10 +2744,8 @@ def smooth_critical_ideal(self, alpha):
d = self.dimension()

# Expand K by the variables of alpha if there are any.
indets = []
for a in alpha:
if a not in K and a in SR:
indets.append(a)
indets = [a for a in alpha if a not in K and a in SR]

indets = sorted(set(indets), key=str) # Delete duplicates in indets.
if indets:
L = PolynomialRing(K, indets).fraction_field()
Expand Down Expand Up @@ -2845,9 +2841,7 @@ def maclaurin_coefficients(self, multi_indices, numerical=0):
return coeffs

# Create biggest multi-index needed.
alpha = []
for i in range(d):
alpha.append(max(nu[i] for nu in multi_indices))
alpha = [max(nu[i] for nu in multi_indices) for i in range(d)]

# Compute Maclaurin expansion of self up to index alpha.
# Use iterated univariate expansions.
Expand Down
12 changes: 3 additions & 9 deletions src/sage/rings/derivation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1133,9 +1133,7 @@ def _bracket_(self, other):
parent = self.parent()
if parent.domain() is not parent.codomain():
raise TypeError("the bracket is only defined for derivations with same domain and codomain")
arg = [ ]
for x in parent.dual_basis():
arg.append(self(other(x)) - other(self(x)))
arg = [self(other(x)) - other(self(x)) for x in parent.dual_basis()]
return parent(arg)

def pth_power(self):
Expand Down Expand Up @@ -1268,9 +1266,7 @@ def precompose(self, morphism):
elif not (isinstance(morphism, Map) and morphism.category_for().is_subcategory(Rings())):
raise TypeError("you must give a homomorphism of rings")
M = RingDerivationModule(morphism.domain(), parent.defining_morphism() * morphism)
arg = [ ]
for x in M.dual_basis():
arg.append(self(morphism(x)))
arg = [self(morphism(x)) for x in M.dual_basis()]
return M(arg)

def postcompose(self, morphism):
Expand Down Expand Up @@ -1328,9 +1324,7 @@ def postcompose(self, morphism):
elif not (isinstance(morphism, Map) and morphism.category_for().is_subcategory(Rings())):
raise TypeError("you must give a homomorphism of rings")
M = RingDerivationModule(parent.domain(), morphism * parent.defining_morphism())
arg = [ ]
for x in M.dual_basis():
arg.append(morphism(self(x)))
arg = [morphism(self(x)) for x in M.dual_basis()]
return M(arg)

def extend_to_fraction_field(self):
Expand Down
9 changes: 4 additions & 5 deletions src/sage/rings/fraction_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,13 +917,12 @@ def some_elements(self):
(2*x^2 + 2)/x^3,
(2*x^2 + 2)/(x^2 - 1),
2]

"""
ret = [self.zero(), self.one()]
for a in self._R.some_elements():
for b in self._R.some_elements():
if a != b and self(a) and self(b):
ret.append(self(a) / self(b))
ret.extend(self(a) / self(b)
for a in self._R.some_elements()
for b in self._R.some_elements()
if a != b and self(a) and self(b))
return ret

def _gcd_univariate_polynomial(self, f, g):
Expand Down
7 changes: 4 additions & 3 deletions src/sage/rings/function_field/divisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,9 +871,10 @@ def _basis(self):
# invariants of M.
basis = []
for j in range(n):
i,ideg = pivot_row[j][0]
for k in range( den.degree() - ideg + 1 ):
basis.append(one.shift(k) * gens[i])
i, ideg = pivot_row[j][0]
gi = gens[i]
basis.extend(one.shift(k) * gi
for k in range(den.degree() - ideg + 1))
# Done!
return basis

Expand Down
17 changes: 7 additions & 10 deletions src/sage/rings/invariants/invariant_theory.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,18 +1038,15 @@ def monomials(self):
def prod(a, b):
if a is None and b is None:
return self._ring.one()
elif a is None:
if a is None:
return b
elif b is None:
if b is None:
return a
else:
return a * b
squares = tuple( prod(x,x) for x in var )
mixed = []
for i in range(self._n):
for j in range(i+1, self._n):
mixed.append(prod(var[i], var[j]))
mixed = tuple(mixed)
return a * b

squares = tuple(prod(x, x) for x in var)
mixed = tuple([prod(var[i], var[j]) for i in range(self._n)
for j in range(i + 1, self._n)])
return squares + mixed

@cached_method
Expand Down
10 changes: 4 additions & 6 deletions src/sage/rings/number_field/S_unit_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,21 @@ def c3_func(SUK, prec=106):
- [AKMRVW]_ :arxiv:`1903.00977`

"""

R = RealField(prec)

all_places = list(SUK.primes()) + SUK.number_field().places(prec)
Possible_U = Combinations(all_places, SUK.rank())
c1 = R(1) # guarantees final c1 >= 1
c1 = R(1) # guarantees final c1 >= 1
for U in Possible_U:
# first, build the matrix C_{i,U}
columns_of_C = []
for unit in SUK.fundamental_units():
columns_of_C.append(column_Log(SUK, unit, U, prec))
columns_of_C = [column_Log(SUK, unit, U, prec)
for unit in SUK.fundamental_units()]
C = matrix(SUK.rank(), SUK.rank(), columns_of_C)
# Is it invertible?
if abs(C.determinant()) > 10**(-10):
poss_c1 = C.inverse().apply_map(abs).norm(Infinity)
c1 = R(max(poss_c1, c1))
return R(0.9999999) / (c1*SUK.rank())
return R(0.9999999) / (c1 * SUK.rank())


def c4_func(SUK, v, A, prec=106):
Expand Down
11 changes: 4 additions & 7 deletions src/sage/rings/number_field/bdd_height.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,16 @@ def bdd_height_iq(K, height_bound):
possible_norm_set = set()
for n in range(class_number):
for m in range(1, int(height_bound + 1)):
possible_norm_set.add(m*class_group_rep_norms[n])
possible_norm_set.add(m * class_group_rep_norms[n])
bdd_ideals = bdd_norm_pr_gens_iq(K, possible_norm_set)

# Distribute the principal ideals
generator_lists = []
for n in range(class_number):
this_ideal = class_group_reps[n]
this_ideal_norm = class_group_rep_norms[n]
gens = []
for i in range(1, int(height_bound + 1)):
for g in bdd_ideals[i*this_ideal_norm]:
if g in this_ideal:
gens.append(g)
gens = [g for i in range(1, int(height_bound + 1))
for g in bdd_ideals[i * this_ideal_norm] if g in this_ideal]
generator_lists.append(gens)

# Build all the output numbers
Expand All @@ -210,7 +207,7 @@ def bdd_height_iq(K, height_bound):
for i in range(s):
for j in range(i + 1, s):
if K.ideal(gens[i], gens[j]) == class_group_reps[n]:
new_number = gens[i]/gens[j]
new_number = gens[i] / gens[j]
for zeta in roots_of_unity:
yield zeta * new_number
yield zeta / new_number
Expand Down
11 changes: 4 additions & 7 deletions src/sage/rings/number_field/number_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -10438,9 +10438,8 @@ def hilbert_symbol_negative_at_S(self, S, b, check=True):
L.append(P)

# This adds some infinite places to L
for sigma in self.real_places():
if sigma(b) < 0 and sigma not in S:
L.append(sigma)
L.extend(sigma for sigma in self.real_places()
if sigma(b) < 0 and sigma not in S)
Cl = self.class_group(proof=False)
U = self.unit_group(proof=False).gens()
SL = S + L
Expand All @@ -10462,10 +10461,8 @@ def hilbert_symbol_negative_at_S(self, S, b, check=True):
# on the set of generators

def phi(x):
v = []
for p in SL:
v.append((1-self.hilbert_symbol(x, b, p))//2)
return V(v)
return V([(1 - self.hilbert_symbol(x, b, p)) // 2 for p in SL])

M = matrix([phi(g) for g in U])

# we have to work around the inconvenience that multiplicative
Expand Down
8 changes: 3 additions & 5 deletions src/sage/rings/number_field/totallyreal_rel.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,15 +453,13 @@ def incr(self, f_out, verbose=False, haltk=0):
# Enumerate all elements of Z_F with T_2 <= br
T2s = []
trace_elts_found = False
for i in range(len(self.trace_elts)):
tre = self.trace_elts[i]
for tre in self.trace_elts:
if tre[0] <= bl and tre[1] >= br:
trace_elts_found = True
if verbose >= 2:
print(" found copy!")
for theta in tre[2]:
if theta.trace() >= bl and theta.trace() <= br:
T2s.append(theta)
T2s.extend(theta for theta in tre[2]
if bl <= theta.trace() <= br)
break
if not trace_elts_found:
T2s = self.F._positive_integral_elements_with_trace([bl,br])
Expand Down
7 changes: 3 additions & 4 deletions src/sage/rings/padics/generic_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,10 +683,8 @@ def convert_multiple(self, *elts):
raise NotImplementedError("multiple conversion of a set of variables for which the module precision is not a lattice is not implemented yet")
for j in range(len(L)):
x = L[j]
dx = []
for i in range(j):
dx.append([L[i], lattice[i,j]])
prec = lattice[j,j].valuation(p)
dx = [[L[i], lattice[i, j]] for i in range(j)]
prec = lattice[j, j].valuation(p)
y = self._element_class(self, x.value(), prec, dx=dx, dx_mode='values', check=False, reduce=False)
for i in indices[id(x)]:
ans[i] = y
Expand All @@ -700,6 +698,7 @@ def convert_multiple(self, *elts):
# We return the created elements
return ans


class pAdicRelaxedGeneric(pAdicGeneric):
r"""
Generic class for relaxed `p`-adics.
Expand Down
7 changes: 3 additions & 4 deletions src/sage/rings/qqbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -4801,11 +4801,10 @@ def radical_expression(self):
roots = poly.roots(SR, multiplicities=False)
if len(roots) != poly.degree():
return self
itv = interval_field(self._value)
while True:
candidates = []
for root in roots:
if interval_field(root).overlaps(interval_field(self._value)):
candidates.append(root)
candidates = [root for root in roots
if interval_field(root).overlaps(itv)]
if len(candidates) == 1:
return candidates[0]
roots = candidates
Expand Down
Loading