This file is indexed.

/usr/share/gap/lib/mapphomo.gi is in gap-libs 4r8p8-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
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
#############################################################################
##
#W  mapphomo.gi                 GAP library                     Thomas Breuer
#W                                                         and Heiko Theißen
##
##
#Y  Copyright (C)  1997,  Lehrstuhl D für Mathematik,  RWTH Aachen,  Germany
#Y  (C) 1998 School Math and Comp. Sci., University of St Andrews, Scotland
#Y  Copyright (C) 2002 The GAP Group
##
##  This file contains the methods for properties of mappings preserving
##  algebraic structure.
##
##  1. methods for general mappings that respect multiplication
##  2. methods for general mappings that respect addition
##  3. methods for general mappings that respect scalar multiplication
##  4. properties and attributes of gen. mappings that respect multiplicative
##     and additive structure
##  5. default equality tests for structure preserving mappings
##


#############################################################################
##
##  1. methods for general mappings that respect multiplication
##

#############################################################################
##
#M  RespectsMultiplication( <mapp> )  . . . . .  for a finite general mapping
##
InstallMethod( RespectsMultiplication,
    "method for a general mapping",
    [ IsGeneralMapping ],
    function( map )
    local S, R, enum, pair1, pair2;

    S:= Source( map );
    R:= Range(  map );
    if not ( IsMagma( S ) and IsMagma( R ) ) then
      return false;
    fi;

    map:= UnderlyingRelation( map );

    if not IsFinite( map ) then
      TryNextMethod();
    fi;
    enum:= Enumerator( map );
    for pair1 in enum do
      for pair2 in enum do
        if not DirectProductElement( [ pair1[1] * pair2[1], pair1[2] * pair2[2] ] )
           in map then
          return false;
        fi;
      od;
    od;
    return true;
    end );


#############################################################################
##
#M  RespectsOne( <mapp> ) . . . . . . . . . . .  for a finite general mapping
##
InstallMethod( RespectsOne,
    "method for a general mapping",
    [ IsGeneralMapping ],
    function( map )
    local S, R;
    S:= Source( map );
    R:= Range(  map );
    return     IsMagmaWithOne( S )
           and IsMagmaWithOne( R )
           and One( R ) in ImagesElm( map, One( S ) );
    end );


#############################################################################
##
#M  RespectsInverses( <mapp> )  . . . . . . . .  for a finite general mapping
##
InstallMethod( RespectsInverses,
    "method for a general mapping",
    [ IsGeneralMapping ],
    function( map )
    local S, R, enum, pair;
    S:= Source( map );
    R:= Range(  map );
    if not ( IsMagmaWithInverses( S ) and IsMagmaWithInverses( R ) ) then
      return false;
    fi;

    map:= UnderlyingRelation( map );
    if not IsFinite( map ) then
      TryNextMethod();
    fi;

    enum:= Enumerator( map );
    for pair in enum do
      if not DirectProductElement( [ Inverse( pair[1] ), Inverse( pair[2] ) ] )
             in map then
        return false;
      fi;
    od;
    return true;
    end );


#############################################################################
##
#M  KernelOfMultiplicativeGeneralMapping( <mapp> )  . for finite gen. mapping
##
InstallMethod( KernelOfMultiplicativeGeneralMapping,
    "method for a finite general mapping",
    [ IsGeneralMapping and RespectsMultiplication and RespectsOne ],
    function( mapp )

    local S, oneR, kernel, pair;

    S:= Source( mapp );
    if IsFinite( S ) then

      oneR:= One( Range( mapp ) );
      kernel:= Filtered( Enumerator( S ),
                         s -> oneR in ImagesElm( mapp, s ) );

    elif IsFinite( UnderlyingRelation( mapp ) ) then

      oneR:= One( Range( mapp ) );
      kernel:= [];
      for pair in Enumerator( UnderlyingRelation( mapp ) ) do
        if pair[2] = oneR then
          Add( kernel, pair[1] );
        fi;
      od;

    else
      TryNextMethod();
    fi;

    if     IsMagmaWithInverses( S )
       and HasRespectsInverses( mapp ) and RespectsInverses( mapp ) then
      return SubmagmaWithInversesNC( S, kernel );
    else
      return SubmagmaWithOneNC( S, kernel );
    fi;
    end );


#############################################################################
##
#M  KernelOfMultiplicativeGeneralMapping( <map> )
#M              . . .  for injective gen. mapping that respects mult. and one
##
InstallMethod( KernelOfMultiplicativeGeneralMapping,
    "method for an injective gen. mapping that respects mult. and one",
    [ IsGeneralMapping and RespectsMultiplication
                       and RespectsOne and IsInjective ],
    SUM_FLAGS,# can't do better in injective case
    map -> TrivialSubmagmaWithOne( Source( map ) ) );


#############################################################################
##
#M  CoKernelOfMultiplicativeGeneralMapping( <mapp> )  for finite gen. mapping
##
InstallMethod( CoKernelOfMultiplicativeGeneralMapping,
    "method for a finite general mapping",
    [ IsGeneralMapping and RespectsMultiplication and RespectsOne ],
    function( mapp )

    local R, oneS, cokernel, rel, pair;

    R:= Range( mapp );
    if IsFinite( R ) then

      oneS:= One( Source( mapp ) );
      rel:= UnderlyingRelation( mapp );
      cokernel:= Filtered( Enumerator( R ),
                           r -> DirectProductElement( [ oneS, r ] ) in rel );

    elif   IsFinite( UnderlyingRelation( mapp ) )
       and HasAsList( UnderlyingRelation( mapp ) ) then

      # Note that we must not call 'Enumerator' for the underlying
      # relation since this is allowed to call (functions that may call)
      # 'CoKernelOfMultiplicativeGeneralMapping'.
      oneS:= One( Source( mapp ) );
      cokernel:= [];
      for pair in AsList( UnderlyingRelation( mapp ) ) do
        if pair[1] = oneS then
          Add( cokernel, pair[2] );
        fi;
      od;

    else
      TryNextMethod();
    fi;

    if     IsMagmaWithInverses( R )
       and HasRespectsInverses( mapp ) and RespectsInverses( mapp ) then
      return SubmagmaWithInversesNC( R, cokernel );
    else
      return SubmagmaWithOneNC( R, cokernel );
    fi;
    end );


#############################################################################
##
#M  CoKernelOfMultiplicativeGeneralMapping( <map> )
#M            . .  for single-valued gen. mapping that respects mult. and one
##
InstallMethod( CoKernelOfMultiplicativeGeneralMapping,
    "method for a single-valued gen. mapping that respects mult. and one",
    [ IsGeneralMapping and RespectsMultiplication
                       and RespectsOne and IsSingleValued ],
    SUM_FLAGS,# can't do better in single-valued case
#T SUM_FLAGS ?
    map -> TrivialSubmagmaWithOne( Range( map ) ) );


#############################################################################
##
#M  IsSingleValued( <map> ) . .  for gen. mapping that respects mult. and one
##
InstallMethod( IsSingleValued,
    "method for a gen. mapping that respects mult. and inverses",
    [ IsGeneralMapping and RespectsMultiplication and RespectsInverses ],
    map -> IsTrivial( CoKernelOfMultiplicativeGeneralMapping( map ) ) );


#############################################################################
##
#M  IsInjective( <map> )  . for gen. mapping that respects mult. and inverses
##
InstallMethod( IsInjective,
    "method for a gen. mapping that respects mult. and one",
    [ IsGeneralMapping and RespectsMultiplication and RespectsInverses ],
    map -> IsTrivial( KernelOfMultiplicativeGeneralMapping( map ) ) );


#############################################################################
##
#M  ImagesElm( <map>, <elm> ) . . .  for s.p. gen. mapping resp. mult. & inv.
##
InstallMethod( ImagesElm,
    "method for s.p. general mapping respecting mult. & inv., and element",
    FamSourceEqFamElm,
    [ IsSPGeneralMapping and RespectsMultiplication and RespectsInverses,
      IsObject ],
    function( map, elm )
    local img;
    img:= ImagesRepresentative( map, elm );
    if img = fail then
      return [];
    else
      return RightCoset( CoKernelOfMultiplicativeGeneralMapping(map), img );
    fi;
    end );


#############################################################################
##
#M  ImagesSet( <map>, <elms> )  . .  for s.p. gen. mapping resp. mult. & inv.
##
InstallMethod( ImagesSet,
    "method for s.p. general mapping respecting mult. & inv., and group",
    CollFamSourceEqFamElms,
    [ IsSPGeneralMapping and RespectsMultiplication and RespectsInverses,
      IsGroup ],
function( map, elms )
  local genimages, img;
  # Try to map a generating set of elms; this works if and only if map
  # is defined on all of elms.
  genimages:= List( GeneratorsOfMagmaWithInverses( elms ),
		    gen -> ImagesRepresentative( map, gen ) );
  if fail in genimages then
    TryNextMethod();
  fi;

  img := SubgroupNC( Range( map ), Concatenation(
	      GeneratorsOfMagmaWithInverses(
		  CoKernelOfMultiplicativeGeneralMapping( map ) ),
	      genimages ) );
  if IsSingleValued(map) then
    # At this point we know that the restriction of map to elms is a
    # group homomorphism. Hence we can transfer some knowledge about
    # elms to img.
    if HasIsInjective(map) and IsInjective(map) then
      UseIsomorphismRelation( elms, img );
    else
      UseFactorRelation( elms, fail, img );
    fi;
  fi;
  return img;
end );

InstallMethod( ImagesSet,
    "method for injective s.p. mapping respecting mult. & inv., and group",
    CollFamSourceEqFamElms,
    [ IsSPGeneralMapping and IsMapping and IsInjective and
      RespectsMultiplication and RespectsInverses,
      IsGroup ],
    function( map, elms )
    local   img;

    img := SubgroupNC( Range( map ),
                    List( GeneratorsOfMagmaWithInverses( elms ),
                          gen -> ImagesRepresentative( map, gen ) ) );
    UseIsomorphismRelation( elms, img );
    if     IsActionHomomorphism( map )
       and HasBaseOfGroup( UnderlyingExternalSet( map ) )
       and not HasBaseOfGroup( img )
       and not HasStabChainMutable( img )  then
        if not IsBound( UnderlyingExternalSet( map )!.basePermImage )  then
            UnderlyingExternalSet( map )!.basePermImage :=
	     List(BaseOfGroup(UnderlyingExternalSet(map)),
	          b->PositionCanonical(HomeEnumerator(
		         UnderlyingExternalSet( map ) ), b ) );
        fi;
        SetBaseOfGroup( img, UnderlyingExternalSet( map )!.basePermImage );
#T is this the right place?
#T and is it allowed to access `!.basePermImage'?
    fi;
    return img;
    end );


#############################################################################
##
#M  PreImagesElm( <map>, <elm> )  .  for s.p. gen. mapping resp. mult. & inv.
##
InstallMethod( PreImagesElm,
    "method for s.p. general mapping respecting mult. & inv., and element",
    FamRangeEqFamElm,
    [ IsSPGeneralMapping and RespectsMultiplication and RespectsInverses,
      IsObject ],
      function( map, elm )
    local   pre;

    pre:= PreImagesRepresentative( map, elm );
    if pre = fail then
      return [];
    else
      return RightCoset( KernelOfMultiplicativeGeneralMapping( map ), pre );
    fi;
    end );


#############################################################################
##
#M  PreImagesSet( <map>, <elms> ) .  for s.p. gen. mapping resp. mult. & inv.
##
InstallMethod( PreImagesSet,
    "method for s.p. general mapping respecting mult. & inv., and group",
    CollFamRangeEqFamElms,
    [ IsSPGeneralMapping and RespectsMultiplication and RespectsInverses,
      IsGroup ],
  function( map, elms )
  local genpreimages,  pre;
    genpreimages:=GeneratorsOfMagmaWithInverses( elms );
    if Length(genpreimages)>0 and CanEasilyCompareElements(genpreimages[1]) then
      # remove identities
      genpreimages:=Filtered(genpreimages,i->i<>One(i));
    fi;

    genpreimages:= List(genpreimages,
                      gen -> PreImagesRepresentative( map, gen ) );
    if fail in genpreimages then
      TryNextMethod();
    fi;

    pre := SubgroupNC( Source( map ), Concatenation(
               GeneratorsOfMagmaWithInverses(
                   KernelOfMultiplicativeGeneralMapping( map ) ),
               genpreimages ) );
    if     HasSize( KernelOfMultiplicativeGeneralMapping( map ) )
       and HasSize( elms )  then
        SetSize( pre, Size( KernelOfMultiplicativeGeneralMapping( map ) )
                * Size( elms ) );
    fi;
    return pre;
    end );

InstallMethod( PreImagesSet,
    "method for injective s.p. mapping respecting mult. & inv., and group",
    CollFamRangeEqFamElms,
    [ IsSPGeneralMapping and IsMapping and IsInjective and
      RespectsMultiplication and RespectsInverses,
      IsGroup ],
    function( map, elms )
    local   pre;

    pre := SubgroupNC( Source( map ),
                    List( GeneratorsOfMagmaWithInverses( elms ),
                          gen -> PreImagesRepresentative( map, gen ) ) );
    UseIsomorphismRelation( elms, pre );
    return pre;
    end );


#############################################################################
##
##  2. methods for general mappings that respect addition
##

#############################################################################
##
#M  RespectsAddition( <mapp> )  . . . . . . . .  for a finite general mapping
##
InstallMethod( RespectsAddition,
    "method for a general mapping",
    [ IsGeneralMapping ],
    function( map )
    local S, R, enum, pair1, pair2;

    S:= Source( map );
    R:= Range(  map );
    if not ( IsAdditiveMagma( S ) and IsAdditiveMagma( R ) ) then
      return false;
    fi;

    map:= UnderlyingRelation( map );
    if not IsFinite( map ) then
      TryNextMethod();
    fi;

    enum:= Enumerator( map );
    for pair1 in enum do
      for pair2 in enum do
        if not DirectProductElement( [ pair1[1] + pair2[1], pair1[2] + pair2[2] ] )
           in map then
          return false;
        fi;
      od;
    od;
    return true;
    end );


#############################################################################
##
#M  RespectsZero( <mapp> )  . . . . . . . . . .  for a finite general mapping
##
InstallMethod( RespectsZero,
    "method for a general mapping",
    [ IsGeneralMapping ],
    function( map )
    local S, R;
    S:= Source( map );
    R:= Range(  map );
    return     IsAdditiveMagmaWithZero( S )
           and IsAdditiveMagmaWithZero( R )
           and Zero( R ) in ImagesElm( map, Zero( S ) );
    end );


#############################################################################
##
#M  RespectsAdditiveInverses( <mapp> )  . . . .  for a finite general mapping
##
InstallMethod( RespectsAdditiveInverses,
    "method for a general mapping",
    [ IsGeneralMapping ],
    function( map )
    local S, R, enum, pair;
    S:= Source( map );
    R:= Range(  map );
    if not (     IsAdditiveGroup( S )
             and IsAdditiveGroup( R ) ) then
      return false;
    fi;

    map:= UnderlyingRelation( map );
    if not IsFinite( map ) then
      TryNextMethod();
    fi;

    enum:= Enumerator( map );
    for pair in enum do
      if not DirectProductElement( [ AdditiveInverse( pair[1] ),
                      AdditiveInverse( pair[2] ) ] )
             in map then
        return false;
      fi;
    od;
    return true;
    end );


#############################################################################
##
#M  KernelOfAdditiveGeneralMapping( <mapp> )  .  for a finite general mapping
##
InstallMethod( KernelOfAdditiveGeneralMapping,
    "method for a finite general mapping",
    [ IsGeneralMapping and RespectsAddition and RespectsZero ],
    function( mapp )

    local S, zeroR, rel, kernel, pair;

    S:= Source( mapp );
    if IsFinite( Source( mapp ) ) then

      zeroR:= Zero( Range( mapp ) );
      rel:= UnderlyingRelation( mapp );
      kernel:= Filtered( Enumerator( S ),
                         s -> DirectProductElement( [ s, zeroR ] ) in rel );

    elif IsFinite( UnderlyingRelation( mapp ) ) then

      zeroR:= Zero( Range( mapp ) );
      kernel:= [];
      for pair in Enumerator( UnderlyingRelation( mapp ) ) do
        if pair[2] = zeroR then
          Add( kernel, pair[1] );
        fi;
      od;

    else
      TryNextMethod();
    fi;

    if     IsAdditiveGroup( S )
       and HasRespectsAdditiveInverses( mapp )
       and RespectsAdditiveInverses( mapp ) then
      return SubadditiveMagmaWithInversesNC( S, kernel );
    else
      return SubadditiveMagmaWithZeroNC( S, kernel );
    fi;
    end );


#############################################################################
##
#M  KernelOfAdditiveGeneralMapping( <map> )
#M              . . .  for injective gen. mapping that respects add. and zero
##
InstallMethod( KernelOfAdditiveGeneralMapping,
    "method for an injective gen. mapping that respects add. and zero",
    [ IsGeneralMapping and RespectsAddition
                       and RespectsZero and IsInjective ],
    SUM_FLAGS,# can't do better in injective case
    map -> TrivialSubadditiveMagmaWithZero( Source( map ) ) );


#############################################################################
##
#M  KernelOfAdditiveGeneralMapping( <map> ) . . . . . . . .  for zero mapping
##
InstallMethod( KernelOfAdditiveGeneralMapping,
    "method for zero mapping",
    [ IsGeneralMapping and RespectsAddition and RespectsZero and IsZero ],
    SUM_FLAGS,# can't do better for zero mapping
    Source );


#############################################################################
##
#M  CoKernelOfAdditiveGeneralMapping( <mapp> )  .  for finite general mapping
##
InstallMethod( CoKernelOfAdditiveGeneralMapping,
    "method for a finite general mapping",
    [ IsGeneralMapping and RespectsAddition and RespectsZero ],
    function( mapp )

    local R, zeroS, rel, cokernel, pair;

    R:= Range( mapp );
    if IsFinite( R ) then

      zeroS:= Zero( Source( mapp ) );
      rel:= UnderlyingRelation( mapp );
      cokernel:= Filtered( Enumerator( R ),
                           r -> DirectProductElement( [ zeroS, r ] ) in rel );

    elif   IsFinite( UnderlyingRelation( mapp ) )
       and HasAsList( UnderlyingRelation( mapp ) ) then

      # Note that we must not call 'Enumerator' for the underlying
      # relation since this is allowed to call (functions that may call)
      # 'CoKernelOfAdditiveGeneralMapping'.
      zeroS:= Zero( Source( mapp ) );
      cokernel:= [];
      for pair in AsList( UnderlyingRelation( mapp ) ) do
        if pair[1] = zeroS then
          Add( cokernel, pair[2] );
        fi;
      od;

    else
      TryNextMethod();
    fi;

    if     IsAdditiveGroup( R )
       and HasRespectsAdditiveInverses( mapp )
       and RespectsAdditiveInverses( mapp ) then
      return SubadditiveMagmaWithInversesNC( R, cokernel );
    else
      return SubadditiveMagmaWithZeroNC( R, cokernel );
    fi;
    end );


#############################################################################
##
#M  CoKernelOfAdditiveGeneralMapping( <map> )
#M            . .  for single-valued gen. mapping that respects add. and zero
##
InstallMethod( CoKernelOfAdditiveGeneralMapping,
    "method for a single-valued gen. mapping that respects add. and zero",
    [ IsGeneralMapping and RespectsAddition
                       and RespectsZero and IsSingleValued ],
    SUM_FLAGS,# can't do better in single-valued case
#T SUM_FLAGS ?
    map -> TrivialSubadditiveMagmaWithZero( Range( map ) ) );


#############################################################################
##
#M  IsSingleValued( <map> ) . for gen. mapping that respects add. & add. inv.
##
InstallMethod( IsSingleValued,
    "method for a gen. mapping that respects add. and add. inverses",
    [ IsGeneralMapping and RespectsAddition and RespectsAdditiveInverses ],
    map -> IsTrivial( CoKernelOfAdditiveGeneralMapping(map) ) );


#############################################################################
##
#M  IsInjective( <map> )  . . for gen. mapping that respects add. & add. inv.
##
InstallMethod( IsInjective,
    "method for a gen. mapping that respects add. and add. inverses",
    [ IsGeneralMapping and RespectsAddition and RespectsAdditiveInverses ],
    map -> IsTrivial( KernelOfAdditiveGeneralMapping(map) ) );


#############################################################################
##
#M  ImagesElm( <map>, <elm> ) . . for s.p. gen. mapping resp. add. & add.inv.
##
InstallMethod( ImagesElm,
    "method for s.p. gen. mapping respecting add. & add.inv., and element",
    FamSourceEqFamElm,
    [ IsSPGeneralMapping and RespectsAddition and RespectsAdditiveInverses,
      IsObject ],
    function( map, elm )
    local img;
    img:= ImagesRepresentative( map, elm );
    if img = fail then
      return [];
    else
      return AdditiveCoset( CoKernelOfAdditiveGeneralMapping( map ), img );
    fi;
    end );


#############################################################################
##
#M  ImagesSet( <map>, <elms> )  . for s.p. gen. mapping resp. add. & add.inv.
##
InstallMethod( ImagesSet,
    "method for s.p. gen. mapping resp. add. & add.inv., and add. group",
    CollFamSourceEqFamElms,
    [ IsSPGeneralMapping and RespectsAddition and RespectsAdditiveInverses,
      IsAdditiveGroup ],
    function( map, elms )
    local genimages;
    genimages:= List( GeneratorsOfAdditiveGroup( elms ),
                      gen -> ImagesRepresentative( map, gen ) );
    if fail in genimages then
      TryNextMethod();
    fi;

    return SubadditiveGroupNC( Range( map ), Concatenation(
               GeneratorsOfAdditiveGroup(
                   CoKernelOfAdditiveGeneralMapping( map ) ),
               genimages ) );
    end );


#############################################################################
##
#M  PreImagesElm( <map>, <elm> )  for s.p. gen. mapping resp. add. & add.inv.
##
InstallMethod( PreImagesElm,
    "method for s.p. gen. mapping respecting add. & add.inv., and element",
    FamRangeEqFamElm,
    [ IsSPGeneralMapping and RespectsAddition and RespectsAdditiveInverses,
      IsObject ],
      function( map, elm )
    local   pre;

    pre:= PreImagesRepresentative( map, elm );
    if pre = fail then
      return [];
    else
      return AdditiveCoset( KernelOfAdditiveGeneralMapping( map ), pre );
    fi;
    end );


#############################################################################
##
#M  PreImagesSet( <map>, <elms> ) for s.p. gen. mapping resp. add. & add.inv.
##
InstallMethod( PreImagesSet,
    "method for s.p. gen. mapping resp. add. & add.inv., and add. group",
    CollFamRangeEqFamElms,
    [ IsSPGeneralMapping and RespectsAddition and RespectsAdditiveInverses,
      IsAdditiveGroup ],
    function( map, elms )
    local genpreimages;
    genpreimages:= List( GeneratorsOfAdditiveGroup( elms ),
                      gen -> PreImagesRepresentative( map, gen ) );
    if fail in genpreimages then
      TryNextMethod();
    fi;

    return SubadditiveGroupNC( Source( map ), Concatenation(
               GeneratorsOfAdditiveGroup(
                   KernelOfAdditiveGeneralMapping( map ) ),
               genpreimages ) );
    end );


#############################################################################
##
##  3. methods for general mappings that respect scalar multiplication
##


#############################################################################
##
#M  RespectsScalarMultiplication( <mapp> )  . .  for a finite general mapping
##
InstallMethod( RespectsScalarMultiplication,
    "method for a general mapping",
    [ IsGeneralMapping ],
    function( map )

    local S, R, D, pair, c;

    S:= Source( map );
    R:= Range(  map );
    if not ( IsLeftModule( S ) and IsLeftModule( R ) ) then
      return false;
    fi;

    D:= LeftActingDomain( S );
    if not IsSubset( LeftActingDomain( R ), D ) then
#T subset is allowed?
      return false;
    fi;

    map:= UnderlyingRelation( map );

    if not IsFinite( D ) or not IsFinite( map ) then
      Error( "cannot determine whether the infinite mapping <map> ",
             "respects scalar multiplication" );
    else
      D:= Enumerator( D );
      for pair in Enumerator( map ) do
        for c in D do
          if not DirectProductElement( [ c * pair[1], c * pair[2] ] ) in map then
            return false;
          fi;
        od;
      od;
      return true;
    fi;
    end );


#############################################################################
##
#M  KernelOfAdditiveGeneralMapping( <mapp> )  . . for a finite linear mapping
##
##  We need a special method for being able to return a left module.
##
InstallMethod( KernelOfAdditiveGeneralMapping,
    "method for a finite linear mapping",
    [ IsGeneralMapping and RespectsAddition and RespectsZero
                       and RespectsScalarMultiplication ],
    function( mapp )

    local S, zeroR, rel, kernel, pair;

    S:= Source( mapp );
    if not IsExtLSet( S ) then
      TryNextMethod();
    fi;

    if IsFinite( S ) then

      zeroR:= Zero( Range( mapp ) );
      rel:= UnderlyingRelation( mapp );
      kernel:= Filtered( Enumerator( S ),
                         s -> DirectProductElement( [ s, zeroR ] ) in rel );

    elif IsFinite( UnderlyingRelation( mapp ) ) then

      zeroR:= Zero( Range( mapp ) );
      kernel:= [];
      for pair in Enumerator( UnderlyingRelation( mapp ) ) do
        if pair[2] = zeroR then
          Add( kernel, pair[1] );
        fi;
      od;

    else
      TryNextMethod();
    fi;

    return LeftModuleByGenerators( LeftActingDomain( S ), kernel );
    end );


#############################################################################
##
#M  CoKernelOfAdditiveGeneralMapping( <mapp> )  . . for finite linear mapping
##
##  We need a special method for being able to return a left module.
##
InstallMethod( CoKernelOfAdditiveGeneralMapping,
    "method for a finite linear mapping",
    [ IsGeneralMapping and RespectsAddition and RespectsZero
                       and RespectsScalarMultiplication ],
    function( mapp )

    local R, zeroS, rel, cokernel, pair;

    R:= Range( mapp );
    if not IsExtLSet( R ) then
      TryNextMethod();
    fi;

    if IsFinite( R ) then

      zeroS:= Zero( Source( mapp ) );
      rel:= UnderlyingRelation( mapp );
      cokernel:= Filtered( Enumerator( R ),
                           r -> DirectProductElement( [ zeroS, r ] ) in rel );

    elif IsFinite( UnderlyingRelation( mapp ) ) then

      zeroS:= Zero( Source( mapp ) );
      cokernel:= [];
      for pair in Enumerator( UnderlyingRelation( mapp ) ) do
        if pair[1] = zeroS then
          Add( cokernel, pair[2] );
        fi;
      od;

    else
      TryNextMethod();
    fi;

    return LeftModuleByGenerators( LeftActingDomain( R ), cokernel );
    end );


#############################################################################
##
#M  ImagesSet( <map>, <elms> )  . . . . .  for linear mapping and left module
##
InstallMethod( ImagesSet,
    "method for linear mapping and left module",
    CollFamSourceEqFamElms,
    [ IsSPGeneralMapping and RespectsAddition and RespectsAdditiveInverses
          and RespectsScalarMultiplication,
      IsLeftModule ],
    function( map, elms )
    local genimages;
    genimages:= List( GeneratorsOfLeftModule( elms ),
                      gen -> ImagesRepresentative( map, gen ) );
    if fail in genimages then
      TryNextMethod();
    fi;

    return SubmoduleNC( Range( map ), Concatenation(
               GeneratorsOfLeftModule(
                   CoKernelOfAdditiveGeneralMapping( map ) ),
               genimages ) );
    end );


#############################################################################
##
#M  PreImagesSet( <map>, <elms> ) . . . .  for linear mapping and left module
##
InstallMethod( PreImagesSet,
    "method for linear mapping and left module",
    CollFamRangeEqFamElms,
    [ IsSPGeneralMapping and RespectsAddition and RespectsAdditiveInverses
          and RespectsScalarMultiplication,
      IsLeftModule ],
    function( map, elms )
    local genpreimages;
    genpreimages:= List( GeneratorsOfLeftModule( elms ),
                         gen -> PreImagesRepresentative( map, gen ) );
    if fail in genpreimages then
      TryNextMethod();
    fi;

    return SubmoduleNC( Source( map ), Concatenation(
               GeneratorsOfLeftModule(
                   KernelOfAdditiveGeneralMapping( map ) ),
               genpreimages ) );
    end );


#############################################################################
##
##  4. properties and attributes of gen. mappings that respect multiplicative
##     and additive structure
##

#############################################################################
##
#M  IsFieldHomomorphism( <mapp> )
##
InstallMethod( IsFieldHomomorphism,
    "method for a general mapping",
    [ IsGeneralMapping ],
    map -> IsRingHomomorphism( map ) and IsField( Source( map ) ) );


#############################################################################
##
#M  ImagesSet( <map>, <elms> )  . . . . . . . . . for algebra hom. and FLMLOR
##
InstallMethod( ImagesSet,
    "method for algebra hom. and FLMLOR",
    CollFamSourceEqFamElms,
    [ IsSPGeneralMapping and RespectsAddition and RespectsAdditiveInverses
          and RespectsScalarMultiplication and RespectsMultiplication,
      IsFLMLOR ],
    function( map, elms )
    local genimages;
    genimages:= List( GeneratorsOfLeftOperatorRing( elms ),
                      gen -> ImagesRepresentative( map, gen ) );
    if fail in genimages then
      TryNextMethod();
    fi;

    return SubFLMLORNC( Range( map ), Concatenation(
               GeneratorsOfLeftOperatorRing(
                   CoKernelOfAdditiveGeneralMapping( map ) ),
               genimages ) );
#T handle the case of ideals!
    end );


#############################################################################
##
#M  ImagesSet( <map>, <elms> )  .  for alg.-with-one hom. and FLMLOR-with-one
##
InstallMethod( ImagesSet,
    "method for algebra-with-one hom. and FLMLOR-with-one",
    CollFamSourceEqFamElms,
    [ IsSPGeneralMapping and RespectsAddition and RespectsAdditiveInverses
          and RespectsScalarMultiplication and RespectsMultiplication
          and RespectsOne,
      IsFLMLORWithOne ],
    function( map, elms )
    local genimages;
    genimages:= List( GeneratorsOfLeftOperatorRingWithOne( elms ),
                      gen -> ImagesRepresentative( map, gen ) );
    if fail in genimages then
      TryNextMethod();
    fi;

    return SubFLMLORWithOneNC( Range( map ), Concatenation(
               GeneratorsOfLeftOperatorRingWithOne(
                   CoKernelOfAdditiveGeneralMapping( map ) ),
               genimages ) );
#T handle the case of ideals!
    end );


#############################################################################
##
#M  PreImagesSet( <map>, <elms> ) . . . . . . . . for algebra hom. and FLMLOR
##
InstallMethod( PreImagesSet,
    "method for algebra hom. and FLMLOR",
    CollFamRangeEqFamElms,
    [ IsSPGeneralMapping and RespectsAddition and RespectsAdditiveInverses
          and RespectsScalarMultiplication and RespectsMultiplication,
      IsFLMLOR ],
    function( map, elms )
    local genpreimages;
    genpreimages:= List( GeneratorsOfLeftOperatorRing( elms ),
                         gen -> PreImagesRepresentative( map, gen ) );
    if fail in genpreimages then
      TryNextMethod();
    fi;

    return SubFLMLORNC( Source( map ), Concatenation(
               GeneratorsOfLeftOperatorRing(
                   KernelOfAdditiveGeneralMapping( map ) ),
               genpreimages ) );
#T handle the case of ideals!
    end );


#############################################################################
##
#M  PreImagesSet( <map>, <elms> )  for alg.-with-one hom. and FLMLOR-with-one
##
InstallMethod( PreImagesSet,
    "method for algebra-with-one hom. and FLMLOR-with-one",
    CollFamRangeEqFamElms,
    [ IsSPGeneralMapping and RespectsAddition and RespectsAdditiveInverses
          and RespectsScalarMultiplication and RespectsMultiplication
          and RespectsOne,
      IsFLMLORWithOne ],
    function( map, elms )
    local genpreimages;
    genpreimages:= List( GeneratorsOfLeftOperatorRingWithOne( elms ),
                         gen -> PreImagesRepresentative( map, gen ) );
    if fail in genpreimages then
      TryNextMethod();
    fi;

    return SubFLMLORNC( Source( map ), Concatenation(
               GeneratorsOfLeftOperatorRingWithOne(
                   KernelOfAdditiveGeneralMapping( map ) ),
               genpreimages ) );
#T handle the case of ideals!
    end );


#############################################################################
##
##  5. default equality tests for structure preserving mappings
##
##  The default methods for equality tests of single-valued and structure
##  preserving general mappings first check some necessary conditions:
##  Source and range of both must be equal, and if both know whether they
##  are injective, surjective or total, the values must be equal if the
##  general mappings are equal.
##
##  In the second step, appropriate generators of the preimage of the general
##  mappings are considered.
##  If the general mapping respects multiplication, one, inverses, addition,
##  zero, additive inverses, scalar multiplication then
##  the preimage is a magma, magma-with-one, magma-with-inverses,
##  additive-magma, additive-magma-with-zero, additive-magma-with-inverses,
##  respectively.
##  So the general mappings are equal if the images of the appropriate
##  generators are equal.
##


#############################################################################
##
#M  \=( <map1>, <map2> )  . . . . . . . . . . . . . . . .  for s.v. gen. map.
##
InstallEqMethodForMappingsFromGenerators( IsObject,
    AsList,
    IsObject,
    "" );


#############################################################################
##
#M  \=( <map1>, <map2> )  . . . . . . . . . .  for s.v. gen. map. resp. mult.
##
InstallEqMethodForMappingsFromGenerators( IsMagma,
    GeneratorsOfMagma,
    RespectsMultiplication,
    " that respect mult." );


#############################################################################
##
#M  \=( <map1>, <map2> )  . . . . . .  for s.v. gen. map. resp. mult. and one
##
InstallEqMethodForMappingsFromGenerators( IsMagmaWithOne,
    GeneratorsOfMagmaWithOne,
    RespectsMultiplication and RespectsOne,
    " that respect mult. and one" );


#############################################################################
##
#M  \=( <map1>, <map2> )  . . . . . . for s.v. gen. map. resp. mult. and inv.
##
InstallEqMethodForMappingsFromGenerators( IsMagmaWithInverses,
    GeneratorsOfMagmaWithInverses,
    RespectsMultiplication and RespectsInverses,
    " that respect mult. and inv." );


#############################################################################
##
#M  \=( <map1>, <map2> )  . . . . . . . . . . . for s.v. gen. map. resp. add.
##
InstallEqMethodForMappingsFromGenerators( IsAdditiveMagma,
    GeneratorsOfAdditiveMagma,
    RespectsAddition,
    " that respect add." );


#############################################################################
##
#M  \=( <map1>, <map2> )  . . . . . .  for s.v. gen. map. resp. add. and zero
##
InstallEqMethodForMappingsFromGenerators( IsAdditiveMagmaWithZero,
    GeneratorsOfAdditiveMagmaWithZero,
    RespectsAddition and RespectsZero,
    " that respect add. and zero" );


#############################################################################
##
#M  \=( <map1>, <map2> )  . . . . for s.v. gen. map. resp. add. and add. inv.
##
InstallEqMethodForMappingsFromGenerators( IsAdditiveGroup,
    GeneratorsOfAdditiveGroup,
    RespectsAddition and RespectsAdditiveInverses,
    " that respect add. and add. inv." );


#############################################################################
##
#M  \=( <map1>, <map2> )  . . .  for s.v. gen. map. resp. mult.,add.,add.inv.
##
InstallEqMethodForMappingsFromGenerators( IsRing,
    GeneratorsOfRing,
    RespectsMultiplication and
    RespectsAddition and RespectsAdditiveInverses,
    " that respect mult.,add.,add.inv." );


#############################################################################
##
#M  \=( <map1>, <map2> )  .  for s.v. gen. map. resp. mult.,one,add.,add.inv.
##
InstallEqMethodForMappingsFromGenerators( IsRingWithOne,
    GeneratorsOfRingWithOne,
    RespectsMultiplication and RespectsOne and
    RespectsAddition and RespectsAdditiveInverses,
    " that respect mult.,one,add.,add.inv." );


#############################################################################
##
#M  \=( <map1>, <map2> )   for s.v. gen. map. resp. add.,add.inv.,scal. mult.
##
InstallEqMethodForMappingsFromGenerators( IsLeftModule,
    GeneratorsOfLeftModule,
    RespectsAddition and RespectsAdditiveInverses and
    RespectsScalarMultiplication,
    " that respect add.,add.inv.,scal. mult." );


#############################################################################
##
#M  \=( <map1>, <map2> )   for s.v.g.m. resp. add.,add.inv.,mult.,scal. mult.
##
InstallEqMethodForMappingsFromGenerators( IsLeftOperatorRing,
    GeneratorsOfLeftOperatorRing,
    RespectsAddition and RespectsAdditiveInverses and
    RespectsMultiplication and RespectsScalarMultiplication,
    " that respect add.,add.inv.,mult.,scal. mult." );


#############################################################################
##
#M  \=( <map1>, <map2> )   s.v.g.m. resp. add.,add.inv.,mult.,one,scal. mult.
##
InstallEqMethodForMappingsFromGenerators( IsLeftOperatorRingWithOne,
    GeneratorsOfLeftOperatorRingWithOne,
    RespectsAddition and RespectsAdditiveInverses and
    RespectsMultiplication and RespectsOne and RespectsScalarMultiplication,
    " that respect add.,add.inv.,mult.,one,scal. mult." );


#############################################################################
##
#M  \=( <map1>, <map2> )   s.v.g.m. resp. add.,add.inv.,mult.,one,scal. mult.
##
InstallEqMethodForMappingsFromGenerators( IsField,
    GeneratorsOfField,
    IsFieldHomomorphism,
    " that is a field homomorphism" );


#############################################################################
##
#E