From eb3695fd1f9b61cdacbad625576f4a5f17a9115e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Sat, 11 Jan 2025 10:03:50 +0100 Subject: [PATCH 1/2] partial pep8 cleanup in Lie conformal algebras --- .../abelian_lie_conformal_algebra.py | 11 ++--- .../affine_lie_conformal_algebra.py | 4 +- .../bosonic_ghosts_lie_conformal_algebra.py | 4 +- .../fermionic_ghosts_lie_conformal_algebra.py | 4 +- .../free_bosons_lie_conformal_algebra.py | 28 ++++++------ .../free_fermions_lie_conformal_algebra.py | 43 +++++++++---------- .../freely_generated_lie_conformal_algebra.py | 4 +- .../lie_conformal_algebra.py | 2 +- ..._conformal_algebra_with_structure_coefs.py | 32 ++++++++------ 9 files changed, 69 insertions(+), 63 deletions(-) diff --git a/src/sage/algebras/lie_conformal_algebras/abelian_lie_conformal_algebra.py b/src/sage/algebras/lie_conformal_algebras/abelian_lie_conformal_algebra.py index 6027dee4a95..3cfa3343d80 100644 --- a/src/sage/algebras/lie_conformal_algebras/abelian_lie_conformal_algebra.py +++ b/src/sage/algebras/lie_conformal_algebras/abelian_lie_conformal_algebra.py @@ -89,16 +89,17 @@ def __init__(self, R, ngens=1, weights=None, names = 'a' self._latex_names = tuple(r'a_{%d}' % i for i in range(ngens)) - names,index_set = standardize_names_index_set(names=names, - index_set=index_set, - ngens=ngens) + names, index_set = standardize_names_index_set(names=names, + index_set=index_set, + ngens=ngens) abeliandict = {} GradedLieConformalAlgebra.__init__(self, R, abeliandict, names=names, - index_set=index_set, weights=weights, + index_set=index_set, + weights=weights, parity=parity) - def _repr_(self): + def _repr_(self) -> str: """ String representation. diff --git a/src/sage/algebras/lie_conformal_algebras/affine_lie_conformal_algebra.py b/src/sage/algebras/lie_conformal_algebras/affine_lie_conformal_algebra.py index e9f697e8257..66d4fe6da5c 100644 --- a/src/sage/algebras/lie_conformal_algebras/affine_lie_conformal_algebra.py +++ b/src/sage/algebras/lie_conformal_algebras/affine_lie_conformal_algebra.py @@ -142,7 +142,7 @@ def cartan_type(self): """ return self._ct - def _repr_(self): + def _repr_(self) -> str: """ The name of this Lie conformal algebra. @@ -152,4 +152,4 @@ def _repr_(self): The affine Lie conformal algebra of type ['A', 1] over Rational Field """ return "The affine Lie conformal algebra of type {} over {}".format( - self._ct,self.base_ring()) + self._ct, self.base_ring()) diff --git a/src/sage/algebras/lie_conformal_algebras/bosonic_ghosts_lie_conformal_algebra.py b/src/sage/algebras/lie_conformal_algebras/bosonic_ghosts_lie_conformal_algebra.py index 2e689ee4c60..a3db3b8334e 100644 --- a/src/sage/algebras/lie_conformal_algebras/bosonic_ghosts_lie_conformal_algebra.py +++ b/src/sage/algebras/lie_conformal_algebras/bosonic_ghosts_lie_conformal_algebra.py @@ -120,7 +120,7 @@ def __init__(self, R, ngens=2, names=None, index_set=None): weights=weights, central_elements=('K',)) - def _repr_(self): + def _repr_(self) -> str: """ String representation. @@ -130,4 +130,4 @@ def _repr_(self): The Bosonic ghosts Lie conformal algebra with generators (beta, gamma, K) over Algebraic Field """ return "The Bosonic ghosts Lie conformal algebra with generators {} "\ - "over {}".format(self.gens(),self.base_ring()) + "over {}".format(self.gens(), self.base_ring()) diff --git a/src/sage/algebras/lie_conformal_algebras/fermionic_ghosts_lie_conformal_algebra.py b/src/sage/algebras/lie_conformal_algebras/fermionic_ghosts_lie_conformal_algebra.py index 6c4418e4751..787310885c3 100644 --- a/src/sage/algebras/lie_conformal_algebras/fermionic_ghosts_lie_conformal_algebra.py +++ b/src/sage/algebras/lie_conformal_algebras/fermionic_ghosts_lie_conformal_algebra.py @@ -117,7 +117,7 @@ def __init__(self, R, ngens=2, names=None, index_set=None): parity=parity, central_elements=('K',)) - def _repr_(self): + def _repr_(self) -> str: """ String representation. @@ -127,4 +127,4 @@ def _repr_(self): The Fermionic ghosts Lie conformal algebra with generators (b, c, K) over Rational Field """ return "The Fermionic ghosts Lie conformal algebra with generators {} "\ - "over {}".format(self.gens(),self.base_ring()) + "over {}".format(self.gens(), self.base_ring()) diff --git a/src/sage/algebras/lie_conformal_algebras/free_bosons_lie_conformal_algebra.py b/src/sage/algebras/lie_conformal_algebras/free_bosons_lie_conformal_algebra.py index e66489d49ca..215cabd83e8 100644 --- a/src/sage/algebras/lie_conformal_algebras/free_bosons_lie_conformal_algebra.py +++ b/src/sage/algebras/lie_conformal_algebras/free_bosons_lie_conformal_algebra.py @@ -114,37 +114,39 @@ def __init__(self, R, ngens=None, gram_matrix=None, names=None, if ngens is None: ngens = gram_matrix.dimensions()[0] try: - assert (gram_matrix in MatrixSpace(R,ngens,ngens)) + assert (gram_matrix in MatrixSpace(R, ngens, ngens)) except AssertionError: raise ValueError("the gram_matrix should be a symmetric " + - "{0} x {0} matrix, got {1}".format(ngens,gram_matrix)) + "{0} x {0} matrix, got {1}".format(ngens, gram_matrix)) if not gram_matrix.is_symmetric(): raise ValueError("the gram_matrix should be a symmetric " + - "{0} x {0} matrix, got {1}".format(ngens,gram_matrix)) + "{0} x {0} matrix, got {1}".format(ngens, gram_matrix)) else: if ngens is None: ngens = 1 gram_matrix = identity_matrix(R, ngens, ngens) latex_names = None - if (names is None) and (index_set is None): + if names is None and index_set is None: names = 'alpha' latex_names = tuple(r'\alpha_{%d}' % i for i in range(ngens)) + ('K',) - names,index_set = standardize_names_index_set(names=names, - index_set=index_set, - ngens=ngens) - bosondict = {(i,j): {1: {('K',0): gram_matrix[index_set.rank(i), - index_set.rank(j)]}} for i in index_set for j in index_set} - - GradedLieConformalAlgebra.__init__(self,R,bosondict,names=names, + names, index_set = standardize_names_index_set(names=names, + index_set=index_set, + ngens=ngens) + bosondict = {(i, j): {1: {('K', 0): gram_matrix[index_set.rank(i), + index_set.rank(j)]}} + for i in index_set for j in index_set} + + GradedLieConformalAlgebra.__init__(self, R, bosondict, + names=names, latex_names=latex_names, index_set=index_set, central_elements=('K',)) self._gram_matrix = gram_matrix - def _repr_(self): + def _repr_(self) -> str: """ String representation. @@ -154,7 +156,7 @@ def _repr_(self): The free Bosons Lie conformal algebra with generators (alpha, K) over Algebraic Real Field """ return "The free Bosons Lie conformal algebra with generators {}"\ - " over {}".format(self.gens(),self.base_ring()) + " over {}".format(self.gens(), self.base_ring()) def gram_matrix(self): r""" diff --git a/src/sage/algebras/lie_conformal_algebras/free_fermions_lie_conformal_algebra.py b/src/sage/algebras/lie_conformal_algebras/free_fermions_lie_conformal_algebra.py index 40810602ac4..32d8f65e8d9 100644 --- a/src/sage/algebras/lie_conformal_algebras/free_fermions_lie_conformal_algebra.py +++ b/src/sage/algebras/lie_conformal_algebras/free_fermions_lie_conformal_algebra.py @@ -94,17 +94,17 @@ def __init__(self, R, ngens=None, gram_matrix=None, names=None, """ from sage.matrix.matrix_space import MatrixSpace from sage.matrix.special import identity_matrix - if (gram_matrix is not None): + if gram_matrix is not None: if ngens is None: ngens = gram_matrix.dimensions()[0] try: - assert (gram_matrix in MatrixSpace(R,ngens,ngens)) + assert (gram_matrix in MatrixSpace(R, ngens, ngens)) except AssertionError: raise ValueError("The gram_matrix should be a symmetric " + - "{0} x {0} matrix, got {1}".format(ngens,gram_matrix)) + "{0} x {0} matrix, got {1}".format(ngens, gram_matrix)) if not gram_matrix.is_symmetric(): raise ValueError("The gram_matrix should be a symmetric " + - "{0} x {0} matrix, got {1}".format(ngens,gram_matrix)) + "{0} x {0} matrix, got {1}".format(ngens, gram_matrix)) else: if ngens is None: ngens = 1 @@ -112,34 +112,33 @@ def __init__(self, R, ngens=None, gram_matrix=None, names=None, latex_names = None - if (names is None) and (index_set is None): - if ngens == 1: - names = 'psi' - else: - names = 'psi_' + if names is None and index_set is None: + names = 'psi' if ngens == 1 else 'psi_' latex_names = tuple(r"\psi_{%d}" % i for i in range(ngens)) + ('K',) from sage.structure.indexed_generators import \ - standardize_names_index_set - names,index_set = standardize_names_index_set(names=names, - index_set=index_set, - ngens=ngens) - fermiondict = {(i,j): {0: {('K', 0): gram_matrix[index_set.rank(i), - index_set.rank(j)]}} for i in index_set for j in index_set} + standardize_names_index_set + names, index_set = standardize_names_index_set(names=names, + index_set=index_set, + ngens=ngens) + fermiondict = {(i, j): {0: {('K', 0): gram_matrix[index_set.rank(i), + index_set.rank(j)]}} + for i in index_set for j in index_set} from sage.rings.rational_field import QQ - weights = (QQ(1/2),)*ngens - parity = (1,)*ngens - GradedLieConformalAlgebra.__init__(self,R,fermiondict,names=names, + weights = (QQ((1, 2)),) * ngens + parity = (1,) * ngens + GradedLieConformalAlgebra.__init__(self, R, fermiondict, names=names, latex_names=latex_names, - index_set=index_set,weights=weights, + index_set=index_set, + weights=weights, parity=parity, central_elements=('K',)) self._gram_matrix = gram_matrix - def _repr_(self): + def _repr_(self) -> str: """ String representation. @@ -149,8 +148,8 @@ def _repr_(self): The free Fermions super Lie conformal algebra with generators (psi, K) over Rational Field """ return "The free Fermions super Lie conformal algebra "\ - "with generators {} over {}".format(self.gens(), - self.base_ring()) + "with generators {} over {}".format(self.gens(), + self.base_ring()) def gram_matrix(self): r""" diff --git a/src/sage/algebras/lie_conformal_algebras/freely_generated_lie_conformal_algebra.py b/src/sage/algebras/lie_conformal_algebras/freely_generated_lie_conformal_algebra.py index 0b77dd91854..803c2997271 100644 --- a/src/sage/algebras/lie_conformal_algebras/freely_generated_lie_conformal_algebra.py +++ b/src/sage/algebras/lie_conformal_algebras/freely_generated_lie_conformal_algebra.py @@ -81,8 +81,8 @@ def lie_conformal_algebra_generators(self): (B[alpha[1]], B[alphacheck[1]], B[-alpha[1]], B['K']) """ F = Family(self._generators, - lambda i: self.monomial((i,Integer(0))), - name="generator map") + lambda i: self.monomial((i, Integer(0))), + name="generator map") from sage.categories.sets_cat import Sets if F in Sets().Finite(): return tuple(F) diff --git a/src/sage/algebras/lie_conformal_algebras/lie_conformal_algebra.py b/src/sage/algebras/lie_conformal_algebras/lie_conformal_algebra.py index 528a587d795..9248927e242 100644 --- a/src/sage/algebras/lie_conformal_algebras/lie_conformal_algebra.py +++ b/src/sage/algebras/lie_conformal_algebras/lie_conformal_algebra.py @@ -331,7 +331,7 @@ def __classcall_private__(cls, R=None, arg0=None, index_set=None, if key not in known_keywords: raise ValueError("got an unexpected keyword argument '%s'" % key) - if isinstance(arg0,dict) and arg0: + if isinstance(arg0, dict) and arg0: graded = kwds.pop("graded", False) if weights is not None or graded: from .graded_lie_conformal_algebra import \ diff --git a/src/sage/algebras/lie_conformal_algebras/lie_conformal_algebra_with_structure_coefs.py b/src/sage/algebras/lie_conformal_algebras/lie_conformal_algebra_with_structure_coefs.py index 3cb8f645cd5..97fc71e09a6 100644 --- a/src/sage/algebras/lie_conformal_algebras/lie_conformal_algebra_with_structure_coefs.py +++ b/src/sage/algebras/lie_conformal_algebras/lie_conformal_algebra_with_structure_coefs.py @@ -148,7 +148,7 @@ def _standardize_s_coeff(s_coeff, index_set, ce, parity=None): index_to_parity = dict(zip(index_set, parity)) sc = {} # mypair has a pair of generators - for mypair in s_coeff.keys(): + for mypair, v in s_coeff.items(): # e.g. v = { 0: { (L,2):3, (G,3):1}, 1:{(L,1),2} } v = s_coeff[mypair] key = tuple(mypair) @@ -186,7 +186,7 @@ def _standardize_s_coeff(s_coeff, index_set, ce, parity=None): kth_product[(i[0], i[1] + j)] = \ kth_product.get((i[0], i[1] + j), 0) kth_product[(i[0], i[1] + j)] += parsgn *\ - v[k+j][i]*(-1)**(k+j+1)*binomial(i[1]+j,j) + v[k+j][i]*(-1)**(k+j+1)*binomial(i[1]+j, j) kth_product = {k: v for k, v in kth_product.items() if v} if kth_product: vals[k] = kth_product @@ -212,7 +212,7 @@ def __init__(self, R, s_coeff, index_set=None, central_elements=None, sage: V = lie_conformal_algebras.NeveuSchwarz(QQ) sage: TestSuite(V).run() """ - names, index_set = standardize_names_index_set(names,index_set) + names, index_set = standardize_names_index_set(names, index_set) if central_elements is None: central_elements = () @@ -220,13 +220,14 @@ def __init__(self, R, s_coeff, index_set=None, central_elements=None, names2 = names + tuple(central_elements) index_set2 = DisjointUnionEnumeratedSets((index_set, Family(tuple(central_elements)))) - d = {x:index_set2[i] for i,x in enumerate(names2)} + d = {x: index_set2[i] for i, x in enumerate(names2)} try: - #If we are given a dictionary with names as keys, - #convert to index_set as keys - s_coeff = {(d[k[0]],d[k[1]]):{a:{(d[x[1]],x[2]): - s_coeff[k][a][x] for x in - s_coeff[k][a]} for a in s_coeff[k]} for k in s_coeff.keys()} + # If we are given a dictionary with names as keys, + # convert to index_set as keys + s_coeff = {(d[k[0]], d[k[1]]): + {a: {(d[x[1]], x[2]): sck[a][x] for x in sck[a]} + for a in s_coeff[k]} + for k, sck in s_coeff.items()} except KeyError: # We assume the dictionary was given with keys in the @@ -274,9 +275,12 @@ def __init__(self, R, s_coeff, index_set=None, central_elements=None, prefix=prefix, names=names, latex_names=latex_names, **kwds) s_coeff = dict(s_coeff) - self._s_coeff = Family({k: tuple((j, sum(c*self.monomial(i) - for i,c in v)) for j,v in s_coeff[k]) for k in s_coeff}) - self._parity = dict(zip(self.gens(),parity+(0,)*len(central_elements))) + self._s_coeff = Family({k: + tuple((j, sum(c * self.monomial(i) + for i, c in v)) for j, v in sck) + for k, sck in s_coeff.items()}) + self._parity = dict(zip(self.gens(), + parity + (0,) * len(central_elements))) def structure_coefficients(self): """ @@ -293,7 +297,7 @@ def structure_coefficients(self): """ return self._s_coeff - def _repr_generator(self, x): + def _repr_generator(self, x) -> str: """ String representation of the generator ``x``. @@ -316,4 +320,4 @@ def _repr_generator(self, x): """ if x in self: return repr(x) - return IndexedGenerators._repr_generator(self,x) + return IndexedGenerators._repr_generator(self, x) From 14d64f9069b22f10600b06c548b47e358a094ec9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Sat, 11 Jan 2025 17:57:45 +0100 Subject: [PATCH 2/2] fix suggested details --- .../lie_conformal_algebra_with_structure_coefs.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/sage/algebras/lie_conformal_algebras/lie_conformal_algebra_with_structure_coefs.py b/src/sage/algebras/lie_conformal_algebras/lie_conformal_algebra_with_structure_coefs.py index 97fc71e09a6..2e7f31cee27 100644 --- a/src/sage/algebras/lie_conformal_algebras/lie_conformal_algebra_with_structure_coefs.py +++ b/src/sage/algebras/lie_conformal_algebras/lie_conformal_algebra_with_structure_coefs.py @@ -87,9 +87,11 @@ class LieConformalAlgebraWithStructureCoefficients( `\lambda`-brackets of the generators:: sage: betagamma_dict = {('b','a'):{0:{('K',0):1}}} - sage: V = LieConformalAlgebra(QQ, betagamma_dict, names=('a','b'), weights=(1,0), central_elements=('K',)) + sage: V = LieConformalAlgebra(QQ, betagamma_dict, names=('a','b'), + ....: weights=(1,0), central_elements=('K',)) sage: V.category() - Category of H-graded finitely generated Lie conformal algebras with basis over Rational Field + Category of H-graded finitely generated Lie conformal algebras + with basis over Rational Field sage: V.inject_variables() Defining a, b, K sage: a.bracket(b) @@ -150,7 +152,6 @@ def _standardize_s_coeff(s_coeff, index_set, ce, parity=None): # mypair has a pair of generators for mypair, v in s_coeff.items(): # e.g. v = { 0: { (L,2):3, (G,3):1}, 1:{(L,1),2} } - v = s_coeff[mypair] key = tuple(mypair) vals = {} for l in v: @@ -293,7 +294,10 @@ def structure_coefficients(self): Finite family {('L', 'L'): ((0, TL), (1, 2*L), (3, 1/2*C))} sage: lie_conformal_algebras.NeveuSchwarz(QQ).structure_coefficients() - Finite family {('G', 'G'): ((0, 2*L), (2, 2/3*C)), ('G', 'L'): ((0, 1/2*TG), (1, 3/2*G)), ('L', 'G'): ((0, TG), (1, 3/2*G)), ('L', 'L'): ((0, TL), (1, 2*L), (3, 1/2*C))} + Finite family {('G', 'G'): ((0, 2*L), (2, 2/3*C)), + ('G', 'L'): ((0, 1/2*TG), (1, 3/2*G)), + ('L', 'G'): ((0, TG), (1, 3/2*G)), + ('L', 'L'): ((0, TL), (1, 2*L), (3, 1/2*C))} """ return self._s_coeff