Skip to content

Commit 870aba2

Browse files
author
Release Manager
committed
sagemathgh-39397: details in braid groups some typing annotations, a few code details including avoid the terrible use of `gens_dict_recursive` ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. URL: sagemath#39397 Reported by: Frédéric Chapoton Reviewer(s): Travis Scrimshaw
2 parents e6975be + 56c0322 commit 870aba2

File tree

1 file changed

+58
-45
lines changed

1 file changed

+58
-45
lines changed

src/sage/groups/braid.py

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
Braid groups are implemented as a particular case of finitely presented groups,
55
but with a lot of specific methods for braids.
66
7-
A braid group can be created by giving the number of strands, and the name of the generators::
7+
A braid group can be created by giving the number of strands, and the name
8+
of the generators::
89
910
sage: BraidGroup(3)
1011
Braid group on 3 strands
@@ -15,8 +16,8 @@
1516
sage: BraidGroup(3,'a,b').gens()
1617
(a, b)
1718
18-
The elements can be created by operating with the generators, or by passing a list
19-
with the indices of the letters to the group::
19+
The elements can be created by operating with the generators, or by passing
20+
a list with the indices of the letters to the group::
2021
2122
sage: B.<s0,s1,s2> = BraidGroup(4)
2223
sage: s0*s1*s0
@@ -81,7 +82,8 @@
8182
GroupMorphismWithGensImages,
8283
)
8384
from sage.groups.free_group import FreeGroup, is_FreeGroup
84-
from sage.groups.perm_gps.permgroup_named import SymmetricGroup, SymmetricGroupElement
85+
from sage.groups.perm_gps.permgroup_named import (SymmetricGroup,
86+
SymmetricGroupElement)
8587
from sage.libs.gap.libgap import libgap
8688
from sage.matrix.constructor import identity_matrix, matrix
8789
from sage.misc.cachefunc import cached_method
@@ -96,10 +98,11 @@
9698
from sage.structure.richcmp import rich_to_bool, richcmp
9799

98100
lazy_import('sage.libs.braiding',
99-
['leftnormalform', 'rightnormalform', 'centralizer', 'supersummitset', 'greatestcommondivisor',
101+
['leftnormalform', 'rightnormalform', 'centralizer',
102+
'supersummitset', 'greatestcommondivisor',
100103
'leastcommonmultiple', 'conjugatingbraid', 'ultrasummitset',
101104
'thurston_type', 'rigidity', 'sliding_circuits', 'send_to_sss',
102-
'send_to_uss', 'send_to_sc', 'trajectory', 'cyclic_slidings' ],
105+
'send_to_uss', 'send_to_sc', 'trajectory', 'cyclic_slidings'],
103106
feature=sage__libs__braiding())
104107
lazy_import('sage.knots.knot', 'Knot')
105108

@@ -500,7 +503,8 @@ def permutation(self, W=None):
500503
"""
501504
return self.coxeter_group_element(W)
502505

503-
def plot(self, color='rainbow', orientation='bottom-top', gap=0.05, aspect_ratio=1, axes=False, **kwds):
506+
def plot(self, color='rainbow', orientation='bottom-top', gap=0.05,
507+
aspect_ratio=1, axes=False, **kwds):
504508
"""
505509
Plot the braid.
506510
@@ -632,7 +636,7 @@ def plot(self, color='rainbow', orientation='bottom-top', gap=0.05, aspect_ratio
632636

633637
def plot3d(self, color='rainbow'):
634638
"""
635-
Plots the braid in 3d.
639+
Plot the braid in 3d.
636640
637641
The following option is available:
638642
@@ -830,6 +834,7 @@ def links_gould_matrix(self, symbolics=False):
830834
r"""
831835
Return the representation matrix of ``self`` according to the R-matrix
832836
representation being attached to the quantum superalgebra `\mathfrak{sl}_q(2|1)`.
837+
833838
See [MW2012]_, section 3 and references given there.
834839
835840
INPUT:
@@ -861,15 +866,16 @@ def links_gould_matrix(self, symbolics=False):
861866
M = rep[0][0].parent().one()
862867
for i in self.Tietze():
863868
if i > 0:
864-
M = M*rep[i-1][0]
869+
M = M * rep[i-1][0]
865870
if i < 0:
866-
M = M*rep[-i-1][1]
871+
M = M * rep[-i-1][1]
867872
return M
868873

869874
@cached_method
870875
def links_gould_polynomial(self, varnames=None, use_symbolics=False):
871876
r"""
872877
Return the Links-Gould polynomial of the closure of ``self``.
878+
873879
See [MW2012]_, section 3 and references given there.
874880
875881
INPUT:
@@ -906,12 +912,13 @@ def links_gould_polynomial(self, varnames=None, use_symbolics=False):
906912
mu = rep[ln - 1] # quantum trace factor
907913
M = mu * self.links_gould_matrix(symbolics=use_symbolics)
908914
d1, d2 = M.dimensions()
909-
e = d1//4
915+
e = d1 // 4
910916
B = M.base_ring()
911917
R = LaurentPolynomialRing(ZZ, varnames)
912918

913919
# partial quantum trace according to I. Marin section 2.5
914-
part_trace = matrix(B, 4, 4, lambda i, j: sum(M[e * i + k, e * j + k] for k in range(e)))
920+
part_trace = matrix(B, 4, 4, lambda i, j: sum(M[e * i + k, e * j + k]
921+
for k in range(e)))
915922
ptemp = part_trace[0, 0] # part_trace == psymb*M.parent().one()
916923
if use_symbolics:
917924
v1, v2 = R.variable_names()
@@ -922,13 +929,13 @@ def links_gould_polynomial(self, varnames=None, use_symbolics=False):
922929
else:
923930
ltemp = ptemp.lift().constant_coefficient()
924931
# Since the result of the calculation is known to be a Laurent polynomial
925-
# in t0 and t1 all exponents of ltemp must be divisable by 2
932+
# in t0 and t1 all exponents of ltemp must be divisible by 2
926933
L = ltemp.parent()
927934
lred = L({(k[0]/2, k[1]/2): v for k, v in ltemp.monomial_coefficients().items()})
928935
t0, t1 = R.gens()
929936
return lred(t0, t1)
930937

931-
def tropical_coordinates(self):
938+
def tropical_coordinates(self) -> list:
932939
r"""
933940
Return the tropical coordinates of ``self`` in the braid group `B_n`.
934941
@@ -972,7 +979,7 @@ def tropical_coordinates(self):
972979

973980
from sage.rings.semirings.tropical_semiring import TropicalSemiring
974981
T = TropicalSemiring(ZZ)
975-
return [T(_) for _ in coord]
982+
return [T(c) for c in coord]
976983

977984
def markov_trace(self, variab=None, normalized=True):
978985
r"""
@@ -1640,7 +1647,7 @@ def right_normal_form(self):
16401647
B = self.parent()
16411648
return tuple([B(b) for b in rnf[:-1]] + [B.delta()**rnf[-1][0]])
16421649

1643-
def centralizer(self):
1650+
def centralizer(self) -> list:
16441651
"""
16451652
Return a list of generators of the centralizer of the braid.
16461653
@@ -1655,7 +1662,7 @@ def centralizer(self):
16551662
B = self.parent()
16561663
return [B._element_from_libbraiding(b) for b in c]
16571664

1658-
def super_summit_set(self):
1665+
def super_summit_set(self) -> list:
16591666
"""
16601667
Return a list with the super summit set of the braid.
16611668
@@ -1773,10 +1780,9 @@ def conjugating_braid(self, other):
17731780
cb = conjugatingbraid(self, other)
17741781
if not cb:
17751782
return None
1776-
else:
1777-
B = self.parent()
1778-
cb[0][0] %= 2
1779-
return B._element_from_libbraiding(cb)
1783+
B = self.parent()
1784+
cb[0][0] %= 2
1785+
return B._element_from_libbraiding(cb)
17801786

17811787
def is_conjugated(self, other) -> bool:
17821788
"""
@@ -1893,7 +1899,7 @@ def pure_conjugating_braid(self, other):
18931899
n2 = len(b2.Tietze())
18941900
return b2 if n2 <= n0 else b0
18951901

1896-
def ultra_summit_set(self):
1902+
def ultra_summit_set(self) -> list:
18971903
"""
18981904
Return a list with the orbits of the ultra summit set of ``self``.
18991905
@@ -1922,7 +1928,7 @@ def ultra_summit_set(self):
19221928
B = self.parent()
19231929
return [[B._element_from_libbraiding(i) for i in s] for s in uss]
19241930

1925-
def thurston_type(self):
1931+
def thurston_type(self) -> str:
19261932
"""
19271933
Return the thurston_type of ``self``.
19281934
@@ -2007,7 +2013,7 @@ def rigidity(self):
20072013
"""
20082014
return Integer(rigidity(self))
20092015

2010-
def sliding_circuits(self):
2016+
def sliding_circuits(self) -> list:
20112017
"""
20122018
Return the sliding circuits of the braid.
20132019
@@ -2304,7 +2310,7 @@ def ultra_summit_set_element(self):
23042310
B = self.parent()
23052311
return tuple([B._element_from_libbraiding(b) for b in to_uss])
23062312

2307-
def sliding_circuits_element(self):
2313+
def sliding_circuits_element(self) -> tuple:
23082314
r"""
23092315
Return an element of the braid's sliding circuits, and the conjugating
23102316
braid.
@@ -2320,7 +2326,7 @@ def sliding_circuits_element(self):
23202326
B = self.parent()
23212327
return tuple([B._element_from_libbraiding(b) for b in to_sc])
23222328

2323-
def trajectory(self):
2329+
def trajectory(self) -> list:
23242330
r"""
23252331
Return the braid's trajectory.
23262332
@@ -2338,7 +2344,7 @@ def trajectory(self):
23382344
B = self.parent()
23392345
return [B._element_from_libbraiding(b) for b in traj]
23402346

2341-
def cyclic_slidings(self):
2347+
def cyclic_slidings(self) -> list:
23422348
r"""
23432349
Return the braid's cyclic slidings.
23442350
@@ -2518,6 +2524,7 @@ def reduced_word(self):
25182524
def tuple_to_word(q_tuple):
25192525
return M.prod(self._gens[i]**exp
25202526
for i, exp in enumerate(q_tuple))
2527+
25212528
ret = {tuple_to_word(q_tuple): q_factor
25222529
for q_tuple, q_factor in self.tuples.items() if q_factor}
25232530
return self._algebra._from_dict(ret, remove_zeros=False)
@@ -2580,7 +2587,7 @@ def eps_monom(q_tuple):
25802587
return sum(q_factor * eps_monom(q_tuple)
25812588
for q_tuple, q_factor in self.tuples.items())
25822589

2583-
def __repr__(self):
2590+
def __repr__(self) -> str:
25842591
r"""
25852592
String representation of ``self``.
25862593
@@ -2616,7 +2623,7 @@ class BraidGroup_class(FiniteTypeArtinGroup):
26162623
"""
26172624
Element = Braid
26182625

2619-
def __init__(self, names):
2626+
def __init__(self, names) -> None:
26202627
"""
26212628
Python constructor.
26222629
@@ -2691,7 +2698,7 @@ def __init__(self, names):
26912698
# For caching hermitian form of unitary Burau representation
26922699
self._hermitian_form = None
26932700

2694-
def __reduce__(self):
2701+
def __reduce__(self) -> tuple:
26952702
"""
26962703
TESTS::
26972704
@@ -2704,7 +2711,7 @@ def __reduce__(self):
27042711
"""
27052712
return (BraidGroup_class, (self.variable_names(), ))
27062713

2707-
def _repr_(self):
2714+
def _repr_(self) -> str:
27082715
"""
27092716
Return a string representation.
27102717
@@ -2795,7 +2802,7 @@ def an_element(self):
27952802
"""
27962803
return self.gen(0)
27972804

2798-
def some_elements(self):
2805+
def some_elements(self) -> list:
27992806
"""
28002807
Return a list of some elements of the braid group.
28012808
@@ -2812,7 +2819,7 @@ def some_elements(self):
28122819
elements_list.append(elements_list[-1]**self.strands())
28132820
return elements_list
28142821

2815-
def _standard_lift_Tietze(self, p):
2822+
def _standard_lift_Tietze(self, p) -> tuple:
28162823
"""
28172824
Helper for :meth:`_standard_lift_Tietze`.
28182825
@@ -2840,8 +2847,7 @@ def _standard_lift_Tietze(self, p):
28402847
(1, 2, 3, 4, 1, 2)
28412848
"""
28422849
G = SymmetricGroup(self.strands())
2843-
pl = G(p)
2844-
return tuple(pl.reduced_word())
2850+
return tuple(G(p).reduced_word())
28452851

28462852
@cached_method
28472853
def _links_gould_representation(self, symbolics=False):
@@ -2887,8 +2893,9 @@ def _links_gould_representation(self, symbolics=False):
28872893
else:
28882894
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
28892895
LR = LaurentPolynomialRing(ZZ, 's0r, s1r')
2896+
s0r, s1r = LR.gens()
28902897
PR = PolynomialRing(LR, 'Yr')
2891-
s0r, s1r, Yr = PR.gens_dict_recursive().values()
2898+
Yr = PR.gen()
28922899
pqr = Yr**2 + (s0r**2 - 1) * (s1r**2 - 1)
28932900
BR = PR.quotient_ring(pqr)
28942901
s0 = BR(s0r)
@@ -3453,9 +3460,10 @@ def mirror_involution(self):
34533460
r"""
34543461
Return the mirror involution of ``self``.
34553462
3456-
This automorphism maps a braid to another one by replacing each generator
3457-
in its word by the inverse. In general this is different from the inverse
3458-
of the braid since the order of the generators in the word is not reversed.
3463+
This automorphism maps a braid to another one by replacing
3464+
each generator in its word by the inverse. In general this is
3465+
different from the inverse of the braid since the order of the
3466+
generators in the word is not reversed.
34593467
34603468
EXAMPLES::
34613469
@@ -3519,7 +3527,7 @@ def presentation_two_generators(self, isomorphisms=False):
35193527
h2 = G.hom(codomain=self, im_gens=[self(a) for a in L2], check=False)
35203528
return (G, h1, h2)
35213529

3522-
def epimorphisms(self, H):
3530+
def epimorphisms(self, H) -> list:
35233531
r"""
35243532
Return the epimorphisms from ``self`` to ``H``, up to automorphism of `H` passing
35253533
through the :meth:`two generator presentation
@@ -3552,12 +3560,15 @@ def epimorphisms(self, H):
35523560
Gg = libgap(G)
35533561
Hg = libgap(H)
35543562
gquotients = Gg.GQuotients(Hg)
3555-
hom1g = libgap.GroupHomomorphismByImagesNC(G0g, Gg, [libgap(hom1(u)) for u in self.gens()])
3563+
hom1g = libgap.GroupHomomorphismByImagesNC(G0g, Gg,
3564+
[libgap(hom1(u))
3565+
for u in self.gens()])
35563566
g0quotients = [hom1g * h for h in gquotients]
35573567
res = []
35583568
# the following closure is needed to attach a specific value of quo to
35593569
# each function in the different morphisms
3560-
fmap = lambda tup: (lambda a: H(prod(tup[abs(i)-1]**sign(i) for i in a.Tietze())))
3570+
fmap = lambda tup: (lambda a: H(prod(tup[abs(i)-1]**sign(i)
3571+
for i in a.Tietze())))
35613572
for quo in g0quotients:
35623573
tup = tuple(H(quo.ImageElm(i.gap()).sage()) for i in self.gens())
35633574
fhom = GroupMorphismWithGensImages(HomSpace, fmap(tup))
@@ -3685,22 +3696,24 @@ class group of the punctured disk.
36853696
36863697
sage: A = B.mapping_class_action(F)
36873698
sage: A
3688-
Right action by Braid group on 4 strands on Free Group on generators {x0, x1, x2, x3}
3699+
Right action by Braid group on 4 strands on Free Group
3700+
on generators {x0, x1, x2, x3}
36893701
sage: A(x0, s1)
36903702
x0
36913703
sage: A(x1, s1)
36923704
x1*x2*x1^-1
36933705
sage: A(x1^-1, s1)
36943706
x1*x2^-1*x1^-1
36953707
"""
3696-
def __init__(self, G, M):
3708+
def __init__(self, G, M) -> None:
36973709
"""
36983710
TESTS::
36993711
37003712
sage: B = BraidGroup(3)
37013713
sage: G = FreeGroup('a, b, c')
37023714
sage: B.mapping_class_action(G) # indirect doctest
3703-
Right action by Braid group on 3 strands on Free Group on generators {a, b, c}
3715+
Right action by Braid group on 3 strands on Free Group
3716+
on generators {a, b, c}
37043717
"""
37053718
import operator
37063719
Action.__init__(self, G, M, False, operator.mul)

0 commit comments

Comments
 (0)