-
Notifications
You must be signed in to change notification settings - Fork 1
/
qq_bqf.c
3016 lines (2836 loc) · 115 KB
/
qq_bqf.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
//This is a collection of methods dealing with primitive integral binary quadratic forms.
#include <pari.h>
#include "qquadraticdecl.h"
//STATIC DECLARATIONS
//COMPOSITION/CLASS GROUP
static GEN bqf_ncgp_nonfundnarrow(GEN cgp, GEN D, GEN rootD);
//REPRESENTATIONS OF NUMBERS BY BQFs
static GEN bqf_reps_all(GEN n);
static GEN bqf_reps_trivial(void);
static void dbqf_reps_proper(GEN qred, GEN D, GEN n, glist **sols, long *nsols, GEN f, int *terminate);
static void ibqf_reps_proper(GEN qorb, GEN D, GEN rootD, GEN n, glist **sols, long *nsols, GEN f, int *terminate);
static GEN bqf_reps_creatervec(glist *sols, glist *scale, llist *nsolslist, long *totnsols, long *count, int half);
static GEN bqf_reps_creatervec_proper(glist *sols, long nsols, int half);
static GEN bqf_reps_makeprimitive(GEN q, GEN *n);
static void bqf_reps_updatesolutions(glist **sols, long *nsols, GEN *a, GEN *b);
//MORE REPRESENTATION OF NUMBERS
static GEN bqf_bigreps_creatervecfin(GEN newsols, GEN a, GEN b, GEN disc);
static GEN bqf_bigreps_creatervecpos(GEN newsols, GEN a, GEN b, GEN disc);
static GEN bqf_bigreps_createrveclin(GEN newsols, GEN a, GEN b, GEN disc);
static GEN zbqf_bigreps(GEN q, GEN n);
static GEN bqf_linearsolve_zall(GEN yzsols, GEN n2, GEN Minv);
static GEN bqf_linearsolve_zfin(GEN yzsols, GEN n2, GEN Minv);
static GEN bqf_linearsolve_zlin(GEN yzsols, GEN n2, GEN Minv);
static GEN bqf_linearsolve_zpos(GEN yzsols, GEN n2, GEN Minv, GEN M);
static GEN bqf_linearsolve_zquad(GEN yzsols, GEN n2, GEN Minv);
//DISCRIMINANT METHODS
//Generates list of discriminants from D1 to D2, can specify if they are fundamental and coprime to a given input.
GEN disclist(GEN D1, GEN D2, int fund, GEN cop){
pari_sp ltop = avma;
if (typ(D1) != t_INT) pari_err_TYPE("disclist",D1);
if (typ(D2) != t_INT) pari_err_TYPE("disclist",D2);
if (typ(cop) != t_INT) pari_err_TYPE("disclist",cop);
glist *S=NULL;//Pointer to the list start
long count=0;//Counts how many
if(fund==0){
if(gequal0(cop)){
GEN D = gen_0; //int
for(D=icopy(D1); cmpii(D, D2) <= 0; D = addis(D, 1)){//Need to icopy as we give back the memory space for D
if(isdisc(D)){
glist_putstart(&S,D);
count++;
}
else cgiv(D);//This is OK as addis(D,1) will still work, it will just overwrite the memory location which is OK.
}
}
else{
GEN D = gen_0;//int
GEN gc=gen_0;
for(D=icopy(D1); cmpii(D, D2) <= 0; D = addis(D, 1)){
gc=gcdii(cop, D);
if(equali1(gc) && isdisc(D)){
cgiv(gc);
glist_putstart(&S,D);
count++;
}
else{
cgiv(gc);
cgiv(D);
}
}
}
}
else{
if(gequal0(cop)){
GEN D=gen_0;
GEN fD=gen_0;
for(D=icopy(D1); cmpii(D, D2) <= 0; D = addis(D, 1)){
if(isdisc(D)==0){cgiv(D);continue;}
fD=coredisc(D);
if(equalii(fD, D)){
cgiv(fD);
glist_putstart(&S,D);
count++;
}
else{
cgiv(fD);
cgiv(D);
}
}
}
else{
GEN D= gen_0;
GEN fD=gen_0;
GEN gc=gen_0;
for(D=icopy(D1); cmpii(D, D2) <= 0; D = addis(D, 1)){
gc=gcdii(cop,D);
if(equali1(gc)){
cgiv(gc);
if(isdisc(D)==0){cgiv(D);continue;}
fD=coredisc(D);
if(equalii(fD, D)){
cgiv(fD);
glist_putstart(&S,D);
count++;
}
else{
cgiv(fD);
cgiv(D);
}
}
else{
cgiv(gc);
cgiv(D);
}
}
}
}
GEN Svec=glist_togvec(S, count, -1);
Svec = gerepileupto(ltop, Svec);
return Svec;
}
//Generate the list of primes dividing D for which D/p^2 is a discriminant, can pass in facs=factorization of D
GEN discprimeindex(GEN D, GEN facs){
pari_sp top = avma;
if(gequal0(facs)) facs = Z_factor(D);
glist *S=NULL;//Pointer to the list start
long numprimes=itos(gel(matsize(facs),1));
long i;
long count=0;//Counts how many
GEN curp=gen_0;
GEN curexp=gen_0;
GEN four=stoi(4);
for(i=1;i<=numprimes;++i){//Run through the primes, test if we can divide out or not.
curp=gcoeff(facs,i,1);
curexp=gcoeff(facs,i,2);
if(cmpii(curexp,gen_2)>=0){//Exponent needs to be at least 2
if(cmpii(curp,gen_2)>0){//If p>2 we are good
glist_putstart(&S,curp);
count++;
}
else if(cmpii(curexp,four)>=0){//Power of 2 is at least 4=good
glist_putstart(&S,curp);
count++;
}
else if(cmpii(curexp,gen_2)==0 && gequal1(modii(diviiexact(D,four),four))){//4*1 mod 4
glist_putstart(&S,curp);
count++;
}
}
}
GEN Svec=glist_togvec(S, count, -1);
return gerepileupto(top, Svec);
}
//discprimeindex but also checks D is a discriminant
GEN discprimeindex_tc(GEN D){
if(!isdisc(D)) pari_err_TYPE("discprimeindex, not a discriminant",D);
return discprimeindex(D,gen_0);//No garbage
}
//Returns 1 if discriminant and 0 if not.
int isdisc(GEN D){
pari_sp top = avma;
if(typ(D)!=t_INT){//Checking integrality
avma=top;
return 0;
}
if(smodis(D,4)<2 && !Z_issquare(D)){//0,1 mod 4 and not a square.
avma=top;
return 1;
}
avma=top;
return 0;
}
//Returns minimal positive solution [T,U] to T^2-DU^2=4
GEN pell(GEN D){
pari_sp top = avma;
GEN u = quadunit0(D, -1);
if(gequalm1(quadnorm(u))) u = gsqr(u);//We need the smallest unit with norm 1, not -1
if(smodis(D, 2)==1){//D is odd
GEN a = shifti(greal(u),1);//a,b are guarenteed integers since the input is a t_QUAD, and a/2+b*omega=u, omega=(D%2+sqrt(D))/2
GEN rvec = cgetg(3, t_VEC);
gel(rvec, 2) = gimag(u);
gel(rvec, 1) = addii(a,gel(rvec,2));
return gerepileupto(top,rvec);
}
else{//D is even
GEN a = greal(u);
GEN rvec = cgetg(3, t_VEC);
gel(rvec, 1) = shifti(a,1);
gel(rvec, 2) = gimag(u);
return gerepileupto(top,rvec);
}
}
//pell, but checks for D>0 discriminant first.
GEN pell_tc(GEN D){
if(!isdisc(D)) pari_err_TYPE("pell, not a positive discriminant",D);
if(signe(D)==-1) pari_err_TYPE("pell, not a positive discriminant",D);
return pell(D);
}
//Returns log(epsilon(D)), where epsilon(D) is the fundamental unit of positive norm.
GEN posreg(GEN D, long prec){
pari_sp top = avma;
if(gequalm1(quadnorm(quadunit(D)))) return gerepileupto(top,gmulsg(2,quadregulator(D, prec)));
return gerepileupto(top,quadregulator(D, prec));
}
//posreg, but checks for D>0 discriminant first.
GEN posreg_tc(GEN D, long prec){
if(!isdisc(D)) pari_err_TYPE("posreg, not a positive discriminant",D);
if(signe(D)==-1) pari_err_TYPE("posreg, not a positive discriminant",D);
return posreg(D,prec);
}
//Returns sqrt(D) of type t_QUAD
GEN quadroot(GEN D){
pari_sp top = avma;
return gerepileupto(top, quadgen0(shifti(D, 2), -1));//Just do quadgen on 4D
}
//quadroot but does type check.
GEN quadroot_tc(GEN D){
if(typ(D)!=t_INT) pari_err_TYPE("Please enter a non-square INTEGER", D);
if(Z_issquare(D)) pari_err_TYPE("Please enter a NON-SQUARE integer", D);
return quadroot(D);
}
//BASIC OPERATIONS ON BINARY QUADRATIC FORMS
//Returns the generator of the automorphism group of q in PSL(2,Z) (which is Z if D>0, Z/2Z if D=-4, Z/3Z if D=-3, and 1 if D<-4
GEN bqf_automorph_tc(GEN q){
pari_sp top=avma;
GEN D=bqf_checkdisc(q);
if(signe(D)==-1) return gerepileupto(top, dbqf_automorph(q, D));
return gerepileupto(top, ibqf_automorph_D(q, D));
}
//Compares two bqfs based on A, then B, then C. It is safe to modify this method, as sorting/searching will call the pointer to this function
int bqf_compare(void *data, GEN q1, GEN q2){
int i;
for(long j=1;j<=3;++j){
i=cmpii(gel(q1,j),gel(q2,j));
switch(i){
case -1:
return -1;
case 1:
return 1;
}
}
return 0;
}
//bqf_compare, but assumes the inputs are d1=[q1,mat1] and d2=[q2,mat2], and compares q1 and q2. This returns 0 if q1=q2, even if mat1!=mat2 (the mats don't matter)
int bqf_compare_tmat(void *data, GEN d1, GEN d2){
return bqf_compare(data,gel(d1,1),gel(d2,1));
}
//Returns the discriminant of q, which must be length 3 vector and integral.
GEN bqf_disc(GEN q){
pari_sp top = avma;
return gerepileupto(top,subii(sqri(gel(q,2)),shifti(mulii(gel(q,1),gel(q,3)),2)));
}
//Returns the discriminant of q after performing a type check.
GEN bqf_disc_tc(GEN q){
bqf_check(q);
return bqf_disc(q);//No avma necessary
}
//Returns 1 if the BQF q (indefinite/positive definite) of discriminant D with sign Dsign is reduced, and 0 if not.
int bqf_isreduced(GEN q, int Dsign){
pari_sp top=avma;
int answer=0;
if(Dsign==1){//D>0, so AC<0 and B>|A+C|
if(signe(gel(q,1))==signe(gel(q,3))) return 0;
GEN s=addii(gel(q,1),gel(q,3));
GEN abss=absi(s);
if(cmpii(gel(q,2),abss)==1) answer=1;//Only need to update it if this happens
avma=top;
return answer;
}
else{//D<0, so |B|<=A<=C and if A=|B| or A=C then B>=0
if(cmpii(gel(q,1),gel(q,3))<=0){
GEN babs=absi(gel(q,2));
if(cmpii(babs,gel(q,1))<=0){
if(equalii(gel(q,1),babs) || equalii(gel(q,1),gel(q,3))){
if(cmpis(gel(q,2),0)>=0) answer=1;
}
else answer=1;
}
}
avma=top;
return answer;
}
}
//bqf_isreduced with typecheck
int bqf_isreduced_tc(GEN q){
pari_sp top=avma;
bqf_check(q);
GEN D=bqf_disc(q);
int ans=bqf_isreduced(q,signe(D));
avma=top;
return ans;
}
//Generates a random proper bqf with max coefficient maxc. If type=1 it will be indefinite, type=-1 positive definite, type=0 either. primitive=1 means primitive, =0 means don't care. This is not designed for efficiency
GEN bqf_random(GEN maxc, int type, int primitive){
pari_sp top=avma;
setrand(getwalltime());
if(typ(maxc)!=t_INT) return gen_0;
if(signe(maxc)!=1) return gen_0;//Just making sure maxc is a positive integer.
GEN q=cgetg(4,t_VEC);
GEN A, B, C, D;
for(;;){
A=randomi(maxc);
if(signe(randomi(gen_2))==0) togglesign_safe(&A);
B=randomi(maxc);
if(signe(randomi(gen_2))==0) togglesign_safe(&B);
C=randomi(maxc);
if(signe(randomi(gen_2))==0) togglesign_safe(&C);
gel(q,1)=A;
gel(q,2)=B;
gel(q,3)=C;
D=bqf_disc(q);
if(type==1){
if(primitive==1){if(!equali1(ZV_content(q))) continue;}// Not primitive
if(signe(D)==1 && isdisc(D)==1) return gerepilecopy(top,q);
}
else if(type==-1){
if(primitive==1){if(!equali1(ZV_content(q))) continue;}// Not primitive
if(signe(D)==-1){
if(signe(A)==-1) ZV_togglesign(q);//Making positive definite
return gerepilecopy(top,q);
}
}
else{
if(primitive==1){if(!equali1(ZV_content(q))) continue;}// Not primitive
if(signe(D)==-1){
if(signe(A)==-1) ZV_togglesign(q);//Making positive definite
return gerepilecopy(top,q);
}
if(isdisc(D)==1) return gerepilecopy(top,q);
}
}
}
//Generates a random bqf of discriminant D with |B|<=2maxc and primitive. This is not designed for efficiency
GEN bqf_random_D(GEN maxc, GEN D){
pari_sp top=avma;
//setrand(getwalltime());
if(typ(maxc)!=t_INT) return gen_0;
if(signe(maxc)!=1) return gen_0;//Just making sure maxc is a positive integer.
if(!isdisc(D)) return gen_0;
GEN B=randomi(maxc);
B=shifti(B,1);
if(smodis(D,2)==1) B=addii(B,gen_1);
GEN AC=shifti(subii(sqri(B),D),-2);
if(signe(randomi(gen_2))==0) B=negi(B);
GEN g=ggcd(D,B);
GEN Aposs=divisors(AC), A, C;
long r, lx=lg(Aposs);
for(;;){
r=itos(randomi(stoi(lx-1)))+1;
A=gel(Aposs,r);
C=gel(Aposs,lx-r);
if(equali1(ggcd(ggcd(A,C),g))) break;
}
if(signe(D)==1 && signe(AC)==-1){
if(signe(randomi(gen_2))==0) A=negi(A);
else togglesign_safe(&C);
}
else if(signe(D)==1){
if(signe(randomi(gen_2))==0){A=negi(A);C=negi(C);}
}
return gerepilecopy(top, mkvec3(A, B, C));
}
//Reduce the bqf q of discriminant D with sign(D)=Dsign, rootD=sqrt(D) (can pass as NULL if D<0 as it is not needed), tmat=1 if we return the transition matrix and 0 else.
GEN bqf_red(GEN q, GEN rootD, int Dsign, int tmat){
if(Dsign==1){
if(tmat==0) return ibqf_red(q,rootD);
return ibqf_red_tmat(q,rootD);
}
if(tmat==0) return dbqf_red(q);
return dbqf_red_tmat(q);
}
//bqf_red with typecheck
GEN bqf_red_tc(GEN q, int tmat, long prec){
pari_sp top=avma;
GEN D=bqf_checkdisc(q);//No squares allowed
return gerepileupto(top,bqf_red(q,gsqrt(absi(D),prec),signe(D),tmat));
}
//Returns the roots of q in order.
GEN bqf_roots(GEN q, GEN D, GEN w){
pari_sp top = avma;
if(Z_issquare(D)){
if(signe(gel(q, 1))==0){//q[1]=0
if(signe(gel(q,2))==0){//Both roots oo
GEN rvec=cgetg(3,t_VEC);//Return vector
gel(rvec, 1) = mkoo();
gel(rvec, 2) = mkoo();
return rvec;//Nothing unnecessary is added to the stack
}
GEN a=Qdivii(negi(gel(q,3)),gel(q,2));//One of the roots
GEN rvec=cgetg(3,t_VEC);//Return vector
if(signe(gel(q,2))==1){//Sign is 1
gel(rvec,1)=gcopy(a);//copy a into rvec[1]
gel(rvec, 2) = mkoo();
return gerepileupto(top,rvec);
}
else{//Sign is -1
gel(rvec, 2)=gcopy(a);//copy a into rvec[2]
gel(rvec, 1) = mkoo();
return gerepileupto(top,rvec);
}
}//Now the roots are distinct, finite, and rational.
GEN den=shifti(gel(q,1),1);
GEN num1=addii(negi(gel(q,2)),w);//first root is (-q[2]+sqrt(D))/(2q[1])
GEN num2=subii(negi(num1),shifti(gel(q,2),1));//Second root is (-q[2]-sqrt(D))/(2q[1])
GEN rvec=cgetg(3,t_VEC);//Return vector
gel(rvec,1)=Qdivii(num1,den);
gel(rvec,2)=Qdivii(num2,den);
return gerepileupto(top,rvec);
}//Now the roots are as above, but need to use gen functions instead
GEN den=shifti(gel(q,1),1);
GEN num1=gadd(negi(gel(q,2)),w);//first root is (-q[2]+sqrt(D))/(2q[1])
GEN num2=gsub(gneg(num1),shifti(gel(q,2),1));//Second root is (-q[2]-sqrt(D))/(2q[1])
GEN rvec=cgetg(3,t_VEC);//Return vector
gel(rvec,1)=gdiv(num1,den);
gel(rvec,2)=gdiv(num2,den);
return gerepileupto(top,rvec);
}
//bqf_roots, where we only submit q and perform checks
GEN bqf_roots_tc(GEN q){
pari_sp top=avma;
bqf_check(q);//We allow square discriminants
GEN D=bqf_disc(q);
GEN w=gen_0;
if(Z_issquare(D)) w=sqrti(D);
else w=quadroot(D);
return gerepileupto(top,bqf_roots(q,D,w));
}
//takes in BQF q and matrix mtx, and outputs mtx*q.
GEN bqf_trans(GEN q, GEN mtx){
pari_sp top = avma;//q=[A,B,C], mtx=[a,b;c,d], rvec=[Aa^2+Bac+Cc^2,2Aab+Bbc+Bad+2Ccd,Ab^2+Bbd+Cd^2]
GEN Aa=mulii(gel(q,1),gcoeff(mtx,1,1));
GEN Bc=mulii(gel(q,2),gcoeff(mtx,2,1));
GEN Cc=mulii(gel(q,3),gcoeff(mtx,2,1));
GEN AapBc=addii(Aa,Bc);
GEN coef1=addii(mulii(AapBc,gcoeff(mtx,1,1)),mulii(Cc,gcoeff(mtx,2,1)));
GEN coef2=addii(addii(mulii(addii(AapBc,Aa),gcoeff(mtx,1,2)),shifti(mulii(Cc,gcoeff(mtx,2,2)),1)),mulii(gel(q,2),mulii(gcoeff(mtx,1,1),gcoeff(mtx,2,2))));
GEN coef3=addii(mulii(addii(mulii(gel(q,1),gcoeff(mtx,1,2)),mulii(gel(q,2),gcoeff(mtx,2,2))),gcoeff(mtx,1,2)),mulii(gel(q,3),sqri(gcoeff(mtx,2,2))));
GEN rvec=cgetg(4,t_VEC);
gel(rvec,1)=icopy(coef1);
gel(rvec,2)=icopy(coef2);
gel(rvec,3)=icopy(coef3);
return gerepileupto(top,rvec);
}
//bqf_trans with typecheck.
GEN bqf_trans_tc(GEN q, GEN mtx){
bqf_check(q);
intmatrix_check(mtx);
return bqf_trans(q,mtx);
}
//bqf_trans by L^n
GEN bqf_transL(GEN q, GEN n){
GEN qnew=cgetg(4,t_VEC);
pari_sp top=avma;
GEN An=mulii(gel(q,1),n);
GEN AnpB=addii(An,gel(q,2));
GEN An2pBn=mulii(AnpB,n);
pari_sp bot=avma;
GEN qnewA=icopy(gel(q,1));
GEN qnewB=addii(AnpB,An);
GEN qnewC=addii(An2pBn,gel(q,3));
gerepileallsp(top,bot,3,&qnewA,&qnewB,&qnewC);
gel(qnew,1)=qnewA;
gel(qnew,2)=qnewB;
gel(qnew,3)=qnewC;
return qnew;
}
//bqf_trans by R^n
GEN bqf_transR(GEN q, GEN n){
GEN qnew=cgetg(4,t_VEC);
pari_sp top=avma;
GEN Cn=mulii(gel(q,3),n);
GEN CnpB=addii(Cn,gel(q,2));
GEN Cn2pBn=mulii(CnpB,n);
pari_sp bot=avma;
GEN qnewC=icopy(gel(q,3));
GEN qnewB=addii(CnpB,Cn);
GEN qnewA=addii(Cn2pBn,gel(q,1));
gerepileallsp(top,bot,3,&qnewA,&qnewB,&qnewC);
gel(qnew,1)=qnewA;
gel(qnew,2)=qnewB;
gel(qnew,3)=qnewC;
return qnew;
}
//bqf_trans by S, i.e. [A,B,C]->[C,-B,A]
GEN bqf_transS(GEN q){
long lx;
GEN qnew=cgetg_copy(q,&lx);
gel(qnew,1)=icopy(gel(q,3));
gel(qnew,2)=icopy(gel(q,2));
togglesign_safe(&gel(qnew,2));
gel(qnew,3)=icopy(gel(q,1));
return qnew;
}
//Outputs a form similar to q whose first coefficient is coprime to n. Useful for passing between ideals and quadratic forms, where we want to control the prime factors of the ideal. q must be definite/indefinite, and primitive (at least gcd(q,n)=1 is necessary). Note that this method is not very efficient or very good, but it works.
GEN bqf_trans_coprime(GEN q, GEN n){
pari_sp top=avma;
q=ZV_copy(q);
GEN r, x=stoi(3);
while(!equali1(gcdii(gel(q,1),n))){
r=randomi(x);
if(signe(r)==0) q=bqf_transL(q, gen_1);
else if(equali1(r)) q=bqf_transR(q, gen_1);
else q=bqf_transS(q);
}
return gerepileupto(top,q);
}
//bqf_trans_coprime with typechecking
GEN bqf_trans_coprime_tc(GEN q, GEN n){
pari_sp top=avma;
bqf_check(q);//Checking the disc
if(typ(n)!=t_INT || gequal0(n)) pari_err_TYPE("Please enter a non-zero integer n", n);
if(!equali1(gcdii(ZV_content(q),n))) pari_err_TYPE("gcd(q) must be coprime to n", n);
avma=top;
return bqf_trans_coprime(q, n);
}
//BASIC METHODS FOR NEGATIVE DISCRIMINANTS
//bqf_automorph for negative discriminants
GEN dbqf_automorph(GEN q, GEN D){
pari_sp top=avma;
int c=cmpis(D,-4);
if(c<0) return mkmat22(gen_1,gen_0,gen_0,gen_1);//D<-4, no automorph.
GEN M=gel(dbqf_red_tmat(q),2);
if(c==0){//D=-4, and the autom is M[0,1;-1,0]M^-1
return gerepileupto(top,ZM_mul(ZM_mul(M,mkmat22(gen_0,gen_1,gen_m1,gen_0)),ZM_inv(M,NULL)));
}
//Now D=-3, autom is M[0,1;-1,-1]M^-1
return gerepileupto(top,ZM_mul(ZM_mul(M,mkmat22(gen_0,gen_1,gen_m1,gen_m1)),ZM_inv(M,NULL)));
}
//bqf_red for negative discriminants
GEN dbqf_red(GEN q){
if(signe(gel(q,1))==-1){//Negating q if if negative definite
pari_sp top=avma;
GEN qnew=cgetg(3,t_VEC);
for(int i=1;i<=3;++i) gel(qnew,i)=negi(gel(q,i));
GEN negans=dbqf_red(qnew);
return gerepileupto(top,gneg(negans));
}//Now q is positive definite.
pari_sp top=avma;
if(bqf_isreduced(q,-1)) return ZV_copy(q);//No garbage needed
//Now q is not reduced, so the pointer to it will change and not point at the original q.
GEN n=gen_0;//n represents if we are doing L^n or R^n
if(signe(gel(q,2))==1) q=bqf_trans(q,mkmat22s(0,1,-1,0));//Pointing wrong way
if(cmpii(gel(q,1),gel(q,3))<=0){//A<=C, so start by going L
n=gfloor(Qdivii(negi(gel(q,2)),shifti(gel(q,1),1)));//n=floor(-B/2A)
q=bqf_transL(q,n);
}//Now we can loop doing R then L
for(;;){
n=gfloor(Qdivii(negi(gel(q,2)),shifti(gel(q,3),1)));//n=floor(-B/2C)
if(equalii(n,gen_0)) break;//Can't go on!
q=bqf_transR(q,n);
n=gfloor(Qdivii(negi(gel(q,2)),shifti(gel(q,1),1)));//n=floor(-B/2A)
if(equalii(n,gen_0)) break;//Can't go on!
q=bqf_transL(q,n);
if(gc_needed(top,1)) gerepileupto(top,q);//Garbage collection if memory needed.
}
//At this point we have B<0 and B+2A>0, B+2C>0. One of q, Sq, Lq, Rq, LSq, RSq is now reduced.
int arelation=cmpii(gel(q,1),gel(q,3));
GEN mB=negi(gel(q,2));
GEN Lmat=gen_0;
switch(arelation){
case -1://A<C
if(cmpii(mB,gel(q,1))==-1) Lmat=mkmat22s(1,0,0,1);//Already reduced
else if(cmpii(mB,gel(q,3))<=0) Lmat=mkmat22s(1,1,0,1);//A<=-B<=C and A!=C-> L
else Lmat=mkmat22s(-1,1,-1,0);//A<C<-B ->LS
break;
case 0://A=C
if(cmpii(mB,gel(q,1))<=0) Lmat=mkmat22s(0,1,-1,0);//-B<=A=C ->S
else Lmat=mkmat22s(1,0,1,1); //A=C<-B ->R
break;
case 1://A>C
if(cmpii(mB,gel(q,3))<=0) Lmat=mkmat22s(0,1,-1,0);//-B<=C<A ->S
else if(cmpii(mB,gel(q,1))==-1) Lmat=mkmat22s(0,1,-1,1);//C<-B<A ->RS
else Lmat=mkmat22s(1,0,1,1);//C<A<=-B ->R
}
return gerepileupto(top,bqf_trans(q,Lmat));
}
//bqf_red for negative discriminants, also returns transition matrix
GEN dbqf_red_tmat(GEN q){
if(signe(gel(q,1))==-1){//Negating q if if negative definite
pari_sp top=avma;
GEN qnew=cgetg(3,t_VEC);
for(int i=1;i<=3;++i) gel(qnew,i)=negi(gel(q,i));
GEN negans=dbqf_red_tmat(qnew);
GEN rvec=cgetg(3,t_VEC);
gel(rvec,1)=gneg(gel(negans,1));
gel(rvec,2)=ZM_copy(gel(negans,2));
return gerepileupto(top,rvec);
}//Now q is positive definite.
GEN rvec=cgetg(3,t_VEC);
pari_sp top=avma;
GEN Lmat=mkmat22s(1,0,0,1);//Will represent L^n
if(bqf_isreduced(q,-1)){
gel(rvec,1)=ZV_copy(q);
gel(rvec,2)=Lmat;//Just reusing the variable
return rvec;//No garbage needed
}//Now q is not reduced, so the pointer to it will change and not point at the original q.
GEN Rmat=mkmat22s(1,0,0,1);//Will represent R^n
pari_sp mid=avma, bot=avma;
GEN n=gen_0;//n represents if we are doing L^n or R^n
GEN mat=gen_0;//Will represent transition matrix
if(signe(gel(q,2))==1){//Pointing wrong way
mat=mkmat22s(0,1,-1,0);
q=bqf_trans(q,mat);
}
else mat=mkmat22s(1,0,0,1);
if(cmpii(gel(q,1),gel(q,3))<=0){//A<=C, so start by going L
n=gfloor(Qdivii(negi(gel(q,2)),shifti(gel(q,1),1)));//n=floor(-B/2A)
gcoeff(Lmat,1,2)=n;
bot=avma;
q=bqf_transL(q,n);
mat=ZM_mul(mat,Lmat);
}//Now we can loop doing R then L
for(;;){
n=gfloor(Qdivii(negi(gel(q,2)),shifti(gel(q,3),1)));//n=floor(-B/2C)
if(equalii(n,gen_0)) break;//Can't go on!
gcoeff(Rmat,2,1)=n;
bot=avma;//If we exit loop after L, this is required for the final gerepileallsp
q=bqf_transR(q,n);
mat=ZM_mul(mat,Rmat);
n=gfloor(Qdivii(negi(gel(q,2)),shifti(gel(q,1),1)));//n=floor(-B/2A)
if(equalii(n,gen_0)) break;//Can't go on!
gcoeff(Lmat,1,2)=n;
bot=avma;
q=bqf_transL(q,n);
mat=ZM_mul(mat,Lmat);
if(gc_needed(mid,1)) gerepileallsp(mid,bot,2,&q,&mat);//Garbage collection if memory needed.
}
//At this point we have B<0 and B+2A>0, B+2C>0. One of q, Sq, Lq, Rq, LSq, RSq is now reduced.
int arelation=cmpii(gel(q,1),gel(q,3));
GEN mB=negi(gel(q,2));
switch(arelation){
case -1://A<C
if(cmpii(mB,gel(q,1))==-1) Lmat=mkmat22s(1,0,0,1);//Just reusing Lmat. Already reduced
else if(cmpii(mB,gel(q,3))<=0) Lmat=mkmat22s(1,1,0,1);//A<=-B<=C and A!=C-> L
else Lmat=mkmat22s(-1,1,-1,0);//A<C<-B ->LS
break;
case 0://A=C
if(cmpii(mB,gel(q,1))<=0) Lmat=mkmat22s(0,1,-1,0);//-B<=A=C ->S
else Lmat=mkmat22s(1,0,1,1); //A=C<-B ->R
break;
case 1://A>C
if(cmpii(mB,gel(q,3))<=0) Lmat=mkmat22s(0,1,-1,0);//-B<=C<A ->S
else if(cmpii(mB,gel(q,1))==-1) Lmat=mkmat22s(0,1,-1,1);//C<-B<A ->RS
else Lmat=mkmat22s(1,0,1,1);//C<A<=-B ->R
}
bot=avma;
q=bqf_trans(q,Lmat);
mat=ZM_mul(mat,Lmat);
gerepileallsp(top,bot,2,&q,&mat);//Garbage
gel(rvec,1)=q;
gel(rvec,2)=mat;
return rvec;
}
//BASIC OPERATIONS SPECIFIC TO INDEFINITE FORMS/POSITIVE DISCRIMINANTS
//ibqf_automorph, but we pass in q and D and don't check. This is useful in a situation where we don't want to care about pell(D) in the ambient method.
GEN ibqf_automorph_D(GEN q, GEN D){
pari_sp top=avma;
return gerepileupto(top,ibqf_automorph_pell(q,pell(D)));
}
//Returns the invariant automorph of the PIBQF q. If q is not primitive, the output may be wrong.
GEN ibqf_automorph_pell(GEN q, GEN qpell){
pari_sp top = avma;
GEN a=subii(gel(qpell,1),mulii(gel(q,2),gel(qpell,2)));//Top left is a/2
GEN d=addii(negi(a),shifti(gel(qpell,1),1));//Bot right is d/2
GEN b=mulii(gel(q,3),gel(qpell,2));//top right is -b
GEN rvec=cgetg(3,t_MAT);
gel(rvec,1)=cgetg(3,t_COL);
gel(rvec,2)=cgetg(3,t_COL);
gcoeff(rvec,1,1)=shifti(a,-1);//Divide by 2
gcoeff(rvec,1,2)=negi(b);
gcoeff(rvec,2,1)=mulii(gel(q,1),gel(qpell,2));
gcoeff(rvec,2,2)=shifti(d,-1);//Divide by 2
return gerepileupto(top,rvec);
}
//Returns 1 if q is reciprocal
int ibqf_isrecip(GEN q, GEN rootD){
pari_sp top=avma;
long lx;
GEN q2=cgetg_copy(q,&lx);
for(int i=1;i<4;i++) gel(q2,i)=negi(gel(q,i));
GEN z=ibqf_isequiv(q,q2,rootD);
if(equali1(z)){avma=top;return 1;}
avma=top;
return 0;
}
//ibqf_isrecip with typechecking
int ibqf_isrecip_tc(GEN q, long prec){
pari_sp top=avma;
GEN D=bqf_checkdisc(q);
if(signe(D)==-1) pari_err_TYPE("Please enter an indefinite binary quadratic form q", q);
int i=ibqf_isrecip(q, gsqrt(D,prec));
avma=top;
return i;
}
//Given river form q and rootD=sqrt(D), finds the left neighbour of q and returns it
GEN ibqf_leftnbr(GEN q, GEN rootD){
pari_sp top = avma;
if(signe(gel(q,1))==1){//A>0
GEN AmB=subii(gel(q,1),gel(q,2));
GEN AmBpC=addii(AmB,gel(q,3));
if(signe(AmBpC)==1){//Apply (0 1;-1 0), then L maximally, then (0 1;-1 0)
avma=top;
GEN delta=floorr(divri(subir(gel(q,2),rootD),shifti(gel(q,3),1)));//floor((B-sqrt(D))/(2C))
GEN tmat=cgetg(3,t_MAT);
gel(tmat,1)=cgetg(3, t_COL);
gel(tmat,2)=cgetg(3, t_COL);
gcoeff(tmat,1,1)=gen_m1;
gcoeff(tmat,1,2)=gen_0;
gcoeff(tmat,2,1)=delta;
gcoeff(tmat,2,2)=gen_m1;
return gerepileupto(top,bqf_trans(q,tmat));
}
else{//Apply (0 1;-1 0), then R maximally
avma=top;
GEN delta=floorr(divri(addri(rootD,gel(q,2)),shifti(gel(q,1),1)));//floor((sqrt(D)+B)/(2A)
GEN tmat=cgetg(3,t_MAT);
gel(tmat,1)=cgetg(3, t_COL);
gel(tmat,2)=cgetg(3, t_COL);
gcoeff(tmat,1,1)=delta;
gcoeff(tmat,1,2)=gen_1;
gcoeff(tmat,2,1)=gen_m1;
gcoeff(tmat,2,2)=gen_0;
return gerepileupto(top,bqf_trans(q,tmat));
}
}
else{
GEN ApB=addii(gel(q,1),gel(q,2));
GEN ApBpC=addii(ApB,gel(q,3));
if(signe(ApBpC)==1){//Apply L maximally, then (0 1;-1 0)
avma=top;
GEN delta=floorr(divri(negi(addri(rootD,gel(q,2))),shifti(gel(q,1),1)));//floor((-sqrt(D)-B)/2A)
GEN tmat=cgetg(3,t_MAT);
gel(tmat,1)=cgetg(3, t_COL);
gel(tmat,2)=cgetg(3, t_COL);
gcoeff(tmat,1,1)=delta;
gcoeff(tmat,1,2)=gen_m1;
gcoeff(tmat,2,1)=gen_1;
gcoeff(tmat,2,2)=gen_0;
return gerepileupto(top,bqf_trans(q,tmat));
}
else{//Apply then R maximally
avma=top;
GEN delta=floorr(divri(subri(rootD,gel(q,2)),shifti(gel(q,3),1)));//floor((sqrt(D)-B)/2C)
GEN tmat=cgetg(3,t_MAT);
gel(tmat,1)=cgetg(3, t_COL);
gel(tmat,2)=cgetg(3, t_COL);
gcoeff(tmat,1,1)=gen_1;
gcoeff(tmat,1,2)=gen_0;
gcoeff(tmat,2,1)=icopy(delta);
gcoeff(tmat,2,2)=gen_1;
return gerepileupto(top,bqf_trans(q,tmat));
}
}
}
//Given river form q and rootD=sqrt(D), finds the left neighbour of q and the transition matrix
GEN ibqf_leftnbr_tmat(GEN q, GEN rootD){
pari_sp top = avma;
if(signe(gel(q,1))==1){//A>0
GEN AmB=subii(gel(q,1),gel(q,2));
GEN AmBpC=addii(AmB,gel(q,3));
if(signe(AmBpC)==1){//Apply (0 1;-1 0), then L maximally, then (0 1;-1 0)
avma=top;
GEN delta=floorr(divri(subir(gel(q,2),rootD),shifti(gel(q,3),1)));//floor((B-sqrt(D))/(2C))
GEN rvec=cgetg(3,t_VEC);
GEN tmat=cgetg(3,t_MAT);
gel(tmat,1)=cgetg(3, t_COL);
gel(tmat,2)=cgetg(3, t_COL);
gcoeff(tmat,1,1)=gen_m1;
gcoeff(tmat,1,2)=gen_0;
gcoeff(tmat,2,1)=icopy(delta);
gcoeff(tmat,2,2)=gen_m1;
gel(rvec,2)=tmat;
gel(rvec,1)=bqf_trans(q,tmat);
return gerepileupto(top,rvec);
}
else{//Apply (0 1;-1 0), then R maximally
avma=top;
GEN delta=floorr(divri(addri(rootD,gel(q,2)),shifti(gel(q,1),1)));//floor((sqrt(D)+B)/(2A)
GEN rvec=cgetg(3,t_VEC);
GEN tmat=cgetg(3,t_MAT);
gel(tmat,1)=cgetg(3, t_COL);
gel(tmat,2)=cgetg(3, t_COL);
gcoeff(tmat,1,1)=icopy(delta);
gcoeff(tmat,1,2)=gen_1;
gcoeff(tmat,2,1)=gen_m1;
gcoeff(tmat,2,2)=gen_0;
gel(rvec,2)=tmat;
gel(rvec,1)=bqf_trans(q,tmat);
return gerepileupto(top,rvec);
}
}
else{
GEN ApB=addii(gel(q,1),gel(q,2));
GEN ApBpC=addii(ApB,gel(q,3));
if(signe(ApBpC)==1){//Apply L maximally, then (0 1;-1 0)
avma=top;
GEN delta=floorr(divri(negi(addri(rootD,gel(q,2))),shifti(gel(q,1),1)));//floor((-sqrt(D)-B)/2A)
GEN rvec=cgetg(3,t_VEC);
GEN tmat=cgetg(3,t_MAT);
gel(tmat,1)=cgetg(3, t_COL);
gel(tmat,2)=cgetg(3, t_COL);
gcoeff(tmat,1,1)=icopy(delta);
gcoeff(tmat,1,2)=gen_m1;
gcoeff(tmat,2,1)=gen_1;
gcoeff(tmat,2,2)=gen_0;
gel(rvec,2)=tmat;
gel(rvec,1)=bqf_trans(q,tmat);
return gerepileupto(top,rvec);
}
else{//Apply then R maximally
avma=top;
GEN delta=floorr(divri(subri(rootD,gel(q,2)),shifti(gel(q,3),1)));//floor((sqrt(D)-B)/2C)
GEN rvec=cgetg(3,t_VEC);
GEN tmat=cgetg(3,t_MAT);
gel(tmat,1)=cgetg(3, t_COL);
gel(tmat,2)=cgetg(3, t_COL);
gcoeff(tmat,1,1)=gen_1;
gcoeff(tmat,1,2)=gen_0;
gcoeff(tmat,2,1)=icopy(delta);
gcoeff(tmat,2,2)=gen_1;
gel(rvec,2)=tmat;
gel(rvec,1)=bqf_trans(q,tmat);
return gerepileupto(top,rvec);
}
}
}
//ibqf_leftnbr, updating the transition matrix
GEN ibqf_leftnbr_update(GEN qvec, GEN rootD){
pari_sp top=avma;
GEN rn=ibqf_leftnbr_tmat(gel(qvec,1),rootD);
long lx;
GEN rvec=cgetg_copy(rn,&lx);
gel(rvec,1)=ZV_copy(gel(rn,1));
gel(rvec,2)=ZM_mul(gel(qvec,2),gel(rn,2));
return gerepileupto(top,rvec);
}
//ibqf_leftnbr but with type checking
GEN ibqf_leftnbr_tc(GEN q, int tmat, long prec){
pari_sp top=avma;
GEN D=bqf_checkdisc(q);
if(signe(D)!=1) pari_err_TYPE("please supply an indefinite binary quadratic form",q);
if(signe(gel(q,1))==signe(gel(q,3))) pari_warn(warner,"form is not on the river!");
if(tmat==0) return gerepileupto(top,ibqf_leftnbr(q,gsqrt(D,prec)));
return gerepileupto(top,ibqf_leftnbr_tmat(q,gsqrt(D,prec)));
}
//bqf_red for positive discriminants
GEN ibqf_red(GEN q, GEN rootD){
pari_sp top=avma;
GEN toriv=ibqf_toriver(q,rootD);//Now the form is on the river
if(bqf_isreduced(toriv,1)) return toriv;//No garbage!
return gerepilecopy(top,ibqf_rightnbr(toriv,rootD));//If not reduced, take the right neighbour
}
//bqf_red for positive discriminants, also returns transition matrix.
GEN ibqf_red_tmat(GEN q, GEN rootD){
pari_sp top=avma;
GEN toriv=ibqf_toriver_tmat(q,rootD);//Now the form is on the river
if(bqf_isreduced(gel(toriv,1),1)) return toriv;
return gerepileupto(top,ibqf_rightnbr_update(toriv,rootD));
}
//Reduces q to a form with A>0
GEN ibqf_red_pos(GEN q, GEN rootD){
pari_sp top=avma;
GEN red1=ibqf_red(q,rootD);
if(signe(gel(red1,1))==-1) red1=ibqf_leftnbr(red1,rootD);
return gerepileupto(top,red1);
}
//ibqf_red_pos plus, transition matrix.
GEN ibqf_red_pos_tmat(GEN q, GEN rootD){
pari_sp top=avma;
GEN red1=ibqf_red_tmat(q,rootD);
if(signe(gel(gel(red1,1),1))==1) return red1;//Done!
return gerepileupto(top,ibqf_leftnbr_update(red1,rootD));//Go left to make A>0
}
//Returns the reduced orbit of q
GEN ibqf_redorbit(GEN q, GEN rootD){
pari_sp top = avma;
GEN qred=ibqf_red(q,rootD);
glist *orbit=NULL;//orbit list
GEN qseek=qred;
long nforms=0;
do{
glist_putstart(&orbit,qseek);
qseek=ibqf_rightnbr(qseek,rootD);
nforms++;
}
while(!ZV_equal(qred,qseek));
return gerepileupto(top,glist_togvec(orbit, nforms, -1));
}
//Returns the reduced orbit of q along with the corresponding transition matrices
GEN ibqf_redorbit_tmat(GEN q, GEN rootD){
pari_sp top = avma;
GEN qred=ibqf_red_tmat(q,rootD);
clist *origin=NULL;//orbit list
clist_putbefore(&origin,qred);
clist *left=origin, *right=origin;
GEN qseekL=qred, qseekR=qred;
long nforms=1;
for(;;){
qseekR=ibqf_rightnbr_update(qseekR,rootD);//No need to check as this will NEVER equal qseekL; the signs of the first coefficient will always be distinct here.
clist_putafter(&right,qseekR);
nforms++;
qseekL=ibqf_leftnbr_update(qseekL,rootD);
if(ZV_equal(gel(qseekL,1),gel(qseekR,1))) break;//Done!
clist_putbefore(&left,qseekL);
nforms++;
}
return gerepileupto(top,clist_togvec(origin,nforms,1));
}
//Returns the forms with A>0 in the reduced orbit of q
GEN ibqf_redorbit_posonly(GEN q, GEN rootD){
pari_sp top = avma;
GEN qred=ibqf_red_pos(q,rootD);
glist *orbit=NULL;//orbit list
GEN qseek=qred;
long nforms=0;
do{
glist_putstart(&orbit,qseek);
qseek=ibqf_rightnbr(ibqf_rightnbr(qseek,rootD),rootD);
nforms++;
}
while(!ZV_equal(qred,qseek));
return gerepileupto(top,glist_togvec(orbit, nforms, -1));
}
//Returns the forms with A>0 in the reduced orbit of q along with the transition matrices
GEN ibqf_redorbit_posonly_tmat(GEN q, GEN rootD){
pari_sp top = avma;
GEN qred=ibqf_red_pos_tmat(q,rootD);
clist *origin=NULL;//orbit list
clist_putbefore(&origin,qred);
clist *left=origin, *right=origin;
GEN qseekL=qred, qseekR=qred;
long nforms=1;
for(;;){
qseekR=ibqf_rightnbr_update(ibqf_rightnbr_update(qseekR,rootD),rootD);