This file is indexed.

/usr/share/gap/lib/trans.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
#############################################################################
##
#W  trans.gi           GAP library                          James D. Mitchell
##
##
#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) 2012 The GAP Group
##

InstallMethod(NumberTransformation, "for a transformation", 
[IsTransformation], 
function(t) 
  local l, n, a, i; 
  n := DegreeOfTransformation(t); 
  a := 0; 
  for i in [1..n] do 
      a := a*n + i^t-1; 
  od; 
  return a+1;   # to be in [1..n^n] 
end); 

#

InstallMethod(NumberTransformation, 
"for a transformation and zero",
[IsTransformation, IsZeroCyc], 
function(t, n) 
  return 1;  
end);

#

InstallMethod(NumberTransformation, 
"for a transformation and positive integer", 
[IsTransformation, IsPosInt], 
function(t, n) 
  local l, a, i; 
  a := 0; 
  for i in [1..n] do 
    a := a*n + i^t-1; 
  od; 
  return a+1;   # to be in [1..n^n] 
end); 

InstallMethod(TransformationNumber,  
"for a positive integer and positive integer", 
[IsPosInt, IsPosInt],
function(a,n) 
  local l, q, i; 
  l := EmptyPlist(n); 
  a := a - 1;   # to be in [0..n^n-1] 
  for i in [n, n-1..1] do  
      q := QuotientRemainder(Integers,a,n); 
      l[i] := q[2]+1; 
      a := q[1]; 
  od; 
  return TransformationNC(l); 
end); 

#

InstallMethod(LT, "for a transformation and cyclotomic", 
[IsTransformation, IsCyclotomic], ReturnFalse);

InstallMethod(LT, "for a cyclotomic and transformation", 
[IsCyclotomic, IsTransformation], ReturnTrue);

InstallMethod(LT, "for a finite field element and transformation", 
[IsFFE, IsTransformation], ReturnFalse);

InstallMethod(LT, "for a transformation and finite field element", 
[IsTransformation, IsFFE], ReturnTrue);

#

InstallMethod(IsGeneratorsOfInverseSemigroup, 
"for a transformation collection", 
[IsTransformationCollection], IsGeneratorsOfMagmaWithInverses);

#
#

InstallMethod(IsGeneratorsOfInverseSemigroup, 
"for a transformation collection", 
[IsTransformationCollection], IsGeneratorsOfMagmaWithInverses);

#

InstallMethod(IsGeneratorsOfMagmaWithInverses,
 "for a transformation collection",
[IsTransformationCollection],
coll-> ForAll(coll, x-> RankOfTransformation(x)=DegreeOfTransformation(x)));

#

InstallMethod(TransformationList, "for a list", [IsList], Transformation);

#

InstallMethod(Transformation, "for a list", [IsList],
function(list)
  local len;
  len:=Length(list);
  if IsDenseList(list) and ForAll(list, i-> IsPosInt(i) and i<=len) then 
    return TransformationNC(list);
  fi;
  return fail;
end);

#

InstallMethod(TransformationListList, "for a list and list",
[IsList, IsList],
function(src, ran)
  if ForAll(src, IsPosInt) and ForAll(ran, IsPosInt) and IsDenseList(src) and
    IsDenseList(ran) and Length(ran)=Length(src) then 
    return TransformationListListNC(src, ran);
  fi;
  return fail;
end);

#

InstallMethod(Transformation, "for a list and list", 
[IsList, IsList], TransformationListList);

InstallMethod(Transformation, "for a list and function",
[IsList, IsFunction], 
function(list, func)
  return TransformationListList(list, List(list, func));
end);

#

InstallMethod(TrimTransformation, "for a transformation and pos int",
[IsTransformation, IsPosInt], TRIM_TRANS);

InstallMethod(TrimTransformation, "for a transformation",
[IsTransformation], 
function(f)
  TRIM_TRANS(f, DegreeOfTransformation(f));
  return;
end);

#

InstallMethod(OnKernelAntiAction, "for a list and transformation", 
[IsDenseList and IsHomogeneousList, IsTransformation], 
function(ker, f)
  local m, i;
  
  if Length(ker)=0 or not IsPosInt(ker[1]) then 
    Error("usage: the first argument <ker> must be a non-empty dense\n", 
    "list of positive integers,");
  fi;
  
  m:=1;
  for i in ker do 
    if i>m then 
      if m+1<>i then 
        Error("usage: the first argument <ker> does not describe the\n",
        " flat kernel of a transformation,");
      fi;
      m:=m+1;
    fi;
  od;
  return ON_KERNEL_ANTI_ACTION(ker, f, 0);
end);

#

InstallMethod(RankOfTransformation, "for a transformation",
[IsTransformation], RANK_TRANS);

#InstallMethod(CoRankOfTransformation, "for a transformation",
#[IsTransformation], CORANK_TRANS);

InstallMethod(RankOfTransformation, "for a transformation and pos int",
[IsTransformation, IsPosInt], RANK_TRANS_INT);

InstallMethod(RankOfTransformation, "for a transformation and zero",
[IsTransformation, IsZeroCyc], function(x, y) return 0; end);

#InstallMethod(CoRankOfTransformation, "for a transformation",
#[IsTransformation], CORANK_TRANS_INT);

InstallMethod(RankOfTransformation, "for a transformation and dense list",
[IsTransformation, IsDenseList], 
function(f, list)
  if IsEmpty(list) then 
    return 0;
  fi;
  return RANK_TRANS_LIST(f, list);
end);

InstallMethod(LargestMovedPoint, "for a transformation", 
[IsTransformation], LARGEST_MOVED_PT_TRANS); 

InstallMethod(LargestImageOfMovedPoint, "for a transformation", 
[IsTransformation], LARGEST_IMAGE_PT); 

InstallMethod(SmallestMovedPoint, "for a transformation", 
[IsTransformation], 
function(f)
  if IsOne(f) then 
    return infinity;
  fi;
  return SMALLEST_MOVED_PT_TRANS(f);
end);

InstallMethod(SmallestImageOfMovedPoint, "for a transformation", 
[IsTransformation], 
function(f)
  if IsOne(f) then 
    return infinity;
  fi;
  return SMALLEST_IMAGE_PT(f);
end);

InstallMethod(MovedPoints, "for a tranformation",
[IsTransformation], MOVED_PTS_TRANS);

InstallMethod(NrMovedPoints, "for a tranformation",
[IsTransformation], NR_MOVED_PTS_TRANS);

InstallMethod(MovedPoints, "for a transformation coll",
[IsTransformationCollection], coll-> Union(List(coll, MovedPoints)));

InstallMethod(NrMovedPoints, "for a transformation coll",
[IsTransformationCollection], coll-> Length(MovedPoints(coll)));

InstallMethod(LargestMovedPoint, "for a transformation collection",
[IsTransformationCollection], coll-> Maximum(List(coll, LargestMovedPoint)));

InstallMethod(LargestImageOfMovedPoint, "for a transformation collection",
[IsTransformationCollection], 
coll-> Maximum(List(coll, LargestImageOfMovedPoint)));

InstallMethod(SmallestMovedPoint, "for a transformation collection",
[IsTransformationCollection], coll-> Minimum(List(coll, SmallestMovedPoint)));

InstallMethod(SmallestImageOfMovedPoint, "for a transformation collection",
[IsTransformationCollection], coll-> Minimum(List(coll, SmallestImageOfMovedPoint)));

InstallMethod(RightOne, "for a transformation", 
[IsTransformation], RIGHT_ONE_TRANS);

InstallMethod(LeftOne, "for a transformation", 
[IsTransformation], LEFT_ONE_TRANS);

InstallMethod(ComponentsOfTransformation, "for a transformation",
[IsTransformation], COMPONENTS_TRANS);

InstallMethod(NrComponentsOfTransformation, "for a transformation",
[IsTransformation], NR_COMPONENTS_TRANS);

InstallMethod(ComponentRepsOfTransformation, "for a transformation",
[IsTransformation], COMPONENT_REPS_TRANS);

InstallMethod(ComponentTransformationInt, 
"for a transformation and positive integer",
[IsTransformation, IsPosInt], COMPONENT_TRANS_INT);

InstallMethod(CycleTransformationInt, 
"for a transformation and positive integer",
[IsTransformation, IsPosInt], CYCLE_TRANS_INT);

#this could be better JDM, could use ImageSetOfTransformation
InstallMethod(CyclesOfTransformation, 
"for a transformation",
[IsTransformation], f-> CYCLES_TRANS_LIST(f, [1..DegreeOfTransformation(f)]));

InstallMethod(CyclesOfTransformation, 
"for a transformation and list",
[IsTransformation, IsList], 
function(f, list)
  if IsDenseList(list) and ForAll(list, IsPosInt) then 
    return CYCLES_TRANS_LIST(f, list);
  fi;
  Error("usage: the second argument must be a dense list of positive integers");
  return;
end);

InstallMethod(IsOne, "for a transformation",
[IsTransformation], IS_ID_TRANS);

InstallMethod(IsIdempotent, "for a transformation", 
[IsTransformation], IS_IDEM_TRANS);

InstallMethod(AsPermutation, "for a transformation", 
[IsTransformation], AS_PERM_TRANS); 

InstallMethod(PermutationOfImage, "for a transformation",
[IsTransformation], PERM_IMG_TRANS);

InstallMethod(AsTransformation, "for a permutation",
[IsPerm], AS_TRANS_PERM);

InstallMethod(AsTransformation, "for a permutation and positive integer", 
[IsPerm, IsInt], 
function(f, n)
  if 0<=n then 
    return AS_TRANS_PERM_INT(f, n);
  fi;
  return fail;
end);

InstallMethod(AsTransformation, "for a transformation",
[IsTransformation], IdFunc); 

InstallMethod(AsTransformation, "for a transformation and degree", 
[IsTransformation, IsInt], 
function(f, n)
  if 0<=n then 
    return AS_TRANS_TRANS(f, n);
  fi;
  return fail;
end);

#

InstallMethod(ConstantTransformation, "for a pos int and pos int",
[IsPosInt, IsPosInt],
function(m,n)
  if m<n then 
    Error("usage: the arguments should be positive integers with the first",
    " greater\nthan or equal to the second,");
    return;
  fi;
  return Transformation(ListWithIdenticalEntries(m, n));;
end);

#

InstallMethod(DegreeOfTransformationCollection, 
"for a transformation collection",
[IsTransformationCollection], 
function(coll)
  return MaximumList(List(coll, DegreeOfTransformation));
end);

#

InstallMethod(FlatKernelOfTransformation, "for a transformation", 
[IsTransformation], x-> FLAT_KERNEL_TRANS_INT(x, DegreeOfTransformation(x)));

#

InstallMethod(FlatKernelOfTransformation, "for a transformation and pos int", 
[IsTransformation, IsPosInt], FLAT_KERNEL_TRANS_INT);

#

InstallMethod(FlatKernelOfTransformation, "for a transformation and zero", 
[IsTransformation, IsZeroCyc], function(x, y) return []; end);

#

InstallMethod(ImageSetOfTransformation, "for a transformation",
[IsTransformation], x-> IMAGE_SET_TRANS_INT(x, DegreeOfTransformation(x)));

#

InstallMethod(ImageSetOfTransformation, "for a transformation and pos int",
[IsTransformation, IsPosInt], IMAGE_SET_TRANS_INT);

#

InstallMethod(ImageSetOfTransformation, "for a transformation and zero",
[IsTransformation, IsZeroCyc], function(x, y) return []; end);

#

InstallMethod(ImageListOfTransformation, "for a transformation and pos int", 
[IsTransformation, IsPosInt], IMAGE_TRANS);

#

InstallMethod(ImageListOfTransformation, "for a transformation and zero", 
[IsTransformation, IsZeroCyc], function(x, y) return []; end);

#

InstallMethod(ImageListOfTransformation, "for a transformation", 
[IsTransformation], x-> IMAGE_TRANS(x, DegreeOfTransformation(x)));

#

InstallMethod(IndexPeriodOfTransformation, "for a transformation",
[IsTransformation], INDEX_PERIOD_TRANS);

#

InstallMethod(Order, "for a transformation",
[IsTransformation], x-> Sum(INDEX_PERIOD_TRANS(x))-1);

#

InstallMethod(IsInjectiveListTrans, "for a list and list",
[IsList, IsList], IS_INJECTIVE_LIST_TRANS);

#

InstallMethod(IsInjectiveListTrans, "for a list and trans",
[IsList, IsTransformation], IS_INJECTIVE_LIST_TRANS);

#

InstallMethod(KernelOfTransformation, "for a transformation", 
[IsTransformation], x-> KERNEL_TRANS(x, DegreeOfTransformation(x)));

#JDM could do better than this

InstallMethod(KernelOfTransformation, 
"for a transformation, positive integer and boolean", 
[IsTransformation, IsPosInt, IsBool], 
function(f, n, bool)
  if bool then 
    return KERNEL_TRANS(f, n);
  fi; 
  n:=Minimum(DegreeOfTransformation(f), n);
  return Filtered(KERNEL_TRANS(f, n), x-> Size(x)<>1);
end);

#

InstallMethod(KernelOfTransformation, "for a transformation and pos int", 
[IsTransformation, IsPosInt], KERNEL_TRANS);

#

InstallMethod(KernelOfTransformation, "for a transformation and pos int", 
[IsTransformation, IsBool],
function(f, bool)
  return KernelOfTransformation(f, DegreeOfTransformation(f), bool);
end);

#

InstallOtherMethod(OneMutable, "for a transformation coll",
[IsTransformationCollection], coll-> IdentityTransformation);

#

InstallMethod(PermLeftQuoTransformationNC, 
"for a transformation and transformation",
[IsTransformation, IsTransformation],
PERM_LEFT_QUO_TRANS_NC);

#

InstallMethod(PermLeftQuoTransformation, 
"for a transformation and transformation", [IsTransformation, IsTransformation],
function(f, g)
  local n;
  n:=Maximum(DegreeOfTransformation(f), DegreeOfTransformation(g));
  if FlatKernelOfTransformation(f, n)<>FlatKernelOfTransformation(g, n) or
    ImageSetOfTransformation(f, n)<>ImageSetOfTransformation(g, n) then 
    Error("usage: the arguments must have equal image set and kernel,");
    return;
  fi;
  return PERM_LEFT_QUO_TRANS_NC(f, g);
end);

#

InstallMethod(PreImagesOfTransformation, 
"for a transformation and positive integer",
[IsTransformation, IsPosInt], PREIMAGES_TRANS_INT);

#

InstallMethod(DisplayString, "for a transformation", 
[IsTransformation], ViewString);

#

InstallMethod(String, "for a transformation",
[IsTransformation], 
function(f) 
  local img, str, i;
 
  if IsOne(f) then 
    return "<identity transformation>";
  fi;
  img:=ImageListOfTransformation(f, DegreeOfTransformation(f));
  str:=ShallowCopy(STRINGIFY("[ ", img[1]));
  for i in [2..Length(img)] do
    Append(str, ", ");
    Append(str, String(img[i]));
  od;
  Append(str, " ]");

  return STRINGIFY("Transformation( ", str, " )");
end);

#

InstallMethod(PrintString, "for a transformation",
[IsTransformation], 
function(f) 
  local img, str, i;

  if IsOne(f) then 
    return "\>IdentityTransformation\<";
  fi;
  
  img:=ImageListOfTransformation(f, DegreeOfTransformation(f));
  str:=PRINT_STRINGIFY("[ ", img[1]);
  for i in [2..Length(img)] do 
    Append(str, ",\> ");
    Append(str, PrintString(img[i]));
    Append(str, "\<");
  od;
  Append(str, " ]");
  return Concatenation("\>Transformation( ", str, " )\<");
end);

#

InstallMethod(ViewString, "for a transformation",
[IsTransformation],
function(f)
  local img, str, deg, i;

  if LargestMovedPoint(f)<UserPreference("TransformationDisplayLimit")
    then  
    if UserPreference("NotationForTransformations")="input" 
      and
      LargestImageOfMovedPoint(f)<UserPreference("TransformationDisplayLimit")
      then 
      return PrintString(f);
    elif UserPreference("NotationForTransformations")="fr" then 
      if IsOne(f) then 
        return "\><identity transformation>\<";
      fi;
      img:=ImageListOfTransformation(f, LargestMovedPoint(f));
      str:=PRINT_STRINGIFY("\>\><transformation: \<", img[1]);
      for i in [2..Length(img)] do 
        Append(str, ",\>");
        Append(str, PrintString(img[i]));
        Append(str, "\<");
      od;
      Append(str, ">\<\<");
      return str;
    fi;
  fi; 
  deg:=DegreeOfTransformation(f);
  return STRINGIFY("\><transformation on ", deg, " pts with rank ",
    RankOfTransformation(f, deg), ">\<"); 
end);

#

InstallMethod(RandomTransformation, "for a pos. int.", [IsPosInt],
function(n)
  local out, i;

  out:=EmptyPlist(n);
  n:=[1..n];

  for i in n do 
    out[i]:=Random(n);
  od;
  return TransformationNC(out);
end);

#

InstallMethod(RandomTransformation, "for pos int and pos int",
[IsPosInt, IsPosInt],
function(deg, rank)
  local dom, seen, im, set, nr, i;

  dom:=[1..deg];
  seen:=BlistList(dom, []);
  im:=EmptyPlist(deg);
  set:=EmptyPlist(rank);
  nr:=0; i:=0;
  
  while nr<rank and i<deg do 
    i:=i+1;
    im[i]:=Random(dom);
    if not seen[im[i]] then
      seen[im[i]]:=true;
      nr:=nr+1;
      AddSet(set, im[i]);
    fi;
  od;
  
  while i<deg do 
    i:=i+1;
    im[i]:=Random(set);
  od;
  return Transformation(im);
end);

#

InstallMethod(RestrictedTransformationNC, 
"for a transformation and list",
[IsTransformation, IsList], RESTRICTED_TRANS);

#

InstallMethod(RestrictedTransformation, 
"for transformation and list",
[IsTransformation, IsList],
function(f, list)

  if not (IsDenseList(list) and IsDuplicateFree(list) 
    and ForAll(list, IsPosInt)) then 
    Error("usage: the second argument should be a duplicate-free, dense ", 
    "list of positive integers,");
    return;
  fi;
  return RESTRICTED_TRANS(f, list);
end);

#

InstallMethod(SmallestIdempotentPower, "for a transformation",
[IsTransformation], SMALLEST_IDEM_POW_TRANS);

#

InstallMethod(TransformationByImageAndKernel, 
"for a list of positive integers and a list of positive integers",
[IsCyclotomicCollection and IsDenseList, 
 IsCyclotomicCollection and IsDenseList],
function(img, ker)
  
  if ForAll(ker, IsPosInt) and ForAll(img, IsPosInt) and
   Maximum(ker)=Length(img) and IsDuplicateFreeList(img) and 
   ForAll(img, x-> x<=Length(ker)) then 
    return TRANS_IMG_KER_NC(img, ker);
  fi;
  return fail;
end);

#

InstallMethod(Idempotent, "for a list of pos ints and list of pos ints",
[IsCyclotomicCollection, IsCyclotomicCollection],
function(img, ker)
  local lookup, m, i;

  if ForAll(ker, IsPosInt) and ForAll(img, IsPosInt) and 
   Maximum(ker)=Length(img) and IS_INJECTIVE_LIST_TRANS(img, ker) and
   IsSet(img) and ForAll(img, x-> x<=Length(ker)) then 
   return IDEM_IMG_KER_NC(img, ker);
  fi;
  return fail;
end);

# based on PermutationOp in oprt.gi

InstallMethod(TransformationOp, "for object, list, function",
[IsObject, IsList, IsFunction],
function(f, D, act)
  local perm, out, new, i, pnt;

  perm:=();

  if IsPlistRep(D) and Length(D)>2 and CanEasilySortElements(D[1]) then 
    if not IsSSortedList(D) then 
      D:=ShallowCopy(D);
      perm:=Sortex(D);
      D:=Immutable(D);
      SetIsSSortedList(D, true);
    fi;
  fi;

  out:=EmptyPlist(Length(D));
  i:=0;

  for pnt in D do 
    new:=PositionCanonical(D, act(pnt, f));
    if new=fail then 
      return fail;
    fi;
    i:=i+1;
    out[i]:=new;
  od;

  out:=Transformation(out);
  
  if not IsOne(perm) then 
    out:=out^(perm^-1);
  fi;

  return out;
end);

#

InstallMethod(TransformationOp, "for an obj and list",
[IsObject, IsList], 
function(obj, list) 
  return TransformationOp(obj, list, OnPoints);
end);

#

InstallMethod(TransformationOp, "for an obj and domain",
[IsObject, IsDomain], 
function(obj, D) 
  return TransformationOp(obj, Enumerator(D), OnPoints);
end);

#

InstallMethod(TransformationOp, "for an obj, domain, and function",
[IsObject, IsDomain, IsFunction], 
function(obj, D, func) 
  return TransformationOp(obj, Enumerator(D), func);
end);

# based on PermutationOp in oprt.gi

# same as the above except no check that PositionCanonical is not fail 

InstallMethod(TransformationOpNC, "for object, list, function",
[IsObject, IsList, IsFunction],
function(f, D, act)
  local perm, out, i, pnt;

  perm:=();

  if IsPlistRep(D) and Length(D)>2 and CanEasilySortElements(D[1]) then 
    if not IsSSortedList(D) then 
      D:=ShallowCopy(D);
      perm:=Sortex(D);
      D:=Immutable(D);
      SetIsSSortedList(D, true);
    fi;
  fi;

  out:=EmptyPlist(Length(D));
  i:=0;
  for pnt in D do 
    i:=i+1;
    out[i]:=PositionCanonical(D, act(pnt, f));
  od;

  out:=Transformation(out);

  if not IsOne(perm) then 
    out:=out^(perm^-1);
  fi;

  return out;
end);

#

InstallMethod(TransformationOpNC, "for object and list",
[IsObject, IsList],
function(f, D)
  return TransformationOpNC(f, Enumerator(D), OnPoints);
end);

#

InstallMethod(TransformationOpNC, "for object and domain",
[IsObject, IsDomain],
function(f, D)
  return TransformationOpNC(f, Enumerator(D), OnPoints);
end);

#

InstallMethod(TransformationOpNC, "for object, domain, function",
[IsObject, IsDomain, IsFunction],
function(f, D, act)
  return TransformationOpNC(f, Enumerator(D), act);
end);

# JDM expand!!

InstallGlobalFunction(TransformationActionNC, 
function(arg)
  if (IsDomain(arg[2]) or IsList(arg[2])) and IsFunction(arg[3]) then 
    if IsMonoid(arg[1]) then 
      return Monoid(GeneratorsOfMonoid(arg[1]), f-> 
       TransformationOpNC(f, arg[2], arg[3]));
    elif IsSemigroup(arg[1]) then 
      return Semigroup(GeneratorsOfSemigroup(arg[1]), f-> 
       TransformationOpNC(f, arg[2], arg[3]));
    fi;
  fi;
  return fail;
end);

#

InstallMethod(InverseOfTransformation, "for a transformation",
[IsTransformation], INV_TRANS);

#

InstallOtherMethod(InverseMutable, "for a transformation",
[IsTransformation], 
function(f)
  if RankOfTransformation(f)=DegreeOfTransformation(f) then 
    return InverseOfTransformation(f);
  fi;
  return fail;
end);

# binary relations etc...

InstallMethod(AsTransformation, "for relation over [1..n]", 
[IsGeneralMapping],
function(rel)
    local ims;

    if not IsEndoGeneralMapping(rel) then
      Error(rel, " is not a binary relation");
    fi;
    ims:= ImagesListOfBinaryRelation(rel);
    if not ForAll(ims, x->Length(x) = 1) then
      return fail;
    fi;
    return Transformation(List(ims, x->x[1]));
end);

InstallMethod(AsTransformation, "for binary relations on points", 
[IsBinaryRelation and IsBinaryRelationOnPointsRep],
function(rel)
  if not IsMapping(rel) then
    Error("usage: the argument must be a binary relation which is a mapping,"); 
    return;
  fi;
    return Transformation(Flat(Successors(rel)));
end);

InstallMethod(AsBinaryRelation, "for a transformation",
[IsTransformation], 
t->BinaryRelationByListOfImagesNC(
  List(ImageListOfTransformation(t, DegreeOfTransformation(t)), x->[x])));

InstallMethod(\*, "for a general mapping and a transformation", 
[IsGeneralMapping, IsTransformation],
function(r, t)
  return r * AsBinaryRelation(t);
end);

InstallMethod(\*, "for a transformation and a general mapping",
[IsTransformation, IsGeneralMapping],
function(t, r)
  return AsBinaryRelation(t) * r;
end);

#EOF