-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemigroup.c
1413 lines (1361 loc) · 57.7 KB
/
semigroup.c
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
/*Methods for thin (semi)-groups.*/
/*INCLUSIONS*/
#include <pari.h>
#include "semigroup.h"
/*STATIC DECLARATIONS*/
/*SECTION 1: BASIC (SEMI)GROUP METHODS*/
/*SECTION 2: SEMIGROUP ORBITS*/
static long** ZM_to_Cmatrix(GEN M);
static void findmissing(long Bmin, long Bmax, long ***mats, long nmats, long *start, long n, long *res, long nres, long modulus, long entry);
static void missing_tofile(long blocks, unsigned long **rclass, long Bmin, long Bmax, long *start, long n, long *res, long nres, long ubits, long modulus);
static void findmissing_parabolic(long Bmin, long Bmax, long ***mats, long ***matsinv, long nmats, long *start, long n, long *res, long nres, long modulus, long entry);
static int findmissinglist(hashtable *hmiss, long Bmin, long Bmax, long ***mats, long ***matsinv, long nmats, long *start, long n, long entry);
/*SECTION 3: PSI METHODS*/
static int* get_kron_vals(unsigned long n, long *len);
/*SECTION 4: CONTINUED FRACTIONS*/
static void contfrac_tail_missing_execute(unsigned long Bmin, unsigned long Bmax);
static void contfrac_tail_missing_tofile(unsigned long *miss, long blocks, long Bmin, long Bmax);
static GEN contfrac_tail_missing_load(long Bmin, long Bmax);
/*SECTION 5: LINEAR REGRESSION*/
/*MAIN BODY*/
/*SECTION 1: BASIC (SEMI)GROUP METHODS*/
/*For M a hyperbolic matrix with positive entries, returns M as a word in L and R, where L=[1,1;0,1] and R=[1,0;1,1]. This is a Vecsmall with 0 representing L and 1 representing R.*/
GEN
LRword(GEN M)
{
pari_sp av = avma;
long maxdepth = 100, i = 1;
long a = itos(gcoeff(M, 1, 1)), b = itos(gcoeff(M, 1, 2));
long c = itos(gcoeff(M, 2, 1)), d = itos(gcoeff(M, 2, 2));
GEN v = cgetg(maxdepth + 1, t_VECSMALL);
while(c || b) {
if (a > c) {
v[i] = 0;/*L*/
a -= c;
b -= d;
}
else {
v[i] = 1;/*R*/
c -= a;
d -= b;
}
i++;
if (i > maxdepth) {
maxdepth <<= 1;
v = vecsmall_lengthen(v, maxdepth);
}
}
return gerepileupto(av, vecsmall_shorten(v, i - 1));
}
/*Returns the largest entry of the ZM M as a long.*/
INLINE long
ZM_largest_s(GEN M)
{
long x = itos(gcoeff(M, 1, 1)), lM = lg(M), i, j;
for (j = 2; j < lM; j++) x = maxss(x, itos(gcoeff(M, 1, j)));
for (i = 2; i < lM; i++) {
for (j = 1; j < lM; j++) x = maxss(x, itos(gcoeff(M, i, j)));
}
return x;
}
/*We estimate the growth, by computing all elements of the orbit (given by the column vector start) that are at most binsize * Nbins. We then group their sizes into bins, and runs a linear regression on the data to determine the growth rate. Returns [c, nu, R^2], where up to N we have c*N^nu, and the R^2 value of the regression is given.*/
GEN
semigroup_growth(GEN mats, long binsize, long Nbins, GEN start, long prec)
{
pari_sp av = avma;
if (!start) start = mkcol2s(1, 1);
if (typ(start) == t_VEC) start = gtocol(start);
else if (typ(start) == t_MAT) start = gel(start, 1);/*Ensure it is a column vector.*/
GEN v = semigroup_mats(mats, binsize * Nbins);
long lv, i;
GEN images = cgetg_copy(v, &lv);
for (i = 1; i < lv; i++) gel(images, i) = ZM_ZC_mul(gel(v, i), start);
GEN images_uniq = vecsort0(images, NULL, 8);/*Sort and remove double counting.*/
long lu = lg(images_uniq);
GEN counts = const_vecsmall(Nbins, 0);
for (i = 1; i < lu; i++) {
GEN r;
GEN SOS = addii(sqri(gmael(images_uniq, i, 1)), sqri(gmael(images_uniq, i, 2)));
long rt = itos(sqrtremi(SOS, &r));
if (!isintzero(r)) rt++;/*rt=ceil(size of the vector)*/
long rem, quo;
quo = sdivss_rem(rt, binsize, &rem);
if (rem) quo++;/*quo=ceil(rt/binsize)*/
if (quo <= Nbins) counts[quo]++;
}
counts = gerepilecopy(av, counts);/*Clear garbage, clone counts to the heap as this is all we need.*/
GEN cumu = cgetg(Nbins + 1, t_COL);/*Cumulative counts*/
gel(cumu, 1) = stoi(counts[1]);
for (i = 2; i <= Nbins; i++) gel(cumu, i) = addis(gel(cumu, i - 1), counts[i]);
for (i = 1; i <= Nbins; i++) gel(cumu, i) = glog(gel(cumu, i), prec);/*Take the log of all entries.*/
GEN X = cgetg(Nbins + 1, t_MAT);
for (i = 1; i <= Nbins; i++) {/*X is matrix with X[,i]=[1,log(binsize*i)].*/
gel(X, i) = mkcol2(gen_1, glog(stoi(binsize * i), prec));
}
GEN reg = OLS(X, cumu, 1);/*cumu[i]=c*(binsize*i)^nu -> take logarithms.*/
GEN c = gexp(gmael(reg, 1, 1), prec);
return gerepilecopy(av, mkvec3(c, gmael(reg, 1, 2), gel(reg, 2)));
}
/*Assume the matrices in mats all have positive entries and infinite order. This returns the matrices in the semigroup they generate that have all entries at most N. If there are relations, the corresponding matrices will get counted multiple times.*/
GEN
semigroup_mats(GEN mats, long N)
{
pari_sp av = avma;
long lmats = lg(mats), nM = lg(gel(mats, 1)) - 1;
long maxdepth = 100, maxfound = 10000, vind = 0;
GEN v = cgetg(maxfound + 1, t_VEC);
GEN depthseq = cgetg(maxdepth + 1, t_VEC);
GEN swaps = const_vecsmall(maxdepth, 0);
gel(depthseq, 1) = matid(nM);
long ind = 2;
while (ind > 1) {
long cind = ++swaps[ind];
if (cind == lmats) {/*Overflowed, go back.*/
swaps[ind] = 0;
ind--;
continue;
}
GEN M = ZM_mul(gel(depthseq, ind - 1), gel(mats, cind));
long maxM = ZM_largest_s(M);
if (maxM > N) continue;/*Too big! Go back.*/
vind++;
if (vind > maxfound) {/*Double size.*/
maxfound <<= 1;
v = vec_lengthen(v, maxfound);
}
gel(v, vind) = M;
gel(depthseq, ind) = M;
ind++;
if (ind > maxdepth) {/*Double depth sequence / swaps length.*/
maxdepth <<= 1;
depthseq = vec_lengthen(depthseq, maxdepth);
swaps = vecsmall_lengthen(swaps, maxdepth);
long i;
for (i = ind; i <= maxdepth; i++) swaps[i] = 0;
}
}
return gerepilecopy(av, vec_shorten(v, vind));
}
/*Returns 1 if all entries of the n x n ZM M are >=0*/
INLINE int
ZM_isnonneg(GEN M, long n)
{
long i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) if (signe(gcoeff(M, i, j)) == -1) return 0;
}
return 1;
}
/*Returns a minimal set of generators for the semigroup spanned by the given matrices, which must all have positive entries and infinite order.*/
GEN
semigroup_mgens(GEN mats)
{
/*Basic strategy: go one by one, testing if the element is required. We do this by repeatedly multiplying on the left by all other matrices until our matrix has negative entries, checking if we ever find another input matrix*/
pari_sp av = avma, av1;
long lgmats, i, minind = 1;/*minind tracks the first surviving index.*/
GEN matsinv = cgetg_copy(mats, &lgmats);/*Store the matrix inverses.*/
if (lgmats == 1) { set_avma(av); return cgetg(1, t_VEC); }/*No input.*/
for (i = 1; i < lgmats; i++) gel(matsinv, i) = ZM_inv(gel(mats, i), NULL);
GEN nextinds = cgetg(lgmats, t_VECSMALL), previnds = cgetg(lgmats, t_VECSMALL);
for (i = 1; i < lgmats; i++) {
nextinds[i] = i + 1;/*Surviving indices point to the next surviving one, and 0 if it's gone/last one.*/
previnds[i] = i - 1;/*Surviving indices point to the previous surviving one, and 0 if it's gone/first one.*/
}
nextinds[lgmats - 1] = 0;
hashtable all;
hash_init_GEN(&all, lgmats, &ZM_equal, 1);/*Stores the matrices for searching.*/
long idind = 0;
for (i = 1; i < lgmats; i++) {
if (!gequal1(gel(mats, i))) {
hash_insert(&all, (void *)gel(mats, i), NULL);/*Insert the non-identity matrices.*/
continue;
}
idind = i;/*Index of the identity matrix.*/
if (i == 1) {/*Delete the first one.*/
minind = 2;/*Update the minimum index.*/
previnds[2] = 0;/*Now this points to 0. previnds[1] is already 0.*/
nextinds[1] = 0;/*This is also gone.*/
continue;
}
long w = nextinds[i];/*i+1 or 0 if i==lgmats-1.*/
nextinds[i - 1] = w;
nextinds[i] = 0;
if (w) previnds[w] = previnds[i];/*If we weren't the last one, update this.*/
previnds[i] = 0;/*Delete*/
}
av1 = avma;
long ind, n = lg(gel(mats, 1)) - 1, maxdepth = 20, dind;/*ind = index we are trying to remove; n x n matrices.*/
GEN depthinds = const_vecsmall(maxdepth, 0);/*Tracks the sequence of moves. Index i>0 denotes multiplying by matsinv[i] on the left. We do 1, 2, 3,...*/
GEN depthseq = cgetg(maxdepth + 1, t_VEC);/*Tracks the resulting matrices.*/
for (ind = 1; ind < lgmats; ind++) {/*We run through the matrices in order, testing if they are necessary or not.*/
if (gc_needed(av1, 2)) {/*GARBAGE DAY! All we need to save between runs is unchanged after av1, or is in Vecsmalls created before av1.*/
set_avma(av1);
depthinds = const_vecsmall(maxdepth, 0);
depthseq = cgetg(maxdepth + 1, t_VEC);
}
if (ind == idind) continue;/*Only one that could be pre-deleted.*/
dind = 2;
gel(depthseq, 1) = gel(mats, ind);/*Starting matrix.*/
depthinds[2] = 0;/*Reset*/
while (dind > 1) {/*Still going.*/
if (!depthinds[dind]) depthinds[dind] = minind;/*Just starting! First matrix we must multiply by the inverse of.*/
else depthinds[dind] = nextinds[depthinds[dind]];/*Move to next one.*/
i = depthinds[dind];/*Which index to try.*/
if (dind == 2 && ind == i) continue;/*We are immediately multiplying by the inverse of our matrix, pointless.*/
if (!i) { dind--; continue; }/*Reached the end of the line here.*/
GEN Mold = gel(depthseq, dind - 1);/*Previous matrix*/
GEN Mnew = ZM_mul(gel(matsinv, i), Mold);/*On the left*/
if (!ZM_isnonneg(Mnew, n)) continue;/*Didn't work, move on to the next one in the same level.*/
if (hash_search(&all, (void *)Mnew)) {/*This already exists. Delete it!*/
if (ind == minind) {/*Delete the first one.*/
minind = nextinds[minind];/*Update the minimum index.*/
previnds[minind] = 0;/*Now this points to 0. previnds[ind] is already 0.*/
nextinds[ind] = 0;/*This is also gone.*/
break;/*No need to go on.*/
}
long w = nextinds[ind];
nextinds[previnds[ind]] = w;/*Update the last index to point to the correct place.*/
nextinds[ind] = 0;/*Delete*/
if (w) previnds[w] = previnds[ind];/*If we weren't the last one, update this.*/
previnds[ind] = 0;/*Delete*/
break;/*No need to go on.*/
}/*Now, it did not exist. Move forward in the depth sequence.*/
gel(depthseq, dind) = Mnew;
dind++;
if (dind > maxdepth) {/*Make these longer.*/
maxdepth <<= 1;/*Double it.*/
depthseq = vec_lengthen(depthseq, maxdepth);
depthinds = vecsmall_lengthen(depthinds, maxdepth);
}
depthinds[dind] = 0;/*(Re)set to 0.*/
}
}
hash_destroy(&all);
GEN mg = vectrunc_init(lgmats);
i = minind;
while (i) {
vectrunc_append(mg, gel(mats, i));
i = nextinds[i];
}
return gerepilecopy(av, mg);
}
/*SECTION 2: SEMIGROUP ORBITS*/
/*Takes in a ZM M and returns a malloc'ed array representing M as a C array.*/
static long**
ZM_to_Cmatrix(GEN M)
{
long ncols = lg(M) - 1, nrows = nbrows(M), i, j;
long **A = (long **)pari_malloc(nrows * sizeof(long *));
for (i = 0; i < nrows; i++) {
A[i] = (long *)pari_malloc(ncols * sizeof(long));
for (j = 0; j < ncols; j++) A[i][j] = itos(gcoeff(M, i + 1, j + 1));
}
return A;
}
/*Runs C code to find the missing positive entries up to the given bound, then saves them to a file. Must also supply the congruence conditions.*/
GEN
semigroup_missing(GEN mats, GEN B, GEN start, GEN congs, long entry, long load)
{
pari_sp av = avma;
long Bmin = 0, Bmax = 0, t = typ(B);
if (t == t_INT) { Bmin = 1; Bmax = itos(B); }
else if (t == t_VEC || t == t_COL) {
if (lg(B) < 3) pari_err_TYPE("B must be a positive integer or a range of positive integers", B);
if (typ(gel(B, 1)) != t_INT) pari_err_TYPE("B must be a positive integer or a range of positive integers", B);
if (typ(gel(B, 2)) != t_INT) pari_err_TYPE("B must be a positive integer or a range of positive integers", B);
Bmin = itou(gel(B, 1));
Bmax = itou(gel(B, 2));
}
else if (t == t_VECSMALL) {
if (lg(B) < 3) pari_err_TYPE("B must be a positive integer or a range of positive integers", B);
Bmin = B[1];
Bmax = B[2];
}
else pari_err_TYPE("B must be a positive integer or a range of positive integers", B);
if (typ(start) == t_VEC) start = gtocol(start);
else if (typ(start) == t_MAT) start = gel(start, 1);/*Ensure it is a column vector.*/
if (Bmin <= 0) Bmin = 1;
long n = lg(start) - 1;
if (entry < 0 || entry > n) pari_err_TYPE("entry must be an index from 1 to n, the dimension of the vectors computed", stoi(entry));
long nmats = lg(mats) - 1, i;
long ***Cmats = (long ***)pari_malloc(nmats * sizeof(long **));/*Stores the matrices as C arrays*/
for (i = 0; i < nmats; i++) Cmats[i] = ZM_to_Cmatrix(gel(mats, i + 1));
long *Cstart = (long *)pari_malloc(n * sizeof(long));/*Stores the starting tuple.*/
for (i = 0; i < n; i++) Cstart[i] = itos(gel(start, i + 1));
if (typ(congs) != t_VEC || lg(congs) < 3 || typ(gel(congs, 1)) != t_VEC) pari_err_TYPE("congs must be the relevant congruence conditions, formatted as [[r1, r2, ..., rk], n] for ri modulo n, which should be sorted.", congs);
long modulus = itos(gel(congs, 2)), nres = lg(gel(congs, 1)) - 1;
long *residues = (long *)pari_malloc(nres * sizeof(long));
for (i = 0; i < nres; i++) residues[i] = itos(gmael(congs, 1, i + 1));
findmissing(Bmin, Bmax, Cmats, nmats, Cstart, n, residues, nres, modulus, entry - 1);
pari_free(residues);
pari_free(Cstart);
long j;
for (i = 0; i < nmats; i++) {
for (j = 0; j < n; j++) pari_free(Cmats[i][j]);
pari_free(Cmats[i]);
}
pari_free(Cmats);
if (!load) return gc_const(av, gen_0);/*Do not load*/
char *fname = stack_sprintf("missing/%Pd", gel(start, 1));
for (i = 2; i <= n; i++) fname = stack_sprintf("%s_%Pd", fname, gel(start, i));
fname = stack_sprintf("%s_%ld-to-%ld.dat", fname, Bmin, Bmax);
set_avma(av);
return gerepileupto(av, gel(gp_readvec_file(fname), 1));/*Load them up!*/
}
/*Updates rclass with the new value. Assumes we are at most Bmax*/
INLINE void
missing_update(unsigned long **rclass, unsigned long *bitswap, long *maxmiss_block, int *maxmiss_bit, long Base, long *Bmax, long value, long *res, long nres, long Bmin, long ubits, long modulus)
{
long shifted = value - Base;
if (shifted <= 0) return;
long b = shifted % modulus;
long a = shifted / modulus;/*shifted=modulus*a+b. b gives the block to insert into, and we need to save "a"*/
long v = a % ubits;
long u = a / ubits;/*a=ubits * u+v. u gives the entry of the array, v gives the bit to swap.*/
rclass[b][u] |= bitswap[v];
if (u != maxmiss_block[b]) return;/*Not swapping in the last block.*/
if (v != maxmiss_bit[b]) return;/*Not swapping the last bit.*/
long i;
for (i = v - 1; i >= 0; i--) {
if (!(rclass[b][u] & bitswap[i])) {/*Zero, so we stop here.*/
maxmiss_bit[b] = i;
goto BMAXUPDATE;
}
}
long j;/*We made it out of the block.*/
for (j = u - 1; j >= 0; j--) {
for (i = ubits - 1; i >= 0; i--) {
if (!(rclass[b][j] & bitswap[i])) {/*Zero, so we stop here.*/
maxmiss_block[b] = j;
maxmiss_bit[b] = i;
goto BMAXUPDATE;
}
}
}
maxmiss_block[b] = -1;/*Everything is gone!*/
maxmiss_bit[b] = -1;
BMAXUPDATE:;/*See if we update Bmax*/
if (value != *Bmax) return;/*Did not remove the largest exception.*/
long worst = res[0];
for (i = 1; i < nres; i++) {
if (maxmiss_block[res[i]] > maxmiss_block[worst]) {
worst = res[i];
continue;
}
if (maxmiss_block[res[i]] < maxmiss_block[worst]) continue;
if (maxmiss_bit[res[i]] >= maxmiss_bit[worst]) worst = res[i];
}
*Bmax = Base + ((maxmiss_block[worst] * ubits + maxmiss_bit[worst]) * modulus ) + worst;/*Update Bmax*/
if (Bmin > *Bmax) *Bmax = 0;/*All eliminated, let's quit early!*/
}
/*executes semigroup_missing*/
static void
findmissing(long Bmin, long Bmax, long ***mats, long nmats, long *start, long n, long *res, long nres, long modulus, long entry)
{
long ubits = sizeof(unsigned long) << 3;/*How many bits in an unsigned long*/
unsigned long *bitswap = (unsigned long*)pari_malloc(ubits * sizeof(unsigned long)), i;/*Used for swapping bits of unsigned longs.*/
bitswap[0] = 1;
for (i = 1; i < ubits; i++) bitswap[i] = bitswap[i - 1] << 1;/*bitswap[i] = 2^i*/
long Base = Bmin - 1;
Base -= (Base % modulus);/*We want to start at a multiple of modulus to not ruin the mod stuff.*/
long classmax = (Bmax - Base)/ modulus + 1;/*Maximal number of entries found in each residue class.*/
long blocks = ((classmax - 1) / ubits) + 1;/*This is the number of unsigned longs we need to store in each class.*/
unsigned long **rclass = (unsigned long **)pari_malloc(modulus * sizeof(unsigned long *));/*Stores pointers to the individual classes.*/
for (i = 0; i < nres; i++) {
rclass[res[i]] = (unsigned long *)pari_calloc(blocks * sizeof(unsigned long));/*pari_calloc the classes we want, since we want them as 0 to start.*/
if (!rclass[res[i]]) {
printf("Insufficient memory to allocate to store the curvatures.\n");
exit(1);
}
}
long Bmaxoriginal = Bmax;/*Save for later in case we change it.*/
long *maxmiss_block = (long *)pari_malloc(modulus * sizeof(long));/*Tracks the largest block in the residue class that still contains 0's*/
int *maxmiss_bit = (int *)pari_malloc(modulus * sizeof(int));/*Tracks the largest bit of said class that is non-zero.*/
int Bmaxmod = Bmax % modulus;/*We now will initialize it.*/
long Bmaxbase = Bmax - Bmaxmod, j;
int foundlargest = 0;
for (i = 0; i < nres; i++) {
long mc = Bmaxbase + res[i];
if (res[i] > Bmaxmod) {
if (!foundlargest) {
foundlargest = 1;
if (i) Bmax = Bmaxbase + res[i - 1];/*Update to the largest actually possible value.*/
else Bmax = Bmaxbase + res[nres - 1] - modulus;
}
mc -= modulus;/*The last curvature of this type.*/
}
if (mc < Bmin) {
maxmiss_bit[res[i]] = -1;
maxmiss_block[res[i]] = -1;
continue;
}
mc -= Base;/*Shift it back.*/
long a = mc / modulus;/*Save a in block res[i]*/
maxmiss_bit[res[i]] = a % ubits;
maxmiss_block[res[i]] = a / ubits;
}
if (!foundlargest) Bmax = Bmaxbase + res[nres - 1];/*They all fit in with the +.*/
for (i = 0; i <= nres; i++) {
if (i == nres) {/*All were -1, so our range actually contains no residues.*/
Bmax = 0;
}
else if (maxmiss_bit[res[i]] != -1) break;/*We have at least one valid residue.*/
}
long maxdepth = 100;/*Maximal depth, to start.*/
long **depthseq = (long **)pari_malloc(maxdepth * sizeof(long *));/*Tracks the sequence of swaps.*/
if (!depthseq) {
printf("Insufficient memory to allocate to store the depth sequence.\n");
exit(1);
}
int *swaps = (int *)pari_malloc(maxdepth * sizeof(int));/*Tracks the sequence of swaps.*/
if (!swaps) {
printf("Insufficient memory to allocate to store the swaps.\n");
exit(1);
}
for (i = 0; i < maxdepth; i++) {
depthseq[i] = (long *)pari_malloc(n * sizeof(long));
swaps[i] = -1;/*Initialize to all -1's*/
}
missing_update(rclass, bitswap, maxmiss_block, maxmiss_bit, Base, &Bmax, start[entry], res, nres, Bmin, ubits, modulus);
for (i = 0; i < n; i++) depthseq[0][i] = start[i];
long ind = 1;/*Which depth we are working at.*/
while (ind > 0) {/*We are coming in trying to swap this circle out.*/
int cind = ++swaps[ind];/*Increment the swapping index.*/
if (cind == nmats) {/*Overflowed, go back.*/
swaps[ind] = -1;
ind--;
continue;
}
long lastind = ind - 1;
int toofar = 0;
for (i = 0; i < n; i++) {/*Apply the matrix action.*/
long en = 0;
for (j = 0; j < n; j++) en += mats[cind][i][j] * depthseq[lastind][j];
if (en > Bmax) toofar = 1;
depthseq[ind][i] = en;
}
if (toofar) {/*We still want to update the entry on the off chance that we overflowed but are OK in our desired place*/
if (depthseq[ind][entry] <= Bmax) missing_update(rclass, bitswap, maxmiss_block, maxmiss_bit, Base, &Bmax, depthseq[ind][entry], res, nres, Bmin, ubits, modulus);
continue;/*A single entry was too large.*/
}
missing_update(rclass, bitswap, maxmiss_block, maxmiss_bit, Base, &Bmax, depthseq[ind][entry], res, nres, Bmin, ubits, modulus);
/*Do the bitswap to update */
ind++;
if (ind == maxdepth) {/*We are going too deep, must pari_reallocate the storage location.*/
long newdepth = maxdepth << 1;/*Double it.*/
depthseq = pari_realloc(depthseq, newdepth * sizeof(long *));
if (!depthseq) {
printf("Insufficient memory to pari_reallocate the depth sequence.\n");
exit(1);
}
swaps = pari_realloc(swaps, newdepth * sizeof(int));
if (!swaps) {
printf("Insufficient memory to pari_reallocate the swaps.\n");
exit(1);
}
for (i = maxdepth; i < newdepth; i++) {
depthseq[i] = (long *)pari_malloc(n * sizeof(long));
swaps[i] = -1;
}
maxdepth = newdepth;
}
}
missing_tofile(blocks, rclass, Bmin, Bmaxoriginal, start, n, res, nres, ubits, modulus);/*Print to file.*/
/*Time to free all of the allocated memory.*/
pari_free(swaps);
for (i = 0; i < maxdepth; i++) pari_free(depthseq[i]);
pari_free(depthseq);
pari_free(maxmiss_block);
pari_free(maxmiss_bit);
for (i = 0; i < nres; i++) pari_free(rclass[res[i]]);
pari_free(rclass);
pari_free(bitswap);
return;
}
/*Prints the found data to a file.*/
static void
missing_tofile(long blocks, unsigned long **rclass, long Bmin, long Bmax, long *start, long n, long *res, long nres, long ubits, long modulus)
{
long Base = Bmin - 1, i;
Base = Base - (Base % modulus);/*We started at a multiple of modulus to not ruin the mod stuff.*/
if (!pari_is_dir("missing")) {
int s = system("mkdir -p missing");
if (s == -1) pari_err(e_MISC, "ERROR CREATING DIRECTORY missing");
}
char *fname = stack_sprintf("missing/%ld", start[0]);
for (i = 1; i < n; i++) fname = stack_sprintf("%s_%ld", fname, start[i]);
fname = stack_sprintf("%s_%ld-to-%ld.dat", fname, Bmin, Bmax);
FILE *F = fopen(fname, "w");
for (i = 0; i < nres; i++) {/*The ith residue*/
long b = res[i];
fprintf(F, "[");
int found = 0;
long u;
for (u = 0; u < blocks; u++) {/*Figure out each block.*/
unsigned long val = rclass[b][u];
long v;
for (v = 0; v < ubits; v++) {
if (!(val & 1)) {/*A missing value*/
long a = ubits * u + v;
long num = modulus * a + b + Base;/*The correct one!*/
if (num <= Bmax && num >= Bmin) {/*Correct range! We only check >=Bmin due to the initial shift by up to modulus.*/
if (found) fprintf(F, ", %ld", num);/*Print it to the file.*/
else { found = 1; fprintf(F, "%ld", num); }
}
}
val >>= 1;/*Shift right one.*/
}
}
fprintf(F, "]\n");
}
fclose(F);
}
/*semigroup_missing, except we store the sequence of swaps differently. This is slightly slower but suggested if a matrix is parabolic, as storing the whole depth sequence and swaps is extremely costly in terms of memory when B is large.*/
GEN
semigroup_missing_parabolic(GEN mats, GEN B, GEN start, GEN congs, long entry, long load)
{
pari_sp av = avma;
long Bmin = 0, Bmax = 0, t = typ(B);
if (t == t_INT) { Bmin = 1; Bmax = itos(B); }
else if (t == t_VEC || t == t_COL) {
if (lg(B) < 3) pari_err_TYPE("B must be a positive integer or a range of positive integers", B);
if (typ(gel(B, 1)) != t_INT) pari_err_TYPE("B must be a positive integer or a range of positive integers", B);
if (typ(gel(B, 2)) != t_INT) pari_err_TYPE("B must be a positive integer or a range of positive integers", B);
Bmin = itou(gel(B, 1));
Bmax = itou(gel(B, 2));
}
else if (t == t_VECSMALL) {
if (lg(B) < 3) pari_err_TYPE("B must be a positive integer or a range of positive integers", B);
Bmin = B[1];
Bmax = B[2];
}
else pari_err_TYPE("B must be a positive integer or a range of positive integers", B);
if (typ(start) == t_VEC) start = gtocol(start);
else if (typ(start) == t_MAT) start = gel(start, 1);/*Ensure it is a column vector.*/
if (Bmin <= 0) Bmin = 1;
long n = lg(start) - 1;
if (entry < 0 || entry > n) pari_err_TYPE("entry must be an index from 1 to n, the dimension of the vectors computed", stoi(entry));
long nmats = lg(mats) - 1, i;
long ***Cmats = (long ***)pari_malloc(nmats * sizeof(long **));/*Stores the matrices as C arrays*/
long ***Cmatsinv = (long ***)pari_malloc(nmats * sizeof(long **));/*Inverses*/
for (i = 0; i < nmats; i++) {
Cmats[i] = ZM_to_Cmatrix(gel(mats, i + 1));
Cmatsinv[i] = ZM_to_Cmatrix(QM_inv(gel(mats, i + 1)));
}
long *Cstart = (long *)pari_malloc(n * sizeof(long));/*Stores the starting tuple.*/
for (i = 0; i < n; i++) Cstart[i] = itos(gel(start, i + 1));
if (typ(congs) != t_VEC || lg(congs) < 3 || typ(gel(congs, 1)) != t_VEC) pari_err_TYPE("congs must be the relevant congruence conditions, formatted as [[r1, r2, ..., rk], n] for ri modulo n, which should be sorted.", congs);
long modulus = itos(gel(congs, 2)), nres = lg(gel(congs, 1)) - 1;
long *residues = (long *)pari_malloc(nres * sizeof(long));
for (i = 0; i < nres; i++) residues[i] = itos(gmael(congs, 1, i + 1));
findmissing_parabolic(Bmin, Bmax, Cmats, Cmatsinv, nmats, Cstart, n, residues, nres, modulus, entry - 1);
pari_free(residues);
pari_free(Cstart);
long j;
for (i = 0; i < nmats; i++) {
for (j = 0; j < n; j++) {
pari_free(Cmats[i][j]);
pari_free(Cmatsinv[i][j]);
}
pari_free(Cmats[i]);
pari_free(Cmatsinv[i]);
}
pari_free(Cmats);
pari_free(Cmatsinv);
if (!load) return gc_const(av, gen_0);/*Do not load*/
char *fname = stack_sprintf("missing/%Pd", gel(start, 1));
for (i = 2; i <= n; i++) fname = stack_sprintf("%s_%Pd", fname, gel(start, i));
fname = stack_sprintf("%s_%ld-to-%ld.dat", fname, Bmin, Bmax);
set_avma(av);
return gerepileupto(av, gel(gp_readvec_file(fname), 1));/*Load them up!*/
}
/*executes semigroup_missing_par*/
static void
findmissing_parabolic(long Bmin, long Bmax, long ***mats, long ***matsinv, long nmats, long *start, long n, long *res, long nres, long modulus, long entry)
{
long ubits = sizeof(unsigned long) << 3;/*How many bits in an unsigned long*/
unsigned long *bitswap = (unsigned long*)pari_malloc(ubits * sizeof(unsigned long)), i;/*Used for swapping bits of unsigned longs.*/
bitswap[0] = 1;
for (i = 1; i < ubits; i++) bitswap[i] = bitswap[i - 1] << 1;/*bitswap[i] = 2^i*/
long Base = Bmin - 1;
Base -= (Base % modulus);/*We want to start at a multiple of modulus to not ruin the mod stuff.*/
long classmax = (Bmax - Base)/ modulus + 1;/*Maximal number of entries found in each residue class.*/
long blocks = ((classmax - 1) / ubits) + 1;/*This is the number of unsigned longs we need to store in each class.*/
unsigned long **rclass = (unsigned long **)pari_malloc(modulus * sizeof(unsigned long *));/*Stores pointers to the individual classes.*/
for (i = 0; i < nres; i++) {
rclass[res[i]] = (unsigned long *)pari_calloc(blocks * sizeof(unsigned long));/*pari_calloc the classes we want, since we want them as 0 to start.*/
if (!rclass[res[i]]) {
printf("Insufficient memory to allocate to store the curvatures.\n");
exit(1);
}
}
long Bmaxoriginal = Bmax;/*Save for later in case we change it.*/
long *maxmiss_block = (long *)pari_malloc(modulus * sizeof(long));/*Tracks the largest block in the residue class that still contains 0's*/
int *maxmiss_bit = (int *)pari_malloc(modulus * sizeof(int));/*Tracks the largest bit of said class that is non-zero.*/
int Bmaxmod = Bmax % modulus;/*We now will initialize it.*/
long Bmaxbase = Bmax - Bmaxmod, j;
int foundlargest = 0;
for (i = 0; i < nres; i++) {
long mc = Bmaxbase + res[i];
if (res[i] > Bmaxmod) {
if (!foundlargest) {
foundlargest = 1;
if (i) Bmax = Bmaxbase + res[i - 1];/*Update to the largest actually possible value.*/
else Bmax = Bmaxbase + res[nres - 1] - modulus;
}
mc -= modulus;/*The last curvature of this type.*/
}
if (mc < Bmin) {
maxmiss_bit[res[i]] = -1;
maxmiss_block[res[i]] = -1;
continue;
}
mc -= Base;/*Shift it back.*/
long a = mc / modulus;/*Save a in block res[i]*/
maxmiss_bit[res[i]] = a % ubits;
maxmiss_block[res[i]] = a / ubits;
}
if (!foundlargest) Bmax = Bmaxbase + res[nres - 1];/*They all fit in with the +.*/
for (i = 0; i <= nres; i++) {
if (i == nres) {/*All were -1, so our range actually contains no residues.*/
Bmax = 0;
}
else if (maxmiss_bit[res[i]] != -1) break;/*We have at least one valid residue.*/
}
long maxdepth = 100;/*Maximal depth, to start.*/
int *swaps = (int *)pari_malloc(maxdepth * sizeof(int));/*Tracks the sequence of swaps.*/
if (!swaps) {
printf("Insufficient memory to allocate to store the swaps.\n");
exit(1);
}
unsigned long *swapfreq = (unsigned long*)pari_malloc(maxdepth * sizeof(unsigned long));/*Tracks the number of times each swap is executed in a row.*/
for (i = 0; i < maxdepth; i++) swaps[i] = -1;/*Initialize to all -1's*/
missing_update(rclass, bitswap, maxmiss_block, maxmiss_bit, Base, &Bmax, start[entry], res, nres, Bmin, ubits, modulus);/*Insert first entry.*/
long v[n];/*Stores the current orbit entry.*/
for (i = 0; i < n; i++) v[i] = start[i];
long swapind = 1;/*Index of the swapping depth.*/
while (swapind) {/*We are coming in trying to swap at this depth. We always pretend to insert the swaps at a new swapdepth, and double check if it actually is a new entry or we are just increasing the swapfreq.*/
int cind = ++swaps[swapind];/*Increment the swapping index.*/
if (cind == nmats) {/*Overflowed, go back.*/
swapind--;
if (!swapind) continue;/*Done!*/
cind = swaps[swapind];/*How to move backwards.*/
long vnew[n];
for (i = 0; i < n; i++) {/*Move v backwards.*/
long en = 0;
for (j = 0; j < n; j++) en += matsinv[cind][i][j] * v[j];
vnew[i] = en;
}
for (i = 0; i < n; i++) v[i] = vnew[i];/*Update v to the previous.*/
if (--swapfreq[swapind]) {/*Decrease and check if non-zero*/
swapind++;
swaps[swapind] = swaps[swapind - 1];/*Move on.*/
}
else swaps[swapind + 1] = -1;/*Reset the subsequent one.*/
continue;
}
int toofar = 0;
long vnew[n];
for (i = 0; i < n; i++) {/*Apply the matrix action.*/
long en = 0;
for (j = 0; j < n; j++) en += mats[cind][i][j] * v[j];
if (en > Bmax) toofar = 1;
vnew[i] = en;
}
if (toofar) {/*We still want to update the entry on the off chance that we overflowed but are OK in our desired place*/
if (vnew[entry] <= Bmax) missing_update(rclass, bitswap, maxmiss_block, maxmiss_bit, Base, &Bmax, vnew[entry], res, nres, Bmin, ubits, modulus);
continue;/*A single entry was too large.*/
}
for (i = 0; i < n; i++) v[i] = vnew[i];/*Update v,*/
missing_update(rclass, bitswap, maxmiss_block, maxmiss_bit, Base, &Bmax, v[entry], res, nres, Bmin, ubits, modulus);/*Do the bitswap to update */
if (swapind > 1 && swaps[swapind - 1] == swaps[swapind]) {/*It gets added to the previous sequence.*/
swapfreq[swapind - 1]++;
swaps[swapind] = -1;
}
else {
swapfreq[swapind] = 1;
swapind++;
if (swapind == maxdepth) {/*We are going too deep, must pari_reallocate the storage location.*/
long newdepth = maxdepth << 1;/*Double it.*/
swaps = pari_realloc(swaps, newdepth * sizeof(int));
if (!swaps) {
printf("Insufficient memory to pari_reallocate the swaps.\n");
exit(1);
}
for (i = maxdepth; i < newdepth; i++) swaps[i] = -1;
swapfreq = pari_realloc(swapfreq, newdepth * sizeof(unsigned long));
maxdepth = newdepth;
}
}
}
missing_tofile(blocks, rclass, Bmin, Bmaxoriginal, start, n, res, nres, ubits, modulus);/*Print to file.*/
/*Time to free all of the allocated memory.*/
pari_free(swaps);
pari_free(swapfreq);
pari_free(maxmiss_block);
pari_free(maxmiss_bit);
for (i = 0; i < nres; i++) pari_free(rclass[res[i]]);
pari_free(rclass);
pari_free(bitswap);
return;
}
/*Given a sorted list of positive integers miss, this returns 1 if none of them are in the given orbit, and 0 if they are.*/
int
semigroup_missinglist(GEN mats, GEN miss, GEN start, long entry)
{
pari_sp av = avma;
if (!RgV_is_ZVpos(miss)) pari_err_TYPE("miss must be a sorted vector of positive integers.", miss);
if (typ(start) == t_VEC) start = gtocol(start);
else if (typ(start) == t_MAT) start = gel(start, 1);/*Ensure it is a column vector.*/
long lmiss = lg(miss);
if (lmiss == 1) return 1;/*Nothing to miss!*/
long Bmin = itos(gel(miss, 1)), Bmax = itos(gel(miss, lmiss - 1));
long n = lg(start) - 1;
if (entry < 0 || entry > n) pari_err_TYPE("entry must be an index from 1 to n, the dimension of the vectors computed", stoi(entry));
long nmats = lg(mats) - 1, i;
long ***Cmats = (long ***)pari_malloc(nmats * sizeof(long **));/*Stores the matrices as C arrays*/
long ***Cmatsinv = (long ***)pari_malloc(nmats * sizeof(long **));/*Inverses*/
for (i = 0; i < nmats; i++) {
Cmats[i] = ZM_to_Cmatrix(gel(mats, i + 1));
Cmatsinv[i] = ZM_to_Cmatrix(QM_inv(gel(mats, i + 1)));
}
long *Cstart = (long *)pari_malloc(n * sizeof(long));/*Stores the starting tuple.*/
for (i = 0; i < n; i++) Cstart[i] = itos(gel(start, i + 1));
hashtable *hmiss = hash_create_ulong(lmiss, 1);/*Hashtable of the missing entries.*/
for (i = 1; i < lmiss; i++) hash_insert(hmiss, (void *)itou(gel(miss, i)), NULL);/*Insert them.*/
int result = findmissinglist(hmiss, Bmin, Bmax, Cmats, Cmatsinv, nmats, Cstart, n, entry - 1);
pari_free(Cstart);
long j;
for (i = 0; i < nmats; i++) {
for (j = 0; j < n; j++) {
pari_free(Cmats[i][j]);
pari_free(Cmatsinv[i][j]);
}
pari_free(Cmats[i]);
pari_free(Cmatsinv[i]);
}
pari_free(Cmats);
pari_free(Cmatsinv);
hash_destroy(hmiss);
return gc_int(av, result);
}
/*executes semigroup_missinglist*/
static int
findmissinglist(hashtable *hmiss, long Bmin, long Bmax, long ***mats, long ***matsinv, long nmats, long *start, long n, long entry)
{
long maxdepth = 100, i;/*Maximal depth, to start.*/
long **depthseq = (long **)pari_malloc(maxdepth * sizeof(long *));/*Tracks the sequence of moves.*/
if (!depthseq) {
printf("Insufficient memory to allocate to store the depth sequence.\n");
exit(1);
}
int *swaps = (int *)pari_malloc(maxdepth * sizeof(int));/*Tracks the sequence of swaps.*/
if (!swaps) {
printf("Insufficient memory to allocate to store the swaps.\n");
exit(1);
}
for (i = 0; i < maxdepth; i++) {
depthseq[i] = (long *)pari_malloc(n * sizeof(long));
swaps[i] = -1;/*Initialize to all -1's*/
}
if (hash_search(hmiss, (void *)start[entry])) {/*Initial entry was in our missing list.*/
pari_free(swaps);
for (i = 0; i < maxdepth; i++) pari_free(depthseq[i]);
pari_free(depthseq);
return 0;
}
for (i = 0; i < n; i++) depthseq[0][i] = start[i];
long ind = 1;/*Which depth we are working at.*/
while (ind > 0) {/*We are coming in trying to swap this circle out.*/
int cind = ++swaps[ind];/*Increment the swapping index.*/
if (cind == nmats) {/*Overflowed, go back.*/
swaps[ind] = -1;
ind--;
continue;
}
long lastind = ind - 1, j;
int toofar = 0;
for (i = 0; i < n; i++) {/*Apply the matrix action.*/
long en = 0;
for (j = 0; j < n; j++) en += mats[cind][i][j] * depthseq[lastind][j];
if (en > Bmax) toofar = 1;
depthseq[ind][i] = en;
}
if (hash_search(hmiss, (void *)depthseq[ind][entry])) {/*Entry is in our missing list.*/
pari_free(swaps);
for (i = 0; i < maxdepth; i++) pari_free(depthseq[i]);
pari_free(depthseq);
return 0;
}
if (toofar) continue;/*A single entry was too large.*/
ind++;
if (ind == maxdepth) {/*We are going too deep, must pari_reallocate the storage location.*/
long newdepth = maxdepth << 1;/*Double it.*/
depthseq = pari_realloc(depthseq, newdepth * sizeof(long *));
if (!depthseq) {
printf("Insufficient memory to pari_reallocate the depth sequence.\n");
exit(1);
}
swaps = pari_realloc(swaps, newdepth * sizeof(int));
if (!swaps) {
printf("Insufficient memory to pari_reallocate the swaps.\n");
exit(1);
}
for (i = maxdepth; i < newdepth; i++) {
depthseq[i] = (long *)pari_malloc(n * sizeof(long));
swaps[i] = -1;
}
maxdepth = newdepth;
}
}
/*Time to free all of the allocated memory.*/
pari_free(swaps);
for (i = 0; i < maxdepth; i++) pari_free(depthseq[i]);
pari_free(depthseq);
return 1;/*We made it!*/
}
/*Returns the largest entry of the ZC C as a long.*/
INLINE long
ZC_largest_s(GEN C)
{
long x = itos(gel(C, 1)), lC = lg(C), i;
for (i = 2; i < lC; i++) x = maxss(x, itos(gel(C, i)));
return x;
}
/*Returns the orbit of mats*start up to max size of entries B. This is not as efficient as the other methods, so use this for intial observation finding, and use the faster methods for larger searches.*/
GEN
semigroup_orbit(GEN mats, long B, GEN start)
{
pari_sp av = avma;
if (typ(start) == t_VEC) start = gtocol(start);
else if (typ(start) == t_MAT) start = gel(start, 1);/*Ensure it is a column vector.*/
long lmats = lg(mats);
long maxdepth = 100, maxfound = 10000, vind = 0;
GEN v = cgetg(maxfound + 1, t_VEC);
GEN depthseq = cgetg(maxdepth + 1, t_VEC);
GEN swaps = const_vecsmall(maxdepth, 0);
gel(depthseq, 1) = start;
long ind = 2;
while (ind > 1) {
long cind = ++swaps[ind];
if (cind == lmats) {/*Overflowed, go back.*/
swaps[ind] = 0;
ind--;
continue;
}
GEN M = ZM_ZC_mul(gel(mats, cind), gel(depthseq, ind - 1));
if (ZV_equal(M, gel(depthseq, ind - 1))) continue;/*We went nowhere, i.e. had an eigenvector with eigenvalue 1. Move on! This makes it 10% slower, but now doesn't break on [0,1]~ or [1,0]~ and was a much more simple fix.*/
long maxM = ZC_largest_s(M);
if (maxM > B) continue;/*Too big! Go back.*/
vind++;
if (vind > maxfound) {/*Double size.*/
maxfound <<= 1;
v = vec_lengthen(v, maxfound);
}
gel(v, vind) = M;
gel(depthseq, ind) = M;
ind++;
if (ind > maxdepth) {/*Double depth sequence / swaps length.*/
maxdepth <<= 1;
depthseq = vec_lengthen(depthseq, maxdepth);
swaps = vecsmall_lengthen(swaps, maxdepth);
long i;
for (i = ind; i <= maxdepth; i++) swaps[i] = 0;
}
}
return gerepilecopy(av, vec_shorten(v, vind));
}
/*SECTION 3: PSI METHODS*/
/*Computes the sequence of kronecker values kron(x, n), returning a Vecsmall. The length of the sequence, M, is the smallest guaranteed period. In other words, M is the smallest positive integer such that n/M is a rational square, M has same prime factors as n, and if n is even then 4|M (thus if n=2*odd prime, then M=4n. If n is not 2 mod 4, then M<=n).*/
GEN
kron_all(unsigned long n)
{
pari_sp av = avma;
long M, i;
int *kvals = get_kron_vals(n, &M);
GEN allk = cgetg(M + 1, t_VECSMALL);
for (i = 0; i < M; i++) allk[i + 1] = kvals[i];
pari_free(kvals);
return gerepileupto(av, allk);
}
/*Computes the smallest m such that in any sequence of m consecutive integers, there is at least one for which kron(r+, n)=1 and kron(r-, n)=-1. If n is a square, returns 0.*/
long
kron_seq_both(unsigned long n)
{
pari_sp av = avma;
if (uissquare(n)) return gc_long(av, 0);/*Square, return 0.*/
long M;
int *kvals = get_kron_vals(n, &M);
/*Now we go through and compute the longest subsequence without both 1 and -1.*/
long firstm1 = 2;/*kron(0/n)=0, kron(1/n)=1.*/
while (kvals[firstm1] != -1) firstm1++;/*Tracks the first -1, which is guaranteed to exist as n is not a square.*/
long longest = 0, i = firstm1 + 1, lastind = firstm1 - 1;/*Initially, one before the first minus 1*/
/*We seek out the longest consecutive sequences of >=0 and <=0, and return this number plus 1.*/
for (;;) {/*Entering, we are seeking 0, -1;s*/
while (!kvals[lastind]) lastind--;/*Can't go into the negatives, gated by 1*/
lastind++;/*Now kvals[lastind-1] = 1, everything between lastind and i is 0 or -1.*/
while (i < M && kvals[i] < 1) i++;
if (i == M) {/*Done, no overflow.*/
long final = M - lastind + 1;/*Final sequence from lastind to M-1, then looping around to 0*/
if (final > longest) longest = final;
/*Note that kvals[M-1]=-1 necessarily, so no need to backtrack*/
if (firstm1 > longest) longest = firstm1;/*Initial sequence, starts at 0 to firstm1 - 1.*/
break;
}
/*Now the sequence is from lastind to i-1, as kvals[i] = 1.*/
long slen = i - lastind;
if (slen > longest) longest = slen;
lastind = i - 1;
while (!kvals[lastind]) lastind--;/*Now go backwards*/
lastind++;/*Now kvals[lastind-1] = -1, everything between lastind and i is 0 or 1.*/
while (i < M && kvals[i] > -1) i++;
if (i == M) {/*Done, overflow.*/
long final = M - lastind + firstm1;/*Final sequence from lastind to M-1, then looping around from 0 to firstm1-1*/
if (final > longest) longest = final;
break;
}
/*Now the sequence is from lastind to i-1, as kvals[i] = -1.*/
slen = i - lastind;
if (slen > longest) longest = slen;
lastind = i - 1;
}
pari_free(kvals);
return gc_long(av, longest + 1);
}
/*Does kron_all, except returns a malloc'ed C array, and updates len to the length.*/
static int*
get_kron_vals(unsigned long n, long *len)
{
if (n == 1) {
int *ret = (int *)pari_malloc(sizeof(int));
ret[0] = 1;
*len = 1;
return ret;
}
GEN fact = factoru(n), ps = gel(fact, 1), pows = gel(fact, 2);
long M = 1, ep = 1, i, lps = lg(ps);/*Divide out by squares stopping at power 1/2, except for p=2 where we stop at 2/3*/
for (i = 1; i < lps; i++) {
long powpar = pows[i] % 2;
if (ps[i] == 2) {/*Factor of 2*/
powpar += 2;
M <<= powpar;
ep <<= (powpar - 1);/*ep is eulerphi(M).*/
continue;
}
M *= ps[i];
ep *= (ps[i] - 1);
if (!powpar) {
M *= ps[i];
ep *= ps[i];
}
}/*Now we can compute the Kronecker symbols (x/M) for 0<=x<M. There are ep that are non-zero.*/
int *kvals = (int *)pari_calloc(M * sizeof(int));/*Tracks values of Kronecker symbols. 0 if not coprime OR uninitialized.*/
long *foundinds = (long *)pari_malloc(ep * sizeof(long));/*Tracks indices of Kronecker values we computed.*/
kvals[1] = 1;
foundinds[0] = 1;
long nfound = 0;
forprime_t T;
unsigned long p;
u_forprime_init(&T, 2, M);/*Iterate over primes.*/
while ( (p = u_forprime_next(&T)) ) {/*After this loop, we have computed <2, 3, 5, ..., p> in Z/MZ.*/
if (kvals[p]) continue;/*We've already computed this one!*/
if (!(M % p)) continue;/*Not coprime with p.*/
long v = krouu(p, M), mpow = 0, ind = p;/*v = the Kronecker value.*/
long last = 1, i = nfound, j, k;
while (!kvals[ind]) {
last *= v;
kvals[ind] = last;/*Update Kronecker value*/