Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is to exceed 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - basemath - qfsolve.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.14.0 lcov report (development 27775-aca467eab2) Lines: 604 614 98.4 %
Date: 2022-07-03 07:33:15 Functions: 33 33 100.0 %
Legend: Lines: hit not hit

          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      116147 : vecextend(GEN v, long n)
      30             : {
      31      116147 :   long i, l = lg(v);
      32      116147 :   GEN w = cgetg(n+1, t_COL);
      33      358675 :   for (i = 1; i < l; i++) gel(w,i) = gel(v,i);
      34      386205 :   for (     ; i <=n; i++) gel(w,i) = gen_0;
      35      116147 :   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      408306 : completebasis(GEN Mv, long redflag)
      43             : {
      44             :   GEN U;
      45             :   long m, n;
      46             : 
      47      408306 :   if (typ(Mv) == t_COL) Mv = mkmat(Mv);
      48      408306 :   n = lg(Mv)-1;
      49      408306 :   m = nbrows(Mv); /* m x n */
      50      408306 :   if (m == n) return Mv;
      51      407732 :   (void)ZM_hnfall_i(shallowtrans(Mv), &U, 0);
      52      407732 :   U = ZM_inv(shallowtrans(U),NULL);
      53      407732 :   if (m==1 || !redflag) return U;
      54             :   /* LLL-reduce the m-n first columns */
      55      122577 :   return shallowconcat(ZM_lll(vecslice(U,1,m-n), 0.99, LLL_INPLACE),
      56      122577 :                        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      285439 : kermodp(GEN M, GEN p, long *d)
      62             : {
      63             :   long j, l;
      64             :   GEN K, B, U;
      65             : 
      66      285439 :   K = FpM_center(FpM_ker(M, p), p, shifti(p,-1));
      67      285439 :   B = completebasis(K,0);
      68      285439 :   l = lg(M); U = cgetg(l, t_MAT);
      69     1362424 :   for (j =  1; j < l; j++) gel(U,j) = gel(B,l-j);
      70      285439 :   *d = lg(K)-1; return U;
      71             : }
      72             : 
      73             : /* INVARIANTS COMPUTATIONS */
      74             : 
      75             : static GEN
      76       33239 : 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 :   return mkmat2(mkcol2(a, b), mkcol2(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 :   return mkmat2(mkcol2(a, b), mkcol2(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       44100 : partialgaussred(GEN a)
     127             : {
     128       44100 :   long n = lg(a)-1, k;
     129       44100 :   a = RgM_shallowcopy(a);
     130      147909 :   for(k = 1; k < n; k++)
     131             :   {
     132      113067 :     GEN ak, p = gcoeff(a,k,k);
     133             :     long i, j;
     134      113067 :     if (isintzero(p)) return stoi(k);
     135      103809 :     ak = row(a, k);
     136      386068 :     for (i=k+1; i<=n; i++) gcoeff(a,k,i) = gdiv(gcoeff(a,k,i), p);
     137      386068 :     for (i=k+1; i<=n; i++)
     138             :     {
     139      282259 :       GEN c = gel(ak,i);
     140      282259 :       if (gequal0(c)) continue;
     141      848287 :       for (j=i; j<=n; j++)
     142      619214 :         gcoeff(a,i,j) = gsub(gcoeff(a,i,j), gmul(c,gcoeff(a,k,j)));
     143             :     }
     144             :   }
     145       34842 :   if (isintzero(gcoeff(a,n,n))) return stoi(n);
     146       34821 :   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       44100 : qflllgram_indef(GEN G, long base, int *fail)
     154             : {
     155             :   GEN M, R, g, DM, S, dR;
     156       44100 :   long i, j, n = lg(G)-1;
     157             : 
     158       44100 :   *fail = 0;
     159       44100 :   R = partialgaussred(G);
     160       44100 :   if (typ(R) == t_INT) return qftriv(G, R, base);
     161       34821 :   R = Q_remove_denom(R, &dR); /* avoid rational arithmetic */
     162       34821 :   M = zeromatcopy(n,n);
     163       34821 :   DM = zeromatcopy(n,n);
     164      168029 :   for (i = 1; i <= n; i++)
     165             :   {
     166      133208 :     GEN d = absi_shallow(gcoeff(R,i,i));
     167      133208 :     if (dR) {
     168      109680 :       gcoeff(M,i,i) = dR;
     169      109680 :       gcoeff(DM,i,i) = mulii(d,dR);
     170             :     } else {
     171       23528 :       gcoeff(M,i,i) = gen_1;
     172       23528 :       gcoeff(DM,i,i) = d;
     173             :     }
     174      392483 :     for (j = i+1; j <= n; j++)
     175             :     {
     176      259275 :       gcoeff(M,i,j) = gcoeff(R,i,j);
     177      259275 :       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       34821 :   g = ZM_transmultosym(M,DM);
     182       34821 :   S = lllgramint(Q_primpart(g));
     183       34821 :   R = qftriv(qf_apply_ZM(G,S), NULL, base);
     184       34821 :   switch(typ(R))
     185             :   {
     186        6167 :     case t_COL: return ZM_ZC_mul(S,R);
     187       21015 :     case t_MAT: *fail = 1; return mkvec2(R, S);
     188        7639 :     default:
     189        7639 :       gel(R,2) = ZM_mul(S, gel(R,2));
     190        7639 :       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        4132 : qf_apply_transvect_Z(GEN G, GEN U, long i, long j, GEN a)
     198             : {
     199        4132 :   long k, n = lg(G)-1;
     200        4132 :   gel(G, j) =  ZC_lincomb(gen_1, a, gel(G,j), gel(G,i));
     201       22609 :   for (k = 1; k < n; k++) gcoeff(G, j, k) = gcoeff(G, k, j);
     202        4132 :   gcoeff(G,j,j) = addmulii(gcoeff(G,j,j), a,
     203        4132 :                            addmulii(gcoeff(G,i,j), a,gcoeff(G,i,i)));
     204        4132 :   gel(U, j) =  ZC_lincomb(gen_1, a, gel(U,j), gel(U,i));
     205        4132 : }
     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 = mkmat2(mkcol2(gcoeff(G3,1,1),gcoeff(G3,1,n)),
     232       10423 :              mkcol2(gcoeff(G3,1,n),gcoeff(G3,2,2)));
     233       10423 :   B = ground(RgM_neg(QM_mul(QM_inv(A), V)));
     234       10423 :   U3 = matid(n);
     235       51884 :   for (j = 2; j < n; j++)
     236             :   {
     237       41461 :     gcoeff(U3,1,j) = gcoeff(B,1,j-1);
     238       41461 :     gcoeff(U3,n,j) = gcoeff(B,2,j-1);
     239             :   }
     240       10423 :   G4 = qf_apply_ZM(G3,U3); /* the last column of G4 is reduced */
     241       10423 :   U = ZM_mul(U,U3);
     242       10423 :   if (n == 3) return mkvec2(G4,U);
     243             : 
     244        8148 :   red = qflllgram_indefgoon(matslice(G4,2,n-1,2,n-1));
     245        8148 :   if (typ(red) == t_MAT) return mkvec2(G4,U);
     246             :   /* Let U5:=matconcat(diagonal[1,red[2],1])
     247             :    * return [qf_apply_ZM(G5, U5), U*U5] */
     248        8148 :   G5 = gel(red,1);
     249        8148 :   U5 = gel(red,2);
     250        8148 :   G6 = cgetg(n+1,t_MAT);
     251        8148 :   gel(G6,1) = gel(G4,1);
     252        8148 :   gel(G6,n) = gel(G4,n);
     253       47334 :   for (j=2; j<n; j++)
     254             :   {
     255       39186 :     gel(G6,j) = cgetg(n+1,t_COL);
     256       39186 :     gcoeff(G6,1,j) = gcoeff(G4,j,1);
     257       39186 :     gcoeff(G6,n,j) = gcoeff(G4,j,n);
     258      259112 :     for (i=2; i<n; i++) gcoeff(G6,i,j) = gcoeff(G5,i-1,j-1);
     259             :   }
     260        8148 :   U6 = mkvec3(mkmat(gel(U,1)), ZM_mul(vecslice(U,2,n-1),U5), mkmat(gel(U,n)));
     261        8148 :   return mkvec2(G6, shallowconcat1(U6));
     262             : }
     263             : 
     264             : /* qf_apply_ZM(G,H),  where H = matrix of \tau_{i,j}, i != j */
     265             : static GEN
     266       13334 : qf_apply_tau(GEN G, long i, long j)
     267             : {
     268       13334 :   long l = lg(G), k;
     269       13334 :   G = RgM_shallowcopy(G);
     270       13334 :   swap(gel(G,i), gel(G,j));
     271       80521 :   for (k = 1; k < l; k++) swap(gcoeff(G,i,k), gcoeff(G,j,k));
     272       13334 :   return G;
     273             : }
     274             : 
     275             : /* LLL reduction of the quadratic form G (Gram matrix)
     276             :  * in dim 3 only, with detG = -1 and sign(G) = [2,1]; */
     277             : static GEN
     278        2282 : qflllgram_indefgoon2(GEN G)
     279             : {
     280             :   GEN red, G2, a, b, c, d, e, f, u, v, r, r3, U2, G3;
     281             :   int fail;
     282             : 
     283        2282 :   red = qflllgram_indef(G,1,&fail); /* always find an isotropic vector. */
     284        2282 :   G2 = qf_apply_tau(gel(red,1),1,3); /* G2[3,3] = 0 */
     285        2282 :   r = row(gel(red,2), 3);
     286        2282 :   swap(gel(r,1), gel(r,3)); /* apply tau_{1,3} */
     287        2282 :   a = gcoeff(G2,3,1);
     288        2282 :   b = gcoeff(G2,3,2);
     289        2282 :   d = bezout(a,b, &u,&v);
     290        2282 :   if (!equali1(d))
     291             :   {
     292           0 :     a = diviiexact(a,d);
     293           0 :     b = diviiexact(b,d);
     294             :   }
     295             :   /* for U2 = [-u,-b,0;-v,a,0;0,0,1]
     296             :    * G3 = qf_apply_ZM(G2,U2) has known last row (-d, 0, 0),
     297             :    * so apply to principal_minor(G3,2), instead */
     298        2282 :   U2 = mkmat2(mkcol2(negi(u),negi(v)), mkcol2(negi(b),a));
     299        2282 :   G3 = qf_apply_ZM(principal_minor(G2,2),U2);
     300        2282 :   r3 = gel(r,3);
     301        2282 :   r = ZV_ZM_mul(mkvec2(gel(r,1),gel(r,2)),U2);
     302             : 
     303        2282 :   a = gcoeff(G3,1,1);
     304        2282 :   b = gcoeff(G3,1,2);
     305        2282 :   c = negi(d); /* G3[1,3] */
     306        2282 :   d = gcoeff(G3,2,2);
     307        2282 :   if (mpodd(a))
     308             :   {
     309        1239 :     e = addii(b,d);
     310        1239 :     a = addii(a, addii(b,e));
     311        1239 :     e = diviiround(negi(e),c);
     312        1239 :     f = diviiround(negi(a), shifti(c,1));
     313        1239 :     a = addmulii(addii(gel(r,1),gel(r,2)), f,r3);
     314             :   }
     315             :   else
     316             :   {
     317        1043 :     e = diviiround(negi(b),c);
     318        1043 :     f = diviiround(negi(shifti(a,-1)), c);
     319        1043 :     a = addmulii(gel(r,1), f, r3);
     320             :   }
     321        2282 :   b = addmulii(gel(r,2), e, r3);
     322        2282 :   return mkvec3(a,b, r3);
     323             : }
     324             : 
     325             : /* QUADRATIC FORM MINIMIZATION */
     326             : /* G symmetric, return ZM_Z_divexact(G,d) */
     327             : static GEN
     328       20314 : ZsymM_Z_divexact(GEN G, GEN d)
     329             : {
     330       20314 :   long i,j,l = lg(G);
     331       20314 :   GEN H = cgetg(l, t_MAT);
     332       79121 :   for (j = 1; j < l; j++)
     333             :   {
     334       58807 :     GEN c = cgetg(l, t_COL), b = gel(G,j);
     335      115486 :     for (i=1; i < j; i++) gcoeff(H,j,i) = gel(c,i) = diviiexact(gel(b,i),d);
     336       58807 :     gel(c,j) = diviiexact(gel(b,j),d);
     337       58807 :     gel(H,j) = c;
     338             :   }
     339       20314 :   return H;
     340             : }
     341             : /* by blocks, in place: G[1,1] /= d, G[2,2] *= d */
     342             : static void
     343       92190 : ZsymM_Z_divexact_partial(GEN G, long n,  GEN d)
     344             : {
     345       92190 :   long i, j, l = lg(G);
     346      185017 :   for(j = 1; j <= n; j++)
     347             :   {
     348       93464 :     for (i = 1; i < j; i++)
     349         637 :       gcoeff(G,i,j) = gcoeff(G,j,i) = diviiexact(gcoeff(G,i,j), d);
     350       92827 :     gcoeff(G,j,j) = diviiexact(gcoeff(G,j,j), d);
     351             :   }
     352      309701 :   for (; j < l; j++)
     353             :   {
     354      454720 :     for (i = n+1; i < j; i++)
     355      237209 :       gcoeff(G,i,j) = gcoeff(G,j,i) = mulii(gcoeff(G,i,j), d);
     356      217511 :     gcoeff(G,j,j) = mulii(gcoeff(G,j,j), d);
     357             :   }
     358       92190 : }
     359             : 
     360             : /* write symmetric G as [A,B;B~,C], A dxd, C (n-d)x(n-d) */
     361             : static void
     362        2261 : blocks4(GEN G, long d, long n, GEN *A, GEN *B, GEN *C)
     363             : {
     364        2261 :   GEN G2 = vecslice(G,d+1,n);
     365        2261 :   *A = principal_minor(G, d);
     366        2261 :   *B = rowslice(G2, 1, d);
     367        2261 :   *C = rowslice(G2, d+1, n);
     368        2261 : }
     369             : static GEN qfsolvemodp(GEN G, GEN p);
     370             : static void
     371       72751 : update_fm(GEN f, GEN a, long i)
     372             : {
     373       72751 :   if (!odd(i))
     374       51191 :     gel(f,1) = a;
     375             :   else
     376             :   {
     377       21560 :     long v = vals(i+1), k;
     378       21560 :     GEN b = gel(f,1), u = QM_mul(b, a);
     379       21560 :     gel(f,1) = gen_0;
     380       21560 :     if (v+2 >= lg(f)) pari_err_BUG("update_fm");
     381       26341 :     for (k = 1; k < v; k++)
     382             :     {
     383        4781 :       u = QM_mul(gel(f, k+1), u);
     384        4781 :       gel(f,k+1) = gen_0; /* for gerepileall */
     385             :     }
     386       21560 :     gel(f,v+1) = u;
     387             :   }
     388       72751 : }
     389             : static GEN
     390       41230 : prod_fm(GEN f, long i)
     391             : {
     392       41230 :   long v = vals(i), k;
     393       41230 :   GEN u = gel(f, ++v);
     394       47691 :   for (i >>= v, k = v+1; i; i >>= 1, k++)
     395        6461 :     if (odd(i)) u = QM_mul(gel(f,k), u);
     396       41230 :   return u;
     397             : }
     398             : 
     399             : /* Minimization of the quadratic form G, deg G != 0, dim n >= 2
     400             :  * G symmetric integral
     401             :  * Returns [G',U,factd] with U in GLn(Q) such that G'=U~*G*U*constant
     402             :  * is integral and has minimal determinant.
     403             :  * In dimension 3 or 4, may return a prime p if the reduction at p is
     404             :  * impossible because of local nonsolvability.
     405             :  * P,E = factor(+/- det(G)), "prime" -1 is ignored. Destroy E. */
     406             : static GEN
     407       16002 : qfminimize_fact(GEN G, GEN P, GEN E, long loc)
     408             : {
     409       16002 :   GEN U = NULL, Ker = NULL, faE, faP;
     410       16002 :   long n = lg(G)-1, lP = lg(P), i;
     411       16002 :   faP = vectrunc_init(lP);
     412       16002 :   faE = vecsmalltrunc_init(lP);
     413      155435 :   for (i = 1; i < lP; i++)
     414             :   {
     415      139916 :     GEN p = gel(P,i);
     416      139916 :     long vp = E[i], wp = vp;
     417      139916 :     if (!vp) continue;
     418      139636 :     if (DEBUGLEVEL >= 3) err_printf("qfminimize: for %Ps^%ld:", p,vp);
     419      345149 :     while (vp)
     420             :     {
     421      247618 :       long idx = 0, dimKer = 0; /* -Wall */
     422      247618 :       GEN d, sol = NULL, FU = zerovec(2*expu(vp)+100);
     423      247618 :       pari_sp av = avma;
     424      320369 :       while (vp) /* loop until vp <= n */
     425             :       {
     426      290738 :         if (DEBUGLEVEL>=3 && vp <= wp)
     427           0 :         { err_printf(" %ld%%", (E[i]-vp)*100/E[i]); wp -= E[i]/100; }
     428             :         /* The case vp = 1 can be minimized only if n is odd. */
     429      290738 :         if (vp == 1 && !odd(n)) break;
     430      283178 :         Ker = kermodp(G,p, &dimKer); /* dimKer <= vp */
     431      283178 :         if (DEBUGLEVEL >= 4) err_printf("    dimKer = %ld\n",dimKer);
     432      283178 :         if (dimKer == n) break;
     433      283178 :         G = qf_apply_ZM(G, Ker);
     434             :         /* 1st case: dimKer < vp */
     435             :         /* then the kernel mod p contains a kernel mod p^2 */
     436      283178 :         if (dimKer >= vp) break;
     437             : 
     438       72751 :         if (DEBUGLEVEL >= 4) err_printf("    case 1: dimker < vp\n");
     439       72751 :         if (dimKer == 1)
     440             :         {
     441             :           long j;
     442       70490 :           gcoeff(G,1,1) = diviiexact(gcoeff(G,1,1), sqri(p));
     443      215950 :           for (j = 2; j <= n; j++)
     444      145460 :             gcoeff(G,1,j) = gcoeff(G,j,1) = diviiexact(gcoeff(G,j,1), p);
     445       70490 :           gel(Ker,1) = RgC_Rg_div(gel(Ker,1), p);
     446       70490 :           vp -= 2;
     447             :         }
     448             :         else
     449             :         {
     450             :           GEN A, B, C, K2;
     451             :           long j, dimKer2;
     452        2261 :           blocks4(G, dimKer,n, &A,&B,&C); A = ZsymM_Z_divexact(A, p);
     453        2261 :           K2 = kermodp(A, p, &dimKer2);
     454             :           /* Write G = [pA,B;B~,C] and apply [K2/p,0;0,Id] by blocks */
     455        2261 :           A = qf_apply_ZM(A,K2); ZsymM_Z_divexact_partial(A, dimKer2, p);
     456        2261 :           B = ZM_transmul(B,K2);
     457        5124 :           for (j = 1; j <= dimKer2; j++) gel(B,j) = ZC_Z_divexact(gel(B,j), p);
     458        2261 :           G = shallowmatconcat(mkmat2(mkcol2(A,B), mkcol2(shallowtrans(B), C)));
     459             :           /* Ker *= [K2,0;0,Id] */
     460        2261 :           B = ZM_mul(vecslice(Ker,1,dimKer),K2);
     461        5124 :           for (j = 1; j <= dimKer2; j++) gel(B,j) = RgC_Rg_div(gel(B,j), p);
     462        2261 :           Ker = shallowconcat(B, vecslice(Ker,dimKer+1,n));
     463        2261 :           vp -= 2*dimKer2;
     464             :         }
     465       72751 :         update_fm(FU, Ker, idx++);
     466       72751 :         if (gc_needed(av, 1))
     467             :         {
     468           0 :           if (DEBUGMEM >= 2) pari_warn(warnmem,"qfminimize");
     469           0 :           gerepileall(av, 2, &G, &FU);
     470             :         }
     471             :       }
     472      247618 :       if (idx)
     473             :       {
     474       41230 :         GEN PU = prod_fm(FU, idx);
     475       41230 :         U = U ? QM_mul(U, PU): PU;
     476             :       }
     477      259609 :       if (vp == 0) break;
     478      217987 :       if (vp == 1 && !odd(n))
     479             :       {
     480        7560 :         vectrunc_append(faP, p);
     481        7560 :         vecsmalltrunc_append(faE, 1);
     482        7560 :         break;
     483             :       }
     484      210427 :       if (dimKer == n)
     485             :       { /* trivial case: dimKer = n */
     486           0 :         if (DEBUGLEVEL >= 4) err_printf("     case 0: dimKer = n\n");
     487           0 :         G = ZsymM_Z_divexact(G, p);
     488      205513 :         vp -= n; continue;
     489             :       }
     490      210427 :       U = U ? QM_mul(U, Ker): Ker;
     491             :       /* vp = dimKer
     492             :        * 2nd case: kernel has dim >= 2 and contains an elt of norm 0 mod p^2,
     493             :        * find it */
     494      210427 :       if (dimKer > 2) {
     495       18053 :         if (DEBUGLEVEL >= 4) err_printf("    case 2.1\n");
     496       18053 :         dimKer = 3;
     497       18053 :         sol = qfsolvemodp(ZsymM_Z_divexact(principal_minor(G,3),p),  p);
     498       18053 :         sol = FpC_red(sol, p);
     499             :       }
     500      192374 :       else if (dimKer == 2)
     501             :       {
     502       97580 :         GEN a = modii(diviiexact(gcoeff(G,1,1),p), p);
     503       97580 :         GEN b = modii(diviiexact(gcoeff(G,1,2),p), p);
     504       97580 :         GEN c = modii(diviiexact(gcoeff(G,2,2),p), p);
     505       97580 :         GEN D = modii(subii(sqri(b), mulii(a,c)), p);
     506       97580 :         if (kronecker(D,p) >= 0)
     507             :         {
     508       97531 :           if (DEBUGLEVEL >= 4) err_printf("    case 2.2\n");
     509       97531 :           sol = signe(a)? mkcol2(Fp_sub(Fp_sqrt(D,p), b, p), a): vec_ei(2,1);
     510             :         }
     511             :       }
     512      210427 :       if (sol)
     513             :       {
     514             :         long j;
     515      115584 :         sol = FpC_center(sol, p, shifti(p,-1));
     516      115584 :         sol = Q_primpart(sol);
     517      115584 :         if (DEBUGLEVEL >= 4) err_printf("    sol = %Ps\n", sol);
     518      115584 :         Ker = completebasis(vecextend(sol,n), 1);
     519      115584 :         G = qf_apply_ZM(G, Ker);
     520      509705 :         for (j = 1; j < n; j++)
     521      394121 :           gcoeff(G,n,j) = gcoeff(G,j,n) = diviiexact(gcoeff(G,j,n), p);
     522      115584 :         gcoeff(G,n,n) = diviiexact(gcoeff(G,n,n), sqri(p));
     523      115584 :         U = QM_mul(U,Ker); gel(U,n) = RgC_Rg_div(gel(U,n), p);
     524      115584 :         vp -= 2; continue;
     525             :       }
     526             :       /* Now 0 < vp = dimKer < 3 and kernel contains no vector with norm p^2 */
     527             :       /* exchanging kernel and image makes minimization easier ? */
     528       94843 :       d = ZM_det(G); if (odd((n-3) / 2)) d = negi(d);
     529       94843 :       if ((vp==1 && kronecker(gmod(gdiv(negi(d), gcoeff(G,1,1)),p), p) >= 0)
     530        4949 :           || (vp==2 && odd(n) && n >= 5)
     531        4935 :           || (vp==2 && !odd(n) && kronecker(modii(diviiexact(d,sqri(p)), p),p) < 0))
     532             :       {
     533             :         long j;
     534       89929 :         if (DEBUGLEVEL >= 4) err_printf("    case 3\n");
     535       89929 :         ZsymM_Z_divexact_partial(G, dimKer, p);
     536      305655 :         for (j = dimKer+1; j <= n; j++) gel(U,j) = RgC_Rg_mul(gel(U,j), p);
     537       89929 :         vp -= 2*dimKer-n; continue;
     538             :       }
     539             : 
     540             :       /* Minimization was not possible so far. */
     541             :       /* If n == 3 or 4, this proves the local nonsolubility at p. */
     542        4914 :       if (loc && (n == 3 || n == 4))
     543             :       {
     544         483 :         if (DEBUGLEVEL >= 1) err_printf(" no local solution at %Ps\n",p);
     545         483 :         return p;
     546             :       }
     547        4431 :       vectrunc_append(faP, p);
     548        4431 :       vecsmalltrunc_append(faE, vp);
     549        4431 :       break;
     550             :     }
     551      139153 :     if (DEBUGLEVEL >= 3) err_printf("\n");
     552             :   }
     553       15519 :   if (!U) U = matid(n);
     554             :   else
     555             :   { /* apply LLL to avoid coefficient explosion */
     556       14455 :     GEN u = lllint(Q_primpart(U));
     557       14455 :     G = qf_apply_ZM(G, u);
     558       14455 :     U = QM_mul(U, u);
     559             :   }
     560       15519 :   return mkvec4(G, U, faP, faE);
     561             : }
     562             : 
     563             : /* assume G square integral */
     564             : static void
     565       19362 : check_symmetric(GEN G)
     566             : {
     567       19362 :   long i,j, l = lg(G);
     568       87969 :   for (i = 1; i < l; i++)
     569      172529 :     for(j = 1; j < i; j++)
     570      103922 :       if (!equalii(gcoeff(G,i,j), gcoeff(G,j,i)))
     571           7 :         pari_err_TYPE("qfsolve [not symmetric]",G);
     572       19355 : }
     573             : 
     574             : GEN
     575          14 : qfminimize(GEN G)
     576             : {
     577          14 :   pari_sp av = avma;
     578             :   GEN d, F, H;
     579          14 :   long n = lg(G)-1;
     580          14 :   if (typ(G) != t_MAT) pari_err_TYPE("qfminimize", G);
     581          14 :   if (n == 0) pari_err_DOMAIN("qfminimize", "dimension" , "=", gen_0, G);
     582          14 :   if (n != nbrows(G)) pari_err_DIM("qfminimize");
     583          14 :   G = Q_primpart(G); RgM_check_ZM(G, "qfminimize");
     584          14 :   check_symmetric(G);
     585          14 :   d = ZM_det(G);
     586          14 :   F = absZ_factor(d);
     587          14 :   H = qfminimize_fact(G, gel(F,1),  ZV_to_zv(gel(F,2)), 0);
     588          14 :   return gerepilecopy(av, mkvec2(gel(H,1), gel(H,2)));
     589             : }
     590             : 
     591             : /* CLASS GROUP COMPUTATIONS */
     592             : 
     593             : /* Compute the square root of the quadratic form q of discriminant D = 4 * md
     594             :  * Not fully implemented; only works for D squarefree except at 2, where the
     595             :  * valuation is 2 or 3. Finally, [P,E] = factor(2*abs(D)) if valuation is 3 and
     596             :  * factor(abs(D / 4)) otherwise */
     597             : static GEN
     598        2282 : qfbsqrt(GEN D, GEN md, GEN q, GEN P)
     599             : {
     600        2282 :   GEN a = gel(q,1), b = shifti(gel(q,2),-1), c = gel(q,3), B = negi(b);
     601        2282 :   GEN m, n, Q, M, N, d = negi(md); /* ac - b^2 */
     602        2282 :   long i, lP = lg(P);
     603             : 
     604             :   /* 1) solve m^2 = a, m*n = -b, n^2 = c in Z/dZ => q(n,m) = 0 mod d */
     605        2282 :   M = cgetg(lP, t_VEC);
     606        2282 :   N = cgetg(lP, t_VEC);
     607        9737 :   for (i = 1; i < lg(P); i++)
     608             :   {
     609        7455 :     GEN p = gel(P,i);
     610        7455 :     if (dvdii(a,p)) { n = Fp_sqrt(c, p); m = Fp_div(B, n, p); }
     611        5642 :     else            { m = Fp_sqrt(a, p); n = Fp_div(B, m, p); }
     612        7455 :     gel(M, i) = m;
     613        7455 :     gel(N, i) = n;
     614             :   }
     615        2282 :   m = ZV_chinese_center(M, P, NULL);
     616        2282 :   n = ZV_chinese_center(N, P, NULL);
     617             : 
     618             :   /* 2) build Q, with det=-1 such that Q(x,y,0) = G(x,y) */
     619        2282 :   N = diviiexact(addii(mulii(a,n), mulii(b,m)), d);
     620        2282 :   M = diviiexact(addii(mulii(b,n), mulii(c,m)), d);
     621        2282 :   Q = diviiexact(subiu(addii(mulii(m,M), mulii(n,N)), 1), d); /*(q(n,m)-d)/d^2 */
     622        2282 :   Q = mkmat3(mkcol3(a,b,N), mkcol3(b,c,M), mkcol3(N,M,Q)); /* det = -1 */
     623             : 
     624             :   /* 3) reduce Q to [0,0,-1; 0,1,0; -1,0,0] */
     625        2282 :   M = qflllgram_indefgoon2(Q);
     626        2282 :   if (signe(gel(M,1)) < 0) M = ZC_neg(M);
     627        2282 :   a = gel(M,1);
     628        2282 :   b = gel(M,2);
     629        2282 :   c = gel(M,3);
     630        2282 :   if (!mpodd(a)) { swap(a, c); togglesign_safe(&b); }
     631        2282 :   return mkqfb(a, shifti(b,1), shifti(c,1), D);
     632             : }
     633             : 
     634             : /* \prod gen[i]^e[i] as a Qfb, e in {0,1}^n nonzero */
     635             : static GEN
     636        3976 : qfb_factorback(GEN gen, GEN e, GEN isqrtD)
     637             : {
     638        3976 :   GEN q = NULL;
     639        3976 :   long j, l = lg(gen), n = 0;
     640       13524 :   for (j = 1; j < l; j++)
     641        9548 :     if (e[j]) { n++; q = q? qfbcompraw(q, gel(gen,j)): gel(gen,j); }
     642        3976 :   return (n <= 1)? q: qfbred0(q, 0, isqrtD, NULL);
     643             : }
     644             : 
     645             : /* unit form discriminant 4d */
     646             : static GEN
     647         994 : id(GEN d)
     648         994 : { return mkmat2(mkcol2(gen_1,gen_0),mkcol2(gen_0,negi(d))); }
     649             : 
     650             : /* Shanks/Bosma-Stevenhagen algorithm to compute the 2-Sylow of the class
     651             :  * group of discriminant D. Only works for D = fundamental discriminant.
     652             :  * When D = 1(4), work with 4D.
     653             :  * P2D,E2D = factor(abs(2*D))
     654             :  * Pm2D = factor(-abs(2*D))[,1].
     655             :  * Return a form having Witt invariants W at Pm2D */
     656             : static GEN
     657        2688 : quadclass2(GEN D, GEN P2D, GEN E2D, GEN Pm2D, GEN W, int n_is_4)
     658             : {
     659        2688 :   GEN U2 = NULL, gen, Wgen, isqrtD, d;
     660             :   long i, r, m;
     661        2688 :   int splice2 = mpodd(D);
     662             : 
     663        2688 :   if (!splice2) d = shifti(D,-2); else { d = D; D = shifti(D,2); }
     664             :   /* D = 4d */
     665        2688 :   if (zv_equal0(W)) return id(d);
     666        1813 :   r = lg(Pm2D) - 4; /* >= 0 since W != 0 */
     667        1813 :   m = (signe(D) > 0)? r+1: r;
     668        1813 :   if (n_is_4)
     669             :   { /* n = 4: look among forms of type q or 2*q, since Q can be imprimitive */
     670         539 :     U2 = hilberts(gen_2, d, Pm2D);
     671         539 :     if (zv_equal(U2,W)) return gmul2n(id(d),1);
     672             :   }
     673             : 
     674        1694 :   gen = cgetg(m+2, t_VEC);
     675        4578 :   for (i = 1; i <= m; i++)
     676             :   { /* no need to look at P2D[1]=2*/
     677        2884 :     GEN q = powiu(gel(P2D,i+1), E2D[i+1]);
     678        2884 :     gel(gen,i) = mkqfb(q, gen_0, negi(diviiexact(d,q)), D);
     679             :   }
     680        1694 :   if (!mpodd(d))
     681             :   {
     682        1470 :     gel(gen, ++m) = mkqfb(gen_2, gen_0, negi(shifti(d,-1)), D);
     683        1470 :     r++;
     684             :   }
     685         224 :   else if (Mod4(d) != 1)
     686             :   {
     687         119 :     gel(gen, ++m) = mkqfb(gen_2, gen_2, shifti(subsi(1,d),-1), D);
     688         119 :     r++;
     689             :   }
     690         105 :   else setlg(gen, m+1);
     691        1694 :   if (!r) return id(d);
     692             :   /* remove 2^3; leave alone 2^4 */
     693        1694 :   if (splice2) P2D = vecsplice(P2D, 1);
     694        1694 :   Wgen = cgetg(m+1, t_MAT);
     695        6167 :   for (i = 1; i <= m; i++) gel(Wgen,i) = hilberts(gmael(gen,i,1), d, Pm2D);
     696        1694 :   isqrtD = signe(D) > 0? sqrti(D) : NULL;
     697             :   for(;;)
     698        2072 :   {
     699        3766 :     GEN Wgen2, gen2, Ker, indexim = gel(Flm_indexrank(Wgen,2), 2);
     700             :     long dKer;
     701        3766 :     if (lg(indexim)-1 >= r)
     702             :     {
     703        1694 :       GEN W2 = Wgen, V;
     704        1694 :       if (lg(indexim) < lg(Wgen)) W2 = vecpermute(Wgen,indexim);
     705        1694 :       if (U2) W2 = vec_append(W2,U2);
     706        1694 :       V = Flm_Flc_invimage(W2, W, 2);
     707        1694 :       if (V)
     708             :       {
     709        1694 :         GEN Q = qfb_factorback(vecpermute(gen,indexim), V, isqrtD);
     710        1694 :         return (U2 && V[lg(V)-1])? qfbmat2(Q): qfbmat(Q);
     711             :       }
     712             :     }
     713        2072 :     Ker = Flm_ker(Wgen,2); dKer = lg(Ker)-1;
     714        2072 :     gen2 = cgetg(m+1, t_VEC);
     715        2072 :     Wgen2 = cgetg(m+1, t_MAT);
     716        4354 :     for (i = 1; i <= dKer; i++)
     717             :     {
     718        2282 :       GEN q = qfb_factorback(gen, gel(Ker,i), isqrtD);
     719        2282 :       gel(gen2,i) = q = qfbsqrt(D, d, q, P2D);
     720        2282 :       gel(Wgen2,i) = hilberts(gel(q,1), d, Pm2D);
     721             :     }
     722        4452 :     for (; i <=m; i++)
     723             :     {
     724        2380 :       long j = indexim[i-dKer];
     725        2380 :       gel(gen2,i) = gel(gen,j);
     726        2380 :       gel(Wgen2,i) = gel(Wgen,j);
     727             :     }
     728        2072 :     gen = gen2; Wgen = Wgen2;
     729             :   }
     730             : }
     731             : 
     732             : /* QUADRATIC EQUATIONS */
     733             : /* is x*y = -1 ? */
     734             : static int
     735       19192 : both_pm1(GEN x, GEN y)
     736       19192 : { return is_pm1(x) && is_pm1(y) && signe(x) == -signe(y); }
     737             : 
     738             : /* Try to solve G = 0 with small coefficients. This is proved to work if
     739             :  * -  det(G) = 1, dim <= 6 and G is LLL reduced
     740             :  * Returns G if no solution is found.
     741             :  * Exit with a norm 0 vector if one such is found.
     742             :  * If base == 1 and norm 0 is obtained, returns [H~*G*H,H] where
     743             :  * the 1st column of H is a norm 0 vector */
     744             : static GEN
     745       44100 : qftriv(GEN G, GEN R, long base)
     746             : {
     747       44100 :   long n = lg(G)-1, i;
     748             :   GEN s, H;
     749             : 
     750             :   /* case 1: A basis vector is isotropic */
     751      141015 :   for (i = 1; i <= n; i++)
     752      115617 :     if (!signe(gcoeff(G,i,i)))
     753             :     {
     754       18702 :       if (!base) return col_ei(n,i);
     755       11052 :       H = matid(n); swap(gel(H,1), gel(H,i));
     756       11052 :       return mkvec2(qf_apply_tau(G,1,i),H);
     757             :     }
     758             :   /* case 2: G has a block +- [1,0;0,-1] on the diagonal */
     759       84514 :   for (i = 2; i <= n; i++)
     760       62936 :     if (!signe(gcoeff(G,i-1,i)) && both_pm1(gcoeff(G,i-1,i-1),gcoeff(G,i,i)))
     761             :     {
     762        3820 :       s = col_ei(n,i); gel(s,i-1) = gen_m1;
     763        3820 :       if (!base) return s;
     764        2972 :       H = matid(n); gel(H,i) = gel(H,1); gel(H,1) = s;
     765        2972 :       return mkvec2(qf_apply_ZM(G,H),H);
     766             :     }
     767       21578 :   if (!R) return G; /* fail */
     768             :   /* case 3: a principal minor is 0 */
     769         563 :   s = ZM_ker(principal_minor(G, itos(R)));
     770         563 :   s = vecextend(Q_primpart(gel(s,1)), n);
     771         563 :   if (!base) return s;
     772         290 :   H = completebasis(s, 0);
     773         290 :   gel(H,n) = ZC_neg(gel(H,1)); gel(H,1) = s;
     774         290 :   return mkvec2(qf_apply_ZM(G,H),H);
     775             : }
     776             : 
     777             : /* p a prime number, G 3x3 symmetric. Finds X!=0 such that X^t G X = 0 mod p.
     778             :  * Allow returning a shorter X: to be completed with 0s. */
     779             : static GEN
     780       18053 : qfsolvemodp(GEN G, GEN p)
     781             : {
     782             :   GEN a,b,c,d,e,f, v1,v2,v3,v4,v5, x1,x2,x3,N1,N2,N3,s,r;
     783             : 
     784             :   /* principal_minor(G,3) = [a,b,d; b,c,e; d,e,f] */
     785       18053 :   a = modii(gcoeff(G,1,1), p);
     786       18053 :   if (!signe(a)) return mkcol(gen_1);
     787       15272 :   v1 = a;
     788       15272 :   b = modii(gcoeff(G,1,2), p);
     789       15272 :   c = modii(gcoeff(G,2,2), p);
     790       15272 :   v2 = modii(subii(mulii(a,c), sqri(b)), p);
     791       15272 :   if (!signe(v2)) return mkcol2(Fp_neg(b,p), a);
     792       12276 :   d = modii(gcoeff(G,1,3), p);
     793       12276 :   e = modii(gcoeff(G,2,3), p);
     794       12276 :   f = modii(gcoeff(G,3,3), p);
     795       12276 :   v4 = modii(subii(mulii(c,d), mulii(e,b)), p);
     796       12276 :   v5 = modii(subii(mulii(a,e), mulii(d,b)), p);
     797       12276 :   v3 = subii(mulii(v2,f), addii(mulii(v4,d), mulii(v5,e))); /* det(G) */
     798       12276 :   v3 = modii(v3, p);
     799       12276 :   N1 =  Fp_neg(v2,  p);
     800       12276 :   x3 = mkcol3(v4, v5, N1);
     801       12276 :   if (!signe(v3)) return x3;
     802             : 
     803             :   /* now, solve in dimension 3... reduction to the diagonal case: */
     804       10618 :   x1 = mkcol3(gen_1, gen_0, gen_0);
     805       10618 :   x2 = mkcol3(negi(b), a, gen_0);
     806       10618 :   if (kronecker(N1,p) == 1) return ZC_lincomb(Fp_sqrt(N1,p),gen_1,x1,x2);
     807        4256 :   N2 = Fp_div(Fp_neg(v3,p), v1, p);
     808        4256 :   if (kronecker(N2,p) == 1) return ZC_lincomb(Fp_sqrt(N2,p),gen_1,x2,x3);
     809        2129 :   N3 = Fp_mul(v2, N2, p);
     810        2129 :   if (kronecker(N3,p) == 1) return ZC_lincomb(Fp_sqrt(N3,p),gen_1,x1,x3);
     811        1065 :   r = gen_1;
     812             :   for(;;)
     813             :   {
     814        2046 :     s = Fp_sub(gen_1, Fp_mul(N1,Fp_sqr(r,p),p), p);
     815        2046 :     if (kronecker(s, p) <= 0) break;
     816         981 :     r = randomi(p);
     817             :   }
     818        1065 :   s = Fp_sqrt(Fp_div(s,N3,p), p);
     819        1065 :   return ZC_add(x1, ZC_lincomb(r,s,x2,x3));
     820             : }
     821             : 
     822             : /* Given a square rational matrix G of dimension n >= 1, solves over Z the
     823             :  * quadratic equation X^tGX = 0. The solution is a t_VEC (a solution) or a
     824             :  * t_MAT (totally isotropic subspace). If no solution exists, returns an
     825             :  * integer: a prime p (no local solution at p), or -1 (no real solution), or
     826             :  * -2 (n = 2 and -deg(G) not a square). */
     827             : static  GEN
     828       12355 : qfsolve_i(GEN G)
     829             : {
     830             :   GEN M, signG, Min, U, G1, M1, G2, M2, solG2, P, E;
     831             :   GEN solG1, sol, Q, d, dQ, detG2, fam2detG;
     832             :   long n, np, codim, dim;
     833             :   int fail;
     834             : 
     835       12355 :   if (typ(G) != t_MAT) pari_err_TYPE("qfsolve", G);
     836       12355 :   n = lg(G)-1;
     837       12355 :   if (n == 0) pari_err_DOMAIN("qfsolve", "dimension" , "=", gen_0, G);
     838       12355 :   if (n != nbrows(G)) pari_err_DIM("qfsolve");
     839       12355 :   G = Q_primpart(G); RgM_check_ZM(G, "qfsolve");
     840       12355 :   check_symmetric(G);
     841             : 
     842             :   /* Trivial case: det = 0 */
     843       12348 :   d = ZM_det(G);
     844       12348 :   if (!signe(d))
     845             :   {
     846           7 :     if (n == 1) return mkcol(gen_1);
     847           0 :     sol = ZM_ker(G);
     848           0 :     if (lg(sol) == 2) sol = gel(sol,1);
     849           0 :     return sol;
     850             :   }
     851             : 
     852             :   /* Small dimension: n <= 2 */
     853       12341 :   if (n == 1) return gen_m1;
     854       12334 :   if (n == 2)
     855             :   {
     856          21 :     GEN t, a =  gcoeff(G,1,1);
     857          21 :     if (!signe(a)) return mkcol2(gen_1, gen_0);
     858          14 :     if (signe(d) > 0) return gen_m1; /* no real solution */
     859          14 :     if (!Z_issquareall(negi(d), &t)) return gen_m2;
     860           7 :     return mkcol2(subii(t,gcoeff(G,1,2)), a);
     861             :   }
     862             : 
     863             :   /* 1st reduction of the coefficients of G */
     864       12313 :   M = qflllgram_indef(G,0,&fail);
     865       12313 :   if (typ(M) == t_COL) return M;
     866       11984 :   G = gel(M,1);
     867       11984 :   M = gel(M,2);
     868             : 
     869             :   /* real solubility */
     870       11984 :   signG = ZV_to_zv(qfsign(G));
     871             :   {
     872       11984 :     long r =  signG[1], s = signG[2];
     873       11984 :     if (!r || !s) return gen_m1;
     874       11914 :     if (r < s) { G = ZM_neg(G); signG = mkvecsmall2(s,r);  }
     875             :   }
     876             : 
     877             :   /* factorization of the determinant */
     878       11914 :   fam2detG = absZ_factor(d);
     879       11914 :   P = gel(fam2detG,1);
     880       11914 :   E = ZV_to_zv(gel(fam2detG,2));
     881             :   /* P,E = factor(|det(G)|) */
     882             : 
     883             :   /* Minimization and local solubility */
     884       11914 :   Min = qfminimize_fact(G, P, E, 1);
     885       11914 :   if (typ(Min) == t_INT) return Min;
     886             : 
     887       11431 :   M = QM_mul(M, gel(Min,2));
     888       11431 :   G = gel(Min,1);
     889       11431 :   P = gel(Min,3);
     890       11431 :   E = gel(Min,4);
     891             :   /* P,E = factor(|det(G))| */
     892             : 
     893             :   /* Now, we know that local solutions exist (except maybe at 2 if n==4)
     894             :    * if n==3, det(G) = +-1
     895             :    * if n==4, or n is odd, det(G) is squarefree.
     896             :    * if n>=6, det(G) has all its valuations <=2. */
     897             : 
     898             :   /* Reduction of G and search for trivial solutions. */
     899             :   /* When |det G|=1, such trivial solutions always exist. */
     900       11431 :   U = qflllgram_indef(G,0,&fail);
     901       11431 :   if (typ(U) == t_COL) return Q_primpart(RgM_RgC_mul(M,U));
     902        2989 :   G = gel(U,1);
     903        2989 :   M = QM_mul(M, gel(U,2));
     904             :   /* P,E = factor(|det(G))| */
     905             : 
     906             :   /* If n >= 6 is even, need to increment the dimension by 1 to suppress all
     907             :    * squares from det(G) */
     908        2989 :   np = lg(P)-1;
     909        2989 :   if (n < 6 || odd(n) || !np)
     910             :   {
     911        1603 :     codim = 0;
     912        1603 :     G1 = G;
     913        1603 :     M1 = NULL;
     914             :   }
     915             :   else
     916             :   {
     917             :     GEN aux;
     918             :     long i;
     919        1386 :     codim = 1; n++;
     920        1386 :     aux = gen_1;
     921        6524 :     for (i = 1; i <= np; i++)
     922        5138 :       if (E[i] == 2) { aux = mulii(aux, gel(P,i)); E[i] = 3; }
     923             :     /* aux = largest square divisor of d; choose sign(aux) so as to balance
     924             :      * the signature of G1 */
     925        1386 :     if (signG[1] > signG[2])
     926             :     {
     927         546 :       signG[2]++;
     928         546 :       aux = negi(aux);
     929             :     }
     930             :     else
     931         840 :       signG[1]++;
     932        1386 :     G1 = shallowmatconcat(diagonal_shallow(mkvec2(G,aux)));
     933             :     /* P,E = factor(|det G1|) */
     934        1386 :     Min = qfminimize_fact(G1, P, E, 1);
     935        1386 :     G1 = gel(Min,1);
     936        1386 :     M1 = gel(Min,2);
     937        1386 :     P = gel(Min,3);
     938        1386 :     E = gel(Min,4);
     939        1386 :     np = lg(P)-1;
     940             :   }
     941             : 
     942             :   /* now, d is squarefree */
     943        2989 :   if (!np) { G2 = G1; M2 = NULL; } /* |d| = 1 */
     944             :   else
     945             :   { /* |d| > 1: increment dimension by 2 */
     946        2723 :     GEN factdP, factdE, W, v = NULL;
     947             :     long i, lfactdP, lP;
     948        2723 :     codim += 2;
     949        2723 :     d = ZV_prod(P); /* d = abs(matdet(G1)); */
     950        2723 :     if (odd(signG[2])) togglesign_safe(&d); /* d = matdet(G1); */
     951        2723 :     if (n == 4)
     952             :     { /* solubility at 2, the only remaining bad prime */
     953         700 :       v = det_minors(G1);
     954         700 :       if (Mod8(d) == 1 && witt(v, gen_2) == 1) return gen_2;
     955             :     }
     956        2688 :     P = shallowconcat(mpodd(d)? mkvec2(NULL,gen_2): mkvec(NULL), P);
     957             :     /* build a binary quadratic form with given Witt invariants */
     958        2688 :     lP = lg(P); W = const_vecsmall(lP-1, 0);
     959             :     /* choose signature of Q (real invariant and sign of the discriminant) */
     960        2688 :     dQ = absi(d);
     961        2688 :     if (signG[1] > signG[2]) togglesign_safe(&dQ); /* signQ = [2,0]; */
     962        2688 :     if (n == 4 && Mod4(dQ) != 1) dQ = shifti(dQ,2);
     963        2688 :     if (n >= 5) dQ = shifti(dQ,3);
     964             : 
     965             :     /* p-adic invariants */
     966        2688 :     if (n == 4)
     967             :     {
     968         665 :       togglesign(gel(v,2));
     969         665 :       togglesign(gel(v,4)); /* v = det_minors(-G1) */
     970        2667 :       for (i = 3; i < lP; i++) W[i] = witt(v, gel(P,i)) < 0;
     971             :     }
     972             :     else
     973             :     {
     974        2023 :       long s = signe(dQ) == signe(d)? 1: -1;
     975             :       GEN t;
     976        2023 :       if (odd((n-3)/2)) s = -s;
     977        2023 :       t = s > 0? utoipos(8): utoineg(8);
     978        6083 :       for (i = 3; i < lP; i++) W[i] = hilbertii(t, gel(P,i), gel(P,i)) > 0;
     979             :     }
     980        2688 :     W[2] = Flv_sum(W, 2); /* for p = 2, use product formula */
     981             : 
     982             :     /* Construction of the 2-class group of discriminant dQ until some product
     983             :      * of the generators gives the desired invariants. */
     984        2688 :     factdP = vecsplice(P, 1); lfactdP =  lg(factdP);
     985        2688 :     factdE = cgetg(lfactdP, t_VECSMALL);
     986       11438 :     for (i = 1; i < lfactdP; i++) factdE[i] = Z_pval(dQ, gel(factdP,i));
     987        2688 :     factdE[1]++;
     988             :     /* factdP,factdE = factor(2|dQ|), P = factor(-2|dQ|)[,1] */
     989        2688 :     Q = quadclass2(dQ, factdP,factdE, P, W, n == 4);
     990             :     /* Build a form of dim=n+2 potentially unimodular */
     991        2688 :     G2 = shallowmatconcat(diagonal_shallow(mkvec2(G1,ZM_neg(Q))));
     992             :     /* Minimization of G2 */
     993        2688 :     detG2 = mulii(d, ZM_det(Q));
     994       11438 :     for (i = 1; i < lfactdP; i++) factdE[i] = Z_pval(detG2, gel(factdP,i));
     995             :     /* factdP,factdE = factor(|det G2|) */
     996        2688 :     Min = qfminimize_fact(G2, factdP,factdE,1);
     997        2688 :     M2 = gel(Min,2);
     998        2688 :     G2 = gel(Min,1);
     999             :   }
    1000             :   /* |det(G2)| = 1, find a totally isotropic subspace for G2 */
    1001        2954 :   solG2 = qflllgram_indefgoon(G2);
    1002             :   /* G2 must have a subspace of solutions of dimension > codim */
    1003        2954 :   dim = codim+1;
    1004        7280 :   while(gequal0(principal_minor(gel(solG2,1), dim))) dim ++;
    1005        2954 :   if (dim == codim+1)
    1006           7 :     pari_err_IMPL("qfsolve, dim >= 10");
    1007        2947 :   solG2 = vecslice(gel(solG2,2), 1, dim-1);
    1008             : 
    1009        2947 :   if (!M2) solG1 = solG2;
    1010             :   else
    1011             :   { /* solution of G1 is simultaneously in solG2 and x[n+1] = x[n+2] = 0*/
    1012             :     GEN K;
    1013        2688 :     solG1 = QM_mul(M2,solG2);
    1014        2688 :     K = QM_ker(rowslice(solG1,n+1,n+2));
    1015        2688 :     solG1 = QM_mul(rowslice(solG1,1,n), K);
    1016             :   }
    1017        2947 :   if (!M1) sol = solG1;
    1018             :   else
    1019             :   { /* solution of G1 is simultaneously in solG2 and x[n] = 0 */
    1020             :     GEN K;
    1021        1386 :     sol = QM_mul(M1,solG1);
    1022        1386 :     K = QM_ker(rowslice(sol,n,n));
    1023        1386 :     sol = QM_mul(rowslice(sol,1,n-1), K);
    1024             :   }
    1025        2947 :   sol = Q_primpart(QM_mul(M, sol));
    1026        2947 :   if (lg(sol) == 2) sol = gel(sol,1);
    1027        2947 :   return sol;
    1028             : }
    1029             : GEN
    1030       12355 : qfsolve(GEN G)
    1031       12355 : { pari_sp av = avma; return gerepilecopy(av, qfsolve_i(G)); }
    1032             : 
    1033             : /* G is a symmetric 3x3 matrix, and sol a solution of sol~*G*sol=0.
    1034             :  * Returns a parametrization of the solutions with the good invariants,
    1035             :  * as a 3x3 matrix, where each line contains the coefficients of each of the 3
    1036             :  * quadratic forms. If fl!=0, the fl-th form is reduced. */
    1037             : GEN
    1038        6993 : qfparam(GEN G, GEN sol, long fl)
    1039             : {
    1040        6993 :   pari_sp av = avma;
    1041             :   GEN U, G1, G2, a, b, c, d, e;
    1042        6993 :   long n, tx = typ(sol);
    1043             : 
    1044        6993 :   if (typ(G) != t_MAT) pari_err_TYPE("qfsolve", G);
    1045        6993 :   if (!is_vec_t(tx)) pari_err_TYPE("qfsolve", G);
    1046        6993 :   if (tx == t_VEC) sol = shallowtrans(sol);
    1047        6993 :   n = lg(G)-1;
    1048        6993 :   if (n == 0) pari_err_DOMAIN("qfsolve", "dimension" , "=", gen_0, G);
    1049        6993 :   if (n != nbrows(G) || n != 3 || lg(sol) != 4) pari_err_DIM("qfsolve");
    1050        6993 :   G = Q_primpart(G); RgM_check_ZM(G,"qfsolve");
    1051        6993 :   check_symmetric(G);
    1052        6993 :   sol = Q_primpart(sol); RgV_check_ZV(sol,"qfsolve");
    1053             :   /* build U such that U[,3] = sol, and |det(U)| = 1 */
    1054        6993 :   U = completebasis(sol,1);
    1055        6993 :   G1 = qf_apply_ZM(G,U); /* G1 has a 0 at the bottom right corner */
    1056        6993 :   a = shifti(gcoeff(G1,1,2),1);
    1057        6993 :   b = shifti(negi(gcoeff(G1,1,3)),1);
    1058        6993 :   c = shifti(negi(gcoeff(G1,2,3)),1);
    1059        6993 :   d = gcoeff(G1,1,1);
    1060        6993 :   e = gcoeff(G1,2,2);
    1061        6993 :   G2 = mkmat3(mkcol3(b,gen_0,d), mkcol3(c,b,a), mkcol3(gen_0,c,e));
    1062        6993 :   sol = ZM_mul(U,G2);
    1063        6993 :   if (fl)
    1064             :   {
    1065        6972 :     GEN v = row(sol,fl);
    1066             :     int fail;
    1067        6972 :     a = gel(v,1);
    1068        6972 :     b = gmul2n(gel(v,2),-1);
    1069        6972 :     c = gel(v,3);
    1070        6972 :     U = qflllgram_indef(mkmat2(mkcol2(a,b),mkcol2(b,c)), 1, &fail);
    1071        6972 :     U = gel(U,2);
    1072        6972 :     a = gcoeff(U,1,1); b = gcoeff(U,1,2);
    1073        6972 :     c = gcoeff(U,2,1); d = gcoeff(U,2,2);
    1074        6972 :     U = mkmat3(mkcol3(sqri(a),mulii(a,c),sqri(c)),
    1075             :                mkcol3(shifti(mulii(a,b),1), addii(mulii(a,d),mulii(b,c)),
    1076             :                       shifti(mulii(c,d),1)),
    1077             :                mkcol3(sqri(b),mulii(b,d),sqri(d)));
    1078        6972 :     sol = ZM_mul(sol,U);
    1079             :   }
    1080        6993 :   return gerepileupto(av, sol);
    1081             : }

Generated by: LCOV version 1.13