This file is indexed.

/usr/share/gap/lib/quogphom.gi is in gap-libs 4r7p5-2.

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
#############################################################################
##
#W  quogphom.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
##

#############################################################################
#############################################################################
##
##  Creating hom cosets and quotient groups
##
#############################################################################
#############################################################################

#############################################################################
##
#M  HomCosetFamily( <hom> )
##
InstallMethod(HomCosetFamily,"for homomorphisms",true,[IsGroupHomomorphism],0, 
function(hom)
local fam,filt;

  if IsPermGroup( Range( hom ) ) then 
      filt:=IsHomCosetToPermRep;
  # KEEP THIS COMMENT:  The order of 'elif' is very important,
  # since an additive group may also be a matrix group.
  elif IsAdditiveGroup( Range( hom ) ) then
      filt:=IsHomCosetToAdditiveEltRep;
  elif IsFFEMatrixGroup( Range( hom ) ) then
      filt:=IsHomCosetToMatrixRep;
  elif IsFpGroup( Range( hom ) ) then
      filt:=IsHomCosetToFpRep;
  elif IsDirectProductElementCollection( Range( hom ) ) then
      filt:=IsHomCosetToTupleRep;
  else 
      filt:=IsHomCosetToObjectRep;
  fi;

  if IsPermGroup(Source(hom)) then
    filt:=filt and IsHomCosetOfPerm;
  elif IsFFEMatrixGroup(Source(hom)) then
    filt:=filt and IsHomCosetOfMatrix;
  elif IsFpGroup(Source(hom)) then
    filt:=filt and IsHomCosetOfFp;
  elif IsAdditiveGroup(Source(hom)) then
    filt:=filt and IsHomCosetOfAdditiveElt;
  elif IsDirectProductElementCollection(Source(hom)) then
    filt:=filt and IsHomCosetOfTuple;
  fi;

  fam:=NewFamily("HomCosetFamily",filt);
  fam!.homomorphism:=hom;
  fam!.defaultType:=NewType(fam,filt);
  return fam;

end);

#############################################################################
##
#F  HomCoset( <hom>, <elt> )
##
InstallGlobalFunction( HomCoset, 
    [ IsGroupHomomorphism, IsAssociativeElement ], 
    function( hom, elt )
	local fam, Rec;

	fam:=HomCosetFamily(hom);
	Rec := rec( Homomorphism := hom, SourceElt := elt );
        return Objectify( fam!.defaultType, Rec );
    end );

#############################################################################
##
#F  HomCosetWithImage( <hom>, <srcElt>, <imgElt> )
##
InstallGlobalFunction( HomCosetWithImage, 
    function( hom, srcElt, imgElt )
        local ret;
	ret := HomCoset( hom, srcElt );
	SetImageElt( ret, imgElt );
	return ret;
    end );


#############################################################################
##
#M  QuotientGroupHom( <hom> )
##
InstallMethod( QuotientGroupHom, "for group homomorphisms", true,
    [ IsGroupHomomorphism ], 0,
    function( hom )
        local grp, genimages, gensource;

        if IsCompositionMappingRep(hom) and
           IsBound(hom!.map2) then
            genimages := MappingGeneratorsImages(hom!.map2)[2];
        else genimages := fail;
        fi;
        # gdc - Actually, GAP doesn't define ImagesSource() for comp. maps.
        # But perhaps it should.
        if not HasImagesSource( hom ) and IsAdditiveGroup( Range(hom) )
           and not IsQuotientToAdditiveGroup( Range(hom) ) then
             if genimages <> fail then
                 SetImagesSource( hom, AdditiveGroup( genimages ) );
             else
                 # GAP4r1 Image(hom) needs patch when range is AdditiveGroup()
                 SetImagesSource( hom,
                     AdditiveGroup( List( GeneratorsOfGroup(Source(hom)),
                                    g->ImageElm(hom,g) ) ) );
             fi;

        fi;
        if genimages <> fail then
            gensource := GeneratorsOfGroup( Source( hom ) );
            grp := Group( List( [1..Length(gensource)],
                                function(i)
                                  return HomCosetWithImage
                                               (hom,gensource[i],genimages[i]);
                                end ) );
        else
            grp := Group( List( GeneratorsOfGroup( Source( hom ) ), 
	                        gen -> HomCoset( hom, gen ) ) );
        fi;
        if HasImagesSource( hom ) then
            UseIsomorphismRelation( ImagesSource(hom), grp );
        fi;
        UseFactorRelation( Source( hom ), fail, grp );
        UseSubsetRelationNC( Range( hom ), grp );
        SetQuotientGroup( Source(hom), grp );
        return grp;
    end );

#############################################################################
##
#F  QuotientGroupByHomomorphism( <hom> )
##
InstallGlobalFunction( QuotientGroupByHomomorphism, 
function( hom )
    return QuotientGroupHom( hom );
end );


#############################################################################
##
#F  QuotientGroupByImages( <srcGroup>, <rangeGroup>, <srcGens>, <imgGens> )
##
##  GAP defines GroupHomomorphismByImages as composition of
##  map to word in generators of source group, followed by map
##  taking word to image group and evaluating word there.
##  GAP delays finding strong gen. set and computing first mapping
##  until it's needed.  Our HomCoset does the same.  It should
##  not call upon the homomorphism until it's needed.
##
InstallGlobalFunction( QuotientGroupByImages, 
function( srcGroup, rangeGroup, srcGens, imgGens )
    local quoGroup, i;

    quoGroup := QuotientGroupHom( GroupHomomorphismByImages
                                  ( srcGroup, rangeGroup, srcGens, imgGens ) );
    for i in [1..Length( srcGens )] do
        SetImageElt( quoGroup.(i), imgGens[i] );
    od;
    UseFactorRelation( srcGroup, fail, quoGroup );
    UseSubsetRelationNC( rangeGroup, quoGroup );
    return quoGroup;    
end );

#############################################################################
##
#F  QuotientGroupByImagesNC( <srcGroup>, <rangeGroup>, <srcGens>, <imgGens> )
##
InstallGlobalFunction( QuotientGroupByImagesNC, 
function( srcGroup, rangeGroup, srcGens, imgGens )
    local quoGroup, i;

    quoGroup := QuotientGroupHom( GroupHomomorphismByImagesNC
                                  ( srcGroup, rangeGroup, srcGens, imgGens ) );
    for i in [1..Length( srcGens )] do
        SetImageElt( quoGroup.(i), imgGens[i] );
    od;
    UseFactorRelation( srcGroup, fail, quoGroup );
    UseSubsetRelationNC( rangeGroup, quoGroup );
    return quoGroup;    
end );


#############################################################################
##
#F  QuotientSubgroupNC( <M>, <gens> )
##
##  gdc - Consider setting new homomorphism for grp with Source() as grp.
##        However, in current representation, this would mean that an
##        element of the subgroup and an element of the group cannot
##        be multiplied together.  How does GAP handle these issues?
#InstallGlobalFunction( QuotientSubgroupNC,
#function( M, gens )
#    local grp;
#    grp := SubgroupNC( M, gens );
#    SetIsAssociative( grp, true );
#    return grp;
#end );


#############################################################################
#############################################################################
##
##  Basic access functions
##
#############################################################################
#############################################################################

#############################################################################
##
#F  IsTrivialHomCoset( <hcoset> )
##
InstallGlobalFunction( IsTrivialHomCoset,
    function( hcoset )
        if IsHomCoset( hcoset ) then
            return IsOne( hcoset ) and IsTrivialHomCoset( SourceElt(hcoset) );
        else 
	    return IsOne( hcoset );
        fi;
    end );

#############################################################################
##
#M  Homomorphism( <hcoset> ) for hom cosets
##
InstallMethod( Homomorphism, "for hom cosets", true,
    [ IsHomCoset ], 0, hcoset -> hcoset!.Homomorphism );

#############################################################################
##
#M  Homomorphism( <Q> ) for quotient groups
##
InstallMethod( Homomorphism, "for quotient groups", true,
    [ IsHomQuotientGroup ], 0, Q -> Homomorphism( Q.1 ) );

#############################################################################
##
#M  Source( <Q> ) for quotient groups
##
InstallMethod( Source, "for quotient groups", true,
    [ IsHomQuotientGroup ], 0, Q -> Source( Homomorphism( One( Q ) ) ) );

#############################################################################
##
#M  Range( <Q> ) for quotient groups
##
InstallMethod( Range, "for quotient groups", true,
    [ IsHomQuotientGroup ], 0, Q -> Range( Homomorphism( One( Q ) ) ) );

#############################################################################
##
#M  ImagesSource( <Q> ) for quotient groups
##
##  NOTE:  Image(quotientGroup) will call ImagesSource(quotientGroup)
##
InstallMethod( ImagesSource, "for quotient groups", true,
    [ IsHomQuotientGroup ], 0, Q -> Image( Homomorphism( One( Q ) ) ) );

InstallOtherMethod( ONE, "for quotient groups", true,
    [ IsHomQuotientGroup ], 2*SUM_FLAGS, Q -> ONE( Q.1 ) );
#T good idea/necessary?

#############################################################################
##
#M  SourceElt( <Q> ) for hom cosets
##
InstallMethod( SourceElt, "for hom cosets", true,
    [ IsHomCoset ], 0, hcoset -> hcoset!.SourceElt );

#############################################################################
##
#M  ImageElt( <Q> ) for hom cosets
##
##  ImageElt is attribute of hcoset -- It will be cached.
##
InstallMethod( ImageElt, "for hom cosets", true,
    [ IsHomCoset ], 0, function(hcoset)
    local img;
    img := ImageElm( Homomorphism( hcoset ), SourceElt( hcoset ) );
    if img = fail then Error("hom coset with invalid image"); fi;
    return img;
end );



#############################################################################
#############################################################################
##
##  Basic operations
##
#############################################################################
#############################################################################

StringImType := function( hcoset ) 
    if IsHomCosetToPerm( hcoset ) then
        return "perm";
    elif IsHomCosetToMatrix( hcoset ) then
	return "matrix";
    elif IsHomCosetToFp( hcoset ) then
	return "word";
    elif IsHomCosetToTuple( hcoset ) then
	return "tuple";
    else
	return "element";
    fi;
end;

#############################################################################
##
#M  ViewObj( <hcoset> ) for hom cosets
##
InstallMethod( ViewObj, "for hom coset", true,
    [ IsHomCoset ], 0,
    function( hcoset )
        Print("( "); 
	if HasImageElt( hcoset ) then
	    View( ImageElt( hcoset ) );
	else
	    View( StringImType( hcoset ) );
	    # place holder if image not known
	fi;
	Print(" <- ");
	ViewObj( SourceElt( hcoset ) ); Print(" )");
    end );

#############################################################################
##
#M  PrintObj( <hcoset> ) for hom cosets
##
InstallMethod( PrintObj, "for hom coset", true,
    [ IsHomCoset ], 0,
    function( hcoset )
        Print("( "); 
	if HasImageElt( hcoset ) then
	    PrintObj( ImageElt( hcoset ) );
	else
	    Print( StringImType( hcoset ) );
	fi;
	Print(" <- ");
	PrintObj( SourceElt( hcoset ) ); Print(" )");
    end );

#############################################################################
##
#M  EQ( <hcoset1>, <hcoset2> ) for hom cosets
##
InstallMethod( EQ, "for hom cosets", IsIdenticalObj,
    [ IsHomCoset, IsHomCoset ], 0,
    function( hcoset1, hcoset2 )
        return  EQ( ImageElt(hcoset1), ImageElt(hcoset2) );
    end );

# I think this is a bad idea. Scott
#############################################################################
##
#M  EQ( <hcoset>, <mat> ) for hom coset matrix rep or add. rep. and identity
##
InstallMethod( EQ, "for hom coset matrix rep or add. rep. and identity", true,
    [ IsHomCosetToMatrix, IsMatrix and IsOne ], 0,
    function( hcoset, mat )
        return IsOne(SourceElt(hcoset));
    end );
#############################################################################
##
#M  EQ( <mat>, <hcoset> ) for identity and hom coset matrix rep or add. rep.
##
InstallMethod( EQ, "for hom coset matrix rep or add. rep. and identity", true,
    [ IsMatrix and IsOne, IsHomCosetToMatrix ], 0,
    function( mat, hcoset )
        return IsOne(SourceElt(hcoset));
    end );


#############################################################################
##
#M  LT( <hcoset1>, <hcoset2> ) for hom cosets
##
InstallMethod( LT, "for hom cosets",IsIdenticalObj,
    [ IsHomCoset, IsHomCoset ], 0,
    function( hcoset1, hcoset2 )
    	return LT( ImageElt(hcoset1), ImageElt(hcoset2) );
    end );

#############################################################################
##
#M  ONE( <hcoset> ) for hom cosets
##
InstallMethod( ONE, "for hom coset", true,
    [ IsHomCoset ], 0,
    function( hcoset )
	local one;
        one := HomCoset( Homomorphism( hcoset ), ONE( SourceElt(hcoset) ) );
	SetImageElt( one, ONE( Range( Homomorphism( hcoset ) ) ) );
	return one;
    end );

#############################################################################
##
#M  One( <hcoset> ) for hom cosets
##
InstallMethod( One, "for hom coset", true,
    [ IsHomCoset ], 0,
    function( hcoset )
	local one;
        one := HomCoset( Homomorphism( hcoset ), One( SourceElt(hcoset) ) );
	SetImageElt( one, One( Range( Homomorphism( hcoset ) ) ) );
	return one;
    end );
#############################################################################
##
#M  ONE( <hcoset> ) for hom coset in additive rep
##
InstallMethod( ONE, "for hom coset in additive rep", true,
    [ IsHomCosetToAdditiveElt ], 2*SUM_FLAGS,
    function( hcoset )
     return HomCosetWithImage( Homomorphism(hcoset),
                               One( SourceElt(hcoset) ), Zero( ImageElt(hcoset) ) );
    end );
#############################################################################
##
#M  One( <hcoset> ) for hom coset in additive rep
##
InstallMethod( One, "for hom coset in additive rep", true,
    [ IsHomCosetToAdditiveElt ], 2*SUM_FLAGS,
    function( hcoset )
     return HomCosetWithImage( Homomorphism(hcoset),
                               One( SourceElt(hcoset) ), Zero( ImageElt(hcoset) ) );
    end );

#############################################################################
##
#M  PROD( <zero>, <hcoset> ) for zero * additive hom coset rep
##
InstallMethod( PROD, "for zero * additive hom coset rep", true,
    [ IsZeroCyc, IsHomCosetToAdditiveElt ], SUM_FLAGS,
    function( zero, hcoset ) return ZERO(hcoset); end );

#############################################################################
##
#M  PROD( <hcoset>, <zero> ) for additive hom coset rep * zero
##
InstallMethod( PROD, "for additive hom coset rep * zero", true,
    [ IsHomCosetToAdditiveElt, IsZeroCyc ], SUM_FLAGS,
    function( hcoset, zero ) return ZERO(hcoset); end );

#############################################################################
##
#M  ZERO( <hcoset> ) for additive hom coset
##
InstallMethod( ZeroSameMutability, "for additive hom coset", true,
    [ IsOrdinaryMatrix and IsHomCosetToAdditiveElt ], 0, hcoset -> ONE(hcoset) );

#############################################################################
##
#M  ZeroOp( <hcoset> ) for additive hom coset
##
InstallMethod( ZeroMutable, "for additive hom coset", true,
    [ IsObject and IsAdditiveElement and IsOrdinaryMatrix and IsHomCosetToAdditiveElt ], 0, 
    hcoset -> One(hcoset) );


##
##  Install default method and identical specialized methods that will
##    will have a higher score to beat out GAP's methods.
##
PROD_HOM_COSET :=
function( hcoset1, hcoset2 )
    local ret;
    ret := HomCoset( Homomorphism( hcoset1 ), 
	    SourceElt(hcoset1) * SourceElt(hcoset2) );
    if HasImageElt( hcoset1 ) and HasImageElt( hcoset2 ) then
        if IsHomCosetToAdditiveElt( hcoset1 ) then
            SetImageElt( ret, ImageElt( hcoset1 ) + ImageElt( hcoset2 ) );
        else SetImageElt( ret, ImageElt( hcoset1 ) * ImageElt( hcoset2 ) );
        fi;
    fi;
    return ret;
end;

#############################################################################
##
#M  PROD( <hcoset1>, <hcoset2> ) for hom cosets
##
InstallMethod( PROD, "for hom cosets", IsIdenticalObj,
    [ IsHomCoset, IsHomCoset ], 0, PROD_HOM_COSET );

# the following methods are needed because hom cosets are *also*
# permutations and we don't want to fall in the permutation multiplicationb
# routine. Alternatively the method above could be ranked higher.
#############################################################################
##
#M  PROD( <hcoset1>, <hcoset2> ) for hom cosets to matrix groups
##
InstallMethod( PROD, "for hom cosets to matrix groups", IsIdenticalObj,
    [ IsHomCosetToMatrix, IsHomCosetToMatrix ], 0, PROD_HOM_COSET );
#############################################################################
##
#M  PROD( <hcoset1>, <hcoset2> ) for hom cosets to permutation group
##
InstallMethod( PROD, "for hom cosets to permutation groups", IsIdenticalObj,
    [ IsHomCosetToPerm, IsHomCosetToPerm ], 0, PROD_HOM_COSET );

#############################################################################
##
#M  PROD( <hcoset1>, <hcoset2> ) for hom cosets to additive groups
##
InstallMethod( PROD, "for hom cosets to additive groups", IsIdenticalObj,
    [ IsHomCosetToAdditiveElt, IsHomCosetToAdditiveElt ], 0, PROD_HOM_COSET );

#############################################################################
##
#M  PROD( <v>, <hcoset> ) for vector and hom coset
##
InstallMethod( PROD, "for vector and hom coset", true,
    [ IsRowVector and IsRingElementList, IsHomCoset ], 0,
    function( v, hcoset ) return v * ImageElt(hcoset); end );

#############################################################################
##
#M  PROD( <hcoset>, <v> ) for hom coset and vector 
##
InstallMethod( PROD, "for hom coset and vector", true,
    [ IsHomCoset, IsVector and IsRingElementList ], 0,
    function( hcoset, v ) return ImageElt(hcoset) * v; end );
##  gdc - Should we protect against 3*hcoset, Z(3)*hcoset, hcoset*3, etc.
##        by signalling an error?
##     3*hcoset is well-defined for IsHomCosetToAddRep, but not for some others.


##
##  We identify \+ and \* if it's an additive group.
##

#############################################################################
##
#M  SUM( <hcoset1>, <hcoset2> ) for hom cosets to additive groups
##
InstallMethod( SUM, "for hom cosets to additive groups", IsIdenticalObj,
    # specialization needed to beat SUM of IsHomCosetOfMatrix 
    [ IsMatrix and IsHomCosetOfMatrix and IsHomCosetToAdditiveElt,
      IsMatrix and IsHomCosetOfMatrix and IsHomCosetToAdditiveElt ],
    0, PROD_HOM_COSET );

#############################################################################
##
#M  SUM( <hcoset1>, <hcoset2> ) for hom cosets to additive groups
##
InstallMethod( SUM, "for hom cosets to additive groups", IsIdenticalObj,
    [ IsHomCosetToAdditiveElt, IsHomCosetToAdditiveElt ], 0, PROD_HOM_COSET );

#############################################################################
##
#M  DIFF( <hcoset1>, <hcoset2> ) for hom cosets to additive groups
##
InstallMethod( DIFF, "for hom cosets to additive groups", IsIdenticalObj,
    # specialization needed to beat DIFF of IsHomCosetOfMatrix 
    [ IsHomCosetOfMatrix and IsHomCosetToAdditiveElt,
      IsHomCosetOfMatrix and IsHomCosetToAdditiveElt ], 0,
    function(hcoset1, hcoset2)
        return hcoset1 + (- hcoset2); #Note \+ and \* are same in this context
    end );

#############################################################################
##
#M  DIFF( <hcoset1>, <hcoset2> ) for hom cosets to additive groups
##
InstallMethod( DIFF, "for hom cosets to additive groups", IsIdenticalObj,
    [ IsHomCosetToAdditiveElt, IsHomCosetToAdditiveElt ], 0,
    function(hcoset1, hcoset2)
        return hcoset1 + (- hcoset2); #Note \+ and \* are same in this context
    end );

#############################################################################
##
#M  AdditiveInverseOp( <hcoset> ) unary minus for hom coset in additive rep 
##
InstallMethod( AdditiveInverseOp, "unary minus for hom coset in additive rep", true,
    [ IsHomCosetToAdditiveElt ], 0,
    hcoset -> HomCosetWithImage( Homomorphism( hcoset ),
                             INV(SourceElt(hcoset)), AINV(ImageElt(hcoset)) ) );

#############################################################################
##
#M  AINV( <hcoset> ) unary minus for hom coset in additive rep 
##
InstallMethod( AdditiveInverseOp, "unary minus for hom coset in additive rep", true,
    [ IsHomCosetToAdditiveElt ], 0,
    hcoset -> HomCosetWithImage( Homomorphism( hcoset ),
                             INV(SourceElt(hcoset)), AINV(ImageElt(hcoset)) ) );

#############################################################################
##
#M  INV( <hcoset> ) unary minus for hom coset in additive rep
##
InstallMethod( INV, "unary minus for hom coset in additive rep", true,
    [ IsHomCosetToAdditiveElt ], 0,
    hcoset -> HomCosetWithImage( Homomorphism( hcoset ),
                             INV(SourceElt(hcoset)), AINV(ImageElt(hcoset)) ) );
#############################################################################
##
#M  Inverse( <hcoset> ) unary minus for hom coset in additive rep
##
InstallMethod( Inverse, "for unary hom coset in additive rep", true,
    [ IsHomCosetToAdditiveElt ], 0,
    hcoset -> HomCosetWithImage( Homomorphism( hcoset ),
                             Inverse(SourceElt(hcoset)), AINV(ImageElt(hcoset)) ) );


#############################################################################
##
#M  QUO( <hcoset1>, <hcoset2> ) for hom cosets
##
InstallMethod( QUO, "for hom cosets", IsIdenticalObj,
    [ IsHomCoset, IsHomCoset ], 0,
    function( hcoset1, hcoset2 )
	local ret;
	ret := HomCoset( Homomorphism( hcoset1 ), 
		SourceElt(hcoset1) / SourceElt(hcoset2) );
	if HasImageElt( hcoset1 ) and HasImageElt( hcoset2 ) then
            if IsHomCosetToAdditiveElt( hcoset1 ) then
                SetImageElt( ret, ImageElt( hcoset1 ) - ImageElt( hcoset2 ) );
            else SetImageElt( ret, ImageElt( hcoset1 ) / ImageElt( hcoset2 ) );
            fi;
	fi;
        return ret;
    end );


##
##  Install default method and identical specialized methods that will
##    will have a higher score to beat out GAP's methods.
##
INV_HOM_COSET :=
    function( hcoset )
	local ret;
        ret := HomCoset( Homomorphism( hcoset ), INV( SourceElt(hcoset) ) );
	if HasImageElt( hcoset ) then
            if IsHomCosetToAdditiveElt( hcoset ) then
                SetImageElt( ret, AINV( ImageElt( hcoset ) ) );
	    else SetImageElt( ret, INV( ImageElt( hcoset ) ) );
            fi;
	fi;
	return ret;
    end;

#############################################################################
##
#M  INV( <hcoset> ) unary minus for hom coset
##
InstallMethod( INV, "unary minus for hom coset", true,
    [ IsHomCoset ], 0, INV_HOM_COSET );

#############################################################################
##
#M  INV( <hcoset> ) for hom cosets to matrix groups
##
InstallMethod( INV, "for hom cosets to matrix groups", true,
    [ IsHomCosetToMatrix ], 0, INV_HOM_COSET );

#############################################################################
##
#M  INV( <hcoset> ) for hom cosets to permutation groups
##
InstallMethod( INV, "for hom cosets to permutation groups", true,
    [ IsHomCosetToPerm ], 0, INV_HOM_COSET );

#############################################################################
##
#M  INV( <hcoset> ) for hom cosets to additive groups
##
InstallMethod( INV, "for hom cosets to additive groups", true,
    [ IsHomCosetToAdditiveElt ], 0, INV_HOM_COSET );

#############################################################################
##
#M  Inverse( <hcoset> ) for hom coset
##
InstallMethod( Inverse, "for hom coset", true,
    [ IsHomCoset ], 0,
    function( hcoset )
        return INV( hcoset );
    end );

#############################################################################
##
#M  POW( <hcoset1>, <hcoset2> ) for hom cosets
##
InstallMethod( POW, "for hom cosets", IsIdenticalObj,
    [ IsHomCoset, IsHomCoset ], 
    # the higher rank is to override a default method for permutations etc.
    1,
    function( hcoset1, hcoset2 )
        local ret;
	if Homomorphism( hcoset1 ) <> Homomorphism( hcoset2 ) then
	    Error( "cosets in different quotient groups" );
	fi;
	ret := HomCoset( Homomorphism( hcoset1 ), 
		SourceElt(hcoset1) ^ SourceElt(hcoset2) );
	if HasImageElt( hcoset1 ) and HasImageElt( hcoset2 ) then
            if IsHomCosetToAdditiveElt( hcoset1 ) then
                SetImageElt( ret, ImageElt( hcoset1 ) * ImageElt( hcoset2 ) );
	    else SetImageElt( ret, ImageElt( hcoset1 ) ^ ImageElt( hcoset2 ) );
            fi;
	fi;
        return ret;
    end );

#############################################################################
##
#M  POW( <hcoset>, <int> ) for hom cosets to integer power
##
InstallMethod( POW, "for hom cosets to integer power", true,
    [ IsHomCoset, IsInt ], 
    # the higher rank is to override a default method for permutations etc.
    1,
    function( hcoset, int )
        local ret;
	ret := HomCoset( Homomorphism( hcoset ), SourceElt(hcoset) ^ int );
	if HasImageElt( hcoset ) then
            if IsHomCosetToAdditiveElt( hcoset ) then
                SetImageElt( ret, int * ImageElt( hcoset ) );
	    else SetImageElt( ret, ImageElt( hcoset ) ^ int );
            fi;
	fi;
        return ret;
    end );

#############################################################################
##
#M  COMM( <hcoset1>, <hcoset2> ) for hom cosets
##
InstallMethod( COMM, "for hom cosets", IsIdenticalObj,
    [ IsHomCoset, IsHomCoset ], 0,
    function( hcoset1, hcoset2 )
        local ret;
	ret := HomCoset( Homomorphism( hcoset1 ), 
		COMM( SourceElt(hcoset1), SourceElt(hcoset2) ) );
	if HasImageElt( hcoset1 ) and HasImageElt( hcoset2 ) then
            if IsHomCosetToAdditiveElt( hcoset1 ) then
                SetImageElt( ret, ZERO( ImageElt( hcoset1 ) ) );
	    else SetImageElt( ret, COMM( ImageElt( hcoset1 ),
                                         ImageElt( hcoset2 ) ) );
            fi;
	fi;
        return ret;
    end );

#############################################################################
##
#M  Order( <hcoset> ) for hom coset
##
InstallMethod( Order, "for hom coset", true,
    [ IsHomCoset ], NICE_FLAGS,
    function( hcoset )
        return Order( ImageElt(hcoset) );
    end );

#############################################################################
##
#M  Order( <hcoset> ) for hom coset in additive rep
##
InstallMethod( Order, "for hom coset in additive rep", true,
    [ IsHomCosetToAdditiveElt ], NICE_FLAGS,
    function( hcoset )
        if IsZero(ImageElt(hcoset)) then return 1;
        #BUG: This won't work if image is hom coset add rep over Z(9), e.g.
        #GAP needs better support for additive matrix groups.
        # In this case, GAP should store field size in additive group,
        #   just as GAP does for IsFFEMatrixGroup.
        elif IsMatrix(ImageElt(hcoset)) then
          hcoset:= DefaultFieldOfMatrix( ImageElt( hcoset ) );
          if hcoset = fail then
            TryNextMethod();
          else
            return Size( hcoset );
          fi;
        else TryNextMethod();
        fi;
    end );

#############################################################################
##
#M  CanonicalElt( <hcoset> )
##
InstallMethod( CanonicalElt, "for hom cosets", true,
    [ IsRightCoset and IsHomCoset ], 0,
    function( hcoset )
        local quoGroup;
	Info( InfoQuotientGroup, 2, "Stripping to find canonical element" );
	quoGroup := QuotientGroupHom( Homomorphism( hcoset ) );
	ChainSubgroup( quoGroup );  # compute a chain if necessary
	return  SourceElt( Sift( quoGroup, hcoset ) )^(-1) *
		SourceElt( hcoset );

    end );


#############################################################################
#############################################################################
##
##  Functions for specific types of image
##
#############################################################################
#############################################################################

#############################################################################
##
#M  POW( <int>, <hcoset> ) for integer and perm hom coset
##
InstallMethod( POW, "for integer and perm hom coset", true,
    [ IsInt, IsHomCosetToPerm ], 0,
    function( int, hcoset )
        return POW( int, ImageElt(hcoset) );
    end );

#############################################################################
##
#M  QUO( <int>, <hcoset> ) for integer and perm hom coset
##
InstallMethod( QUO, "for integer and perm hom coset", true,
    [ IsInt, IsHomCosetToPerm ], 0,
    function( int, hcoset )
        return QUO( int, ImageElt(hcoset) );
    end );

##  gdc - IsVector( matrix ) => true;  But IsRingElementList(matrix) => false
#############################################################################
##
#M  POW( <vec>, <hcoset> ) for vector and matrix hom coset
##
InstallMethod( POW, "for vector and matrix hom coset", true,
    [ IsVector and IsRingElementList,
      IsMatrix and IsHomCosetToMatrix and IsRingElementTable ], 0,
    function( vec, hcoset )
        return POW( vec, ImageElt(hcoset) );
    end );

#############################################################################
##
#M  SUM( <hcoset1>, <hcoset2> ) for two matrix hom cosets
##
InstallMethod( SUM, "for two matrix hom cosets", true,
    [ IsMatrix and IsHomCosetToMatrix and IsHomCosetOfMatrix,
      IsMatrix and IsHomCosetToMatrix and IsHomCosetOfMatrix ], 0,
    function( hcoset1, hcoset2 )
        local ret;
        if Homomorphism( hcoset1 ) <> Homomorphism( hcoset2 ) then
            Error( "cosets in different quotient groups" );
        fi;
        ret := HomCoset( Homomorphism( hcoset1 ),
                SourceElt(hcoset1) + SourceElt(hcoset2) );
        if HasImageElt( hcoset1 ) and HasImageElt( hcoset2 ) then
            SetImageElt( ret, ImageElt( hcoset1 ) + ImageElt( hcoset2 ) );
        fi;
        return ret;
    end );

#############################################################################
##
#M  DIFF( <hcoset1>, <hcoset2> ) for two matrix hom cosets
##
InstallMethod( DIFF, "for two matrix hom cosets", true,
    [ IsHomCosetToMatrix and IsHomCosetOfMatrix,
      IsHomCosetToMatrix and IsHomCosetOfMatrix ], 0,
    function( hcoset1, hcoset2 )
        local ret;
        if Homomorphism( hcoset1 ) <> Homomorphism( hcoset2 ) then
            Error( "cosets in different quotient groups" );
        fi;
        ret := HomCoset( Homomorphism( hcoset1 ),
                SourceElt(hcoset1) - SourceElt(hcoset2) );
        if HasImageElt( hcoset1 ) and HasImageElt( hcoset2 ) then
            SetImageElt( ret, ImageElt( hcoset1 ) - ImageElt( hcoset2 ) );
        fi;
        return ret;
    end );

#############################################################################
##
#M  SmallestMovedPointPerm( <hcoset> ) for hom coset to permutation
##
InstallMethod( SmallestMovedPointPerm, "for hom coset to permutation", true,
    [ IsPerm and IsHomCosetToPerm ], 0,
    function( hcoset )
        return SmallestMovedPointPerm( ImageElt(hcoset) );
    end );

#############################################################################
##
#M  LargestMovedPointPerm( <hcoset> ) for hom coset to permutation
##
InstallMethod( LargestMovedPointPerm, "for hom coset to permutation", true,
    [ IsPerm and IsHomCosetToPerm ], 0,
    function( hcoset )
        return LargestMovedPointPerm( ImageElt(hcoset) );
    end );

#############################################################################
##
#M  ELM_LIST( <hcoset>, <i> ) for hom coset to matrix
##
InstallMethod( ELM_LIST, "for hom coset to matrix", true,
    [ IsMatrix and IsHomCosetToMatrix, IsInt ], 0,
    function( hcoset, i )
	return ELM_LIST( ImageElt( hcoset ), i );
    end );

#############################################################################
##
#M  Length( <hcoset> ) for hom coset to matri
##
InstallMethod( Length, "for hom coset to matrix", true,
    [ IsMatrix and IsHomCosetToMatrix ], 0,
    function( hcoset )
	return Length( ImageElt( hcoset ) );
    end );

#############################################################################
##
##  Length( <hcoset> ) for hom coset to fp 
##
##InstallMethod( Length, "for hom coset to fp", true,
##   [ IsWord and IsHomCosetToFp ], 0,
##    function( hcoset )
##	return Length( ImageElt( hcoset ) );
##   end );

#############################################################################
##
#M  Length( <hcoset> ) for hom coset to tuple
##
InstallMethod( Length, "for hom coset to tuple", true,
    [ IsDirectProductElement and IsHomCosetToTuple ], 0,
    function( hcoset )
	return Length( ImageElt( hcoset ) );
    end );

#############################################################################
##
#M  DefaultFieldOfMatrix( <hcoset> ) for hom coset to matrix
##
InstallMethod( DefaultFieldOfMatrix, "for hom coset", true,
    [ IsMatrix and IsHomCoset ], 0,
    hcoset -> DefaultFieldOfMatrix( ImageElt( hcoset ) ) );


# SourceElt() will not be valid, but ImageElt() will be correct.
GroupFromAdditiveGroup := function( addGrp )
    local hom;
    hom := GroupHomomorphismByFunction( addGrp, addGrp, x->x );
    return Group( List( GeneratorsOfAdditiveGroup(addGrp),
                         g -> HomCoset( hom, g ) ) );
end;


#############################################################################
##
#M  ImagesSet( <quoGroup> ) to overcome bad family relation in default
##     (When quotient groups inherit families, this will be cleaner.)
##
InstallMethod( ImagesSet,
    "for general mapping, and quotient group with bad family relation",
    true,
    [ IsGeneralMapping, IsHomQuotientGroup ], -8,
    function( map, grp )
        return ImagesSet( map, Image(Homomorphism(grp)) );
    end );
    
    
    InstallMethod( PreOrbishProcessing, [IsHomQuotientGroup], 
            function(G)
        local g,h;
        g := G;
        while IsHomQuotientGroup(g) do
            h := Homomorphism(One(g));
            g := Group(List(GeneratorsOfGroup(g), x->Image(h, SourceElt(x))));
        od;
        return g;
    end);
#E