forked from gap-system/gap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermutat.cc
3039 lines (2616 loc) · 97.1 KB
/
permutat.cc
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
/****************************************************************************
**
*W permutat.cc GAP source Martin Schönert
** & Alice Niemeyer
**
**
*Y Copyright (C) 1996, 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 functions for permutations (small and large).
**
** Mathematically a permutation is a bijective mapping of a finite set onto
** itself. In \GAP\ this subset must always be of the form [ 1, 2, .., N ],
** where N is at most $2^32$.
**
** Internally a permutation <p> is viewed as a mapping of [ 0, 1, .., N-1 ],
** because in C indexing of arrays is done with the origin 0 instead of 1.
** A permutation is represented by a bag of type 'T_PERM2' or 'T_PERM4' of
** the form
**
** (CONST_)ADDR_OBJ(p)
** |
** v
** +------+-------+-------+-------+-------+- - - -+-------+-------+
** | inv. | image | image | image | image | | image | image |
** | perm | of 0 | of 1 | of 2 | of 3 | | of N-2| of N-1|
** +------+-------+-------+-------+-------+- - - -+-------+-------+
** ^
** |
** (CONST_)ADDR_PERM2(p) resp. (CONST_)ADDR_PERM4(p)
**
** The first entry of the bag <p> is either zero, or a reference to another
** permutation representing the inverse of <p>. The remaining entries of the
** bag form an array describing the permutation. For bags of type 'T_PERM2',
** the entries are of type 'UInt2' (defined in 'system.h' as an 16 bit wide
** unsigned integer type), for type 'T_PERM4' the entries are of type
** 'UInt4' (defined as a 32bit wide unsigned integer type). The first of
** these entries is the image of 0, the second is the image of 1, and so on.
** Thus, the entry at C index <i> is the image of <i>, if we view the
** permutation as mapping of [ 0, 1, 2, .., N-1 ] as described above.
**
** Permutations are never shortened. For example, if the product of two
** permutations of degree 100 is the identity, it is nevertheless stored as
** array of length 100, in which the <i>-th entry is of course simply <i>.
** Testing whether a product has trailing fixpoints would be pretty costly,
** and permutations of equal degree can be handled by the functions faster.
*/
extern "C" {
#include "permutat.h"
#include "ariths.h"
#include "bool.h"
#include "error.h"
#include "gapstate.h"
#include "integer.h"
#include "io.h"
#include "listfunc.h"
#include "lists.h"
#include "modules.h"
#include "opers.h"
#include "plist.h"
#include "precord.h"
#include "range.h"
#include "records.h"
#include "saveload.h"
#include "sysfiles.h"
#include "trans.h"
}
#ifdef GAP_KERNEL_DEBUG
template <typename T>
static bool CHECK_PERM_TYPE(Obj perm);
template <>
inline bool CHECK_PERM_TYPE<UInt2>(Obj perm)
{
return TNUM_OBJ(perm) == T_PERM2;
}
template <>
inline bool CHECK_PERM_TYPE<UInt4>(Obj perm)
{
return TNUM_OBJ(perm) == T_PERM4;
}
#endif
template <typename T>
static inline UInt SIZEBAG_PERM(UInt deg)
{
return sizeof(Obj) + deg * sizeof(T);
}
template <typename T>
static inline Obj NEW_PERM(UInt deg)
{
return NewBag(sizeof(T) == 2 ? T_PERM2 : T_PERM4, SIZEBAG_PERM<T>(deg));
}
template <typename T>
static inline UInt DEG_PERM(Obj perm)
{
GAP_ASSERT(CHECK_PERM_TYPE<T>(perm));
return (SIZE_OBJ(perm) - sizeof(Obj)) / sizeof(T);
}
template <typename T>
static inline T * ADDR_PERM(Obj perm)
{
GAP_ASSERT(CHECK_PERM_TYPE<T>(perm));
return (T *)(ADDR_OBJ(perm) + 1);
}
template <typename T>
static inline const T * CONST_ADDR_PERM(Obj perm)
{
GAP_ASSERT(CHECK_PERM_TYPE<T>(perm));
return (const T *)(CONST_ADDR_OBJ(perm) + 1);
}
//
// The 'ResultType' template is used by functions which take two permutations
// as argument to select the type of the output they produce: by default, a
// T_PERM4, whose entries are stored as UInt4. But if both inputs are
// T_PERM2, then as a special case the output is also a T_PERM2, whose
// entries are stored as UInt2.
//
template <typename TL, typename TR>
struct ResultType {
typedef UInt4 type;
};
template <>
struct ResultType<UInt2, UInt2> {
typedef UInt2 type;
};
/****************************************************************************
**
*F IMAGE(<i>,<pt>,<dg>) . . . . . . image of <i> under <pt> of degree <dg>
**
** 'IMAGE' returns the image of the point <i> under the permutation of
** degree <dg> pointed to by <pt>. If the point <i> is greater than or
** equal to <dg> the image is <i> itself.
**
** 'IMAGE' is implemented as a macro so do not use it with arguments that
** have side effects.
*/
#define IMAGE(i,pt,dg) (((i) < (dg)) ? (pt)[(i)] : (i))
/****************************************************************************
**
*V IdentityPerm . . . . . . . . . . . . . . . . . . . identity permutation
**
** 'IdentityPerm' is an identity permutation.
*/
Obj IdentityPerm;
static ModuleStateOffset PermutatStateOffset = -1;
typedef struct {
/****************************************************************************
**
*V TmpPerm . . . . . . . handle of the buffer bag of the permutation package
**
** 'TmpPerm' is the handle of a bag of type 'T_PERM4', which is created at
** initialization time of this package. Functions in this package can use
** this bag for whatever purpose they want. They have to make sure of
** course that it is large enough.
** The buffer is *not* guaranteed to have any particular value, routines
** that require a zero-initialization need to do this at the start.
** This buffer is only constructed once it is needed, to avoid startup
** costs (particularly when starting new threads).
** Use the UseTmpPerm(<size>) utility function to ensure it is constructed!
*/
Obj TmpPerm;
} PermutatModuleState;
#define TmpPerm MODULE_STATE(Permutat).TmpPerm
static UInt1 * UseTmpPerm(UInt size)
{
if (TmpPerm == (Obj)0)
TmpPerm = NewBag(T_PERM4, size);
else if (SIZE_BAG(TmpPerm) < size)
ResizeBag(TmpPerm, size);
return (UInt1 *)(ADDR_OBJ(TmpPerm) + 1);
}
template <typename T>
static inline T * ADDR_TMP_PERM()
{
// no GAP_ASSERT here on purpose
return (T *)(ADDR_OBJ(TmpPerm) + 1);
}
/****************************************************************************
**
*F TypePerm( <perm> ) . . . . . . . . . . . . . . . . type of a permutation
**
** 'TypePerm' returns the type of permutations.
**
** 'TypePerm' is the function in 'TypeObjFuncs' for permutations.
*/
Obj TYPE_PERM2;
Obj TYPE_PERM4;
Obj TypePerm2 (
Obj perm )
{
return TYPE_PERM2;
}
Obj TypePerm4 (
Obj perm )
{
return TYPE_PERM4;
}
/****************************************************************************
**
*F PrintPerm( <perm> ) . . . . . . . . . . . . . . . . . print a permutation
**
** 'PrintPerm' prints the permutation <perm> in the usual cycle notation. It
** uses the degree to print all points with same width, which looks nicer.
** Linebreaks are prefered most after cycles and next most after commas.
**
** It does not remember which points have already been printed. To avoid
** printing a cycle twice each is printed with the smallest element first.
** This may in the worst case, for (1,2,..,n), take n^2/2 steps, but is fast
** enough to keep a terminal at 9600 baud busy for all but the extrem cases.
*/
template <typename T>
void PrintPerm(Obj perm)
{
UInt degPerm; /* degree of the permutation */
const T * ptPerm; /* pointer to the permutation */
UInt p, q; /* loop variables */
UInt isId; /* permutation is the identity? */
const char * fmt1; /* common formats to print points */
const char * fmt2; /* common formats to print points */
/* set up the formats used, so all points are printed with equal width */
degPerm = DEG_PERM<T>(perm);
if ( degPerm < 10 ) { fmt1 = "%>(%>%1d%<"; fmt2 = ",%>%1d%<"; }
else if ( degPerm < 100 ) { fmt1 = "%>(%>%2d%<"; fmt2 = ",%>%2d%<"; }
else if ( degPerm < 1000 ) { fmt1 = "%>(%>%3d%<"; fmt2 = ",%>%3d%<"; }
else if ( degPerm < 10000 ) { fmt1 = "%>(%>%4d%<"; fmt2 = ",%>%4d%<"; }
else { fmt1 = "%>(%>%5d%<"; fmt2 = ",%>%5d%<"; }
/* run through all points */
isId = 1;
ptPerm = CONST_ADDR_PERM<T>(perm);
for ( p = 0; p < degPerm; p++ ) {
/* find the smallest element in this cycle */
q = ptPerm[p];
while ( p < q ) q = ptPerm[q];
/* if the smallest is the one we started with lets print the cycle */
if ( p == q && ptPerm[p] != p ) {
isId = 0;
Pr(fmt1,(Int)(p+1),0L);
ptPerm = CONST_ADDR_PERM<T>(perm);
for ( q = ptPerm[p]; q != p; q = ptPerm[q] ) {
Pr(fmt2,(Int)(q+1),0L);
ptPerm = CONST_ADDR_PERM<T>(perm);
}
Pr("%<)",0L,0L);
/* restore pointer, in case Pr caused a garbage collection */
ptPerm = CONST_ADDR_PERM<T>(perm);
}
}
/* special case for the identity */
if ( isId ) Pr("()",0L,0L);
}
/****************************************************************************
**
*F EqPerm( <opL>, <opR> ) . . . . . . . test if two permutations are equal
**
** 'EqPerm' returns 'true' if the two permutations <opL> and <opR> are equal
** and 'false' otherwise.
**
** Two permutations may be equal, even if the two sequences do not have the
** same length, if the larger permutation fixes the exceeding points.
**
*/
template <typename TL, typename TR>
Int EqPerm(Obj opL, Obj opR)
{
UInt degL; /* degree of the left operand */
const TL * ptL; /* pointer to the left operand */
UInt degR; /* degree of the right operand */
const TR * ptR; /* pointer to the right operand */
UInt p; /* loop variable */
/* get the degrees */
degL = DEG_PERM<TL>(opL);
degR = DEG_PERM<TR>(opR);
/* set up the pointers */
ptL = CONST_ADDR_PERM<TL>(opL);
ptR = CONST_ADDR_PERM<TR>(opR);
/* search for a difference and return False if you find one */
if ( degL <= degR ) {
for ( p = 0; p < degL; p++ )
if ( *(ptL++) != *(ptR++) )
return 0L;
for ( p = degL; p < degR; p++ )
if ( p != *(ptR++) )
return 0L;
}
else {
for ( p = 0; p < degR; p++ )
if ( *(ptL++) != *(ptR++) )
return 0L;
for ( p = degR; p < degL; p++ )
if ( *(ptL++) != p )
return 0L;
}
/* otherwise they must be equal */
return 1L;
}
/****************************************************************************
**
*F LtPerm( <opL>, <opR> ) . test if one permutation is smaller than another
**
** 'LtPerm' returns 'true' if the permutation <opL> is strictly less than
** the permutation <opR>. Permutations are ordered lexicographically with
** respect to the images of 1,2,.., etc.
*/
template <typename TL, typename TR>
Int LtPerm(Obj opL, Obj opR)
{
UInt degL; /* degree of the left operand */
const TL * ptL; /* pointer to the left operand */
UInt degR; /* degree of the right operand */
const TR * ptR; /* pointer to the right operand */
UInt p; /* loop variable */
/* get the degrees of the permutations */
degL = DEG_PERM<TL>(opL);
degR = DEG_PERM<TR>(opR);
/* set up the pointers */
ptL = CONST_ADDR_PERM<TL>(opL);
ptR = CONST_ADDR_PERM<TR>(opR);
/* search for a difference and return if you find one */
if ( degL <= degR ) {
for (p = 0; p < degL; p++, ptL++, ptR++)
if (*ptL != *ptR) {
return *ptL < *ptR;
}
for (p = degL; p < degR; p++, ptR++)
if (p != *ptR) {
return p < *ptR;
}
}
else {
for (p = 0; p < degR; p++, ptL++, ptR++)
if (*ptL != *ptR) {
return *ptL < *ptR;
}
for (p = degR; p < degL; p++, ptL++)
if (*ptL != p) {
return *ptL < p;
}
}
/* otherwise they must be equal */
return 0;
}
/****************************************************************************
**
*F ProdPerm( <opL>, <opR> ) . . . . . . . . . . . . product of permutations
**
** 'ProdPerm' returns the product of the two permutations <opL> and <opR>.
**
** This is a little bit tuned but should be sufficiently easy to understand.
*/
template <typename TL, typename TR>
Obj ProdPerm(Obj opL, Obj opR)
{
typedef typename ResultType<TL,TR>::type Res;
Obj prd; /* handle of the product (result) */
UInt degP; /* degree of the product */
Res * ptP; /* pointer to the product */
UInt degL; /* degree of the left operand */
const TL * ptL; /* pointer to the left operand */
UInt degR; /* degree of the right operand */
const TR * ptR; /* pointer to the right operand */
UInt p; /* loop variable */
/* compute the size of the result and allocate a bag */
degL = DEG_PERM<TL>(opL);
degR = DEG_PERM<TR>(opR);
degP = degL < degR ? degR : degL;
prd = NEW_PERM<Res>( degP );
/* set up the pointers */
ptL = CONST_ADDR_PERM<TL>(opL);
ptR = CONST_ADDR_PERM<TR>(opR);
ptP = ADDR_PERM<Res>(prd);
/* if the left (inner) permutation has smaller degree, it is very easy */
if ( degL <= degR ) {
for ( p = 0; p < degL; p++ )
*(ptP++) = ptR[ *(ptL++) ];
for ( p = degL; p < degR; p++ )
*(ptP++) = ptR[ p ];
}
/* otherwise we have to use the macro 'IMAGE' */
else {
for ( p = 0; p < degL; p++ )
*(ptP++) = IMAGE( ptL[ p ], ptR, degR );
}
/* return the result */
return prd;
}
/****************************************************************************
**
*F QuoPerm( <opL>, <opR> ) . . . . . . . . . . . . quotient of permutations
**
** 'QuoPerm' returns the quotient of the permutations <opL> and <opR>, i.e.,
** the product '<opL>\*<opR>\^-1'.
**
** Unfortunatly this can not be done in <degree> steps, we need 2 * <degree>
** steps.
*/
Obj QuoPerm(Obj opL, Obj opR)
{
return PROD(opL, INV(opR));
}
/****************************************************************************
**
*F LQuoPerm( <opL>, <opR> ) . . . . . . . . . left quotient of permutations
**
** 'LQuoPerm' returns the left quotient of the two permutations <opL> and
** <opR>, i.e., the value of '<opL>\^-1*<opR>', which sometimes comes handy.
**
** This can be done as fast as a single multiplication or inversion.
*/
template <typename TL, typename TR>
Obj LQuoPerm(Obj opL, Obj opR)
{
typedef typename ResultType<TL,TR>::type Res;
Obj mod; /* handle of the quotient (result) */
UInt degM; /* degree of the quotient */
Res * ptM; /* pointer to the quotient */
UInt degL; /* degree of the left operand */
const TL * ptL; /* pointer to the left operand */
UInt degR; /* degree of the right operand */
const TR * ptR; /* pointer to the right operand */
UInt p; /* loop variable */
/* compute the size of the result and allocate a bag */
degL = DEG_PERM<TL>(opL);
degR = DEG_PERM<TR>(opR);
degM = degL < degR ? degR : degL;
mod = NEW_PERM<Res>( degM );
/* set up the pointers */
ptL = CONST_ADDR_PERM<TL>(opL);
ptR = CONST_ADDR_PERM<TR>(opR);
ptM = ADDR_PERM<Res>(mod);
/* it is one thing if the left (inner) permutation is smaller */
if ( degL <= degR ) {
for ( p = 0; p < degL; p++ )
ptM[ *(ptL++) ] = *(ptR++);
for ( p = degL; p < degR; p++ )
ptM[ p ] = *(ptR++);
}
/* and another if the right (outer) permutation is smaller */
else {
for ( p = 0; p < degR; p++ )
ptM[ *(ptL++) ] = *(ptR++);
for ( p = degR; p < degL; p++ )
ptM[ *(ptL++) ] = p;
}
/* return the result */
return mod;
}
/****************************************************************************
**
*F InvPerm( <perm> ) . . . . . . . . . . . . . . . inverse of a permutation
*/
template <typename T>
Obj InvPerm(Obj perm)
{
Obj inv; /* handle of the inverse (result) */
T * ptInv; /* pointer to the inverse */
const T * ptPerm; /* pointer to the permutation */
UInt deg; /* degree of the permutation */
UInt p; /* loop variables */
inv = STOREDINV_PERM(perm);
if (inv != 0)
return inv;
deg = DEG_PERM<T>(perm);
inv = NEW_PERM<T>(deg);
// get pointer to the permutation and the inverse
ptPerm = CONST_ADDR_PERM<T>(perm);
ptInv = ADDR_PERM<T>(inv);
// invert the permutation
for ( p = 0; p < deg; p++ )
ptInv[ *ptPerm++ ] = p;
// store and return the inverse
SET_STOREDINV_PERM(perm, inv);
return inv;
}
/****************************************************************************
**
*F PowPermInt( <opL>, <opR> ) . . . . . . . integer power of a permutation
**
** 'PowPermInt' returns the <opR>-th power of the permutation <opL>. <opR>
** must be a small integer.
**
** This repeatedly applies the permutation <opR> to all points which seems
** to be faster than binary powering, and does not need temporary storage.
*/
template <typename T>
Obj PowPermInt(Obj opL, Obj opR)
{
Obj pow; /* handle of the power (result) */
T * ptP; /* pointer to the power */
const T * ptL; /* pointer to the permutation */
UInt1 * ptKnown; /* pointer to temporary bag */
UInt deg; /* degree of the permutation */
Int exp, e; /* exponent (right operand) */
UInt len; /* length of cycle (result) */
UInt p, q, r; /* loop variables */
/* handle zeroth and first powers and stored inverses separately */
if ( opR == INTOBJ_INT(0))
return IdentityPerm;
if ( opR == INTOBJ_INT(1))
return opL;
if (opR == INTOBJ_INT(-1))
return InvPerm<T>(opL);
/* get the operands and allocate a result bag */
deg = DEG_PERM<T>(opL);
pow = NEW_PERM<T>(deg);
/* compute the power by repeated mapping for small positive exponents */
if ( IS_INTOBJ(opR)
&& 2 <= INT_INTOBJ(opR) && INT_INTOBJ(opR) < 8 ) {
/* get pointer to the permutation and the power */
exp = INT_INTOBJ(opR);
ptL = CONST_ADDR_PERM<T>(opL);
ptP = ADDR_PERM<T>(pow);
/* loop over the points of the permutation */
for ( p = 0; p < deg; p++ ) {
q = p;
for ( e = 0; e < exp; e++ )
q = ptL[q];
ptP[p] = q;
}
}
/* compute the power by raising the cycles individually for large exps */
else if ( IS_INTOBJ(opR) && 8 <= INT_INTOBJ(opR) ) {
/* make sure that the buffer bag is large enough */
UseTmpPerm(SIZE_OBJ(opL));
ptKnown = ADDR_TMP_PERM<UInt1>();
/* clear the buffer bag */
memset(ptKnown, 0, DEG_PERM<T>(opL));
/* get pointer to the permutation and the power */
exp = INT_INTOBJ(opR);
ptL = CONST_ADDR_PERM<T>(opL);
ptP = ADDR_PERM<T>(pow);
/* loop over all cycles */
for ( p = 0; p < deg; p++ ) {
/* if we haven't looked at this cycle so far */
if ( ptKnown[p] == 0 ) {
/* find the length of this cycle */
len = 1;
for ( q = ptL[p]; q != p; q = ptL[q] ) {
len++; ptKnown[q] = 1;
}
/* raise this cycle to the power <exp> mod <len> */
r = p;
for ( e = 0; e < exp % len; e++ )
r = ptL[r];
ptP[p] = r;
r = ptL[r];
for ( q = ptL[p]; q != p; q = ptL[q] ) {
ptP[q] = r;
r = ptL[r];
}
}
}
}
/* compute the power by raising the cycles individually for large exps */
else if ( TNUM_OBJ(opR) == T_INTPOS ) {
/* make sure that the buffer bag is large enough */
UseTmpPerm(SIZE_OBJ(opL));
ptKnown = ADDR_TMP_PERM<UInt1>();
/* clear the buffer bag */
memset(ptKnown, 0, DEG_PERM<T>(opL));
/* get pointer to the permutation and the power */
ptL = CONST_ADDR_PERM<T>(opL);
ptP = ADDR_PERM<T>(pow);
/* loop over all cycles */
for ( p = 0; p < deg; p++ ) {
/* if we haven't looked at this cycle so far */
if ( ptKnown[p] == 0 ) {
/* find the length of this cycle */
len = 1;
for ( q = ptL[p]; q != p; q = ptL[q] ) {
len++; ptKnown[q] = 1;
}
/* raise this cycle to the power <exp> mod <len> */
r = p;
exp = INT_INTOBJ( ModInt( opR, INTOBJ_INT(len) ) );
for ( e = 0; e < exp; e++ )
r = ptL[r];
ptP[p] = r;
r = ptL[r];
for ( q = ptL[p]; q != p; q = ptL[q] ) {
ptP[q] = r;
r = ptL[r];
}
}
}
}
/* compute the power by repeated mapping for small negative exponents */
else if ( IS_INTOBJ(opR)
&& -8 < INT_INTOBJ(opR) && INT_INTOBJ(opR) < 0 ) {
/* get pointer to the permutation and the power */
exp = -INT_INTOBJ(opR);
ptL = CONST_ADDR_PERM<T>(opL);
ptP = ADDR_PERM<T>(pow);
/* loop over the points */
for ( p = 0; p < deg; p++ ) {
q = p;
for ( e = 0; e < exp; e++ )
q = ptL[q];
ptP[q] = p;
}
}
/* compute the power by raising the cycles individually for large exps */
else if ( IS_INTOBJ(opR) && INT_INTOBJ(opR) <= -8 ) {
/* make sure that the buffer bag is large enough */
UseTmpPerm(SIZE_OBJ(opL));
ptKnown = ADDR_TMP_PERM<UInt1>();
/* clear the buffer bag */
memset(ptKnown, 0, DEG_PERM<T>(opL));
/* get pointer to the permutation and the power */
exp = -INT_INTOBJ(opR);
ptL = CONST_ADDR_PERM<T>(opL);
ptP = ADDR_PERM<T>(pow);
/* loop over all cycles */
for ( p = 0; p < deg; p++ ) {
/* if we haven't looked at this cycle so far */
if ( ptKnown[p] == 0 ) {
/* find the length of this cycle */
len = 1;
for ( q = ptL[p]; q != p; q = ptL[q] ) {
len++; ptKnown[q] = 1;
}
/* raise this cycle to the power <exp> mod <len> */
r = p;
for ( e = 0; e < exp % len; e++ )
r = ptL[r];
ptP[r] = p;
r = ptL[r];
for ( q = ptL[p]; q != p; q = ptL[q] ) {
ptP[r] = q;
r = ptL[r];
}
}
}
}
/* compute the power by raising the cycles individually for large exps */
else if ( TNUM_OBJ(opR) == T_INTNEG ) {
/* do negation first as it can cause a garbage collection */
opR = AInvInt(opR);
/* make sure that the buffer bag is large enough */
UseTmpPerm(SIZE_OBJ(opL));
ptKnown = ADDR_TMP_PERM<UInt1>();
/* clear the buffer bag */
memset(ptKnown, 0, DEG_PERM<T>(opL));
/* get pointer to the permutation and the power */
ptL = CONST_ADDR_PERM<T>(opL);
ptP = ADDR_PERM<T>(pow);
/* loop over all cycles */
for ( p = 0; p < deg; p++ ) {
/* if we haven't looked at this cycle so far */
if ( ptKnown[p] == 0 ) {
/* find the length of this cycle */
len = 1;
for ( q = ptL[p]; q != p; q = ptL[q] ) {
len++; ptKnown[q] = 1;
}
/* raise this cycle to the power <exp> mod <len> */
r = p;
exp = INT_INTOBJ( ModInt( opR, INTOBJ_INT(len) ) );
for ( e = 0; e < exp % len; e++ )
r = ptL[r];
ptP[r] = p;
r = ptL[r];
for ( q = ptL[p]; q != p; q = ptL[q] ) {
ptP[r] = q;
r = ptL[r];
}
}
}
}
/* return the result */
return pow;
}
/****************************************************************************
**
*F PowIntPerm( <opL>, <opR> ) . . . image of an integer under a permutation
**
** 'PowIntPerm' returns the image of the positive integer <opL> under the
** permutation <opR>. If <opL> is larger than the degree of <opR> it is a
** fixpoint of the permutation and thus simply returned.
*/
template <typename T>
Obj PowIntPerm(Obj opL, Obj opR)
{
Int img; /* image (result) */
GAP_ASSERT(TNUM_OBJ(opL) == T_INTPOS || TNUM_OBJ(opL) == T_INT);
/* large positive integers (> 2^28-1) are fixed by any permutation */
if ( TNUM_OBJ(opL) == T_INTPOS )
return opL;
img = INT_INTOBJ(opL);
RequireArgumentCondition("PowIntPerm", opL, "point", img > 0,
"must be a positive integer");
/* compute the image */
if ( img <= DEG_PERM<T>(opR) ) {
img = (CONST_ADDR_PERM<T>(opR))[img-1] + 1;
}
/* return it */
return INTOBJ_INT(img);
}
/****************************************************************************
**
*F QuoIntPerm( <opL>, <opR> ) . preimage of an integer under a permutation
**
** 'QuoIntPerm' returns the preimage of the preimage integer <opL> under the
** permutation <opR>. If <opL> is larger than the degree of <opR> is is a
** fixpoint, and thus simply returned.
**
** There are basically two ways to find the preimage. One is to run through
** <opR> and look for <opL>. The index where it's found is the preimage.
** The other is to find the image of <opL> under <opR>, the image of that
** point and so on, until we come back to <opL>. The last point is the
** preimage of <opL>. This is faster because the cycles are usually short.
*/
static Obj PERM_INVERSE_THRESHOLD;
template <typename T>
Obj QuoIntPerm(Obj opL, Obj opR)
{
T pre; /* preimage (result) */
Int img; /* image (left operand) */
const T * ptR; /* pointer to the permutation */
GAP_ASSERT(TNUM_OBJ(opL) == T_INTPOS || TNUM_OBJ(opL) == T_INT);
/* large positive integers (> 2^28-1) are fixed by any permutation */
if ( TNUM_OBJ(opL) == T_INTPOS )
return opL;
img = INT_INTOBJ(opL);
RequireArgumentCondition("QuoIntPerm", opL, "point", img > 0,
"must be a positive integer");
Obj inv = STOREDINV_PERM(opR);
if (inv == 0 && PERM_INVERSE_THRESHOLD != 0 &&
IS_INTOBJ(PERM_INVERSE_THRESHOLD) &&
DEG_PERM<T>(opR) <= INT_INTOBJ(PERM_INVERSE_THRESHOLD))
inv = InvPerm<T>(opR);
if (inv != 0)
return INTOBJ_INT(
IMAGE(img - 1, CONST_ADDR_PERM<T>(inv), DEG_PERM<T>(inv)) + 1);
/* compute the preimage */
if ( img <= DEG_PERM<T>(opR) ) {
pre = T(img - 1);
ptR = CONST_ADDR_PERM<T>(opR);
while (ptR[pre] != T(img - 1))
pre = ptR[pre];
/* return it */
return INTOBJ_INT(pre + 1);
}
else
return INTOBJ_INT(img);
}
/****************************************************************************
**
*F PowPerm( <opL>, <opR> ) . . . . . . . . . . . conjugation of permutations
**
** 'PowPerm' returns the conjugation of the two permutations <opL> and
** <opR>, that s defined as the following product '<opR>\^-1 \*\ <opL> \*\
** <opR>'.
*/
template <typename TL, typename TR>
Obj PowPerm(Obj opL, Obj opR)
{
typedef typename ResultType<TL,TR>::type Res;
Obj cnj; /* handle of the conjugation (res) */
UInt degC; /* degree of the conjugation */
Res * ptC; /* pointer to the conjugation */
UInt degL; /* degree of the left operand */
const TL * ptL; /* pointer to the left operand */
UInt degR; /* degree of the right operand */
const TR * ptR; /* pointer to the right operand */
UInt p; /* loop variable */
/* compute the size of the result and allocate a bag */
degL = DEG_PERM<TL>(opL);
degR = DEG_PERM<TR>(opR);
degC = degL < degR ? degR : degL;
cnj = NEW_PERM<Res>( degC );
/* set up the pointers */
ptL = CONST_ADDR_PERM<TL>(opL);
ptR = CONST_ADDR_PERM<TR>(opR);
ptC = ADDR_PERM<Res>(cnj);
/* it is faster if the both permutations have the same size */
if ( degL == degR ) {
for ( p = 0; p < degC; p++ )
ptC[ ptR[p] ] = ptR[ ptL[p] ];
}
/* otherwise we have to use the macro 'IMAGE' three times */
else {
for ( p = 0; p < degC; p++ )
ptC[ IMAGE(p,ptR,degR) ] = IMAGE( IMAGE(p,ptL,degL), ptR, degR );
}
/* return the result */
return cnj;
}
/****************************************************************************
**
*F CommPerm( <opL>, <opR> ) . . . . . . . . commutator of two permutations
**
** 'CommPerm' returns the commutator of the two permutations <opL> and
** <opR>, that is defined as '<hd>\^-1 \*\ <opR>\^-1 \*\ <opL> \*\ <opR>'.
*/
template <typename TL, typename TR>
Obj CommPerm(Obj opL, Obj opR)
{
typedef typename ResultType<TL,TR>::type Res;
Obj com; /* handle of the commutator (res) */
UInt degC; /* degree of the commutator */
Res * ptC; /* pointer to the commutator */
UInt degL; /* degree of the left operand */
const TL * ptL; /* pointer to the left operand */
UInt degR; /* degree of the right operand */
const TR * ptR; /* pointer to the right operand */
UInt p; /* loop variable */
/* compute the size of the result and allocate a bag */
degL = DEG_PERM<TL>(opL);
degR = DEG_PERM<TR>(opR);
degC = degL < degR ? degR : degL;
com = NEW_PERM<Res>( degC );
/* set up the pointers */
ptL = CONST_ADDR_PERM<TL>(opL);
ptR = CONST_ADDR_PERM<TR>(opR);
ptC = ADDR_PERM<Res>(com);
/* it is faster if the both permutations have the same size */
if ( degL == degR ) {
for ( p = 0; p < degC; p++ )
ptC[ ptL[ ptR[ p ] ] ] = ptR[ ptL[ p ] ];
}
/* otherwise we have to use the macro 'IMAGE' four times */
else {
for ( p = 0; p < degC; p++ )
ptC[ IMAGE( IMAGE(p,ptR,degR), ptL, degL ) ]
= IMAGE( IMAGE(p,ptL,degL), ptR, degR );
}
/* return the result */
return com;
}