Skip to content

Commit 5ebb4c8

Browse files
author
Release Manager
committed
sagemathgh-37984: Ruff details in algebras and categories various small fixes for ruff suggestions in algebras and categories ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. URL: sagemath#37984 Reported by: Frédéric Chapoton Reviewer(s): Matthias Köppe
2 parents fb543b7 + 9ec8971 commit 5ebb4c8

15 files changed

+81
-94
lines changed

src/sage/algebras/cluster_algebra.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1769,7 +1769,7 @@ def d_vector_to_g_vector(self, d) -> tuple:
17691769
sage: A.d_vector_to_g_vector((1,0,-1))
17701770
(-1, 1, 2)
17711771
"""
1772-
dm = vector((x if x < 0 else 0 for x in d))
1772+
dm = vector(x if x < 0 else 0 for x in d)
17731773
dp = vector(d) - dm
17741774
return tuple(- dm - self.euler_matrix() * dp)
17751775

src/sage/algebras/fusion_rings/f_matrix.py

Lines changed: 47 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from sage.rings.polynomial.polydict import ETuple
4242
from sage.rings.qqbar import AA, QQbar, number_field_elements_from_algebraics
4343

44+
4445
class FMatrix(SageObject):
4546
r"""
4647
An F-matrix for a :class:`FusionRing`.
@@ -297,7 +298,7 @@ def __init__(self, fusion_ring, fusion_label="f", var_prefix='fx', inject_variab
297298
self.pool = None
298299

299300
#######################
300-
### Class utilities ###
301+
# Class utilities #
301302
#######################
302303

303304
def _repr_(self):
@@ -434,35 +435,31 @@ def fmat(self, a, b, c, d, x, y, data=True):
434435
(-zeta60^14 + zeta60^6 + zeta60^4 - 1),
435436
(zeta60^14 - zeta60^6 - zeta60^4 + 1)]
436437
"""
437-
if (self._FR.Nk_ij(a, b, x) == 0 or self._FR.Nk_ij(x, c, d) == 0
438-
or self._FR.Nk_ij(b, c, y) == 0 or self._FR.Nk_ij(a, y, d) == 0):
438+
if (self._FR.Nk_ij(a, b, x) == 0
439+
or self._FR.Nk_ij(x, c, d) == 0
440+
or self._FR.Nk_ij(b, c, y) == 0
441+
or self._FR.Nk_ij(a, y, d) == 0):
439442
return 0
440443

441444
# Some known zero F-symbols
442445
if a == self._FR.one():
443-
if x == b and y == d:
444-
return 1
445-
else:
446-
return 0
446+
return 1 if x == b and y == d else 0
447+
447448
if b == self._FR.one():
448-
if x == a and y == c:
449-
return 1
450-
else:
451-
return 0
449+
return 1 if x == a and y == c else 0
450+
452451
if c == self._FR.one():
453-
if x == d and y == b:
454-
return 1
455-
else:
456-
return 0
452+
return 1 if x == d and y == b else 0
453+
457454
if data:
458455
# Better to use try/except for speed. Somewhat trivial, but worth
459456
# hours when method is called ~10^11 times
460457
try:
461458
return self._fvars[a, b, c, d, x, y]
462459
except KeyError:
463460
return 0
464-
else:
465-
return (a, b, c, d, x, y)
461+
462+
return (a, b, c, d, x, y)
466463

467464
def fmatrix(self, a, b, c, d):
468465
r"""
@@ -583,7 +580,7 @@ def findcases(self, output=False):
583580
idx_map = {}
584581
ret = {}
585582
id_anyon = self._FR.one()
586-
for (a, b, c, d) in product(self._FR.basis(), repeat=4):
583+
for a, b, c, d in product(self._FR.basis(), repeat=4):
587584
if a == id_anyon or b == id_anyon or c == id_anyon:
588585
continue
589586
for x in self.f_from(a, b, c, d):
@@ -593,10 +590,8 @@ def findcases(self, output=False):
593590
ret[(a, b, c, d, x, y)] = v
594591
idx_map[i] = (a, b, c, d, x, y)
595592
i += 1
596-
if output:
597-
return idx_map, ret
598-
else:
599-
return i
593+
594+
return (idx_map, ret) if output else i
600595

601596
def f_from(self, a, b, c, d):
602597
r"""
@@ -649,7 +644,7 @@ def f_to(self, a, b, c, d):
649644
if self._FR.Nk_ij(b, c, y) != 0 and self._FR.Nk_ij(a, y, d) != 0]
650645

651646
####################
652-
### Data getters ###
647+
# Data getters #
653648
####################
654649

655650
def get_fvars(self):
@@ -855,7 +850,7 @@ def get_radical_expression(self):
855850
return {sextuple: val.radical_expression() for sextuple, val in self.get_fvars_in_alg_field().items()}
856851

857852
#######################
858-
### Private helpers ###
853+
# Private helpers #
859854
#######################
860855

861856
def _get_known_vals(self):
@@ -894,12 +889,12 @@ def _get_known_nonz(self):
894889
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100)
895890
"""
896891
nonz = {idx: 100 for idx in self._singles}
897-
for idx, v in self._ks.items():
892+
for idx, _ in self._ks.items():
898893
nonz[idx] = 100
899894
return ETuple(nonz, self._poly_ring.ngens())
900895

901896
##############################
902-
### Variables partitioning ###
897+
# Variables partitioning #
903898
##############################
904899

905900
def largest_fmat_size(self):
@@ -967,7 +962,7 @@ def get_fvars_by_size(self, n, indices=False):
967962
return var_set
968963

969964
############################
970-
### Checkpoint utilities ###
965+
# Checkpoint utilities #
971966
############################
972967

973968
def save_fvars(self, filename):
@@ -1009,12 +1004,10 @@ def save_fvars(self, filename):
10091004
True
10101005
sage: os.remove(filename)
10111006
"""
1012-
final_state = [
1013-
self._fvars,
1014-
self._non_cyc_roots,
1015-
self.get_coerce_map_from_fr_cyclotomic_field(),
1016-
self._qqbar_embedding,
1017-
]
1007+
final_state = [self._fvars,
1008+
self._non_cyc_roots,
1009+
self.get_coerce_map_from_fr_cyclotomic_field(),
1010+
self._qqbar_embedding]
10181011
with open(filename, 'wb') as f:
10191012
pickle.dump(final_state, f)
10201013

@@ -1212,7 +1205,7 @@ def _restore_state(self, filename):
12121205
self._update_reduction_params()
12131206

12141207
#################
1215-
### MapReduce ###
1208+
# MapReduce #
12161209
#################
12171210

12181211
def start_worker_pool(self, processes=None):
@@ -1275,7 +1268,7 @@ class methods.
12751268
self._reset_solver_state()
12761269
# Set up shared memory resource handlers
12771270
n_proc = cpu_count() if processes is None else processes
1278-
self._pid_list = shared_memory.ShareableList([0]*(n_proc+1))
1271+
self._pid_list = shared_memory.ShareableList([0] * (n_proc+1))
12791272
pids_name = self._pid_list.shm.name
12801273
self._solved = shared_memory.ShareableList(self._solved)
12811274
s_name = self._solved.shm.name
@@ -1304,7 +1297,7 @@ def init(fmats_id, solved_name, vd_name, ks_names, fvar_names, n_proc, pids_name
13041297
self.pool = Pool(processes=n_proc, initializer=init, initargs=args)
13051298
self._pid_list[0] = getpid()
13061299
for i, p in enumerate(self.pool._pool):
1307-
self._pid_list[i+1] = p.pid
1300+
self._pid_list[i + 1] = p.pid
13081301
# return True
13091302

13101303
def shutdown_worker_pool(self):
@@ -1394,7 +1387,7 @@ def _map_triv_reduce(self, mapper, input_iter, worker_pool=None, chunksize=None,
13941387
return results
13951388

13961389
########################
1397-
### Equations set up ###
1390+
# Equations set up #
13981391
########################
13991392

14001393
def get_orthogonality_constraints(self, output=True):
@@ -1494,7 +1487,9 @@ def get_defining_equations(self, option, output=True):
14941487
self._reset_solver_state()
14951488
n_proc = self.pool._processes if self.pool is not None else 1
14961489
params = [(child_id, n_proc, output) for child_id in range(n_proc)]
1497-
eqns = self._map_triv_reduce('get_reduced_'+option, params, worker_pool=self.pool, chunksize=1, mp_thresh=0)
1490+
eqns = self._map_triv_reduce('get_reduced_' + option, params,
1491+
worker_pool=self.pool, chunksize=1,
1492+
mp_thresh=0)
14981493
if output:
14991494
F = self._field
15001495
for i, eq_tup in enumerate(eqns):
@@ -1503,7 +1498,7 @@ def get_defining_equations(self, option, output=True):
15031498
self.ideal_basis.extend(eqns)
15041499

15051500
############################
1506-
### Equations processing ###
1501+
# Equations processing #
15071502
############################
15081503

15091504
def _tup_to_fpoly(self, eq_tup):
@@ -1611,7 +1606,7 @@ def _triangular_elim(self, eqns=None, verbose=True):
16111606
self.ideal_basis = eqns
16121607

16131608
#####################
1614-
### Graph methods ###
1609+
# Graph methods #
16151610
#####################
16161611

16171612
def equations_graph(self, eqns=None):
@@ -1820,7 +1815,7 @@ def _get_component_variety(self, var, eqns):
18201815
return [{inv_idx_map[i]: value for i, (key, value) in enumerate(sorted(soln.items()))} for soln in var_in_R]
18211816

18221817
#######################
1823-
### Solution method ###
1818+
# Solution method #
18241819
#######################
18251820

18261821
# TODO: this can probably be improved by constructing a set of defining polynomials
@@ -1919,7 +1914,7 @@ def _get_explicit_solution(self, eqns=None, verbose=True):
19191914
for fx, rhs in self._ks.items():
19201915
if not self._solved[fx]:
19211916
lt = (ETuple({fx: 2}, n), one)
1922-
eqns.append(((lt, (ETuple({}, n), -rhs))))
1917+
eqns.append((lt, (ETuple({}, n), -rhs)))
19231918
eqns_partition = self._partition_eqns(verbose=verbose)
19241919

19251920
F = self._field
@@ -1956,20 +1951,20 @@ def _get_explicit_solution(self, eqns=None, verbose=True):
19561951
if self.attempt_number_field_computation():
19571952
if verbose:
19581953
print("Computing appropriate NumberField...")
1959-
roots = [self._FR.field().gen()]+[r[1] for r in non_cyclotomic_roots]
1954+
roots = [self._FR.field().gen()] + [r[1] for r in non_cyclotomic_roots]
19601955
self._field, bf_elts, self._qqbar_embedding = number_field_elements_from_algebraics(roots, minimal=True)
19611956
else:
19621957
self._field = QQbar
19631958
bf_elts = [self._qqbar_embedding(F.gen())]
19641959
bf_elts += [rhs for fx, rhs in non_cyclotomic_roots]
1965-
self._qqbar_embedding = lambda x : x
1960+
self._qqbar_embedding = lambda x: x
19661961
self._non_cyc_roots = bf_elts[1:]
19671962

19681963
# Embed cyclotomic field into newly constructed base field
19691964
cyc_gen_as_bf_elt = bf_elts.pop(0)
19701965
phi = self._FR.field().hom([cyc_gen_as_bf_elt], self._field)
19711966
self._coerce_map_from_cyc_field = phi
1972-
numeric_fvars = {k : phi(v) for k, v in numeric_fvars.items()}
1967+
numeric_fvars = {k: phi(v) for k, v in numeric_fvars.items()}
19731968
for i, elt in enumerate(bf_elts):
19741969
numeric_fvars[non_cyclotomic_roots[i][0]] = elt
19751970
# Update polynomial ring
@@ -2114,7 +2109,7 @@ def find_orthogonal_solution(self, checkpoint=False, save_results="", warm_start
21142109
print("Set up {} hex and orthogonality constraints...".format(len(self.ideal_basis)))
21152110

21162111
# Unzip _fvars and link to shared_memory structure if using multiprocessing
2117-
if use_mp:# and loads_shared_memory:
2112+
if use_mp: # and loads_shared_memory:
21182113
self._fvars = self._shared_fvars
21192114
else:
21202115
n = self._poly_ring.ngens()
@@ -2164,12 +2159,12 @@ def find_orthogonal_solution(self, checkpoint=False, save_results="", warm_start
21642159
self._chkpt_status = 7
21652160
self.clear_equations()
21662161
if checkpoint:
2167-
remove("fmatrix_solver_checkpoint_"+self.get_fr_str()+".pickle")
2162+
remove("fmatrix_solver_checkpoint_" + self.get_fr_str() + ".pickle")
21682163
if save_results:
21692164
self.save_fvars(save_results)
21702165

21712166
#########################
2172-
### Cyclotomic method ###
2167+
# Cyclotomic method #
21732168
#########################
21742169

21752170
def _fix_gauge(self, algorithm=""):
@@ -2202,8 +2197,8 @@ def _fix_gauge(self, algorithm=""):
22022197
break
22032198

22042199
# Fix var = 1, substitute, and solve equations
2205-
self.ideal_basis.add(var-1)
2206-
print("adding equation...", var-1)
2200+
self.ideal_basis.add(var - 1)
2201+
print("adding equation...", var - 1)
22072202
self.ideal_basis = set(Ideal(list(self.ideal_basis)).groebner_basis(algorithm=algorithm))
22082203
self._substitute_degree_one()
22092204
self._update_equations()
@@ -2336,7 +2331,7 @@ def find_cyclotomic_solution(self, equations=None, algorithm="", verbose=True, o
23362331
if equations is None:
23372332
if verbose:
23382333
print("Setting up hexagons and pentagons...")
2339-
equations = self.get_defining_equations("hexagons")+self.get_defining_equations("pentagons")
2334+
equations = self.get_defining_equations("hexagons") + self.get_defining_equations("pentagons")
23402335
if verbose:
23412336
print("Finding a Groebner basis...")
23422337
self.ideal_basis = set(Ideal(equations).groebner_basis(algorithm=algorithm))
@@ -2352,7 +2347,7 @@ def find_cyclotomic_solution(self, equations=None, algorithm="", verbose=True, o
23522347
return self._fvars
23532348

23542349
#####################
2355-
### Verifications ###
2350+
# Verifications #
23562351
#####################
23572352

23582353
def fmats_are_orthogonal(self):

src/sage/algebras/lie_algebras/classical_lie_algebra.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,8 @@ def __init__(self, R, n):
447447
gens = []
448448
for i in range(n):
449449
for j in range(n):
450-
names.append('E_{0}_{1}'.format(i,j))
451-
mat = MS({(i,j):one})
450+
names.append('E_{}_{}'.format(i, j))
451+
mat = MS({(i, j): one})
452452
mat.set_immutable()
453453
gens.append(mat)
454454
self._n = n

src/sage/algebras/lie_algebras/free_lie_algebra.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def _repr_(self):
8080
sage: L.Lyndon()
8181
Free Lie algebra generated by (x, y) over Rational Field in the Lyndon basis
8282
"""
83-
return "{0} in the {1} basis".format(self.realization_of(), self._basis_name)
83+
return "{} in the {} basis".format(self.realization_of(), self._basis_name)
8484

8585
def _repr_term(self, x):
8686
"""

src/sage/algebras/lie_algebras/heisenberg.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ def step(self):
194194
class Element(LieAlgebraElement):
195195
pass
196196

197-
class HeisenbergAlgebra_fd():
197+
198+
class HeisenbergAlgebra_fd:
198199
"""
199200
Common methods for finite-dimensional Heisenberg algebras.
200201
"""
@@ -417,7 +418,8 @@ def _repr_(self):
417418
sage: lie_algebras.Heisenberg(QQ, 3)
418419
Heisenberg algebra of rank 3 over Rational Field
419420
"""
420-
return "Heisenberg algebra of rank {0} over {1}".format(self._n, self.base_ring())
421+
return "Heisenberg algebra of rank {} over {}".format(self._n, self.base_ring())
422+
421423

422424
class InfiniteHeisenbergAlgebra(HeisenbergAlgebra_abstract, LieAlgebraWithGenerators):
423425
r"""

src/sage/algebras/lie_algebras/lie_algebra.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ def __init__(self, R, names=None, index_set=None, category=None):
894894
LieAlgebraWithGenerators.__init__(self, R, names, index_set, category)
895895
self.__ngens = len(self._indices)
896896

897-
def _repr_(self):
897+
def _repr_(self) -> str:
898898
"""
899899
Return a string representation of ``self``.
900900
@@ -905,9 +905,9 @@ def _repr_(self):
905905
Lie algebra on 2 generators (x, y) over Rational Field
906906
"""
907907
if self.__ngens == 1:
908-
return "Lie algebra on the generator {0} over {1}".format(
908+
return "Lie algebra on the generator {} over {}".format(
909909
self.gen(0), self.base_ring())
910-
return "Lie algebra on {0} generators {1} over {2}".format(
910+
return "Lie algebra on {} generators {} over {}".format(
911911
self.__ngens, self.gens(), self.base_ring())
912912

913913
@lazy_attribute

0 commit comments

Comments
 (0)