-
Notifications
You must be signed in to change notification settings - Fork 1
/
qq_quat.c
2401 lines (2200 loc) · 103 KB
/
qq_quat.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
//Quaternionic methods over Q
#include <pari.h>
#include "qquadraticdecl.h"
//The length (lg, so technically length+1) of a quaternion algebra entry and an initialized quaternion order.
#define QALEN 5
#define QAORDLEN 8
//STATIC DECLARATIONS
static GEN qa_ord_type(GEN Q, GEN ord, GEN level);
static GEN qa_init_m2z(void);
static GEN qa_ord_init_trace0basis(GEN Q, GEN ord, GEN maxds);
static GEN qa_conjbasis_orient(GEN Q, GEN ord, GEN v1, GEN v2, GEN e2);
static int qa_embed_isnewoptimal(GEN Q, GEN ord, GEN ordinv, GEN D, GEN Dmod2, GEN dfacs, GEN emb, GEN gcdf1g1h1, GEN embs, long pos, long prec);
static int qa_embedor_compare(void *data, GEN pair1, GEN pair2);
static int qa_orbitrepsprime_checksol(GEN Q, GEN order, GEN T, GEN W, GEN *reps, long *place, GEN sol);
//A quaternion algebra Q is stored as the vector [nf, pset, [a,b,-ab], product of finite primes in pset] where it is an algebra over the field nf, pset is the set of ramified primes, and the algebra can be represented as (a,b/nf), so that i^2=a, j^2=b, k^2=-ab. Over Q, we use nf=0, and by convention will always have gcd(a, b)=1.
//An order is stored as order=[ord, type, [d1, d2, d3, d4], level, prime factorization of the level, ord^(-1), [basis of ord intersect trace 0 elements]]. The column vectors of ord form a Z-basis of the order. type=-1 means a general order, =0 is maximal, and =1 is Eichler. d_i is the maximal denominator appearing in coefficients of 1,i,j,k. The prime factorization is stored as [[p_1, e_1],...,[p_n, e_n]].
//Orders can sometimes be passed in as the 4x4 matrix of column vectors, OR containing the full suite of computed data. The typecheck methods will in general allow either. Other methods will take the first type if the variable is named "ord", and the second type if it is named "order".
//BASIC OPERATIONS ON ELEMENTS IN QUATERNION ALGEBRAS
//Returns the conjugate of the quaternion element x. Note that the quaternion algebra is not required as an input.
GEN qa_conj(GEN x){
long lx;
GEN ret=cgetg_copy(x, &lx);
gel(ret, 1)=gcopy(gel(x, 1));
for(int i=2;i<5;i++) gel(ret, i)=gneg(gel(x, i));
return ret;//No garbage
}
//qa_conj with typechecking
GEN qa_conj_tc(GEN x){
qa_eltcheck(x);
return qa_conj(x);
}
//Returns yxy^(-1)
GEN qa_conjby(GEN Q, GEN x, GEN y){
pari_sp top=avma;
GEN yinv=qa_inv(Q, y);
GEN yx=qa_mul(Q, y, x);
return gerepileupto(top, qa_mul(Q, yx, yinv));
}
//qa_conjby with typecheck
GEN qa_conjby_tc(GEN Q, GEN x, GEN y){
qa_check(Q);qa_eltcheck(x);qa_eltcheck(y);
return qa_conjby(Q, x, y);
}
//Returns the inverse of the quaternion element x.
GEN qa_inv(GEN Q, GEN x){
pari_sp top=avma;
GEN qconj=qa_conj(x);
GEN n=qa_norm(Q, x);
if(gequal0(n)) pari_err_INV("Cannot invert norm 0 element", x);
return gerepileupto(top, gdiv(qconj,n));
}
//qa_inv with typechecking
GEN qa_inv_tc(GEN Q, GEN x){
qa_check(Q);qa_eltcheck(x);
return qa_inv(Q, x);
}
//Given an indefinite quaternion algebra with a>0, this outputs the image of x under the standard embedding into M_2(R). This is given by 1->1, i->[sqrt(a),0;0,-sqrt(a)], j->[0,b;1,0], k->[0,b sqrt(a);-sqrt(a),0].
GEN qa_m2rembed(GEN Q, GEN x){
pari_sp top=avma;
GEN rta;
if(lg(qa_getpram(Q))==1) rta=gen_1;//quadgen only takes non-squares
else rta=quadroot(qa_geta(Q));
GEN x2rt=gmul(gel(x, 2), rta), x4rt=gmul(gel(x, 4), rta);
GEN x3px4rt=gadd(gel(x, 3), x4rt);
GEN emb=cgetg(3, t_MAT);
gel(emb, 1)=cgetg(3, t_COL), gel(emb, 2)=cgetg(3, t_COL);
gcoeff(emb, 1, 1)=gadd(gel(x, 1), x2rt);
gcoeff(emb, 1, 2)=gmul(qa_getb(Q), x3px4rt);
gcoeff(emb, 2, 1)=gsub(gel(x, 3), x4rt);
gcoeff(emb, 2, 2)=gsub(gel(x, 1), x2rt);
return gerepileupto(top, emb);
}
//qa_m2r_embed with typecheck
GEN qa_m2rembed_tc(GEN Q, GEN x){
qa_indefcheck(Q);
qa_eltcheck(x);
return qa_m2rembed(Q, x);
}
//Returns the min poly of x, i.e [1, b, c] for x^2+bx+c or [1, b] for x+b
GEN qa_minpoly(GEN Q, GEN x){
if(gequal0(gel(x, 2)) && gequal0(gel(x, 3)) && gequal0(gel(x, 4))){//Rational
GEN ret=cgetg(3, t_VEC);gel(ret, 1)=gen_1;gel(ret, 2)=gneg(gel(x, 1));
return ret;
}
GEN ret=cgetg(4, t_VEC);
gel(ret, 1)=gen_1;
gel(ret, 2)=gmulgs(gel(x, 1), -2);//- the trace
gel(ret, 3)=qa_norm(Q, x);
return ret;
}
//qa_minpoly wiht typechecking
GEN qa_minpoly_tc(GEN Q, GEN x){
qa_check(Q);qa_eltcheck(x);
return qa_minpoly(Q, x);
}
//Multiplies x by y in Q. Note that x, y CAN be column vectors, where the result is the same type as x.
GEN qa_mul(GEN Q, GEN x, GEN y){
pari_sp top=avma;
GEN a=qa_geta(Q), b=qa_getb(Q), mab=qa_getmab(Q);//The a and b and -ab for the q-alg
GEN A1A2=gmul(gel(x, 1), gel(y, 1)), aB1B2=gmul(a, gmul(gel(x, 2), gel(y, 2))), bC1C2=gmul(b, gmul(gel(x, 3), gel(y, 3))), mabD1D2=gmul(mab, gmul(gel(x, 4), gel(y, 4)));
GEN A1A2paB1B2=gadd(A1A2, aB1B2), bC1C2mabD1D2=gadd(bC1C2, mabD1D2);//The two parts of the 1 coefficient
GEN A1B2=gmul(gel(x, 1), gel(y, 2)), A2B1=gmul(gel(x, 2), gel(y, 1)), C1D2=gmul(gel(x, 3), gel(y, 4)), C2D1=gmul(gel(x, 4), gel(y, 3));
GEN A1B2pA2B1=gadd(A1B2, A2B1), mbC1D2pC2D1=gmul(b, gsub(C2D1, C1D2));//The two parts of the i coefficient
GEN A1C2=gmul(gel(x, 1), gel(y, 3)), A2C1=gmul(gel(x, 3), gel(y, 1)), B1D2=gmul(gel(x, 2), gel(y, 4)), B2D1=gmul(gel(x, 4), gel(y, 2));
GEN A1C2pA2C1=gadd(A1C2, A2C1), aB1D2maB2D1=gmul(a, gsub(B1D2, B2D1));//The two parts of the j coefficient
GEN A1D2=gmul(gel(x, 1), gel(y, 4)), A2D1=gmul(gel(x, 4), gel(y, 1)), B1C2=gmul(gel(x, 2), gel(y, 3)), B2C1=gmul(gel(x, 3), gel(y, 2));
GEN A1D2pA2D1=gadd(A1D2, A2D1), B1C2mB2C1=gsub(B1C2, B2C1);//The two parts of the k coefficient
long lx;
GEN ret=cgetg_copy(x, &lx);
gel(ret, 1)=gadd(A1A2paB1B2, bC1C2mabD1D2);
gel(ret, 2)=gadd(A1B2pA2B1, mbC1D2pC2D1);
gel(ret, 3)=gadd(A1C2pA2C1, aB1D2maB2D1);
gel(ret, 4)=gadd(A1D2pA2D1, B1C2mB2C1);
return gerepileupto(top, ret);
}
//qa_mul with typechecking
GEN qa_mul_tc(GEN Q, GEN x, GEN y){
qa_check(Q);qa_eltcheck(x);qa_eltcheck(y);
return qa_mul(Q, x, y);
}
//Returns the set of elements in S1*S2
GEN qa_mulsets(GEN Q, GEN S1, GEN S2){
long l1=lg(S1)-1, l2=lg(S2)-1, ind=1;
GEN prod=cgetg(l1*l2+1, t_VEC);
for(long i=1;i<=l1;i++){
for(long j=1;j<=l2;j++){
gel(prod, ind)=qa_mul(Q, gel(S1, i), gel(S2, j));
ind++;
}
}
return prod;
}
//Multiplies the elements of a vector together
GEN qa_mulvec(GEN Q, GEN L){
pari_sp top=avma;
long lx=lg(L);
GEN prod;
if(lx==1){prod=zerovec(4);gel(prod, 1)=gen_1;return prod;}
if(lx==2) return gcopy(gel(L, 1));
prod=qa_mul(Q, gel(L, 1), gel(L, 2));
for(long i=3;i<lx;i++) prod=qa_mul(Q, prod, gel(L, i));
return gerepileupto(top, prod);
}
//qa_mul_vec with typechecking
GEN qa_mulvec_tc(GEN Q, GEN L){
qa_check(Q);
if(typ(L)!=t_VEC) pari_err_TYPE("Please input a vector of elements of Q", L);
return qa_mulvec(Q, L);
}
//Returns L[indices[1]]*...*L[indices[n]], where indices is a vecsmall.
GEN qa_mulvecindices(GEN Q, GEN L, GEN indices){
pari_sp top=avma;
long lx=lg(indices);
GEN prod;
if(lx==1){prod=zerovec(4);gel(prod, 1)=gen_1;return prod;}
if(lx==2) return gcopy(gel(L, indices[1]));
prod=qa_mul(Q, gel(L, indices[1]), gel(L, indices[2]));
for(long i=3;i<lx;i++) prod=qa_mul(Q, prod, gel(L, indices[i]));
return gerepileupto(top, prod);
}
//qa_mulvecindices with typechecking and setting indices to be a vecsmall
GEN qa_mulvecindices_tc(GEN Q, GEN L, GEN indices){
pari_sp top=avma;
qa_check(Q);
if(typ(L)!=t_VEC) pari_err_TYPE("Please input a vector of elements of Q", L);
GEN vsmallindices=gtovecsmall(indices);
pari_CATCH(CATCH_ALL){
avma=top;
pari_CATCH_reset();
pari_err_TYPE("Invalid inputs; does indices have members that are too big?", indices);
return gen_0;
}
pari_TRY{
GEN result=qa_mulvecindices(Q, L, vsmallindices);
pari_CATCH_reset();
return gerepileupto(top, result);
}
pari_ENDCATCH
}
//Returns the norm of x
GEN qa_norm(GEN Q, GEN x){
pari_sp top=avma;
GEN a=qa_geta(Q), b=qa_getb(Q);
GEN t1=gsub(gsqr(gel(x, 1)), gmul(gsqr(gel(x, 2)), a));
GEN t2=gmul(b, gsub(gmul(a, gsqr(gel(x, 4))), gsqr(gel(x, 3))));
return gerepileupto(top, gadd(t1, t2));
}
//qa_norm with typechecking
GEN qa_norm_tc(GEN Q, GEN x){
qa_check(Q);qa_eltcheck(x);
return qa_norm(Q, x);
}
//Powers x to the n
GEN qa_pow(GEN Q, GEN x, GEN n){
pari_sp top=avma;
if(gequal0(n)) return mkvec4(gen_1, gen_0, gen_0, gen_0);//1
if(signe(n)==-1){x=qa_inv(Q, x);n=negi(n);}
GEN dig=binary_zv(n);
GEN qpow=gcopy(x);
for(long i=2;i<lg(dig);i++){
qpow=qa_square(Q, qpow);
if(dig[i]==1) qpow=qa_mul(Q, qpow, x);
}
return gerepileupto(top, qpow);
}
//qa_pow with typechecking
GEN qa_pow_tc(GEN Q, GEN x, GEN n){
qa_check(Q);qa_eltcheck(x);
if(typ(n)!=t_INT) pari_err_TYPE("Please enter an integer power", n);
return qa_pow(Q, x, n);
}
//Given an x=[e,f,g,h] in a quaternion algebra Q, this returns the roots of [e,f,g,h] under the standard embedding into SL(2,R), with the "first root" coming first. We must have a>0 for this to be good.
GEN qa_roots(GEN Q, GEN x, long prec){
pari_sp top=avma;
if(gsigne(gel(x, 1))==-1) x=gneg(x);//Make x have positive trace
GEN abvec=qa_getabvec(Q);
GEN n=qa_norm(Q, x), x1sq=gsqr(gel(x, 1));
if(gsigne(n)!=1 || gcmp(x1sq, n)==-1) pari_err_TYPE("Not a positive norm hyperbolic element", x);
GEN roota=gsqrt(gel(abvec, 1), prec);
GEN den=gsub(gel(x, 3), gmul(gel(x, 4), roota));
if(gequal0(gel(x, 3)) && gequal0(gel(x, 4))){//x[3]=x[4]=0
int s=gsigne(gel(x, 2));
GEN ret=cgetg(3, t_VEC);
if(s==1){gel(ret, 1)=mkoo();gel(ret, 2)=gen_0;}
else if(s==-1){gel(ret, 1)=gen_0;gel(ret, 2)=mkoo();}
else pari_err_TYPE("x must not be rational", x);
return gerepileupto(top, ret);
}
else if(gequal0(den)){//We must be in the M_2(Q) case, presumably with a=1
GEN rtpart=gdiv(gmul(gadd(gel(x, 3), gel(x, 4)), gel(abvec, 2)), gmulgs(gel(x, 2), 2));//b(x[3]+x[4])/(2x[2])
GEN ret=cgetg(3, t_VEC);
if(gsigne(gel(x, 2))==1){
gel(ret, 1)=mkoo();
gel(ret, 2)=gneg(rtpart);
}
else{
gel(ret, 1)=gneg(rtpart);
gel(ret, 2)=mkoo();
}
return gerepileupto(top, ret);
}//Now x[3]-sqrt(a)*x[4]=den is nonzero
GEN x2rt1=gmul(gel(x, 2), roota), rootpart=gsqrt(gsub(x1sq, n), prec);
GEN r1num=gadd(x2rt1, rootpart);
GEN r2num=gsub(x2rt1, rootpart);
GEN ret=cgetg(3, t_VEC);
gel(ret, 1)=gdiv(r1num, den);
gel(ret, 2)=gdiv(r2num, den);
return gerepileupto(top, ret);
}
//qa_roots with typecheck
GEN qa_roots_tc(GEN Q, GEN x, long prec){
qa_indefcheck(Q);
qa_eltcheck(x);
return qa_roots(Q, x, prec);
}
//Returns the square of x in Q
GEN qa_square(GEN Q, GEN x){
pari_sp top=avma;
GEN a=qa_geta(Q), b=qa_getb(Q);
GEN A2paB2=gadd(gsqr(gel(x, 1)), gmul(a, gsqr(gel(x, 2)))), bC2mabD2=gmul(b, gsub(gsqr(gel(x, 3)), gmul(a, gsqr(gel(x, 4))))), twoA=gmulgs(gel(x, 1), 2);
long lx;
GEN ret=cgetg_copy(x, &lx);
gel(ret, 1)=gadd(A2paB2, bC2mabD2);
gel(ret, 2)=gmul(twoA, gel(x, 2));
gel(ret, 3)=gmul(twoA, gel(x, 3));
gel(ret, 4)=gmul(twoA, gel(x, 4));
return gerepileupto(top, ret);
}
//qa_square with typechecking
GEN qa_square_tc(GEN Q, GEN x){
qa_check(Q);qa_eltcheck(x);
return qa_square(Q, x);
}
//Returns the trace of x, an element of a quaternion algebra.
GEN qa_trace(GEN x){return gmulgs(gel(x, 1), 2);}
//qa_trace with typechecking
GEN qa_trace_tc(GEN x){qa_eltcheck(x);return qa_trace(x);}
//BASIC OPERATIONS ON ORDERS/LATTICES IN QUATERNION ALGEBRAS
//Checks if x is in the order specificed by ordinv^(-1).
int qa_isinorder(GEN Q, GEN ordinv, GEN x){
pari_sp top=avma;
GEN xcol=gtocol(x);
GEN v=gmul(ordinv, xcol);
if(typ(Q_content(v))==t_INT){avma=top;return 1;}//No denominators, in order.
avma=top;return 0;//Denominators, not in order
}
//qa_isinorder with typechecking, and taking in the order or initialized order (and not its inverse).
int qa_isinorder_tc(GEN Q, GEN ord, GEN x){
pari_sp top=avma;
qa_check(Q);qa_eltcheck(x);
if(typ(ord)==t_VEC && lg(ord)==QAORDLEN) return qa_isinorder(Q, qa_getordinv(ord), x);
QM_check(ord);
int isord=qa_isinorder(Q, ginv(ord), x);
avma=top;return isord;
}
//Returns 1 if the order specified by the row vectors of ord is indeed an order, and 0 else.
int qa_isorder(GEN Q, GEN ord, GEN ordinv){
pari_sp top=avma;
GEN x;
for(int i=1;i<=4;i++){
for(int j=1;j<=4;j++){
x=qa_mul(Q, gel(ord, i), gel(ord, j));//Inputs are columns, so output will be too.
settyp(x, t_VEC);//Making x a row vector instead of column.
if(!qa_isinorder(Q, ordinv, x)){avma=top;return 0;}
}
}
avma=top;
return 1;
}
//qa_isorder with typecheck
int qa_isorder_tc(GEN Q, GEN ord){
pari_sp top=avma;
qa_check(Q);
QM_check(ord);
int isord=qa_isorder(Q, ord, ginv(ord));
avma=top;
return isord;
}
//Given a quaternion algebra Q and a lattice L in Q, this method finds the left order associated to L. L is inputted as a 4x4 matrix with column space being L
GEN qa_leftorder(GEN Q, GEN L, GEN Linv){
pari_sp top=avma;
GEN qa_i=mkcol4s(0,1,0,0), qa_j=mkcol4s(0,0,1,0), qa_k=mkcol4s(0,0,0,1);//representing i, j, k as column vectors
GEN Mvec=cgetg(5, t_VEC);
for(int i=1;i<=4;i++){
gel(Mvec, i)=cgetg(5, t_MAT);//Matrices for doing L[,i]*(1,i,j,k)
gel(gel(Mvec, i), 1)=gel(L, i);//1*L[i]
gel(gel(Mvec, i), 2)=qa_mul(Q, qa_i, gel(L, i));//i*L[i]
gel(gel(Mvec, i), 3)=qa_mul(Q, qa_j, gel(L, i));//j*L[i]
gel(gel(Mvec, i), 4)=qa_mul(Q, qa_k, gel(L, i));//k*L[i]
gel(Mvec, i)=gmul(Linv, gel(Mvec, i));//L^(-1)*M
gel(Mvec, i)=shallowtrans(gel(Mvec, i));//Transposing it
}
GEN M=shallowconcat(shallowconcat(shallowconcat(gel(Mvec, 1), gel(Mvec, 2)), gel(Mvec, 3)), gel(Mvec, 4));//Concatenating into a 4x16 matrix
M=shallowtrans(QM_hnf(M));//Taking hnf, transposing.
return gerepileupto(top, QM_hnf(ginv(M)));
}
//qa_leftorder with typechecking
GEN qa_leftorder_tc(GEN Q, GEN L){
pari_sp top=avma;
qa_check(Q);QM_check(L);
return gerepileupto(top, qa_leftorder(Q, L, ginv(L)));
}
//Given a quaternion algebra Q and a lattice L in Q, this method finds the right order associated to L. L is inputted as a 4x4 matrix with column space being L
GEN qa_rightorder(GEN Q, GEN L, GEN Linv){
pari_sp top=avma;
GEN qa_i=mkcol4s(0,1,0,0), qa_j=mkcol4s(0,0,1,0), qa_k=mkcol4s(0,0,0,1);//representing i, j, k as column vectors
GEN Mvec=cgetg(5, t_VEC);
for(int i=1;i<=4;i++){
gel(Mvec, i)=cgetg(5, t_MAT);//Matrices for doing L[,i]*(1,i,j,k)
gel(gel(Mvec, i), 1)=gel(L, i);//1*L[i]
gel(gel(Mvec, i), 2)=qa_mul(Q, gel(L, i), qa_i);//i*L[i]
gel(gel(Mvec, i), 3)=qa_mul(Q, gel(L, i), qa_j);//j*L[i]
gel(gel(Mvec, i), 4)=qa_mul(Q, gel(L, i), qa_k);//k*L[i]
gel(Mvec, i)=gmul(Linv, gel(Mvec, i));//L^(-1)*M
gel(Mvec, i)=shallowtrans(gel(Mvec, i));//Transposing it
}
GEN M=shallowconcat(shallowconcat(shallowconcat(gel(Mvec, 1), gel(Mvec, 2)), gel(Mvec, 3)), gel(Mvec, 4));//Concatenating into a 4x16 matrix
M=shallowtrans(QM_hnf(M));//Taking hnf, transposing.
return gerepileupto(top, QM_hnf(ginv(M)));
}
//qa_leftorder with typechecking
GEN qa_rightorder_tc(GEN Q, GEN L){
pari_sp top=avma;
qa_check(Q);QM_check(L);
return gerepileupto(top, qa_rightorder(Q, L, ginv(L)));
}
//Conjugates the order ord by the (invertible element) c.
GEN qa_ord_conj(GEN Q, GEN ord, GEN c){
pari_sp top=avma;
GEN neword=cgetg(5, t_MAT);
gel(neword, 1)=qa_conjby(Q, gel(ord, 1), c);settyp(gel(neword, 1), t_COL);//Making the columns and converting from t_VEC to t_COL
gel(neword, 2)=qa_conjby(Q, gel(ord, 2), c);settyp(gel(neword, 2), t_COL);
gel(neword, 3)=qa_conjby(Q, gel(ord, 3), c);settyp(gel(neword, 3), t_COL);
gel(neword, 4)=qa_conjby(Q, gel(ord, 4), c);settyp(gel(neword, 4), t_COL);
return gerepileupto(top, QM_hnf(neword));
}
//qa_ord_conj with typechecking
GEN qa_ord_conj_tc(GEN Q, GEN ord, GEN c){
qa_check(Q);qa_eltcheck(c);
GEN actualord=qa_ordcheck(ord);
return qa_ord_conj(Q, actualord, c);
}
//Returns the discriminant of the order ord.
GEN qa_ord_disc(GEN Q, GEN ord){
pari_sp top=avma;
GEN M=cgetg(5, t_MAT);
for(long i=1;i<5;i++) gel(M, i)=cgetg(5, t_COL);
for(long i=1;i<5;i++){
for(long j=1;j<5;j++){
gcoeff(M, i, j)=qa_trace(qa_mul(Q, gel(ord, i), gel(ord, j)));
}
}
GEN d=absi(ZM_det(M));
return gerepileupto(top, sqrti(d));
}
//qa_ord with typechecking (we do NOT check that ord does give an order however).
GEN qa_ord_disc_tc(GEN Q, GEN ord){
qa_check(Q);
GEN actualord=qa_ordcheck(ord);
return qa_ord_disc(Q, actualord);
}
//Returns the matrix M such that v^T*M*v=nrd(x) where x=v[1]*ord[1]+v[2]*ord[2]+v[3]*ord[3]+v[4]*ord[4] (ord[i]=ith column of ord).
GEN qa_ord_normform(GEN Q, GEN ord){
pari_sp top=avma;
long lx;
GEN ordconj=cgetg_copy(ord, &lx);
for(long i=1;i<=4;i++) gel(ordconj, i)=qa_conj(gel(ord, i));//The conjugate basis
GEN mat=cgetg(5, t_MAT);
for(long i=1;i<=4;i++) gel(mat, i)=cgetg(5, t_COL);
for(long i=1;i<=4;i++){
for(long j=i;j<=4;j++) gcoeff(mat, i, j)=gel(qa_mul(Q, gel(ord, i), gel(ordconj, j)), 1);//Upper triangle
}
for(long i=2;i<=4;i++) for(long j=1;j<i;j++) gcoeff(mat, i, j)=gcoeff(mat, j, i);//Lower triangle
return gerepilecopy(top, mat);
}
//Returns the type of the order, 0 if maximal, 1 if Eichler, -1 else.
static GEN qa_ord_type(GEN Q, GEN ord, GEN level){
if(equali1(level)) return gen_0;
pari_sp top=avma;
//There should be MUCH better ways to do this, but this is what I am doing for now.
GEN maxord=qa_superorders(Q, ord, level);
GEN howmany=sumdivk(level, 0);//Product of e_i+1
if(gequal(stoi(lg(maxord)-1), howmany)){avma=top;return gen_1;}//Eichler
return gc_const(top, gen_m1);
}
//Finds all superorders when n need not be a prime
GEN qa_superorders(GEN Q, GEN ord, GEN n){
pari_sp top=avma;
if(equali1(n)){
GEN ret=cgetg(2, t_VEC);
gel(ret, 1)=gcopy(ord);
return ret;//Trivially, ord is the only one for n=1.
}
GEN fact=Z_factor(n);
GEN currentords=mkvec(ord), p, newords;
long pexp, lords;
for(long pind=1;pind<lg(gel(fact, 1));pind++){//The prime
p=gcoeff(fact, pind, 1);
pexp=itos(gcoeff(fact, pind, 2));//The exponent of p
for(long exp=1;exp<=pexp;exp++){
newords=cgetg_copy(currentords, &lords);
for(long k=1;k<lords;k++) gel(newords, k)=qa_superorders_prime(Q, gel(currentords, k), ginv(gel(currentords, k)), p);//Finding all the new orders
currentords=shallowconcat1(newords);//Making them into one vector
currentords=gen_sort_uniq(currentords, (void*)cmp_universal, &cmp_nodata);//Removing duplicates.
}
}
return gerepileupto(top, currentords);
}
//Finds all superorders ord' such that the index of ord in ord' is n. ord' should be an order and n is prime.
GEN qa_superorders_prime(GEN Q, GEN ord, GEN ordinv, GEN n){
pari_sp top=avma;
//We are looking for X=sum(a_i e_i) with a_i integral so that x*ei is in <ord, x> for i=1,2,3,4 and x is integral. The first (4) conditions give rise to 4 matrices, and x needs to be an eigenvector for all 4 modulo n. If this holds and x is integral, then we have a legit order.
GEN Ms=cgetg(5, t_VEC), M=cgetg(5, t_MAT);
for(long k=1;k<5;k++){//Woring on Ms[k]
for(long i=1;i<5;i++) gel(M, i)=qa_mul(Q, gel(ord, i), gel(ord, k));//Result is a column vector, as desired.
gel(Ms, k)=FpM_red(QM_mul(ordinv, M), n);//Multiply by ordinv to get in terms of original bases, reduce modulo n
}
GEN space=cgetg(5, t_VEC);
GEN nspace=cgetg(5, t_VECSMALL);
long maxsupspaces=1, spacesize;
for(long i=1;i<5;i++){
gel(space, i)=FpM_eigenvecs(gel(Ms, i), n);
spacesize=lg(gel(space, i));
if(spacesize==1){avma=top;return cgetg(1, t_VEC);}//One of the matrices had no eigenspace modulo n.
nspace[i]=spacesize;
maxsupspaces=maxsupspaces*(spacesize-1);//Update maxsupspaces
}
GEN ords=vectrunc_init(maxsupspaces);
GEN A, B, C, D, neword;
for(long i1=1;i1<nspace[1];i1++){
A=gmael3(space, 1, i1, 2);
for(long i2=1;i2<nspace[2];i2++){
B=FpM_intersect(A, gmael3(space, 2, i2, 2), n);
if(lg(B)==1) continue;//Nope
for(long i3=1;i3<nspace[3];i3++){
C=FpM_intersect(B, gmael3(space, 3, i3, 2), n);
if(lg(C)==1) continue;//Nope
for(long i4=1;i4<nspace[4];i4++){
D=FpM_intersect(C, gmael3(space, 4, i4, 2), n);
if(lg(D)==1) continue;//Nope
else if (lg(D)>2) pari_err_TYPE("D unexpectedly had dimension >1, please report this bug.", D);
D=gdiv(gadd(gadd(gmul(gel(ord, 1), gcoeff(D, 1, 1)), gmul(gel(ord, 2), gcoeff(D, 2, 1))), gadd(gmul(gel(ord, 3), gcoeff(D, 3, 1)), gmul(gel(ord, 4), gcoeff(D, 4, 1)))), n);//Creating the original element by translating back to [1, i, j, k] coeffs and dividing by n
if(typ(qa_norm(Q, D))!=t_INT) continue;//Not integral
if(typ(qa_trace(D))!=t_INT) continue;//Not integral
//If we reach here, we are guarenteed an order
neword=QM_hnf(shallowconcat(ord, D));//The new order
vectrunc_append(ords, neword);//Add the new order
}
}
}
}
return gerepilecopy(top, ords);
}
//qa_superorders with typecheck
GEN qa_superorders_tc(GEN Q, GEN ord, GEN n){
qa_check(Q);
if(typ(n)!=t_INT || signe(n)!=1) pari_err_TYPE("Please enter a positive integer n", n);
if(typ(ord)==t_VEC && lg(ord)==QAORDLEN) return qa_superorders(Q, qa_getord(ord), n);
QM_check(ord);
return qa_superorders(Q, ord, n);
}
//INITIALIZATION METHODS
//Returns an initialized Eichler order of level l contained in the maximal order maxord.
GEN qa_eichlerorder(GEN Q, GEN l, GEN maxord){
pari_sp top=avma, mid;
//We start by finding an element with norm divisible by l, conjugating maxord by it, and intersecting to get an Eichler order whose level is hopefully divisible by l.
long var=fetch_var();
GEN x=pol_x(var);//The variable x
GEN b1=gel(maxord, 1), b2=gel(maxord, 2), b3=gel(maxord, 3), b4=gel(maxord, 4);//Basis elements
GEN base=gmul(x, b1), lfac=Z_factor(l), maxorddisc=qa_getpramprod(Q);
GEN c4=gen_0, elt4, elt3, elt2, elt, nform, A, B, C, Dneg, roots, xsol, conord, intord, newlevel, extralevel, r;
int found=0;
for(;;){
c4=addis(c4, 1);
elt4=gmul(c4, b4);
for(GEN c3=gen_0;gcmp(c3, c4)<=0;c3=addis(c3, 1)){
elt3=gadd(elt4, gmul(c3, b3));
for(GEN c2=gen_0;gcmp(c2, c4)<=0;c2=addis(c2, 1)){
mid=avma;
elt2=gadd(elt3, gmul(c2, b2));
elt=gadd(base, elt2);
nform=qa_norm(Q, elt);//Quadratic in x.
A=polcoef_i(nform, 2, var);
B=polcoef_i(nform, 1, var);
C=polcoef_i(nform, 0, var);
Dneg=gsub(gmulgs(gmul(A, C), 4), gsqr(B));//4AC-B^2
roots=Zn_quad_roots(lfac, gen_0, Dneg);
if(roots==NULL){avma=mid;continue;}//Nope
//We have an element!
for(long i=1;i<lg(gel(roots, 2));i++){
xsol=Fp_red(gdiv(gsub(gel(gel(roots, 2), i), B), gmulgs(A, 2)), l);//A solution for x
elt=gadd(elt2, gmul(b1, xsol));
pari_CATCH(e_INV){//When Q is unramified everywhere, elt may have norm 0
elt=gadd(elt, gmul(b1, l));//Add l until we get an element with non-zero norm.
}
pari_RETRY{
conord=qa_ord_conj(Q, maxord, elt);
}
pari_ENDCATCH
intord=module_intersect(maxord, conord);
newlevel=diviiexact(qa_ord_disc(Q, intord), maxorddisc);
extralevel=dvmdii(newlevel, l, &r);//Dividing newlevel by l
if(gequal0(r)){found=1;break;}
}
if(found==1) break;
avma=mid;
continue;
}
if(found==1) break;
}
if(found==1) break;
}
delete_var();//Delete the variable
gerepileall(top, 3, &intord, &extralevel, &lfac);//Clearing all the crap.
GEN ord=gel(qa_superorders(Q, intord, extralevel), 1);
//Now we initialize the order
long nprimes=lgcols(lfac);//nprimes-1=number of distinct prime divisors of level.
GEN r1=row(ord, 1), r2=row(ord, 2), r3=row(ord, 3), r4=row(ord, 4);
GEN ret=cgetg(QAORDLEN, t_VEC);
gel(ret, 1)=QM_hnf(ord);
gel(ret, 2)=gen_1;
gel(ret, 3)=cgetg(5, t_VEC);
gel(gel(ret, 3), 1)=Q_denom(r1);gel(gel(ret, 3), 2)=Q_denom(r2);gel(gel(ret, 3), 3)=Q_denom(r3);gel(gel(ret, 3), 4)=Q_denom(r4);
gel(ret, 4)=icopy(l);
gel(ret, 5)=cgetg(nprimes, t_VEC);
for(long i=1;i<nprimes;i++){gel(gel(ret, 5), i)=mkvec2copy(gcoeff(lfac, i, 1), gcoeff(lfac, i, 2));}
gel(ret, 6)=ginv(gel(ret, 1));
gel(ret, 7)=qa_ord_init_trace0basis(Q, gel(ret, 1), gel(ret, 3));
return gerepileupto(top, ret);
}
//qa_eichler order with type checking and presetting baseord
GEN qa_eichlerorder_tc(GEN Q, GEN l, GEN maxord){
pari_sp top=avma;
qa_check(Q);
if(gequal0(maxord)) maxord=gel(qa_maximalorder(Q, matid(4)), 1);//Compute maximal order
else maxord=qa_ordcheck(maxord);//Retrieve the maximal order
if(!equali1(gcdii(l, qa_getpramprod(Q)))) pari_err_TYPE("The level of an Eichler order must be coprime to the discriminant of Q.", l);
return gerepileupto(top, qa_eichlerorder(Q, l, maxord));
}
//Initialized a quaternion order with relevant data.
GEN qa_ord_init(GEN Q, GEN ord){
pari_sp top=avma;
GEN disc=qa_ord_disc(Q, ord);
GEN level=diviiexact(disc, qa_getpramprod(Q));
GEN lfac=Z_factor(level);
long nprimes=lgcols(lfac);//nprimes-1=number of distinct prime divisors of level.
GEN r1=row(ord, 1), r2=row(ord, 2), r3=row(ord, 3), r4=row(ord, 4);
GEN ret=cgetg(QAORDLEN, t_VEC);
gel(ret, 1)=QM_hnf(ord);
gel(ret, 2)=qa_ord_type(Q, ord, level);
gel(ret, 3)=cgetg(5, t_VEC);
gel(gel(ret, 3), 1)=Q_denom(r1);gel(gel(ret, 3), 2)=Q_denom(r2);gel(gel(ret, 3), 3)=Q_denom(r3);gel(gel(ret, 3), 4)=Q_denom(r4);
gel(ret, 4)=icopy(level);
gel(ret, 5)=cgetg(nprimes, t_VEC);
for(long i=1;i<nprimes;i++){gel(gel(ret, 5), i)=mkvec2copy(gcoeff(lfac, i, 1), gcoeff(lfac, i, 2));}
gel(ret, 6)=ginv(gel(ret, 1));
gel(ret, 7)=qa_ord_init_trace0basis(Q, gel(ret, 1), gel(ret, 3));
return gerepileupto(top, ret);
}
//qa_ord_init with typecheck
GEN qa_ord_init_tc(GEN Q, GEN ord){
qa_check(Q);QM_check(ord);
return qa_ord_init(Q, ord);
}
//Initialized the quaternion algebra given by a, b.
GEN qa_init_ab(GEN a, GEN b){
pari_sp top=avma;
GEN pset=qa_ram_fromab(a, b), psetprod=gen_1;//Ramifying primes
long lp=lg(pset)-1;
if(lp>1){
if(typ(gel(pset, lp))==t_INFINITY) lp--;//Definite
for(long i=1;i<=lp;i++) psetprod=mulii(psetprod, gel(pset, i));//The discriminant
}
GEN ma=gneg(a);//-a
GEN alg=cgetg(5, t_VEC);
gel(alg, 1)=gen_0;
gel(alg, 2)=gcopy(pset);
gel(alg, 3)=cgetg(4, t_VEC);
gel(gel(alg, 3), 1)=icopy(a);//a
gel(gel(alg, 3), 2)=icopy(b);//b
gel(gel(alg, 3), 3)=mulii(ma, b);//-ab
gel(alg, 4)=icopy(psetprod);
return gerepileupto(top, alg);
}
//qa_init_ab with typechecking
GEN qa_init_ab_tc(GEN a, GEN b){
if(typ(a)!=t_INT || gequal0(a)) pari_err_TYPE("Please enter two non-zero integers", a);
if(typ(b)!=t_INT || gequal0(b)) pari_err_TYPE("Please enter two non-zero integers", b);
return qa_init_ab(a, b);
}
//Primes must be sorted, and the oo prime is present. If type=1 assumes indefinite, and type=-1 is definite.
GEN qa_init_primes(GEN pset, int type){
pari_sp top=avma;
if(lg(pset)==1) return qa_init_m2z();
GEN prodp=gen_1, relations, extracong, u;//We initiate a prime search with the correct search conditions.
if(type==-1){//Definite
for(long i=1;i<lg(pset)-1;i++) prodp=mulii(prodp, gel(pset, i));//Product of all the finite primes
u=gen_m1;
if(equalii(gel(pset, 1), gen_2)){//2 ramifies (it is the smallest prime, so must be first as pset is assumed to be sorted).
relations=cgetg(lg(pset)-2, t_VEC);
for(long i=1;i<lg(pset)-2;i++) gel(relations, i)=mkvec2(gel(pset, i+1), stoi(-kronecker(gen_m1, gel(pset, i+1))));//u*q is a non-residue for each odd prime ramifying
extracong=mkvec2s(8, 3);
}
else{//2 does not ramify
relations=cgetg(lg(pset)-1, t_VEC);
for(long i=1;i<lg(pset)-1;i++) gel(relations, i)=mkvec2(gel(pset, i), stoi(-kronecker(gen_m1, gel(pset, i))));//u*q is a non-residue for each odd prime ramifying
extracong=mkvec2s(8, 7);
}
}
else{//Indefinite
for(long i=1;i<lg(pset);i++) prodp=mulii(prodp, gel(pset, i));//Product of all the finite primes
u=gen_1;
if(equalii(gel(pset, 1), gen_2)){//2 ramifies (it is the smallest prime, so must be first as pset is assumed to be sorted).
relations=cgetg(lg(pset)-1, t_VEC);
for(long i=1;i<lg(pset)-1;i++) gel(relations, i)=mkvec2(gel(pset, i+1), gen_m1);//u*q is a non-residue for each odd prime ramifying
extracong=mkvec2s(8, 5);
}
else{//2 does not ramify
long lx;
relations=cgetg_copy(pset, &lx);
for(long i=1;i<lg(pset);i++) gel(relations, i)=mkvec2(gel(pset, i), gen_m1);//u*q is a non-residue for each odd prime ramifying
extracong=mkvec2s(8, 1);
}
}
GEN p=prime_ksearch(relations, extracong);
GEN alg=cgetg(5, t_VEC);
gel(alg, 1)=gen_0;
gel(alg, 2)=gcopy(pset);
gel(alg, 3)=cgetg(4, t_VEC);
gel(gel(alg, 3), 1)=mulii(prodp, u);
gel(gel(alg, 3), 2)=mulii(u, p);
gel(gel(alg, 3), 3)=mulii(prodp, p);
togglesign_safe(&gel(gel(alg, 3), 3));//Fixing to -ab
gel(alg, 4)=icopy(prodp);
return gerepileupto(top, alg);
}
//qa_init_primes with sorting of pset, adding in oo if missing. Checks for positive integers but NOT distinct primes.
GEN qa_init_primes_tc(GEN pset){
pari_sp top=avma;
if(typ(pset)!=t_VEC) pari_err_TYPE("Please enter a vector of primes", pset);
if(lg(pset)==1) return qa_init_m2z();
GEN psort=sort(pset);
for(long i=1;i<lg(psort);i++){
if(typ(gel(psort, i))!=t_INT || signe(gel(psort, i))==-1){
if(typ(gel(psort, i))!=t_INFINITY) pari_err_TYPE("Please enter a vector of primes", pset);
}
}
long l=lg(psort);
if(l%2==1){
if(typ(gel(psort, l-1))==t_INFINITY) return gerepileupto(top, qa_init_primes(psort, -1));//Definite
return gerepileupto(top, qa_init_primes(psort, 1));//Indefinite
}
if(typ(gel(psort, l-1))==t_INFINITY) pari_err_TYPE("Odd length list with oo included not allowed", pset);
GEN psortoo=cgetg(l+1, t_VEC);
for(long i=1;i<l;i++) gel(psortoo, i)=gel(psort, i);
gel(psortoo, l)=mkoo();//Adding in oo prime
return gerepileupto(top, qa_init_primes(psortoo, -1));//Definitie
}
//Initializes the quaternion algebra for M_2(Z)
static GEN qa_init_m2z(void){
GEN alg=cgetg(5, t_VEC);
gel(alg, 1)=gen_0;
gel(alg, 2)=cgetg(1, t_VEC);
gel(alg, 3)=mkvec3(gen_1, gen_1, gen_m1);
gel(alg, 4)=gen_1;
return alg;
}
//Initializes a quaternion algebra over Q with two primes and a corresponding maximal order
GEN qa_init_2primes(GEN p, GEN q){
pari_sp top=avma;
if(cmpii(p,q)==-1){GEN r=q;q=p;p=r;}//WLOG p>q
GEN a, b, ord=zeromatcopy(4, 4);
if(equalii(q,gen_2)){//Case of q=2
long m8=smodis(p, 8);
GEN r;
switch(m8){
case 1://p==1(8) and q=2; (2p, -r)
r=prime_ksearch(mkvec(mkvec2(p, gen_m1)), mkvec2(stoi(8), stoi(3)));
a=shifti(p, 1);
b=negi(r);
GEN x=Zp_sqrt(shifti(p, 1), r, 1);//sqrt(2p) modulo r
gcoeff(ord, 1, 1)=gen_1;gcoeff(ord, 1, 2)=ghalf;
gcoeff(ord, 2, 3)=ghalf;
gcoeff(ord, 3, 2)=ghalf;gcoeff(ord, 3, 4)=Qdivii(x, r);
gcoeff(ord, 4, 3)=ghalf;gcoeff(ord, 4, 4)=Qdivii(gen_1, r);
break;
case 5://p==5(8) and q=2; (p, 2)
a=p;
b=gen_2;
gcoeff(ord, 1, 1)=gen_1;gcoeff(ord, 1, 2)=ghalf;
gcoeff(ord, 2, 2)=ghalf;
gcoeff(ord, 3, 3)=gen_1;gcoeff(ord, 3, 4)=ghalf;
gcoeff(ord, 4, 4)=ghalf;
break;
default: //p==3(4); (p, -1)
a=p;
b=gen_m1;
gcoeff(ord, 1, 1)=gen_1;gcoeff(ord, 1, 4)=ghalf;
gcoeff(ord, 2, 2)=gen_1;gcoeff(ord, 2, 4)=ghalf;
gcoeff(ord, 3, 3)=gen_1;gcoeff(ord, 3, 4)=ghalf;
gcoeff(ord, 4, 4)=ghalf;
}
}
else{//Now q>2
long p1=smodis(p, 4), q1=smodis(q, 4);
if(p1==3 && q1==3){//p==q==3 (4); (pq, -1)
a=mulii(p, q);
b=gen_m1;
gcoeff(ord, 1, 1)=gen_1;gcoeff(ord, 1, 2)=ghalf;
gcoeff(ord, 2, 2)=ghalf;
gcoeff(ord, 3, 3)=gen_1;gcoeff(ord, 3, 4)=ghalf;
gcoeff(ord, 4, 4)=ghalf;
}
else{//p==1 (4) or q==1 (4)
if(kronecker(p, q)==-1){//(p/q)=-1
a=p;
b=q;
gcoeff(ord, 1, 1)=gen_1;gcoeff(ord, 1, 2)=ghalf;gcoeff(ord, 1, 4)=ghalf;
gcoeff(ord, 2, 4)=ghalf;
gcoeff(ord, 3, 4)=ghalf;
gcoeff(ord, 4, 4)=ghalf;
if(p1==1){
gcoeff(ord, 2, 2)=ghalf;gcoeff(ord, 3, 3)=gen_1;
}
else{
gcoeff(ord, 2, 3)=gen_1;gcoeff(ord, 3, 2)=ghalf;
}
}
else{//(p/q)=1
GEN s1, s2;
if(p1==1) s1=gen_m1;
else s1=gen_1;
if(q1==1) s2=gen_m1;
else s2=gen_1;//Getting the signs of -p%4 and -q%4
GEN r=prime_ksearch(mkvec2(mkvec2(p, s1), mkvec2(q, s2)), mkvec2(stoi(4), stoi(3)));
a=mulii(p, q);
b=negi(r);
GEN x=Zp_sqrt(mulii(p, q), r, 1);//sqrt(pq) modulo r
gcoeff(ord, 1, 1)=gen_1;gcoeff(ord, 1, 2)=ghalf;
gcoeff(ord, 2, 3)=ghalf;
gcoeff(ord, 3, 2)=ghalf;gcoeff(ord, 3, 4)=Qdivii(x, r);
gcoeff(ord, 4, 3)=ghalf;gcoeff(ord, 4, 4)=Qdivii(gen_1, r);
}
}
}
GEN ret=cgetg(3, t_VEC);
GEN alg=cgetg(5, t_VEC);
gel(alg, 1)=gen_0;
gel(alg, 2)=mkvec2copy(q, p);
gel(alg, 3)=cgetg(4, t_VEC);
gel(gel(alg, 3), 1)=icopy(a);
gel(gel(alg, 3), 2)=icopy(b);
gel(gel(alg, 3), 3)=mulii(a,b);
togglesign_safe(&gel(gel(alg, 3), 3));//Fixing to -ab
gel(alg, 4)=mulii(p, q);
gel(ret, 1)=alg;
gel(ret, 2)=qa_ord_init(alg, ord);
return gerepileupto(top, ret);
}
//Initializes a quaternion algebra over Q with two primes and checks the inputs are valid.
GEN qa_init_2primes_tc(GEN p, GEN q){
if(typ(p)!=t_INT || typ(q)!=t_INT || equalii(p, q) || !isprime(p) || !isprime(q)) pari_err_TYPE("Please input two distinct prime numbers", mkvec2(p, q));
return qa_init_2primes(p, q);
}
//Returns generators for the basis of the elements of ord with trace zero
static GEN qa_ord_init_trace0basis(GEN Q, GEN ord, GEN maxds){
pari_sp top=avma;
GEN tracezero=zeromatcopy(4, 3);
gcoeff(tracezero, 2, 1)=gdivsg(1, gel(maxds, 2));
gcoeff(tracezero, 3, 2)=gdivsg(1, gel(maxds, 3));
gcoeff(tracezero, 4, 3)=gdivsg(1, gel(maxds, 4));
GEN space=module_intersect(ord, tracezero);
GEN ret=cgetg(4, t_VEC);
gel(ret, 1)=gtovec(gel(space, 1));
gel(ret, 2)=gtovec(gel(space, 2));
gel(ret, 3)=gtovec(gel(space, 3));
return gerepileupto(top, ret);
}
//Returns an initialized maximal order of Q containing ord.
GEN qa_maximalorder(GEN Q, GEN baseord){
pari_sp top=avma;
GEN level=diviiexact(qa_ord_disc(Q, baseord), qa_getpramprod(Q));//The level of baseord
GEN sups=qa_superorders(Q, baseord, level);//Get all maximal orders containing baseord; this is non-empty.
return gerepileupto(top, qa_ord_init(Q, gel(sups, 1)));
}
//qa_maximal order with type checking and presetting baseord
GEN qa_maximalorder_tc(GEN Q, GEN baseord){
pari_sp top=avma;
qa_check(Q);
if(gequal0(baseord)) baseord=matid(4);//This is an order
else baseord=qa_ordcheck(baseord);//Retrieve the base order
return gerepileupto(top, qa_maximalorder(Q, baseord));
}
//Given an a and a b, returns the set of primes ramifying in the quaternion algebra ramified at a, b
GEN qa_ram_fromab(GEN a, GEN b){
pari_sp top=avma;
glist *S=NULL;
long nram=0;
if(signe(a)==-1 && signe(b)==-1){glist_putstart(&S, mkoo());nram++;}
if(hilbertii(a, b, gen_2)==-1){glist_putstart(&S, gen_2);nram++;}
GEN ashift, bshift;
Z_pvalrem(a, gen_2, &ashift);
Z_pvalrem(b, gen_2, &bshift);
if(signe(ashift)==-1) ashift=negi(ashift);
if(signe(bshift)==-1) bshift=negi(bshift);//Now ashift, bshift are oddd and positive, so we factorize
GEN fact=Z_factor(ashift), p;
for(long i=1;i<lg(gel(fact, 1));i++){
p=gcoeff(fact, i, 1);
if(hilbertii(a, b, p)==-1){glist_putstart(&S, p);nram++;}//-1, so ramifies!
}
fact=Z_factor(bshift);
for(long i=1;i<lg(gel(fact, 1));i++){
p=gcoeff(fact, i, 1);
if(hilbertii(a, b, p)==-1){glist_putstart(&S, p);nram++;}//-1, so ramifies!
}
GEN pset=glist_togvec(S, nram, -1);
return gerepileupto(top, gen_sort_uniq(pset, NULL, &cmp_data));
}
//qa_ram_fromab with typechecking
GEN qa_ram_fromab_tc(GEN a, GEN b){
if(typ(a)!=t_INT || gequal0(a)) pari_err_TYPE("a, b must be non-zero integers", a);
if(typ(b)!=t_INT || gequal0(b)) pari_err_TYPE("a, b must be non-zero integers", b);
return qa_ram_fromab(a, b);
}
//CONJUGATION OF ELEMENTS IN A GIVEN ORDER
//Returns the basis for the 2-dimensional Z-module of the set of x for which x*e1=e2*x. Returns 0 if e1, e2 are not conjugate or rational.
GEN qa_conjbasis(GEN Q, GEN ord, GEN ordinv, GEN e1, GEN e2, int orient){
pari_sp top=avma;
if(!gequal(gel(e1, 1), gel(e2, 1))) return gen_0;//Traces must be equal.
if((gequal0(gel(e1, 2)) && gequal0(gel(e1, 3)) && gequal0(gel(e1, 4))) || (gequal0(gel(e2, 2)) && gequal0(gel(e2, 3)) && gequal0(gel(e2, 4)))) return gen_0;//Don't allow rational.
GEN a=qa_geta(Q), b=qa_getb(Q), mab=qa_getmab(Q);
//Solving v*e1=e2*v yields that v must be in the kernel of W, described as follows:
//[m,n,p,q]=e1, [m,r,x,y]=e2, W=[0,a*(n-r),b*(p-x),a*b*(y-q);n-r,0,-b*(y+q),b*(p+x);p-x,a*(q+y),0,-a*(n+r);y-q,-x-p,r+n,0]
GEN W=zeromatcopy(4, 4);
GEN nmr=gsub(gel(e1, 2), gel(e2, 2)), pmx=gsub(gel(e1, 3), gel(e2, 3)), ymq=gsub(gel(e2, 4), gel(e1, 4));
GEN npr=gadd(gel(e1, 2), gel(e2, 2)), ppx=gadd(gel(e1, 3), gel(e2, 3)), ypq=gadd(gel(e1, 4), gel(e2, 4));
gcoeff(W, 1, 2)=gmul(a, nmr);gcoeff(W, 1, 3)=gmul(b, pmx);gcoeff(W, 1, 4)=gneg(gmul(mab, ymq));
gcoeff(W, 2, 1)=nmr;gcoeff(W, 2, 3)=gneg(gmul(b, ypq));gcoeff(W, 2, 4)=gmul(b, ppx);
gcoeff(W, 3, 1)=pmx;gcoeff(W, 3, 2)=gmul(a, ypq);gcoeff(W, 3, 4)=gneg(gmul(a, npr));
gcoeff(W, 4, 1)=ymq;gcoeff(W, 4, 2)=gneg(ppx);gcoeff(W, 4, 3)=npr;
GEN ker=matkerint0(W, 0);//Integer kernel. It is of rank 0 (if not conjugate) and 2 if conjugate.
if(lg(ker)==1){avma=top;return gen_0;}//Done, no solution.
//Now ker is comprised of two primitive ZC's v1, v2. We need to solve Av1+Bv2=ker*[A,B] is in the order ord.
GEN ordker=shallowtrans(QM_mul(ordinv, ker));//Now [A, B]*ordker must be integral.
GEN H=QM_hnf(ordker);//The corret space is now H^(-1)*ordker, and we multuply by ord to get back the coefficients of i, j, k
GEN space=QM_mul(ord, shallowtrans(QM_mul(ginv(H), ordker)));
if(orient==0){
GEN ret=cgetg(3, t_VEC);
gel(ret, 1)=gtovec(gel(space, 1));
gel(ret, 2)=gtovec(gel(space, 2));
return gerepileupto(top, ret);
}
GEN v1=gtovec(gel(space, 1));
GEN v2=gtovec(gel(space, 2));
return gerepileupto(top, qa_conjbasis_orient(Q, ord, v1, v2, e2));
}
//qa_conjbasis with initializing ordinv and typecheck
GEN qa_conjbasis_tc(GEN Q, GEN ord, GEN e1, GEN e2, int orient){
pari_sp top=avma;
qa_check(Q);qa_eltcheck(e1);qa_eltcheck(e2);
GEN ordinv;
if(typ(ord)==t_VEC && lg(ord)==QAORDLEN){ordinv=qa_getordinv(ord);ord=qa_getord(ord);}
else{QM_check(ord);ordinv=ginv(ord);}
return gerepileupto(top, qa_conjbasis(Q, ord, ordinv, e1, e2, orient));
}
//Orients the basis [v1, v2] as either that or [v1, -v2], so that v2*conj(v1)-v1*conj(v) has a positive ratio to phi_2(sqrt(D_2)), i.e. e2-trace(e2)/2.
static GEN qa_conjbasis_orient(GEN Q, GEN ord, GEN v1, GEN v2, GEN e2){
pari_sp top=avma;
GEN v1conj=qa_conj(v1), v2conj=qa_conj(v2);
GEN x=gsub(qa_mul(Q, v2, v1conj), qa_mul(Q, v1, v2conj));//Needs to be a positive multiple of e2
GEN e2shift=zerovec(4);
for(int i=2;i<=4;i++) gel(e2shift, i)=gel(e2, i);