-
Notifications
You must be signed in to change notification settings - Fork 113
/
Cohomology.jl
1700 lines (1504 loc) · 46.3 KB
/
Cohomology.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
module GrpCoh
using Oscar
import Oscar:action
import AbstractAlgebra: Group, Module
import Base: parent
######################################################################
#
# to allow additive motation for multiplicative objetcs,
# eg
# M = MultGrp(K::AnticNumberField) will result in
# M(a) + M(b) = M(ab)
#
# since the Gmodule stuff is strictly additive.
#
struct MultGrp{T} <: Oscar.Hecke.GrpAb
data::Any #should be like parent_type{T}
elem_rep::T
function MultGrp(L::S) where {S}
return MultGrp(L, one(L))
end
function MultGrp(L, a::typ) where typ
return new{typ}(L, a)
end
end
function Base.show(io::IO, M::MultGrp)
println(io, "multiplicative group of $(M.data)")
end
struct MultGrpElem{T} <: Oscar.Hecke.GrpAbElem
data::T
parent::MultGrp{T}
end
function Base.show(io::IO, m::MultGrpElem)
print(io, "$(m.data)")
end
(M::MultGrp{T})(a::T) where {T} = MultGrpElem{T}(a, M)
Oscar.parent(a::MultGrpElem) = a.parent
Oscar.elem_type(a::MultGrp{T}) where T = MultGrpElem{T}
import Base: ==, +, -, *
*(a::Integer, b::MultGrpElem{T}) where T = MultGrpElem{T}(b.data^a, parent(b))
*(a::fmpz, b::MultGrpElem{T}) where T = MultGrpElem{T}(b.data^a, parent(b))
+(a::MultGrpElem{T}, b::MultGrpElem{T}) where T = MultGrpElem{T}(a.data*b.data, parent(a))
-(a::MultGrpElem{T}, b::MultGrpElem{T}) where T = MultGrpElem{T}(a.data//b.data, parent(a))
-(a::MultGrpElem{T}) where T = MultGrpElem{T}(inv(a.data), parent(a))
==(a::MultGrpElem{T}, b::MultGrpElem{T}) where T = a.data == b.data
Base.hash(a::MultGrpElem, u::UInt = UInt(1235)) = hash(a.data. u)
##############################################################
#
# basic G-Modules:
#
# a fin. gen group G acting on some module M
# the action is given via maps (should be automorphisms of M)
#
# very little assumptions in general.
#
@attributes mutable struct GModule{gT,mT}
G::gT
M::mT
ac::Vector{Map} # automorphisms of M, one for each generator of G
function GModule(M, G::T, ac::Vector{<:Map}) where {T <: Oscar.GAPGroup}
r = new{T,typeof(M)}()
r.G = G
r.ac = ac
r.M = M
@assert all(x -> domain(x) == codomain(x) == r.M, ac)
return r
end
function GModule(G::T, ac::Vector{<:Map}) where {T <: Oscar.GAPGroup}
return GModule(domain(ac[1]), G, ac)
end
F::Group # G as an Fp-group (if set)
mF::GAPGroupHomomorphism # F -> G, maps F[i] to G[i]
iac::Vector{Map} # the inverses of ac
end
function Base.show(io::IO, C::GModule)
print(io, C.G, " acting on ", C.M, "\nvia: ", C.ac)
end
"""
Checks if the action maps satisfy the same relations
as the generators of `G`.
"""
function is_consistent(M::GModule)
G, mG = fp_group(M)
V = Module(M)
R = relators(G)
for r = R
w = word(r)
a = action(M, mG(w[1]< 0 ? inv(gen(G, -w[1])) : gen(G, w[1])))
for i=2:length(w)
a = a* action(M, mG(w[i]< 0 ? inv(gen(G, -w[i])) : gen(G, w[i])))
end
all(x->a(x) == x, gens(V)) || return false
end
return true
end
###########################################################
#
# Supporting group theory
# To be moved and revised eventually
###########################################################
"""
Compute an fp-presentation of the group generated by 'g'
and returns both the group and the map from the new group to the
parent of the generators.
"""
function fp_group(g::Vector{<:Oscar.GAPGroupElem})
G = parent(g[1])
@assert all(x->parent(x) == G, g)
X = GAP.Globals.IsomorphismFpGroupByGenerators(G.X, GAP.Globals.GeneratorsOfGroup(G.X))
F = FPGroup(GAP.Globals.Range(X))
return F, GAPGroupHomomorphism(F, G, GAP.Globals.InverseGeneralMapping(X))
end
"""
For an element of an fp-group, return a corresponding word as a sequence
of integers. A positive integers indicates the corresponding generator,
a negative one the inverse.
"""
function word(y::FPGroupElem)
z = GAP.Globals.UnderlyingElement(y.X)
return map(Int, GAP.Globals.LetterRepAssocWord(z))
end
"""
The relations defining 'F' as an array of pairs.
"""
function Oscar.relations(F::FPGroup)
R = relators(F)
z = one(free_group(F))
return [(x, z) for x = R]
end
function Oscar.relations(G::Oscar.GAPGroup)
f = GAP.Globals.IsomorphismFpGroupByGenerators(G.X, GAP.Globals.GeneratorsOfGroup(G.X))
f !=GAP.Globals.fail || throw(ArgumentError("Could not convert group into a group of type FPGroup"))
H = FPGroup(GAPWrap.Image(f))
return relations(H)
end
function Oscar.relations(G::PcGroup)
f = GAP.Globals.IsomorphismFpGroupByPcgs(GAP.Globals.FamilyPcgs(G.X), GAP.julia_to_gap("g"))
f !=GAP.Globals.fail || throw(ArgumentError("Could not convert group into a group of type FPGroup"))
H = FPGroup(GAPWrap.Image(f))
return relations(H)
end
##########################################################
#
# Basics for gmodules
#
# access and action
#
AbstractAlgebra.Group(C::GModule) = C.G
AbstractAlgebra.Module(C::GModule) = C.M
action(C::GModule) = C.ac
function inv_action(C::GModule)
if !isdefined(C, :iac)
C.iac = map(inv, C.ac)
end
return C.iac
end
function fp_group(C::GModule)
#TODO: better for PcGroup!!!
if !isdefined(C, :F)
if order(Group(C)) == 1
C.F = free_group(0)
C.mF = hom(C.F, Group(C), gens(C.F), elem_type(Group(C))[])
else
C.F, C.mF = fp_group(gens(Group(C)))
end
end
return C.F, C.mF
end
#TODO? have a GModuleElem and action via ^?
"""
For an array of objects in the module, compute the image under the
action of `g`, ie. an array where each entry is mapped.
"""
function action(C::GModule, g, v::Array)
@assert parent(g) == Group(C)
F, mF = fp_group(C)
ac = action(C)
iac = inv_action(C)
for i = word(preimage(mF, g))
if i > 0
v = map(ac[i], v)
else
v = map(iac[-i], v)
end
end
return v
end
"""
The image of `v` under `g`
"""
function action(C::GModule, g, v)
return action(C, g, [v])[1]
end
"""
The operation of `g` on the module as an automorphism.
"""
function action(C::GModule, g)
v = gens(Module(C))
return hom(Module(C), Module(C), action(C, g, v))
end
#Main goal: cohomology computations.
# So "empty" structure for parent of co-chains and a co-chain.
# currently co-chains are dumb: the values need to all be known
# on creation. They should support lazy filling.
# Also possibly they should be "modules", ie. inherit addition
# and scalar multiplication (and possibly? the G-operation)
#
struct AllCoChains{N, G, M} #Int (dim), Group(elem), Module(elem)
end
struct CoChain{N, G, M}
C::GModule
d::Dict{NTuple{N, G}, M}
end
function Base.show(io::IO, C::CoChain{N}) where {N}
print(io, "$N-cochain with values in ", C.C.M)
end
Oscar.Nemo.elem_type(::AllCoChains{N,G,M}) where {N,G,M} = CoChain{N,G,M}
Oscar.Nemo.elem_type(::Type{AllCoChains{N,G,M}}) where {N,G,M} = CoChain{N,G,M}
Oscar.Nemo.parent_type(::CoChain{N,G,M}) where {N,G,M}= AllCoChains{N,G,M}
"""
Evaluate a 0-cochain
"""
(C::CoChain{0})() = first(values(C.d))
#TODO: should this rather be a map from a 1-tuple of group elements?
"""
Evaluate a 1-cochain, a 1-cochain is a map from the group into the
module
"""
function (C::CoChain{1})(g::Oscar.BasicGAPGroupElem)
if haskey(C.d, (g,))
return C.d[(g,)]
end
F, mF = fp_group(C.C)
G = parent(g)
@assert G == group(C.C)
@assert ngens(F) == ngens(G)
@assert all(x->mF(gen(F, i)) == gen(G, i), 1:ngens(G))
w = word(preimage(mF, g))
t = zero(Module(C.C))
ac = action(C.C)
iac = inv_action(C.C)
G = Group(C.C)
#TODO: build up the group element step by step
# and store the values: (use Dimino code in Hecke)
#XXX: this is wrong!, compare to the H_one code below
# problem is that F and G might have different gens
# needs different Gap code: write g as a word in the
# generators of G and use this.
# also inverses are more complicated.
for i = w
if i > 0
t = ac[i](t)+C.d[(gen(G, i),)]
else
t = iac[-i](t-C.d[(gen(G, -i),)])
end
end
C.d[(g,)] = t
return t
end
(C::CoChain{1})(g::NTuple{1, <:Oscar.BasicGAPGroupElem}) = C(g[1])
#should support lazy via call-back.
"""
Evaluate a 2-cochain, a 2-cochain is a map from pairs of group elements
into the module
"""
function (C::CoChain{2})(g::Oscar.BasicGAPGroupElem, h::Oscar.BasicGAPGroupElem)
if haskey(C.d, (g,h))
return C.d[(g,h)]
end
end
(C::CoChain{2})(g::NTuple{2, <:Oscar.BasicGAPGroupElem}) = C(g[1], g[2])
"""
H^0(G, M)
Returns a module (same type as M) that abstractly represent
the 0-cohomology group as well as a map realizing this via explicit
co-chains
"""
function H_zero(C::GModule)
z = get_attribute(C, :H_zero)
if z !== nothing
return domain(z), z
end
G = Group(C)
M = Module(C)
id = hom(M, M, gens(M))
ac = action(C)
k = kernel(id - ac[1])[1]
for i=2:length(ac)
k = intersect(k, kernel(id - ac[i])[1])
end
z = MapFromFunc(x->CoChain{0,elem_type(G),elem_type(M)}(C, Dict(() => x)), y->y(), k, AllCoChains{0,elem_type(G),elem_type(M)}())
set_attribute!(C, :H_zero => z)
return k, z
end
#= TODO
- break out coboundaries and cochains
- depending on the module type:
- intersect yields an embedding (Z-module) or not GrpAb
- make sure that image/ kernel are consistent
- preimage
- issubset yields (for GrpAb) only true/ false, not the map
- is_subgroup has the "wrong" order of arguments (and cannot apply
to modules)
- quo does ONLY work if B is a direct submodule of A (Z-modules)
- mat or matrix is used to get "the matrix" from a hom
- zero_hom/ zero_obj/ identity_hom is missing
- Janko-Module-Homs have different types, they probably need to
come under a common abstract type or be more selective
=#
"""
Code of the H^1(G, M) computation:
returns homomorphisms A and B s.th.
M_1 -A-> M_2 -B-> M_3
satisfies
H^1 = kern(B)/image(A)
Or, kern(B) are the 1-co-chains, image(A) the 1-co-boundaries.
If M is a free abelian group (Z^n), then this is used in the solvable
quotient to compute the H^1 of Q^n/Z^n via duality.
"""
function H_one_maps(C::GModule)
#= idea, after Holt:
H^1 = crossed homs. due to action on the right
f(ab) = f(a)^b + f(b)
if G=<g_1, ..., g_r | r_1, ..., r_l>
then X in H^1 iff X(r_i) = 0 for all i
X:G->M is given as X in M^r, where X(g_i) = X[i]
X(r_i) corresponds to some map phi_i : M^r -> M
phi_i = oplus h_j M for some homs h_j coming from the word in r
so, a kernel computation again
=#
G = Group(C)
n = ngens(G)
M = Module(C)
D, pro, inj = direct_product([M for i=1:n]..., task = :both)
F, mF = fp_group(C)
R = relators(F)
@assert G == F
K = D
ac = action(C)
iac = inv_action(C)
idM = hom(M, M, gens(M)) #identity map to start with
#TODO: require an identity_hom constructor
Kr, pKr, iKr = direct_product([M for i=R]..., task = :both)
gg = nothing
i = 1
for r = R
W = word(r)
g = idM
P = hom(D, M, [zero(M) for i=1:ngens(D)])
for w in W
if w < 0
#by above: f(ab) = f(a)^b + f(b)
#thus 0 = f(1) = f(a a^-1) = f(a)^(a^-1) + f(a^-1)
P = P*iac[-w]-pro[-w]*iac[-w]
g = g*iac[-w]
else
P = P*ac[w]+pro[w]
g = g*ac[w]
end
end
@assert all(x -> x == g(x), gens(M))
if gg === nothing
gg = P*iKr[i]
else
gg += P*iKr[i]
end
i += 1
end
#K is Z[1] - the co-cycles
#TODO: is kernel(g) directly faster than the method above (H_zero)
# where kernel(g) is computed slice by slice?
#TODO: cache the expensive objects!!!
g = sum((ac[i] - idM)*inj[i] for i=1:n)
return g, gg
end
"""
H^1(G, M)
Returns an abstract module (of the same type as M) describing the
first co-homology group. Furthermore, the second return value
is a map realising elements of H^1 as explicit co-cycles.
"""
function H_one(C::GModule)
z = get_attribute(C, :H_one)
if z !== nothing
return domain(z), z
end
g, gg = H_one_maps(C)
K = kernel(gg)[1]
D = domain(gg)
lf, lft = is_subgroup(D, K)
Q, mQ = quo(K, image(g)[1])
M = Module(C)
G = group(C)
z = MapFromFunc(
x->CoChain{1,elem_type(G),elem_type(M)}(C, Dict([(gen(G, i),) => pro[i](lft(preimage(mQ, x))) for i=1:ngens(G)])),
y->mQ(preimage(lft, sum(inj[i](y(gen(G, i))) for i=1:n))), Q, AllCoChains{1, elem_type(G), elem_type(M)}())
set_attribute!(C, :H_one => z)
return Q, z
#need to ALSO return the coboundary(s)
end
"""
Computes an isomorphic fp-group and a confluent system of
relations given as pairs of words.
Returns the new group, the isomorphism and the confluent relations.
"""
function confluent_fp_group(G::Oscar.GAPGroup)
C = GAP.Globals.ConfluentMonoidPresentationForGroup(G.X)
#has different generators than G! So the action will have to
#be adjusted to those words. I do not know if a RWS (Confluent) can
#just be changed...
k = C.monhom #[2] #hopefully the monhom entry in 4.12 it will be the name
M = GAP.Globals.Range(k)
g = [GAP.Globals.PreImageElm(k, x) for x = GAP.Globals.GeneratorsOfMonoid(M)]
g = map(GAP.Globals.UnderlyingElement, g)
g = map(GAP.Globals.LetterRepAssocWord, g)
@assert all(x->length(x) == 1, g)
g = map(x->Int(x[1]), g)
R = GAP.Globals.RelationsOfFpMonoid(M)
ru = Vector{Tuple{Vector{Int}, Vector{Int}}}()
for r = R
push!(ru, (map(x->g[Int(x)], GAP.Globals.LetterRepAssocWord(r[1])),
map(x->g[Int(x)], GAP.Globals.LetterRepAssocWord(r[2]))))
end
#now to express the new gens as words in the old ones:
Fp = FPGroup(GAP.Globals.Range(C.fphom))
return Fp, GAPGroupHomomorphism(Fp, G, GAP.Globals.InverseGeneralMapping(C.fphom)), ru
end
#############################
#
# H^2
#
# Thought of as group extensions given via a rewriting system.
# so we need collection...
#
mutable struct CollectCtx
r::Vector{Tuple{Vector{Int}, Vector{Int}}} #the rules, RWS
d1::Dict{Int, Int} #rules where lhs has length 1
d2::Dict{Tuple{Int, Int}, Vector{Int}} # length 2 prefixes
f::Function #(w::Vector{Int}, r::Int, p::Int)
#to be called in addition (to play with the tail(s))
#w the word, to be "reduced" using rule no r at pos p
T::Any
function CollectCtx(R::Vector{Tuple{Vector{Int}, Vector{Int}}})
n = new()
n.r = R
n.d1 = Dict{Int, Int}()
n.d2 = Dict{Tuple{Int, Int}, Vector{Int}}()
for i = 1:length(R)
r = R[i]
if length(r[1]) == 1
#still confused about that one...
# but I have a rule [-1] -> [1, 2]
# @assert length(r[2]) == 1
n.d1[r[1][1]] = i
continue
end
@assert length(r[1]) > 1
p = (r[1][1], r[1][2])
if Base.haskey(n.d2, p)
push!(n.d2[p], i)
else
n.d2[p] = [i]
end
end
for p = keys(n.d2)
sort!(n.d2[p], lt = (a,b) -> isless(R[a], R[b]))
end
return n
end
end
function Base.collect(w::Vector{Int}, C::CollectCtx)
d1 = C.d1
d2 = C.d2
R = C.r
do_f = isdefined(C, :f)
nc = 0
i = 1
while true
nc += 1
if i>=length(w)
return w
end
if haskey(d1, w[i])
if do_f
C.f(C, w, d1[w[i]], i)
end
w = vcat(w[1:i-1], R[d1[w[i]]][2], w[i+1:end])
end
if haskey(d2, (w[i], w[i+1]))
for r = d2[(w[i], w[i+1])]
if length(R[r][1]) + i-1 <= length(w) &&
R[r][1] == w[i:i+length(R[r][1])-1]
if do_f
C.f(C, w, r, i)
end
w = vcat(w[1:i-1], R[r][2], w[i+length(R[r][1]):end])
i = 0
break
end
end
end
i += 1
end
return w
end
#= Hulpke-Dietrich:
UNIVERSAL COVERS OF FINITE GROUPS
https://arxiv.org/pdf/1910.11453.pdf
almost the same as Holt
=#
function H_two(C::GModule)
z = get_attribute(C, :H_two)
if false && z !== nothing
return domain(z[1]), z[1], z[2]
end
G = Group(C)
M = Module(C)
id = hom(M, M, gens(M), check = false)
Ac = action(C)
iAc = inv_action(C)
F, mF = fp_group(C) #mF: F -> G
FF, mFF, R = confluent_fp_group(G) #mFF: FF -> G
#now map the action generators (for gens(G)) to the gens for the RWS
ac = []
iac = []
for g = gens(FF)
f = id
for i = word(preimage(mF, mFF(g)))
if i < 0
f = f*iAc[-i]
else
f = f*Ac[i]
end
end
push!(ac, f)
push!(iac, inv(f))
end
c = CollectCtx(R)
#rules with length(LHS) == 1 and rules of the form
# [a a^-1] -> [], [a^-1 a] -> [] do not get tails
pos = Vector{Int}()
n = 0
for i = 1:length(R)
r = R[i]
if length(r[1]) == 1
push!(pos, 0)
continue
end
if length(r[1]) == 2 && length(r[2]) == 0 && r[1][1] == -r[1][2]
push!(pos, 0)
continue
end
n += 1
push!(pos, n)
end
if n == 0
D = sub(M, elem_type(M)[])[1]
pro = []
inj = []
else
D, pro, inj = direct_product([M for i=1:n]..., task = :both)
end
#when collecting (i.e. applying the RWS we need to also
#use the tails: g v h -> gh h(v)
#and if [gh] -> [x] with tail t, then
# gh -> x t, so
# g v h -> gh h(v) -> x t+h(v)
# Hulpke calls this the normal version: reduced group word
# at the beginning, module at the end, the tail.
# collect will call the extra function c.f if set in the
# CollectCtx
function symbolic_collect(C::CollectCtx, w::Vector{Int}, r::Int, p::Int)
#w = ABC and B == r[1], B -> r[2] * tail[r]
# -> A r[2] C C(tail)
# C = c1 c2 ... C(tail):
@assert w[p:p+length(R[r][1])-1] == R[r][1]
if pos[r] == 0
return
end
T = pro[pos[r]]
for i=w[p+length(R[r][1]):end]
if i < 0
T = T*iac[-i]
else
T = T*ac[i]
end
end
C.T += T
end
c.f = symbolic_collect
E = D
all_T = []
Z = hom(D, M, [M[0] for i=1:ngens(D)], check = false)
for i = 1:length(R)
r = R[i]
for j=1:length(R)
# i == j && continue
s = R[j]
#we want overlaps, all of them:
#r[1] = AB, s[1] = BC this is what we need to find...
#(then we call collect on r[2]C and As[2] they should agree)
for l=1:min(length(s[1]), length(r[1]))
if r[1][end-l+1:end] == s[1][1:l]
#TODO AB -> Ss s,t are tails
# BC -> Tt
# (AB)C -> SsC -> SC C(s)
# A(BC) -> ATt -> AT t
if pos[i] > 0
c.T = pro[pos[i]]
for h = s[1][l+1:end]
if h < 0
c.T = c.T * iac[-h]
else
c.T = c.T * ac[h]
end
end
else
c.T = Z
end
z1 = collect(vcat(r[2], s[1][l+1:end]), c)
T = c.T
c.T = Z
z2 = collect(vcat(r[1][1:end-l], s[2]), c)
if pos[j] > 0
c.T += pro[pos[j]]
end
@assert z1 == z2
push!(all_T, T-c.T)
end
end
end
end
if length(all_T) == 0
Q = sub(M, elem_type(M)[])[1]
jinj = hom(M, Q, elem_type(Q)[Q[0] for m = gens(M)])
else
Q, jinj = direct_product([M for i in all_T]..., task = :sum)
end
mm = reduce(+, [all_T[i]*jinj[i] for i = 1:length(all_T)], init = hom(D, Q, elem_type(Q)[Q[0] for i=1:ngens(D)]))
E, mE = kernel(mm)
@assert all(x->all(y->iszero(y(mE(x))), all_T), gens(E))
@assert all(x->iszero(mm(mE(x))), gens(E))
if length(ac) == 0
B = sub(M, elem_type(M)[])[1]
B_pro = []
B_inj = []
else
B, B_pro, B_inj = direct_product([M for i=1:length(ac)]..., task = :both)
end
CC = hom(B, D, elem_type(D)[zero(D) for i=1:ngens(B)], check = false)
for i=1:length(R)
if pos[i] == 0
continue
end
r = R[i]
if false && length(r[1]) == 1
continue
end
#we have words r[1] and r[2] of shape g_1 g_2 ....
#they need to be replaced by g_1 pro[1] g_2 pro[2]
#and then sorted: g_1 pro[1] g_2 pro[2] ... ->
# g_1 g_2 (pro[1] * g_2 + pro[2]) ...
if r[1][1] < 0
T = -B_pro[-r[1][1]]*iac[-r[1][1]]
else
T = B_pro[r[1][1]]
end
for j=2:length(r[1])
if r[1][j] < 0
T = (T-B_pro[-r[1][j]])*iac[-r[1][j]]
else
T = T*ac[r[1][j]] + B_pro[r[1][j]]
end
end
if length(r[2]) == 0
S = hom(B, M, [M[0] for g = gens(B)], check = false)
elseif r[2][1] < 0
S = -B_pro[-r[2][1]]*iac[-r[2][1]]
else
S = B_pro[r[2][1]]
end
for j=2:length(r[2])
if r[2][j] < 0
S = (S-B_pro[-r[2][j]])*iac[-r[2][j]]
else
S = S*ac[r[2][j]] + B_pro[r[2][j]]
end
end
# @assert issubset(image((T-S)*inj[pos[i]])[1], E)
CC += (T-S)*inj[pos[i]]
end
i, mi = image(CC)
# @show intersect(i, E)
H2, mH2 = quo(E, i)
function TailFromCoChain(cc::CoChain{2})
#for all tails, ie. rules with pos[r]>0, we need to use
#the 2-chain to compute inv(r[2])*r[1]
#rule with tail: r[1] = r[2]*t, so t = r[2]^-1*r[1]
T = zero(D)
for r=1:length(pos)
if pos[r] == 0
continue
end
t1 = zero(M)
g1 = one(G)
w = R[r][1]
for i=1:length(w)
if w[i] > 0
t1 = ac[w[i]](t1)+cc(g1, mFF(gen(FF, w[i])))
g1 = g1*mFF(gen(FF, w[i]))
else
#need to mult by (w, 0)^-1 = (w^-1, -cc(w, w^-1))
#so (g, t) (w, 0)^-1 = (g w^-1, t^w^-1 - cc(w, w^-1) + cc(g, w^-1)
t1 = iac[-w[i]](t1)-cc(mFF(gen(FF, -w[i])), inv(mFF(gen(FF, -w[i]))))+cc(g1, inv(mFF(gen(FF, -w[i]))))
g1 = g1*mFF(inv(gen(FF, -w[i])))
end
end
t2 = zero(M)
g2 = one(G)
w = R[r][2]
for i=1:length(w)
if w[i] > 0
t2 = ac[w[i]](t2)+cc(g2, mFF(gen(FF, w[i])))
g2 = g2*mFF(gen(FF, w[i]))
else
#need to mult by (w, 0)^-1 = (w^-1, -cc(w, w^-1))
#so (g, t) (w, 0)^-1 = (g w^-1, t^w^-1 - cc(w, w^-1) + cc(g, w^-1)
t2 = iac[-w[i]](t2)-cc(mFF(gen(FF, -w[i])), inv(mFF(gen(FF, -w[i]))))+cc(g2, inv(mFF(gen(FF, -w[i]))))
g2 = g2*mFF(inv(gen(FF, -w[i])))
end
end
@assert g1 == g2
#=
w = [-x for x = reverse(R[r][2])]
append!(w, R[r][1])
#w is inv(r[2])*r[1]
t = zero(M)
g = one(G)
for i=1:length(w)
if w[i] > 0
t = ac[w[i]](t)+cc(g, mFF(gen(FF, w[i])))
g = g*mFF(gen(FF, w[i]))
else
#need to mult by (w, 0)^-1 = (w^-1, -cc(w, w^-1))
#so (g, t) (w, 0)^-1 = (g w^-1, t^w^-1 - cc(w, w^-1) + cc(g, w^-1)
t = iac[-w[i]](t)-cc(mFF(gen(FF, -w[i])), inv(mFF(gen(FF, -w[i]))))+cc(g, inv(mFF(gen(FF, -w[i]))))
g = g*inv(gen(G, -w[i]))
end
end
#= Maybe? Not clear what I actually want/ need here, darn
wrong currently...
if length(R[r][2]) > 0
r2 = R[r][2][1] < 0 ? inv(gen(FF, -R[r][2][1])) : gen(FF, R[r][2][1])
for i=2:length(R[r][2])
r2 *= R[r][2][i] < 0 ? inv(gen(FF, -R[r][2][i])) : gen(FF, R[r][2][i])
end
else
r2 = one(FF)
end
@show r2, mFF(r2)
t = t + cc(mFF(inv(r2)), mFF(r2))
=#
=#
T += inj[pos[r]](t1-t2)
end
return T
end
function TailToCoChain(t)
c.f = function(C::CollectCtx, w::Vector{Int}, r::Int, p::Int)
#w = ABC and B == r[1], B -> r[2] * tail[r]
# -> A r[2] C C(tail)
# C = c1 c2 ... C(tail):
@assert w[p:p+length(R[r][1])-1] == R[r][1]
if pos[r] == 0
return
end
T = pro[pos[r]](t)
for i=w[p+length(R[r][1]):end]
if i < 0
T = iac[-i](T)
else
T = ac[i](T)
end
end
C.T += T
end
di = Dict{NTuple{2, elem_type(G)}, elem_type(M)}()
#= if I figure out how to extend from generators
w = [word(order(G) == 1 ? one(domain(mFF)) : preimage(mFF, g)) for g = gens(G)]
for i=1:ngens(G)
for j=1:ngens(G)
c.T = zero(M)
collect(vcat(w[i], w[j]), c)
di[(gen(G, i), gen(G, j))] = c.T
end
end
=#
for g = G
for h = G
c.T = zero(M)
if order(G) > 1
gg = collect(word(preimage(mFF, g)), c)
hh = collect(word(preimage(mFF, h)), c)
c.T = zero(M)
d = collect(vcat(gg, hh), c)
end
di[(g, h)] = c.T
end
end
return CoChain{2,elem_type(G),elem_type(M)}(C, di)
end
symbolic_chain = function(g, h)
c.f = symbolic_collect
if order(G) == 1
w = word(preimage(mFF, one(G)))
else
c.T = Z
wg = collect(word(preimage(mFF, g)), c)
wh = collect(word(preimage(mFF, h)), c)
w = vcat(wg, wh)
end
c.T = Z
w = collect(w, c)
return mE*c.T, w
end
set_attribute!(C, :H_two_symbolic_chain => (symbolic_chain, mH2))
function iscoboundary(cc::CoChain{2})
t = TailFromCoChain(cc)
fl, b = haspreimage(CC, t)
if !fl
return false, nothing
end
d = Dict{Tuple{elem_type(G), }, elem_type(M)}()
# t gives, directly, the images of the generators (of FF)
im_g = [B_pro[i](b) for i=1:ngens(FF)]
# otherwise: sigma(g, h) + sigma(gh) = sigma(g)^h + sigma(h)
# this gives the images fir the inverses, and then for everything
im_gi = [cc((mFF(gen(FF, i)), mFF(inv(gen(FF, i))))) - iac[i](im_g[i]) for i=1:ngens(FF)]
@assert domain(mFF) == FF
@assert codomain(mFF) == G == group(cc.C)
for g = G
m = zero(M)
h = one(G)
w = word(preimage(mFF, g))
for i=1:length(w)
if w[i] < 0
m = iac[-w[i]](m)+im_gi[-w[i]]-cc((h, mFF(inv(gen(FF, -w[i])))))
h = h*mFF(inv(gen(FF, -w[i])))
else
m = ac[w[i]](m)+im_g[w[i]]-cc((h, mFF(gen(FF, w[i]))))
h = h*mFF(gen(FF, w[i]))
end
end
d[(g,)] = m
@assert g == h
end
return true, CoChain{1,elem_type(G),elem_type(M)}(C, d)
end
z = (MapFromFunc(x->TailToCoChain(mE(x)),
y->preimage(mE, TailFromCoChain(y)), E, AllCoChains{2,elem_type(G),elem_type(M)}()),
# y->TailFromCoChain(y), D, AllCoChains{2,elem_type(G),elem_type(M)}()),
iscoboundary)
set_attribute!(C, :H_two => z)
return H2, z[1], z[2]
#now the rest...
#(g, m)*(h, n) = (gh, m^h+n+gamma(g, h)) where gamma is "the" 2-cocycle
#using tails:
# gmhn -> gh h(m)+n -> x t+h(m) + n where x is the reduced
# word under collection and t is the
# "tail"
# so gamma(g, h) = t
# given gamma need the tails:
# need to implement the group operation for the extension
# (g, v)(h, u) -> (gh, v^h + u + gamma(g, h))
# then the rules with tails need to be evaluated at
# the group generators (g_i, 0)
# r -> s gives a relation r s^-1 which should evaluate, using gamma
# to (0, t) where t is the tail for this rule
end
"""
For a gmodule `C` compute the `i`-th cohomology group
where `i` can be `0`, `1` or `2`.
Together with the abstract module, a map is provided that will
produce explicit cochains.
"""
function cohomology_group(C::GModule{PermGroup,GrpAbFinGen}, i::Int)
#should also allow modules...
if i==0
return H_zero(C)
elseif i==1