-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathGAPGroups.jl
2557 lines (2031 loc) · 73.1 KB
/
GAPGroups.jl
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
# TODO: as soon as GAP packages like `polycyclic` or `rcwa` are loaded,
# the custom group types and isos they define should be added to the arrays
# _gap_group_types resp. _iso_function
group_element(G::T, x::GapObj) where T <: GAPGroup = BasicGAPGroupElem{T}(G, x)
#TODO: document `check_parent`!
# If `check_parent(a, b)` yields `false` then `a` and `b` do not fit
# together.
# A `true` result does *not* imply that the objects will fit together,
# since we check only the Julia side of the data.
#
# The current situation is that GAP knows whether an operation for two
# group elements or for a group and a group element makes sense;
# for example, think about an element and a subgroup of some f.p. group.
#
# The Oscar objects may contain additional information:
# Oscar permutation groups have a degree, and conjugating a permutation group
# by an element of another permutation group shall be allowed only if
# both have the same degree, in order to assign this degree to the result.
# Analogously, Oscar matrix groups have `deg` and `ring`.
check_parent(g::GAPGroupElem, h::GAPGroupElem) = (parent(g) === parent(h))
#TODO: Insert such checks in the code, check for failures!
# default: compare only types
check_parent(G::T, g::GAPGroupElem) where T <: GAPGroup = (T === typeof(g.parent))
# `PermGroup`: compare types and degrees
check_parent(G::PermGroup, g::PermGroupElem) = (degree(G) == degree(parent(g)))
# `MatrixGroup`: compare types, dimensions, and coefficient rings
# (This cannot be defined here because `MatrixGroup` is not yet defined.)
# TODO: ideally the `elements` method below would be turned into a method for
# `collect`, as it is much faster than plain `collect`. Unfortunately, though,
# for some groups (e.g. permutation groups) the order of elements computed
# by this function may differ from that computed by an iterator over G. So
# this is not an option right now.
function elements(G::GAPGroup)
els = GAPWrap.AsList(GapObj(G))
return [group_element(G, x::GapObj) for x in els]
end
function parent(x::GAPGroupElem)
return x.parent
end
# coercion embeds a group element into a different parent
function (G::GAPGroup)(x::BasicGAPGroupElem{T}) where T<:GAPGroup
@req GapObj(x) in GapObj(G) "the element does not embed in the group"
return group_element(G, GapObj(x))
end
"""
is_finite(G::GAPGroup) -> Bool
Return `true` if `G` is finite, and `false` otherwise.
# Examples
```jldoctest
julia> is_finite(symmetric_group(5))
true
julia> is_finite(free_group(2))
false
```
"""
@gapattribute is_finite(G::GAPGroup) = GAP.Globals.IsFinite(GapObj(G))::Bool
# Base.is_finite(G::PcGroup) = true
"""
is_finite_order(g::GAPGroupElem) -> Bool
Return `true` if `g` has finite order, and `false` otherwise.
# Examples
```jldoctest
julia> is_finite_order(gen(symmetric_group(5), 1))
true
julia> is_finite_order(gen(free_group(2), 1))
false
```
"""
is_finite_order(x::GAPGroupElem) = GAPWrap.IsInt(GAPWrap.Order(GapObj(x)))
"""
order(::Type{T} = ZZRingElem, x::Union{GAPGroupElem, GAPGroup}) where T <: IntegerUnion
Return the order of `x`, as an instance of `T`.
For a group element `x` in the group `G`, the order of `x` is the smallest
positive integer `n` such that `x^n` is the identity of `G`.
For a group `x`, the order of `x` is the number of elements in `x`.
An exception is thrown if the order of `x` is infinite,
use [`is_finite`](@ref) for checking the finiteness of a group,
and [`is_finite_order`](@ref) for checking whether a group element
has finite order.
# Examples
```jldoctest
julia> g = symmetric_group(3);
julia> order(g)
6
julia> order(gen(g, 1))
3
julia> g = free_group(1);
julia> is_finite(g)
false
julia> is_finite_order(gen(g, 1))
false
```
"""
function order(::Type{T}, x::Union{GAPGroupElem, GAPGroup}) where T <: IntegerUnion
ord = GAPWrap.Order(GapObj(x))
if ord === GAP.Globals.infinity
throw(InfiniteOrderError(x))
end
return T(ord)
end
has_order(G::GAPGroup) = GAPWrap.HasSize(GapObj(G))
set_order(G::GAPGroup, val::T) where T<:IntegerUnion = GAPWrap.SetSize(GapObj(G), GAP.Obj(val))
"""
is_trivial(G::GAPGroup)
Return `true` if `G` has order `1`, and `false` otherwise.
# Examples
```jldoctest
julia> is_trivial(symmetric_group(1))
true
julia> is_trivial(symmetric_group(2))
false
```
"""
@gapattribute is_trivial(G::GAPGroup) = GAP.Globals.IsTrivial(GapObj(G))::Bool
@doc raw"""
exponent(::Type{T} = ZZRingElem, G::GAPGroup) where T <: IntegerUnion
Return the exponent of `G`, as an instance of `T`,
i.e., the smallest positive integer $e$ such that
$g^e$ is the identity of `G` for every $g$ in `G`.
# Examples
```jldoctest
julia> exponent(symmetric_group(3))
6
julia> exponent(symmetric_group(13))
360360
```
"""
@gapattribute exponent(x::GAPGroup) = ZZRingElem(GAP.Globals.Exponent(GapObj(x))::GapInt)
Base.exponent(::Type{T}, G::GAPGroup) where T <: IntegerUnion = T(GAP.Globals.Exponent(GapObj(G))::GapInt)
"""
rand(rng::Random.AbstractRNG = Random.GLOBAL_RNG, G::Group)
Return a random element of `G`, using the random number generator `rng`.
"""
Base.rand(G::GAPGroup) = Base.rand(Random.GLOBAL_RNG, G)
function Base.rand(rng::Random.AbstractRNG, G::GAPGroup)
s = GAP.Globals.Random(GAP.wrap_rng(rng), GapObj(G))::GapObj
return group_element(G, s)
end
function Base.rand(rng::Random.AbstractRNG, rs::Random.SamplerTrivial{Gr}) where Gr<:GAPGroup
return rand(rng, rs[])
end
"""
rand_pseudo(G::GAPGroup)
Return a pseudo random element of `G`. This works faster than `rand`,
but the returned elements are not necessarily uniformly distributed.
It is sometimes necessary to work with finite groups that we cannot
effectively enumerate, e.g. matrix groups over finite fields. We may not even
know the size of these groups. Yet many algorithms need to sample elements
from the group "as randomly as possible", whatever that means; but also they
need this *fast*.
The function `rand_pseudo` returns elements that are cheap to compute and
somehow random, but makes no guarantees about their distribution.
For finitely presented groups, it returns random words of bounded length.
For finite permutation and matrix groups, it uses a variant of the product
replacement algorithm. For most inputs, the resulting stream of elements
relatively quickly converges to a uniform distribution.
"""
function rand_pseudo(G::GAPGroup; radius::Int = 10)
return group_element(G, GAP.Globals.PseudoRandom(GapObj(G); radius = radius)::GapObj)
end
# We allow arithmetic operations between two group elements with
# *nonidentical parents*.
# In this case, the parent of the resulting element is set according to
# the following rules, depending on the types of the parents.
#
# - `PermGroup`, `PermGroup`:
# The operation is allowed whenever the parents have the same degree,
# then the parent of the result is the symmetric group of that degree.
#
# - `PcGroup`, `PcGroup` and
# `FPGroup`, `FPGroup`:
# The operation is allowed whenever the parents have the same `GapObj`
# (in the sense of `===`),
# then the first of the two groups is taken as the parent of the result.
#
# - `SubPcGroup`, `SubPcGroup` and
# `SubFPGroup`, `SubFPGroup`:
# The operation is allowed whenever the `full_group` fields of the parents
# have the same `GapObj`
# (in the sense of `===`),
# then the first of the two groups is taken as the parent of the result.
#
# - `SubPcGroup`, `PcGroup` and
# `PcGroup`, `SubPcGroup` and
# `SubFPGroup`, `FPGroup` and
# `FPGroup`, `SubFPGroup`:
# The operation is allowed whenever the `full_group` of the `SubPcGroup`
# (`SubFPGroup`) and the `PcGroup` (`FPGroup`) have the same `GapObj`
# (in the sense of `===`),
# then the `full_group` of the `SubPcGroup` (`SubFPGroup`) is taken
# as the parent of the result.
# Note that we take a group of type `SubPcGroup` (`SubFpGroup`) in order
# to achieve type stability:
# Multiplying two `SubPcGroupElem`s with identical parent yields an element
# with this parent, of type `SubPcGroup`, hence the product of two
# `SubPcGroupElem`s with different parents is also a `SubPcGroupElem`.
#
# - `MatrixGroup`, `MatrixGroup`:
# The operation is allowed whenever the two groups have the same `degree`
# and `base_ring`,
# then the general linear group of that degree over that ring
# is taken as the parent of the result.
#
# - `AutomorphismGroup`, `AutomorphismGroup`:
# The operation is allowed whenever the two groups have the same `.G` field,
# then the full automorphism group of that group
# is taken as the parent of the result.
#
# - `DirectProductGroup`, `DirectProductGroup` and
# `SemidirectProductGroup`, `SemidirectProductGroup` and
# `WreathProductGroup`, `WreathProductGroup`:
# The operation is allowed whenever the two groups have the same `.Xfull`
# field,
# then the direct/semidirect/wreath product of these groups
# is taken as the parent of the result.
#
# For other types of groups, we throw an exception if the parent groups are
# not equal and their `GapObj`s are not equal.
#
# Note that we do not want to perform `==` checks that are more expensive
# then `===` checks.
# In general, we cannot guarantee that the Oscar groups objects in question
# are *identical* because the same GAP group can be wrapped several times,
# but we want to force that their `GapObj`s are identical.
# (Permutation groups are an exception,
# we want to force only that the degrees are equal.)
# Thus we regard it as an error for example to ask for the product of two
# `PcGroupElem`s whose parents are equal in the sense of `==`
# but whose `GapObj`s are not identical.
#
function _common_parent_group(x::PermGroup, y::PermGroup)
x === y && return x
@req degree(x) == degree(y) "the groups have different degrees"
return symmetric_group(degree(x))
end
function _common_parent_group(x::PcGroup, y::PcGroup)
GapObj(x) === GapObj(y) && return x
throw(ArgumentError("the groups are not compatible"))
end
function _common_parent_group(x::FPGroup, y::FPGroup)
GapObj(x) === GapObj(y) && return x
throw(ArgumentError("the groups are not compatible"))
end
function _common_parent_group(x::SubPcGroup, y::SubPcGroup)
x === y && return x
@req GapObj(x.full_group) === GapObj(y.full_group) "the groups belong to different full groups"
return as_sub_pc_group(x.full_group)
end
function _common_parent_group(x::SubPcGroup, y::PcGroup)
@req GapObj(x.full_group) === GapObj(y) "the groups belong to different full groups"
return as_sub_pc_group(x.full_group)
end
function _common_parent_group(x::PcGroup, y::SubPcGroup)
@req GapObj(y.full_group) === GapObj(x) "the groups belong to different full groups"
return as_sub_pc_group(y.full_group)
end
function _common_parent_group(x::SubFPGroup, y::SubFPGroup)
x === y && return x
@req GapObj(x.full_group) === GapObj(y.full_group) "the groups belong to different full groups"
return as_sub_fp_group(x.full_group)
end
function _common_parent_group(x::SubFPGroup, y::FPGroup)
@req GapObj(x.full_group) === GapObj(y) "the groups belong to different full groups"
return as_sub_fp_group(x.full_group)
end
function _common_parent_group(x::FPGroup, y::SubFPGroup)
@req GapObj(y.full_group) === GapObj(x) "the groups belong to different full groups"
return as_sub_fp_group(y.full_group)
end
function _common_parent_group(x::AutomorphismGroup{T}, y::AutomorphismGroup{T}) where T <: GAPGroup
x === y && return x
@req x.G === y.G "the groups belong to different full groups"
return automorphism_group(x.G)
end
function _common_parent_group(x::DirectProductGroup, y::DirectProductGroup)
x === y && return x
@req x.Xfull === y.Xfull "the groups belong to different full groups"
return DirectProductGroup(x.Xfull, x.L, x.Xfull, true)
end
function _common_parent_group(x::SemidirectProductGroup, y::SemidirectProductGroup)
x === y && return x
@req x.Xfull === y.Xfull "the groups belong to different full groups"
return SemidirectProductGroup{typeof(x.N), typeof(x.H)}(x.Xfull, x.N, x.H, x.f, x.Xfull, true)
end
function _common_parent_group(x::WreathProductGroup, y::WreathProductGroup)
x === y && return x
@req x.Xfull === y.Xfull "the groups belong to different full groups"
return WreathProductGroup(x.Xfull, x.G, x.H, x.a, x.Xfull, true)
end
# generic method
function _common_parent_group(x::T, y::T) where T <: GAPGroup
(x === y || GapObj(x) == GapObj(y)) && return x
throw(ArgumentError("the groups are not compatible"))
end
function _prod(x::GAPGroupElem, y::GAPGroupElem)
G = _common_parent_group(parent(x), parent(y))
return group_element(G, GapObj(x)*GapObj(y))
end
Base.:*(x::GAPGroupElem, y::GAPGroupElem) = _prod(x, y)
isequal(x::GAPGroup, y::GAPGroup) = GapObj(x) == GapObj(y)
function ==(x::GAPGroup, y::GAPGroup)
_check_compatible(x, y)
return GapObj(x) == GapObj(y)
end
isequal(x::BasicGAPGroupElem, y::BasicGAPGroupElem) = GapObj(x) == GapObj(y)
# For two `BasicGAPGroupElem`s,
# we allow the question for equality if their parents fit together
# in the sense of `_check_compatible`,
# and compare the `GapObj`s if this is the case.
function ==(x::BasicGAPGroupElem, y::BasicGAPGroupElem)
_check_compatible(parent(x), parent(y))
return GapObj(x) == GapObj(y)
end
# For two `GAPGroupElem`s,
# if no specialized method is applicable then no `==` comparison is allowed.
function ==(x::GAPGroupElem, y::GAPGroupElem)
_check_compatible(parent(x), parent(y); error = false) || throw(ArgumentError("parents of x and y are not compatible"))
throw(ArgumentError("== is not implemented for the given types"))
end
"""
one(G::GAPGroup) -> elem_type(G)
Return the identity of the group `G`.
"""
Base.one(x::GAPGroup) = group_element(x, GAPWrap.Identity(GapObj(x)))
"""
one(x::GAPGroupElem{T}) -> GAPGroupElem{T}
Return the identity of the parent group of `x`.
"""
Base.one(x::GAPGroupElem) = one(parent(x))
Base.show(io::IO, x::GAPGroupElem) = print(io, String(GAPWrap.StringViewObj(GapObj(x))))
# Printing GAP groups
function Base.show(io::IO, G::GAPGroup)
@show_name(io, G)
@show_special(io, G)
print(io, "Group")
if !is_terse(io)
if has_order(G)
if is_finite(G)
print(io, " of order ", order(G))
else
print(io, " of infinite order")
end
end
end
end
function Base.show(io::IO, G::Union{FPGroup, SubFPGroup})
@show_name(io, G)
@show_special(io, G)
if GAPWrap.IsFreeGroup(GapObj(G))
print(io, "Free group")
if !is_terse(io) && GAP.Globals.HasRankOfFreeGroup(GapObj(G))::Bool
print(io, " of rank ", GAP.Globals.RankOfFreeGroup(GapObj(G))::Int)
end
else
T = typeof(G) == FPGroup ? "Finitely presented group" : "Sub-finitely presented group"
print(io, T) # FIXME: actually some of these groups are *not* finitely presented
if !is_terse(io)
if has_order(G)
if is_finite(G)
print(io, " of order ", order(G))
else
print(io, " of infinite order")
end
end
end
end
end
function Base.show(io::IO, G::PermGroup)
@show_name(io, G)
@show_special(io, G)
# Treat groups specially which know that they are nat. symmetric/alternating.
io = pretty(io)
if has_is_natural_symmetric_group(G) && is_natural_symmetric_group(G) &&
number_of_moved_points(G) == degree(G)
print(io, LowercaseOff(), "Sym(", degree(G), ")")
elseif has_is_natural_alternating_group(G) && is_natural_alternating_group(G) &&
number_of_moved_points(G) == degree(G)
print(io, LowercaseOff(), "Alt(", degree(G), ")")
else
print(io, "Permutation group")
if !is_terse(io)
print(io, " of degree ", degree(G))
if has_order(G)
if is_finite(G)
print(io, " and order ", order(G))
else
print(io, " and infinite order")
end
elseif GAP.Globals.HasStabChainMutable(GapObj(G))
# HACK: to show order in a few more cases where it is trivial to get
# but really, GAP should be using this anyway?
s = GAP.Globals.SizeStabChain( GAP.Globals.StabChainMutable( GapObj(G) ) )
print(io, " and order ", ZZRingElem(s))
end
end
end
end
function Base.show(io::IO, G::Union{PcGroup,SubPcGroup})
@show_name(io, G)
@show_special(io, G)
T = typeof(G) == PcGroup ? "Pc group" : "Sub-pc group"
print(io, T)
if !is_terse(io)
if isfinite(G)
print(io, " of order ", order(G))
else
print(io, " of infinite order")
end
end
end
Base.isone(x::GAPGroupElem) = GAPWrap.IsOne(GapObj(x))
Base.inv(x::GAPGroupElem) = group_element(parent(x), GAPWrap.Inverse(GapObj(x)))
Base.:^(x::GAPGroupElem, y::Int) = group_element(parent(x), (GapObj(x) ^ y)::GapObj)
Base.:^(x::GAPGroupElem, y::ZZRingElem) = Nemo._generic_power(x, y) # TODO: perhaps let GAP handle this; also handle arbitrary Integer subtypes?
div_right(x::GAPGroupElem, y::GAPGroupElem) = group_element(parent(x), (GapObj(x) / GapObj(y))::GapObj)
div_left(x::GAPGroupElem, y::GAPGroupElem) = group_element(parent(x), (GapObj(y) \ GapObj(x))::GapObj)
Base.conj(x::GAPGroupElem, y::GAPGroupElem) = group_element(_common_parent_group(parent(x), parent(y)), (GapObj(x) ^ GapObj(y))::GapObj)
# AbstractAlgebra defines `x^y` for group elements of the *same* type only.
Base.:^(x::GAPGroupElem, y::GAPGroupElem) = Base.conj(x, y)
"""
comm(x::GAPGroupElem, y::GAPGroupElem)
Return the commutator of `x` and `y`,
which is defined as `x^-1*y^-1*x*y`,
and usually denoted as `[x,y]` in the literature.
"""
comm(x::GAPGroupElem, y::GAPGroupElem) = x^-1*x^y
Base.IteratorSize(::Type{<:GAPGroup}) = Base.SizeUnknown()
Base.IteratorSize(::Type{PermGroup}) = Base.HasLength()
Base.iterate(G::GAPGroup) = iterate(G, GAPWrap.Iterator(GapObj(G)))
function Base.iterate(G::GAPGroup, state)
GAPWrap.IsDoneIterator(state) && return nothing
i = GAPWrap.NextIterator(state)::GapObj
return group_element(G, i), state
end
# need this function just for the iterator
Base.length(x::GAPGroup)::Int = order(Int, x)
"""
Base.in(g::GAPGroupElem, G::GAPGroup)
Return whether `g` is an element of `G`.
The parent of `g` need not be equal to `G`.
"""
Base.in(g::GAPGroupElem, G::GAPGroup) = GapObj(g) in GapObj(G)
"""
gens(G::Group)
Return a vector of generators of `G`.
To get the `i`-th generator,
use `G[i]` or `gen(G,i)` (see [`gen`](@ref)) instead of `gens(G)[i]`,
as that is more efficient.
# Examples
```jldoctest
julia> g = symmetric_group(5); gens(g)
2-element Vector{PermGroupElem}:
(1,2,3,4,5)
(1,2)
julia> g[2]
(1,2)
```
!!! note
The output of `gens(G)` is not, in general, the minimal list of generators for `G`.
"""
function gens(G::GAPGroup)
L = GAPWrap.GeneratorsOfGroup(GapObj(G))::GapObj
res = Vector{elem_type(G)}(undef, length(L))
for i = 1:length(res)
res[i] = group_element(G, L[i]::GapObj)
end
return res
end
"""
has_gens(G::Group)
Return whether generators for the group `G` are known.
# Examples
```jldoctest
julia> F = free_group(2)
Free group of rank 2
julia> has_gens(F)
true
julia> H = derived_subgroup(F)[1]
Free group
julia> has_gens(H)
false
```
"""
has_gens(G::GAPGroup) = GAP.Globals.HasGeneratorsOfGroup(GapObj(G))::Bool
"""
gen(G::GAPGroup, i::Int)
Return `one(G)` if `i == 0`,
the `i`-th element of the vector `gens(G)` if `i` is positive,
and the inverse of the `i`-th element of `gens(G)` if `i` is negative.
For positive `i`, this is equivalent to `G[i]`, and returns `gens(G)[i]`
but may be more efficient than the latter.
An exception is thrown if `abs(i)` is larger than the length of `gens(G)`.
# Examples
```jldoctest
julia> g = symmetric_group(5); gen(g, 1)
(1,2,3,4,5)
julia> g[-1]
(1,5,4,3,2)
```
"""
function gen(G::GAPGroup, i::Int)
i == 0 && return one(G)
L = GAPWrap.GeneratorsOfGroup(GapObj(G))::GapObj
0 < i && i <= length(L) && return group_element(G, L[i]::GapObj)
i < 0 && -i <= length(L) && return group_element(G, inv(L[-i])::GapObj)
@req false "i must be in the range -$(length(L)):$(length(L))"
end
"""
number_of_generators(G::GAPGroup) -> Int
Return the length of the vector [`gens`](@ref)`(G)`.
!!! warning "WARNING:"
this is *NOT*, in general, the minimum number of generators for G.
"""
number_of_generators(G::GAPGroup) = length(GAPWrap.GeneratorsOfGroup(GapObj(G)))
"""
small_generating_set(G::GAPGroup)
Return a reasonably short vector of elements in `G` that generate `G`;
in general the length of this vector is not minimal.
# Examples
```jldoctest
julia> length(small_generating_set(abelian_group(SubPcGroup, [2,3,4])))
2
julia> length(small_generating_set(abelian_group(PermGroup, [2,3,4])))
3
```
"""
@gapattribute function small_generating_set(G::GAPGroup)
# We claim that the finiteness check is cheap in Oscar.
# This does not hold in GAP,
# and GAP's method selection benefits from the known finiteness flag.
if G isa MatrixGroup && is_infinite(base_ring(G))
is_finite(G)
end
L = GAP.Globals.SmallGeneratingSet(GapObj(G))::GapObj
res = Vector{elem_type(G)}(undef, length(L))
for i = 1:length(res)
res[i] = group_element(G, L[i]::GapObj)
end
return res
end
"""
minimal_size_generating_set(G::GAPGroup)
Return a vector of minimal length of elements in `G` that generate `G`.
# Examples
```jldoctest
julia> length(minimal_size_generating_set(abelian_group(SubPcGroup, [2,3,4])))
2
julia> length(minimal_size_generating_set(abelian_group(PermGroup, [2,3,4])))
2
julia> minimal_size_generating_set(symmetric_group(5))
2-element Vector{PermGroupElem}:
(1,2,3,4,5)
(1,2)
```
"""
@gapattribute function minimal_size_generating_set(G::GAPGroup)
L = GAP.Globals.MinimalGeneratingSet(GapObj(G))::GapObj
res = Vector{elem_type(G)}(undef, length(L))
for i = 1:length(res)
res[i] = group_element(G, L[i]::GapObj)
end
return res
end
################################################################################
#
# Conjugacy Classes
#
################################################################################
@attributes mutable struct GAPGroupConjClass{T<:GAPGroup, S<:Union{GAPGroupElem,GAPGroup}} <: GroupConjClass{T, S}
X::T
repr::S
CC::GapObj
function GAPGroupConjClass(G::T, obj::S, C::GapObj) where T<:GAPGroup where S<:Union{GAPGroupElem, GAPGroup}
return new{T, S}(G, obj, C, Dict{Symbol,Any}())
end
end
GAP.@install GapObj(obj::GAPGroupConjClass) = obj.CC
Base.eltype(::Type{GAPGroupConjClass{T,S}}) where {T,S} = S
Base.hash(x::GAPGroupConjClass, h::UInt) = h # FIXME
function Base.show(io::IO, ::MIME"text/plain", x::GAPGroupConjClass)
println(io, "Conjugacy class of")
io = pretty(io)
print(io, Indent())
println(io, Lowercase(), x.repr, " in")
print(io, Lowercase(), acting_group(x))
print(io, Dedent())
end
function Base.show(io::IO, x::GAPGroupConjClass{T, S}) where T where S
if is_terse(io)
if S <: GAPGroupElem
print(io, "Conjugacy class of group elements")
else
print(io, "Conjugacy class of subgroups")
end
else
print(io, "Conjugacy class of ")
io = pretty(io)
print(terse(io), Lowercase(), x.repr, " in ", Lowercase(), acting_group(x))
end
end
action_function(C::GAPGroupConjClass) = ^
==(a::GAPGroupConjClass{T, S}, b::GAPGroupConjClass{T, S}) where S where T = a.CC == b.CC
function Base.length(::Type{T}, C::GAPGroupConjClass) where T <: IntegerUnion
return T(GAPWrap.Size(C.CC))
end
Base.length(C::GroupConjClass) = length(ZZRingElem, C)
Base.lastindex(C::GroupConjClass) = length(C)
Base.keys(C::GroupConjClass) = keys(1:length(C))
is_transitive(C::GroupConjClass) = true
orbit(G::GAPGroup, g::T) where T<: Union{GAPGroupElem, GAPGroup} = conjugacy_class(G, g)
orbits(C::GAPGroupConjClass) = [C]
function permutation(C::GAPGroupConjClass, g::GAPGroupElem)
pi = GAP.Globals.Permutation(GapObj(g), C.CC, GAP.Globals.OnPoints)::GapObj
return group_element(action_range(C), pi)
end
@attr GAPGroupHomomorphism{T, PermGroup} function action_homomorphism(C::GAPGroupConjClass{T}) where T
G = acting_group(C)
acthom = GAP.Globals.ActionHomomorphism(GapObj(G), C.CC, GAP.Globals.OnPoints)::GapObj
# See the comment about `SetJuliaData` in the `action_homomorphism` method
# for `GSetByElements`.
GAP.Globals.SetJuliaData(acthom, GAP.Obj([C, G]))
return GAPGroupHomomorphism(G, action_range(C), acthom)
end
"""
representative(C::GroupConjClass)
Return a representative of the conjugacy class `C`.
# Examples
```jldoctest
julia> G = symmetric_group(4);
julia> C = conjugacy_class(G, G([2, 1, 3, 4]))
Conjugacy class of
(1,2) in
Sym(4)
julia> representative(C)
(1,2)
```
"""
representative(C::GroupConjClass) = C.repr
"""
acting_group(C::GroupConjClass)
Return the acting group of `C`.
# Examples
```jldoctest
julia> G = symmetric_group(4);
julia> C = conjugacy_class(G, G([2, 1, 3, 4]))
Conjugacy class of
(1,2) in
Sym(4)
julia> acting_group(C) === G
true
```
"""
acting_group(C::GroupConjClass) = C.X
# START elements conjugation
"""
conjugacy_class(G::Group, g::GAPGroupElem) -> GroupConjClass
Return the conjugacy class `cc` of `g` in `G`, where `g` = `representative`(`cc`).
# Examples
```jldoctest
julia> G = symmetric_group(4);
julia> C = conjugacy_class(G, G([2, 1, 3, 4]))
Conjugacy class of
(1,2) in
Sym(4)
```
"""
function conjugacy_class(G::GAPGroup, g::GAPGroupElem)
return GAPGroupConjClass(G, g, GAPWrap.ConjugacyClass(GapObj(G),GapObj(g)))
end
function Base.rand(C::GroupConjClass{S,T}) where S where T<:GAPGroupElem
return Base.rand(Random.GLOBAL_RNG, C)
end
function Base.rand(rng::Random.AbstractRNG, C::GAPGroupConjClass{S,T}) where S where T<:GAPGroupElem
return group_element(acting_group(C), GAP.Globals.Random(GAP.wrap_rng(rng), C.CC)::GapObj)
end
Base.in(g::GAPGroupElem, C::GAPGroupConjClass) = GapObj(g) in C.CC
Base.in(G::GAPGroup, C::GAPGroupConjClass) = GapObj(G) in C.CC
Base.IteratorSize(::Type{<:GAPGroupConjClass}) = Base.SizeUnknown()
Base.iterate(cc::GAPGroupConjClass) = iterate(cc, GAPWrap.Iterator(cc.CC))
function Base.iterate(cc::GAPGroupConjClass{S,T}, state::GapObj) where {S,T}
GAPWrap.IsDoneIterator(state) && return nothing
i = GAPWrap.NextIterator(state)::GapObj
if T <: GAPGroupElem
return group_element(acting_group(cc), i), state
else
return _as_subgroup(acting_group(cc), i)[1], state
end
end
"""
number_of_conjugacy_classes(G::GAPGroup)
Return the number of conjugacy classes of elements in `G`.
"""
@gapattribute number_of_conjugacy_classes(G::GAPGroup) = ZZRingElem(GAP.Globals.NrConjugacyClasses(GapObj(G))::GapInt)
number_of_conjugacy_classes(::Type{T}, G::GAPGroup) where T <: IntegerUnion = T(GAPWrap.NrConjugacyClasses(GapObj(G)))
"""
conjugacy_classes(G::Group)
Return a vector of all conjugacy classes of elements in `G`.
It is guaranteed that the class of the identity is in the first position.
"""
function conjugacy_classes(G::GAPGroup)
L=Vector{GapObj}(GAPWrap.ConjugacyClasses(GapObj(G)))
return [GAPGroupConjClass(G, group_element(G, GAPWrap.Representative(cc)), cc) for cc in L]
end
@doc raw"""
is_conjugate(G::GAPGroup, x::GAPGroupElem, y::GAPGroupElem)
Return whether `x` and `y` are conjugate elements in `G`,
i.e., there is an element `z` in `G` such that `inv(z)*x*z` equals `y`.
To also return the element `z`, use
[`is_conjugate_with_data(G::GAPGroup, x::GAPGroupElem, y::GAPGroupElem)`](@ref).
"""
function is_conjugate(G::GAPGroup, x::GAPGroupElem, y::GAPGroupElem)
if isdefined(G,:descr) && (G.descr == :GL || G.descr == :SL)
return is_conjugate_with_data_in_gl_or_sl(G, x, y)[1]
end
return GAPWrap.IsConjugate(GapObj(G), GapObj(x), GapObj(y))
end
"""
is_conjugate_with_data(G::Group, x::GAPGroupElem, y::GAPGroupElem)
If `x` and `y` are conjugate in `G`,
return `(true, z)`, where `inv(z)*x*z == y` holds;
otherwise, return `(false, nothing)`.
If the conjugating element `z` is not needed, use
[`is_conjugate(G::GAPGroup, x::GAPGroupElem, y::GAPGroupElem)`](@ref).
"""
function is_conjugate_with_data(G::GAPGroup, x::GAPGroupElem, y::GAPGroupElem)
if isdefined(G,:descr) && (G.descr == :GL || G.descr == :SL)
return is_conjugate_with_data_in_gl_or_sl(G, x, y)
end
conj = GAPWrap.RepresentativeAction(GapObj(G), GapObj(x), GapObj(y))
if conj != GAP.Globals.fail
return true, group_element(G, conj)
else
return false, nothing
end
end
# END elements conjugation
# START subgroups conjugation
"""
conjugacy_class(G::Group, H::Group) -> GroupConjClass
Return the subgroup conjugacy class `cc` of `H` in `G`, where `H` = `representative`(`cc`).
"""
function conjugacy_class(G::GAPGroup, H::GAPGroup)
#T _check_compatible
return GAPGroupConjClass(G, H, GAPWrap.ConjugacyClassSubgroups(GapObj(G),GapObj(H)))
end
function Base.rand(C::GroupConjClass{S,T}) where S where T<:GAPGroup
return Base.rand(Random.GLOBAL_RNG, C)
end
function Base.rand(rng::Random.AbstractRNG, C::GroupConjClass{S,T}) where S where T<:GAPGroup
return _oscar_subgroup(GAP.Globals.Random(GAP.wrap_rng(rng), C.CC), acting_group(C))
end
"""
subgroup_classes(G::GAPGroup; order::T = ZZRingElem(-1)) where T <: IntegerUnion
Return a vector of all conjugacy classes of subgroups of `G` or,
if `order` is positive, the classes of subgroups of this order.
# Examples
```jldoctest
julia> G = symmetric_group(3)
Sym(3)
julia> subgroup_classes(G)
4-element Vector{GAPGroupConjClass{PermGroup, PermGroup}}:
Conjugacy class of permutation group in G
Conjugacy class of permutation group in G
Conjugacy class of permutation group in G
Conjugacy class of permutation group in G
julia> subgroup_classes(G, order = ZZRingElem(2))
1-element Vector{GAPGroupConjClass{PermGroup, PermGroup}}:
Conjugacy class of permutation group in G
```
"""
function subgroup_classes(G::GAPGroup; order::T = ZZRingElem(-1)) where T <: IntegerUnion
L = Vector{GapObj}(GAPWrap.ConjugacyClassesSubgroups(GapObj(G)))
res = [GAPGroupConjClass(G, _as_subgroup_bare(G, GAPWrap.Representative(cc)), cc) for cc in L]
if order != -1
filter!(x -> AbstractAlgebra.order(representative(x)) == order, res)
end
return res
end
"""
subgroups(G::GAPGroup)
Return an iterator over all subgroups in `G`.
Very likely it is better to use [`subgroup_classes`](@ref) instead.
# Examples
```jldoctest
julia> println([order(H) for H in subgroups(symmetric_group(3))])
ZZRingElem[1, 2, 2, 2, 3, 6]
julia> println([order(H) for H in subgroups(quaternion_group(8))])
ZZRingElem[1, 2, 4, 4, 4, 8]
```
"""
subgroups(G::GAPGroup) = Iterators.flatten(subgroup_classes(G))
"""
maximal_subgroup_classes(G::Group)
Return a vector of all conjugacy classes of maximal subgroups of `G`.