-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroupPresentations.jl
3516 lines (3236 loc) · 115 KB
/
GroupPresentations.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
"""
This is a port of some GAP3/VkCurve functionality on *presentations* of
*finitely presented groups*.
We have defined just enough functionality on finitely presented groups so
that presentations can be translated to finitely presented groups and
vice-versa. The focus is on presentations, the goal being to simplify them.
The elements of finitely presented groups are `AbsWord` or abstract words,
representing elements of a free group.
```julia-repl
julia> @AbsWord a,b,c,d,e,f # same as a=AbsWord(:a);b=AbsWord(:b)...
julia> F=FpGroup([a,b,c,d,e,f])
FreeGroup(a,b,c,d,e,f)
julia> G=F/[a^2,b^2,d*f^-1,e^2,f^2,a*b^-1*c,a*e*c^-1,b*d^-1*c,c*d*e^-1,a*f*c^-2,c^4]
FreeGroup(a,b,c,d,e,f)/[a²,b²,df⁻¹,e²,f²,ab⁻¹c,aec⁻¹,bd⁻¹c,cde⁻¹,afc⁻²,c⁴]
julia> simplify(F) # the main function of this package
Presentation: 2 generators, 4 relators, total length 16
Presentation: 2 generators, 3 relators, total length 10
FreeGroup(a,c)/[a²,ac⁻¹ac⁻¹,c⁴]
```
The simplification is done by the following process:
```julia-rep1
julia> P=Presentation(G);simplify(P);G=FpGroup(P)
```
The functions `Presentation` and `FpGroup` create a presentation from a
finitely presented group and vice versa.
In order to speed up the algorithms, the relators in a presentation are not
represented internally by `AbsWord`s, but by lists of positive or negative
generator numbers which we call *Tietze words*. Here is another example
with a few functions to explore presentations.
```julia-repl
julia> @AbsWord a,b
julia> F=FpGroup([a,b])
FreeGroup(a,b)
julia> G=F/[a^2,b^7,comm(a,a^b),comm(a,a^(b^2))*inv(b^a)]
FreeGroup(a,b)/[a²,b⁷,a⁻¹b⁻¹a⁻¹bab⁻¹ab,a⁻¹b⁻²a⁻¹b²ab⁻²ab²a⁻¹b⁻¹a]
julia> P=Presentation(G) # by default give a summary
Presentation: 2 generators, 4 relators, total length 30
julia> relators(P)
4-element Vector{AbsWord}:
a²
b⁷
ab⁻¹abab⁻¹ab
b⁻²ab²ab⁻²ab²ab⁻¹
```
```julia-rep1
julia> showgens(P)
1. a 10 occurrences involution
2. b 20 occurrences
julia> dump(P) # here in relators inverses are represented by capitalizing
# F relator
1:3 aa
2:0 bbbbbbb
3:0 aBabaBab
4:0 abbaBBabbaBBB
gens=AbsWord[a, b] involutions:AbsWord[a] modified=false numredunds=0
julia> display_balanced(P)
1: a=A
2: bbbbbbb=1
3: aBab=BAbA
4: BBabbaBBabbaB=1
julia> P=tryconjugate(P) # try to conjugate the generators
Presentation: 2 generators, 4 relators, total length 30
Bab=> Presentation: 2 generators, 3 relators, total length 28
# Bab gives Presentation: 2 generators, 3 relators, total length 28
Presentation: 2 generators, 3 relators, total length 28
julia> FpGroup(P) # slightly simplified group
FreeGroup(a,b)/[b⁷,bab⁻¹abab⁻¹a,b⁻¹ab²ab⁻²ab²ab⁻²]
```
for more information look at the help strings of `AbsWord, FpGroup,
Presentation, relators, display_balanced, simplify, conjugate,
tryconjugate`.
A minimal thing to add to this package so it would be a reasonable package
for finitely preented groups is the Coxeter-Todd algorithm.
"""
module GroupPresentations
## Changing Presentations
#
#The functions `AddGenerator`, `AddRelator`, `RemoveRelator` can be used to
#change a presentation. In general, they will change the isomorphism type of
#the group defined by the presentation, hence, though they are sometimes
#used as subroutines by Tietze transformations functions like `Substitute`,
#they do *not* perform Tietze transformations themselves.
#
## Tietze Transformations
#
#The functions described in this section can be used to modify a
#group presentation by Tietze transformations.
#
#In general, the aim of such modifications will be to *simplify* the given
#presentation, i.e., to reduce the number of generators and the number of
#relators without increasing too much the sum of all relator lengths which
#we will call the *total length* of the presentation. Depending on the
#concrete presentation under investigation one may end up with a nice, short
#presentation or with a very huge one.
#
#There is no way to find the shortest presentation which can be obtained
#from a given one. Therefore, what we offer are some lower-level Tietze
#transformation functions and, in addition, a heuristic higher-level
#function (which of course cannot be the optimal choice for all
#presentations).
#
#The design of these functions follows closely the concept of the ANU Tietze
#transformation program designed by George Havas cite{Hav69} which has been
#available from Canberra since 1977 in a stand-alone version implemented by
#Peter Kenne and James Richardson and later on revised by Edmund
#F.~Robertson (see cite{HKRR84}, cite{Rob88}).
#
#The higher-level function is `simplify`. The lower-level functions are
#`Eliminate`, `Search`, `SearchEqual`, and `FindCyclicJoins`.
#
#Some of these functions may eliminate generators, but they do *not*
#introduce new generators. However, sometimes you will need to substitute
#certain words as new generators in order to improve your presentation.
#Therefore there are the functions `Substitute` and `SubstituteCyclicJoins`
#which introduce new generators.
#
#Finally the functions `tracing` and `images` can
#be used to determine and to display the images or preimages of the involved
#generators under the isomorphism which is defined by the sequence of Tietze
#transformations which are applied to a presentation.
#
#The functions, `show_pairs`, and `PrintOptions`, can be useful. There are
#also the *Tietze options*: parameters which essentially influence the
#performance of the functions mentioned above; they are not specified as
#arguments of function calls. Instead, they are stored in the presentation.
rio(io::IO=stdout;p...)=IOContext(io,:limit=>true,p...)
function stringind(io::IO,n::Integer)
if get(io,:TeX,false)
n in 0:9 ? "_"*string(n) : "_{"*string(n)*"}"
elseif get(io,:limit,false)
if n<0 res=['₋']; n=-n else res=Char[] end
for i in reverse(digits(n)) push!(res,Char(0x2080+i)) end
String(res)
else "_"*string(n)
end
end
const supvec=collect("⁰¹²³⁴⁵⁶⁷⁸⁹")
function stringexp(io::IO,n::Integer)
if isone(n) ""
elseif get(io,:TeX,false)
n in 0:9 ? "^"*string(n) : "^{"*string(n)*"}"
elseif get(io,:limit,false)
if n<0 res=['⁻']; n=-n else res=Char[] end
for i in reverse(digits(n)) push!(res,supvec[i+1]) end
String(res)
else "^"*string(n)
end
end
using PermGroups
using Combinat: tally
export AbsWord, @AbsWord, Presentation, FpGroup, Go, GoGo, conjugate,
tryconjugate, simplify, relators, display_balanced, tracing, images,
showgens
plural(n,w)=string(n)*" "*w*(n==1 ? "" : "s")
#------------------ Abstract Words ----------------------------------
"""
An `AbsWord` represents an element of the free group on some generators.
The generators are indexed by `Symbols`. For example the `Absword`
representing `a³b⁻²a` is represented internally as
`[:a => 3, :b => -2, :a => 1]`. The mulitiplcation follows the group rule:
```julia-repl
julia> w=AbsWord([:a => 3, :b => -2, :a => 1])
a³b⁻²a
julia> w*AbsWord([:a=>-1,:b=>1])
a³b⁻¹
```
A positive `AbsWord` may be obtained by giving `Symbols` as arguments
```julia-repl
julia> AbsWord(:b,:a,:a,:b)
ba²b
```
"""
struct AbsWord
d::Vector{Pair{Symbol,Int}}
function AbsWord(v::Vector{Pair{Symbol,Int}};check=true)
if check && length(v)>0
ri=1
for i in 2:length(v)
if ri==0 || first(v[i])!=first(v[ri])
ri+=1
v[ri]=v[i]
else c=last(v[ri])+last(v[i])
if iszero(c) && ri>0 ri-=1
else v[ri]=first(v[ri])=>c
end
end
end
resize!(v,ri)
end
new(v)
end
end
AbsWord(x::Symbol...)=AbsWord([s=>1 for s in x])
"`@AbsWord x,y` is the same as `x=AbsWord(:x);y=AbsWord(y)`"
macro AbsWord(t)
if t isa Expr
for v in t.args
Base.eval(Main,:($v=AbsWord($(Core.QuoteNode(Symbol(v))))))
end
elseif t isa Symbol
Base.eval(Main,:($t=AbsWord($(Core.QuoteNode(t)))))
end
end
"AbsWord(s::String) defines an `AbsWord` from a line of display_balanced"
function AbsWord(s::AbstractString)
ss=split(s,"=")
if ss[end]=="1" pop!(ss) end
if length(ss)==2 return AbsWord(ss[1])*inv(AbsWord(ss[2])) end
s=ss[1]
res=Pair{Symbol,Int}[]
for c in collect(s)
if islowercase(c) push!(res,Symbol(c)=>1)
else push!(res,Symbol(lowercase(c))=>-1)
end
end
AbsWord(res)
end
function Base.show(io::IO,a::AbsWord)
if isone(a) print(io,".") end
for (s,c) in a.d
print(io,string(s))
if c!=1 print(io,stringexp(io,c)) end
end
end
Base.one(::Type{AbsWord})=AbsWord(Pair{Symbol,Int}[])
Base.isone(a::AbsWord)=isempty(a.d)
Base.:*(a::AbsWord,b::AbsWord)=AbsWord(vcat(a.d,b.d))
Base.inv(a::AbsWord)=AbsWord([k=>-v for (k,v) in reverse(a.d)];check=false)
Base.:^(a::AbsWord, n::Integer)=n>=0 ? Base.power_by_squaring(a,n) :
Base.power_by_squaring(inv(a),-n)
Base.:^(a::AbsWord, b::AbsWord)=inv(b)*a*b
Base.:/(a::AbsWord, b::AbsWord)=a*inv(b)
Base.:\(a::AbsWord, b::AbsWord)=inv(a)*b
Base.length(a::AbsWord)=sum(x->abs(last(x)),a.d;init=0)
Base.copy(a::AbsWord)=AbsWord(copy(a.d))
Base.:(==)(a::AbsWord,b::AbsWord)=a.d==b.d
"returns symbol for an AbsWord of length 1"
function mon(w::AbsWord)
if length(w.d)!=1 || last(w.d[1])!=1 error("not a generator") end
first(w.d[1])
end
function Base.getindex(w::AbsWord,i::Integer)
for (s,n) in w.d
if i>abs(n) i-=abs(n)
elseif n<0 return s=>-1
else return s=>1
end
end
error("index out of bounds")
end
Base.getindex(w::AbsWord,i::AbstractVector{<:Integer})=AbsWord(getindex.(Ref(w),i))
#-------------------------- FpGroups -----------------------------
struct FpGroup <: Group{AbsWord}
gens::Vector{AbsWord}
rels::Vector{AbsWord}
end
FpGroup(gens::Vector{AbsWord})=FpGroup(gens,AbsWord[])
FpGroup(s::Symbol...)=FpGroup(AbsWord.(collect(s)),AbsWord[])
function Base.show(io::IO,G::FpGroup)
print(io,"FreeGroup(",join(gens(G),","),")")
if length(G.rels)>0 print(io,"/[");join(io,G.rels,",");print(io,"]") end
end
function Base.:/(G::FpGroup,rel::Vector{AbsWord})
append!(G.rels,rel)
G
end
# Tietze word from AbsWord
function Groups.word(G::FpGroup,w::AbsWord)
res=Vector{Int}(undef,length(w))
x=1
for (s,m) in w.d
p=findfirst(x->x.d[1][1]==s,gens(G))
if isnothing(p) error(w," is not a word for ",G) end
for i in 1:abs(m)
res[x]=m<0 ? -p : p
x+=1
end
end
res
end
function Groups.elements(F::FpGroup,i)
l1=map(x->x.d[1],gens(F))
l=map(x->x[1]=>-x[2],l1)
l=vcat(l,l1)
res=AbsWord.(collect.(Iterators.product(fill(l,i)...)))
filter(x->length(x)==i,res)
end
#-------------------------- Tietze words and structs -------------
"""
`TietzeWord(word::AbsWord, generators::Vector{AbsWord})`
Let `generators` be a list of abstract generators and `word` an abstract
word in these generators. The function `TietzeWord` returns the
corresponding (reduced) Tietze word.
```julia-repl
julia> F=FpGroup(:a,:b,:c)
FreeGroup(a,b,c)
julia> GroupPresentations.TietzeWord(comm(F(1),F(2))*inv(F(3)^2*F(2)),gens(F))
5-element Vector{Int64}:
-1
-2
1
-3
-3
```
"""
function TietzeWord(w::AbsWord,gens::Vector{AbsWord})
ss=mon.(gens)
res=Int[]
for (s,c) in w.d
p=findfirst(==(s),ss)
if p===nothing error(s," is not in ",gens) end
if c>0 append!(res,fill(p,c))
else append!(res,fill(-p,-c))
end
end
res
end
@GapObj mutable struct Presentation
generators::Vector{AbsWord} # copy of initial gens
inverses::Vector{Int}
relators::Vector{Vector{Int}}
flags::Vector{Int}
modified::Bool
numredunds::Int
end
# possible fields: tietze, nextFree, eliminationsLimit,
# expandLimit, generatorsLimit, lengthLimit, loopLimit, debug,
# saveLimit, searchSimultaneous, protected, status
" `relators(P::Presentation)` relators of `P` as `AbsWord`s. "
relators(P::Presentation)=AbsWord.(P.relators,Ref(P.generators))
"""
`FpGroup(P::Presentation)`
returns the finitely presented group defined by the presentation `P`.
"""
function FpGroup(P::Presentation)
debug(P,3,"# converting presentation to FpGroup")
if P.numredunds>0 RemoveGenerators(P) end
sort!(P)
G=FpGroup(deepcopy(P.generators),relators(P))
if haskey(P, :imagesOldGens)
G.imagesOldGens=deepcopy(P.imagesOldGens)
end
if haskey(P, :preImagesNewGens)
G.preImagesNewGens=deepcopy(P.preImagesNewGens)
end
G
end
Presentation(s::Vector{Symbol},r,pl=1)=Presentation(AbsWord.(s),r,pl)
function Presentation(gens::Vector{AbsWord},grels::Vector{AbsWord},debug::Int=1)
numgens=length(gens)
rels=map(w->reduceword(TietzeWord(w,gens);cyclically=true),grels)
P=Presentation(deepcopy(gens),numgens:-1:-numgens,rels,fill(1,length(rels)),
false, 0, Dict{Symbol,Any}())
P.nextFree=numgens+1
P.eliminationsLimit=100
P.expandLimit=150
P.generatorsLimit=0
P.lengthLimit=0 # infinity
P.loopLimit=0 # infinity
P.debug=debug
P.saveLimit=10
P.searchSimultaneous=20
PrintStatus(P,2)
P.protected=length(P.generators);HandleLength1Or2Relators(P);P.protected=0
sort!(P)
PrintStatus(P,2)
P
end
function Presentation(v::Vector{Vector{Int}})
gens=sort(unique(abs(x) for r in v for x in r))
if length(gens)!=maximum(gens) error("hummm!") end
vars=length(gens)<=26 ? Symbol.('a':'a'+length(gens)-1) :
map(i->Symbol("x",stringind(rio(),i)),1:length(gens))
Presentation(AbsWord.(vars),map(r->AbsWord(r,vars),v))
end
"""
`Presentation( G::FpGroup[, debug=1])`
returns the presentation corresponding to the given finitely presented
group `G`.
The optional `debug` parameter can be used to restrict or to extend the
amount of output provided by Tietze transformation functions when being
applied to the created presentation. The default value 1 is designed for
interactive use and implies explicit messages to be displayed by most of
these functions. A `debug` value of 0 will suppress these messages, whereas
a `debug` value of 2 will enforce some additional output.
"""
Presentation(G::FpGroup,printlevel::Integer=1)=Presentation(G.gens,G.rels)
# takes as input the output of display_balanced
function Presentation(s::String;level=1)
s=replace(s,r"\n\s*="=>"=")
l=split(s,"\n")
l=map(s->replace(s,r"^ *[0-9]*: *"=>""),l)
l=filter(x->match(r"^ *$",x)===nothing,l)
rels=AbsWord.(l)
l=map(x->first.(x.d),rels)
atoms=length(l)==1 ? unique(l[1]) : union(map(x->first.(x.d),rels)...)
sort!(atoms)
Presentation(AbsWord.(atoms),rels,level)
end
Base.getindex(T::Presentation,i)=T.inverses[length(T.generators)+1-i]
Base.setindex!(T::Presentation,j,i)=T.inverses[length(T.generators)+1-i]=j
# reduce cyclically a Tietze word
function reduceword(w::Vector{Int};cyclically=false)
i=1;
res=Int[]
for j in eachindex(w)
if isempty(res) || res[end]!=-w[j] if w[j]!=0 push!(res,w[j]) end
else pop!(res)
end
end
b=1;e=length(res)
if cyclically while b<e && res[b]==-res[e] b+=1;e-=1 end end
res[b:e]
end
# reduce cyclically a Tietze word using the table of inverses
function reduceword(w::Vector{Int},T::Presentation)
i=1;
res=Int[]
for j in eachindex(w)
if T[w[j]]==0 continue end
if isempty(res) || T[res[end]]!=T[-w[j]] push!(res,T[w[j]])
else pop!(res)
end
end
b=1;e=length(res)
while b<e && T[res[b]]==T[-res[e]] b+=1;e-=1 end
res[b:e]
end
# sorts by length relators and remove duplicates and empty ones
function Base.sort!(T::Presentation)
if isempty(T.relators) return end
p=sortperm(T.relators,by=length)
p=unique(i->T.relators[i],p)
if isempty(T.relators[p[1]]) p=p[2:end] end
if p==eachindex(T.relators) return end
if T.debug>=3 println("#sort! the relators") end
numrels=length(p)
T.relators[1:numrels].=T.relators[p];resize!(T.relators,numrels)
T.flags[1:numrels].=T.flags[p];resize!(T.flags,numrels)
end
function debug(P,i,arg...)
if P.debug>=i println("#",arg...) end
end
"""
`PrintStatus(P::Presentation)`
updates and prints the current status of a presentation `P`, i.e., the
number of generators, the number of relators, and the total length of all
relators.
The printing is suppressed if none of the three values has changed since
the last call.
"""
function PrintStatus(P::Presentation,level=0)
if P.debug<level return end
status=[length(P.generators)-P.numredunds, length(P.relators),
sum(length,P.relators;init=0)]
if !haskey(P,:status) || status!=P.status
P.status=status
show(stdout,P)
println()
end
end
function Base.show(io::IO, p::Presentation)
print(io,"Presentation")
if haskey(p,:name) print(io," ",p.name) end
print(io,": ",plural(length(p.generators)-p.numredunds,"generator"),
", ",plural(length(p.relators),"relator"),", total length ",
sum(length,p.relators;init=0))
end
function Base.dump(T::Presentation)
l=filter(i->T[-i]!=-T[i],eachindex(T.generators))
l1=filter(i->T[-i]==T[i],l)
println("# F relator")
for i in eachindex(T.relators)
println("$i:",T.flags[i]," ",alphab(T.relators[i],T.generators))
end
print("gens=",T.generators)
if length(l1)>0 print(" involutions:",T.generators[l1])end
println(" modified=",T.modified," numredunds=",T.numredunds)
if l1!=l display(stack(map(i->[T.generators[i],T[i],T[-i]],l))) end
end
function display_balanced(T,dumb=false)
f(i,w)=vcat(w,w)[i:i+div(length(w),2)-1]
used=Set{Int}();for x in T.relators, y in x push!(used,abs(y)) end
if length(T.generators)>length(used)
print("There are ",length(T.generators)-length(used)," free generators\n")
end
for (i,w) in enumerate(T.relators)
lw=length(w)
if isodd(lw) || dumb println(i, ": ", alphab(w,T.generators), "=1")
elseif lw>0
m=argmax(map(i->count(>(0), f(i,w)), 1:lw))
println("$i: ",alphab(f(m,w),T.generators),"=",alphab(-reverse(f(m+div(lw,2),w)),T.generators))
end
end
end
"""
`showgens(P,list=eachindex(P.generators))`
prints the generators of `P` with the total number of their occurrences in
the relators, and notes involutions. A second `list` argument prints only
those generators.
"""
function showgens(T::Presentation,list=eachindex(T.generators))
gens=T.generators
if isempty(gens) println("#I there are no generators");return end
occur=Occurrences(T)
if list isa Integer list=[list] end
for i in list
print(i, ". ", gens[i], " ",plural(occur[1][i],"occurrence"))
if T[-i]>0 print(" involution") end
println()
end
end
"""
`show_pairs(P,n=10)`
shows the `n` most frequently occurring squarefree relator subwords of
length 2 with their number of occurrences.`n=0` is interpreted as infinity.
This is useful information in the context of the `Substitute` command.
"""
function show_pairs(P::Presentation,n::Integer=10)
for (m,(num,i,j,k)) in enumerate(MostFrequentPairs(P,n))
geni=P.generators[i];if k>1 geni=inv(geni) end
genj=P.generators[j];if isodd(k) genj=inv(genj) end
println(rio(),"#I $m. ",plural(num,"occurrence")," of ",geni*genj)
end
end
"""
`AbsWord(word, generators)`
Tranforms Tietze word `word` to an absword, using given generators `gens`.
```julia-repl
julia> AbsWord([-1,-2,1,-3,-3],AbsWord.([:a,:b,:c]))
a⁻¹b⁻¹ac⁻²
```
"""
AbsWord(tz::Vector{Int},gens::Vector{AbsWord})=AbsWord(tz,mon.(gens))
AbsWord(tz::Vector{Int},ss::Vector{Symbol})=AbsWord(map(i->ss[abs(i)]=>sign(i),tz))
(P::Presentation)(l::Int...)=AbsWord(collect(l),P.generators)
"""
`AddGenerator( P[, generator])`
`AddGenerator` adds a new generator to the list of generators.
If you don't specify a second argument, then `AddGenerator` will define a
new abstract generator `_xi` where `i` is the least positive integer which
has not yet been used as a generator number. Though this new generator will
be printed as `_xi`, you will have to use the external variable `P.i` if
you want to access it.
If you specify a second argument, then `generator` must be an abstract
generator which does not yet occur in the presentation. `AddGenerator` will
add it to the presentation.
"""
function AddGenerator(P::Presentation,gen=nothing)
Check(P)
if gen===nothing
gen=NewGenerator(P)
debug(P,1,"AddGenerator new generator is ", gen)
else
if !(gen isa AbsWord && length(gen)==1)
error("second argument must be an abstract generator")
end
if gen in P.generators || inv(gen) in P.generators
println("#I generator ",gen," is already in the presentation")
return
end
push!(P.generators,gen)
P.inverses=vcat([length(P.generators)],P.inverses, [-length(P.generators)])
end
tracing(P,false)
P.modified=true
end
"""
NewGenerator(P::Presentation)
defines a new abstract generator and adds it to `P`.
Let i be the smallest positive integer for which the generator `_xi` is not
a generator of `P`. A new abstract generator `_xi` is defined and then
added to `P.generators`.
Warning: `NewGenerator` is an internal subroutine of the Tietze
routines. You should not call it. Instead, you should call the function
`AddGenerator`, if needed.
"""
function NewGenerator(P::Presentation)
new=P.nextFree
while true
gen=AbsWord(Symbol("_x",new))
if gen in P.generators new+=1;continue end
end
P.nextFree=new+1
push!(P.generators,gen)
P.inverses=vcat([length(P.generators)],P.inverses,[-length(P.generators)])
gen
end
"""
`AddRelator(P::Presentation, word::AbsWord)`
adds the word `word` to the list of relators. `word` must
be a word in the generators of the given presentation.
"""
function AddRelator(P::Presentation, word::AbsWord)
Check(P)
debug(P,3,"AddRelator adding ", word)
rel=reduceword(TietzeWord(word,P.generators);cyclically=true)
if !isempty(rel)
push!(P.relators,rel)
push!(P.flags,1)
P.modified=true
end
tracing(P,false)
end
"""
HandleLength1Or2Relators(presentation) . . . handle short relators
`HandleLength1Or2Relators` searches for relators of length 1 or 2 and
performs suitable Tietze transformations for each of them:
Generators occurring in relators of length 1 are eliminated.
Generators occurring in square relators of length 2 are marked to be
involutions.
If a relator of length 2 involves two different generators, then the
generator with the larger number is substituted by the other one in all
relators and finally eliminated from the set of generators.
"""
function HandleLength1Or2Relators(T::Presentation)
debug(T,3,"Handle length 1 or 2 relators")
gens=T.generators
rels=T.relators
done=false
while !done
done=true
i=0
while i<length(T.relators)
i+=1
lg=length(rels[i])
if 0<lg<=2 && T.flags[i]<=2
rep1=T[rels[i][1]]
if lg==1
rep1=abs(rep1)
if rep1>T.protected
if T[rep1]==rep1 T.numredunds+=1 end
T[rep1]=T[-rep1]=0
debug(T,2,"Handle12 eliminating ",gens[rep1]," redund=",T.numredunds)
UpdateGeneratorImages(T, rep1, Int[])
done=false
end
else
rep2=T[rels[i][2]]
if abs(rep2)<abs(rep1) rep1,rep2=rep2,rep1 end
if rep1<0 rep1,rep2=(-rep1,-rep2) end
if rep1==0 # already eliminated
rep2=abs(rep2)
if rep2>T.protected
if T[rep2]==rep2 T.numredunds+=1 end
T[rep2]=T[-rep2]=0
debug(T,2,"Handle12 eliminating ",gens[rep2]," redund=",T.numredunds)
UpdateGeneratorImages(T, rep2, Int[])
done=false
end
elseif rep1!=-rep2 # otherwise not reduced
if rep1!=rep2 # not an involution
if T[rep2]==T[-rep2] && T[-rep1]<0 # rep2^2=1 => rep1^2=1
push!(rels,[rep1, rep1])
push!(T.flags,1)
end
if abs(rep2)>T.protected
if T[rep2]==rep2 T.numredunds+=1 end
T[rep2]=T[-rep1];T[-rep2]=rep1
if haskey(T,:imagesOldGens) || T.debug>=2
if rep2>0 rep1=T[-rep1] end
debug(T,2,"Handle12 eliminating ",gens[abs(rep2)],"=",
alphab([rep1],T.generators)," redund=",T.numredunds)
UpdateGeneratorImages(T,abs(rep2),[rep1])
end
done=false
end
elseif T[-rep1]<0 # an involution not yet detected
rels[i]=[rep1,rep1]
T.flags[i]=3
T[-rep1]=rep1
done=false
end
end
end
end
end
if !done
for i in eachindex(T.generators)
if T[i]!=i T[i],T[-i]=T[T[i]],T[-T[i]] end
end
# the next loop is FunTzReplaceGens
for i in eachindex(T.relators)
if length(T.relators[i])==2 && T.flags[i]==3 && abs(T[T.relators[i][1]])== abs(T.relators[i][1])continue end#don't remove involutions
T.relators[i]=reduceword(T.relators[i],T)
T.flags[i]=1
end
end
end
if T.numredunds>0 RemoveGenerators(T) end
end
"""
RelsViaCosetTable(G,cosets) . . . . . . . construct relators for the
RelsViaCosetTable(G,cosets,ggens) . . . . . . . . . given concrete
RelsViaCosetTable(G,cosets,F,words,H,F1) . . . . . . . group
`RelsViaCosetTable` constructs a defining set of relators for the given
concrete group using John Cannon's relations finding algrotithm.
It is a subroutine of function `PresentationViaCosetTable`. Hence its
input and output are specifically designed for this purpose. In
particular, it does not check the arguments.
G, # given group
cosets, # right cosets of G with respect to H
"""
function RelsViaCosetTable(G,cosets,arg...)
# F, # given free group
# words, # given words for the generators of H
# H, # subgroup, if specified
# F1, # f.p. group isomorphic to H
# F2, # f.p. group isomorphic to G
# ng1, # position number of identity element in G
# nh1, # position number of identity element in H
# perms, # permutations induced by the gens on the cosets
# stage, # 1 or 2
# table, # columns in the table for gens
# rels, # representatives of the relators
# relsGen, # relators sorted by start generator
# subgroup, # rows for the subgroup gens
# rels1, # list of relators
# app, # arguments list for `MakeConsequences`
# index, # index of the table
# col, # generator col in auxiliary table
# perm, # permutations induced by a generator on the cosets
# gens, # abstract gens in which the relators are written
# gens2, # the above abstract gens and their inverses
# ggens, # concrete generators of G
# ngens, # number of generators of G
# ngens2, # twice the above number
# order, # order of a generator
# actcos, # part 1 of Schreier vector of G by H
# actgen, # part 2 of Schreier vector of G by H
# tab0, # auxiliary table in parallel to table <table>
# cosRange, # range from 1 to index (= number of cosets)
# genRange, # range of the odd integers from 1 to 2*ngens-1
# geners, # order in which the table cols are worked off
# next, # local coset number
# left1, # part 1 of Schreier vector of H by trivial group
# right1, # part 2 of Schreier vector of H by trivial group
# n, # number of subgroup element
# words2, # words for the generators of H and their inverses
# h # subgroup element
if length(arg)==1 ggens=arg[1]
else ggens=G[:generators]
end
ngens=length(ggens)
ngens2=ngens * 2
if cosets[1] in G ng1=PositionSorted(cosets, cosets[1] ^ 0)
else ng1=1
end
index=length(cosets)
tab0=[]
table=[]
subgroup=[]
cosRange=1:index
genRange=map((i->begin 2i-1 end), 1:ngens)
if length(arg)<2
stage=1
F2=FreeGroup(ngens)
rels=[]
else
stage=2
F=arg[1]
words=arg[2]
F2=Group(F[:generators], IdWord)
if haskey(F, :namesGenerators)
F2[:namesGenerators]=F[:namesGenerators]
end
H=arg[3]
nh1=PositionSorted(H[:elements], (H[:elements])[1] ^ 0)
F1=arg[4]
rels=map(rel->MappedWord(rel, F1[:generators], words), F1[:relators])
left1=F1[:actcos]
right1=F1[:actgen]
words2=[]
for i in 1:length(F1[:generators])
push!(words2, words[i])
push!(words2, words[i] ^ -1)
end
end
gens=F2[:generators]
gens2=[]
perms=map((gen->begin Permutation(gen, cosets, OnRight) end), ggens)
for i in 1:ngens
push!(gens2, gens[i])
push!(gens2, gens[i] ^ -1)
perm=perms[i]
col=OnTuples(cosRange, perm)
gen=0*cosRange
push!(tab0, col)
push!(table, gen)
order=Order(G, ggens[i])
if order==2 push!(rels, gens[i] ^ 2)
else col=OnTuples(cosRange, perm ^ -1)
gen=0*cosRange
end
push!(tab0, col)
push!(table, gen)
end
cosets=0*cosRange
actcos=0*cosRange
actgen=0*cosRange
cosets[1]=ng1
actcos[ng1]=ng1
j=1
i=0
while i<index
i+=1
c=cosets[i]
g=0
while g<ngens2
g+=1
next=(tab0[g])[c]
if next>0 && actcos[next]==0
g1=(g+2 * mod(g, 2))-1
table[g][c]=next
table[g1][next]=c
tab0[g][c]=0
tab0[g1][next]=0
actcos[next]=c
actgen[next]=g
j=j+1
cosets[j]=next
if j==index
g=ngens2
i=index
end
end
end
end
rels=RelatorRepresentatives(rels)
app=fill(0,11)
app[1]=table
app[5]=subgroup
if stage==2
relsGen=RelsSortedByStartGen(gens, rels, table)
app[4]=relsGen
for g in genRange
gen0=tab0[g]
for c=cosRange
if gen0[c]==0
app[10]=g
app[11]=c
n=MakeConsequences(app)
end
end
end
end
geners=1:ngens2
for i in cosets
for j=geners
if table[j][i]==0
g=(j+2 * mod(j, 2))-1
c=tab0[j][i]
table[j][i]=c
table[g][c]=i
tab0[j][i]=0
tab0[g][c]=0
rel=IdWord
while c != ng1
g=actgen[c]
rel=rel * gens2[g] ^ -1
c=actcos[c]
end
rel=rel ^ -1 * gens2[j] ^ -1
c=i
while c != ng1
g=actgen[c]
rel=rel * gens2[g] ^ -1
c=actcos[c]
end
if stage==2
h=MappedWord(rel, gens, ggens)
n=PositionSorted(H[:elements], h)
while n != nh1
g=right1[n]
rel=rel * words2[g] ^ -1
n=left1[n]
end
end
rels1=RelatorRepresentatives([rel])
if length(rels1)>0
rel=rels1[1]
if !rel in rels
push!(rels, rel)
end
end
relsGen=RelsSortedByStartGen(gens, rels, table)
app[4]=relsGen
for g in genRange
gen=table[g]
gen0=tab0[g]
inv0=tab0[g+1]
for c=cosRange
if gen[c]>0
gen0[c]=0
inv0[gen[c]]=0
end
end
end
for g in genRange
gen0=tab0[g]
for c=cosRange
if gen0[c]==0
app[10]=g