-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathhomomorphisms.jl
1585 lines (1313 loc) · 52.3 KB
/
homomorphisms.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
function Base.show(io::IO, x::GAPGroupHomomorphism)
if is_terse(io)
print(io, "Group homomorphism")
else
io = pretty(io)
print(io, "Hom: ")
print(terse(io), Lowercase(), domain(x), " -> ", Lowercase(), codomain(x))
end
end
function ==(f::GAPGroupHomomorphism, g::GAPGroupHomomorphism)
return GapObj(f) == GapObj(g)
end
function Base.hash(f::GAPGroupHomomorphism{S,T}, h::UInt) where S where T
b = 0xdc777737af4c0c7b % UInt
return xor(hash(GapObj(f), hash((S, T), h)), b)
end
Base.:*(f::GAPGroupHomomorphism{S, T}, g::GAPGroupHomomorphism{T, U}) where S where T where U = compose(f, g)
function Base.inv(f::GAPGroupHomomorphism{S,T}) where S where T
@assert GAPWrap.IsBijective(GapObj(f)) "f is not bijective"
return GAPGroupHomomorphism(codomain(f), domain(f), GAP.Globals.InverseGeneralMapping(GapObj(f)))
end
order(f::GAPGroupHomomorphism) = GAPWrap.Order(GapObj(f))
function Base.:^(f::GAPGroupHomomorphism{S,T}, n::Int64) where S where T
if n==1
return f
else
@assert domain(f) == codomain(f) "Domain and codomain do not coincide"
return GAPGroupHomomorphism(domain(f),codomain(f),(GapObj(f))^n)
end
end
function compose(f::GAPGroupHomomorphism{S, T}, g::GAPGroupHomomorphism{T, U}) where {S, T, U}
dom = domain(f)
cod = codomain(g)
@assert codomain(f) == domain(g)
mp = GAP.Globals.CompositionMapping(GapObj(g), GapObj(f))
return GAPGroupHomomorphism(dom, cod, mp)
end
#(g::GAPGroupHomomorphism{T, U})(f::GAPGroupHomomorphism{S, T}) where {S,T,U} = compose(f,g)
"""
id_hom(G::GAPGroup)
Return the identity homomorphism on the group `G`.
"""
function id_hom(G::GAPGroup)
return GAPGroupHomomorphism(G, G, GAP.Globals.IdentityMapping(GapObj(G)))
end
"""
trivial_morphism(G::GAPGroup, H::GAPGroup = G)
Return the homomorphism from `G` to `H` sending every element of `G` into the
identity of `H`.
"""
function trivial_morphism(G::GAPGroup, H::GAPGroup = G)
return hom(G, H, _ -> one(H))
end
"""
hom(G::GAPGroup, H::GAPGroup, f::Function)
Return the group homomorphism defined by the function `f`.
"""
function hom(G::GAPGroup, H::GAPGroup, img::Function)
#I create the gap function from the julia function
#The julia function is supposed to be defined on GAPGroupElem
#We need a function defined on the underlying GapObj
function gap_fun(x::GapObj)
el = group_element(G, x)
img_el = img(el)
return GapObj(img_el)
end
mp = GAPWrap.GroupHomomorphismByFunction(GapObj(G), GapObj(H), GAP.Obj(gap_fun))
return GAPGroupHomomorphism(G, H, mp)
end
# This method is used for those embeddings that are
# the identity on the GAP side.
function hom(G::GAPGroup, H::GAPGroup, img::Function, preimg::Function; is_known_to_be_bijective::Bool = false)
function gap_fun(x::GapObj)
el = group_element(G, x)
img_el = img(el)
return GapObj(img_el)
end
function gap_pre_fun(x::GapObj)
el = group_element(H, x)
preimg_el = preimg(el)
return GapObj(preimg_el)
end
if is_known_to_be_bijective
mp = GAPWrap.GroupHomomorphismByFunction(GapObj(G), GapObj(H), GAP.Obj(gap_fun), GAP.Obj(gap_pre_fun))
else
mp = GAPWrap.GroupHomomorphismByFunction(GapObj(G), GapObj(H), GAP.Obj(gap_fun), false, GAP.Obj(gap_pre_fun))
end
return GAPGroupHomomorphism(G, H, mp)
end
"""
hom(G::Group, H::Group, gensG::Vector = gens(G), imgs::Vector; check::Bool = true)
Return the group homomorphism defined by `gensG`[`i`] -> `imgs`[`i`] for every
`i`. In order to work, the elements of `gensG` must generate `G`.
If `check` is set to `false` then it is not checked whether the mapping
defines a group homomorphism.
"""
function hom(G::GAPGroup, H::GAPGroup, gensG::Vector, imgs::Vector; check::Bool = true)
vgens = GapObj(gensG; recursive = true)
vimgs = GapObj(imgs; recursive = true)
if check
mp = GAP.Globals.GroupHomomorphismByImages(GapObj(G), GapObj(H), vgens, vimgs)
else
mp = GAP.Globals.GroupHomomorphismByImagesNC(GapObj(G), GapObj(H), vgens, vimgs)
end
@req mp !== GAP.Globals.fail "Invalid input"
return GAPGroupHomomorphism(G, H, mp)
end
function hom(G::GAPGroup, H::GAPGroup, imgs::Vector; check::Bool = true)
return hom(G, H, gens(G), imgs; check)
end
################################################################################
#
# `hom` between `GAPGroup` and `FinGenAbGroup`
#
################################################################################
# Map `G::GAPGroup` to `A::FinGenAbGroup` by prescribing images.
# Return a composition of homomorphisms `G -> G/G' -> B -> A`,
# not a `GAPGroupHomomorphism`.
function hom(G::GAPGroup, A::FinGenAbGroup, gensG::Vector, imgs::Vector{FinGenAbGroupElem}; check::Bool = true)
# map G to G/G'
(q, map1) = quo(G, derived_subgroup(G)[1])
# map G/G' to an isomorphic additive group B
iso = isomorphism(FinGenAbGroup, q)
B = codomain(iso)
# map B to A as prescribed
if length(gensG) == 0
map2 = hom(B, A, [zero(B)], [zero(A)], check = check)
else
map2 = hom(B, A, [iso(map1(x)) for x in gensG], imgs, check = check)
end
# create the composition
return compose(map1, compose(iso, map2))
end
function hom(G::GAPGroup, A::FinGenAbGroup, imgs::Vector{FinGenAbGroupElem}; check::Bool = true)
return hom(G, A, gens(G), imgs; check)
end
# Map `A::FinGenAbGroup` to `G::GAPGroup` by prescribing images.
# Return a composition `A -> B -> G` where `A -> B` is an isomorphism
# to a `GAPGroup`.
function hom(A::FinGenAbGroup, G::GAPGroup, gensA::Vector, imgs::Vector{<:GAPGroupElem}; check::Bool = true)
map1 = isomorphism(PcGroup, A)
map2 = hom(codomain(map1), G, [map1(x) for x in gensA], imgs, check = check)
return compose(map1, map2)
end
function hom(A::FinGenAbGroup, G::GAPGroup, imgs::Vector{<:GAPGroupElem}; check::Bool = true)
return hom(A, G, gens(A), imgs; check)
end
################################################################################
#
# `hom` between `GAPGroup` and `MultTableGroup`
#
################################################################################
# Map `G::GAPGroup` to `M::MultTableGroup` by prescribing images.
# Return a composition of homomorphisms `G -> B -> M`,
# not a `GAPGroupHomomorphism`.
function hom(G::GAPGroup, M::MultTableGroup, gensG::Vector, imgs::Vector{MultTableGroupElem}; check::Bool = true)
# map M to an isomorphic perm. group B
iso = isomorphism(PermGroup, M)
B = codomain(iso)
# map G to B as prescribed
map1 = hom(G, B, gensG, [iso(x) for x in imgs], check = check)
# create the composition
return compose(map1, inv(iso))
end
function hom(G::GAPGroup, M::MultTableGroup, imgs::Vector{MultTableGroupElem}; check::Bool = true)
return hom(G, M, gens(G), imgs; check)
end
# Map `M::MultTableGroup` to `G::GAPGroup` by prescribing images.
# Return a composition `M -> B -> G` where `M -> B` is an isomorphism
# to a `GAPGroup`.
function hom(M::MultTableGroup, G::GAPGroup, gensM::Vector, imgs::Vector{<:GAPGroupElem}; check::Bool = true)
map1 = isomorphism(PermGroup, M)
map2 = hom(codomain(map1), G, [map1(x) for x in gensM], imgs, check = check)
return compose(map1, map2)
end
function hom(M::MultTableGroup, G::GAPGroup, imgs::Vector{<:GAPGroupElem}; check::Bool = true)
return hom(M, G, gens(M), imgs; check)
end
function domain(f::GAPGroupHomomorphism)
return f.domain
end
function codomain(f::GAPGroupHomomorphism)
return f.codomain
end
(f::GAPGroupHomomorphism)(x::GAPGroupElem) = image(f, x)
Base.:^(x::GAPGroupElem,f::GAPGroupHomomorphism) = image(f,x)
"""
image(f::GAPGroupHomomorphism, x::GAPGroupElem)
(f::GAPGroupHomomorphism)(x::GAPGroupElem)
Return `f`(`x`).
"""
function image(f::GAPGroupHomomorphism, x::GAPGroupElem)
return group_element(codomain(f), GAPWrap.ImagesRepresentative(GapObj(f), GapObj(x)))
end
"""
preimage(f::GAPGroupHomomorphism, x::GAPGroupElem)
Return an element `y` in the domain of `f` with the property `f(y) == x`.
See [`has_preimage_with_preimage(f::GAPGroupHomomorphism, x::GAPGroupElem; check::Bool = true)`](@ref)
for a check whether `x` has such a preimage.
"""
function preimage(f::GAPGroupHomomorphism, x::GAPGroupElem)
fl, p = has_preimage_with_preimage(f, x)
@assert fl
return p
end
"""
is_surjective(f::GAPGroupHomomorphism)
Return whether `f` is surjective.
"""
function is_surjective(f::GAPGroupHomomorphism)
return GAPWrap.IsSurjective(GapObj(f))
end
"""
is_injective(f::GAPGroupHomomorphism)
Return whether `f` is injective.
"""
function is_injective(f::GAPGroupHomomorphism)
return GAPWrap.IsInjective(GapObj(f))
end
"""
is_invertible(f::GAPGroupHomomorphism)
Return whether `f` is invertible.
"""
function is_invertible(f::GAPGroupHomomorphism)
return GAPWrap.IsBijective(GapObj(f))
end
"""
is_bijective(f::GAPGroupHomomorphism)
Return whether `f` is bijective.
"""
function is_bijective(f::GAPGroupHomomorphism)
return GAPWrap.IsBijective(GapObj(f))
end
"""
is_invariant(f::GAPGroupHomomorphism, H::GAPGroup)
Return whether `f(H) == H` holds.
An exception is thrown if `domain(f)` and `codomain(f)` are not equal
or if `H` is not contained in `domain(f)`.
"""
function is_invariant(f::GAPGroupHomomorphism, H::GAPGroup)
@assert domain(f) == codomain(f) "Not an endomorphism!"
@assert GAPWrap.IsSubset(GapObj(domain(f)), GapObj(H)) "Not a subgroup of the domain"
return GAPWrap.Image(GapObj(f), GapObj(H)) == GapObj(H)
end
"""
restrict_homomorphism(f::GAPGroupHomomorphism, H::Group)
restrict_homomorphism(f::GAPGroupElem{AutomorphismGroup{T}}, H::T) where T <: Group
Return the restriction of `f` to `H`.
An exception is thrown if `H` is not a subgroup of `domain(f)`.
"""
function restrict_homomorphism(f::GAPGroupHomomorphism, H::GAPGroup)
# We have to check whether `H` is really a subgroup of `f.domain`,
# since `GAP.Globals.RestrictedMapping` does not check this.
# (The GAP documentation does not claim anything about the result
# in the case that `H` is not a subgroup of `f.domain`,
# and in fact just the given map may be returned.)
@assert is_subset(H, domain(f)) "Not a subgroup!"
return GAPGroupHomomorphism(H, f.codomain, GAP.Globals.RestrictedMapping(GapObj(f), GapObj(H))::GapObj)
end
################################################################################
#
# Image, Kernel, Cokernel
#
################################################################################
"""
kernel(f::GAPGroupHomomorphism)
Return the kernel of `f`, together with its embedding into `domain`(`f`).
"""
function kernel(f::GAPGroupHomomorphism)
K = GAP.Globals.Kernel(GapObj(f))::GapObj
return _as_subgroup(domain(f), K)
end
"""
image(f::GAPGroupHomomorphism)
Return the image of `f` as subgroup of `codomain`(`f`), together with the embedding homomorphism.
"""
function image(f::GAPGroupHomomorphism)
K = GAPWrap.Image(GapObj(f))
return _as_subgroup(codomain(f), K)
end
"""
image(f::GAPGroupHomomorphism{S, T}, H::S) where S <: GAPGroup where T <: GAPGroup
(f::GAPGroupHomomorphism{S, T})(H::S)
Return `f`(`H`), together with the embedding homomorphism into `codomain`(`f`).
"""
function image(f::GAPGroupHomomorphism{S, T}, H::S) where S <: GAPGroup where T <: GAPGroup
H1 = GAPWrap.Image(GapObj(f), GapObj(H))
return _as_subgroup(codomain(f), H1)
end
(f::GAPGroupHomomorphism{S, T})(H::S) where S <: GAPGroup where T <: GAPGroup = image(f,H)
"""
cokernel(f::GAPGroupHomomorphism)
Return the cokernel of `f`, that is, the quotient of the codomain of `f`
by the normal closure of the image.
"""
function cokernel(f::GAPGroupHomomorphism)
K, mK = image(f)
C = codomain(f)
return quo(C, normal_closure(C, K)[1])
end
"""
has_preimage_with_preimage(f::GAPGroupHomomorphism, x::GAPGroupElem; check::Bool = true)
Return (`true`, `y`) if there exists `y` in `domain(f)`
such that `f`(`y`) = `x` holds;
otherwise, return (`false`, `o`) where `o` is the identity of `domain(f)`.
If `check` is set to `false` then the test whether `x` is an element of
`image(f)` is omitted.
"""
function has_preimage_with_preimage(f::GAPGroupHomomorphism, x::GAPGroupElem; check::Bool = true)
return _haspreimage(GapObj(f), domain(f), image(f)[1], x, check = check)
end
# helper function for computing `has_preimage_with_preimage` for
# both `GAPGroupHomomorphism` (fieldnames `domain`, `codomain`, `map`)
# and `AutomorphismGroupElem{T}` (fieldnames `parent`, `X`)
function _haspreimage(mp::GapObj, dom::GAPGroup, img::GAPGroup, x::GAPGroupElem; check::Bool = true)
# `GAP.Globals.PreImagesRepresentative` does not promise anything
# if the given element is not in the codomain of the map.
#TODO:
# Apparently the documentation of `GAP.Globals.PreImagesRepresentative`
# is wrong in the situation that `x` is not in the *image* of `f`,
# the function can then run into an error or return some group element,
# see https://github.com/gap-system/gap/issues/4088.
# Until this problem gets fixed on the GAP side, we perform a membership test
# before calling `GAP.Globals.PreImagesRepresentative`.
check && ! (x in img) && return false, one(dom)
r = GAP.Globals.PreImagesRepresentative(mp, GapObj(x))::GapObj
if r == GAP.Globals.fail
return false, one(dom)
else
return true, group_element(dom, r)
end
end
"""
preimage(f::GAPGroupHomomorphism{S, T}, H::GAPGroup) where S <: GAPGroup where T <: GAPGroup
If `H` is a subgroup of the codomain of `f`, return the subgroup `f^-1(H)`,
together with its embedding homomorphism into the domain of `f`.
"""
function preimage(f::GAPGroupHomomorphism{S, T}, H::GAPGroup) where S <: GAPGroup where T <: GAPGroup
H1 = GAP.Globals.PreImage(GapObj(f), GapObj(H))::GapObj
return _as_subgroup(domain(f), H1)
end
################################################################################
#
# IsIsomorphic
#
################################################################################
"""
is_isomorphic_with_map(G::Group, H::Group)
Return (`true`,`f`) if `G` and `H` are isomorphic groups, where `f` is a group
isomorphism. Otherwise, return (`false`,`f`), where `f` is the trivial
homomorphism.
# Examples
```jldoctest
julia> is_isomorphic_with_map(symmetric_group(3), dihedral_group(6))
(true, Hom: Sym(3) -> pc group)
```
"""
function is_isomorphic_with_map(G::GAPGroup, H::GAPGroup)
mp = GAP.Globals.IsomorphismGroups(GapObj(G), GapObj(H))::GapObj
if mp === GAP.Globals.fail
return false, trivial_morphism(G, H)
else
return true, GAPGroupHomomorphism(G, H, mp)
end
end
function is_isomorphic_with_map(G::GAPGroup, H::MultTableGroup)
HtoP = isomorphism(PermGroup, H)
P = codomain(HtoP)
fl, GtoP = is_isomorphic_with_map(G, P)
if !fl
return false, nothing
else
return true, GtoP * inv(HtoP)
end
end
function is_isomorphic_with_map(G::MultTableGroup, H::GAPGroup)
GtoP = isomorphism(PermGroup, G)
P = codomain(GtoP)
fl, PtoH = is_isomorphic_with_map(P, H)
if !fl
return false, nothing
else
return true, GtoP * PtoH
end
end
"""
is_isomorphic(G::Group, H::Group)
Return `true` if `G` and `H` are isomorphic groups, and `false` otherwise.
# Examples
```jldoctest
julia> is_isomorphic(symmetric_group(3), dihedral_group(6))
true
```
"""
function is_isomorphic(G::GAPGroup, H::GAPGroup)
mp = GAP.Globals.IsomorphismGroups(GapObj(G), GapObj(H))::GapObj
return mp !== GAP.Globals.fail
end
function is_isomorphic(G::GAPGroup, H::MultTableGroup)
P = PermGroup(H)
return is_isomorphic(G, P)
end
function is_isomorphic(G::MultTableGroup, H::GAPGroup)
P = PermGroup(G)
return is_isomorphic(P, H)
end
function is_isomorphic(G::GAPGroup, H::FinGenAbGroup)
is_abelian(G) || return false
return abelian_invariants(G) == abelian_invariants(H)
end
function is_isomorphic(G::FinGenAbGroup, H::GAPGroup)
is_abelian(H) || return false
return abelian_invariants(G) == abelian_invariants(H)
end
"""
isomorphism(G::Group, H::Group)
Return a group isomorphism between `G` and `H` if they are isomorphic groups.
Otherwise throw an exception.
# Examples
```jldoctest
julia> isomorphism(symmetric_group(3), dihedral_group(6))
Group homomorphism
from Sym(3)
to pc group of order 6
```
"""
function isomorphism(G::GAPGroup, H::GAPGroup)
mp = GAP.Globals.IsomorphismGroups(GapObj(G), GapObj(H))::GapObj
@req mp !== GAP.Globals.fail "the groups are not isomorphic"
return GAPGroupHomomorphism(G, H, mp)
end
function isomorphism(G::GAPGroup, A::FinGenAbGroup)
@req is_abelian(G) "the groups are not isomorphic"
map2 = isomorphism(PcGroup, A)
map1 = isomorphism(G, codomain(map2))
return compose(map1, inv(map2))
end
function isomorphism(A::FinGenAbGroup, G::GAPGroup)
@req is_abelian(G) "the groups are not isomorphic"
map1 = isomorphism(PcGroup, A)
map2 = isomorphism(codomain(map1), G)
return compose(map1, map2)
end
function isomorphism(G::GAPGroup, H::MultTableGroup)
HtoP = isomorphism(PermGroup, H)
P = codomain(HtoP)
fl, GtoP = is_isomorphic_with_map(G, P)
@req fl "the groups are not isomorphic"
return GtoP * inv(HtoP)
end
function isomorphism(G::MultTableGroup, H::GAPGroup)
GtoP = isomorphism(PermGroup, G)
P = codomain(GtoP)
fl, PtoH = is_isomorphic_with_map(P, H)
@req fl "the groups are not isomorphic"
return GtoP * PtoH
end
"""
isomorphic_subgroups(H::Group, G::Group)
Return a vector of injective group homomorphism from `H` to `G`,
where the images are representatives of those conjugacy classes
of subgroups of `G` that are isomorphic with `H`.
# Examples
```jldoctest
julia> isomorphic_subgroups(alternating_group(5), alternating_group(6))
2-element Vector{GAPGroupHomomorphism{PermGroup, PermGroup}}:
Hom: Alt(5) -> Alt(6)
Hom: Alt(5) -> Alt(6)
julia> isomorphic_subgroups(symmetric_group(4), alternating_group(5))
GAPGroupHomomorphism{PermGroup, PermGroup}[]
```
"""
function isomorphic_subgroups(H::GAPGroup, G::GAPGroup)
res = GAPWrap.IsomorphicSubgroups(GapObj(G), GapObj(H))
return [GAPGroupHomomorphism(H, G, x) for x in res]
end
function isomorphic_subgroups(H::FinGenAbGroup, G::GAPGroup)
isoH = isomorphism(PcGroup, H)
res = isomorphic_subgroups(codomain(isoH), G)
return [isoH*x for x in res]
end
function isomorphic_subgroups(H::GAPGroup, G::FinGenAbGroup)
isoG = inv(isomorphism(PcGroup, G))
res = isomorphic_subgroups(H, domain(isoG))
return [x*isoG for x in res]
end
function isomorphic_subgroups(H::FinGenAbGroup, G::FinGenAbGroup)
isoH = isomorphism(PcGroup, H)
isoG = inv(isomorphism(PcGroup, G))
res = isomorphic_subgroups(codomain(isoH), domain(isoG))
return [isoH*x*isoG for x in res]
end
################################################################################
#
# Conversions between types
#
################################################################################
function _isomorphism_same_type(G::T, on_gens::Bool) where T <: GAPGroup
# Known isomorphisms are cached in the attribute `:isomorphisms`.
isos = get_attribute!(Dict{Tuple{Type, Bool}, Any}, G, :isomorphisms)::Dict{Tuple{Type, Bool}, Any}
return get!(isos, (T, on_gens)) do
return id_hom(G)
end::GAPGroupHomomorphism{T, T}
end
for T in [PermGroup, SubFPGroup, SubPcGroup]
@eval begin
function isomorphism(::Type{$T}, G::$T; on_gens::Bool = false)
on_gens && error("only `on_gens = false` is supported for `isomorphism(::Type{$T}, G::$T)`")
return _isomorphism_same_type(G, on_gens)
end
end
end
for T in [FPGroup, PcGroup]
@eval begin
isomorphism(::Type{$T}, G::$T; on_gens::Bool = false) = _isomorphism_same_type(G, on_gens)
end
end
_get_iso_function(::Type{PermGroup}) = GAP.Globals.IsomorphismPermGroup
# We use `GAP.Globals.IsomorphismPcGroup` as the `_get_iso_function` value
# for both `PcGroup` and `SubPcGroup`.
# Note that `_get_iso_function` is used to create a GAP mapping,
# and afterwards an Oscar mapping is created whose codomain is obtained
# from the codomain `G` in GAP by calling `T(G)` where `T` is the desired type.
_get_iso_function(::Type{PcGroup}) = GAP.Globals.IsomorphismPcGroup
_get_iso_function(::Type{SubPcGroup}) = GAP.Globals.IsomorphismPcGroup
_get_iso_function(::Type{SubFPGroup}) = GAP.Globals.IsomorphismFpGroup
function isomorphism(::Type{T}, G::GAPGroup; on_gens::Bool=false) where T <: Union{SubPcGroup, SubFPGroup, PermGroup}
on_gens && error("only `on_gens = false` is supported for `isomorphism(::Type{<:Union{SubPcGroup, SubFPGroup, PermGroup}}, G::GAPGroup)`")
# Known isomorphisms are cached in the attribute `:isomorphisms`.
isos = get_attribute!(Dict{Tuple{Type, Bool}, Any}, G, :isomorphisms)::Dict{Tuple{Type, Bool}, Any}
return get!(isos, (T, on_gens)) do
fun = _get_iso_function(T)
f = fun(GapObj(G))::GapObj
@req f !== GAP.Globals.fail "Could not convert group into a group of type $T"
H = T(GAPWrap.ImagesSource(f))
# TODO: remove the next line once GAP 4.13.0 is available in Oscar -> still broken in GAP 4.14.0
GAP.Globals.UseIsomorphismRelation(GapObj(G), GapObj(H))
return GAPGroupHomomorphism(G, H, f)
end::GAPGroupHomomorphism{typeof(G), T}
end
function isomorphism(::Type{FPGroup}, G::GAPGroup; on_gens::Bool=false)
# Known isomorphisms are cached in the attribute `:isomorphisms`.
isos = get_attribute!(Dict{Tuple{Type, Bool}, Any}, G, :isomorphisms)::Dict{Tuple{Type, Bool}, Any}
return get!(isos, (FPGroup, on_gens)) do
if on_gens
Ggens = GAPWrap.GeneratorsOfGroup(GapObj(G))
# The computations are easy if `Ggens` is a pcgs,
# otherwise GAP will call `CoKernel`.
if GAP.Globals.HasFamilyPcgs(GapObj(G))
pcgs = GAP.Globals.InducedPcgsWrtFamilyPcgs(GapObj(G))
if pcgs == Ggens
# `pcgs` fits *and* is an object in `GAP.Globals.IsPcgs`,
# for which a special `GAPWrap.IsomorphismFpGroupByGenerators`
# method is applicable.
# (Currently the alternative is a cokernel computation.
# It might be useful to improve this on the GAP side.)
Ggens = pcgs
end
end
f = GAPWrap.IsomorphismFpGroupByGenerators(GapObj(G), Ggens)
else
f = GAPWrap.IsomorphismFpGroup(GapObj(G))
end
@req f !== GAP.Globals.fail "Could not convert group into a group of type FPGroup"
H = FPGroup(GAPWrap.ImagesSource(f))
return GAPGroupHomomorphism(G, H, f)
end::GAPGroupHomomorphism{typeof(G), FPGroup}
end
# very simpleminded, should be supported by GAP ...
function _is_pc_sequence(list::Vector{T}) where T <: GAPGroupElem
l = length(list)
l == 0 && return true
F = parent(list[1])
G = trivial_subgroup(F)[1]
for i in l:-1:1
H = G
G = sub(F, list[i:l])[1]
(is_normal_subgroup(H, G) && is_prime(index(G, H))) || return false
end
return true
end
# If `G` is not a full pc group then switch to a full pc group in Oscar.
# If `G` is infinite then there is `GAP.Globals.IsomorphismPcGroup`,
# but it has currently only methods for finite groups or for groups
# that come from the packages Cryst, AClib, or Polenta.
function isomorphism(T::Type{PcGroup}, G::GAPGroup; on_gens::Bool=false)
# Known isomorphisms are cached in the attribute `:isomorphisms`.
isos = get_attribute!(Dict{Tuple{Type, Bool}, Any}, G, :isomorphisms)::Dict{Tuple{Type, Bool}, Any}
return get!(isos, (T, on_gens)) do
if on_gens
# Throw an exception if `gens(G)` is not a polycyclic sequence for `G`.
@req _is_pc_sequence(gens(G)) "the generators do not form a pc sequence"
# Create a group in `IsPcGroup` if `G` is finite,
# and a group in `IsPcpGroup` otherwise.
if is_finite(G)
fam = GAP.Globals.ElementsFamily(GAP.Globals.FamilyObj(GapObj(G)))::GapObj
Ggens = GapObj(gens(G); recursive = true)::GapObj
Cpcgs = GAP.Globals.PcgsByPcSequence(fam, Ggens)::GapObj
CC = GAP.Globals.PcGroupWithPcgs(Cpcgs)::GapObj
CCpcgs = GAPWrap.FamilyPcgs(CC)
f = GAP.Globals.GroupHomomorphismByImages(GapObj(G), CC, Cpcgs, CCpcgs)::GapObj
return GAPGroupHomomorphism(G, T(CC), f)
else
f = GAP.Globals.IsomorphismPcpGroup(GapObj(G))::GapObj
#T how to create a pcp that consists of the images of `gens(G)`,
#T or get `fail`?
error("do not know how to create a pcp group on given generators in GAP")
end
else
fun = _get_iso_function(T)
#T change this: use IsomorphismPcpGroup if G is infinite!
f = fun(GapObj(G))::GapObj
@req f !== GAP.Globals.fail "Could not convert group into a group of type $T"
# The codomain of `f` can be a *subgroup* of a full pc group.
# In this situation, we have to switch to a full pc group.
C = GAP.Globals.Range(f)::GapObj
if !_is_full_pc_group(C)
@assert GAPWrap.IsPcGroup(C) || GAP.Globals.IsPcpGroup(C)::Bool
if GAPWrap.IsPcGroup(C)::Bool
Cpcgs = GAP.Globals.Pcgs(C)::GapObj
CC = GAP.Globals.PcGroupWithPcgs(Cpcgs)::GapObj
CCpcgs = GAPWrap.FamilyPcgs(CC)
else
Cpcgs = GAP.Globals.Pcp(C)::GapObj
CC = GAP.Globals.PcpGroupByPcp(Cpcgs)::GapObj
CCpcgs = GAP.Globals.Pcp(CC)::GapObj
end
switch = GAP.Globals.GroupHomomorphismByImages(C, CC, Cpcgs, CCpcgs)::GapObj
f = GAP.Globals.CompositionMapping(switch, f)::GapObj
end
end
H = T(GAPWrap.ImagesSource(f))
return GAPGroupHomomorphism(G, H, f)
end::GAPGroupHomomorphism{typeof(G), T}
end
"""
isomorphism(::Type{FinGenAbGroup}, G::GAPGroup)
Return a map from `G` to an isomorphic (additive) group of type `FinGenAbGroup`.
An exception is thrown if `G` is not abelian or not finite.
"""
function isomorphism(::Type{FinGenAbGroup}, G::GAPGroup; on_gens::Bool=false)
on_gens && error("only `on_gens = false` is supported for `isomorphism(::Type{FinGenAbGroup}, G::GAPGroup)`")
# Known isomorphisms are cached in the attribute `:isomorphisms`.
isos = get_attribute!(Dict{Tuple{Type, Bool}, Any}, G, :isomorphisms)::Dict{Tuple{Type, Bool}, Any}
return get!(isos, (FinGenAbGroup, on_gens)) do
@req is_abelian(G) "the group is not abelian"
@req is_finite(G) "the group is not finite"
#T this restriction is not nice
indep = GAP.Globals.IndependentGeneratorsOfAbelianGroup(GapObj(G))::GapObj
orders = ZZRingElem[GAPWrap.Order(x) for x in indep]
n = length(indep)
A = abelian_group(FinGenAbGroup, orders)
f(g) = A(Vector{ZZRingElem}(GAPWrap.IndependentGeneratorExponents(GapObj(G), GapObj(g))))
finv = function(g::elem_type(FinGenAbGroup))
res = GAPWrap.One(GapObj(G))
for i in 1:n
res = res * indep[i]^GAP.Obj(g.coeff[i])
end
return group_element(G, res)
end
return GroupIsomorphismFromFunc(G, A, f, finv)
end::GroupIsomorphismFromFunc{typeof(G), FinGenAbGroup}
end
"""
isomorphism(::Type{T}, A::FinGenAbGroup) where T <: GAPGroup
Return an isomorphism from `A` to a group of type `T`.
An exception is thrown if no such isomorphism exists.
"""
function isomorphism(::Type{T}, A::FinGenAbGroup; on_gens::Bool=false) where T <: GAPGroup
on_gens && error("only `on_gens = false` is supported for `isomorphism(::Type{<:GAPGroup}, A::FinGenAbGroup)`")
# Known isomorphisms are cached in the attribute `:isomorphisms`.
isos = get_attribute!(Dict{Tuple{Type, Bool}, Any}, A, :isomorphisms)::Dict{Tuple{Type, Bool}, Any}
return get!(isos, (T, on_gens)) do
@assert T != PcGroup "There should be a special method for type PcGroup"
@assert T != FPGroup "There should be a special method for type FPGroup"
# find independent generators
if is_diagonal(rels(A))
exponents = diagonal(rels(A))
A2 = A
A2_to_A = identity_map(A)
else
exponents = elementary_divisors(A)
A2, A2_to_A = snf(A)
end
A_to_A2 = inv(A2_to_A)
# Create an isomorphic GAP group whose `GAPWrap.GeneratorsOfGroup`
# consists of independent elements of the orders in `exponents`.
G = abelian_group(T, exponents)
GapG = GapObj(G)
# `GAPWrap.GeneratorsOfGroup(GapG)` consists of independent elements
# of the orders in `exponents`.
# `GAPWrap.IndependentGeneratorsOfAbelianGroup(GapG)` chooses generators
# that may differ from these generators,
# and that belong to the exponent vectors returned by
# `GAPWrap.IndependentGeneratorExponents(GapG, g)`.
# `GAPWrap.GeneratorsOfGroup(GapG)` corresponds to `gens(A2)`,
# we let `hom` compute elements in `A2` that correspond to
# `GAP.Globals.IndependentGenerators(GapG)`.
Ggens = Vector{GapObj}(GAPWrap.GeneratorsOfGroup(GapG)::GapObj)
if length(Ggens) < length(exponents)
# It may happen that GAP omits the generators of order 1. Insert them.
@assert length(Ggens) + length(filter(is_one, exponents)) ==
length(exponents)
o = GapObj(one(G))
newGgens = Vector{GapObj}()
pos = 1
for i in 1:length(exponents)
if exponents[i] == 1
push!(newGgens, o)
else
push!(newGgens, Ggens[pos])
pos = pos+1
end
end
Ggens = newGgens
end
gensindep = GAPWrap.IndependentGeneratorsOfAbelianGroup(GapG)::GapObj
orders = [GAPWrap.Order(g) for g in gensindep]
exps = map(x -> x == GAP.Globals.infinity ? ZZRingElem(0) : ZZRingElem(x), orders)
Aindep = abelian_group(exps)
imgs = [Vector{ZZRingElem}(GAPWrap.IndependentGeneratorExponents(GapG, a)) for a in Ggens]
A2_to_Aindep = hom(A2, Aindep, elem_type(Aindep)[Aindep(e) for e in imgs])
Aindep_to_A = compose(inv(A2_to_Aindep), A2_to_A)
n = length(exponents)
f = function(a::elem_type(FinGenAbGroup))
exp = A_to_A2(a)
img = GAP.Globals.One(GapG)
for i in 1:n
img = img * Ggens[i]^GAP.Obj(exp[i])
end
return group_element(G, img)
end
finv = function(g)
exp = Vector{ZZRingElem}(GAPWrap.IndependentGeneratorExponents(GapG, GapObj(g)))
return Aindep_to_A(Aindep(exp))
end
return GroupIsomorphismFromFunc(A, G, f, finv)
end::GroupIsomorphismFromFunc{FinGenAbGroup, T}
end
################################################################################
#
# special methods for `isomorphism` to abelian `PcGroup` or `FPGroup`:
# computing images and preimages is easier in these cases,
# since we need not go via `GAP.Globals.IndependentGeneratorsOfAbelianGroup`
# and `GAPWrap.IndependentGeneratorExponents`
#
################################################################################
function isomorphism(::Type{PcGroup}, A::FinGenAbGroup; on_gens::Bool=false)
on_gens && error("only `on_gens = false` is supported for `isomorphism(::Type{PcGroup}, A::FinGenAbGroup)`")
# Known isomorphisms are cached in the attribute `:isomorphisms`.
isos = get_attribute!(Dict{Tuple{Type, Bool}, Any}, A, :isomorphisms)::Dict{Tuple{Type, Bool}, Any}
return get!(isos, (PcGroup, on_gens)) do
# find independent generators
# trivial diagonal entries can get removed by `GAPWrap.AbelianPcpGroup`,
# thus we cannot simply take the diagonal
exponents = elementary_divisors(A)
A2, A2_to_A = snf(A)
n = length(exponents)
A_to_A2 = inv(A2_to_A)
if 0 in exponents
# Create a `PcpGroup` in GAP.
# Its `GeneratorsOfGroup` are the generators of the defining
# presentations, they correspond to the generators of `A2`.
GapG = GAPWrap.AbelianPcpGroup(length(exponents), GapObj(exponents; recursive = true))::GapObj
C = GAPWrap.Collector(GapG)::GapObj
G = PcGroup(GapG)
f = function(a::FinGenAbGroupElem)
diag = A_to_A2(a)
exps = GapObj([diag[i] for i in 1:n], recursive = true)
return group_element(G, GAPWrap.PcpElementByExponentsNC(C, exps))
end
finv = g -> A2_to_A(A2(_exponent_vector(g)))
else
#TODO: As soon as https://github.com/gap-packages/polycyclic/issues/88 is fixed,
# we can change the code to create a `PcpGroup` in GAP also if the group
# is finite.
# Create a `PcGroup` in GAP.
# The generators of this group correspond to those of `A2`
# but they are in general only a subset of the defining pcgs.
# We implement the decomposition of an element into the generators
# via a matrix multiplication with the exponent vector of the element.
GapG = GAPWrap.AbelianGroup(GAP.Globals.IsPcGroup, GapObj(exponents; recursive = true))
Gpcgs = GAPWrap.FamilyPcgs(GapG)
G = PcGroup(GAPWrap.SubgroupNC(GapG, Gpcgs))
# Find the "intervals" in the pcgs that correspond to the generators.
indepgens = Vector{GapObj}(GAPWrap.GeneratorsOfGroup(GapG))
pcgs = Vector{GapObj}(Gpcgs)
m = length(pcgs)
relord = GAPWrap.RelativeOrders(Gpcgs)
starts = [findfirst(isequal(x), pcgs) for x in indepgens]
push!(starts, length(pcgs)+1)
M = zero_matrix(ZZ, m, n)
for j in 1:length(indepgens)
e = 1
for i in starts[j]:(starts[j+1]-1)
M[i, j] = e
e = e * relord[i]
end
end
starts = starts[1:n]
f = function(a::FinGenAbGroupElem)
diag = A_to_A2(a)
v = zeros(ZZ, m)
v[starts] = [diag[i] for i in 1:n]
exps = GapObj(v, recursive = true)
return group_element(G, GAPWrap.LinearCombinationPcgs(Gpcgs, GapObj(v, true)))
end
finv = g -> A2_to_A(A2(_exponent_vector(g) * M))
end
return GroupIsomorphismFromFunc(A, G, f, finv)
end::GroupIsomorphismFromFunc{FinGenAbGroup, PcGroup}
end
function isomorphism(::Type{FPGroup}, A::FinGenAbGroup; on_gens::Bool=false)
on_gens && error("only `on_gens = false` is supported for `isomorphism(::Type{FPGroup}, A::FinGenAbGroup)`")
# Known isomorphisms are cached in the attribute `:isomorphisms`.
isos = get_attribute!(Dict{Tuple{Type, Bool}, Any}, A, :isomorphisms)::Dict{Tuple{Type, Bool}, Any}
return get!(isos, (FPGroup, on_gens)) do
# Do not call `abelian_group(FPGroup, ...)`,
# because then we would need an indirection via a group with
# diagonal relations.
# Instead, we create a group with the same defining relations.
n = ngens(A)
G = free_group(n; eltype = :syllable)
R = rels(A)
gG = gens(G)
gGi = map(inv, gG)
s = vcat(elem_type(G)[gG[i]*gG[j]*gGi[i]*gGi[j] for i in 1:n for j in (i+1):n],
elem_type(G)[prod([gen(G, i)^R[j,i] for i=1:n if !iszero(R[j,i])], init = one(G)) for j=1:nrows(R)])
F, mF = quo(G, s)
set_is_abelian(F, true)
set_is_finite(F, is_finite(A))
is_finite(A) && set_order(F, order(A))
return MapFromFunc(
A, F,
y->F([i => y[i] for i=1:n]),
x->A(exponents_of_abelianization(x)))
end::MapFromFunc{FinGenAbGroup, FPGroup}
end
####
mutable struct GroupIsomorphismFromFunc{R, T} <: Map{R, T, Hecke.HeckeMap, MapFromFunc}
map::MapFromFunc{R, T}
end
function GroupIsomorphismFromFunc{R, T}(D::R, C::T, f, g) where {R, T}
return GroupIsomorphismFromFunc{R, T}(MapFromFunc(D, C, f, g))
end
function GroupIsomorphismFromFunc(D, C, f, g)
return GroupIsomorphismFromFunc{typeof(D), typeof(C)}(D, C, f, g)
end
# install the same methods as for `MapFromFunc`,
# see `Hecke.jl/src/Map/MapType.jl`
domain(f::GroupIsomorphismFromFunc) = domain(f.map)