File: DavisComplex.gi

package info (click to toggle)
gap-hap 1.74%2Bds-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 58,664 kB
  • sloc: xml: 16,678; sh: 197; javascript: 155; makefile: 121; ansic: 47; perl: 24
file content (890 lines) | stat: -rw-r--r-- 25,616 bytes parent folder | download
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
InstallGlobalFunction(DavisComplex, function(CoxMat)
local DC, GEN, STAB, BOUNDARY, COEFF, CONJ, DIMENSION, numberOfCells,
irreducible, equalCoxeterMatrices, generateA, generateB, generateD,
H_4, F_4, E_6, E_7, E_8, irreducibleComponents, irr, spherical, extractSpherical, canonicalGenerators, generateStabs, gens, generateBoundary, generateCoeff, 
generateConj, getDimension, getNumberOfSimplices, chains, 
DavisComplexCombinatorial, aux, ExportAsContractibleGcomplex, IsSphericalCoxeterGroup, CreateCoxeterMatrix, MaximalSpherical ;




##########################################################
# DavisComplexes, a HAP sub-package in GAP
# by Ruben J. Sanchez-Garcia and Alexander D. Rahm,
#
# containing Ruben J. Sanchez-Garcia's implementation
# of the Davis complex for Coxeter groups.
#
##########################################################


##########################################################################
# Function IsSphericalCoxeterGroup
# The input is a Coxeter matrix, the output true or false
# Ruben J Sanchez-Garcia & Alexander Rahm
# GAP version 4.3 (TBU)
# May 2015
##########################################################################

#### SPHERICAL ####
## Check whether the Coxeter group represented by a
## Coxeter matrix M is spherical (finite)

## We need firstly the procedures irreducible, irreducibleComponents
## and equalCoxeterMatrices


#### OBTAIN IRREDUCIBLE COMPONENTS OF A COXETER GROUP ####
## if irreducible returns [ true, CoxeterMatrix ] 
## else returns [false, M1, M2] ] where Mi are two 
## submatrices corresponding to two components, 
## maybe not yet irreducible 
irreducible := function(CoxeterMatrix)
    local N, candidates, partition, tested, remove, s, c;
    
    N := Size( CoxeterMatrix );
    if N = 1 then return [true, CoxeterMatrix]; fi;
    
    candidates := [2..N];
    partition := [1];
    tested := [];
    
    while (candidates <> []) and (not IsEqualSet(partition, tested)) do
        s := Difference( partition, tested )[1]; 
        remove := [];
        for c in candidates do
            if CoxeterMatrix[s][c] <> 2 then
                AddSet( partition, c );
                AddSet( remove, c );
            fi;
        od;
        SubtractSet(candidates, remove);
        AddSet(tested, s);
    od;
    
    if candidates = [] then
        return [ true, CoxeterMatrix ];
    else
        return [ false, CoxeterMatrix{partition}{partition}, 
                            CoxeterMatrix{candidates}{candidates} ];
    fi;
    
end;


#### EQUAL AS COXETER MATRICES ####
## Determine whether there is a permutation p s.t.
## M1[p(i)][p(j)] = M2[i][j] all i,j
equalCoxeterMatrices := function( M1, M2)
    local perm, N, ContinueVariable, i, j;
    if DimensionsMat(M1) <> DimensionsMat(M2) then
        return false;
    elif 
        #checks whether entries(M1) = entries(M2)
        (IsEqualSet( Collected( Flat(M1) ), Collected( Flat(M2) ) ) = false) then
        return false;
    else
        N := Size(M1);
        for perm in SymmetricGroup(N) do
            ContinueVariable := true; 
            i := 1;
            while ContinueVariable and (i < N) do
                j := i+1;
                while ContinueVariable and (j <= N) do
                    if M1[i^perm][j^perm] = M2[i][j] then
                        j := j+1;
                    else
                        ContinueVariable := false; 
                    fi;
                od;
                i := i+1;
            od;
            if ContinueVariable = true then return true; fi;
        od;
        return false;
    fi;
end;


#### MATRICES SPHERICAL CASES ####
## Matrices for spherical cases: A_n, B_n, D_n,
generateA := function(N)
    local matrix, i;
    
    matrix := ( NullMat(N,N) + 2 ) - IdentityMat(N); #matrix all 2's except 1's at diagonal
    matrix[1][2] := 3;
    matrix[N][N-1] := 3;
    for i in [2..N-1] do
        matrix[i][i-1] := 3;
        matrix[i][i+1] := 3;
    od;
    return matrix;
end;

generateB := function(N)
    local matrix, i;
    
    matrix := ( NullMat(N,N) + 2 ) - IdentityMat(N); #matrix all 2's except 1's at diagonal
    matrix[1][2] := 4;
    matrix[N][N-1] := 4;
    for i in [2..N-1] do
        matrix[i][i-1] := 3;
        matrix[i][i+1] := 3;
    od;
    return matrix;
end;

generateD := function(N)
    local matrix, i;
    matrix := generateA(N);
    matrix[1][2] := 2;
    matrix[1][3] := 3;
    matrix[2][1] := 2;
    matrix[3][1] := 3;    
    return matrix;
end;

## Matrices for the cases H_4, F_4, E_6, E_7, E_8
H_4 := [ [1,5,2,2], [5,1,3,2], [2,3,1,3], [2,2,3,1] ];
F_4 := [ [1,3,2,2], [3,1,4,2], [2,4,1,3], [2,2,3,1] ];
E_6 := [ [1,3,2,2,2,2], [3,1,3,2,2,2], [2,3,1,3,3,2], [2,2,3,1,2,2], [2,2,3,2,1,3], [2,2,2,2,3,1] ];
E_7 := [ [1,3,2,2,2,2,2], [3,1,3,2,2,2,2], [2,3,1,3,3,2,2], [2,2,3,1,2,2,2], [2,2,3,2,1,3,2], [2,2,2,2,3,1,3], 
            [2,2,2,2,2,3,1] ];
E_8 := [ [1,3,2,2,2,2,2,2], [3,1,3,2,2,2,2,2], [2,3,1,3,3,2,2,2], [2,2,3,1,2,2,2,2], [2,2,3,2,1,3,2,2], 
            [2,2,2,2,3,1,3,2], [2,2,2,2,2,3,1,3], [2,2,2,2,2,2,3,1] ];

##########################################################################
# Auxiliary functions to CoxeterGroups.g
# Ruben J Sanchez
# University of Southampton 2001-04
# GAP version 4.3
# July 2004
##########################################################################



irreducibleComponents := function(CoxeterMatrix)
    local irr, comp1, comp2;
    
    irr := irreducible( CoxeterMatrix );
    if irr[1] = true then
        return [irr[2]];
    else
        comp1 := irreducibleComponents( irr[2] );    
        comp2 := irreducibleComponents( irr[3] );
        return Concatenation(comp1, comp2);
    fi;
end;


#### SPHERICAL FUNCTION ####
## Check whether the Coxeter group represented
## Coxeter matrix M is spherical (finite)
IsSphericalCoxeterGroup:=function(M)
    local N, i, j, sum, CoxeterMatrix, components, irre, 
            comp, fourOrfive, caseA, caseB, caseD;
    
    # finite for size 0 (trivial) or 1 (cyclic order 2)
    N := Size(M);
    if N <= 1 then
        return true;
    fi;
       
    # check whether some order is infinity
    for i in [1..N] do
        for j in [i+1..N] do
            if M[i][j] = 0 then return false;
            fi;
        od;
    od;           
        
    if N = 2 then 
        return true;
    fi;
        
    if N = 3 then
        sum := 1/M[1][2] + 1/M[1][3] + 1/M[2][3];
        if sum > 1 then 
            return true;
        else
            return false;
        fi;
    fi;
    
    # if Size >= 4, get irreducible components
    components := irreducibleComponents( M );
    irre := (Size(components) = 1); #irreducible?
    #if reducible, check recursively IsSphericalCoxeterGroup() on each component
    if not irre then
        for comp in components do
            if not IsSphericalCoxeterGroup(comp) then
                return false;
            fi;
        od;
        return true;
    #if irreducible, check exceptional cases
    else
        #easy checks: there is entries > 5? & #4's or 5's > 1 
        fourOrfive := false;
        for i in [1..N] do
            for j in [i+1..N] do
                if M[i][j] > 5 then
                    return false;
                elif (M[i][j] = 4) or (M[i][j] = 5) then
                    if fourOrfive = true then
                        return false;
                    else fourOrfive := true;
                    fi;
                fi;
            od;
        od;
        #check exceptional cases H_4, F_4, E_6, E_7, E_8
        if N <= 8 then
            if equalCoxeterMatrices(M, H_4) then
               return true;
            fi;
            if equalCoxeterMatrices(M, F_4) then
               return true;
            fi;
            if equalCoxeterMatrices(M, E_6) then
                return true;
            fi;
            if equalCoxeterMatrices(M, E_7) then
                return true;
            fi;
            if equalCoxeterMatrices(M, E_8) then
                return true;
            fi;
        fi;
        #check the cases A_n, B_n and D_n
        caseA := generateA(N); caseB := generateB(N); caseD := generateD(N);
        if equalCoxeterMatrices(M, caseA) then
                return true;
        fi;
        if equalCoxeterMatrices(M, caseB) then
                return true;
        fi;
        if equalCoxeterMatrices(M, caseD) then
                return true;
        fi;
        return false;
    fi;
       
end;






#### SPHERICAL FUNCTION ####
## Check whether the Coxeter group represented
## Coxeter matrix M is spherical (finite)
spherical := function(M)
    local N, i, j, sum, CoxeterMatrix, components, irre, 
            comp, fourOrfive, caseA, caseB, caseD;
    
    # finite for size 0 (trivial) or 1 (cyclic order 2)
    N := Size(M);
    if N <= 1 then
        return true;
    fi;
       
    # check whether some order is infinity
    for i in [1..N] do
        for j in [i+1..N] do
            if M[i][j] = 0 then return false;
            fi;
        od;
    od;           
        
    if N = 2 then 
        return true;
    fi;
        
    if N = 3 then
        sum := 1/M[1][2] + 1/M[1][3] + 1/M[2][3];
        if sum > 1 then 
            return true;
        else
            return false;
        fi;
    fi;
    
    # if Size >= 4, get irreducible components
    components := irreducibleComponents( M );
    irre := (Size(components) = 1); #irreducible?
    #if reducible, check recursively spherical() on each component
    if not irre then
        for comp in components do
            if not spherical(comp) then
                return false;
            fi;
        od;
        return true;
    #if irreducible, check exceptional cases
    else
        #easy checks: there is entries > 5? & #4's or 5's > 1 
        fourOrfive := false;
        for i in [1..N] do
            for j in [i+1..N] do
                if M[i][j] > 5 then
                    return false;
                elif (M[i][j] = 4) or (M[i][j] = 5) then
                    if fourOrfive = true then
                        return false;
                    else fourOrfive := true;
                    fi;
                fi;
            od;
        od;
        #check exceptional cases H_4, F_4, E_6, E_7, E_8
        if N <= 8 then
            if equalCoxeterMatrices(M, H_4) then
               return true;
            fi;
            if equalCoxeterMatrices(M, F_4) then
               return true;
            fi;
            if equalCoxeterMatrices(M, E_6) then
                return true;
            fi;
            if equalCoxeterMatrices(M, E_7) then
                return true;
            fi;
            if equalCoxeterMatrices(M, E_8) then
                return true;
            fi;
        fi;
        #check the cases A_n, B_n and D_n
        caseA := generateA(N); caseB := generateB(N); caseD := generateD(N);
        if equalCoxeterMatrices(M, caseA) then
                return true;
        fi;
        if equalCoxeterMatrices(M, caseB) then
                return true;
        fi;
        if equalCoxeterMatrices(M, caseD) then
                return true;
        fi;
        return false;
    fi;
       
end;



#### EXTRACT SPHERICAL SIMPLICES ####
## Remove from 'allchains' all chains containing a non-spherical subset
extractSpherical := function( allchains, numberOfGenerators, CoxeterMatrix )
    local L, i, j, subset, element, simplices_N, localchains;
    
    localchains := ShallowCopy(allchains);
    L := Size(CoxeterMatrix);
    for i in [L,L-1..2] do
        for subset in Combinations([1..L], i) do
            if spherical( CoxeterMatrix{subset}{subset} ) = false then
                for j in [1..Size(localchains)] do
                    SubtractSet( localchains[j], Filtered( localchains[j], C -> subset in C ) );
                od;
            fi;
        od;
    od;
return localchains;
end;         

#### GENERATING MATRICES (CANONICAL REPRESENTATION) ####
## Each matrix correpond to one generator ##
## Generator s_i correspond to matrix A[i] ##
## Input: Coxeter matrix M ##
canonicalGenerators := function(M)
    local cos, A, i, j, GEN;
    # auxiliary function: cos(n) = cos(Pi/n) = 1/2*(e^(Pi*I/n)-e^(Pi*I/n)^(2*n-1))
    # using GAP function E(n) = e^(2*Pi*I/n)
    cos := function(n)
        if n>0 then
            return (1/2)*( E(2*n) + E(2*n)^(2*n-1) );
        elif n = 0 then   #infinity case
            return 1;
        fi;
    end;

    # generating matrices, stored in array A
    A := [];
    GEN := Size(M);
    for i in [1..GEN] do
        A[i] := IdentityMat(GEN);
        for j in [1..GEN] do
            A[i][i][j] := A[i][i][j] + 2*cos(M[i][j]);
        od;
    od;
    return A;
end;


#### GENERATE STABILIZERS ####
# Generate list of stabilizers out of
# an array of 'chains' (such that chains[i] = chains of length i)
# with respect to the Coxeter matrix 'M'
# Note.- There cannot be a empty list of i-simplices
generateStabs := function( chains, M )
    local L, i, j, groupStabOfChain, simplices, simplex, stabs, gens;
    
    # Returns group corresponding to stabilizer of simplex
    # represented by chain, with generators strored in matrix gens
    groupStabOfChain := function( chain, gens )
        local l, smallest, set;
        if Size(chain[1]) < 1 then return Group( One ( gens[1] ) );
        else
            smallest := chain[1]; l := Size(smallest);
            for set in chain do
                if Size(set) < l then
                    smallest := set;
                    l := Size(set);
                fi;
            od;
            return Group( gens{smallest} );
        fi; 
    end;
    
    L := MaximumList( List( chains, x -> Size(x[1])) ); # length of longest chain
    simplices := []; stabs := [];
    gens := canonicalGenerators(M);
    for i in [1..L] do 
        j := 0; stabs[i] := []; 
        for simplex in chains[i] do
            j := j+1; 
            Add( stabs[i], groupStabOfChain( simplex, gens ) ); #stabs is NOT a set, must keep order
     #       SetName( stabs[i][j], Concatenation("Stab", String(i-1), "_", String(j)));
        od;
    od; 
    return stabs;
end;


#### GENERATE BOUNDARY ####
# Generated list of boundaries of each chain in
# the list of 'chains', where chains[i] = list of i-chains
generateBoundary := function( chains )
    local L, i, boundaryOfChain, simplices, simplex, boundary;
    
   # Returns list of chains = simplices in the boundary of N-simplex
   # given by chain, given by position in N-simplices
    boundaryOfChain := function( chain, simplices )
        local L, i, boundaryComponent, solution, temp, pos;
    
        L := Size(chain); 
        if L = 1 then return []; fi;
        if L < 1 then return fail; fi;
        solution := [];
        for i in Set( List( chain, Size ) ) do
            temp := First( chain, x -> Size(x) = i ); 
            boundaryComponent := Difference( chain, [temp] ); 
            pos := PositionSet( simplices, boundaryComponent ); 
            Add( solution, pos );
        od;
        return solution;
    end; 
    
    L := MaximumList( List( chains, x -> Size(x[1])) ); # length of longest chain
    simplices := []; boundary := [];
    for i in [2..L] do
        simplices[i-1] := ShallowCopy( chains[i] );
        boundary[i-1] := []; 
        for simplex in simplices[i-1] do
            Add( boundary[i-1], boundaryOfChain( simplex, chains[i-1] ) );
        od;
    od; 
    return boundary;
end;


#### GENERATE COEFF AND CONJ (both 'standard') ####
# Generate list COEFF = alternating +-1's and
# list CONJ = constant 1's, out of list BOUNDARY (replacing
# elements by appropiate +-1)
generateCoeff := function( boundary )
    local coeff;
    coeff := List( boundary, x -> List(x, y -> List (y, z -> (-1)^(Position(y,z)+1) )));
    return coeff;
end;
generateConj := function( boundary )
    local conj;
    conj := List( boundary, x -> List(x, y -> List (y, z -> 1)));
    return conj;
end;

#### DIMENSION AND NUMBER OF SIMPLICES (out of collection of chains 'simplices) ####
getDimension := function(simplices)
    return MaximumList( List( simplices, x -> Size(x[1])) ) - 1; # length of longest chain
end;
getNumberOfSimplices := function(simplices)
    return List(simplices, Size);
end;


##########################################################
# DavisComplexes, a HAP sub-package in GAP
# by Ruben J. Sanchez-Garcia and Alexander D. Rahm,
#
# containing Ruben J. Sanchez-Garcia's implementation
# of the Davis complex for Coxeter groups.
#
##########################################################

CreateCoxeterMatrix:= function( numberOfGenerators, CoxeterMatrixEntries)
# CreateCoxeterMatrix := function( numberOfGenerators, CoxeterMatrixEntries)
local GEN, M, m, k, i, j;

   if IsInt(numberOfGenerators) and numberOfGenerators > 0 then
        GEN := numberOfGenerators;
   else
	return fail;
	#break;
   fi;
   if Size(CoxeterMatrixEntries) = GEN*(GEN-1)/2 then

        M := IdentityMat(GEN); #to be completed into a Coxeter matrix
	k := 1;
        for i in [1..GEN-1] do
            for j in [i+1..GEN] do
                m := CoxeterMatrixEntries[k];
		if IsInt(m) and m > -1 then
                    	M[i][j] := Int(m);
		    	k := k+1;
                else
			return fail;
			#break;
		fi;
            od;
        od;    
   else
	return fail;
	#break;
   fi;
#### COMPLETE COXETER MATRIX (symmetric) ####
   for i in [1..GEN] do
        for j in [1..i-1] do
            M[i][j] := M[j][i];
        od;
   od;
   return M;
end;   





##########################################################################
# Program to compute the maximal spherical subgroups of a Coxeter group
# The input is a Coxeter matrix M
# The output is a list of subsets of the Coxeter generators labeled 1..N
# Ruben J Sanchez-Garcia & Alexander Rahm
# GAP version 4.3 (TBU)
# May 2015
##########################################################################
        
MaximalSpherical:= function(M)
    local S, subset, max_spherical, temp_spherical, l, m, found;

    # Coxeter generators
    S := [1..Size(M)];

    # if spherical return all generators
    if IsSphericalCoxeterGroup(M) then
        return [S];
    fi;

    # if not, evaluate each subset recursively
    max_spherical := [];
    for subset in Combinations(S,Length(S)-1) do
        temp_spherical :=  MaximalSpherical(M{subset}{subset});
        # for each returned spherical subset, check is not yet a subset in max_spherical
        for l in temp_spherical do
            found := false; 
            for m in max_spherical do
                if IsSubset(m, subset{l}) then #use subset{l} to keep consistent numbering
                    found := true;
                    #break;
                fi;
            od;
            if not(found) then
                Append(max_spherical, [subset{l}]);
            fi;
        od;
    od;

    return max_spherical;
end;

##########################################################################
# Program to compute the combinatorial structure of the Davis complex
# The input is a Coxeter matrix M
# The output is a list of subsets of the Coxeter generators labeled 1..N
# Ruben J Sanchez-Garcia & Alexander Rahm
# GAP version 4.3 (TBU)
# May 2015
##########################################################################
        

# Subfunction chains
    # computes all chains of subsets of S
    chains := function(S)
        local partialSolution, Hijos, hijo, addChainLink;

        # subfunction - add maximal set to the chains 
        addChainLink := function( chains, set )
            local C, newChain, solution;
            solution := Set(ShallowCopy(chains));
            for C in chains do
                newChain := Set(ShallowCopy(C));
                AddSet( newChain, set );
                AddSet( solution, newChain );
            od;
            AddSet( solution, [set] );
            return solution;
        end;

        # Trivial case
        if Size(S) = 0 then
            return [Combinations(S)];
        fi;

        # Reduction to smaller cases: recursive call
        partialSolution := [];
        Hijos := Combinations( S, Size(S)-1 );
        for hijo in Hijos do
            Append( partialSolution, chains(hijo) );
        od;

        partialSolution := addChainLink(partialSolution, S);

        return partialSolution;
    end;

DavisComplexCombinatorial := function(MaxSpherical)
    local s, i, aux, DC, L;

    # adds all chains of each maximal spherical, deleting duplicates 
    aux := [];
    for s in MaxSpherical do
        aux := Set(Concatenation(aux,chains(s)));
    od;

    # filters chains by length
    DC:= [];
    L := Maximum(List(aux,Length)); # length of longest chain
    for i in [1..L] do
        DC[i] := Filtered( aux, C -> Size(C) = i );
    od;

    return DC;
end;


ExportAsContractibleGcomplex := function(CoxeterMatrix, DC, GEN, STAB, BOUNDARY, COEFF, CONJ, DIMENSION, numberOfCells)
local	j, CoxeterGenerators,
        G, StabilizerGroups, Stabilizer,
        lnth,
        dims,Dimension,
        Boundary,
        boundaryList,
        Elts,
	Rot,Stab,
        RotSubGroups,action, ActionRecord,
        TransMat,
        x, n,k,s,BI,SGN,tmp, LstEl;

# groupname:= Concatenation("Coxeter",String(GEN),"group");
# for i in [1..GEN-1] do
#      for j in [i+1..GEN] do
#           groupname :=  Concatenation( groupname,"-",String( CoxeterMatrix[i][j]));
#      od;
# od;  
# name:=groupname;

if HAP_GCOMPLEX_SETUP[1] then 
TransMat:=function(x) return x^-1; end;
else
TransMat:=function(x) return x; end;
fi;

lnth:=Size(DC)-1;

dims:=List([1..lnth+1],n->Size(DC[n]));

###################
Dimension:=function(n)
if n>lnth then return 0; fi;
return dims[n+1];
end;
###################

Elts:=[IdentityMat(GEN)];  
StabilizerGroups:=[];
RotSubGroups:=[];
boundaryList:=[];


#######
for n in [1..lnth+1] do
boundaryList[n]:=[];
StabilizerGroups[n]:=[];
RotSubGroups[n]:=[];
  for k in [1..Dimension(n-1)] do
  Append(Elts,Elements(STAB[n][k]));
  Add(StabilizerGroups[n],STAB[n][k]);
  Add(RotSubGroups[n],STAB[n][k]);
  od;
od;

CoxeterGenerators := [];
n := lnth; ## Use the stabilisers of the 
## (top-1 = n)-dimensional cells 
## as generators for the Coxeter group.
  for k in [1..Dimension(n-1)] do

  Append(CoxeterGenerators,Elements(STAB[n][k]));
  od;
####

Elts := SSortedList(Elts);
CoxeterGenerators := SSortedList(CoxeterGenerators);
CoxeterGenerators := Difference(CoxeterGenerators, [IdentityMat(GEN)]);

#######
for n in [1..lnth+1] do ## the cell dimension is n-1.
boundaryList[n]:=[];

  tmp := List([]);
  if n-1 > 0 then
    for j in [1..n] do
  	Append( tmp, [IdentityMat(GEN)]);
    od;		
  fi;
  LstEl:=tmp;

for k in [1..Dimension(n-1)] do
  if n-1 > 0 then   
       BI := BOUNDARY[n-1][k];
       SGN:=COEFF[n-1][k];
  else
       BI:= [];	
       SGN:= [];	
  fi;

   for s in [1..Length(BI)] do
          BI[s]:=[SGN[s]*BI[s], Position(Elts,LstEl[s])];
   od;
Add(boundaryList[n],BI);
od;
od;
####

ActionRecord:=[];
for n in [1..lnth+1] do
ActionRecord[n]:=[];
for k in [1..Dimension(n-1)] do
ActionRecord[n][k]:=[];
od;
od;

G:=Group(CoxeterGenerators);

####################
Boundary:=function(n,k)
if k>0 then
return boundaryList[n+1][k];
else
return NegateWord(boundaryList[n+1][-k]);
fi;
end;
####################

####################
Stabilizer:=function(n,k)
return StabilizerGroups[n+1][k];
end;
####################


####################
action:=function(n,k,g)
local id,r,u,H,abk,ans;

abk:=AbsInt(k);

if not IsBound(ActionRecord[n+1][abk][g]) then 
H:=StabilizerGroups[n+1][abk];

if Order(H)=infinity then ActionRecord[n+1][abk][g]:=1;
#So we are assuming that any infinite stabilizer group acts trivially!!
else
######
id:=CanonicalRightCosetElement(H,Identity(H));
r:=CanonicalRightCosetElement(H,Elts[g]^-1);
r:=id^-1*r;
u:=r*Elts[g];

if u in RotSubGroups[n+1][abk] then  ans:= 1;
else ans:= -1; fi;

ActionRecord[n+1][abk][g]:=ans;
fi;
######
fi;

return ActionRecord[n+1][abk][g];
end;

##########################
return Objectify(HapNonFreeResolution,
            rec(
            dimension:=Dimension,
            boundary:=Boundary,
            homotopy:=fail,
            elts:=Elts,
            group:=G,
            stabilizer:=Stabilizer,
            action:=action,
            properties:=
            [["length",Maximum(1000,lnth)],
             ["characteristic",0],
             ["type","resolution"],
             ["reduced",true]]  ));

end; ## end of function ExportAsContractibleGcomplex
################################################
################################################



#InstallGlobalFunction(DavisComplex, function(CoxMat)
#local DC, GEN, STAB, BOUNDARY, COEFF, CONJ, DIMENSION, numberOfCells;
    if not IsMatrix(CoxMat) then CoxMat:=CoxeterMatrix(CoxMat); fi;
    GEN := Size(CoxMat);
    DC:= DavisComplexCombinatorial(MaximalSpherical(CoxMat));

#### EXTRACT SPHERICAL SIMPLICES ####
    DC := extractSpherical( DC, GEN, CoxMat );
#### REMOVE ANY EMPTY LIST OF CHAINS ####
    DC := Difference( DC, [[]] );
#### GENERATE STABILIZERS ####
    STAB := generateStabs( DC, CoxMat );

#### GENERATE BOUNDARY ####
    BOUNDARY := generateBoundary( DC );

#### GENERATE COEFF AND CONJ (both 'standard') ####
    COEFF := generateCoeff( BOUNDARY );
    CONJ := generateConj( BOUNDARY );

#### DIMENSION AND NUMBER OF SIMPLICES ####
    DIMENSION := getDimension(DC);
    numberOfCells := getNumberOfSimplices(DC);

return ExportAsContractibleGcomplex(CoxeterMatrix, DC, GEN, STAB, BOUNDARY, COEFF, CONJ, DIMENSION, numberOfCells);
# end;
end);