Skip to content

Commit 9ba87b6

Browse files
author
Release Manager
committed
sagemathgh-39388: using the binomial method in categories + cleanup just using the faster binomial method there plus cleanup in one of the two modified files ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. URL: sagemath#39388 Reported by: Frédéric Chapoton Reviewer(s): David Coudert
2 parents 0889ab4 + b01e2a2 commit 9ba87b6

File tree

2 files changed

+46
-47
lines changed

2 files changed

+46
-47
lines changed

src/sage/categories/examples/graded_connected_hopf_algebras_with_basis.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from sage.categories.graded_hopf_algebras_with_basis import GradedHopfAlgebrasWithBasis
1414
from sage.combinat.free_module import CombinatorialFreeModule
15-
from sage.arith.misc import binomial
15+
from sage.rings.integer import Integer
1616
from sage.misc.cachefunc import cached_method
1717
from sage.sets.non_negative_integers import NonNegativeIntegers
1818

@@ -144,8 +144,8 @@ def coproduct_on_basis(self, i):
144144
P0 # P3 + 3*P1 # P2 + 3*P2 # P1 + P3 # P0
145145
"""
146146
return self.sum_of_terms(
147-
((i-j, j), binomial(i, j))
148-
for j in range(i+1)
147+
((i - j, j), Integer(i).binomial(j))
148+
for j in range(i + 1)
149149
)
150150

151151

src/sage/categories/finite_dimensional_lie_algebras_with_basis.py

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
# https://www.gnu.org/licenses/
1818
# ****************************************************************************
1919

20-
20+
from sage.categories.category_with_axiom import CategoryWithAxiom_over_base_ring
21+
from sage.categories.lie_algebras import LieAlgebras
22+
from sage.categories.subobjects import SubobjectsCategory
2123
from sage.misc.abstract_method import abstract_method
2224
from sage.misc.cachefunc import cached_method
2325
from sage.misc.lazy_attribute import lazy_attribute
2426
from sage.misc.lazy_import import LazyImport
25-
from sage.categories.category_with_axiom import CategoryWithAxiom_over_base_ring
26-
from sage.categories.lie_algebras import LieAlgebras
27-
from sage.categories.subobjects import SubobjectsCategory
27+
from sage.rings.integer import Integer
2828
from sage.sets.family import Family
2929

3030

@@ -147,7 +147,7 @@ def names_map(x):
147147
F = FreeAlgebra(self.base_ring(), names)
148148
except ValueError:
149149
names = ['b{}'.format(i) for i in range(self.dimension())]
150-
self._UEA_names_map = {g: names[i] for i,g in enumerate(I)}
150+
self._UEA_names_map = {g: names[i] for i, g in enumerate(I)}
151151
names_map = self._UEA_names_map.__getitem__
152152
F = FreeAlgebra(self.base_ring(), names)
153153
# ``F`` is the free algebra over the basis of ``self``. The
@@ -207,7 +207,7 @@ def _basis_key_inverse(self):
207207
sage: [L._basis_key_inverse[k] for k in L._basis_ordering]
208208
[0, 1, 2, 3, 4, 5]
209209
"""
210-
return {k: i for i,k in enumerate(self._basis_ordering)}
210+
return {k: i for i, k in enumerate(self._basis_ordering)}
211211

212212
def _basis_key(self, x):
213213
"""
@@ -288,7 +288,7 @@ def from_vector(self, v, order=None):
288288
if order is None:
289289
order = self._basis_ordering
290290
B = self.basis()
291-
return self.sum(v[i] * B[k] for i,k in enumerate(order) if v[i] != 0)
291+
return self.sum(v[i] * B[k] for i, k in enumerate(order) if v[i] != 0)
292292

293293
def killing_matrix(self, x, y):
294294
r"""
@@ -424,9 +424,9 @@ def structure_coefficients(self, include_zeros=False):
424424
if not include_zeros and val == zero:
425425
continue
426426
if self._basis_key(x) > self._basis_key(y):
427-
d[y,x] = -val
427+
d[y, x] = -val
428428
else:
429-
d[x,y] = val
429+
d[x, y] = val
430430
return Family(d)
431431

432432
def centralizer_basis(self, S):
@@ -478,8 +478,8 @@ def centralizer_basis(self, S):
478478
"""
479479
from sage.matrix.constructor import matrix
480480

481-
#from sage.algebras.lie_algebras.subalgebra import LieSubalgebra
482-
#if isinstance(S, LieSubalgebra) or S is self:
481+
# from sage.algebras.lie_algebras.subalgebra import LieSubalgebra
482+
# if isinstance(S, LieSubalgebra) or S is self:
483483
if S is self:
484484
from sage.matrix.special import identity_matrix
485485
m = identity_matrix(self.base_ring(), self.dimension())
@@ -493,11 +493,11 @@ def centralizer_basis(self, S):
493493
for k in S.keys():
494494
v = S[k].to_vector()
495495
sc[k] = v
496-
sc[k[1],k[0]] = -v
496+
sc[k[1], k[0]] = -v
497497
X = self.basis().keys()
498498
d = len(X)
499499
c_mat = matrix(self.base_ring(),
500-
[[sum(m[i,j] * sc[x,xp][k] for j,xp in enumerate(X)
500+
[[sum(m[i, j] * sc[x, xp][k] for j, xp in enumerate(X)
501501
if (x, xp) in sc)
502502
for x in X]
503503
for i in range(m.nrows()) for k in range(d)])
@@ -702,24 +702,24 @@ def derivations_basis(self):
702702
R = self.base_ring()
703703
B = self.basis()
704704
keys = list(B.keys())
705-
scoeffs = {(j,y,i): c for y in keys for i in keys
706-
for j,c in self.bracket(B[y], B[i])
707-
}
705+
scoeffs = {(j, y, i): c for y in keys for i in keys
706+
for j, c in self.bracket(B[y], B[i])
707+
}
708708
zero = R.zero()
709709
data = {}
710710
N = len(keys)
711-
for ii,i in enumerate(keys):
712-
for ij,j in enumerate(keys[ii+1:]):
711+
for ii, i in enumerate(keys):
712+
for ij, j in enumerate(keys[ii+1:]):
713713
ijp = ij + ii + 1
714-
for il,l in enumerate(keys):
714+
for il, l in enumerate(keys):
715715
row = ii + N * il + N**2 * ij
716-
for ik,k in enumerate(keys):
717-
data[row,ik+N*il] = (data.get((row,ik+N*il), zero)
718-
+ scoeffs.get((k, i, j), zero))
719-
data[row,ii+N*ik] = (data.get((row,ii+N*ik), zero)
720-
- scoeffs.get((l, k, j), zero))
721-
data[row,ijp+N*ik] = (data.get((row,ijp+N*ik), zero)
722-
- scoeffs.get((l, i, k), zero))
716+
for ik, k in enumerate(keys):
717+
data[row, ik+N*il] = (data.get((row, ik+N*il), zero)
718+
+ scoeffs.get((k, i, j), zero))
719+
data[row, ii+N*ik] = (data.get((row, ii+N*ik), zero)
720+
- scoeffs.get((l, k, j), zero))
721+
data[row, ijp+N*ik] = (data.get((row, ijp+N*ik), zero)
722+
- scoeffs.get((l, i, k), zero))
723723
mat = matrix(R, data, sparse=True)
724724
return tuple([matrix(R, N, N, list(b)) for b in mat.right_kernel().basis()])
725725

@@ -1524,7 +1524,7 @@ def is_abelian(self):
15241524
"""
15251525
return len(self.structure_coefficients()) == 0
15261526
# TODO: boolean handling of empty family
1527-
#return not self.structure_coefficients()
1527+
# return not self.structure_coefficients()
15281528

15291529
def is_solvable(self):
15301530
r"""
@@ -1699,9 +1699,7 @@ def chevalley_eilenberg_complex(self, M=None, dual=False, sparse=True, ncpus=Non
16991699
sparse=sparse,
17001700
ncpus=ncpus).dual()
17011701

1702-
import itertools
17031702
from itertools import combinations, product
1704-
from sage.arith.misc import binomial
17051703
from sage.matrix.matrix_space import MatrixSpace
17061704
from sage.algebras.lie_algebras.representation import Representation_abstract
17071705
R = self.base_ring()
@@ -1776,8 +1774,8 @@ def compute_diff(k):
17761774
Y.pop(i)
17771775
# This is where we would do the action on
17781776
# the coefficients module
1779-
#ret[indices[tuple(Y)]] += mone**i * zero
1780-
for j in range(i+1,k):
1777+
# ret[indices[tuple(Y)]] += mone**i * zero
1778+
for j in range(i + 1, k):
17811779
# We shift j by 1 because we already removed
17821780
# an earlier element from X.
17831781
Z = tuple(Y[:j-1] + Y[j:])
@@ -1807,8 +1805,9 @@ def compute_diff(k):
18071805
else:
18081806
p2_data.append(ret)
18091807

1810-
nrows = binomial(len(LI), k)
1811-
ncols = binomial(len(LI), k-1)
1808+
lenLI = Integer(len(LI))
1809+
nrows = lenLI.binomial(k)
1810+
ncols = lenLI.binomial(k - 1)
18121811
MS = MatrixSpace(R, nrows, ncols, sparse=sparse)
18131812
if M is None:
18141813
p2 = MS(p2_data).transpose()
@@ -1852,7 +1851,7 @@ def compute_diff(k):
18521851
else:
18531852
p1_data.append(ret)
18541853

1855-
nrows = len(MI) * binomial(len(LI), k)
1854+
nrows = len(MI) * Integer(len(LI)).binomial(k)
18561855
ncols = len(ten_ind)
18571856
MS = MatrixSpace(R, nrows, ncols, sparse=sparse)
18581857
ret = MS(p1_data).transpose() + p2
@@ -1862,8 +1861,7 @@ def compute_diff(k):
18621861
from sage.homology.chain_complex import ChainComplex
18631862
ind = list(range(1, len(LI) + 1))
18641863
chain_data = {X[0][0]: M for X, M in compute_diff(ind)}
1865-
C = ChainComplex(chain_data, degree_of_differential=-1)
1866-
return C
1864+
return ChainComplex(chain_data, degree_of_differential=-1)
18671865

18681866
def homology(self, deg=None, M=None, sparse=True, ncpus=None):
18691867
r"""
@@ -2019,11 +2017,11 @@ def as_finite_dimensional_algebra(self):
20192017
M = []
20202018
for kp in K:
20212019
if (k, kp) in S:
2022-
M.append( -S[k,kp].to_vector() )
2020+
M.append(-S[k, kp].to_vector())
20232021
elif (kp, k) in S:
2024-
M.append( S[kp,k].to_vector() )
2022+
M.append(S[kp, k].to_vector())
20252023
else:
2026-
M.append( zero_vec )
2024+
M.append(zero_vec)
20272025
mats.append(matrix(R, M))
20282026
from sage.algebras.finite_dimensional_algebras.finite_dimensional_algebra import FiniteDimensionalAlgebra
20292027
return FiniteDimensionalAlgebra(R, mats, names=self._names)
@@ -2172,7 +2170,7 @@ def sc(i, j):
21722170
vs = 'X{}_{}'
21732171
else:
21742172
vs = 'X{}{}'
2175-
R = PolynomialRing(self.base_ring(), ','.join(vs.format(i,j)
2173+
R = PolynomialRing(self.base_ring(), ','.join(vs.format(i, j)
21762174
for i in range(n)
21772175
for j in range(n)))
21782176
X = [[R.gen(i+n*j) for i in range(n)] for j in range(n)]
@@ -2184,10 +2182,10 @@ def sc(i, j):
21842182
if i != j:
21852183
s = sc(i, j)
21862184
d[k] = (R.sum(s[I[u]] * X[a][u] for u in range(n))
2187-
- R.sum(sc(s,t)[I[a]] * X[s][i] * X[t][j]
2185+
- R.sum(sc(s, t)[I[a]] * X[s][i] * X[t][j]
21882186
for s in range(n) for t in range(n) if s != t))
21892187
else:
2190-
d[k] = -R.sum(sc(s,t)[I[a]] * X[s][i] * X[t][j]
2188+
d[k] = -R.sum(sc(s, t)[I[a]] * X[s][i] * X[t][j]
21912189
for s in range(n) for t in range(n) if s != t)
21922190
return Family(keys, d.__getitem__)
21932191

@@ -2494,7 +2492,8 @@ def faithful_representation(self, algorithm=None):
24942492
raise ValueError("invalid algorithm '{}'".format(algorithm))
24952493

24962494
class ElementMethods:
2497-
def adjoint_matrix(self, sparse=False): # In #11111 (more or less) by using matrix of a morphism
2495+
def adjoint_matrix(self, sparse=False):
2496+
# In #11111 (more or less) by using matrix of a morphism
24982497
"""
24992498
Return the matrix of the adjoint action of ``self``.
25002499
@@ -2598,7 +2597,7 @@ def to_vector(self, sparse=False, order=None):
25982597
from sage.modules.free_module import FreeModule
25992598
M = FreeModule(self.parent().base_ring(), self.dimension(), sparse=True)
26002599
if order is None:
2601-
order = {b: i for i,b in enumerate(self.parent()._basis_ordering)}
2600+
order = {b: i for i, b in enumerate(self.parent()._basis_ordering)}
26022601
return M({order[k]: c for k, c in mc.items()})
26032602
else:
26042603
M = self.parent().module()

0 commit comments

Comments
 (0)