-
Notifications
You must be signed in to change notification settings - Fork 59
/
GrpAbFinGen.jl
2276 lines (1974 loc) · 62.6 KB
/
GrpAbFinGen.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
################################################################################
#
# GrpAb/FinGenAbGroup.jl : Finitely generated abelian groups
#
# This file is part of Hecke.
#
# Copyright (c) 2015, 2016, 2017: Claus Fieker, Tommy Hofmann
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#
# Copyright (C) 2015, 2016, 2017 Tommy Hofmann, Claus Fieker
#
################################################################################
import Base.+, Nemo.snf, Nemo.parent, Base.rand, Nemo.is_snf
################################################################################
#
# Functions for some abstract interfaces
#
################################################################################
elem_type(::Type{FinGenAbGroup}) = FinGenAbGroupElem
parent_type(::Type{FinGenAbGroupElem}) = FinGenAbGroup
is_abelian(::FinGenAbGroup) = true
##############################################################################
#
# Constructors
#
##############################################################################
@doc raw"""
abelian_group(::Type{T} = FinGenAbGroup, M::ZZMatrix) -> FinGenAbGroup
Creates the abelian group with relation matrix `M`. That is, the group will
have `ncols(M)` generators and each row of `M` describes one relation.
"""
abelian_group(M::ZZMatrix) = abelian_group(FinGenAbGroup, M)
function abelian_group(::Type{FinGenAbGroup}, M::ZZMatrix)
if is_snf(M) && nrows(M) > 0 && ncols(M) > 0 && !isone(M[1, 1])
N = ZZRingElem[M[i, i] for i = 1:min(nrows(M), ncols(M))]
if ncols(M) > nrows(M)
N = vcat(N, ZZRingElem[0 for i = 1:ncols(M)-nrows(M)])
end
G = FinGenAbGroup(N)
else
G = FinGenAbGroup(M)
end
return G
end
@doc raw"""
abelian_group(::Type{T} = FinGenAbGroup, M::AbstractMatrix{<:IntegerUnion})
Creates the abelian group with relation matrix `M`. That is, the group will
have `ncols(M)` generators and each row of `M` describes one relation.
"""
function abelian_group(M::AbstractMatrix{<:IntegerUnion})
return abelian_group(FinGenAbGroup, M)
end
function abelian_group(::Type{FinGenAbGroup}, M::AbstractMatrix{<:IntegerUnion})
return abelian_group(matrix(FlintZZ, M))
end
function _issnf(N::Vector{T}) where T <: IntegerUnion
if isone(length(N)) && isone(N[1])
return false
end
for i = 1:length(N)-1
if isone(abs(N[i]))
return false
end
if iszero(N[i])
if !iszero(N[i+1])
return false
end
elseif !iszero(mod(N[i+1], N[i]))
return false
end
end
return true
end
@doc raw"""
abelian_group(::Type{T} = FinGenAbGroup, M::AbstractVector{<:IntegerUnion}) -> FinGenAbGroup
abelian_group(::Type{T} = FinGenAbGroup, M::IntegerUnion...) -> FinGenAbGroup
Creates the direct product of the cyclic groups $\mathbf{Z}/m_i$,
where $m_i$ is the $i$th entry of `M`.
"""
function abelian_group(M::AbstractVector{<:Union{Any, IntegerUnion}})
if eltype(M) === Any
_M = convert(Vector{ZZRingElem}, (ZZ.(M)))::Vector{ZZRingElem}
return abelian_group(FinGenAbGroup, _M)
else
return abelian_group(FinGenAbGroup, M)
end
end
function abelian_group(::Type{FinGenAbGroup}, M::AbstractVector{<:IntegerUnion})
if _issnf(M)
G = FinGenAbGroup(M)
else
N = zero_matrix(FlintZZ, length(M), length(M))
for i = 1:length(M)
N[i,i] = M[i]
end
G = FinGenAbGroup(N)
end
if !isempty(M)
res = lcm(M)
if !iszero(res)
G.exponent = res
end
end
return G
end
function abelian_group(M::IntegerUnion...)
return abelian_group(collect(M))
end
@doc raw"""
free_abelian_group(::Type{T} = FinGenAbGroup, n::Int) -> FinGenAbGroup
Creates the free abelian group of rank `n`.
"""
free_abelian_group(n::Int) = free_abelian_group(FinGenAbGroup, n)
function free_abelian_group(::Type{FinGenAbGroup}, n::Int)
return abelian_group(FinGenAbGroup, zeros(Int, n))
end
################################################################################
#
# String I/O
#
################################################################################
function show(io::IO, A::FinGenAbGroup)
@show_name(io, A)
@show_special(io, A)
if is_snf(A)
show_snf(io, A)
else
show_gen(io, A)
end
end
function show(io::IO, ::MIME"text/plain", A::FinGenAbGroup)
@show_name(io, A)
@show_special(io, MIME"text/plain"(), A)
if is_snf(A)
show_snf(io, MIME"text/plain"(), A)
else
show_gen(io, MIME"text/plain"(), A)
end
end
function show_hom(io::IO, G::FinGenAbGroup)
D = get_attribute(G, :hom)
D === nothing && error("only for hom")
io = pretty(io)
if is_terse(io)
print(io, LowercaseOff(), "Hom of abelian groups")
else
print(io, LowercaseOff(), "Hom(")
print(terse(io), D[1])
print(io, ", ")
print(terse(io), D[2])
print(io, ")")
end
end
show_hom(io::IO, ::MIME"text/plain", G::FinGenAbGroup) = show_hom(io, G)
function show_direct_product(io::IO, G::FinGenAbGroup)
D = get_attribute(G, :direct_product)
D === nothing && error("only for direct products")
if is_terse(io)
print(io, "Direct product of abelian groups")
else
print(io, "Direct product of ", ItemQuantity(length(D), "abelian group"))
end
end
function show_direct_product(io::IO, ::MIME"text/plain", G::FinGenAbGroup)
D = get_attribute(G, :direct_product)
D === nothing && error("only for direct products")
io = pretty(io)
print(io, "Direct product of")
for G in D
print(io, "\n", Indent(), G, Dedent())
end
end
function show_direct_sum(io::IO, G::FinGenAbGroup)
D = get_attribute(G, :direct_product)
D === nothing && error("only for direct sums")
if is_terse(io)
print(io, "Direct sum of abelian groups")
else
print(io, "Direct sum of ", ItemQuantity(length(D), "abelian group"))
end
end
function show_direct_sum(io::IO, ::MIME"text/plain", G::FinGenAbGroup)
D = get_attribute(G, :direct_product)
D === nothing && error("only for direct sums")
io = pretty(io)
print(io, "Direct sum of")
for G in D
print(io, "\n", Indent(), G, Dedent())
end
end
function show_tensor_product(io::IO, G::FinGenAbGroup)
D = get_attribute(G, :tensor_product)
D === nothing && error("only for tensor products")
if is_terse(io)
print(io, "Tensor product of abelian groups")
else
print(io, "Tensor product of ", ItemQuantity(length(D), "abelian group"))
end
end
function show_tensor_product(io::IO, ::MIME"text/plain", G)#::FinGenAbGroup)
D = get_attribute(G, :tensor_product)
D === nothing && error("only for tensor products")
io = pretty(io)
print(io, "Tensor product of")
for G in D
print(io, "\n", Indent(), G, Dedent())
end
end
function show_gen(io::IO, ::MIME"text/plain", A::FinGenAbGroup)
io = pretty(io)
println(io, "Finitely generated abelian group")
if isdefined(A, :snf_map)
println(io, Indent(), "isomorphic to ", domain(A.snf_map), Dedent())
end
println(io, Indent(), "with ", ItemQuantity(ncols(rels(A)), "generator"), " and ", ItemQuantity(nrows(rels(A)), "relation"), " and relation matrix")
show(io, MIME"text/plain"(), rels(A))
print(io, Dedent())
end
function show_gen(io::IO, A::FinGenAbGroup)
if is_terse(io)
print(io, "Finitely generated abelian group")
else
print(io, "Finitely generated abelian group with ", ItemQuantity(ncols(rels(A)), "generator"), " and ", ItemQuantity(nrows(rels(A)), "relation"))
end
end
show_snf(io::IO, ::MIME"text/plain", A::FinGenAbGroup) = show_snf_structure(io, A)
show_snf(io::IO, A::FinGenAbGroup) = show_snf_structure(io, A)
function show_snf_structure(io::IO, A::FinGenAbGroup, mul = "x")
@assert is_snf(A)
len = length(A.snf)
io = pretty(io)
if len == 0
print(io, LowercaseOff(), "Z/1")
return
end
if A.snf[1] == 0
if len == 1
print(io, LowercaseOff(), "Z")
else
print(io, LowercaseOff(), "Z^$(len)")
end
return
end
i = 1
while i <= len
inv = A.snf[i]
j = 1
while i + j <= len && inv == A.snf[i + j]
j += 1
end
if iszero(inv)
print(io, LowercaseOff(), "Z")
if j > 1
print(io, "^($j)")
end
else
if j > 1
print(io, "(Z/$(inv))^$(j)")
else
print(io, LowercaseOff(), "Z/$(inv)")
end
end
if i + j - 1 < len
print(io, " $mul ")
end
i += j
end
end
################################################################################
#
# Hash function
#
################################################################################
# We use the default hash, since we use === as == for abelian groups
################################################################################
#
# Field access
#
################################################################################
@doc raw"""
is_snf(G::FinGenAbGroup) -> Bool
Return whether the current relation matrix of the group $G$ is in Smith
normal form.
"""
is_snf(A::FinGenAbGroup) = A.is_snf
@doc raw"""
number_of_generators(G::FinGenAbGroup) -> Int
Return the number of generators of $G$ in the current representation.
"""
function number_of_generators(A::FinGenAbGroup)
if is_snf(A)
return length(A.snf)
else
return ncols(A.rels)
end
end
@doc raw"""
number_of_relations(G::FinGenAbGroup) -> Int
Return the number of relations of $G$ in the current representation.
"""
function number_of_relations(A::FinGenAbGroup)
if is_snf(A)
return length(A.snf)
else
return nrows(A.rels)
end
end
@doc raw"""
rels(A::FinGenAbGroup) -> ZZMatrix
Return the currently used relations of $G$ as a single matrix.
"""
function rels(A::FinGenAbGroup)
if is_snf(A)
return rels_snf(A)
else
return rels_gen(A)
end
end
function rels_gen(A::FinGenAbGroup)
return A.rels
end
function rels_snf(A::FinGenAbGroup)
M = zero_matrix(FlintZZ, ngens(A), ngens(A))
for i = 1:ngens(A)
M[i,i] = A.snf[i]
end
return M
end
################################################################################
#
# Hermite normal form
#
################################################################################
function assure_has_hnf(A::FinGenAbGroup)
if isdefined(A, :hnf)
return nothing
end
R = rels(A)
if isdefined(A, :exponent) && nrows(R) >= ncols(R)
A.hnf = hnf_modular_eldiv(R, A.exponent)
else
A.hnf = hnf(R)
end
i = nrows(A.hnf)
while i>0 && is_zero_row(A.hnf, i)
i -= 1
end
if i < nrows(A.hnf)
A.hnf = A.hnf[1:i, :]
end
return nothing
end
################################################################################
#
# Smith normal form
#
################################################################################
@doc raw"""
snf(A::FinGenAbGroup) -> FinGenAbGroup, FinGenAbGroupHom
Return a pair $(G, f)$, where $G$ is an abelian group in canonical Smith
normal form isomorphic to $A$ and an isomorphism $f : G \to A$.
"""
function snf(G::FinGenAbGroup)
if isdefined(G, :snf_map)
return domain(G.snf_map)::FinGenAbGroup, G.snf_map::FinGenAbGroupHom
end
if is_snf(G)
G.snf_map = FinGenAbGroupHom(G) # identity
return G, G.snf_map::FinGenAbGroupHom
end
if isdefined(G, :exponent)
if isdefined(G, :hnf)
S, T = snf_for_groups(G.hnf, G.exponent)
else
S, T = snf_for_groups(G.rels, G.exponent)
end
else
S, _, T = snf_with_transform(G.rels, false, true)
end
m = min(nrows(S), ncols(S))
if m > 0 && nrows(S) >= ncols(S)
e = S[m, m]
if e > 1
if fits(Int, e) && is_prime(e)
F = Native.GF(Int(e), cached = false)
TF = map_entries(F, T)
iT = lift(inv(TF))
else
iT = invmod(T, e)
end
else
iT = inv(T)
end
else
iT = inv(T)
end
return _reduce_snf(G, S, T, iT)
end
# For S in SNF with G.rels = U*S*T and Ti = inv(T) this removes
# the ones at the diagonal of S and constructs the homomorphism.
function _reduce_snf(G::FinGenAbGroup, S::ZZMatrix, T::ZZMatrix, Ti::ZZMatrix)
d = ZZRingElem[S[i,i] for i = 1:min(nrows(S), ncols(S))]
while length(d) < ngens(G)
push!(d, 0)
end
pos = Int[i for i = 1:length(d) if !isone(d[i])]
r = Int[i for i = 1:nrows(T)]
s = ZZRingElem[ d[i] for i in pos]
TT = sub(T, r, pos)
TTi = sub(Ti, pos, r)
H = FinGenAbGroup(s)
if !isempty(s) && !iszero(s[end])
H.exponent = s[end]
G.exponent = s[end]
end
mp = hom(H, G, TTi, TT, check = false)
G.snf_map = mp
return H, mp::FinGenAbGroupHom
end
################################################################################
#
# Finiteness
#
################################################################################
@doc raw"""
isfinite(A::FinGenAbGroup) -> Bool
Return whether $A$ is finite.
"""
isfinite(A::FinGenAbGroup) = all(!iszero, elementary_divisors(A))
################################################################################
#
# Rank
#
################################################################################
@doc raw"""
torsion_free_rank(A::FinGenAbGroup) -> Int
Return the torsion free rank of $A$, that is, the dimension of the
$\mathbf{Q}$-vectorspace $A \otimes_{\mathbf Z} \mathbf Q$.
See also [`rank`](@ref).
"""
function torsion_free_rank(A::FinGenAbGroup)
if !is_snf(A)
A = snf(A)[1]
end
return length(findall(iszero, A.snf))
end
# return the p-rank of A, which is the dimension of the elementary abelian
# group A/pA, or in other words, the rank (=size of minimal generating set) of
# the maximal p-quotient of A
function rank(A::FinGenAbGroup, p::Union{Int, ZZRingElem})
if !is_snf(A)
A = snf(A)[1]
end
if isempty(A.snf)
return 0
end
if !iszero(mod(A.snf[end], p))
return 0
end
i = findfirst(x -> iszero(mod(x, p)), A.snf)
return length(A.snf)-i+1
end
@doc raw"""
rank(A::FinGenAbGroup) -> Int
Return the rank of $A$, that is, the size of a minimal generating set for $A$.
See also [`torsion_free_rank`](@ref).
"""
rank(A::FinGenAbGroup) = error("rank(::FinGenAbGroup) has been renamed to torsion_free_rank")
#rank(A::FinGenAbGroup) = is_snf(A) ? length(A.snf) : return rank(snf(A)[1])
################################################################################
#
# Order
#
################################################################################
@doc raw"""
order(A::FinGenAbGroup) -> ZZRingElem
Return the order of $A$. It is assumed that $A$ is finite.
"""
function order(A::FinGenAbGroup)
is_infinite(A) && error("Group must be finite")
return prod(elementary_divisors(A))
end
################################################################################
#
# Exponent
#
################################################################################
@doc raw"""
exponent(A::FinGenAbGroup) -> ZZRingElem
Return the exponent of $A$. It is assumed that $A$ is finite.
"""
function exponent(A::FinGenAbGroup)
is_infinite(A) && error("Group must be finite")
s = elementary_divisors(A)
length(s)==0 && return ZZ(1)
return s[end]
end
################################################################################
#
# Trivial
#
################################################################################
@doc raw"""
is_trivial(A::FinGenAbGroup) -> Bool
Return whether $A$ is the trivial group.
"""
is_trivial(A::FinGenAbGroup) = isfinite(A) && isone(order(A))
################################################################################
#
# Isomorphism
#
################################################################################
@doc raw"""
is_isomorphic(G::FinGenAbGroup, H::FinGenAbGroup) -> Bool
Return whether $G$ and $H$ are isomorphic.
"""
function is_isomorphic(G::FinGenAbGroup, H::FinGenAbGroup)
return elementary_divisors(G) == elementary_divisors(H)
end
################################################################################
#
# Direct products/direct sums/biproducts
#
################################################################################
#TODO: check the universal properties here!!!
@doc raw"""
direct_sum(G::FinGenAbGroup...) -> FinGenAbGroup, Vector{FinGenAbGroupHom}
Return the direct sum $D$ of the (finitely many) abelian groups $G_i$, together
with the injections $G_i \to D$.
For finite abelian groups, finite direct sums and finite direct products agree and
they are therefore called biproducts.
If one wants to obtain $D$ as a direct product together with the projections
$D \to G_i$, one should call `direct_product(G...)`.
If one wants to obtain $D$ as a biproduct together with the projections and the
injections, one should call `biproduct(G...)`.
Otherwise, one could also call `canonical_injections(D)` or `canonical_projections(D)`
later on.
"""
function direct_sum(G::FinGenAbGroup...; task::Symbol = :sum, kwargs...)
@assert task in [:sum, :prod, :both, :none]
return _direct_product(:sum, G...; task = task, kwargs...)
end
@doc raw"""
direct_product(G::FinGenAbGroup...) -> FinGenAbGroup, Vector{FinGenAbGroupHom}
Return the direct product $D$ of the (finitely many) abelian groups $G_i$, together
with the projections $D \to G_i$.
For finite abelian groups, finite direct sums and finite direct products agree and
they are therefore called biproducts.
If one wants to obtain $D$ as a direct sum together with the injections $D \to G_i$,
one should call `direct_sum(G...)`.
If one wants to obtain $D$ as a biproduct together with the projections and the
injections, one should call `biproduct(G...)`.
Otherwise, one could also call `canonical_injections(D)` or `canonical_projections(D)`
later on.
"""
function direct_product(G::FinGenAbGroup...; task::Symbol = :prod, kwargs...)
@assert task in [:prod, :sum, :both, :none]
return _direct_product(:prod, G...; task = task, kwargs...)
end
@doc raw"""
biproduct(G::FinGenAbGroup...) -> FinGenAbGroup, Vector{FinGenAbGroupHom}, Vector{FinGenAbGroupHom}
Return the direct product $D$ of the (finitely many) abelian groups $G_i$, together
with the projections $D \to G_i$ and the injections $G_i \to D$.
For finite abelian groups, finite direct sums and finite direct products agree and
they are therefore called biproducts.
If one wants to obtain $D$ as a direct sum together with the injections $G_i \to D$,
one should call `direct_sum(G...)`.
If one wants to obtain $D$ as a direct product together with the projections $D \to G_i$,
one should call `direct_product(G...)`.
Otherwise, one could also call `canonical_injections(D)` or `canonical_projections(D)`
later on.
"""
function biproduct(G::FinGenAbGroup...; task::Symbol = :both, kwargs...)
@assert task in [:prod, :sum, :both, :none]
return _direct_product(:prod, G...; task = task, kwargs...)
end
@doc raw"""
⊕(A::FinGenAbGroup...) -> FinGenAbGroup
Return the direct sum $D$ of the (finitely many) abelian groups $G_i$.
If one wants to access the injections $G_i \to D$ or the projections $D \to G_i$ later,
one can call respectively `canonical_injections(D)` or `canonical_projections(D)`.
"""
function _direct_product(t::Symbol, G::FinGenAbGroup...
; add_to_lattice::Bool = false, L::GrpAbLattice = GroupLattice, task::Symbol = :sum)
@assert task in [:prod, :sum, :both, :none]
Dp = abelian_group(cat([rels(x) for x = G]..., dims = (1,2)))
for x = G
assure_has_hnf(x)
end
#works iff hnf is stripping the zero rows
Dp.hnf = cat([x.hnf for x = G]..., dims = (1,2))
if t === :prod
set_attribute!(Dp, :direct_product =>G, :show => show_direct_product)
elseif t === :sum
set_attribute!(Dp, :direct_product =>G, :show => show_direct_sum)
else
error("illegal symbol passed in")
end
inj = FinGenAbGroupHom[]
pro = FinGenAbGroupHom[]
jj = 0
for j=1:length(G)
g = G[j]
if task in [:sum, :both]
# m = hom(g, Dp, FinGenAbGroupElem[Dp[j+i] for i = 1:ngens(g)], check = false)
# should just be 0...Id 0... 0
x = zero_matrix(ZZ, ngens(g), ngens(Dp))
for j = 1:ngens(g)
x[j, jj+j] = 1
end
m = hom(g, Dp, x, check = false)
add_to_lattice && append!(L, m)
push!(inj, m)
end
if task in [:prod, :both]
# m = hom(Dp, g, vcat(FinGenAbGroupElem[g[0] for i = 1:j], gens(g), FinGenAbGroupElem[g[0] for i=j+ngens(g)+1:ngens(Dp)]), check = false)
x = zero_matrix(ZZ, ngens(Dp), ngens(g))
for j = 1:ngens(g)
x[jj+j, j] = 1
end
m = hom(Dp, g, x, check = false)
add_to_lattice && append!(L, m)
push!(pro, m)
end
jj += ngens(g)
end
if task == :none
return Dp
elseif task == :prod
return Dp, pro
elseif task == :sum
return Dp, inj
else
return Dp, pro, inj
end
end
⊕(A::FinGenAbGroup...) = direct_sum(A..., task = :none)
#TODO: use matrices as above - or design special maps that are not tied
# to matrices but operate directly.
@doc raw"""
canonical_injections(G::FinGenAbGroup) -> Vector{FinGenAbGroupHom}
Given a group $G$ that was created as a direct product, return the
injections from all components.
"""
function canonical_injections(G::FinGenAbGroup)
D = get_attribute(G, :direct_product)
D === nothing && error("1st argument must be a direct product")
return [canonical_injection(G, i) for i=1:length(D)]
end
@doc raw"""
canonical_injection(G::FinGenAbGroup, i::Int) -> FinGenAbGroupHom
Given a group $G$ that was created as a direct product, return the
injection from the $i$th component.
"""
function canonical_injection(G::FinGenAbGroup, i::Int)
D = get_attribute(G, :direct_product)
D === nothing && error("1st argument must be a direct product")
s = sum([ngens(D[j]) for j = 1:i-1], init = 0)
h = hom(D[i], G, [G[s+j] for j = 1:ngens(D[i])])
return h
end
@doc raw"""
canonical_projections(G::FinGenAbGroup) -> Vector{FinGenAbGroupHom}
Given a group $G$ that was created as a direct product, return the
projections onto all components.
"""
function canonical_projections(G::FinGenAbGroup)
D = get_attribute(G, :direct_product)
D === nothing && error("1st argument must be a direct product")
return [canonical_projection(G, i) for i=1:length(D)]
end
@doc raw"""
canonical_projection(G::FinGenAbGroup, i::Int) -> FinGenAbGroupHom
Given a group $G$ that was created as a direct product, return the
projection onto the $i$th component.
"""
function canonical_projection(G::FinGenAbGroup, i::Int)
D = get_attribute(G, :direct_product)
D === nothing && error("1st argument must be a direct product")
H = D[i]
h = hom(G, H, vcat( [FinGenAbGroupElem[H[0] for j = 1:ngens(D[h])] for h = 1:i-1]...,
gens(H),
[FinGenAbGroupElem[H[0] for j = 1:ngens(D[h])] for h = i+1:length(D)]...))
return h
end
function matrix(M::Map{FinGenAbGroup, FinGenAbGroup})
if typeof(M) == FinGenAbGroupHom
return M.map
end
G = domain(M)
return reduce(vcat, [M(g).coeff for g = gens(G)])
end
function matrix(M::Generic.IdentityMap{FinGenAbGroup})
return identity_matrix(FlintZZ, ngens(domain(M)))
end
@doc raw"""
hom_direct_sum(G::FinGenAbGroup, H::FinGenAbGroup, A::Matrix{ <: Map{FinGenAbGroup, FinGenAbGroup}}) -> Map
Given groups $G$ and $H$ that are created as direct products as well
as a matrix $A$ containing maps $A[i,j] : G_i \to H_j$, return
the induced homomorphism.
"""
function hom_direct_sum(G::FinGenAbGroup, H::FinGenAbGroup, A::Matrix{ <: Map{FinGenAbGroup, FinGenAbGroup}})
r, c = size(A)
if c == 1
dG = [G]
else
dG = get_attribute(G, :direct_product)
end
if r == 1
dH = [H]
else
dH = get_attribute(H, :direct_product)
end
if dG === nothing || dH === nothing
error("both groups need to be direct products")
end
@assert all(i -> domain(A[i[1], i[2]]) == dG[i[1]] && codomain(A[i[1], i[2]]) == dH[i[2]], Base.Iterators.ProductIterator((1:r, 1:c)))
h = hom(G, H, reduce(vcat, [reduce(hcat, [matrix(A[i,j]) for j=1:c]) for i=1:r]))
return h
end
"""
hom_direct_sum(G::FinGenAbGroup, H::FinGenAbGroup, V::Vector{<:Map{FinGenAbGroup, FinGenAbGroup}})
For groups `G = prod G_i` and `H = prod H_i` as well as maps `V_i: G_i -> H_i`,
build the induced map from `G -> H`.
"""
function hom_direct_sum(G::FinGenAbGroup, H::FinGenAbGroup, V::Vector{<:Map{FinGenAbGroup, FinGenAbGroup}})
dG = get_attribute(G, :direct_product)
dH = get_attribute(H, :direct_product)
if dG === nothing || dH === nothing
error("both groups need to be direct products")
end
@assert length(V) == length(dG) == length(dH)
@assert all(i -> domain(V[i]) == dG[i] && codomain(V[i]) == dH[i], 1:length(V))
h = hom(G, H, cat([matrix(V[i]) for i=1:length(V)]..., dims=(1,2)), check = !true)
return h
end
function _flat(G::FinGenAbGroup)
s = get_attribute(G, :direct_product)
if s === nothing
return [G]
end
return reduce(vcat, [_flat(x) for x = s])
end
function _tensor_flat(G::FinGenAbGroup)
s = get_attribute(G, :tensor_product)
if s === nothing
return [G]
end
return reduce(vcat, [_tensor_flat(x) for x = s])
end
@doc raw"""
flat(G::FinGenAbGroup) -> FinGenAbGroupHom
Given a group $G$ that is created using (iterated) direct products, or
(iterated) tensor products, return a group isomorphism into a flat product:
for $G := (A \oplus B) \oplus C$, it returns the isomorphism
$G \to A \oplus B \oplus C$ (resp. $\otimes$).
"""
function flat(G::FinGenAbGroup)
s = get_attribute(G, :direct_product)
if get_attribute(G, :direct_product) !== nothing
H = direct_product(_flat(G)..., task = :none)
elseif get_attribute(G, :tensor_product) !== nothing
H = tensor_product(_tensor_flat(G)..., task = :none)
else
H = G
end
return hom(G, H, identity_matrix(FlintZZ, ngens(G)), identity_matrix(FlintZZ, ngens(G)))
end
################################################################################
#Tensor product
################################################################################
function tensor_product2(G::FinGenAbGroup, H::FinGenAbGroup)
RG = rels(G)
RH = rels(H)
R = vcat(transpose(kronecker_product(transpose(RG), identity_matrix(FlintZZ, ngens(H)))),
transpose(kronecker_product(identity_matrix(FlintZZ, ngens(G)), transpose(RH))))
G = abelian_group(R)
end
struct TupleParent{T <: Tuple}
function TupleParent(t::T) where {T}
return new{T}()
end
end
function show(io::IO, P::TupleParent{T}) where {T}
print(io, "parent of tuples of type $T")
end
elem_type(::Type{TupleParent{T}}) where {T} = T
parent(t::Tuple) = TupleParent(t)
@doc raw"""
tensor_product(G::FinGenAbGroup...; task::Symbol = :map) -> FinGenAbGroup, Map
Given groups $G_i$, compute the tensor product $G_1\otimes \cdots \otimes G_n$.
If `task` is set to ":map", a map $\phi$ is returned that
maps tuples in $G_1 \times \cdots \times G_n$ to pure tensors
$g_1 \otimes \cdots \otimes g_n$. The map admits a preimage as well.
"""
function tensor_product(G::FinGenAbGroup...; task::Symbol = :map)
@assert task in [:map, :none]
local T
if length(G) == 1
T = G[1]
else
T = tensor_product2(G[2], G[1])
for i = 3:length(G)
T = tensor_product2(G[i], T)
end
end
set_attribute!(T, :tensor_product => G, :show => show_tensor_product)
if task == :none
return T
end
g = vec(collect(Base.Iterators.ProductIterator(Tuple(gens(g) for g = reverse(G)))))
function pure(g::FinGenAbGroupElem...)
@assert length(g) == length(G)
@assert all(i-> parent(g[i]) == G[i], 1:length(G))
return T(vec(collect(prod(x) for x = Base.Iterators.product([h.coeff for h = reverse(g)]...))))
end
function pure(T::Tuple)
return pure(T...)
end
function inv_pure(t::FinGenAbGroupElem)
p = Base.findall(i -> !iszero(t[i]), 1:ngens(T))
if length(p) == 0
return Tuple(collect(g[0] for g = G))
end
@assert length(p) == 1
@assert t[p[1]] == 1
return reverse(g[p[1]])
end
return T, MapFromFunc(TupleParent(Tuple([g[0] for g = G])), T, pure, inv_pure)
end
⊗(G::FinGenAbGroup...) = tensor_product(G..., task = :none)