This file is indexed.

/usr/share/gap/lib/solmxgrp.gi is in gap-libs 4r6p5-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
#############################################################################
##
#W  solmxgrp.gi			GAP Library		       Gene Cooperman
#W							     and Scott Murray
##
##
#Y  Copyright (C)  1996,  Lehrstuhl D für Mathematik,  RWTH Aachen,  Germany
#Y  (C) 1999 School Math and Comp. Sci., University of St Andrews, Scotland
#Y  Copyright (C) 2002 The GAP Group
##
##  REFERENCE:
##   E.M. Luks, ``Computing in solvable matrix groups'',
##   Proc. 33^{rd}$ IEEE Foundations of Computer Science (FOCS-33), 1992,
##   pp.~111-120.
##  (group membership and related algorithms (Size, Random, Enumerator, etc.)
##   currently implemented through abelian, and nilpotent,
##   will be extended to solvable in next release)
##
##  Cleaning up of code:
##    Testing for Cyclic and QuotientToAdditiveGroup should be
##       combined in routine:  if IsBaseCaseGroup(G) then ...
##    The method, InvariantSubspaceOrCyclicGroup, "for abelian non-char.
##       p-group" is too long and hence should be rewritten.
##

# InfoChain already declared.
#DeclareInfoClass("InfoChain");
# default info level is 0
#SetInfoLevel(InfoChain, 1);

#############################################################################
##
#F  SetIsCyclicWithSize( <G>, <gen>, <size> )
##
InstallGlobalFunction( SetIsCyclicWithSize,
    function(G, gen, size)
        if size = 1 then SetIsTrivial(G,true); return; fi;
        if IsOne(gen) then Error("internal error"); fi;
        SetIsCyclic(G,true);
        SetGeneratorOfCyclicGroup(G,gen);
        SetSize(G,size);
        IsPGroup(G); # It's cheap to test it now.
    end );

#############################################################################
##
#F  ConjugateMatrixActionToLinearAction( <g> )
##
InstallGlobalFunction( ConjugateMatrixActionToLinearAction, function(g)
    local i, j, d, ginv, zero, one, basisMatrix, newLinearMatrix;
    if not IsMatrix(g) then Error("Invalid input"); fi;
    d := Length(g);
    basisMatrix := List( [1..d], x->List([1..d],x->Zero(g[1][1])) );
    zero := Zero(g[1][1]);
    one := One(DefaultFieldOfMatrix(g));
    newLinearMatrix := [];
    ginv := g^(-1);
    for i in [1..d] do
        for j in [1..d] do
            basisMatrix[i][j] := one;
            Add( newLinearMatrix, Flat(ginv * basisMatrix * g) );
            basisMatrix[i][j] := zero;
        od;
    od;
    # return transpose for action, matrix * vec
    return TransposedMat( newLinearMatrix );
end );            

#############################################################################
##
#F  ConjugateMatrixGroupToLinearGroup( <G> )
##
ConjugateMatrixGroupToLinearGroup := function( G )
    return List( GeneratorsOfGroup( G ), g->[g, ConjugateMatrixActionToLinearAction(g) ] );
end;

#############################################################################
#############################################################################
##
##  Abelian matrix groups
##
#############################################################################
#############################################################################

#############################################################################
##
#M  MakeHomChain( <G> )
##
##  
##
InstallMethod( MakeHomChain, "for arbitrary group", true,
        [ IsGroup ], 0,
    function( G )
        # Test abelian first.  It's cheaper.
        if IsFFEMatrixGroup(G) and IsAbelian(G) then
            return MakeHomChain(G);
        elif IsFFEMatrixGroup(G) and IsNilpotentGroup(G) then
            return MakeHomChain(G);
        fi;
        Error("MakeHomChain currently implemented only for nilpotent groups");
    end );
InstallMethod( MakeHomChain, "for nilpotent group with chain", true,
               [ IsGroup and IsNilpotentGroup and HasChainSubgroup ], 0,
    G -> ChainSubgroup(G) );
InstallMethod( MakeHomChain, "for abelian group", true,
               [ IsGroup and IsAbelian ], 0,
    function( G )
        local PowerFnc, SetPGroup, pGroups, pgroupGens, otherPGens, first, pow, H;
        PowerFnc := power -> (g->g^power);
        SetPGroup := function( H, p, exponent)
            SetIsPGroup(H, true );
            SetPrimePGroup( H, p );
            SetExponent( H, exponent );
            if Length(GeneratorsOfGroup(H)) = 1 then
                SetIsCyclicWithSize( H, GeneratorsOfGroup(H)[1], exponent );
            fi;
            # UseSubsetRelation( G, H );
            return H;
        end;
        #Returns list of triples, [p,pgroupGens,exponentOfPGroup]
        pgroupGens := PGroupGeneratorsOfAbelianGroup( G );
        if Length(pgroupGens) = 0 then
            if not IsTrivial(G) then Error("internal error: not triv"); fi;
            SetIsTrivial( G, true );
            Info(InfoChain, 1, "Abelian group is trivial");
            return G;
        fi;
        if Length(pgroupGens) = 1 then
            SetPGroup( G, pgroupGens[1][1], pgroupGens[1][3] );
            Info(InfoChain, 1, "Abelian group is a p-group");
            return MakeHomChain( G );
        fi;
        Info(InfoChain, 1, "Making abelian chain as direct product of ",
                           Length(pgroupGens), " p-groups:  ",
                           List(pgroupGens,x->x[1]));
        pGroups := List( pgroupGens,
                         x -> SetPGroup(SubgroupNC(G,x[2]), x[1], x[3]) );

        # Be nice and tell GAP what we discovered, but don't pay cost
        #  of creating all the homomorphisms for embeddings and projections
        # If GAP knows about p-groups, it can do the homomorphisms on demand.
        first := [1];
        ForAll( pgroupGens,
                function( x ) Add( first, first[Length(first)]+Length(x[2]) );
                              return true;
                end );
        # MODIFYING ORIGINAL GENERATORS OF G; MAKE SURE THIS IS SHALLOW COPY
        # MySetGeneratorsOfGroup( G, Concatenation( List(pgroupGens, x->x[2]) ) );
        G!.PGroupGenerators := Concatenation( List(pgroupGens, x->x[2]) );

        while Length(pgroupGens) > 1 do
            SetDirectProductInfo( G,
                rec( groups := pGroups, first := first,
                     embeddings := [], projections := [] ) );
            otherPGens := pgroupGens{[2..Length(pgroupGens)]};
            if Length(pgroupGens) > 2 then
                H := SubgroupNC(G, Concatenation(List(otherPGens, x->x[2])));
            else H := pGroups[Length(pGroups)];
            fi;
            UseSubsetRelation( G, H );
            pow := ChineseRem( List(pgroupGens,x->x[3]),
                               Concatenation([1], List(otherPGens,x->0)) );
            if pow = 1 then Error("pow = 1, identity projection"); fi;
            ChainSubgroupByProjectionFunction( G, H, pGroups[1],
                                              PowerFnc(pow) );
            # This should be inside ChainSubgroupByProjectionFunction()
            MakeHomChain( QuotientGroup( Transversal( H ) ) );
            G := H;
            pgroupGens := otherPGens;
            pGroups := pGroups{[2..Length(pGroups)]};
            first := first{Flat([1,[3..Length(first)]])} + 1 - first[2];
            first[1] := 1;
        od;
        return MakeHomChain(H);
    end );

#############################################################################
##
#M  BasisOfHomCosetAddMatrixGroup( <> )
##
## GAP has V := VectorSpace(FieldOfMatrixGroup(quo), GeneratorsOfGroup(quo));
##    but how does one bootstrap up to get Dimension(V) and Basis(V)?
##    This may go away when there's a clearer way to do it in GAP.
##    This should work for IsAdditiveQuotientGroup, IsAdditiveGroup,
##    and IsFFEMatrixGroup
## LeftModuleByGenerators() also works, but again, GAP refuses to find
##     a basis for it.
## SemiEchelonBasis(V) fails with UseSubsetRelation(arg[1], S);
##
InstallGlobalFunction( BasisOfHomCosetAddMatrixGroupFnc,
function( G )
     local gens, oneOfGroup, g, v, c, basis, residue, firsts, tmp,
           b, i, fld, one, zero, fldSize, MyIntFFE, MyAdditiveOrder;
     # A better way (valid for additive groups, too) is:
     # field := Field( GeneratorsOfNearAdditiveGroup(G));
     # one := One(field);
     # why does FieldOfMatrixGroup work? IsFFEMatrixGroup(G)?
     if IsFFEMatrixGroup(G) then
         gens := GeneratorsOfGroup(G);
         oneOfGroup := One(G);
         fld := FieldOfMatrixGroup(G);
     elif IsAdditiveGroup(G) and HasGeneratorsOfNearAdditiveGroup(G) then
         gens := GeneratorsOfNearAdditiveGroup(G);
         oneOfGroup := Zero(G);
         if not IsEmpty(gens) then fld := Field(Flat(gens));
         else fld := Field(Flat(One(G)));
         fi;
     else Error("can't handle this case");
     fi;
     one := One(fld);
     zero := Zero(fld);
     fldSize := Size(fld);
     # HACK:  until GAP fixes IntFFE(); returns fail if impossible (not error)
     MyIntFFE := f -> First([0..fldSize], i -> f=i*one);
     basis := [];
     residue := [];
     firsts := [];
     for g in gens do
         v := Flat(g);
         for i in [1..Length(basis)] do
             c := PositionNot( v, zero );
             if c > Length(v) then break; fi; # g is now zero vector
             b := Flat(basis[i]); # for finding b[c];  Faster without Flat()
             c := PositionNot( b, zero );
             c := MyIntFFE( v[c] / b[c] );
             if c = fail then # then switch gen with current basis vector
                 tmp := g; g := basis[i]; basis[i] := tmp;
                 tmp := v; v := b; b[i] := tmp;
                 c := MyIntFFE( v[c] / b[c] );
                 if c = fail then Error("internal error"); fi;
             fi;
             g := g - c * basis[i];
             v := Flat(g);
         od;
         if g = oneOfGroup then Add(residue,g); # One(G) is 0 matrix here
         else Add(basis,g);
         fi;
     od;
     Sort(basis);  # GAP Sort works by side effect, only.
     basis := Reversed(basis);
     if IsFFEMatrixGroup(G) or IsQuotientToAdditiveGroup(G) then
         SetSize( G, Product( basis, Order ) );
     elif IsAdditiveGroup(G) then # What's GAP for Order() of elt in add. grp?
         #GAP should have Size() method for IsAdditiveGroup as below:
         # SizeOfChainOfGroup() calls this for now.
         MyAdditiveOrder := function(g)
             if IsZero(g) then return 1;
             else return Size(DefaultFieldOfMatrix(g));
             fi;
         end;
         SetSize( G, Product( basis, MyAdditiveOrder ) );
     fi;
     firsts := List(basis, v->PositionNot(v,zero));
     return rec(basis := basis, firsts := firsts, residue := residue);
end );

# This should eventually generalize to something like:
#    BasisOfAdditiveMatrixGroup, for which both of these are example.
InstallMethod( BasisOfHomCosetAddMatrixGroup, "by linear algebra", true,
    [ IsGroup and IsQuotientToAdditiveGroup ], 0,
    BasisOfHomCosetAddMatrixGroupFnc );
InstallMethod( BasisOfHomCosetAddMatrixGroup, "by linear algebra", true,
    [ IsAdditiveGroup ], 0, BasisOfHomCosetAddMatrixGroupFnc );

#############################################################################
##
#F  SiftVector( <basisVecList>, <vec> )
#F  SiftVector( <basisVecList> )
##
InstallGlobalFunction( SiftVector, function(arg)
    local basisVecs, b, firsts, zero, one, fldSize, MyIntFFE, fnc;
    # HACK:  until GAP fixes IntFFE(); returns fail if impossible (not error)
    MyIntFFE := f -> First([0..fldSize], i -> f=i*one);
    basisVecs := arg[1];
    b := List(basisVecs, Flat);
    zero := Zero(b[1][1]);
    one := One(b[1][1]);
    fldSize := Size(Field(Flat(b)));
    firsts := List( b, i->PositionNot(i,zero) );
    if 0 in firsts then Error("internal error"); fi;

    fnc := function(vec)
        local i, c, v;
        for i in [1..Length(b)] do
            v := Flat(vec);  # time for Flat dominated by arithmetic below
            c := MyIntFFE( v[firsts[i]] / b[i][firsts[i]] );
            if c = fail then return fail;
            else vec := vec - c * basisVecs[i];
            fi;
        od;
        return vec;
    end;

    if Length(arg)=2 then return fnc(arg[2]); else return fnc; fi;
end );

#############################################################################
##
#M  SiftFunction( <> )
##
##  
##
InstallMethod( SiftFunction,
    "for abelian quotient to additive group (by lin. algebra)", true,
    [ IsGroup and IsFFEMatrixGroup and IsQuotientToAdditiveGroup ], 0,
    G -> SiftVector( BasisOfHomCosetAddMatrixGroup(G).basis ) );

#############################################################################
##
#M  MakeHomChain( <> )
##
##  
##
InstallMethod( MakeHomChain, "by linear algebra", true,
    [ IsGroup and IsFFEMatrixGroup and IsQuotientToAdditiveGroup ], 0,
function( G )
     local SiftFnc;
     SiftFnc := SiftFunction(G);
     Info(InfoChain, 2, "Extending chain by kernel of abelian image.");
     # This is different from homomorphism transversal.
     # In hom transv, we induce a quotient group
     # Here we want a simple sift, in fact from quotient group to ord. grp.
     ChainSubgroupBySiftFunction( Source(G),
     KernelOfMultiplicativeGeneralMapping(G),
                         g->SourceElt(SiftFnc(HomCoset(Homomorphism(G),g))) );
     if HasSize(G) then SetSize(TransversalOfChainSubgroup(Source(G)), Size(G)); fi;
     if IsTrivial(KernelOfMultiplicativeGeneralMapping(G)) then return
     KernelOfMultiplicativeGeneralMapping(G); fi;
     return MakeHomChain(KernelOfMultiplicativeGeneralMapping(G));
end );

##  Currently, Cyclic and QuotientToAdditiveGroup are manageable.
##  If the argument, G, is already a manageable group, it returns G, itself.
ManageableQuotientOfAbelianPGroup :=
    function( G )
        local subspace, hom, V, fnc, kernel, quo, quo2, grp;
        Info(InfoChain, 2, "Making abelian ", PrimePGroup(G), "-group chain");
        if IsQuotientToAdditiveGroup(G) then # base case
            Error("internal error:  base case");
        fi;
        if ForAny(GeneratorsOfGroup(G), IsZero) then
            Error("internal error:  zero matrix");
        fi;
        if not IsPGroup(G) or not IsAbelian(G) then Error("wrong arg"); fi;
        subspace := InvariantSubspaceOrCyclicGroup( G );
        if IsVectorSpace( subspace ) then
          Info(InfoChain, 2, "Invariant subspace of rank ",
                 Dimension(subspace), " in dimension ",
                 Length(GeneratorsOfVectorSpace(subspace)[1]), " found.");
          Info(InfoChain, 2, "Trying action on invariant subspace");
          hom := NaturalHomomorphismByInvariantSubspace
                                                          ( G, subspace );
          if ForAll( GeneratorsOfGroup(G), g -> IsOne(ImageElm(hom,g)) ) then
             Info(InfoChain, 2, "Trying action on quotient of invar. subspace");
             hom := NaturalHomomorphismByFixedPointSubspace
							  ( G, subspace );
          fi;
          if ForAll( GeneratorsOfGroup(G), g -> IsOne(ImageElm(hom,g)) ) then
              Info(InfoChain, 2, "Trying homomorphism to Hom(V,W)");
              hom := NaturalHomomorphismByHomVW( G, subspace );
              Info(InfoChain, 2, "Creating QuotientToAdditiveGroup");
          fi;
          ChainSubgroupByHomomorphism( hom );
          quo := QuotientGroup(TransversalOfChainSubgroup(G));
          # After calling this, we might discover quo is cyclic.
          IsAbelian(quo);  # Tell GAP quo is abelian in case not propagated.
          # MakeHomChain(quo); # this need only be ChainSubgroup(quo);
          if IsQuotientToAdditiveGroup(quo) then return quo; fi;
          quo2 := ManageableQuotientOfAbelianPGroup(quo);
          if HasIsCyclic(quo) and IsCyclic(quo) then
              # MakeHomChain(quo);
              return quo;
          fi;
          return QuotientGroupByChainHomomorphicImage(quo, quo2);
        else
          Info(InfoChain, 2, PrimePGroup(G), "-group is cyclic.");
          if not HasGeneratorOfCyclicGroup(subspace) then
              Error("internal error:  cyclic group missing single generator");
          fi;
          SetIsCyclicWithSize( G, GeneratorOfCyclicGroup(subspace),
                                  Size(subspace) );
          return G;
        fi;
    end;

#############################################################################
##
#M  MakeHomChain( <> )
##
##GDC - Problem:  Really, this should apply only if it's not
##       HomCosetAddRep.  However, ordinary groups are okay.
##       I'd really like a property:  IsFFEMatrixGroup and IsNotHomCosetAddGroup
##
InstallMethod( MakeHomChain, "for abelian p-group", true,
    [ IsGroup and IsFFEMatrixGroup and IsAbelian and IsPGroup ], 0,
    function( G )
        local quo, kernel;
        quo := ManageableQuotientOfAbelianPGroup(G);
        if IsIdenticalObj(quo,G) then # then HasGeneratorOfCyclicGroup(G)
            MakeHomChain(G);
            return quo; # then not a quotient grp
        else
          IsAbelian(quo); # Special Kernel method for abelian grp
          Info(InfoChain, 2, "Finding kernel of quotient group acting on",
                          " subspace of dimension ",
                          DimensionOfMatrixGroup(quo) );
	  # sets KernelOfMultiplicativeGeneralMapping(Homomorphism(quo))
          kernel := KernelOfHomQuotientGroup(quo); 
          # Now that we have the full kernel, make new ChainSubgroup(grp)
          ChainSubgroupByHomomorphism( Homomorphism(quo) );
          if IsTrivial(kernel) then return kernel;
          else return MakeHomChain( kernel );
          fi;
        fi;
    end );

#############################################################################
##
#M  MakeHomChain( <> )
##
##  We need IsFFEMatrixGroup, or we lose to IsFFEMatrixGroup and IsAbelian
##
InstallMethod( MakeHomChain, "for cyclic p-groups", true,
    [ IsGroup and IsFFEMatrixGroup and IsCyclic and IsPGroup ], 0,
    function( G )
        if IsUniformMatrixGroup( G ) or HasGeneratorOfCyclicGroup( G ) then
            return ChainSubgroupBySiftFunction( G, TrivialSubgroup(G),
                                                SiftFunction( G ) );
        else TryNextMethod(); return;
        fi;
    end );


#############################################################################
#############################################################################
##
##  Abelian matrix p-groups:
##
#############################################################################
#############################################################################

#############################################################################
##
#M  InvariantSubspaceOrCyclicGroup( <H> )
##
##  Lemma 4.4 of Luks reference:  returns proper invariant subspace
##     or return isomorphic cyclic group with GeneratorOfCyclicGroup
##     attribute and Size attribute set
##
InstallMethod( InvariantSubspaceOrCyclicGroup, "for abelian group", true,
    [ IsFFEMatrixGroup and IsAbelian ], 0,
    function( H )
        if Length( GeneratorsOfGroup(H) ) = 1 then
            SetIsCyclicWithSize( H, H.1, Order(H.1) );
            return InvariantSubspaceOrCyclicGroup( H );
        else TryNextMethod(); return;
        fi;
    end );
InstallMethod( InvariantSubspaceOrCyclicGroup, "for abelian p-group", true,
    [ IsFFEMatrixGroup and IsAbelian and IsPGroup ], 0,
    function( H )
        IsCharacteristicMatrixPGroup( H );  # Have GAP decide true or false
        return InvariantSubspaceOrCyclicGroup( H );
    end );
InstallMethod( InvariantSubspaceOrCyclicGroup, "for trivial group", true,
    [ IsTrivial ], 0, H -> H );
InstallMethod( InvariantSubspaceOrCyclicGroup, "for abelian char. p-group",true,
    [ IsFFEMatrixGroup and IsAbelian and IsPGroup and IsCharacteristicMatrixPGroup ], 0,
    function( H )
    local gen, gens, space;

    space := UnderlyingVectorSpace(H);
    for gen in GeneratorsOfGroup( H ) do
        # Must first test IsTrivial(space) due to bug in GAP-4r1
        if not IsTrivial(space) then
            space := Intersection2( space, FixedPointSpace( gen ) );
        fi;
    od;
    # This is because char(H) = p
    if space = TrivialSubspace( space ) then
        Error("This shouldn't occur in characteristic case.");
        SetIsCyclic(H,true);
        return H;
    fi;
    return space;
end );
##
##      This method is too long.  It should now be a short routine that
##      calls InvariantSubspaceOrUniformCyclicPGroup()
##      followed by SiftFunction() for cyclic matrix p-Group.  - Gene
##
InstallMethod( InvariantSubspaceOrCyclicGroup, "for abelian non-char. p-group",
    true,
    [ IsFFEMatrixGroup and IsAbelian and IsPGroup and IsNoncharacteristicMatrixPGroup ],
    0,
    function( H )
    local Horig, p, gens, tmp, h, k, h1, k1, ordH, ordK, h1inv,
          space, trivSpace, r, CopyGroup, MySetGeneratorsOfGroup;

 ##  NOTE:  ShallowCopy(G) silently refuses to make a copy of G.
 CopyGroup := function( G )
    local H;
    H := Group( GeneratorsOfGroup(G) );
    # This should SetPrimePGroup() for H
    UseIsomorphismRelation( G, H );
    return H;
 end;

 # We should not be doing this -- Scott.
 # Agreed.  The usage here is to pass in the "shell of a group", and
 # recursively add generators to the shell, to avoid the overhead of
 # destructively modifying a generator list, and constantly making
 # temporary groups based on it.  When this function is rewritten,
 # we can remove this.		-- Gene.
 MySetGeneratorsOfGroup :=
    function(G,gens) G!.GeneratorsOfMagmaWithInverses := gens; end;

    Horig := H;
    p := PrimePGroup( Horig );
    H := CopyGroup(Horig);
    # SET UP PROBLEM
    gens := GeneratorsOfGroup( H );
    tmp := Filtered( gens, g -> not IsOne(g) );
    if Length(tmp) < Length(gens) then
        gens := tmp;
        MySetGeneratorsOfGroup( H, gens );
    fi;
    if Length(gens) < 2 then
        if Length(gens) = 0 then SetIsTrivial( H, true );
        else SetIsCyclicWithSize( Horig, gens[1], Order(gens[1]) );
        fi;
        return InvariantSubspaceOrUniformCyclicPGroup( Horig );
    fi;
    h := gens[1];
    k := gens[2];
    ordH := Order(h);
    ordK := Order(k);
    if ordH < ordK then
        tmp := h;
        h := k;
        k := tmp;
        tmp := ordH;
        ordH := ordK;
        ordK := tmp;
    fi;
    # ALGORITHM
    h1 := h^(ordH/p);
    k1 := k^(ordK/p);
    space := FixedPointSpace( h1 );
    trivSpace := TrivialSubspace(space);
    if trivSpace <> space then return space; fi;
    h1inv := h1^(-1);
    for r in [0..p-1] do
        space := FixedPointSpace( h1inv^r*k1 );
        if space <> trivSpace then
            if space <> UnderlyingVectorSpace(H) then
                return space;
            else break;
            fi;
        fi;
    od;
    if space = trivSpace then Error("internal error: no FixedPointSpace"); fi;
    if k1 <> h1^r then Error("internal error:  k1 <> h1^r"); fi;
    tmp := h^((-r)*ordH/ordK) * k;
    if IsOne( tmp ) then
        gens := Concatenation( [h], gens{[3..Length(gens)]} );
    else gens := Concatenation( [h, tmp], gens{[3..Length(gens)]} );
    fi;
    # Change generating set of this group:
    MySetGeneratorsOfGroup( H, gens );
    space := InvariantSubspaceOrCyclicGroup( H );
    return space;
    if IsVectorSpace(space) then return space;
    else # else space is really a cyclic group.
      SetIsCyclicWithSize( Horig, GeneratorOfCyclicGroup(space), Size(space) );
      return Horig;
    fi;
    # return InvariantSubspaceOrCyclicGroup( AsSubgroup( H, Group(gens) ) );
end );

#############################################################################
##
#M  InvariantSubspaceOrUniformCyclicPGroup( <G> )
##
##  Matrix group is uniform if fixed point space of every element
##    is either the trivial space or the entire space.
##
InstallMethod(InvariantSubspaceOrUniformCyclicPGroup, "for matrix group", true,
[IsFFEMatrixGroup], 0,
    function( G )
    local p, gens, g, space;
    if not (IsFFEMatrixGroup and IsCyclic and IsPGroup) then
        Error("implemented only for cyclic matrix p-groups");
    fi;
    p := PrimePGroup(G);
    if p=fail then
        # the group is trivial
        SetIsUniformMatrixGroup( G, true );
        return G;
    fi;
    if HasGeneratorOfCyclicGroup(G) then gens := [ GeneratorOfCyclicGroup(G) ];
    else gens := GeneratorsOfGroup(G);
    fi;
    for g in gens do
        if not IsOne(g) then
            space := FixedPointSpace( g^(Order(g)/p) );
            if space <> UnderlyingVectorSpace(g)
               and Dimension(space) <> 0 then
                return FixedPointSpace( g^(Order(g)/p) );
            fi;
        fi;
    od;
    SetIsUniformMatrixGroup( G, true );
    return G;
end);

#############################################################################
##
#M  SiftFunction( <> )
##
##  For group of size $p^r$, performs in $r p$ multiplies and 
##     uses O(1) space.  Alternative is $r\log p$ multiplies
##     storing $r\log p$ matrices via Schreier tree.
##  Comment below shows how to turn it into $r \log p$ multiplies
##     while storing $\log p$ vectors.
##
InstallMethod( SiftFunction, "for cyclic matrix p-groups", true,
    [ IsGroup and IsFFEMatrixGroup and IsCyclic and IsPGroup ], 0,
    function( H )
    local gens, cyclicGen, ordCyclicGen, p,
        cyclicGen1, cyclicGen1inv, space, 
	underlyingVectorSpace, trivSpace, SiftFnc;
    gens := Filtered( GeneratorsOfGroup(H), g -> not IsOne(g) );
    if Length(gens) = 0 then return k -> k; fi;
    cyclicGen := GeneratorOfCyclicGroup( H );
    ordCyclicGen := Order( cyclicGen );
    p := PrimePGroup( H );
    underlyingVectorSpace := UnderlyingVectorSpace( H );
    trivSpace := TrivialSubspace( underlyingVectorSpace );
    #if not IsUniformMatrixGroup( H ) then Error("not uniform matrix grp"); fi;
        cyclicGen1 := cyclicGen^(ordCyclicGen/p);
        # space := FixedPointSpace( cyclicGen1 );
        # if space = trivSpace then 
        #     Error("cyclicGen is identity");
        # elif space <> underlyingVectorSpace then
        #     Error("matrix group is not uniform");
        # fi;
        cyclicGen1inv := cyclicGen1^(-1);
    # PRODUCE SIFT FUNCTION
    SiftFnc := function( k )
        local ordK, space, k1, tmp, r;
        ordK := Order(k);
        if ordK = 1 then return k; fi; # k is identity
        if ordCyclicGen mod ordK <> 0 then return k; fi; # k not in group
        k1 := k^(ordK/p);
        tmp := k1;
        # Saving image of base vector of < cyclicGen > would
        #     allow one to quickly find r.  So, this part could
        #     use our Random Schreier Sims code.
        for r in [0..p-1] do
            if IsOne(tmp) then break; fi;
            # NOW:  tmp = cyclicGen1inv^r * k1
            # space := FixedPointSpace( tmp );
            # if space <> trivSpace then
            #     if space = underlyingVectorSpace then break;
            #     else return k; # H uniform.  So tmp not in H
            #     fi;
            # fi;
            tmp := tmp * cyclicGen1inv;
        od;
        # if space = trivSpace then Error("cyclicGen1 is identity"); fi;
        if not IsOne(tmp) then return k; fi;
        # NOW:  cyclicGen1^r = k1
        tmp := cyclicGen^((-r)*ordCyclicGen/ordK) * k;
        # NOW:  Order(k)/Order(tmp) >= p
        if IsOne(tmp) then return tmp;
        else return SiftFnc(tmp);
        fi;
    end;
    return SiftFnc;
end );


#############################################################################
#############################################################################
##
##  Normal closure and Kernel of quotient group:
##
#############################################################################
#############################################################################

##  Create normal closure with a chain.
NormalClosureByChain := function(grp, subgp)
    local gens, h, g, count, tmp, x;
    if IsTrivial(subgp) then return subgp; fi;
    if IsAbelian(grp) then return subgp; fi;
    # Take randomized generators of normal closure
    subgp := SubgroupNC( grp,
                         List([1..5], i->RandomNormalSubproduct(grp,subgp)) );
    #Test if subgp is cyclic:
    # Routines about should be used to add more effic. method for IsCyclic(G)
    #   for GAP matrix groups.
    # This part can be slow, because GAP may use NiceObject() to compute Size()
    if HasSize(subgp) then
        g := First( GeneratorsOfGroup(subgp), h->Order(h)=Size(subgp) );
    else
        #This part can be simplified when NiceObject() isn't default.
        g := fail;
        for x in GeneratorsOfGroup(subgp) do
            tmp := Group([x]);
            MakeHomChain(tmp);
            if First(GeneratorsOfGroup(subgp), h->not IsOne(Sift(tmp,h)))
               = fail then
                g := x;
                break;
            fi;
        od;
    fi;
    if g <> fail then SetGeneratorOfCyclicGroup( subgp, g ); fi;
    # Form subgroup chain
    MakeHomChain(subgp);
    # Deterministically test it and extend it
    gens := List(GeneratorsOfGroup(subgp));
    for h in gens do
        for g in GeneratorsOfGroup(grp) do
            #Current GAP default for IN can call NiceObject()
            if not IsOne(Sift(subgp,h^g)) then
            # if not h^g in subgp then
		Add( gens, h^g );
                subgp := Group(gens);
                MakeHomChain(subgp);
            fi;
        od;
    od;
    return subgp;
end;

#############################################################################
##
#M  KernelOfHomQuotientGroup( <> )
##
##This should be generally useful in GAP.  It finds the kernel of
##   any homomorphism to an abelian group.
##
##This would be more efficient if we picked out a non-redundant
##   (independent) generating set for the abelian group, and then
##   used commutator relations on only those.  GAP has function
##   IndependentGeneratorsOfAbelianGroup() of unknown efficiency.
##   Or, we could program it ourselves.
##
InstallMethod( KernelOfHomQuotientGroup,
        "for abelian quotient group via presentation", true,
        [ IsHomQuotientGroup and IsAbelian ], 0,
    function( quo )
        local indGenSet, gens, gens2, SiftFnc, hom, rels, srcGrp, kerGrp ;

        # if IsQuotientToAdditiveGroup(quo) then
        #     indGenSet := BasisOfHomCosetAddMatrixGroup(quo);
        #     gens2 := indGenSet.basis;
        #     SiftFnc := SiftVector(indGenSet.basis);
        #     rels := indGenSet.residue;
        # else Error("KernelOfMultiplicativeGeneralMapping() not implemented for this case.");
        # fi;
        # Append( rels, List( gens2, SiftFnc ) );
        # If all source groups of quo are abelian, this is unnec.
        # Append(rels, ListX( gens2, gens2,
        #                     function(g1,g2) return Comm(g1,g2); end ) );
        # if not ForAll(rels, IsOne) then
        #     Error("internal error: invalid relation of presentation");
        # fi;

        #gens := GeneratorsOfGroup(quo);
        #rels := List( gens, g -> Sift(quo,g) );
        #if not ForAll(rels, IsOne) then
        #    Error("internal error: invalid relation of presentation");
        #fi;
        #rels := List( rels, g -> SourceElt(quo,g) );
        #gens2 := List( gens2, g->SourceElt(g) );
        #Append(rels, ListX(gens2,gens2,function(g1,g2) return Comm(g1,g2);end));

        IsPGroup( quo ); # Have GAP check this.

        srcGrp := Source(Homomorphism(quo));
        # TrivialQuotientSubgroup is where the presentations are formed.
        kerGrp := TrivialQuotientSubgroup( quo );
        # gdc - can't use Source(kerGrp) here.  Note bug in quotientgp.gi
        kerGrp := Group(List(GeneratorsOfGroup(kerGrp), g->SourceElt(g)));
        kerGrp := NormalClosureByChain( srcGrp, kerGrp );
        if not IsTrivial(kerGrp) then
            kerGrp := Group( Filtered( GeneratorsOfGroup(kerGrp),
                                       g -> not IsTrivialHomCoset(g) ) );
        fi;
        UseSubsetRelationNC(srcGrp,kerGrp);
        hom := Homomorphism(quo);
        SetKernelOfMultiplicativeGeneralMapping( hom, kerGrp );
        if IsTrivial(kerGrp) then Info(InfoChain, 2,
                                         "  (kernel is trivial)\n");
        fi;

        if HasSize(kerGrp) and HasSize(srcGrp) and 
           Size(kerGrp) = Size(srcGrp) then
            Error("internal error:  kernel not smaller");
        fi;
        return kerGrp;
end );

##  InstallMethod( KernelOfMultiplicativeGeneralMapping, "Monte Carlo algorithm for quotient group", true,
##          [ IsTransvByHomomorphism ], 0,
##      function( transv )
##      local hom, G, i, gens;
##      hom := Homomorphism(transv);
##      G := Source( hom );
##      gens := [];
##      for i in [1..15] do  # HACK
##          Add(gens, SiftOneLevel( transv, PseudoRandom(G) ) );
##      od;
##      # ChainSubgroup(G) is already kernel of hom;  Can set relations now.
##      if not HasKernelOfMultiplicativeGeneralMapping(hom) then
##          Error("internal error:  missing kernel to hom");
##      fi;
##      MySetGeneratorsOfGroup( KernelOfMultiplicativeGeneralMapping(hom), gens );
##      UseSubsetRelation( Source(hom), KernelOfMultiplicativeGeneralMapping(hom) );
##      UseFactorRelation( Source(hom), KernelOfMultiplicativeGeneralMapping(hom), Image(hom) );
##      UseIsomorphismRelation( Image(hom), QuotientGroup(transv) );
##      return KernelOfMultiplicativeGeneralMapping(hom);
##  end);



#############################################################################
#############################################################################
##
##  Cyclic matrix p-groups:
##  Exports:  Size, IN, Random, Enumerator, Sift
##  Internal:  GeneratorOfCyclicGroup, TrivialQuotientSubgroup (presentation)
##
#############################################################################
#############################################################################
        
#GeneratorOfCyclicGroup() only implemented currently for cases
#  needed by solmxgrp.gi;  solmxgrp.gi purposely doesn't compute
#  it in the general case --- because it is sometimes more efficient
#  to find an invariant subspace and recurse.
CanFindGeneratorOfCyclicGroup := function(G)
    if HasGeneratorOfCyclicGroup(G) then return true;
    elif Length(GeneratorsOfGroup(G)) = 1 then return true;
    elif IsFFEMatrixGroup(G) and HasIsCyclic(G) and IsCyclic(G) and
      HasIsPGroup(G) and IsPGroup(G) and
       IsUniformMatrixGroup(G) and IsNoncharacteristicMatrixPGroup(G) then
        return true;
    else return false;
    fi;
end;

#############################################################################
##
#M  Size( <G> )
##
InstallMethod( Size, "for cyclic matrix p-group", true,
        [ IsFFEMatrixGroup and IsCyclic and IsPGroup ], NICE_FLAGS+10,
        function (G)
            if CanFindGeneratorOfCyclicGroup(G) then
                return Order( GeneratorOfCyclicGroup( G ) );
            else TryNextMethod(); return;
            fi;
        end );
InstallMethod( Size, "for cyclic 1-gen. group", true,
        [ IsGroup and IsCyclic and HasGeneratorOfCyclicGroup ], NICE_FLAGS+10,
        G -> Order( GeneratorOfCyclicGroup( G ) ) );

#############################################################################
##
#M  Random( <G> )
##
InstallMethod( Random, "for cyclic matrix p-group", true,
        [ IsFFEMatrixGroup and IsCyclic and IsPGroup ], 0,
        function (G)
            if CanFindGeneratorOfCyclicGroup(G) then
                return GeneratorOfCyclicGroup( G )^Random([1..Size(G)]);
            else return; TryNextMethod();
            fi;
        end );

#############################################################################
##
#M  TrivialQuotientSubgroup( <G> )
##
##  Works on any group, but IsOne(gen) for all generators, gen
##  Useful for SourceElt(gen) if group is a quotient group.
##
InstallMethod( TrivialQuotientSubgroup,
        "for cyclic matrix p-group via presentation (assuming sift fnc)", true,
        [ IsFFEMatrixGroup and IsCyclic and IsPGroup ], 0,
        G -> SubgroupNC( G,
                # NOTE: Sift(G,g) = Sift(G)(g).  Should pre-compute Sift(G).
                Concatenation( List( GeneratorsOfGroup( G ), g->Sift(G,g) ),
                               # presentation for independent generators
                               [GeneratorOfCyclicGroup( G )^Size(G)] )));

#############################################################################
##
#M  Enumerator( <G> )
##
InstallMethod( Enumerator, "for cyclic matrix p-group", true,
        [ IsFFEMatrixGroup and IsCyclic and IsPGroup ], NICE_FLAGS,
        function (G)
          if CanFindGeneratorOfCyclicGroup(G) then
              return List( [0..Size(G)-1], i->GeneratorOfCyclicGroup(G)^i );
          else TryNextMethod(); return;
          fi;
        end );

#############################################################################
##
#M  IN( <G> )
##
InstallMethod( IN, "for cyclic matrix p-group", true,
        [ IsMultiplicativeElementWithInverse,
          IsFFEMatrixGroup and IsCyclic and IsPGroup ], NICE_FLAGS,
        function(g, G) return Sift(G, g) = One(G); end );

##
##  These next two do all the real work:
##

#############################################################################
##
#M  Sift( <G> )
##
InstallMethod( Sift, "for cyclic matrix p-group", true,
        [ IsFFEMatrixGroup and IsCyclic and IsPGroup and HasGeneratorOfCyclicGroup,
          IsMultiplicativeElementWithInverse ], 0,
        function(G, g) return SiftFunction(G)(g); end );


#############################################################################
#############################################################################
##
##  General abelian matrix group: (certain operations only)
##
#############################################################################
#############################################################################

##  gens must be IndependentAbelianGenerators
EnumerateIndependentAbelianProducts := function( G, gens )
        local first, rest;
        if Length(gens) = 0 then return One(G); fi;
        first := List( [0..Order(gens[1])-1], i->(gens[1])^i );
        if Length(gens) = 1 then return first;
        else
            rest := EnumerateIndependentAbelianProducts
                              ( G, gens{[2..Length(gens)]} );
            return ListX( first, rest, function(h,g) return h*g; end );
        fi;
end;

#############################################################################
##
#M  Enumerator( <G> )
##
InstallMethod( Enumerator, "for quotient to additive group", true,
        [ IsGroup and IsFFEMatrixGroup and IsQuotientToAdditiveGroup ],
        2*SUM_FLAGS+46,  # need to beat "system getter"
        G -> EnumerateIndependentAbelianProducts
                    (G, BasisOfHomCosetAddMatrixGroup(G).basis) );

#############################################################################
##
#M  Sift( <G>, <g> )
##
InstallMethod( Sift, "for quotient to additive group", true,
        [ IsGroup and IsFFEMatrixGroup and IsQuotientToAdditiveGroup,
          IsHomCosetToAdditiveElt ], 0,
        function(G, g) return SiftFunction(G)(g); end );

#############################################################################
##
#M  TrivialQuotientSubgroup( <G> )
##
##  Works on any group, but primarily useful for quotient groups
##  IsOne(gen) for all generators, gen,
##    but SourceElt(gen) is non-trivial for a general quotient group.
##
InstallMethod( TrivialQuotientSubgroup,
        "for abelian matrix group via presentation (assuming Sift fnc)", true,
        [ IsFFEMatrixGroup and IsAbelian ], 0,
    function(G)
        local gens;
        gens := IndependentGeneratorsOfAbelianMatrixGroup(G);
        return SubgroupNC( G,
                Concatenation( List( GeneratorsOfGroup(G), g->Sift(G,g) ),
                               # presentation for independent generators
                               List( gens, g->g^Order(g) ),
                               ListX( gens, gens,
				      function(g1,g2) return Comm(g1,g2); end )
                             ));
end );


#############################################################################
#############################################################################
##
##  Additive abelian group:
##
#############################################################################
#############################################################################

#############################################################################
##
#M  TrivialQuotientSubgroup( <quo> )
##
InstallMethod( TrivialQuotientSubgroup,
        "for additive quotient group via presentation", true,
        [ IsQuotientToAdditiveGroup ], 0,
        function( quo )
        local indGenSet, gens, gens2, SiftFnc, hom, rels, srcGrp, kerGrp ;

  if not ForAll(GeneratorsOfGroup(quo), g->ImageElm(Homomorphism(g),SourceElt(g))
                         = ImageElt(g)) then
       Error("bad gens of grp");
  fi;
        indGenSet := BasisOfHomCosetAddMatrixGroup(quo);
        gens2 := indGenSet.basis;
  if not ForAll(gens2, g->ImageElm(Homomorphism(g),SourceElt(g))
                         = ImageElt(g)) then
       Error("bad gens2");
  fi;
        SiftFnc := SiftVector(indGenSet.basis);
        rels := indGenSet.residue;
        if not IsMutable(rels) then rels := List(rels); fi;
  if not ForAll(rels, g->ImageElm(Homomorphism(g),SourceElt(g))
                         = ImageElt(g)) then
       Error("bad residue");
  fi;
        Append( rels, List( gens2, g -> g^Order(g) ) );
  if not ForAll(rels, g->ImageElm(Homomorphism(g),SourceElt(g))
                         = ImageElt(g)) then
       Error("bad order");
  fi;
        Append( rels, List( gens2, SiftFnc ) );
        # If all source groups of quo are abelian, this is unnec.
        Append(rels, ListX( gens2, gens2,
                            function(g1,g2) return Comm(g1,g2); end ) );
        if not ForAll(rels, IsOne) then
            Error("internal error: invalid relation of presentation");
        fi;
        return SubgroupNC(quo, rels);
    end );

#############################################################################
#############################################################################
##
##  Nilpotent matrix groups:
##
#############################################################################
#############################################################################

##  Always try SizeUpperBound first.

#############################################################################
##
#M  CanFindNilpotentClassTwoElement( <G> )
##
InstallMethod( CanFindNilpotentClassTwoElement, "compute elt or fail", true,
    [ IsGroup ], 0,
function(G)
    local gens, g, count, i;
    gens := GeneratorsOfGroup( G );
    g := First(gens, h -> not IsInCenter(G,h));
    SetIsAbelian(G, g = fail);
    if IsAbelian(G) then return false; fi;
    # gdc -
    # Want max. length derived series for _nilpotent_ group of a given size.
    # There should be much better bound than LogInt(SizeUpperBound(G),2).
    # I can look it up some other time.
    for count in [1..LogInt(SizeUpperBound(G),2)] do
        i := PositionProperty(gens, h -> not IsInCenter(G,Comm(g,h)));
        if i = fail then
            SetNilpotentClassTwoElement(G,g);
            return true;
        else g := Comm(g,gens[i]);
        fi;
    od;
    return false;
end );

#############################################################################
##
#M  NilpotentClassTwoElement( <G> )
##
InstallMethod( NilpotentClassTwoElement,
    "by calling CanFindNilpotentClassTwoElement()", true, [ IsGroup ], 0,
function(G)
    if CanFindNilpotentClassTwoElement(G) then
        return NilpotentClassTwoElement(G);
    else TryNextMethod(); return;
    fi;
end );

#############################################################################
##
#F  NaturalHomomorphismByNilpotentClassTwoElement( <G> )
##
InstallGlobalFunction( NaturalHomomorphismByNilpotentClassTwoElement,
function(G)
    local elt;
    elt := NilpotentClassTwoElement(G);
    if elt = fail then return Error("abelian or not nilpotent"); fi;
    return GroupHomomorphismByFunction
       ( G, Group( List( GeneratorsOfGroup(G), h->Comm(h,elt) ) ),
         h->Comm(h,elt) );
end );

#############################################################################
##
#F  ManageableQuotientOfNilpotentGroup( <G> )
##

ManageableQuotientOfNilpotentGroup := function( G )
            local hom, quo;
            hom := NaturalHomomorphismByNilpotentClassTwoElement(G);
            ChainSubgroupByHomomorphism( hom );
            quo := QuotientGroup(TransversalOfChainSubgroup(G));
            IsAbelian(quo);  # Tell GAP quo is abelian in case not propagated.
            return quo;
        end;
        
        
#############################################################################
##
#M  MakeHomChain( <G> )
##

InstallMethod( MakeHomChain, "for nilpotent group", true,
        [ IsGroup and IsNilpotentGroup ], 0,
    function( G )
        local quo, kernel;
        if IsAbelian(G) then return MakeHomChain(G); fi;
        quo := ManageableQuotientOfNilpotentGroup(G);
        Info(InfoChain, 2, "Finding kernel of homomorphism by",
                           "nilpotent class 2 elt");
        IsAbelian(quo); # Special Kernel method for abelian grp
        MakeHomChain(quo);
        # sets Kernel(Homomorphism(quo))
        kernel := KernelOfHomQuotientGroup(quo);
        # Now that we have the full kernel, make new ChainSubgroup(grp)
        ChainSubgroupByHomomorphism( Homomorphism(quo) );
        if IsTrivial(kernel) then return kernel;
        else return MakeHomChain( kernel );
        fi;
    end );

#E