Line data Source code
1 : /* Copyright (C) 2000-2004 The PARI group.
2 :
3 : This file is part of the PARI/GP package.
4 :
5 : PARI/GP is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU General Public License as published by the Free Software
7 : Foundation; either version 2 of the License, or (at your option) any later
8 : version. It is distributed in the hope that it will be useful, but WITHOUT
9 : ANY WARRANTY WHATSOEVER.
10 :
11 : Check the License for details. You should have received a copy of it, along
12 : with the package; see the file 'COPYING'. If not, write to the Free Software
13 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
14 :
15 : /* Copyright (C) 2014 Denis Simon
16 : * Adapted from qfsolve.gp v. 09/01/2014
17 : * http://www.math.unicaen.fr/~simon/qfsolve.gp
18 : *
19 : * Author: Denis SIMON <simon@math.unicaen.fr> */
20 :
21 : #include "pari.h"
22 : #include "paripriv.h"
23 :
24 : #define DEBUGLEVEL DEBUGLEVEL_qfsolve
25 :
26 : /* LINEAR ALGEBRA */
27 : /* complete by 0s, assume l-1 <= n */
28 : static GEN
29 116174 : vecextend(GEN v, long n)
30 : {
31 116174 : long i, l = lg(v);
32 116174 : GEN w = cgetg(n+1, t_COL);
33 358754 : for (i = 1; i < l; i++) gel(w,i) = gel(v,i);
34 386255 : for ( ; i <=n; i++) gel(w,i) = gen_0;
35 116174 : return w;
36 : }
37 :
38 : /* Gives a unimodular matrix with the last column(s) equal to Mv.
39 : * Mv can be a column vector or a rectangular matrix.
40 : * redflag = 0 or 1. If redflag = 1, LLL-reduce the n-#v first columns. */
41 : static GEN
42 408745 : completebasis(GEN Mv, long redflag)
43 : {
44 : GEN U;
45 : long m, n;
46 :
47 408745 : if (typ(Mv) == t_COL) Mv = mkmat(Mv);
48 408745 : n = lg(Mv)-1;
49 408745 : m = nbrows(Mv); /* m x n */
50 408745 : if (m == n) return Mv;
51 408056 : (void)ZM_hnfall_i(shallowtrans(Mv), &U, 0);
52 408056 : U = ZM_inv(shallowtrans(U),NULL);
53 408056 : if (m==1 || !redflag) return U;
54 : /* LLL-reduce the m-n first columns */
55 122760 : return shallowconcat(ZM_lll(vecslice(U,1,m-n), 0.99, LLL_INPLACE),
56 122760 : vecslice(U, m-n+1,m));
57 : }
58 :
59 : /* Return U in GLn(Z) whose first d columns span Ker (M mod p). */
60 : static GEN
61 285697 : kermodp(GEN M, GEN p, long *d)
62 : {
63 : long j, l;
64 : GEN K, B, U;
65 :
66 285697 : K = FpM_center(FpM_ker(M, p), p, shifti(p,-1));
67 285697 : B = completebasis(K,0);
68 285697 : l = lg(M); U = cgetg(l, t_MAT);
69 1363343 : for (j = 1; j < l; j++) gel(U,j) = gel(B,l-j);
70 285697 : *d = lg(K)-1; return U;
71 : }
72 :
73 : /* INVARIANTS COMPUTATIONS */
74 :
75 : static GEN
76 33336 : principal_minor(GEN G, long i) { return matslice(G,1,i,1,i); }
77 : static GEN
78 700 : det_minors(GEN G)
79 : {
80 700 : long i, l = lg(G);
81 700 : GEN v = cgetg(l+1, t_VEC);
82 700 : gel(v,1) = gen_1;
83 3500 : for (i = 2; i <= l; i++) gel(v,i) = ZM_det(principal_minor(G,i-1));
84 700 : return v;
85 : }
86 :
87 : static GEN
88 7294 : hilberts(GEN a, GEN b, GEN P)
89 : {
90 7294 : long i, lP = lg(P);
91 7294 : GEN v = cgetg(lP, t_VECSMALL);
92 42273 : for (i = 1; i < lP; i++) v[i] = hilbertii(a, b, gel(P,i)) < 0;
93 7294 : return v;
94 : }
95 :
96 : /* 4 | disc(q); special case of gtomat */
97 : static GEN
98 1673 : qfbmat(GEN q)
99 : {
100 1673 : GEN a = gel(q,1), b = shifti(gel(q,2), -1), c = gel(q,3);
101 1673 : retmkmat22(a, b, b, c);
102 : }
103 : /* 2*qfbmat(q) */
104 : static GEN
105 21 : qfbmat2(GEN q)
106 : {
107 21 : GEN a = shifti(gel(q,1), 1), b = gel(q,2), c = shifti(gel(q,3), 1);
108 21 : retmkmat22(a, b, b, c);
109 : }
110 : /* Given a symmetric matrix G over Z, compute the Witt invariant of G at p
111 : * v = det_minors(G) [G diagonalized]; assume that none of the v[i] is 0. */
112 : static long
113 2121 : witt(GEN v, GEN p)
114 : {
115 2121 : long k = lg(v)-2, h = hilbertii(gel(v,k), gel(v,k+1), p);
116 8484 : for (k--; k >= 1; k--)
117 6363 : if (hilbertii(negi(gel(v,k)), gel(v,k+1),p) < 0) h = -h;
118 2121 : return h;
119 : }
120 :
121 : /* QUADRATIC FORM REDUCTION */
122 : /* private version of qfgaussred:
123 : * - early abort if k-th principal minor is singular, return stoi(k)
124 : * - else return a matrix whose upper triangular part is qfgaussred(a) */
125 : static GEN
126 44387 : partialgaussred(GEN a)
127 : {
128 44387 : long n = lg(a)-1, k;
129 44387 : a = RgM_shallowcopy(a);
130 148410 : for(k = 1; k < n; k++)
131 : {
132 113378 : GEN ak, p = gcoeff(a,k,k);
133 : long i, j;
134 113378 : if (isintzero(p)) return stoi(k);
135 104023 : ak = row(a, k);
136 386501 : for (i=k+1; i<=n; i++) gcoeff(a,k,i) = gdiv(gcoeff(a,k,i), p);
137 386501 : for (i=k+1; i<=n; i++)
138 : {
139 282478 : GEN c = gel(ak,i);
140 282478 : if (gequal0(c)) continue;
141 848303 : for (j=i; j<=n; j++)
142 619228 : gcoeff(a,i,j) = gsub(gcoeff(a,i,j), gmul(c,gcoeff(a,k,j)));
143 : }
144 : }
145 35032 : if (isintzero(gcoeff(a,n,n))) return stoi(n);
146 35010 : return a;
147 : }
148 :
149 : /* LLL-reduce a positive definite qf QD bounding the indefinite G, dim G > 1.
150 : * Then finishes by looking for trivial solution */
151 : static GEN qftriv(GEN G, GEN z, long base);
152 : static GEN
153 44387 : qflllgram_indef(GEN G, long base, int *fail)
154 : {
155 : GEN M, R, g, DM, S, dR;
156 44387 : long i, j, n = lg(G)-1;
157 :
158 44387 : *fail = 0;
159 44387 : R = partialgaussred(G);
160 44387 : if (typ(R) == t_INT) return qftriv(G, R, base);
161 35010 : R = Q_remove_denom(R, &dR); /* avoid rational arithmetic */
162 35010 : M = zeromatcopy(n,n);
163 35010 : DM = zeromatcopy(n,n);
164 168652 : for (i = 1; i <= n; i++)
165 : {
166 133642 : GEN d = absi_shallow(gcoeff(R,i,i));
167 133642 : if (dR) {
168 109781 : gcoeff(M,i,i) = dR;
169 109781 : gcoeff(DM,i,i) = mulii(d,dR);
170 : } else {
171 23861 : gcoeff(M,i,i) = gen_1;
172 23861 : gcoeff(DM,i,i) = d;
173 : }
174 393239 : for (j = i+1; j <= n; j++)
175 : {
176 259597 : gcoeff(M,i,j) = gcoeff(R,i,j);
177 259597 : gcoeff(DM,i,j) = mulii(d, gcoeff(R,i,j));
178 : }
179 : }
180 : /* G = M~*D*M, D diagonal, DM=|D|*M, g = M~*|D|*M */
181 35010 : g = ZM_transmultosym(M,DM);
182 35010 : S = lllgramint(Q_primpart(g));
183 35010 : R = qftriv(qf_apply_ZM(G,S), NULL, base);
184 35010 : switch(typ(R))
185 : {
186 6215 : case t_COL: return ZM_ZC_mul(S,R);
187 21161 : case t_MAT: *fail = 1; return mkvec2(R, S);
188 7634 : default:
189 7634 : gel(R,2) = ZM_mul(S, gel(R,2));
190 7634 : return R;
191 : }
192 : }
193 :
194 : /* G symmetric, i < j, let E = E_{i,j}(a), G <- E~*G*E, U <- U*E.
195 : * Everybody integral */
196 : static void
197 4131 : qf_apply_transvect_Z(GEN G, GEN U, long i, long j, GEN a)
198 : {
199 4131 : long k, n = lg(G)-1;
200 4131 : gel(G, j) = ZC_lincomb(gen_1, a, gel(G,j), gel(G,i));
201 22600 : for (k = 1; k < n; k++) gcoeff(G, j, k) = gcoeff(G, k, j);
202 4131 : gcoeff(G,j,j) = addmulii(gcoeff(G,j,j), a,
203 4131 : addmulii(gcoeff(G,i,j), a,gcoeff(G,i,i)));
204 4131 : gel(U, j) = ZC_lincomb(gen_1, a, gel(U,j), gel(U,i));
205 4131 : }
206 :
207 : /* LLL reduction of the quadratic form G (Gram matrix)
208 : * where we go on, even if an isotropic vector is found. */
209 : static GEN
210 11102 : qflllgram_indefgoon(GEN G)
211 : {
212 : GEN red, U, A, U1,U2,U3,U5,U6, V, B, G2,G3,G4,G5, G6, a, g;
213 11102 : long i, j, n = lg(G)-1;
214 : int fail;
215 :
216 11102 : red = qflllgram_indef(G,1, &fail);
217 11102 : if (fail) return red; /*no isotropic vector found: nothing to do*/
218 : /* otherwise a solution is found: */
219 11088 : U1 = gel(red,2);
220 11088 : G2 = gel(red,1); /* G2[1,1] = 0 */
221 11088 : U2 = gel(ZV_extgcd(row(G2,1)), 2);
222 11088 : G3 = qf_apply_ZM(G2,U2);
223 11088 : U = ZM_mul(U1,U2); /* qf_apply(G,U) = G3 */
224 : /* G3[1,] = [0,...,0,g], g^2 | det G */
225 11088 : g = gcoeff(G3,1,n);
226 11088 : a = diviiround(negi(gcoeff(G3,n,n)), shifti(g,1));
227 11088 : if (signe(a)) qf_apply_transvect_Z(G3,U,1,n,a);
228 : /* G3[n,n] reduced mod 2g */
229 11088 : if (n == 2) return mkvec2(G3,U);
230 10423 : V = rowpermute(vecslice(G3, 2,n-1), mkvecsmall2(1,n));
231 10423 : A = mkmat22(gcoeff(G3,1,1),gcoeff(G3,1,n),gcoeff(G3,1,n),gcoeff(G3,2,2));
232 10423 : B = ground(RgM_neg(QM_mul(QM_inv(A), V)));
233 10423 : U3 = matid(n);
234 51884 : for (j = 2; j < n; j++)
235 : {
236 41461 : gcoeff(U3,1,j) = gcoeff(B,1,j-1);
237 41461 : gcoeff(U3,n,j) = gcoeff(B,2,j-1);
238 : }
239 10423 : G4 = qf_apply_ZM(G3,U3); /* the last column of G4 is reduced */
240 10423 : U = ZM_mul(U,U3);
241 10423 : if (n == 3) return mkvec2(G4,U);
242 :
243 8148 : red = qflllgram_indefgoon(matslice(G4,2,n-1,2,n-1));
244 8148 : if (typ(red) == t_MAT) return mkvec2(G4,U);
245 : /* Let U5:=matconcat(diagonal[1,red[2],1])
246 : * return [qf_apply_ZM(G5, U5), U*U5] */
247 8148 : G5 = gel(red,1);
248 8148 : U5 = gel(red,2);
249 8148 : G6 = cgetg(n+1,t_MAT);
250 8148 : gel(G6,1) = gel(G4,1);
251 8148 : gel(G6,n) = gel(G4,n);
252 47334 : for (j=2; j<n; j++)
253 : {
254 39186 : gel(G6,j) = cgetg(n+1,t_COL);
255 39186 : gcoeff(G6,1,j) = gcoeff(G4,j,1);
256 39186 : gcoeff(G6,n,j) = gcoeff(G4,j,n);
257 259112 : for (i=2; i<n; i++) gcoeff(G6,i,j) = gcoeff(G5,i-1,j-1);
258 : }
259 8148 : U6 = mkvec3(mkmat(gel(U,1)), ZM_mul(vecslice(U,2,n-1),U5), mkmat(gel(U,n)));
260 8148 : return mkvec2(G6, shallowconcat1(U6));
261 : }
262 :
263 : /* qf_apply_ZM(G,H), where H = matrix of \tau_{i,j}, i != j */
264 : static GEN
265 13328 : qf_apply_tau(GEN G, long i, long j)
266 : {
267 13328 : long l = lg(G), k;
268 13328 : G = RgM_shallowcopy(G);
269 13328 : swap(gel(G,i), gel(G,j));
270 80509 : for (k = 1; k < l; k++) swap(gcoeff(G,i,k), gcoeff(G,j,k));
271 13328 : return G;
272 : }
273 :
274 : /* LLL reduction of the quadratic form G (Gram matrix)
275 : * in dim 3 only, with detG = -1 and sign(G) = [2,1]; */
276 : static GEN
277 2282 : qflllgram_indefgoon2(GEN G)
278 : {
279 : GEN red, G2, a, b, c, d, e, f, u, v, r, r3, U2, G3;
280 : int fail;
281 :
282 2282 : red = qflllgram_indef(G,1,&fail); /* always find an isotropic vector. */
283 2282 : G2 = qf_apply_tau(gel(red,1),1,3); /* G2[3,3] = 0 */
284 2282 : r = row(gel(red,2), 3);
285 2282 : swap(gel(r,1), gel(r,3)); /* apply tau_{1,3} */
286 2282 : a = gcoeff(G2,3,1);
287 2282 : b = gcoeff(G2,3,2);
288 2282 : d = bezout(a,b, &u,&v);
289 2282 : if (!equali1(d))
290 : {
291 0 : a = diviiexact(a,d);
292 0 : b = diviiexact(b,d);
293 : }
294 : /* for U2 = [-u,-b,0;-v,a,0;0,0,1]
295 : * G3 = qf_apply_ZM(G2,U2) has known last row (-d, 0, 0),
296 : * so apply to principal_minor(G3,2), instead */
297 2282 : U2 = mkmat22(negi(u),negi(b),negi(v),a);
298 2282 : G3 = qf_apply_ZM(principal_minor(G2,2),U2);
299 2282 : r3 = gel(r,3);
300 2282 : r = ZV_ZM_mul(mkvec2(gel(r,1),gel(r,2)),U2);
301 :
302 2282 : a = gcoeff(G3,1,1);
303 2282 : b = gcoeff(G3,1,2);
304 2282 : c = negi(d); /* G3[1,3] */
305 2282 : d = gcoeff(G3,2,2);
306 2282 : if (mpodd(a))
307 : {
308 1239 : e = addii(b,d);
309 1239 : a = addii(a, addii(b,e));
310 1239 : e = diviiround(negi(e),c);
311 1239 : f = diviiround(negi(a), shifti(c,1));
312 1239 : a = addmulii(addii(gel(r,1),gel(r,2)), f,r3);
313 : }
314 : else
315 : {
316 1043 : e = diviiround(negi(b),c);
317 1043 : f = diviiround(negi(shifti(a,-1)), c);
318 1043 : a = addmulii(gel(r,1), f, r3);
319 : }
320 2282 : b = addmulii(gel(r,2), e, r3);
321 2282 : return mkvec3(a,b, r3);
322 : }
323 :
324 : /* QUADRATIC FORM MINIMIZATION */
325 : /* G symmetric, return ZM_Z_divexact(G,d) */
326 : static GEN
327 20427 : ZsymM_Z_divexact(GEN G, GEN d)
328 : {
329 20427 : long i,j,l = lg(G);
330 20427 : GEN H = cgetg(l, t_MAT);
331 79460 : for (j = 1; j < l; j++)
332 : {
333 59033 : GEN c = cgetg(l, t_COL), b = gel(G,j);
334 115825 : for (i=1; i < j; i++) gcoeff(H,j,i) = gel(c,i) = diviiexact(gel(b,i),d);
335 59033 : gel(c,j) = diviiexact(gel(b,j),d);
336 59033 : gel(H,j) = c;
337 : }
338 20427 : return H;
339 : }
340 : /* by blocks, in place: G[1,1] /= d, G[2,2] *= d */
341 : static void
342 92340 : ZsymM_Z_divexact_partial(GEN G, long n, GEN d)
343 : {
344 92340 : long i, j, l = lg(G);
345 185432 : for(j = 1; j <= n; j++)
346 : {
347 93844 : for (i = 1; i < j; i++)
348 752 : gcoeff(G,i,j) = gcoeff(G,j,i) = diviiexact(gcoeff(G,i,j), d);
349 93092 : gcoeff(G,j,j) = diviiexact(gcoeff(G,j,j), d);
350 : }
351 309923 : for (; j < l; j++)
352 : {
353 454829 : for (i = n+1; i < j; i++)
354 237246 : gcoeff(G,i,j) = gcoeff(G,j,i) = mulii(gcoeff(G,i,j), d);
355 217583 : gcoeff(G,j,j) = mulii(gcoeff(G,j,j), d);
356 : }
357 92340 : }
358 :
359 : /* write symmetric G as [A,B;B~,C], A dxd, C (n-d)x(n-d) */
360 : static void
361 2374 : blocks4(GEN G, long d, long n, GEN *A, GEN *B, GEN *C)
362 : {
363 2374 : GEN G2 = vecslice(G,d+1,n);
364 2374 : *A = principal_minor(G, d);
365 2374 : *B = rowslice(G2, 1, d);
366 2374 : *C = rowslice(G2, d+1, n);
367 2374 : }
368 : static GEN qfsolvemodp(GEN G, GEN p);
369 : static void
370 72816 : update_fm(GEN f, GEN a, long i)
371 : {
372 72816 : if (!odd(i))
373 51251 : gel(f,1) = a;
374 : else
375 : {
376 21565 : long v = vals(i+1), k;
377 21565 : GEN b = gel(f,1), u = QM_mul(b, a);
378 21565 : gel(f,1) = gen_0;
379 21565 : if (v+2 >= lg(f)) pari_err_BUG("update_fm");
380 26336 : for (k = 1; k < v; k++)
381 : {
382 4771 : u = QM_mul(gel(f, k+1), u);
383 4771 : gel(f,k+1) = gen_0; /* for gerepileall */
384 : }
385 21565 : gel(f,v+1) = u;
386 : }
387 72816 : }
388 : static GEN
389 41235 : prod_fm(GEN f, long i)
390 : {
391 41235 : long v = vals(i), k;
392 41235 : GEN u = gel(f, ++v);
393 47790 : for (i >>= v, k = v+1; i; i >>= 1, k++)
394 6555 : if (odd(i)) u = QM_mul(gel(f,k), u);
395 41235 : return u;
396 : }
397 :
398 : /* Minimization of the quadratic form G, deg G != 0, dim n >= 2
399 : * G symmetric integral
400 : * Returns [G',U,factd] with U in GLn(Q) such that G'=U~*G*U*constant
401 : * is integral and has minimal determinant.
402 : * In dimension 3 or 4, may return a prime p if the reduction at p is
403 : * impossible because of local nonsolvability.
404 : * P,E = factor(+/- det(G)), "prime" -1 is ignored. Destroy E. */
405 : static GEN
406 16002 : qfminimize_fact(GEN G, GEN P, GEN E, long loc)
407 : {
408 16002 : GEN U = NULL, Ker = NULL, faE, faP;
409 16002 : long n = lg(G)-1, lP = lg(P), i;
410 16002 : faP = vectrunc_init(lP);
411 16002 : faE = vecsmalltrunc_init(lP);
412 155473 : for (i = 1; i < lP; i++)
413 : {
414 139954 : GEN p = gel(P,i);
415 139954 : long vp = E[i], wp = vp;
416 139954 : if (!vp) continue;
417 139674 : if (DEBUGLEVEL >= 3) err_printf("qfminimize: for %Ps^%ld:", p,vp);
418 345267 : while (vp)
419 : {
420 247693 : long idx = 0, dimKer = 0; /* -Wall */
421 247693 : GEN d, sol = NULL, FU = zerovec(2*expu(vp)+100);
422 247693 : pari_sp av = avma;
423 320509 : while (vp) /* loop until vp <= n */
424 : {
425 290883 : if (DEBUGLEVEL>=3 && vp <= wp)
426 0 : { err_printf(" %ld%%", (E[i]-vp)*100/E[i]); wp -= E[i]/100; }
427 : /* The case vp = 1 can be minimized only if n is odd. */
428 290883 : if (vp == 1 && !odd(n)) break;
429 283323 : Ker = kermodp(G,p, &dimKer); /* dimKer <= vp */
430 283323 : if (DEBUGLEVEL >= 4) err_printf(" dimKer = %ld\n",dimKer);
431 283323 : if (dimKer == n) break;
432 283323 : G = qf_apply_ZM(G, Ker);
433 : /* 1st case: dimKer < vp */
434 : /* then the kernel mod p contains a kernel mod p^2 */
435 283323 : if (dimKer >= vp) break;
436 :
437 72816 : if (DEBUGLEVEL >= 4) err_printf(" case 1: dimker < vp\n");
438 72816 : if (dimKer == 1)
439 : {
440 : long j;
441 70442 : gcoeff(G,1,1) = diviiexact(gcoeff(G,1,1), sqri(p));
442 215806 : for (j = 2; j <= n; j++)
443 145364 : gcoeff(G,1,j) = gcoeff(G,j,1) = diviiexact(gcoeff(G,j,1), p);
444 70442 : gel(Ker,1) = RgC_Rg_div(gel(Ker,1), p);
445 70442 : vp -= 2;
446 : }
447 : else
448 : {
449 : GEN A, B, C, K2;
450 : long j, dimKer2;
451 2374 : blocks4(G, dimKer,n, &A,&B,&C); A = ZsymM_Z_divexact(A, p);
452 2374 : K2 = kermodp(A, p, &dimKer2);
453 : /* Write G = [pA,B;B~,C] and apply [K2/p,0;0,Id] by blocks */
454 2374 : A = qf_apply_ZM(A,K2); ZsymM_Z_divexact_partial(A, dimKer2, p);
455 2374 : B = ZM_transmul(B,K2);
456 5465 : for (j = 1; j <= dimKer2; j++) gel(B,j) = ZC_Z_divexact(gel(B,j), p);
457 2374 : G = shallowmatconcat(mkmat22(A,shallowtrans(B),B,C));
458 : /* Ker *= [K2,0;0,Id] */
459 2374 : B = ZM_mul(vecslice(Ker,1,dimKer),K2);
460 5465 : for (j = 1; j <= dimKer2; j++) gel(B,j) = RgC_Rg_div(gel(B,j), p);
461 2374 : Ker = shallowconcat(B, vecslice(Ker,dimKer+1,n));
462 2374 : vp -= 2*dimKer2;
463 : }
464 72816 : update_fm(FU, Ker, idx++);
465 72816 : if (gc_needed(av, 1))
466 : {
467 0 : if (DEBUGMEM >= 2) pari_warn(warnmem,"qfminimize");
468 0 : gerepileall(av, 2, &G, &FU);
469 : }
470 : }
471 247693 : if (idx)
472 : {
473 41235 : GEN PU = prod_fm(FU, idx);
474 41235 : U = U ? QM_mul(U, PU): PU;
475 : }
476 259684 : if (vp == 0) break;
477 218067 : if (vp == 1 && !odd(n))
478 : {
479 7560 : vectrunc_append(faP, p);
480 7560 : vecsmalltrunc_append(faE, 1);
481 7560 : break;
482 : }
483 210507 : if (dimKer == n)
484 : { /* trivial case: dimKer = n */
485 0 : if (DEBUGLEVEL >= 4) err_printf(" case 0: dimKer = n\n");
486 0 : G = ZsymM_Z_divexact(G, p);
487 205593 : vp -= n; continue;
488 : }
489 210507 : U = U ? QM_mul(U, Ker): Ker;
490 : /* vp = dimKer
491 : * 2nd case: kernel has dim >= 2 and contains an elt of norm 0 mod p^2,
492 : * find it */
493 210507 : if (dimKer > 2) {
494 18053 : if (DEBUGLEVEL >= 4) err_printf(" case 2.1\n");
495 18053 : dimKer = 3;
496 18053 : sol = qfsolvemodp(ZsymM_Z_divexact(principal_minor(G,3),p), p);
497 18053 : sol = FpC_red(sol, p);
498 : }
499 192454 : else if (dimKer == 2)
500 : {
501 97623 : GEN a = modii(diviiexact(gcoeff(G,1,1),p), p);
502 97623 : GEN b = modii(diviiexact(gcoeff(G,1,2),p), p);
503 97623 : GEN c = modii(diviiexact(gcoeff(G,2,2),p), p);
504 97623 : GEN D = modii(subii(sqri(b), mulii(a,c)), p);
505 97623 : if (kronecker(D,p) >= 0)
506 : {
507 97574 : if (DEBUGLEVEL >= 4) err_printf(" case 2.2\n");
508 97574 : sol = signe(a)? mkcol2(Fp_sub(Fp_sqrt(D,p), b, p), a): vec_ei(2,1);
509 : }
510 : }
511 210507 : if (sol)
512 : {
513 : long j;
514 115627 : sol = FpC_center(sol, p, shifti(p,-1));
515 115627 : sol = Q_primpart(sol);
516 115627 : if (DEBUGLEVEL >= 4) err_printf(" sol = %Ps\n", sol);
517 115627 : Ker = completebasis(vecextend(sol,n), 1);
518 115627 : G = qf_apply_ZM(G, Ker);
519 509834 : for (j = 1; j < n; j++)
520 394207 : gcoeff(G,n,j) = gcoeff(G,j,n) = diviiexact(gcoeff(G,j,n), p);
521 115627 : gcoeff(G,n,n) = diviiexact(gcoeff(G,n,n), sqri(p));
522 115627 : U = QM_mul(U,Ker); gel(U,n) = RgC_Rg_div(gel(U,n), p);
523 115627 : vp -= 2; continue;
524 : }
525 : /* Now 0 < vp = dimKer < 3 and kernel contains no vector with norm p^2 */
526 : /* exchanging kernel and image makes minimization easier ? */
527 94880 : d = ZM_det(G); if (odd((n-3) / 2)) d = negi(d);
528 94880 : if ((vp==1 && kronecker(gmod(gdiv(negi(d), gcoeff(G,1,1)),p), p) >= 0)
529 4949 : || (vp==2 && odd(n) && n >= 5)
530 4935 : || (vp==2 && !odd(n) && kronecker(modii(diviiexact(d,sqri(p)), p),p) < 0))
531 : {
532 : long j;
533 89966 : if (DEBUGLEVEL >= 4) err_printf(" case 3\n");
534 89966 : ZsymM_Z_divexact_partial(G, dimKer, p);
535 305766 : for (j = dimKer+1; j <= n; j++) gel(U,j) = RgC_Rg_mul(gel(U,j), p);
536 89966 : vp -= 2*dimKer-n; continue;
537 : }
538 :
539 : /* Minimization was not possible so far. */
540 : /* If n == 3 or 4, this proves the local nonsolubility at p. */
541 4914 : if (loc && (n == 3 || n == 4))
542 : {
543 483 : if (DEBUGLEVEL >= 1) err_printf(" no local solution at %Ps\n",p);
544 483 : return p;
545 : }
546 4431 : vectrunc_append(faP, p);
547 4431 : vecsmalltrunc_append(faE, vp);
548 4431 : break;
549 : }
550 139191 : if (DEBUGLEVEL >= 3) err_printf("\n");
551 : }
552 15519 : if (!U) U = matid(n);
553 : else
554 : { /* apply LLL to avoid coefficient explosion */
555 14455 : GEN u = lllint(Q_primpart(U));
556 14455 : G = qf_apply_ZM(G, u);
557 14455 : U = QM_mul(U, u);
558 : }
559 15519 : return mkvec4(G, U, faP, faE);
560 : }
561 :
562 : /* assume G square integral */
563 : static void
564 19649 : check_symmetric(GEN G)
565 : {
566 19649 : long i,j, l = lg(G);
567 89117 : for (i = 1; i < l; i++)
568 174251 : for(j = 1; j < i; j++)
569 104783 : if (!equalii(gcoeff(G,i,j), gcoeff(G,j,i)))
570 7 : pari_err_TYPE("qfsolve [not symmetric]",G);
571 19642 : }
572 : /* assume G symmetric and integral */
573 : static void
574 14 : symmetric_non0_coeff(GEN G, long *pi, long *pj)
575 : {
576 14 : long i, j, l = lg(G);
577 14 : *pi = *pj = 0;
578 14 : for (i = 1; i < l; i++)
579 14 : for (j = 1; j <= i; j++)
580 14 : if (signe(gcoeff(G,i,j))) { *pi = i; *pj = j; return; }
581 : }
582 :
583 : GEN
584 14 : qfminimize(GEN G)
585 : {
586 14 : pari_sp av = avma;
587 : GEN c, d, F, H, U;
588 14 : long i, j, n = lg(G)-1;
589 14 : if (typ(G) != t_MAT) pari_err_TYPE("qfminimize", G);
590 14 : if (n == 0) pari_err_DOMAIN("qfminimize", "dimension" , "=", gen_0, G);
591 14 : if (n != nbrows(G)) pari_err_DIM("qfminimize");
592 14 : G = Q_primpart(G); RgM_check_ZM(G, "qfminimize");
593 14 : check_symmetric(G);
594 14 : d = ZM_det(G);
595 14 : if (!signe(d)) pari_err_DOMAIN("qfminimize", "det" , "=", gen_0, gen_0);
596 14 : F = absZ_factor(d);
597 14 : H = qfminimize_fact(G, gel(F,1), ZV_to_zv(gel(F,2)), 0);
598 14 : symmetric_non0_coeff(G, &i, &j);
599 14 : U = gel(H,2); H = gel(H,1);
600 14 : c = gdiv(gcoeff(H,i,j), RgV_dotproduct(gel(U,i), RgM_RgC_mul(G, gel(U,j))));
601 14 : return gerepilecopy(av, mkvec3(H, U, c));
602 : }
603 :
604 : /* CLASS GROUP COMPUTATIONS */
605 :
606 : /* Compute the square root of the quadratic form q of discriminant D = 4 * md
607 : * Not fully implemented; only works for D squarefree except at 2, where the
608 : * valuation is 2 or 3. Finally, [P,E] = factor(2*abs(D)) if valuation is 3 and
609 : * factor(abs(D / 4)) otherwise */
610 : static GEN
611 2282 : qfbsqrt(GEN D, GEN md, GEN q, GEN P)
612 : {
613 2282 : GEN a = gel(q,1), b = shifti(gel(q,2),-1), c = gel(q,3), B = negi(b);
614 2282 : GEN m, n, Q, M, N, d = negi(md); /* ac - b^2 */
615 2282 : long i, lP = lg(P);
616 :
617 : /* 1) solve m^2 = a, m*n = -b, n^2 = c in Z/dZ => q(n,m) = 0 mod d */
618 2282 : M = cgetg(lP, t_VEC);
619 2282 : N = cgetg(lP, t_VEC);
620 9737 : for (i = 1; i < lg(P); i++)
621 : {
622 7455 : GEN p = gel(P,i);
623 7455 : if (dvdii(a,p)) { n = Fp_sqrt(c, p); m = Fp_div(B, n, p); }
624 5642 : else { m = Fp_sqrt(a, p); n = Fp_div(B, m, p); }
625 7455 : gel(M, i) = m;
626 7455 : gel(N, i) = n;
627 : }
628 2282 : m = ZV_chinese_center(M, P, NULL);
629 2282 : n = ZV_chinese_center(N, P, NULL);
630 :
631 : /* 2) build Q, with det=-1 such that Q(x,y,0) = G(x,y) */
632 2282 : N = diviiexact(addii(mulii(a,n), mulii(b,m)), d);
633 2282 : M = diviiexact(addii(mulii(b,n), mulii(c,m)), d);
634 2282 : Q = diviiexact(subiu(addii(mulii(m,M), mulii(n,N)), 1), d); /*(q(n,m)-d)/d^2 */
635 2282 : Q = mkmat3(mkcol3(a,b,N), mkcol3(b,c,M), mkcol3(N,M,Q)); /* det = -1 */
636 :
637 : /* 3) reduce Q to [0,0,-1; 0,1,0; -1,0,0] */
638 2282 : M = qflllgram_indefgoon2(Q);
639 2282 : if (signe(gel(M,1)) < 0) M = ZC_neg(M);
640 2282 : a = gel(M,1);
641 2282 : b = gel(M,2);
642 2282 : c = gel(M,3);
643 2282 : if (!mpodd(a)) { swap(a, c); togglesign_safe(&b); }
644 2282 : return mkqfb(a, shifti(b,1), shifti(c,1), D);
645 : }
646 :
647 : /* \prod gen[i]^e[i] as a Qfb, e in {0,1}^n nonzero */
648 : static GEN
649 3976 : qfb_factorback(GEN gen, GEN e, GEN isqrtD)
650 : {
651 3976 : GEN q = NULL;
652 3976 : long j, l = lg(gen), n = 0;
653 13524 : for (j = 1; j < l; j++)
654 9548 : if (e[j]) { n++; q = q? qfbcompraw(q, gel(gen,j)): gel(gen,j); }
655 3976 : return (n <= 1)? q: qfbred0(q, 0, isqrtD, NULL);
656 : }
657 :
658 : /* unit form discriminant 4d */
659 : static GEN
660 994 : id(GEN d)
661 994 : { retmkmat22(gen_1, gen_0, gen_0, negi(d)); }
662 :
663 : /* Shanks/Bosma-Stevenhagen algorithm to compute the 2-Sylow of the class
664 : * group of discriminant D. Only works for D = fundamental discriminant.
665 : * When D = 1(4), work with 4D.
666 : * P2D,E2D = factor(abs(2*D))
667 : * Pm2D = factor(-abs(2*D))[,1].
668 : * Return a form having Witt invariants W at Pm2D */
669 : static GEN
670 2688 : quadclass2(GEN D, GEN P2D, GEN E2D, GEN Pm2D, GEN W, int n_is_4)
671 : {
672 2688 : GEN U2 = NULL, gen, Wgen, isqrtD, d;
673 : long i, r, m;
674 2688 : int splice2 = mpodd(D);
675 :
676 2688 : if (!splice2) d = shifti(D,-2); else { d = D; D = shifti(D,2); }
677 : /* D = 4d */
678 2688 : if (zv_equal0(W)) return id(d);
679 1813 : r = lg(Pm2D) - 4; /* >= 0 since W != 0 */
680 1813 : m = (signe(D) > 0)? r+1: r;
681 1813 : if (n_is_4)
682 : { /* n = 4: look among forms of type q or 2*q, since Q can be imprimitive */
683 539 : U2 = hilberts(gen_2, d, Pm2D);
684 539 : if (zv_equal(U2,W)) return gmul2n(id(d),1);
685 : }
686 :
687 1694 : gen = cgetg(m+2, t_VEC);
688 4578 : for (i = 1; i <= m; i++)
689 : { /* no need to look at P2D[1]=2*/
690 2884 : GEN q = powiu(gel(P2D,i+1), E2D[i+1]);
691 2884 : gel(gen,i) = mkqfb(q, gen_0, negi(diviiexact(d,q)), D);
692 : }
693 1694 : if (!mpodd(d))
694 : {
695 1470 : gel(gen, ++m) = mkqfb(gen_2, gen_0, negi(shifti(d,-1)), D);
696 1470 : r++;
697 : }
698 224 : else if (Mod4(d) != 1)
699 : {
700 119 : gel(gen, ++m) = mkqfb(gen_2, gen_2, shifti(subsi(1,d),-1), D);
701 119 : r++;
702 : }
703 105 : else setlg(gen, m+1);
704 1694 : if (!r) return id(d);
705 : /* remove 2^3; leave alone 2^4 */
706 1694 : if (splice2) P2D = vecsplice(P2D, 1);
707 1694 : Wgen = cgetg(m+1, t_MAT);
708 6167 : for (i = 1; i <= m; i++) gel(Wgen,i) = hilberts(gmael(gen,i,1), d, Pm2D);
709 1694 : isqrtD = signe(D) > 0? sqrti(D) : NULL;
710 : for(;;)
711 2072 : {
712 3766 : GEN Wgen2, gen2, Ker, indexim = gel(Flm_indexrank(Wgen,2), 2);
713 : long dKer;
714 3766 : if (lg(indexim)-1 >= r)
715 : {
716 1694 : GEN W2 = Wgen, V;
717 1694 : if (lg(indexim) < lg(Wgen)) W2 = vecpermute(Wgen,indexim);
718 1694 : if (U2) W2 = vec_append(W2,U2);
719 1694 : V = Flm_Flc_invimage(W2, W, 2);
720 1694 : if (V)
721 : {
722 1694 : GEN Q = qfb_factorback(vecpermute(gen,indexim), V, isqrtD);
723 1694 : return (U2 && V[lg(V)-1])? qfbmat2(Q): qfbmat(Q);
724 : }
725 : }
726 2072 : Ker = Flm_ker(Wgen,2); dKer = lg(Ker)-1;
727 2072 : gen2 = cgetg(m+1, t_VEC);
728 2072 : Wgen2 = cgetg(m+1, t_MAT);
729 4354 : for (i = 1; i <= dKer; i++)
730 : {
731 2282 : GEN q = qfb_factorback(gen, gel(Ker,i), isqrtD);
732 2282 : gel(gen2,i) = q = qfbsqrt(D, d, q, P2D);
733 2282 : gel(Wgen2,i) = hilberts(gel(q,1), d, Pm2D);
734 : }
735 4452 : for (; i <=m; i++)
736 : {
737 2380 : long j = indexim[i-dKer];
738 2380 : gel(gen2,i) = gel(gen,j);
739 2380 : gel(Wgen2,i) = gel(Wgen,j);
740 : }
741 2072 : gen = gen2; Wgen = Wgen2;
742 : }
743 : }
744 :
745 : /* QUADRATIC EQUATIONS */
746 : /* is x*y = -1 ? */
747 : static int
748 19357 : both_pm1(GEN x, GEN y)
749 19357 : { return is_pm1(x) && is_pm1(y) && signe(x) == -signe(y); }
750 :
751 : /* Try to solve G = 0 with small coefficients. This is proved to work if
752 : * - det(G) = 1, dim <= 6 and G is LLL reduced
753 : * Returns G if no solution is found.
754 : * Exit with a norm 0 vector if one such is found.
755 : * If base == 1 and norm 0 is obtained, returns [H~*G*H,H] where
756 : * the 1st column of H is a norm 0 vector */
757 : static GEN
758 44387 : qftriv(GEN G, GEN R, long base)
759 : {
760 44387 : long n = lg(G)-1, i;
761 : GEN s, H;
762 :
763 : /* case 1: A basis vector is isotropic */
764 141548 : for (i = 1; i <= n; i++)
765 116020 : if (!signe(gcoeff(G,i,i)))
766 : {
767 18859 : if (!base) return col_ei(n,i);
768 11046 : H = matid(n); swap(gel(H,1), gel(H,i));
769 11046 : return mkvec2(qf_apply_tau(G,1,i),H);
770 : }
771 : /* case 2: G has a block +- [1,0;0,-1] on the diagonal */
772 84745 : for (i = 2; i <= n; i++)
773 63037 : if (!signe(gcoeff(G,i-1,i)) && both_pm1(gcoeff(G,i-1,i-1),gcoeff(G,i,i)))
774 : {
775 3820 : s = col_ei(n,i); gel(s,i-1) = gen_m1;
776 3820 : if (!base) return s;
777 2974 : H = matid(n); gel(H,i) = gel(H,1); gel(H,1) = s;
778 2974 : return mkvec2(qf_apply_ZM(G,H),H);
779 : }
780 21708 : if (!R) return G; /* fail */
781 : /* case 3: a principal minor is 0 */
782 547 : s = ZM_ker(principal_minor(G, itos(R)));
783 547 : s = vecextend(Q_primpart(gel(s,1)), n);
784 547 : if (!base) return s;
785 288 : H = completebasis(s, 0);
786 288 : gel(H,n) = ZC_neg(gel(H,1)); gel(H,1) = s;
787 288 : return mkvec2(qf_apply_ZM(G,H),H);
788 : }
789 :
790 : /* p a prime number, G 3x3 symmetric. Finds X!=0 such that X^t G X = 0 mod p.
791 : * Allow returning a shorter X: to be completed with 0s. */
792 : static GEN
793 18053 : qfsolvemodp(GEN G, GEN p)
794 : {
795 : GEN a,b,c,d,e,f, v1,v2,v3,v4,v5, x1,x2,x3,N1,N2,N3,s,r;
796 :
797 : /* principal_minor(G,3) = [a,b,d; b,c,e; d,e,f] */
798 18053 : a = modii(gcoeff(G,1,1), p);
799 18053 : if (!signe(a)) return mkcol(gen_1);
800 15273 : v1 = a;
801 15273 : b = modii(gcoeff(G,1,2), p);
802 15273 : c = modii(gcoeff(G,2,2), p);
803 15273 : v2 = modii(subii(mulii(a,c), sqri(b)), p);
804 15273 : if (!signe(v2)) return mkcol2(Fp_neg(b,p), a);
805 12277 : d = modii(gcoeff(G,1,3), p);
806 12277 : e = modii(gcoeff(G,2,3), p);
807 12277 : f = modii(gcoeff(G,3,3), p);
808 12277 : v4 = modii(subii(mulii(c,d), mulii(e,b)), p);
809 12277 : v5 = modii(subii(mulii(a,e), mulii(d,b)), p);
810 12277 : v3 = subii(mulii(v2,f), addii(mulii(v4,d), mulii(v5,e))); /* det(G) */
811 12277 : v3 = modii(v3, p);
812 12277 : N1 = Fp_neg(v2, p);
813 12277 : x3 = mkcol3(v4, v5, N1);
814 12277 : if (!signe(v3)) return x3;
815 :
816 : /* now, solve in dimension 3... reduction to the diagonal case: */
817 10619 : x1 = mkcol3(gen_1, gen_0, gen_0);
818 10619 : x2 = mkcol3(negi(b), a, gen_0);
819 10619 : if (kronecker(N1,p) == 1) return ZC_lincomb(Fp_sqrt(N1,p),gen_1,x1,x2);
820 4256 : N2 = Fp_div(Fp_neg(v3,p), v1, p);
821 4256 : if (kronecker(N2,p) == 1) return ZC_lincomb(Fp_sqrt(N2,p),gen_1,x2,x3);
822 2129 : N3 = Fp_mul(v2, N2, p);
823 2129 : if (kronecker(N3,p) == 1) return ZC_lincomb(Fp_sqrt(N3,p),gen_1,x1,x3);
824 1065 : r = gen_1;
825 : for(;;)
826 : {
827 2046 : s = Fp_sub(gen_1, Fp_mul(N1,Fp_sqr(r,p),p), p);
828 2046 : if (kronecker(s, p) <= 0) break;
829 981 : r = randomi(p);
830 : }
831 1065 : s = Fp_sqrt(Fp_div(s,N3,p), p);
832 1065 : return ZC_add(x1, ZC_lincomb(r,s,x2,x3));
833 : }
834 :
835 : /* Given a square rational matrix G of dimension n >= 1, solves over Z the
836 : * quadratic equation X^tGX = 0. The solution is a t_VEC (a solution) or a
837 : * t_MAT (totally isotropic subspace). If no solution exists, returns an
838 : * integer: a prime p (no local solution at p), or -1 (no real solution), or
839 : * -2 (n = 2 and -deg(G) not a square). */
840 : static GEN
841 12502 : qfsolve_i(GEN G)
842 : {
843 : GEN M, signG, Min, U, G1, M1, G2, M2, solG2, P, E;
844 : GEN solG1, sol, Q, d, dQ, detG2, fam2detG;
845 : long n, np, codim, dim;
846 : int fail;
847 :
848 12502 : if (typ(G) != t_MAT) pari_err_TYPE("qfsolve", G);
849 12502 : n = lg(G)-1;
850 12502 : if (n == 0) pari_err_DOMAIN("qfsolve", "dimension" , "=", gen_0, G);
851 12502 : if (n != nbrows(G)) pari_err_DIM("qfsolve");
852 12502 : G = Q_primpart(G); RgM_check_ZM(G, "qfsolve");
853 12502 : check_symmetric(G);
854 :
855 : /* Trivial case: det = 0 */
856 12495 : d = ZM_det(G);
857 12495 : if (!signe(d))
858 : {
859 7 : if (n == 1) return mkcol(gen_1);
860 0 : sol = ZM_ker(G);
861 0 : if (lg(sol) == 2) sol = gel(sol,1);
862 0 : return sol;
863 : }
864 :
865 : /* Small dimension: n <= 2 */
866 12488 : if (n == 1) return gen_m1;
867 12481 : if (n == 2)
868 : {
869 21 : GEN t, a = gcoeff(G,1,1);
870 21 : if (!signe(a)) return mkcol2(gen_1, gen_0);
871 14 : if (signe(d) > 0) return gen_m1; /* no real solution */
872 14 : if (!Z_issquareall(negi(d), &t)) return gen_m2;
873 7 : return mkcol2(subii(t,gcoeff(G,1,2)), a);
874 : }
875 :
876 : /* 1st reduction of the coefficients of G */
877 12460 : M = qflllgram_indef(G,0,&fail);
878 12460 : if (typ(M) == t_COL) return M;
879 11984 : G = gel(M,1);
880 11984 : M = gel(M,2);
881 :
882 : /* real solubility */
883 11984 : signG = ZV_to_zv(qfsign(G));
884 : {
885 11984 : long r = signG[1], s = signG[2];
886 11984 : if (!r || !s) return gen_m1;
887 11914 : if (r < s) { G = ZM_neg(G); signG = mkvecsmall2(s,r); }
888 : }
889 :
890 : /* factorization of the determinant */
891 11914 : fam2detG = absZ_factor(d);
892 11914 : P = gel(fam2detG,1);
893 11914 : E = ZV_to_zv(gel(fam2detG,2));
894 : /* P,E = factor(|det(G)|) */
895 :
896 : /* Minimization and local solubility */
897 11914 : Min = qfminimize_fact(G, P, E, 1);
898 11914 : if (typ(Min) == t_INT) return Min;
899 :
900 11431 : M = QM_mul(M, gel(Min,2));
901 11431 : G = gel(Min,1);
902 11431 : P = gel(Min,3);
903 11431 : E = gel(Min,4);
904 : /* P,E = factor(|det(G))| */
905 :
906 : /* Now, we know that local solutions exist (except maybe at 2 if n==4)
907 : * if n==3, det(G) = +-1
908 : * if n==4, or n is odd, det(G) is squarefree.
909 : * if n>=6, det(G) has all its valuations <=2. */
910 :
911 : /* Reduction of G and search for trivial solutions. */
912 : /* When |det G|=1, such trivial solutions always exist. */
913 11431 : U = qflllgram_indef(G,0,&fail);
914 11431 : if (typ(U) == t_COL) return Q_primpart(RgM_RgC_mul(M,U));
915 2989 : G = gel(U,1);
916 2989 : M = QM_mul(M, gel(U,2));
917 : /* P,E = factor(|det(G))| */
918 :
919 : /* If n >= 6 is even, need to increment the dimension by 1 to suppress all
920 : * squares from det(G) */
921 2989 : np = lg(P)-1;
922 2989 : if (n < 6 || odd(n) || !np)
923 : {
924 1603 : codim = 0;
925 1603 : G1 = G;
926 1603 : M1 = NULL;
927 : }
928 : else
929 : {
930 : GEN aux;
931 : long i;
932 1386 : codim = 1; n++;
933 1386 : aux = gen_1;
934 6524 : for (i = 1; i <= np; i++)
935 5138 : if (E[i] == 2) { aux = mulii(aux, gel(P,i)); E[i] = 3; }
936 : /* aux = largest square divisor of d; choose sign(aux) so as to balance
937 : * the signature of G1 */
938 1386 : if (signG[1] > signG[2])
939 : {
940 546 : signG[2]++;
941 546 : aux = negi(aux);
942 : }
943 : else
944 840 : signG[1]++;
945 1386 : G1 = shallowmatconcat(diagonal_shallow(mkvec2(G,aux)));
946 : /* P,E = factor(|det G1|) */
947 1386 : Min = qfminimize_fact(G1, P, E, 1);
948 1386 : G1 = gel(Min,1);
949 1386 : M1 = gel(Min,2);
950 1386 : P = gel(Min,3);
951 1386 : E = gel(Min,4);
952 1386 : np = lg(P)-1;
953 : }
954 :
955 : /* now, d is squarefree */
956 2989 : if (!np) { G2 = G1; M2 = NULL; } /* |d| = 1 */
957 : else
958 : { /* |d| > 1: increment dimension by 2 */
959 2723 : GEN factdP, factdE, W, v = NULL;
960 : long i, lfactdP, lP;
961 2723 : codim += 2;
962 2723 : d = ZV_prod(P); /* d = abs(matdet(G1)); */
963 2723 : if (odd(signG[2])) togglesign_safe(&d); /* d = matdet(G1); */
964 2723 : if (n == 4)
965 : { /* solubility at 2, the only remaining bad prime */
966 700 : v = det_minors(G1);
967 700 : if (Mod8(d) == 1 && witt(v, gen_2) == 1) return gen_2;
968 : }
969 2688 : P = shallowconcat(mpodd(d)? mkvec2(NULL,gen_2): mkvec(NULL), P);
970 : /* build a binary quadratic form with given Witt invariants */
971 2688 : lP = lg(P); W = const_vecsmall(lP-1, 0);
972 : /* choose signature of Q (real invariant and sign of the discriminant) */
973 2688 : dQ = absi(d);
974 2688 : if (signG[1] > signG[2]) togglesign_safe(&dQ); /* signQ = [2,0]; */
975 2688 : if (n == 4 && Mod4(dQ) != 1) dQ = shifti(dQ,2);
976 2688 : if (n >= 5) dQ = shifti(dQ,3);
977 :
978 : /* p-adic invariants */
979 2688 : if (n == 4)
980 : {
981 665 : togglesign(gel(v,2));
982 665 : togglesign(gel(v,4)); /* v = det_minors(-G1) */
983 2667 : for (i = 3; i < lP; i++) W[i] = witt(v, gel(P,i)) < 0;
984 : }
985 : else
986 : {
987 2023 : long s = signe(dQ) == signe(d)? 1: -1;
988 : GEN t;
989 2023 : if (odd((n-3)/2)) s = -s;
990 2023 : t = s > 0? utoipos(8): utoineg(8);
991 6083 : for (i = 3; i < lP; i++) W[i] = hilbertii(t, gel(P,i), gel(P,i)) > 0;
992 : }
993 2688 : W[2] = Flv_sum(W, 2); /* for p = 2, use product formula */
994 :
995 : /* Construction of the 2-class group of discriminant dQ until some product
996 : * of the generators gives the desired invariants. */
997 2688 : factdP = vecsplice(P, 1); lfactdP = lg(factdP);
998 2688 : factdE = cgetg(lfactdP, t_VECSMALL);
999 11438 : for (i = 1; i < lfactdP; i++) factdE[i] = Z_pval(dQ, gel(factdP,i));
1000 2688 : factdE[1]++;
1001 : /* factdP,factdE = factor(2|dQ|), P = factor(-2|dQ|)[,1] */
1002 2688 : Q = quadclass2(dQ, factdP,factdE, P, W, n == 4);
1003 : /* Build a form of dim=n+2 potentially unimodular */
1004 2688 : G2 = shallowmatconcat(diagonal_shallow(mkvec2(G1,ZM_neg(Q))));
1005 : /* Minimization of G2 */
1006 2688 : detG2 = mulii(d, ZM_det(Q));
1007 11438 : for (i = 1; i < lfactdP; i++) factdE[i] = Z_pval(detG2, gel(factdP,i));
1008 : /* factdP,factdE = factor(|det G2|) */
1009 2688 : Min = qfminimize_fact(G2, factdP,factdE,1);
1010 2688 : M2 = gel(Min,2);
1011 2688 : G2 = gel(Min,1);
1012 : }
1013 : /* |det(G2)| = 1, find a totally isotropic subspace for G2 */
1014 2954 : solG2 = qflllgram_indefgoon(G2);
1015 : /* G2 must have a subspace of solutions of dimension > codim */
1016 2954 : dim = codim+1;
1017 7280 : while(gequal0(principal_minor(gel(solG2,1), dim))) dim ++;
1018 2954 : if (dim == codim+1)
1019 7 : pari_err_IMPL("qfsolve, dim >= 10");
1020 2947 : solG2 = vecslice(gel(solG2,2), 1, dim-1);
1021 :
1022 2947 : if (!M2) solG1 = solG2;
1023 : else
1024 : { /* solution of G1 is simultaneously in solG2 and x[n+1] = x[n+2] = 0*/
1025 : GEN K;
1026 2688 : solG1 = QM_mul(M2,solG2);
1027 2688 : K = QM_ker(rowslice(solG1,n+1,n+2));
1028 2688 : solG1 = QM_mul(rowslice(solG1,1,n), K);
1029 : }
1030 2947 : if (!M1) sol = solG1;
1031 : else
1032 : { /* solution of G1 is simultaneously in solG2 and x[n] = 0 */
1033 : GEN K;
1034 1386 : sol = QM_mul(M1,solG1);
1035 1386 : K = QM_ker(rowslice(sol,n,n));
1036 1386 : sol = QM_mul(rowslice(sol,1,n-1), K);
1037 : }
1038 2947 : sol = Q_primpart(QM_mul(M, sol));
1039 2947 : if (lg(sol) == 2) sol = gel(sol,1);
1040 2947 : return sol;
1041 : }
1042 : GEN
1043 12502 : qfsolve(GEN G)
1044 12502 : { pari_sp av = avma; return gerepilecopy(av, qfsolve_i(G)); }
1045 :
1046 : /* G is a symmetric 3x3 matrix, and sol a solution of sol~*G*sol=0.
1047 : * Returns a parametrization of the solutions with the good invariants,
1048 : * as a 3x3 matrix, where each line contains the coefficients of each of the 3
1049 : * quadratic forms. If fl!=0, the fl-th form is reduced. */
1050 : GEN
1051 7133 : qfparam(GEN G, GEN sol, long fl)
1052 : {
1053 7133 : pari_sp av = avma;
1054 : GEN U, G1, G2, a, b, c, d, e;
1055 7133 : long n, tx = typ(sol);
1056 :
1057 7133 : if (typ(G) != t_MAT) pari_err_TYPE("qfsolve", G);
1058 7133 : if (!is_vec_t(tx)) pari_err_TYPE("qfsolve", G);
1059 7133 : if (tx == t_VEC) sol = shallowtrans(sol);
1060 7133 : n = lg(G)-1;
1061 7133 : if (n == 0) pari_err_DOMAIN("qfsolve", "dimension" , "=", gen_0, G);
1062 7133 : if (n != nbrows(G) || n != 3 || lg(sol) != 4) pari_err_DIM("qfsolve");
1063 7133 : G = Q_primpart(G); RgM_check_ZM(G,"qfsolve");
1064 7133 : check_symmetric(G);
1065 7133 : sol = Q_primpart(sol); RgV_check_ZV(sol,"qfsolve");
1066 : /* build U such that U[,3] = sol, and |det(U)| = 1 */
1067 7133 : U = completebasis(sol,1);
1068 7133 : G1 = qf_apply_ZM(G,U); /* G1 has a 0 at the bottom right corner */
1069 7133 : a = shifti(gcoeff(G1,1,2),1);
1070 7133 : b = shifti(negi(gcoeff(G1,1,3)),1);
1071 7133 : c = shifti(negi(gcoeff(G1,2,3)),1);
1072 7133 : d = gcoeff(G1,1,1);
1073 7133 : e = gcoeff(G1,2,2);
1074 7133 : G2 = mkmat3(mkcol3(b,gen_0,d), mkcol3(c,b,a), mkcol3(gen_0,c,e));
1075 7133 : sol = ZM_mul(U,G2);
1076 7133 : if (fl)
1077 : {
1078 7112 : GEN v = row(sol,fl);
1079 : int fail;
1080 7112 : a = gel(v,1);
1081 7112 : b = gmul2n(gel(v,2),-1);
1082 7112 : c = gel(v,3);
1083 7112 : U = qflllgram_indef(mkmat22(a, b, b, c), 1, &fail);
1084 7112 : U = gel(U,2);
1085 7112 : a = gcoeff(U,1,1); b = gcoeff(U,1,2);
1086 7112 : c = gcoeff(U,2,1); d = gcoeff(U,2,2);
1087 7112 : U = mkmat3(mkcol3(sqri(a),mulii(a,c),sqri(c)),
1088 : mkcol3(shifti(mulii(a,b),1), addii(mulii(a,d),mulii(b,c)),
1089 : shifti(mulii(c,d),1)),
1090 : mkcol3(sqri(b),mulii(b,d),sqri(d)));
1091 7112 : sol = ZM_mul(sol,U);
1092 : }
1093 7133 : return gerepileupto(av, sol);
1094 : }
|