This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
permgroup.py
4467 lines (3530 loc) · 158 KB
/
permgroup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
r"""
Permutation groups
A permutation group is a finite group `G` whose elements are
permutations of a given finite set `X` (i.e., bijections
`X \longrightarrow X`) and whose group operation is the composition of
permutations. The number of elements of `X` is called the degree of `G`.
In Sage, a permutation is represented as either a string that
defines a permutation using disjoint cycle notation, or a list of
tuples, which represent disjoint cycles. That is::
(a,...,b)(c,...,d)...(e,...,f) <--> [(a,...,b), (c,...,d),..., (e,...,f)]
() = identity <--> []
You can make the "named" permutation groups (see
``permgp_named.py``) and use the following
constructions:
- permutation group generated by elements,
- ``direct_product_permgroups``, which takes a list of permutation
groups and returns their direct product.
JOKE: Q: What's hot, chunky, and acts on a polygon? A: Dihedral
soup. Renteln, P. and Dundes, A. "Foolproof: A Sampling of
Mathematical Folk Humor." Notices Amer. Math. Soc. 52, 24-34,
2005.
Index of methods
----------------
Here are the method of a :func:`PermutationGroup`
{METHODS_OF_PermutationGroup_generic}
AUTHORS:
- David Joyner (2005-10-14): first version
- David Joyner (2005-11-17)
- William Stein (2005-11-26): rewrite to better wrap Gap
- David Joyner (2005-12-21)
- William Stein and David Joyner (2006-01-04): added conjugacy_class_representatives
- David Joyner (2006-03): reorganization into subdirectory perm_gps;
added __contains__, has_element; fixed _cmp_; added subgroup
class+methods, PGL,PSL,PSp, PSU classes,
- David Joyner (2006-06): added PGU, functionality to SymmetricGroup,
AlternatingGroup, direct_product_permgroups
- David Joyner (2006-08): added degree,
ramification_module_decomposition_modular_curve and
ramification_module_decomposition_hurwitz_curve methods to PSL(2,q),
MathieuGroup, is_isomorphic
- Bobby Moretti (2006)-10): Added KleinFourGroup, fixed bug in DihedralGroup
- David Joyner (2006-10): added is_subgroup (fixing a bug found by
Kiran Kedlaya), is_solvable, normalizer, is_normal_subgroup, Suzuki
- David Kohel (2007-02): fixed __contains__ to not enumerate group
elements, following the convention for __call__
- David Harvey, Mike Hansen, Nick Alexander, William Stein
(2007-02,03,04,05): Various patches
- Nathan Dunfield (2007-05): added orbits
- David Joyner (2007-06): added subgroup method (suggested by David
Kohel), composition_series, lower_central_series,
upper_central_series, cayley_table, quotient_group, sylow_subgroup,
is_cyclic, homology, homology_part, cohomology, cohomology_part,
poincare_series, molien_series, is_simple, is_monomial,
is_supersolvable, is_nilpotent, is_perfect, is_polycyclic,
is_elementary_abelian, is_pgroup, gens_small,
isomorphism_type_info_simple_group. moved all the"named"
groups to a new file.
- Nick Alexander (2007-07): move is_isomorphic to isomorphism_to, add
from_gap_list
- William Stein (2007-07): put is_isomorphic back (and make it better)
- David Joyner (2007-08): fixed bugs in composition_series,
upper/lower_central_series, derived_series,
- David Joyner (2008-06): modified is_normal (reported by
W. J. Palenstijn), and added normalizes
- David Joyner (2008-08): Added example to docstring of cohomology.
- Simon King (2009-04): __cmp__ methods for PermutationGroup_generic and PermutationGroup_subgroup
- Nicolas Borie (2009): Added orbit, transversals, stabiliser and strong_generating_system methods
- Christopher Swenson (2012): Added a special case to compute the order efficiently.
(This patch Copyright 2012 Google Inc. All Rights Reserved. )
- Javier Lopez Pena (2013): Added conjugacy classes.
REFERENCES:
- Cameron, P., Permutation Groups. New York: Cambridge University
Press, 1999.
- Wielandt, H., Finite Permutation Groups. New York: Academic Press,
1964.
- Dixon, J. and Mortimer, B., Permutation Groups, Springer-Verlag,
Berlin/New York, 1996.
.. note::
Though Suzuki groups are okay, Ree groups should *not* be wrapped
as permutation groups - the construction is too slow - unless (for
small values or the parameter) they are made using explicit
generators.
"""
#*****************************************************************************
# Copyright (C) 2006 William Stein <wstein@gmail.com>
# David Joyner <wdjoyner@gmail.com>
#
# Distributed under the terms of the GNU General Public License (GPL)
# http://www.gnu.org/licenses/
#*****************************************************************************
from __future__ import absolute_import
from six.moves import range
from six import integer_types
from functools import wraps
from sage.misc.randstate import current_randstate
from sage.groups.group import FiniteGroup
from sage.rings.all import QQ, Integer
from sage.interfaces.expect import is_ExpectElement
from sage.interfaces.gap import gap, GapElement
from sage.groups.perm_gps.permgroup_element import PermutationGroupElement, standardize_generator
from sage.groups.abelian_gps.abelian_group import AbelianGroup
from sage.misc.cachefunc import cached_method
from sage.groups.class_function import ClassFunction
from sage.sets.finite_enumerated_set import FiniteEnumeratedSet
from sage.categories.all import FiniteEnumeratedSets
from sage.groups.conjugacy_classes import ConjugacyClassGAP
from sage.structure.richcmp import (richcmp_method, richcmp_not_equal,
richcmp, rich_to_bool, op_EQ)
def load_hap():
r"""
Load the GAP hap package into the default GAP interpreter interface.
EXAMPLES::
sage: sage.groups.perm_gps.permgroup.load_hap() # optional - gap_packages
"""
from sage.features.gap import GapPackage
GapPackage("hap", spkg="gap_packages").require()
gap.load_package("hap")
def hap_decorator(f):
"""
A decorator for permutation group methods that require HAP. It
checks to see that HAP is installed as well as checks that the
argument ``p`` is either 0 or prime.
EXAMPLES::
sage: from sage.groups.perm_gps.permgroup import hap_decorator
sage: def foo(self, n, p=0): print("Done")
sage: foo = hap_decorator(foo)
sage: foo(None, 3) #optional - gap_packages
Done
sage: foo(None, 3, 0) # optional - gap_packages
Done
sage: foo(None, 3, 5) # optional - gap_packages
Done
sage: foo(None, 3, 4) #optional - gap_packages
Traceback (most recent call last):
...
ValueError: p must be 0 or prime
"""
@wraps(f)
def wrapped(self, n, p=0):
load_hap()
from sage.arith.all import is_prime
if not (p == 0 or is_prime(p)):
raise ValueError("p must be 0 or prime")
return f(self, n, p=p)
return wrapped
def direct_product_permgroups(P):
"""
Takes the direct product of the permutation groups listed in ``P``.
EXAMPLES::
sage: G1 = AlternatingGroup([1,2,4,5])
sage: G2 = AlternatingGroup([3,4,6,7])
sage: D = direct_product_permgroups([G1,G2,G1])
sage: D.order()
1728
sage: D = direct_product_permgroups([G1])
sage: D==G1
True
sage: direct_product_permgroups([])
Symmetric group of order 0! as a permutation group
"""
n = len(P)
if n == 0:
from sage.groups.perm_gps.permgroup_named import SymmetricGroup
return SymmetricGroup(0)
elif n == 1:
return P[0]
else:
G = gap.DirectProduct(*P)
return PermutationGroup(gap_group=G)
def from_gap_list(G, src):
r"""
Convert a string giving a list of GAP permutations into a list of
elements of ``G``.
EXAMPLES::
sage: from sage.groups.perm_gps.permgroup import from_gap_list
sage: G = PermutationGroup([[(1,2,3),(4,5)],[(3,4)]])
sage: L = from_gap_list(G, "[(1,2,3)(4,5), (3,4)]"); L
[(1,2,3)(4,5), (3,4)]
sage: L[0].parent() is G
True
sage: L[1].parent() is G
True
"""
# src is a list of strings, each of which is a permutation of
# integers in cycle notation. It may contain \n and spaces.
src = [str(g)[1:].split(")(")
for g in str(src).replace(" ","").replace("\n","")[1:-2].split("),")]
# src is a list of list of strings. Each string is a list of
# integers separated by ','
src = [G([tuple(G._domain_from_gap[int(x)] for x in cycle.split(","))
for cycle in g])
for g in src]
# src is now a list of group elements
return src
def PermutationGroup(gens=None, gap_group=None, domain=None, canonicalize=True, category=None):
"""
Return the permutation group associated to `x` (typically a
list of generators).
INPUT:
- ``gens`` - list of generators (default: ``None``)
- ``gap_group`` - a gap permutation group (default: ``None``)
- ``canonicalize`` - bool (default: ``True``); if ``True``,
sort generators and remove duplicates
OUTPUT:
- A permutation group.
EXAMPLES::
sage: G = PermutationGroup([[(1,2,3),(4,5)],[(3,4)]])
sage: G
Permutation Group with generators [(3,4), (1,2,3)(4,5)]
We can also make permutation groups from PARI groups::
sage: H = pari('x^4 - 2*x^3 - 2*x + 1').polgalois()
sage: G = PariGroup(H, 4); G
PARI group [8, -1, 3, "D(4)"] of degree 4
sage: H = PermutationGroup(G); H # optional - database_gap
Transitive group number 3 of degree 4
sage: H.gens() # optional - database_gap
[(1,2,3,4), (1,3)]
We can also create permutation groups whose generators are Gap
permutation objects::
sage: p = gap('(1,2)(3,7)(4,6)(5,8)'); p
(1,2)(3,7)(4,6)(5,8)
sage: PermutationGroup([p])
Permutation Group with generators [(1,2)(3,7)(4,6)(5,8)]
Permutation groups can work on any domain. In the following
examples, the permutations are specified in list notation,
according to the order of the elements of the domain::
sage: list(PermutationGroup([['b','c','a']], domain=['a','b','c']))
[(), ('a','b','c'), ('a','c','b')]
sage: list(PermutationGroup([['b','c','a']], domain=['b','c','a']))
[()]
sage: list(PermutationGroup([['b','c','a']], domain=['a','c','b']))
[(), ('a','b')]
There is an underlying gap object that implements each
permutation group::
sage: G = PermutationGroup([[(1,2,3,4)]])
sage: G._gap_()
Group( [ (1,2,3,4) ] )
sage: gap(G)
Group( [ (1,2,3,4) ] )
sage: gap(G) is G._gap_()
True
sage: G = PermutationGroup([[(1,2,3),(4,5)],[(3,4)]])
sage: current_randstate().set_seed_gap()
sage: G._gap_().DerivedSeries()
[ Group( [ (3,4), (1,2,3)(4,5) ] ), Group( [ (1,5)(3,4), (1,5)(2,4), (1,5,3) ] ) ]
TESTS::
sage: r = Permutation("(1,7,9,3)(2,4,8,6)")
sage: f = Permutation("(1,3)(4,6)(7,9)")
sage: PermutationGroup([r,f]) #See Trac #12597
Permutation Group with generators [(1,3)(4,6)(7,9), (1,7,9,3)(2,4,8,6)]
sage: PermutationGroup(SymmetricGroup(5))
Traceback (most recent call last):
...
TypeError: gens must be a tuple, list, or GapElement
"""
if not is_ExpectElement(gens) and hasattr(gens, '_permgroup_'):
return gens._permgroup_()
if gens is not None and not isinstance(gens, (tuple, list, GapElement)):
raise TypeError("gens must be a tuple, list, or GapElement")
return PermutationGroup_generic(gens=gens, gap_group=gap_group, domain=domain,
canonicalize=canonicalize, category=category)
@richcmp_method
class PermutationGroup_generic(FiniteGroup):
"""
A generic permutation group.
EXAMPLES::
sage: G = PermutationGroup([[(1,2,3),(4,5)],[(3,4)]])
sage: G
Permutation Group with generators [(3,4), (1,2,3)(4,5)]
sage: G.center()
Subgroup of (Permutation Group with generators [(3,4), (1,2,3)(4,5)]) generated by [()]
sage: G.group_id() # optional - database_gap
[120, 34]
sage: n = G.order(); n
120
sage: G = PermutationGroup([[(1,2,3),(4,5)],[(3,4)]])
sage: TestSuite(G).run()
"""
def __init__(self, gens=None, gap_group=None, canonicalize=True, domain=None, category=None):
r"""
Initialize ``self``.
INPUT:
- ``gens`` - list of generators (default: ``None``)
- ``gap_group`` - a gap permutation group (default: ``None``)
- ``canonicalize`` - bool (default: ``True``); if ``True``,
sort generators and remove duplicates
OUTPUT:
- A permutation group.
EXAMPLES:
We explicitly construct the alternating group on four
elements::
sage: A4 = PermutationGroup([[(1,2,3)],[(2,3,4)]]); A4
Permutation Group with generators [(2,3,4), (1,2,3)]
sage: A4.__init__([[(1,2,3)],[(2,3,4)]]); A4
Permutation Group with generators [(2,3,4), (1,2,3)]
sage: A4.center()
Subgroup of (Permutation Group with generators [(2,3,4), (1,2,3)]) generated by [()]
sage: A4.category()
Category of finite enumerated permutation groups
sage: TestSuite(A4).run()
TESTS::
sage: TestSuite(PermutationGroup([[]])).run()
sage: TestSuite(PermutationGroup([])).run()
sage: TestSuite(PermutationGroup([(0,1)])).run()
"""
from sage.categories.permutation_groups import PermutationGroups
category = PermutationGroups().FinitelyGenerated().Finite().or_subcategory(category)
super(PermutationGroup_generic, self).__init__(category=category)
if (gens is None and gap_group is None):
raise ValueError("you must specify gens or gap_group")
#Handle the case where only the GAP group is specified.
if gens is None:
if isinstance(gap_group, str):
gap_group = gap(gap_group)
gens = [gen for gen in gap_group.GeneratorsOfGroup()]
if domain is None:
gens = [standardize_generator(x) for x in gens]
domain = set()
for x in gens:
for cycle in x:
domain = domain.union(cycle)
domain = sorted(domain)
#Here we need to check if all of the points are integers
#to make the domain contain all integers up to the max.
#This is needed for backward compatibility
if all(isinstance(p, (Integer,) + integer_types) for p in domain):
domain = list(range(min([1] + domain), max([1] + domain)+1))
if domain not in FiniteEnumeratedSets():
domain = FiniteEnumeratedSet(domain)
self._domain = domain
self._deg = len(self._domain)
self._domain_to_gap = dict((key, i+1) for i, key in enumerate(self._domain))
self._domain_from_gap = dict((i+1, key) for i, key in enumerate(self._domain))
if not gens: # length 0
gens = [()]
gens = [self.element_class(x, self, check=False) for x in gens]
if canonicalize:
gens = sorted(set(gens))
self._gens = gens
def construction(self):
"""
Return the construction of ``self``.
EXAMPLES::
sage: P1 = PermutationGroup([[(1,2)]])
sage: P1.construction()
(PermutationGroupFunctor[(1,2)], Permutation Group with generators [()])
sage: PermutationGroup([]).construction() is None
True
This allows us to perform computations like the following::
sage: P1 = PermutationGroup([[(1,2)]]); p1 = P1.gen()
sage: P2 = PermutationGroup([[(1,3)]]); p2 = P2.gen()
sage: p = p1*p2; p
(1,2,3)
sage: p.parent()
Permutation Group with generators [(1,2), (1,3)]
sage: p.parent().domain()
{1, 2, 3}
Note that this will merge permutation groups with different
domains::
sage: g1 = PermutationGroupElement([(1,2),(3,4,5)])
sage: g2 = PermutationGroup([('a','b')], domain=['a', 'b']).gens()[0]
sage: g2
('a','b')
sage: p = g1*g2; p
(1,2)(3,4,5)('a','b')
"""
gens = self.gens()
if len(gens) == 1 and gens[0].is_one():
return None
else:
from sage.categories.pushout import PermutationGroupFunctor
return (PermutationGroupFunctor(gens, self.domain()),
PermutationGroup([]))
@cached_method
def _has_natural_domain(self):
"""
Returns True if the underlying domain is of the form (1,...,n)
EXAMPLES::
sage: SymmetricGroup(3)._has_natural_domain()
True
sage: SymmetricGroup((1,2,3))._has_natural_domain()
True
sage: SymmetricGroup((1,3))._has_natural_domain()
False
sage: SymmetricGroup((3,2,1))._has_natural_domain()
False
"""
domain = self.domain()
natural_domain = FiniteEnumeratedSet(list(range(1, len(domain)+1)))
return domain == natural_domain
def _gap_init_(self):
r"""
Returns a string showing how to declare / initialize ``self`` in Gap.
Stored in the ``self._gap_string`` attribute.
EXAMPLES:
The ``_gap_init_`` method shows how you
would define the Sage ``PermutationGroup_generic``
object in Gap::
sage: A4 = PermutationGroup([[(1,2,3)],[(2,3,4)]]); A4
Permutation Group with generators [(2,3,4), (1,2,3)]
sage: A4._gap_init_()
'Group([PermList([1, 3, 4, 2]), PermList([2, 3, 1, 4])])'
"""
return 'Group([%s])'%(', '.join([g._gap_init_() for g in self.gens()]))
def _magma_init_(self, magma):
r"""
Returns a string showing how to declare / initialize self in Magma.
EXAMPLES:
We explicitly construct the alternating group on four
elements. In Magma, one would type the string below to construct
the group::
sage: A4 = PermutationGroup([[(1,2,3)],[(2,3,4)]]); A4
Permutation Group with generators [(2,3,4), (1,2,3)]
sage: A4._magma_init_(magma)
'PermutationGroup<4 | (2,3,4), (1,2,3)>'
sage: S = SymmetricGroup(['a', 'b', 'c'])
sage: S._magma_init_(magma)
'PermutationGroup<3 | (1,2,3), (1,2)>'
"""
g = ', '.join([g._gap_cycle_string() for g in self.gens()])
return 'PermutationGroup<%s | %s>'%(self.degree(), g)
def __richcmp__(self, right, op):
"""
Compare ``self`` and ``right``.
The comparison extends the subgroup relation. Hence, it is first checked
whether one of the groups is subgroup of the other. If this is not the
case then the ordering is whatever it is in Gap.
.. NOTE::
The comparison does not provide a total ordering, as can be seen
in the examples below.
EXAMPLES::
sage: G1 = PermutationGroup([[(1,2,3),(4,5)],[(3,4)]])
sage: G2 = PermutationGroup([[(1,2,3),(4,5)]])
sage: G1 > G2 # since G2 is a subgroup of G1
True
sage: G1 < G2
False
The following example shows that the comparison does not yield a total
ordering::
sage: H1 = PermutationGroup([[(1,2)],[(5,6)]])
sage: H2 = PermutationGroup([[(3,4)]])
sage: H3 = PermutationGroup([[(1,2)]])
sage: H1 < H2 # according to Gap's ordering
True
sage: H2 < H3 # according to Gap's ordering
True
sage: H3 < H1 # since H3 is a subgroup of H1
True
"""
if not isinstance(right, PermutationGroup_generic):
return NotImplemented
if self is right:
return rich_to_bool(op, 0)
gSelf = self._gap_()
gRight = right._gap_()
gapcmp = gSelf._cmp_(gRight)
if not gapcmp:
return rich_to_bool(op, 0)
if gSelf.IsSubgroup(gRight):
return rich_to_bool(op, 1)
if gRight.IsSubgroup(gSelf):
return rich_to_bool(op, -1)
return rich_to_bool(op, gapcmp)
Element = PermutationGroupElement
def _element_constructor_(self, x, check=True):
"""
Coerce ``x`` into this permutation group.
The input can be either a string that defines a permutation in
cycle notation, a permutation group element, a list of integers
that gives the permutation as a mapping, a list of tuples, or the
integer 1.
EXAMPLES:
We illustrate each way to make a permutation in `S_4`::
sage: G = SymmetricGroup(4)
sage: G((1,2,3,4))
(1,2,3,4)
sage: G([(1,2),(3,4)])
(1,2)(3,4)
sage: G('(1,2)(3,4)')
(1,2)(3,4)
sage: G('(1,2)(3)(4)')
(1,2)
sage: G(((1,2,3),(4,)))
(1,2,3)
sage: G(((1,2,3,4),))
(1,2,3,4)
sage: G([1,2,4,3])
(3,4)
sage: G([2,3,4,1])
(1,2,3,4)
sage: G(G((1,2,3,4)))
(1,2,3,4)
sage: G(1)
()
Some more examples::
sage: G = PermutationGroup([(1,2,3,4)])
sage: G([(1,3), (2,4)])
(1,3)(2,4)
sage: G(G.0^3)
(1,4,3,2)
sage: G(1)
()
sage: G((1,4,3,2))
(1,4,3,2)
sage: G([(1,2)])
Traceback (most recent call last):
...
TypeError: permutation [(1, 2)] not in Permutation Group with generators [(1,2,3,4)]
"""
if isinstance(x, integer_types + (Integer,)) and x == 1:
return self.identity()
if isinstance(x, PermutationGroupElement):
x_parent = x.parent()
if (isinstance(x_parent, PermutationGroup_subgroup)
and x_parent._ambient_group is self):
return self.element_class(x.cycle_tuples(), self, check=False)
from sage.groups.perm_gps.permgroup_named import SymmetricGroup
compatible_domains = all(point in self._domain_to_gap
for point in x_parent.domain())
if compatible_domains and (isinstance(self, SymmetricGroup)
or x._gap_() in self._gap_()):
return self.element_class(x.cycle_tuples(), self, check=False)
return self.element_class(x, self, check=check)
def _coerce_map_from_(self, G):
r"""
Return if there is a coercion map from ``G`` into ``self``.
EXAMPLES:
We have coercion maps from subgroups::
sage: G = SymmetricGroup(5)
sage: H = G.subgroup([[(1,2,3)], [(4,5)]])
sage: G._coerce_map_from_(H)
True
sage: K = H.subgroup([[(1,2,3)]])
sage: H.has_coerce_map_from(K)
True
sage: G.has_coerce_map_from(K)
True
sage: G = PermutationGroup([[(1,2)], [(3,4,5)], [(2,3)]])
sage: G.has_coerce_map_from(K)
True
We illustrate some arithmetic that involves coercion
of elements in different permutation groups::
sage: g1 = PermutationGroupElement([(1,2),(3,4,5)])
sage: g1.parent()
Symmetric group of order 5! as a permutation group
sage: g2 = PermutationGroupElement([(1,2)])
sage: g2.parent()
Symmetric group of order 2! as a permutation group
sage: g1*g2
(3,4,5)
sage: g2*g2
()
sage: g2*g1
(3,4,5)
We try to convert in a non-permutation::
sage: G = PermutationGroup([[(1,2,3,4)], [(1,2)]])
sage: G(2)
Traceback (most recent call last):
...
TypeError: 'sage.rings.integer.Integer' object is not iterable
"""
if isinstance(G, PermutationGroup_subgroup):
if G._ambient_group is self:
return True
if self.has_coerce_map_from(G._ambient_group):
return self._coerce_map_via([G._ambient_group], G)
if isinstance(G, PermutationGroup_generic):
if G.is_subgroup(self):
return True
return super(PermutationGroup_generic, self)._coerce_map_from_(G)
def list(self):
"""
Return list of all elements of this group.
EXAMPLES::
sage: G = PermutationGroup([[(1,2,3,4)], [(1,2)]])
sage: G.list()
[(), (1,2), (1,2,3,4), (1,3)(2,4), (1,3,4), (2,3,4), (1,4,3,2),
(1,3,2,4), (1,3,4,2), (1,2,4,3), (1,4,2,3), (2,4,3), (1,4,3),
(1,4)(2,3), (1,4,2), (1,3,2), (1,3), (3,4), (2,4), (1,4), (2,3),
(1,2)(3,4), (1,2,3), (1,2,4)]
sage: G = PermutationGroup([[('a','b')]], domain=('a', 'b')); G
Permutation Group with generators [('a','b')]
sage: G.list()
[(), ('a','b')]
TESTS:
Test :trac:`9155`::
sage: G = SymmetricGroup(2)
sage: elements = G.list()
sage: elements.remove(G("()"))
sage: elements
[(1,2)]
sage: G.list()
[(), (1,2)]
"""
return [x for x in self]
def __contains__(self, item):
"""
Returns boolean value of ``item in self``.
EXAMPLES::
sage: G = SymmetricGroup(16)
sage: g = G.gen(0)
sage: h = G.gen(1)
sage: g^7*h*g*h in G
True
sage: G = SymmetricGroup(4)
sage: g = G((1,2,3,4))
sage: h = G((1,2))
sage: H = PermutationGroup([[(1,2,3,4)], [(1,2),(3,4)]])
sage: g in H
True
sage: h in H
False
sage: G = PermutationGroup([[('a','b')]], domain=('a', 'b'))
sage: [('a', 'b')] in G
True
"""
if isinstance(item, integer_types + (Integer,)):
return item == 1
try:
item = self._element_constructor_(item, check=True)
except Exception:
return False
return True
def has_element(self, item):
"""
Returns boolean value of ``item in self`` - however *ignores*
parentage.
EXAMPLES::
sage: G = CyclicPermutationGroup(4)
sage: gens = G.gens()
sage: H = DihedralGroup(4)
sage: g = G([(1,2,3,4)]); g
(1,2,3,4)
sage: G.has_element(g)
True
sage: h = H([(1,2),(3,4)]); h
(1,2)(3,4)
sage: G.has_element(h)
False
"""
return item in self
def __iter__(self):
"""
Return an iterator over the elements of this group.
EXAMPLES::
sage: G = PermutationGroup([[(1,2,3)], [(1,2)]])
sage: [a for a in G]
[(), (1,2), (1,2,3), (2,3), (1,3,2), (1,3)]
Test that it is possible to iterate through moderately large groups
(:trac:`18239`)::
sage: p = [(i,i+1) for i in range(1,601,2)]
sage: q = [tuple(range(1+i,601,3)) for i in range(3)]
sage: A = PermutationGroup([p,q])
sage: A.cardinality()
60000
sage: for x in A: # long time - 2 secs
....: pass # long time
"""
from sage.sets.recursively_enumerated_set import RecursivelyEnumeratedSet
return iter(RecursivelyEnumeratedSet(seeds=[self.one()],
successors=lambda g: (g._mul_(h) for h in self.gens())))
def gens(self):
"""
Return tuple of generators of this group. These need not be
minimal, as they are the generators used in defining this group.
EXAMPLES::
sage: G = PermutationGroup([[(1,2,3)], [(1,2)]])
sage: G.gens()
[(1,2), (1,2,3)]
Note that the generators need not be minimal, though duplicates are
removed::
sage: G = PermutationGroup([[(1,2)], [(1,3)], [(2,3)], [(1,2)]])
sage: G.gens()
[(2,3), (1,2), (1,3)]
We can use index notation to access the generators returned by
``self.gens``::
sage: G = PermutationGroup([[(1,2,3,4), (5,6)], [(1,2)]])
sage: g = G.gens()
sage: g[0]
(1,2)
sage: g[1]
(1,2,3,4)(5,6)
TESTS:
We make sure that the trivial group gets handled correctly::
sage: SymmetricGroup(1).gens()
[()]
"""
return self._gens
def gens_small(self):
"""
For this group, returns a generating set which has few elements.
As neither irredundancy nor minimal length is proven, it is fast.
EXAMPLES::
sage: R = "(25,27,32,30)(26,29,31,28)( 3,38,43,19)( 5,36,45,21)( 8,33,48,24)" ## R = right
sage: U = "( 1, 3, 8, 6)( 2, 5, 7, 4)( 9,33,25,17)(10,34,26,18)(11,35,27,19)" ## U = top
sage: L = "( 9,11,16,14)(10,13,15,12)( 1,17,41,40)( 4,20,44,37)( 6,22,46,35)" ## L = left
sage: F = "(17,19,24,22)(18,21,23,20)( 6,25,43,16)( 7,28,42,13)( 8,30,41,11)" ## F = front
sage: B = "(33,35,40,38)(34,37,39,36)( 3, 9,46,32)( 2,12,47,29)( 1,14,48,27)" ## B = back or rear
sage: D = "(41,43,48,46)(42,45,47,44)(14,22,30,38)(15,23,31,39)(16,24,32,40)" ## D = down or bottom
sage: G = PermutationGroup([R,L,U,F,B,D])
sage: len(G.gens_small())
2
The output may be unpredictable, due to the use of randomized
algorithms in GAP. Note that both the following answers are equally valid.
::
sage: G = PermutationGroup([[('a','b')], [('b', 'c')], [('a', 'c')]])
sage: G.gens_small() # random
[('b','c'), ('a','c','b')] ## (on 64-bit Linux)
[('a','b'), ('a','c','b')] ## (on Solaris)
sage: len(G.gens_small()) == 2
True
"""
gens = self._gap_().SmallGeneratingSet()
return [self.element_class(x, self, check=False) for x in gens]
def gen(self, i=None):
r"""
Returns the i-th generator of ``self``; that is, the i-th element of
the list ``self.gens()``.
The argument `i` may be omitted if there is only one generator (but
this will raise an error otherwise).
EXAMPLES:
We explicitly construct the alternating group on four
elements::
sage: A4 = PermutationGroup([[(1,2,3)],[(2,3,4)]]); A4
Permutation Group with generators [(2,3,4), (1,2,3)]
sage: A4.gens()
[(2,3,4), (1,2,3)]
sage: A4.gen(0)
(2,3,4)
sage: A4.gen(1)
(1,2,3)
sage: A4.gens()[0]; A4.gens()[1]
(2,3,4)
(1,2,3)
sage: P1 = PermutationGroup([[(1,2)]]); P1.gen()
(1,2)
"""
gens = self.gens()
if i is None:
if len(gens) == 1:
return gens[0]
else:
raise ValueError("You must specify which generator you want")
else:
return gens[i]
def ngens(self):
"""
Return the number of generators of ``self``.
EXAMPLES::
sage: A4 = PermutationGroup([[(1,2,3)], [(2,3,4)]]); A4
Permutation Group with generators [(2,3,4), (1,2,3)]
sage: A4.ngens()
2
"""
return len(self.gens())
def identity(self):
"""
Return the identity element of this group.
EXAMPLES::
sage: G = PermutationGroup([[(1,2,3),(4,5)]])
sage: e = G.identity()
sage: e
()
sage: g = G.gen(0)
sage: g*e
(1,2,3)(4,5)
sage: e*g
(1,2,3)(4,5)
sage: S = SymmetricGroup(['a','b','c'])
sage: S.identity()
()
"""
return self.element_class([], self, check=True)
def exponent(self):
r"""
Computes the exponent of the group.
The exponent `e` of a group `G` is the LCM of the orders of its
elements, that is, `e` is the smallest integer such that `g^e=1` for all
`g \in G`.
EXAMPLES::
sage: G = AlternatingGroup(4)
sage: G.exponent()
6
"""
return Integer(self._gap_().Exponent())
def largest_moved_point(self):
"""
Return the largest point moved by a permutation in this group.
EXAMPLES::