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 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 - modules - thue.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.8.0 lcov report (development 19352-1b11b25) Lines: 875 932 93.9 %
Date: 2016-08-25 06:11:27 Functions: 58 59 98.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  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. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : 
      17             : /********************************************************************/
      18             : /**                                                                **/
      19             : /**             THUE EQUATION SOLVER (G. Hanrot)                   **/
      20             : /**                                                                **/
      21             : /********************************************************************/
      22             : /* In all the forthcoming remarks, "paper" designs the paper "Thue Equations of
      23             :  * High Degree", by Yu. Bilu and G. Hanrot, J. Number Theory (1996). The
      24             :  * numbering of the constants corresponds to Hanrot's thesis rather than to the
      25             :  * paper. See also
      26             :  * "Solving Thue equations without the full unit group", Math. Comp. (2000) */
      27             : 
      28             : /* Check whether tnf is a valid structure */
      29             : static int
      30         700 : checktnf(GEN tnf)
      31             : {
      32         700 :   long l = lg(tnf);
      33             :   GEN v;
      34         700 :   if (typ(tnf)!=t_VEC || (l!=8 && l!=4)) return 0;
      35         343 :   v = gel(tnf,1);
      36         343 :   if (typ(v) != t_VEC || lg(v) != 4) return 0;
      37         343 :   if (l == 4) return 1; /* s=0 */
      38             : 
      39         175 :   (void)checkbnf(gel(tnf,2));
      40         350 :   return (typ(gel(tnf,3)) == t_COL
      41         175 :        && typ(gel(tnf,4)) == t_COL
      42         175 :        && typ(gel(tnf,5)) == t_MAT
      43         175 :        && typ(gel(tnf,6)) == t_MAT
      44         350 :        && typ(gel(tnf,7)) == t_VEC);
      45             : }
      46             : 
      47             : /* Compensates rounding errors for computation/display of the constants.
      48             :  * Round up if dir > 0, down otherwise */
      49             : static GEN
      50        8285 : myround(GEN x, long dir)
      51             : {
      52        8285 :   GEN eps = powis(stoi(dir > 0? 10: -10), -10);
      53        8285 :   return gmul(x, gadd(gen_1, eps));
      54             : }
      55             : 
      56             : /* v a t_VEC/t_VEC */
      57             : static GEN
      58        4016 : vecmax_shallow(GEN v) { return gel(v, vecindexmax(v)); }
      59             : 
      60             : static GEN
      61         267 : tnf_get_roots(GEN poly, long prec, long S, long T)
      62             : {
      63         267 :   GEN R0 = QX_complex_roots(poly, prec), R = cgetg(lg(R0), t_COL);
      64             :   long k;
      65             : 
      66         267 :   for (k=1; k<=S; k++) gel(R,k) = gel(R0,k);
      67             :   /* swap roots to get the usual order */
      68         463 :   for (k=1; k<=T; k++)
      69             :   {
      70         196 :     gel(R,k+S)  = gel(R0,2*k+S-1);
      71         196 :     gel(R,k+S+T)= gel(R0,2*k+S);
      72             :   }
      73         267 :   return R;
      74             : }
      75             : 
      76             : /* Computation of the logarithmic height of x (given by embeddings) */
      77             : static GEN
      78        4335 : LogHeight(GEN x, long prec)
      79             : {
      80        4335 :   pari_sp av = avma;
      81        4335 :   long i, n = lg(x)-1;
      82        4335 :   GEN LH = gen_1;
      83       18376 :   for (i=1; i<=n; i++)
      84             :   {
      85       14041 :     GEN t = gabs(gel(x,i), prec);
      86       14041 :     if (gcmpgs(t,1) > 0) LH = gmul(LH, t);
      87             :   }
      88        4335 :   return gerepileupto(av, gdivgs(glog(LH,prec), n));
      89             : }
      90             : 
      91             : /* |x|^(1/n), x t_INT */
      92             : static GEN
      93          91 : absisqrtn(GEN x, long n, long prec)
      94          91 : { GEN r = itor(x,prec); setabssign(r); return sqrtnr(r, n); }
      95             : 
      96             : static GEN
      97        4212 : get_emb(GEN x, GEN r)
      98             : {
      99        4212 :   long l = lg(r), i;
     100             :   GEN y;
     101             : 
     102        4212 :   if (typ(x) == t_INT) return const_col(l-1, x);
     103        4191 :   y = cgetg(l, t_COL);
     104       17549 :   for (i=1; i<l; i++)
     105             :   {
     106       13430 :     GEN e = poleval(x, gel(r,i));
     107       13430 :     if (gequal0(e) || (typ(e) != t_INT && precision(e) <= LOWDEFAULTPREC ))
     108          72 :       return NULL;
     109       13358 :     gel(y,i) = e;
     110             :   }
     111        4119 :   return y;
     112             : }
     113             : 
     114             : /* Computation of the conjugates (sigma_i(v_j)), and log. heights, of elts of v */
     115             : static GEN
     116         623 : Conj_LH(GEN v, GEN *H, GEN r, long prec)
     117             : {
     118         623 :   long j, l = lg(v);
     119         623 :   GEN e, M = cgetg(l,t_MAT);
     120             : 
     121         623 :   (*H) = cgetg(l,t_COL);
     122        4763 :   for (j = 1; j < l; j++)
     123             :   {
     124        4212 :     if (! (e = get_emb(gel(v,j), r)) ) return NULL; /* FAIL */
     125        4140 :     gel(M,j) = e;
     126        4140 :     gel(*H,j) = LogHeight(e, prec);
     127             :   }
     128         551 :   return M;
     129             : }
     130             : 
     131             : /* Computation of M, its inverse A and precision check (see paper) */
     132             : static GEN
     133         195 : T_A_Matrices(GEN MatFU, long r, GEN *eps5, long prec)
     134             : {
     135             :   GEN A, p1, m1, IntM, nia, eps3, eps2;
     136         195 :   long e = prec2nbits(prec);
     137             : 
     138         195 :   m1 = matslice(MatFU, 1,r, 1,r); /* minor order r */
     139         195 :   m1 = glog(gabs(m1, prec), prec); /* HIGH accuracy */
     140         195 :   A = RgM_inv(m1); if (!A) pari_err_PREC("thue");
     141         195 :   IntM = RgM_Rg_add(RgM_mul(A,m1), gen_m1);
     142         195 :   IntM = gabs(IntM, 0);
     143             : 
     144         195 :   eps2 = gadd(vecmax(IntM), real2n(-e, LOWDEFAULTPREC)); /* t_REAL */
     145         195 :   nia = vecmax(gabs(A, 0));
     146             : 
     147             :   /* Check for the precision in matrix inversion. See paper, Lemma 2.4.2. */
     148         195 :   p1 = addrr(mulsr(r, gmul2n(nia, e)), eps2); /* t_REAL */
     149         195 :   if (expo(p1) < -2*r) pari_err_PREC("thue");
     150             : 
     151         195 :   p1 = addrr(mulsr(r, gmul2n(nia,-e)), eps2);
     152         195 :   eps3 = mulrr(mulsr(2*r*r,nia), p1);
     153         195 :   if (!signe(eps3))
     154         116 :     eps3 = real2n(expo(eps3), LOWDEFAULTPREC);
     155             :   else
     156          79 :     eps3 = myround(eps3, 1);
     157             : 
     158         195 :   if (DEBUGLEVEL>1) err_printf("epsilon_3 -> %Ps\n",eps3);
     159         195 :   *eps5 = mulur(r, eps3); return A;
     160             : }
     161             : 
     162             : /* find a few large primes such that p Z_K = P1 P2 P3 Q, whith f(Pi/p) = 1
     163             :  * From x - \alpha y = \prod u_i^b_i we will deduce 3 equations in F_p
     164             :  * in check_prinfo. Eliminating x,y we get a stringent condition on (b_i). */
     165             : static GEN
     166         195 : get_prime_info(GEN bnf)
     167             : {
     168         195 :   long n = 1, e = gexpo(bnf_get_reg(bnf)), nbp = e < 20? 1: 2;
     169         195 :   GEN L = cgetg(nbp+1, t_VEC), nf = checknf(bnf), fu = bnf_get_fu(bnf);
     170         195 :   GEN X = pol_x(nf_get_varn(nf));
     171             :   ulong p;
     172        2370 :   for(p = 2147483659UL; n <= nbp; p = unextprime(p+1))
     173             :   {
     174        2175 :     GEN PR, A, U, LP = idealprimedec_limit_f(bnf, utoipos(p), 1);
     175             :     long i;
     176        2175 :     if (lg(LP) < 4) continue;
     177         202 :     A = cgetg(5, t_VECSMALL);
     178         202 :     U = cgetg(4, t_VEC);
     179         202 :     PR = cgetg(4, t_VEC);
     180         808 :     for (i = 1; i <= 3; i++)
     181             :     {
     182         606 :       GEN modpr = zkmodprinit(nf, gel(LP,i));
     183         606 :       GEN a = nf_to_Fq(nf, X, modpr);
     184         606 :       GEN u = nfV_to_FqV(fu, nf, modpr);
     185         606 :       A[i] = itou(a);
     186         606 :       gel(U,i) = ZV_to_Flv(u,p);
     187         606 :       gel(PR,i) = modpr;
     188             :     }
     189         202 :     A[4] = p;
     190         202 :     gel(L,n++) = mkvec3(A,U,PR);
     191             :   }
     192         195 :   return L;
     193             : }
     194             : 
     195             : /* Performs basic computations concerning the equation.
     196             :  * Returns a "tnf" structure containing
     197             :  *  1) the polynomial
     198             :  *  2) the bnf (used to solve the norm equation)
     199             :  *  3) roots, with presumably enough precision
     200             :  *  4) The logarithmic heights of units
     201             :  *  5) The matrix of conjugates of units
     202             :  *  6) its inverse
     203             :  *  7) a few technical constants */
     204             : static GEN
     205         195 : inithue(GEN P, GEN bnf, long flag, long prec)
     206             : {
     207         195 :   GEN MatFU, x0, tnf, tmp, gpmin, dP, csts, ALH, eps5, ro, c1, c2, Ind = gen_1;
     208         195 :   long k,j, n = degpol(P);
     209             :   long s,t, prec_roots;
     210             : 
     211         195 :   if (!bnf)
     212             :   {
     213         182 :     bnf = Buchall(P, nf_FORCE, maxss(prec, DEFAULTPREC));
     214         182 :     if (flag) (void)bnfcertify(bnf);
     215             :     else
     216         154 :       Ind = floorr(mulru(bnf_get_reg(bnf), 5));
     217             :   }
     218             : 
     219         195 :   nf_get_sign(bnf_get_nf(bnf), &s, &t);
     220         195 :   prec_roots = prec;
     221             :   for(;;)
     222             :   {
     223         267 :     ro = tnf_get_roots(P, prec_roots, s, t);
     224         267 :     MatFU = Conj_LH(bnf_get_fu(bnf), &ALH, ro, prec);
     225         267 :     if (MatFU) break;
     226          72 :     prec_roots = precdbl(prec_roots);
     227          72 :     if (DEBUGLEVEL>1) pari_warn(warnprec, "inithue", prec_roots);
     228          72 :   }
     229             : 
     230         195 :   dP = ZX_deriv(P);
     231         195 :   c1 = NULL; /* min |P'(r_i)|, i <= s */
     232         591 :   for (k=1; k<=s; k++)
     233             :   {
     234         396 :     tmp = gabs(poleval(dP,gel(ro,k)),prec);
     235         396 :     if (!c1 || gcmp(tmp,c1) < 0) c1 = tmp;
     236             :   }
     237         195 :   c1 = gdiv(int2n(n-1), c1);
     238         195 :   c1 = gprec_w(myround(c1, 1), DEFAULTPREC);
     239             : 
     240         195 :   c2 = NULL; /* max |r_i - r_j|, i!=j */
     241         843 :   for (k=1; k<=n; k++)
     242        1485 :     for (j=k+1; j<=n; j++)
     243             :     {
     244         837 :       tmp = gabs(gsub(gel(ro,j),gel(ro,k)), prec);
     245         837 :       if (!c2 || gcmp(c2,tmp) > 0) c2 = tmp;
     246             :     }
     247         195 :   c2 = gprec_w(myround(c2, -1), DEFAULTPREC);
     248             : 
     249         195 :   if (t==0)
     250          90 :     x0 = real_1(DEFAULTPREC);
     251             :   else
     252             :   {
     253         105 :     gpmin = NULL; /* min |P'(r_i)|, i > s */
     254         231 :     for (k=1; k<=t; k++)
     255             :     {
     256         126 :       tmp = gabs(poleval(dP,gel(ro,s+k)), prec);
     257         126 :       if (!gpmin || gcmp(tmp,gpmin) < 0) gpmin = tmp;
     258             :     }
     259         105 :     gpmin = gprec_w(gpmin, DEFAULTPREC);
     260             : 
     261             :     /* Compute x0. See paper, Prop. 2.2.1 */
     262         105 :     x0 = gmul(gpmin, vecmax_shallow(gabs(imag_i(ro), prec)));
     263         105 :     x0 = sqrtnr(gdiv(int2n(n-1), x0), n);
     264             :   }
     265         195 :   if (DEBUGLEVEL>1)
     266           0 :     err_printf("c1 = %Ps\nc2 = %Ps\nIndice <= %Ps\n", c1, c2, Ind);
     267             : 
     268         195 :   ALH = gmul2n(ALH, 1);
     269         195 :   tnf = cgetg(8,t_VEC); csts = cgetg(9,t_VEC);
     270         195 :   gel(tnf,1) = P;
     271         195 :   gel(tnf,2) = bnf;
     272         195 :   gel(tnf,3) = ro;
     273         195 :   gel(tnf,4) = ALH;
     274         195 :   gel(tnf,5) = MatFU;
     275         195 :   gel(tnf,6) = T_A_Matrices(MatFU, s+t-1, &eps5, prec);
     276         195 :   gel(tnf,7) = csts;
     277         195 :   gel(csts,1) = c1; gel(csts,2) = c2;   gel(csts,3) = LogHeight(ro, prec);
     278         195 :   gel(csts,4) = x0; gel(csts,5) = eps5; gel(csts,6) = utoipos(prec);
     279         195 :   gel(csts,7) = Ind;
     280         195 :   gel(csts,8) = get_prime_info(bnf);
     281         195 :   return tnf;
     282             : }
     283             : 
     284             : typedef struct {
     285             :   GEN c10, c11, c13, c15, c91, bak, NE, Ind, hal, MatFU, divro, Hmu;
     286             :   GEN delta, lambda, inverrdelta, ro, Pi, Pi2;
     287             :   long r, iroot, deg;
     288             : } baker_s;
     289             : 
     290             : static void
     291        4086 : other_roots(long iroot, long *i1, long *i2)
     292             : {
     293        4086 :   switch (iroot) {
     294        1587 :     case 1: *i1=2; *i2=3; break;
     295        1176 :     case 2: *i1=1; *i2=3; break;
     296        1323 :    default: *i1=1; *i2=2; break;
     297             :   }
     298        4086 : }
     299             : /* low precision */
     300        4442 : static GEN abslog(GEN x) { return gabs(glog(gtofp(x,DEFAULTPREC),0), 0); }
     301             : 
     302             : /* Compute Baker's bound c9 and B_0, the bound for the b_i's. See Thm 2.3.1 */
     303             : static GEN
     304        3730 : Baker(baker_s *BS)
     305             : {
     306             :   GEN tmp, B0, hb0, c9, Indc11;
     307             :   long i1, i2;
     308             : 
     309        3730 :   other_roots(BS->iroot, &i1,&i2);
     310             :   /* Compute a bound for the h_0 */
     311        3730 :   hb0 = gadd(gmul2n(BS->hal,2), gmul2n(gadd(BS->Hmu,mplog2(DEFAULTPREC)), 1));
     312        3730 :   tmp = gmul(BS->divro, gdiv(gel(BS->NE,i1), gel(BS->NE,i2)));
     313        3730 :   tmp = gmax(gen_1, abslog(tmp));
     314        3730 :   hb0 = gmax(hb0, gdiv(tmp, BS->bak));
     315        3730 :   c9 = gmul(BS->c91,hb0);
     316        3730 :   c9 = gprec_w(myround(c9, 1), DEFAULTPREC);
     317        3730 :   Indc11 = rtor(mulir(BS->Ind,BS->c11), DEFAULTPREC);
     318             :   /* Compute B0 according to Lemma 2.3.3 */
     319        3730 :   B0 = mulir(shifti(BS->Ind,1),
     320             :              divrr(addrr(mulrr(c9,mplog(divrr(mulir(BS->Ind, c9),BS->c10))),
     321             :                          mplog(Indc11)), BS->c10));
     322        3730 :   B0 = gmax(B0, dbltor(2.71828183));
     323        3730 :   B0 = gmax(B0, mulrr(divir(BS->Ind, BS->c10),
     324             :                       mplog(divrr(Indc11, BS->Pi2))));
     325             : 
     326        3730 :   if (DEBUGLEVEL>1) {
     327           0 :     err_printf("  B0  = %Ps\n",B0);
     328           0 :     err_printf("  Baker = %Ps\n",c9);
     329             :   }
     330        3730 :   return B0;
     331             : }
     332             : 
     333             : /* || x d ||, x t_REAL, d t_INT */
     334             : static GEN
     335        9365 : errnum(GEN x, GEN d)
     336             : {
     337        9365 :   GEN dx = mulir(d, x), D = subri(dx, roundr(dx));
     338        9365 :   setabssign(D); return D;
     339             : }
     340             : 
     341             : /* Try to reduce the bound through continued fractions; see paper. */
     342             : static int
     343        4609 : CF_1stPass(GEN *B0, GEN kappa, baker_s *BS)
     344             : {
     345        4609 :   GEN a, b, q, ql, qd, l0, denbound = mulri(*B0, kappa);
     346             : 
     347        4609 :   if (cmprr(mulrr(dbltor(0.1),sqrr(denbound)), BS->inverrdelta) > 0)
     348           0 :     return -1;
     349             : 
     350        4609 :   q = denom( bestappr(BS->delta, denbound) );
     351        4609 :   qd = errnum(BS->delta, q);
     352        4609 :   ql = errnum(BS->lambda,q);
     353             : 
     354        4609 :   l0 = subrr(ql, addrr(mulrr(qd, *B0), divri(dbltor(0.1),kappa)));
     355        4609 :   if (signe(l0) <= 0) return 0;
     356             : 
     357        3857 :   if (BS->r > 1) {
     358        3129 :     a = BS->c15; b = BS->c13;
     359             :   }
     360             :   else {
     361         728 :     a = BS->c11; b = BS->c10;
     362         728 :     l0 = mulrr(l0, Pi2n(1, DEFAULTPREC));
     363             :   }
     364        3857 :   *B0 = divrr(mplog(divrr(mulir(q,a), l0)), b);
     365        3857 :   if (DEBUGLEVEL>1) err_printf("    B0 -> %Ps\n",*B0);
     366        3857 :   return 1;
     367             : }
     368             : 
     369             : static void
     370       10194 : get_B0Bx(baker_s *BS, GEN l0, GEN *B0, GEN *Bx)
     371             : {
     372       10194 :   GEN t = divrr(mulir(BS->Ind, BS->c15), l0);
     373       10194 :   *B0 = divrr(mulir(BS->Ind, mplog(t)), BS->c13);
     374       10194 :   *Bx = sqrtnr(shiftr(t,1), BS->deg);
     375       10194 : }
     376             : 
     377             : static int
     378       18542 : LLL_1stPass(GEN *pB0, GEN kappa, baker_s *BS, GEN *pBx)
     379             : {
     380       18542 :   GEN B0 = *pB0, Bx = *pBx, lllmat, C, l0, l1, triv;
     381             :   long e;
     382             : 
     383       18542 :   C = grndtoi(mulir(mulii(BS->Ind, kappa),
     384             :                     gpow(B0, dbltor(2.2), DEFAULTPREC)), &e);
     385             : 
     386       18542 :   if (DEBUGLEVEL > 1) err_printf("C (bitsize) : %d\n", expi(C));
     387       18542 :   lllmat = matid(3);
     388       18542 :   if (cmpri(B0, BS->Ind) > 0)
     389             :   {
     390       13197 :     gcoeff(lllmat, 1, 1) = grndtoi(divri(B0, BS->Ind), &e);
     391       13197 :     triv = shiftr(sqrr(B0), 1);
     392             :   }
     393             :   else
     394        5345 :     triv = addir(sqri(BS->Ind), sqrr(B0));
     395             : 
     396       18542 :   gcoeff(lllmat, 3, 1) = grndtoi(negr(mulir(C, BS->lambda)), &e);
     397       18542 :   if (e >= 0) return -1;
     398       18529 :   gcoeff(lllmat, 3, 2) = grndtoi(negr(mulir(C, BS->delta)), &e);
     399       18529 :   if (e >= 0) return -1;
     400       18529 :   gcoeff(lllmat, 3, 3) = C;
     401       18529 :   lllmat = ZM_lll(lllmat, 0.99, LLL_IM|LLL_INPLACE);
     402             : 
     403       18529 :   l0 = gnorml2(gel(lllmat,1));
     404       18529 :   l0 = subrr(divir(l0, dbltor(1.8262)), triv); /* delta = 0.99 */
     405       18529 :   if (signe(l0) <= 0) return 0;
     406             : 
     407       12788 :   l1 = shiftr(addri(shiftr(B0,1), BS->Ind), -1);
     408       12788 :   l0 = divri(subrr(sqrtr(l0), l1), C);
     409             : 
     410       12788 :   if (signe(l0) <= 0) return 0;
     411             : 
     412       10047 :   get_B0Bx(BS, l0, &B0, &Bx);
     413       10047 :   if (DEBUGLEVEL>=2)
     414             :   {
     415           0 :     err_printf("LLL_First_Pass successful\n");
     416           0 :     err_printf("B0 -> %Ps\n", B0);
     417           0 :     err_printf("x <= %Ps\n", Bx);
     418             :   }
     419       10047 :   *pB0 = B0; *pBx = Bx; return 1;
     420             : }
     421             : 
     422             : /* add solution (x,y) if not already known */
     423             : static void
     424         637 : add_sol(GEN *pS, GEN x, GEN y)
     425         637 : { *pS = shallowconcat(*pS, mkvec(mkvec2(x,y))); }
     426             : /* z = P(p,q), d = deg P, |z| = |rhs|. Check signs and (possibly)
     427             :  * add solutions (p,q), (-p,-q) */
     428             : static void
     429         161 : add_pm(GEN *pS, GEN p, GEN q, GEN z, long d, GEN rhs)
     430             : {
     431         161 :   if (signe(z) == signe(rhs))
     432             :   {
     433         112 :     add_sol(pS, p, q);
     434         112 :     if (!odd(d)) add_sol(pS, negi(p), negi(q));
     435             :   }
     436             :   else
     437          49 :     if (odd(d))  add_sol(pS, negi(p), negi(q));
     438         161 : }
     439             : 
     440             : /* Check whether a potential solution is a true solution. Return 0 if
     441             :  * truncation error (increase precision) */
     442             : static int
     443          77 : CheckSol(GEN *pS, GEN z1, GEN z2, GEN P, GEN rhs, GEN ro)
     444             : {
     445          77 :   GEN x, y, ro1 = gel(ro,1), ro2 = gel(ro,2);
     446             :   long e;
     447             : 
     448          77 :   y = grndtoi(real_i(gdiv(gsub(z2,z1), gsub(ro1,ro2))), &e);
     449          77 :   if (e > 0) return 0;
     450          77 :   if (!signe(y)) return 1; /* y = 0 taken care of in SmallSols */
     451          70 :   x = gadd(z1, gmul(ro1, y));
     452          70 :   x = grndtoi(real_i(x), &e);
     453          70 :   if (e > 0) return 0;
     454          70 :   if (e <= -13)
     455             :   { /* y != 0 and rhs != 0; check whether P(x,y) = rhs or P(-x,-y) = rhs */
     456          70 :     GEN z = poleval(ZX_rescale(P,y),x);
     457          70 :     if (absequalii(z, rhs)) add_pm(pS, x,y, z, degpol(P), rhs);
     458             :   }
     459          70 :   return 1;
     460             : }
     461             : 
     462             : static const long EXPO1 = 7;
     463             : static int
     464       30392 : round_to_b(GEN v, long B, long b, GEN Delta2, long i1, GEN L)
     465             : {
     466       30392 :   long i, l = lg(v);
     467       30392 :   if (!b)
     468             :   {
     469        2423 :     for (i = 1; i < l; i++)
     470             :     {
     471             :       long c;
     472        2148 :       if (i == i1)
     473        1152 :         c = 0;
     474             :       else
     475             :       {
     476         996 :         GEN d = gneg(gel(L,i));
     477             :         long e;
     478         996 :         d = grndtoi(d,&e);
     479        1941 :         if (e > -EXPO1 || is_bigint(d)) return 0;
     480          51 :         c = itos(d); if (labs(c) > B) return 0;
     481             :       }
     482        1203 :       v[i] = c;
     483             :     }
     484             :   }
     485             :   else
     486             :   {
     487       58234 :     for (i = 1; i < l; i++)
     488             :     {
     489             :       long c;
     490       37268 :       if (i == i1)
     491       28928 :         c = b;
     492             :       else
     493             :       {
     494        8340 :         GEN d = gsub(gmulgs(gel(Delta2,i), b), gel(L,i));
     495             :         long e;
     496        8340 :         d = grndtoi(d,&e);
     497       16546 :         if (e > -EXPO1 || is_bigint(d)) return 0;
     498         134 :         c = itos(d); if (labs(c) > B) return 0;
     499             :       }
     500       29062 :       v[i] = c;
     501             :     }
     502             :   }
     503       21241 :   return 1;
     504             : }
     505             : 
     506             : /* mu \prod U[i]^b[i] */
     507             : static ulong
     508       63723 : Fl_factorback(ulong mu, GEN U, GEN b, ulong p)
     509             : {
     510       63723 :   long i, l = lg(U);
     511       63723 :   ulong r = mu;
     512      128001 :   for (i = 1; i < l; i++)
     513             :   {
     514       64278 :     long c = b[i];
     515       64278 :     ulong u = U[i];
     516       64278 :     if (!c) continue;
     517       63048 :     if (c < 0) { u = Fl_inv(u,p); c = -c; }
     518       63048 :     r = Fl_mul(r, Fl_powu(u,c,p), p);
     519             :   }
     520       63723 :   return r;
     521             : }
     522             : 
     523             : /* x - alpha y = \pm mu \prod \mu_i^{b_i}. Reduce mod 3 distinct primes of
     524             :  * degree 1 above the same p, and eliminate x,y => drastic conditions on b_i */
     525             : static int
     526       21241 : check_pr(GEN bi, GEN Lmu, GEN L)
     527             : {
     528       21241 :   GEN A = gel(L,1), U = gel(L,2);
     529       21241 :   ulong a = A[1], b = A[2], c = A[3], p = A[4];
     530       21241 :   ulong r = Fl_mul(Fl_sub(c,b,p), Fl_factorback(Lmu[1],gel(U,1),bi, p), p);
     531       21241 :   ulong s = Fl_mul(Fl_sub(a,c,p), Fl_factorback(Lmu[2],gel(U,2),bi, p), p);
     532       21241 :   ulong t = Fl_mul(Fl_sub(b,a,p), Fl_factorback(Lmu[3],gel(U,3),bi, p), p);
     533       21241 :   return Fl_add(Fl_add(r,s,p),t,p) == 0;
     534             : }
     535             : static int
     536       21241 : check_prinfo(GEN b, GEN Lmu, GEN prinfo)
     537             : {
     538             :   long i;
     539       21318 :   for (i = 1; i < lg(prinfo); i++)
     540       21241 :     if (!check_pr(b, gel(Lmu,i), gel(prinfo,i))) return 0;
     541          77 :   return 1;
     542             : }
     543             : /* For each possible value of b_i1, compute the b_i's
     544             : * and 2 conjugates of z = x - alpha y. Then check. */
     545             : static int
     546        1220 : TrySol(GEN *pS, GEN B0, long i1, GEN Delta2, GEN Lambda, GEN ro,
     547             :        GEN Lmu, GEN NE, GEN MatFU, GEN prinfo, GEN P, GEN rhs)
     548             : {
     549        1220 :   long bi1, i, B = itos(gceil(B0)), l = lg(Delta2);
     550        1220 :   GEN b = cgetg(l,t_VECSMALL), L = cgetg(l,t_VEC);
     551             : 
     552        3436 :   for (i = 1; i < l; i++)
     553             :   {
     554        2216 :     if (i == i1)
     555        1220 :       gel(L,i) = gen_0;
     556             :     else
     557             :     {
     558         996 :       GEN delta = gel(Delta2,i);
     559         996 :       gel(L, i) = gsub(gmul(delta,gel(Lambda,i1)), gel(Lambda,i));
     560             :     }
     561             :   }
     562       31612 :   for (bi1 = -B; bi1 <= B; bi1++)
     563             :   {
     564             :     GEN z1, z2;
     565       30392 :     if (!round_to_b(b, B, bi1, Delta2, i1, L)) continue;
     566       21241 :     if (!check_prinfo(b, Lmu, prinfo)) continue;
     567          77 :     z1 = gel(NE,1);
     568          77 :     z2 = gel(NE,2);
     569         182 :     for (i = 1; i < l; i++)
     570             :     {
     571         105 :       z1 = gmul(z1, gpowgs(gcoeff(MatFU,1,i), b[i]));
     572         105 :       z2 = gmul(z2, gpowgs(gcoeff(MatFU,2,i), b[i]));
     573             :     }
     574          77 :     if (!CheckSol(pS, z1,z2,P,rhs,ro)) return 0;
     575             :   }
     576        1220 :   return 1;
     577             : }
     578             : 
     579             : /* find q1,q2,q3 st q1 b + q2 c + q3 ~ 0 */
     580             : static GEN
     581         147 : GuessQi(GEN b, GEN c, GEN *eps)
     582             : {
     583         147 :   const long shift = 65;
     584             :   GEN Q, Lat;
     585             : 
     586         147 :   Lat = matid(3);
     587         147 :   gcoeff(Lat,3,1) = ground(gmul2n(b, shift));
     588         147 :   gcoeff(Lat,3,2) = ground(gmul2n(c, shift));
     589         147 :   gcoeff(Lat,3,3) = int2n(shift);
     590             : 
     591         147 :   Q = gel(lllint(Lat),1);
     592         147 :   if (gequal0(gel(Q,2))) return NULL; /* FAIL */
     593             : 
     594         147 :   *eps = gadd(gadd(gel(Q,3), gmul(gel(Q,1),b)), gmul(gel(Q,2),c));
     595         147 :   *eps = mpabs(*eps); return Q;
     596             : }
     597             : 
     598             : /* x a t_REAL */
     599             : static GEN
     600         323 : myfloor(GEN x) { return expo(x) > 30 ? ceil_safe(x): floorr(x); }
     601             : 
     602             : /* Check for not-so-small solutions. Return a t_REAL or NULL */
     603             : static GEN
     604         168 : MiddleSols(GEN *pS, GEN bound, GEN roo, GEN P, GEN rhs, long s, GEN c1)
     605             : {
     606             :   long j, k, nmax, d;
     607             :   GEN bndcf;
     608             : 
     609         168 :   if (expo(bound) < 0) return bound;
     610         161 :   d = degpol(P);
     611         161 :   bndcf = sqrtnr(shiftr(c1,1), d - 2);
     612         161 :   if (cmprr(bound, bndcf) < 0) return bound;
     613             :   /* divide by log2((1+sqrt(5))/2)
     614             :    * 1 + ==> ceil
     615             :    * 2 + ==> continued fraction is normalized if last entry is 1
     616             :    * 3 + ==> start at a0, not a1 */
     617         134 :   nmax = 3 + (long)(dbllog2(bound) * 1.44042009041256);
     618         134 :   bound = myfloor(bound);
     619             : 
     620         409 :   for (k = 1; k <= s; k++)
     621             :   {
     622         275 :     GEN ro = real_i(gel(roo,k)), t = gboundcf(ro, nmax);
     623             :     GEN pm1, qm1, p0, q0;
     624             : 
     625         275 :     pm1 = gen_0; p0 = gen_1;
     626         275 :     qm1 = gen_1; q0 = gen_0;
     627             : 
     628       10884 :     for (j = 1; j < lg(t); j++)
     629             :     {
     630             :       GEN p, q, z, Q, R;
     631             :       pari_sp av;
     632       10878 :       p = addii(mulii(p0, gel(t,j)), pm1); pm1 = p0; p0 = p;
     633       10878 :       q = addii(mulii(q0, gel(t,j)), qm1); qm1 = q0; q0 = q;
     634       10878 :       if (cmpii(q, bound) > 0) break;
     635       10609 :       if (DEBUGLEVEL >= 2) err_printf("Checking (+/- %Ps, +/- %Ps)\n",p, q);
     636       10609 :       av = avma;
     637       10609 :       z = poleval(ZX_rescale(P,q), p); /* = P(p/q) q^dep(P) */
     638       10609 :       Q = dvmdii(rhs, z, &R);
     639       10609 :       if (R != gen_0) { avma = av; continue; }
     640         196 :       setabssign(Q);
     641         196 :       if (Z_ispowerall(Q, d, &Q))
     642             :       {
     643          91 :         if (!is_pm1(Q)) { p = mulii(p, Q); q = mulii(q, Q); }
     644          91 :         add_pm(pS, p, q, z, d, rhs);
     645             :       }
     646             :     }
     647         275 :     if (j == lg(t))
     648             :     {
     649             :       long prec;
     650           6 :       if (j > nmax) pari_err_BUG("thue [short continued fraction]");
     651             :       /* the theoretical value is bit_prec = gexpo(ro)+1+log2(bound) */
     652           6 :       prec = precdbl(precision(ro));
     653           6 :       if (DEBUGLEVEL>1) pari_warn(warnprec,"thue",prec);
     654           6 :       roo = realroots(P, NULL, prec);
     655           6 :       if (lg(roo)-1 != s) pari_err_BUG("thue [realroots]");
     656           6 :       k--;
     657             :     }
     658             :   }
     659         134 :   return bndcf;
     660             : }
     661             : 
     662             : static void
     663      579978 : check_y_root(GEN *pS, GEN P, GEN Y)
     664             : {
     665      579978 :   GEN r = nfrootsQ(P);
     666             :   long j;
     667      580391 :   for (j = 1; j < lg(r); j++)
     668         413 :     if (typ(gel(r,j)) == t_INT) add_sol(pS, gel(r,j), Y);
     669      579978 : }
     670             : 
     671             : static void
     672      290045 : check_y(GEN *pS, GEN P, GEN poly, GEN Y, GEN rhs)
     673             : {
     674      290045 :   long j, l = lg(poly);
     675      290045 :   GEN Yn = Y;
     676      290045 :   gel(P, l-1) = gel(poly, l-1);
     677     1160082 :   for (j = l-2; j >= 2; j--)
     678             :   {
     679      870037 :     gel(P,j) = mulii(Yn, gel(poly,j));
     680      870037 :     if (j > 2) Yn = mulii(Yn, Y);
     681             :   }
     682      290045 :   gel(P,2) = subii(gel(P,2), rhs); /* P = poly(Y/y)*y^deg(poly) - rhs */
     683      290045 :   check_y_root(pS, P, Y);
     684      290045 : }
     685             : 
     686             : /* Check for solutions under a small bound (see paper) */
     687             : static GEN
     688         189 : SmallSols(GEN S, GEN x3, GEN poly, GEN rhs)
     689             : {
     690         189 :   pari_sp av = avma;
     691             :   GEN X, P, rhs2;
     692         189 :   long j, l = lg(poly), n = degpol(poly);
     693             :   ulong y, By;
     694             : 
     695         189 :   x3 = myfloor(x3);
     696             : 
     697         189 :   if (DEBUGLEVEL>1) err_printf("* Checking for small solutions <= %Ps\n", x3);
     698         189 :   if (lgefint(x3) > 3)
     699           0 :     pari_err_OVERFLOW(stack_sprintf("thue (SmallSols): y <= %Ps", x3));
     700         189 :   By = itou(x3);
     701             :   /* y = 0 first: solve X^n = rhs */
     702         189 :   if (odd(n))
     703             :   {
     704         147 :     if (Z_ispowerall(absi(rhs), n, &X))
     705          28 :       add_sol(&S, signe(rhs) > 0? X: negi(X), gen_0);
     706             :   }
     707          42 :   else if (signe(rhs) > 0 && Z_ispowerall(rhs, n, &X))
     708             :   {
     709          14 :     add_sol(&S, X, gen_0);
     710          14 :     add_sol(&S, negi(X), gen_0);
     711             :   }
     712         189 :   rhs2 = shifti(rhs,1);
     713             :   /* y != 0 */
     714         189 :   P = cgetg(l, t_POL); P[1] = poly[1];
     715      290122 :   for (y = 1; y <= By; y++)
     716             :   {
     717      289933 :     pari_sp av2 = avma;
     718      289933 :     long lS = lg(S);
     719      289933 :     GEN Y = utoipos(y);
     720             :     /* try y */
     721      289933 :     check_y(&S, P, poly, Y, rhs);
     722             :     /* try -y */
     723      289933 :     for (j = l-2; j >= 2; j -= 2) togglesign( gel(P,j) );
     724      289933 :     if (j == 0) gel(P,2) = subii(gel(P,2), rhs2);
     725      289933 :     check_y_root(&S, P, utoineg(y));
     726      289933 :     if (lS == lg(S)) { avma = av2; continue; } /* no solution found */
     727             : 
     728         140 :     if (gc_needed(av,1))
     729             :     {
     730           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"SmallSols");
     731           0 :       gerepileall(av, 2, &S, &rhs2);
     732           0 :       P = cgetg(l, t_POL); P[1] = poly[1];
     733             :     }
     734             :   }
     735         189 :   return S;
     736             : }
     737             : 
     738             : /* Computes [x]! */
     739             : static double
     740         182 : fact(double x)
     741             : {
     742         182 :   double ft = 1.0;
     743         182 :   x = floor(x); while (x>1) { ft *= x; x--; }
     744         182 :   return ft ;
     745             : }
     746             : 
     747             : static GEN
     748          98 : RgX_homogenize(GEN P, long v)
     749             : {
     750          98 :   GEN Q = leafcopy(P);
     751          98 :   long i, l = lg(P), d = degpol(P);
     752          98 :   for (i = 2; i < l; i++) gel(Q,i) = monomial(gel(Q,i), d--, v);
     753          98 :   return Q;
     754             : }
     755             : 
     756             : /* Compute all relevant constants needed to solve the equation P(x,y)=a given
     757             :  * the solutions of N_{K/Q}(x)=a (see inithue). */
     758             : GEN
     759         357 : thueinit(GEN pol, long flag, long prec)
     760             : {
     761         357 :   GEN POL, C, L, fa, tnf, bnf = NULL;
     762         357 :   pari_sp av = avma;
     763             :   long s, lfa, dpol;
     764             : 
     765         357 :   if (checktnf(pol)) { bnf = checkbnf(gel(pol,2)); pol = gel(pol,1); }
     766         357 :   if (typ(pol)!=t_POL) pari_err_TYPE("thueinit",pol);
     767         357 :   dpol = degpol(pol);
     768         357 :   if (dpol <= 0) pari_err_CONSTPOL("thueinit");
     769         350 :   RgX_check_ZX(pol, "thueinit");
     770         350 :   if (varn(pol)) { pol = leafcopy(pol); setvarn(pol, 0); }
     771             : 
     772         350 :   POL = Q_primitive_part(pol, &C);
     773         350 :   L = gen_1;
     774         350 :   fa = ZX_factor(POL); lfa = lgcols(fa);
     775         350 :   if (lfa > 2 || itos(gcoeff(fa,1,2)) > 1)
     776             :   { /* reducible polynomial, no need to reduce to the monic case */
     777          91 :     GEN P, Q, R, g, f = gcoeff(fa,1,1), E = gcoeff(fa,1,2);
     778          91 :     long e = itos(E);
     779          91 :     long vy = fetch_var();
     780          91 :     long va = fetch_var();
     781          91 :     long vb = fetch_var();
     782          91 :     C = C? ginv(C): gen_1;
     783          91 :     if (e != 1)
     784             :     {
     785          63 :       if (lfa == 2) {
     786          42 :         tnf = mkvec3(mkvec3(POL,C,L), thueinit(f, flag, prec), E);
     787          42 :         delete_var(); delete_var(); delete_var();
     788          42 :         return gerepilecopy(av, tnf);
     789             :       }
     790          21 :       P = gpowgs(f,e);
     791             :     }
     792             :     else
     793          28 :       P = f;
     794          49 :     g = RgX_div(POL, P);
     795          49 :     P = RgX_Rg_sub(RgX_homogenize(f, vy), pol_x(va));
     796          49 :     Q = RgX_Rg_sub(RgX_homogenize(g, vy), pol_x(vb));
     797          49 :     R = polresultant0(P, Q, -1, 0);
     798          49 :     tnf = mkvec3(mkvec3(POL,C,L), mkvecsmall4(degpol(f), e, va,vb),  R);
     799          49 :     delete_var(); delete_var(); delete_var();
     800          49 :     return gerepilecopy(av, tnf);
     801             :   }
     802             :   /* POL monic: POL(x) = C pol(x/L), L integer */
     803         259 :   POL = ZX_primitive_to_monic(POL, &L);
     804         259 :   C = gdiv(powiu(L, dpol), gel(pol, dpol+2));
     805         259 :   pol = POL;
     806             : 
     807         259 :   s = ZX_sturm(pol);
     808         259 :   if (s)
     809             :   {
     810         182 :     long PREC, n = degpol(pol);
     811         182 :     double d, dr, dn = (double)n;
     812             : 
     813         182 :     if (dpol <= 2) pari_err_DOMAIN("thueinit", "P","=",pol,pol);
     814         182 :     dr = (double)((s+n-2)>>1); /* s+t-1 */
     815         182 :     d = dn*(dn-1)*(dn-2);
     816             :     /* Guess precision by approximating Baker's bound. The guess is most of
     817             :      * the time not sharp, ie 10 to 30 decimal digits above what is _really_
     818             :      * necessary. Note that the limiting step is the reduction. See paper. */
     819         728 :     PREC = nbits2prec((long)((5.83 + (dr+4)*5 + log(fact(dr+3)) + (dr+3)*log(dr+2) +
     820         364 :                      (dr+3)*log(d) + log(log(2*d*(dr+2))) + (dr+1))
     821         364 :                      /10.)*32+32);
     822             : 
     823         182 :     if (flag == 0) PREC = (long)(2.2 * PREC); /* Lazy, to be improved */
     824         182 :     if (PREC < prec) PREC = prec;
     825         182 :     if (DEBUGLEVEL >=2) err_printf("prec = %d\n", PREC);
     826             : 
     827             :     for (;;)
     828             :     {
     829         182 :       if (( tnf = inithue(pol, bnf, flag, PREC) )) break;
     830           0 :       PREC = precdbl(PREC);
     831           0 :       if (DEBUGLEVEL>1) pari_warn(warnprec,"thueinit",PREC);
     832           0 :       bnf = NULL; avma = av;
     833           0 :     }
     834             :   }
     835             :   else
     836             :   {
     837             :     GEN ro, c0;
     838             :     long k,l;
     839          77 :     if (!bnf)
     840             :     {
     841          77 :       bnf = Buchall(pol, nf_FORCE, DEFAULTPREC);
     842          77 :       if (flag) (void)bnfcertify(bnf);
     843             :     }
     844          77 :     ro = nf_get_roots(bnf_get_nf(bnf));
     845          77 :     l = lg(ro);
     846          77 :     c0 = imag_i(gel(ro,1));
     847          77 :     for (k = 2; k < l; k++) c0 = mulrr(c0, imag_i(gel(ro,k)));
     848          77 :     c0 = invr(sqrr(c0)); tnf = mkvec3(pol,bnf,c0);
     849             :   }
     850         259 :   gel(tnf,1) = mkvec3(gel(tnf,1), C, L);
     851         259 :   return gerepilecopy(av,tnf);
     852             : }
     853             : 
     854             : /* arg(t^2) / 2Pi; arg(t^2) = arg(t/conj(t)) */
     855             : static GEN
     856         588 : argsqr(GEN t, GEN Pi)
     857             : {
     858         588 :   GEN v, u = divrr(garg(t,0), Pi); /* in -1 < u <= 1 */
     859             :   /* reduce mod Z to -1/2 < u <= 1/2 */
     860         588 :   if (signe(u) > 0)
     861             :   {
     862         119 :     v = subrs(u,1); /* ]-1,0] */
     863         119 :     if (abscmprr(v,u) < 0) u = v;
     864             :   }
     865             :   else
     866             :   {
     867         469 :     v = addrs(u,1);/* ]0,1] */
     868         469 :     if (abscmprr(v,u) <= 0) u = v;
     869             :   }
     870         588 :   return u;
     871             : }
     872             : /* i1 != i2 */
     873             : static void
     874        3730 : init_get_B(long i1, long i2, GEN Delta2, GEN Lambda, GEN Deps5, baker_s *BS,
     875             :            long prec)
     876             : {
     877             :   GEN delta, lambda;
     878        3730 :   if (BS->r > 1)
     879             :   {
     880        3436 :     delta = gel(Delta2,i2);
     881        3436 :     lambda = gsub(gmul(delta,gel(Lambda,i1)), gel(Lambda,i2));
     882        3436 :     if (Deps5) BS->inverrdelta = divrr(Deps5, addsr(1,delta));
     883             :   }
     884             :   else
     885             :   { /* r == 1: i1 = s = t = 1; i2 = 2 */
     886         294 :     GEN fu = gel(BS->MatFU,1), ro = BS->ro, t;
     887             : 
     888         294 :     t = gel(fu,2);
     889         294 :     delta = argsqr(t, BS->Pi);
     890         294 :     if (Deps5) BS->inverrdelta = shiftr(gabs(t,prec), prec2nbits(prec)-1);
     891             : 
     892         294 :     t = gmul(gsub(gel(ro,1), gel(ro,2)), gel(BS->NE,3));
     893         294 :     lambda = argsqr(t, BS->Pi);
     894             :   }
     895        3730 :   BS->delta = delta;
     896        3730 :   BS->lambda = lambda;
     897        3730 : }
     898             : 
     899             : static GEN
     900        1220 : get_B0(long i1, GEN Delta2, GEN Lambda, GEN Deps5, long prec, baker_s *BS)
     901             : {
     902        1220 :   GEN B0 = Baker(BS);
     903        1220 :   long step = 0, i2 = (i1 == 1)? 2: 1;
     904             :   for(;;) /* i2 from 1 to r unless r = 1 [then i2 = 2] */
     905             :   {
     906        1220 :     init_get_B(i1,i2, Delta2,Lambda,Deps5, BS, prec);
     907             :     /* Reduce B0 as long as we make progress: newB0 < oldB0 - 0.1 */
     908             :     for (;;)
     909             :     {
     910        3864 :       GEN oldB0 = B0, kappa = utoipos(10);
     911             :       long cf;
     912             : 
     913        4616 :       for (cf = 0; cf < 10; cf++, kappa = muliu(kappa,10))
     914             :       {
     915        4609 :         int res = CF_1stPass(&B0, kappa, BS);
     916        4609 :         if (res < 0) return NULL; /* prec problem */
     917        4609 :         if (res) break;
     918         752 :         if (DEBUGLEVEL>1) err_printf("CF failed. Increasing kappa\n");
     919             :       }
     920        3864 :       if (!step && cf == 10)
     921             :       { /* Semirational or totally rational case */
     922             :         GEN Q, ep, q, l0, denbound;
     923             : 
     924           0 :         if (! (Q = GuessQi(BS->delta, BS->lambda, &ep)) ) break;
     925             : 
     926           0 :         denbound = gadd(B0, absi(gel(Q,1)));
     927           0 :         q = denom( bestappr(BS->delta, denbound) );
     928           0 :         l0 = subrr(errnum(BS->delta, q), ep);
     929           0 :         if (signe(l0) <= 0) break;
     930             : 
     931           0 :         B0 = divrr(mplog(divrr(mulir(gel(Q,2), BS->c15), l0)),  BS->c13);
     932           0 :         if (DEBUGLEVEL>1) err_printf("Semirat. reduction: B0 -> %Ps\n",B0);
     933             :       }
     934             :       /* if no progress, stop */
     935        3864 :       if (gcmp(oldB0, gadd(B0,dbltor(0.1))) <= 0) return gmin(oldB0, B0);
     936        2644 :       else step++;
     937        2644 :     }
     938           0 :     i2++; if (i2 == i1) i2++;
     939           0 :     if (i2 > BS->r) break;
     940           0 :   }
     941           0 :   pari_err_BUG("thue (totally rational case)");
     942           0 :   return NULL; /* not reached */
     943             : }
     944             : 
     945             : static GEN
     946        2510 : get_Bx_LLL(long i1, GEN Delta2, GEN Lambda, long prec, baker_s *BS)
     947             : {
     948        2510 :   GEN B0 = Baker(BS), Bx = NULL;
     949        2510 :   long step = 0, i2 = (i1 == 1)? 2: 1;
     950             :   for(;;) /* i2 from 1 to r unless r = 1 [then i2 = 2] */
     951             :   {
     952        2510 :     init_get_B(i1,i2, Delta2,Lambda,NULL, BS, prec);
     953        2510 :     if (DEBUGLEVEL>1) err_printf("  Entering LLL...\n");
     954             :     /* Reduce B0 as long as we make progress: newB0 < oldB0 - 0.1 */
     955             :     for (;;)
     956             :     {
     957       10375 :       GEN oldBx = Bx, kappa = utoipos(10);
     958       10375 :       const long cfMAX = 10;
     959             :       long cf;
     960             : 
     961       18857 :       for (cf = 0; cf < cfMAX; cf++, kappa = muliu(kappa,10))
     962             :       {
     963       18542 :         int res = LLL_1stPass(&B0, kappa, BS, &Bx);
     964       18542 :         if (res < 0) return NULL;
     965       18529 :         if (res) break;
     966        8482 :         if (DEBUGLEVEL>1) err_printf("LLL failed. Increasing kappa\n");
     967             :       }
     968             : 
     969             :       /* FIXME: TO BE COMPLETED */
     970       10362 :       if (!step && cf == cfMAX)
     971             :       { /* Semirational or totally rational case */
     972             :         GEN Q, ep, q, l0, denbound;
     973             : 
     974         147 :         if (! (Q = GuessQi(BS->delta, BS->lambda, &ep)) ) break;
     975             : 
     976             :         /* Q[2] != 0 */
     977         147 :         denbound = gadd(mulri(B0, absi(gel(Q,1))),
     978         147 :                         mulii(BS->Ind, absi(gel(Q,2))));
     979         147 :         q = denom( bestappr(BS->delta, denbound) );
     980         147 :         l0 = divri(subrr(errnum(BS->delta, q), ep), absi(gel(Q,2)));
     981         147 :         if (signe(l0) <= 0) break;
     982             : 
     983         147 :         get_B0Bx(BS, l0, &B0, &Bx);
     984         147 :         if (DEBUGLEVEL>1)
     985           0 :           err_printf("Semirat. reduction: B0 -> %Ps x <= %Ps\n",B0, Bx);
     986             :       }
     987             :       /* if no progress, stop */
     988       10362 :       if (oldBx && gcmp(oldBx, Bx) <= 0) return oldBx; else step++;
     989        7865 :     }
     990           0 :     i2++; if (i2 == i1) i2++;
     991           0 :     if (i2 > BS->r) break;
     992           0 :   }
     993           0 :   pari_err_BUG("thue (totally rational case)");
     994           0 :   return NULL; /* not reached */
     995             : }
     996             : 
     997             : static GEN
     998         168 : LargeSols(GEN P, GEN tnf, GEN rhs, GEN ne)
     999             : {
    1000         168 :   GEN S = NULL, Delta0, ro, ALH, bnf, nf, MatFU, A, csts, dP, Bx;
    1001             :   GEN c1,c2,c3,c4,c90,c91,c14, x0, x1, x2, x3, tmp, eps5, prinfo;
    1002             :   long iroot, ine, n, r, Prec, prec, s,t;
    1003             :   baker_s BS;
    1004         168 :   pari_sp av = avma;
    1005             : 
    1006         168 :   prec = 0; /*-Wall*/
    1007         168 :   bnf = NULL; /*-Wall*/
    1008         168 :   iroot = 1;
    1009         168 :   ine = 1;
    1010             : 
    1011             : START:
    1012         181 :   if (S) /* restart from precision problems */
    1013             :   {
    1014          13 :     S = gerepilecopy(av, S);
    1015          13 :     prec = precdbl(prec);
    1016          13 :     if (DEBUGLEVEL) pari_warn(warnprec,"thue",prec);
    1017          13 :     tnf = inithue(P, bnf, 0, prec);
    1018             :   }
    1019             :   else
    1020         168 :     S = cgetg(1, t_VEC);
    1021         181 :   bnf= gel(tnf,2);
    1022         181 :   nf = bnf_get_nf(bnf);
    1023         181 :   csts = gel(tnf,7);
    1024         181 :   nf_get_sign(nf, &s, &t);
    1025         181 :   BS.r = r = s+t-1; n = degpol(P);
    1026         181 :   ro     = gel(tnf,3);
    1027         181 :   ALH    = gel(tnf,4);
    1028         181 :   MatFU  = gel(tnf,5);
    1029         181 :   A      = gel(tnf,6);
    1030         181 :   c1     = gel(csts,1); c1 = gmul(absi(rhs), c1);
    1031         181 :   c2     = gel(csts,2);
    1032         181 :   BS.hal = gel(csts,3);
    1033         181 :   x0     = gel(csts,4);
    1034         181 :   eps5   = gel(csts,5);
    1035         181 :   Prec = itos(gel(csts,6));
    1036         181 :   BS.Ind = gel(csts,7);
    1037         181 :   BS.MatFU = MatFU;
    1038         181 :   BS.bak = muluu(n, (n-1)*(n-2)); /* safe */
    1039         181 :   BS.deg = n;
    1040         181 :   prinfo = gel(csts,8);
    1041             : 
    1042         181 :   if (t) x0 = gmul(x0, absisqrtn(rhs, n, Prec));
    1043         181 :   tmp = divrr(c1,c2);
    1044         181 :   c3 = mulrr(dbltor(1.39), tmp);
    1045         181 :   c4 = mulur(n-1, c3);
    1046         181 :   c14 = mulrr(c4, vecmax_shallow(RgM_sumcol(gabs(A,DEFAULTPREC))));
    1047             : 
    1048         181 :   x1 = gmax(x0, sqrtnr(shiftr(tmp,1),n));
    1049         181 :   x2 = gmax(x1, sqrtnr(mulur(10,c14), n));
    1050         181 :   x3 = gmax(x2, sqrtnr(shiftr(c14, EXPO1+1),n));
    1051         543 :   c90 = gmul(shiftr(mulur(18,mppi(DEFAULTPREC)), 5*(4+r)),
    1052         362 :                     gmul(gmul(mpfact(r+3), powiu(muliu(BS.bak,r+2), r+3)),
    1053         181 :                          glog(muliu(BS.bak,2*(r+2)),DEFAULTPREC)));
    1054             : 
    1055         181 :   dP = ZX_deriv(P);
    1056         181 :   Delta0 = RgM_sumcol(A);
    1057             : 
    1058         343 :   for (; iroot<=s; iroot++)
    1059             :   {
    1060         356 :     GEN Delta = Delta0, Delta2, D, Deps5, MatNE, Hmu, diffRo, c5, c7, ro0;
    1061             :     long i1, iroot1, iroot2, k;
    1062             : 
    1063         356 :     if (iroot <= r) Delta = RgC_add(Delta, RgC_Rg_mul(gel(A,iroot), stoi(-n)));
    1064         356 :     D = gabs(Delta,Prec); i1 = vecindexmax(D);
    1065         356 :     c5 = gel(D, i1);
    1066         356 :     Delta2 = RgC_Rg_div(Delta, gel(Delta, i1));
    1067         356 :     c5  = myround(gprec_w(c5,DEFAULTPREC), 1);
    1068         356 :     Deps5 = divrr(subrr(c5,eps5), eps5);
    1069         356 :     c7  = mulur(r,c5);
    1070         356 :     BS.c10 = divur(n,c7);
    1071         356 :     BS.c13 = divur(n,c5);
    1072         356 :     if (DEBUGLEVEL>1) {
    1073           0 :       err_printf("* real root no %ld/%ld\n", iroot,s);
    1074           0 :       err_printf("  c10 = %Ps\n",BS.c10);
    1075           0 :       err_printf("  c13 = %Ps\n",BS.c13);
    1076             :     }
    1077             : 
    1078         356 :     prec = Prec;
    1079             :     for (;;)
    1080             :     {
    1081         356 :       if (( MatNE = Conj_LH(ne, &Hmu, ro, prec) )) break;
    1082           0 :       prec = precdbl(prec);
    1083           0 :       if (DEBUGLEVEL>1) pari_warn(warnprec,"thue",prec);
    1084           0 :       ro = tnf_get_roots(P, prec, s, t);
    1085           0 :     }
    1086         356 :     ro0 = gel(ro,iroot);
    1087         356 :     BS.ro    = ro;
    1088         356 :     BS.iroot = iroot;
    1089         356 :     BS.Pi  = mppi(prec);
    1090         356 :     BS.Pi2 = Pi2n(1,prec);
    1091         356 :     diffRo = cgetg(r+1, t_VEC);
    1092        1068 :     for (k=1; k<=r; k++)
    1093             :     {
    1094         712 :       GEN z = gel(ro,k);
    1095         712 :       z = (k == iroot)? gdiv(rhs, poleval(dP, z)): gsub(ro0, z);
    1096         712 :       gel(diffRo,k) = gabs(z, prec);
    1097             :     }
    1098         356 :     other_roots(iroot, &iroot1,&iroot2);
    1099         356 :     BS.divro = gdiv(gsub(ro0, gel(ro,iroot2)), gsub(ro0, gel(ro,iroot1)));
    1100             :     /* Compute h_1....h_r */
    1101         356 :     c91 = c90;
    1102        1068 :     for (k=1; k<=r; k++)
    1103             :     {
    1104         712 :       GEN z = gdiv(gcoeff(MatFU,iroot1,k), gcoeff(MatFU,iroot2,k));
    1105         712 :       z = gmax(gen_1, abslog(z));
    1106         712 :       c91 = gmul(c91, gmax(gel(ALH,k), gdiv(z, BS.bak)));
    1107             :     }
    1108         356 :     BS.c91 = c91;
    1109             : 
    1110        4073 :     for (; ine<lg(ne); ine++)
    1111             :     {
    1112        3730 :       pari_sp av2 = avma;
    1113        3730 :       long lS = lg(S);
    1114             :       GEN Lambda, B0, c6, c8;
    1115        3730 :       GEN NE = gel(MatNE,ine), v = cgetg(r+1,t_COL);
    1116             : 
    1117        3730 :       if (DEBUGLEVEL>1) err_printf("  - norm sol. no %ld/%ld\n",ine,lg(ne)-1);
    1118       11547 :       for (k=1; k<=r; k++)
    1119             :       {
    1120        7817 :         GEN z = gdiv(gel(diffRo,k), gabs(gel(NE,k), prec));
    1121        7817 :         gel(v,k) = glog(z, prec);
    1122             :       }
    1123        3730 :       Lambda = RgM_RgC_mul(A,v);
    1124             : 
    1125        3730 :       c6 = addrr(dbltor(0.1), vecmax_shallow(gabs(Lambda,DEFAULTPREC)));
    1126        3730 :       c6 = myround(c6, 1);
    1127        3730 :       c8 = addrr(dbltor(1.23), mulur(r,c6));
    1128        3730 :       BS.c11= mulrr(shiftr(c3,1) , mpexp(divrr(mulur(n,c8),c7)));
    1129        3730 :       BS.c15= mulrr(shiftr(c14,1), mpexp(divrr(mulur(n,c6),c5)));
    1130        3730 :       BS.NE = NE;
    1131        3730 :       BS.Hmu = gel(Hmu,ine);
    1132        3730 :       if (is_pm1(BS.Ind))
    1133             :       {
    1134        1220 :         GEN mu = gel(ne,ine), Lmu = cgetg(lg(prinfo),t_VEC);
    1135             :         long i, j;
    1136             : 
    1137        2440 :         for (i = 1; i < lg(prinfo); i++)
    1138             :         {
    1139        1220 :           GEN v = gel(prinfo,i), PR = gel(v,3), L = cgetg(4, t_VECSMALL);
    1140        1220 :           for (j = 1; j <= 3; j++) L[j] = itou(nf_to_Fq(nf, mu, gel(PR,j)));
    1141        1220 :           gel(Lmu, i) = L;
    1142             :         }
    1143        2440 :         if (! (B0 = get_B0(i1, Delta2, Lambda, Deps5, prec, &BS)) ||
    1144        1220 :             !TrySol(&S, B0, i1, Delta2, Lambda, ro, Lmu, NE,MatFU,prinfo,
    1145             :                     P,rhs))
    1146             :           goto START;
    1147        1220 :         if (lg(S) == lS) avma = av2;
    1148             :       }
    1149             :       else
    1150             :       {
    1151        2510 :         if (! (Bx = get_Bx_LLL(i1, Delta2, Lambda, prec, &BS)) )
    1152          13 :            goto START;
    1153        2497 :         x3 = gerepileupto(av2, gmax(Bx, x3));
    1154             :       }
    1155             :     }
    1156         343 :     ine = 1;
    1157             :   }
    1158         168 :   x3 = gmax(x0, MiddleSols(&S, x3, ro, P, rhs, s, c1));
    1159         168 :   return SmallSols(S, x3, P, rhs);
    1160             : }
    1161             : 
    1162             : /* restrict to solutions (x,y) with L | x, replacing each by (x/L, y) */
    1163             : static GEN
    1164         294 : filter_sol_x(GEN S, GEN L)
    1165             : {
    1166             :   long i, k, l;
    1167         294 :   if (is_pm1(L)) return S;
    1168          77 :   l = lg(S); k = 1;
    1169         525 :   for (i = 1; i < l; i++)
    1170             :   {
    1171         448 :     GEN s = gel(S,i), r;
    1172         448 :     gel(s,1) = dvmdii(gel(s,1), L, &r);
    1173         448 :     if (r == gen_0) gel(S, k++) = s;
    1174             :   }
    1175          77 :   setlg(S, k); return S;
    1176             : }
    1177             : 
    1178             : static GEN bnfisintnorm_i(GEN bnf, GEN a, long s, GEN z);
    1179             : static GEN
    1180         161 : tnf_get_Ind(GEN tnf) { return gmael(tnf,7,7); }
    1181             : static GEN
    1182         245 : tnf_get_bnf(GEN tnf) { return gel(tnf,2); }
    1183             : 
    1184             : static void
    1185           0 : maybe_warn(GEN bnf, GEN a, GEN Ind)
    1186             : {
    1187           0 :   if (!is_pm1(Ind) && !is_pm1(bnf_get_no(bnf)) && !is_pm1(a))
    1188           0 :     pari_warn(warner, "The result returned by 'thue' is conditional on the GRH");
    1189           0 : }
    1190             : /* return solutions of Norm(x) = a mod U(K)^+ */
    1191             : static GEN
    1192         196 : get_ne(GEN bnf, GEN a, GEN Ind)
    1193             : {
    1194         196 :   if (DEBUGLEVEL) maybe_warn(bnf,a,Ind);
    1195         196 :   return bnfisintnorm(bnf, a);
    1196             : }
    1197             : /* return solutions of |Norm(x)| = |a| mod U(K) */
    1198             : static GEN
    1199          28 : get_neabs(GEN bnf, GEN a, GEN Ind)
    1200             : {
    1201          28 :   if (DEBUGLEVEL) maybe_warn(bnf,a,Ind);
    1202          28 :   return bnfisintnormabs(bnf, a);
    1203             : }
    1204             : 
    1205             : /* Let P(z)=z^2+Bz+C, convert t=u+v*z (mod P) solution of norm equation N(t)=A
    1206             :  * to [x,y] = [u,-v] form: y^2P(x/y) = A */
    1207             : static GEN
    1208         490 : ne2_to_xy(GEN t)
    1209             : {
    1210             :   GEN u,v;
    1211         490 :   switch(degpol(t))
    1212             :   {
    1213           0 :     case -1: u = v = gen_0; break;
    1214          28 :     case 0: u = gel(t,2); v = gen_0; break;
    1215         462 :     default: u = gel(t,2); v = gel(t,3);
    1216             :   }
    1217         490 :   return mkvec2(u, gneg(v));
    1218             : }
    1219             : static GEN
    1220          42 : ne2V_to_xyV(GEN v)
    1221             : {
    1222             :   long i, l;
    1223          42 :   GEN w = cgetg_copy(v,&l);
    1224          42 :   for (i=1; i<l; i++) gel(w,i) = ne2_to_xy(gel(v,i));
    1225          42 :   return w;
    1226             : }
    1227             : 
    1228             : static GEN
    1229          21 : sol_0(void) { retmkvec( mkvec2(gen_0,gen_0) ); }
    1230             : static void
    1231         140 : sols_from_R(GEN Rab, GEN *pS, GEN P, GEN POL, GEN rhs)
    1232             : {
    1233         140 :   GEN ry = nfrootsQ(Rab);
    1234         140 :   long k, l = lg(ry);
    1235         280 :   for (k = 1; k < l; k++)
    1236         140 :     if (typ(gel(ry,k)) == t_INT) check_y(pS, P, POL, gel(ry,k), rhs);
    1237         140 : }
    1238             : /* Given a tnf structure as returned by thueinit, a RHS and
    1239             :  * optionally the solutions to the norm equation, returns the solutions to
    1240             :  * the Thue equation F(x,y)=a */
    1241             : GEN
    1242         343 : thue(GEN tnf, GEN rhs, GEN ne)
    1243             : {
    1244         343 :   pari_sp av = avma;
    1245             :   GEN POL, C, L, x3, S;
    1246             : 
    1247         343 :   if (typ(tnf) == t_POL) tnf = thueinit(tnf, 0, DEFAULTPREC);
    1248         343 :   if (!checktnf(tnf)) pari_err_TYPE("thue [please apply thueinit()]", tnf);
    1249         343 :   if (typ(rhs) != t_INT) pari_err_TYPE("thue",rhs);
    1250         343 :   if (ne && typ(ne) != t_VEC) pari_err_TYPE("thue",ne);
    1251             : 
    1252             :   /* solve P(x,y) = rhs <=> POL(L x, y) = C rhs, with POL monic in Z[X] */
    1253         343 :   POL = gel(tnf,1);
    1254         343 :   C = gel(POL,2); rhs = gmul(C, rhs);
    1255         343 :   if (typ(rhs) != t_INT) { avma = av; return cgetg(1, t_VEC); }
    1256         343 :   if (!signe(rhs))
    1257             :   {
    1258          28 :     GEN v = gel(tnf,2);
    1259          28 :     avma = av;
    1260             :     /* at least 2 irreducible factors, one of which has degree 1 */
    1261          28 :     if (typ(v) == t_VECSMALL && v[1] ==1)
    1262           7 :       pari_err_DOMAIN("thue","#sols","=",strtoGENstr("oo"),rhs);
    1263          21 :     return sol_0();
    1264             :   }
    1265         315 :   L = gel(POL,3);
    1266         315 :   POL = gel(POL,1);
    1267         315 :   if (lg(tnf) == 8)
    1268             :   {
    1269         168 :     if (!ne) ne = get_ne(tnf_get_bnf(tnf), rhs, tnf_get_Ind(tnf));
    1270         168 :     if (lg(ne) == 1) { avma = av; return cgetg(1, t_VEC); }
    1271         168 :     S = LargeSols(POL, tnf, rhs, ne);
    1272             :   }
    1273         147 :   else if (typ(gel(tnf,3)) == t_REAL)
    1274             :   { /* Case s=0. All solutions are "small". */
    1275          77 :     GEN bnf = tnf_get_bnf(tnf);
    1276          77 :     GEN c0 = gel(tnf,3), F;
    1277          77 :     x3 = sqrtnr(mulir(absi(rhs),c0), degpol(POL));
    1278          77 :     x3 = addrr(x3, dbltor(0.1)); /* guard from round-off errors */
    1279          77 :     S = cgetg(1,t_VEC);
    1280          77 :     if (!ne && expo(x3) > 10)
    1281             :     {
    1282             :       long l;
    1283             :       GEN P;
    1284          56 :       if (expi(rhs) < 150) F = Z_factor(rhs);
    1285             :       else
    1286             :       {
    1287           0 :         F = Z_factor_limit(rhs, 500000);
    1288           0 :         P = gel(F,1); l = lg(P);
    1289           0 :         if (!is_pm1(rhs) && !BPSW_psp(gel(P,l-1))) F = NULL;
    1290             :       }
    1291          56 :       if (F) ne = get_ne(bnf, F, gen_1);
    1292             :     }
    1293          77 :     if (ne)
    1294             :     {
    1295          63 :       if (lg(ne) == 1) { avma = av; return cgetg(1,t_VEC); }
    1296          49 :       if (degpol(POL) == 2) /* quadratic imaginary */
    1297             :       {
    1298          42 :         GEN u = bnf_get_tuU(bnf);
    1299          42 :         long w =  bnf_get_tuN(bnf);
    1300          42 :         if (w == 4) /* u = I */
    1301          14 :           ne = shallowconcat(ne, RgXQV_RgXQ_mul(ne,u,POL));
    1302          28 :         else if (w == 6) /* u = j */
    1303             :         {
    1304          14 :           GEN u2 = RgXQ_sqr(u,POL);
    1305          14 :           ne = shallowconcat1(mkvec3(ne, RgXQV_RgXQ_mul(ne,u,POL),
    1306             :                                          RgXQV_RgXQ_mul(ne,u2,POL)));
    1307             :         }
    1308          42 :         ne = ne2V_to_xyV(ne);
    1309          42 :         S = shallowconcat(ne, RgV_neg(ne));
    1310             :       }
    1311             :     }
    1312          63 :     if (lg(S) == 1) S = SmallSols(S, x3, POL, rhs);
    1313             :   }
    1314          70 :   else if (typ(gel(tnf,3)) == t_INT) /* reducible case, pure power*/
    1315             :   {
    1316          35 :     GEN bnf, ne1 = NULL, ne2 = NULL;
    1317          35 :     long e = itos( gel(tnf,3) );
    1318          35 :     if (!Z_ispowerall(rhs, e, &rhs)) { avma = av; return cgetg(1, t_VEC); }
    1319          28 :     tnf = gel(tnf,2);
    1320          28 :     bnf = tnf_get_bnf(tnf);
    1321          28 :     ne = get_neabs(bnf, rhs, lg(tnf)==8?tnf_get_Ind(tnf): gen_1);
    1322          28 :     ne1= bnfisintnorm_i(bnf,rhs,1,ne);
    1323          28 :     S = thue(tnf, rhs, ne1);
    1324          28 :     if (!odd(e) && lg(tnf)==8) /* if s=0, norms are positive */
    1325             :     {
    1326           7 :       ne2 = bnfisintnorm_i(bnf,rhs,-1,ne);
    1327           7 :       S = shallowconcat(S, thue(tnf, negi(rhs), ne2));
    1328             :     }
    1329             :   }
    1330             :   else /* other reducible cases */
    1331             :   { /* solve f^e * g = rhs, f irreducible factor of smallest degree */
    1332          35 :     GEN P, D, v = gel(tnf, 2), R = gel(tnf, 3);
    1333          35 :     long i, l, e = v[2], va = v[3], vb = v[4];
    1334          35 :     P = cgetg(lg(POL), t_POL); P[1] = POL[1];
    1335          35 :     D = divisors(rhs); l = lg(D);
    1336          35 :     S = cgetg(1,t_VEC);
    1337         217 :     for (i = 1; i < l; i++)
    1338             :     {
    1339         182 :       GEN Rab, df = gel(D,i), dg = gel(D,l-i); /* df*dg=|rhs| */
    1340         182 :       if (e > 1 && !Z_ispowerall(df, e, &df)) continue;
    1341             :       /* Rab: univariate polynomial in Z[Y], whose roots are the possible y. */
    1342             :       /* Here and below, Rab != 0 */
    1343          70 :       if (signe(rhs) < 0) dg = negi(dg); /* df*dg=rhs */
    1344          70 :       Rab = gsubst(gsubst(R, va, df), vb, dg);
    1345          70 :       sols_from_R(Rab, &S,P,POL,rhs);
    1346          70 :       Rab = gsubst(gsubst(R, va, negi(df)), vb, odd(e)? negi(dg): dg);
    1347          70 :       sols_from_R(Rab, &S,P,POL,rhs);
    1348             :     }
    1349             :   }
    1350         294 :   S = filter_sol_x(S, L);
    1351         294 :   S = gen_sort_uniq(S, (void*)lexcmp, cmp_nodata);
    1352         294 :   return gerepileupto(av, S);
    1353             : }
    1354             : 
    1355             : /********************************************************************/
    1356             : /**                                                                **/
    1357             : /**                      BNFISINTNORM (K. Belabas)                 **/
    1358             : /**                                                                **/
    1359             : /********************************************************************/
    1360             : struct sol_abs
    1361             : {
    1362             :   GEN rel; /* Primes PR[i] above a, expressed on generators of Cl(K) */
    1363             :   GEN partrel; /* list of vectors, partrel[i] = rel[1..i] * u[1..i] */
    1364             :   GEN cyc;     /* orders of generators of Cl(K) given in bnf */
    1365             : 
    1366             :   long *f;     /* f[i] = f(PR[i]/p), inertia degree */
    1367             :   long *n;     /* a = prod p^{ n_p }. n[i]=n_p if PR[i] divides p */
    1368             :   long *next;  /* index of first P above next p, 0 if p is last */
    1369             :   long *S;     /* S[i] = n[i] - sum_{ 1<=k<=i } f[k]*u[k] */
    1370             :   long *u;     /* We want principal ideals I = prod PR[i]^u[i] */
    1371             :   GEN  normsol;/* lists of copies of the u[] which are solutions */
    1372             : 
    1373             :   long nPR;    /* length(T->rel) = #PR */
    1374             :   long sindex, smax; /* current index in T->normsol; max. index */
    1375             : };
    1376             : 
    1377             : /* u[1..i] has been filled. Norm(u) is correct.
    1378             :  * Check relations in class group then save it. */
    1379             : static void
    1380       46137 : test_sol(struct sol_abs *T, long i)
    1381             : {
    1382             :   long k, l;
    1383             :   GEN s;
    1384             : 
    1385       92274 :   if (T->partrel && !ZV_dvd(gel(T->partrel, i),  T->cyc)) return;
    1386       30072 :   if (T->sindex == T->smax)
    1387             :   { /* no more room in solution list: enlarge */
    1388           0 :     long new_smax = T->smax << 1;
    1389           0 :     GEN  new_normsol = new_chunk(new_smax+1);
    1390             : 
    1391           0 :     for (k=1; k<=T->smax; k++) gel(new_normsol,k) = gel(T->normsol,k);
    1392           0 :     T->normsol = new_normsol; T->smax = new_smax;
    1393             :   }
    1394       30072 :   gel(T->normsol, ++T->sindex) = s = cgetg_copy(T->u, &l);
    1395       30072 :   for (k=1; k <= i; k++) s[k] = T->u[k];
    1396       30072 :   for (   ; k < l;  k++) s[k] = 0;
    1397       30072 :   if (DEBUGLEVEL>2)
    1398             :   {
    1399           0 :     err_printf("sol = %Ps\n",s);
    1400           0 :     if (T->partrel) err_printf("T->partrel = %Ps\n",T->partrel);
    1401           0 :     err_flush();
    1402             :   }
    1403             : }
    1404             : /* partrel[i] <-- partrel[i-1] + u[i] * rel[i] */
    1405             : static void
    1406       21189 : fix_partrel(struct sol_abs *T, long i)
    1407             : {
    1408       21189 :   pari_sp av = avma;
    1409       21189 :   GEN part1 = gel(T->partrel,i);
    1410       21189 :   GEN part0 = gel(T->partrel,i-1);
    1411       21189 :   GEN rel = gel(T->rel, i);
    1412       21189 :   ulong u = T->u[i];
    1413       21189 :   long k, l = lg(part1);
    1414       62496 :   for (k=1; k < l; k++)
    1415       41307 :     affii(addii(gel(part0,k), muliu(gel(rel,k), u)), gel(part1,k));
    1416       21189 :   avma = av;
    1417       21189 : }
    1418             : 
    1419             : /* Recursive loop. Suppose u[1..i] has been filled
    1420             :  * Find possible solutions u such that, Norm(prod PR[i]^u[i]) = a, taking
    1421             :  * into account:
    1422             :  *  1) the relations in the class group if need be.
    1423             :  *  2) the factorization of a. */
    1424             : static void
    1425      107212 : isintnorm_loop(struct sol_abs *T, long i)
    1426             : {
    1427      107212 :   if (T->S[i] == 0) /* sum u[i].f[i] = n[i], do another prime */
    1428             :   {
    1429       45759 :     long k, next = T->next[i];
    1430       45759 :     if (next == 0) { test_sol(T, i); return; } /* no primes left */
    1431             : 
    1432             :     /* some primes left */
    1433       13909 :     if (T->partrel) gaffect(gel(T->partrel,i), gel(T->partrel, next-1));
    1434       13909 :     for (k=i+1; k < next; k++) T->u[k] = 0;
    1435       13909 :     i = next-1;
    1436             :   }
    1437       61453 :   else if (i == T->next[i]-2 || i == T->nPR-1)
    1438             :   { /* only one Prime left above prime; change prime, fix u[i+1] */
    1439             :     long q;
    1440       32004 :     if (T->S[i] % T->f[i+1]) return;
    1441       17990 :     q = T->S[i] / T->f[i+1];
    1442       17990 :     i++; T->u[i] = q;
    1443       17990 :     if (T->partrel) fix_partrel(T,i);
    1444       17990 :     if (T->next[i] == 0) { test_sol(T,i); return; }
    1445             :   }
    1446             : 
    1447       47061 :   i++; T->u[i] = 0;
    1448       47061 :   if (T->partrel) gaffect(gel(T->partrel,i-1), gel(T->partrel,i));
    1449       47061 :   if (i == T->next[i-1])
    1450             :   { /* change prime */
    1451       34902 :     if (T->next[i] == i+1 || i == T->nPR) /* only one Prime above p */
    1452             :     {
    1453       10339 :       T->S[i] = 0;
    1454       10339 :       T->u[i] = T->n[i] / T->f[i]; /* we already know this is exact */
    1455       10339 :       if (T->partrel) fix_partrel(T, i);
    1456             :     }
    1457       24563 :     else T->S[i] = T->n[i];
    1458             :   }
    1459       12159 :   else T->S[i] = T->S[i-1]; /* same prime, different Prime */
    1460             :   for(;;)
    1461             :   {
    1462       89495 :     isintnorm_loop(T, i);
    1463       89495 :     T->S[i] -= T->f[i]; if (T->S[i] < 0) break;
    1464       42434 :     T->u[i]++;
    1465       42434 :     if (T->partrel) {
    1466       22309 :       pari_sp av = avma;
    1467       22309 :       gaffect(ZC_add(gel(T->partrel,i), gel(T->rel,i)), gel(T->partrel,i));
    1468       22309 :       avma = av;
    1469             :     }
    1470       42434 :   }
    1471             : }
    1472             : 
    1473             : static int
    1474       25795 : get_sol_abs(struct sol_abs *T, GEN bnf, GEN fact, GEN *ptPR)
    1475             : {
    1476       25795 :   GEN nf = bnf_get_nf(bnf);
    1477       25795 :   GEN P = gel(fact,1), E = gel(fact,2), PR;
    1478       25795 :   long N = nf_get_degree(nf), nP = lg(P)-1, Ngen, max, nPR, i, j;
    1479             : 
    1480       25795 :   max = nP*N; /* upper bound for T->nPR */
    1481       25795 :   T->f = new_chunk(max+1);
    1482       25795 :   T->n = new_chunk(max+1);
    1483       25795 :   T->next = new_chunk(max+1);
    1484       25795 :   *ptPR = PR = cgetg(max+1, t_VEC); /* length to be fixed later */
    1485             : 
    1486       25795 :   nPR = 0;
    1487       63497 :   for (i = 1; i <= nP; i++)
    1488             :   {
    1489       45780 :     GEN L = idealprimedec(nf, gel(P,i));
    1490       45780 :     long lL = lg(L), gcd, k, v;
    1491       45780 :     ulong vn = itou(gel(E,i));
    1492             : 
    1493             :     /* check that gcd_{P | p} f_P  divides  n_p */
    1494       45780 :     gcd = pr_get_f(gel(L,1));
    1495       45780 :     for (j=2; gcd > 1 && j < lL; j++) gcd = ugcd(gcd, pr_get_f(gel(L,j)));
    1496       45780 :     if (gcd > 1 && vn % gcd)
    1497             :     {
    1498        8078 :       if (DEBUGLEVEL>2)
    1499           0 :       { err_printf("gcd f_P  does not divide n_p\n"); err_flush(); }
    1500        8078 :       return 0;
    1501             :     }
    1502       37702 :     v = (i==nP)? 0: nPR + lL;
    1503      112686 :     for (k = 1; k < lL; k++)
    1504             :     {
    1505       74984 :       GEN pr = gel(L,k);
    1506       74984 :       gel(PR, ++nPR) = pr;
    1507       74984 :       T->f[nPR] = pr_get_f(pr) / gcd;
    1508       74984 :       T->n[nPR] = vn / gcd;
    1509       74984 :       T->next[nPR] = v;
    1510             :     }
    1511             :   }
    1512       17717 :   T->nPR = nPR;
    1513       17717 :   setlg(PR, nPR + 1);
    1514             : 
    1515       17717 :   T->u = cgetg(nPR+1, t_VECSMALL);
    1516       17717 :   T->S = new_chunk(nPR+1);
    1517       17717 :   T->cyc = bnf_get_cyc(bnf);
    1518       17717 :   Ngen = lg(T->cyc)-1;
    1519       17717 :   if (Ngen == 0)
    1520        4788 :     T->rel = T->partrel = NULL; /* trivial Cl(K), no relations to check */
    1521             :   else
    1522             :   {
    1523       12929 :     int triv = 1;
    1524       12929 :     T->partrel = new_chunk(nPR+1);
    1525       12929 :     T->rel = new_chunk(nPR+1);
    1526       58730 :     for (i=1; i <= nPR; i++)
    1527             :     {
    1528       45801 :       GEN c = isprincipal(bnf, gel(PR,i));
    1529       45801 :       gel(T->rel,i) = c;
    1530       45801 :       if (triv && !ZV_equal0(c)) triv = 0; /* non trivial relations in Cl(K)*/
    1531             :     }
    1532             :     /* triv = 1: all ideals dividing a are principal */
    1533       12929 :     if (triv) T->rel = T->partrel = NULL;
    1534             :   }
    1535       17717 :   if (T->partrel)
    1536             :   {
    1537       10612 :     long B = ZV_max_lg(T->cyc) + 3;
    1538       59990 :     for (i = 0; i <= nPR; i++)
    1539             :     { /* T->partrel[0] also needs to be initialized */
    1540       49378 :       GEN c = cgetg(Ngen+1, t_COL); gel(T->partrel,i) = c;
    1541      134386 :       for (j=1; j<=Ngen; j++)
    1542             :       {
    1543       85008 :         GEN z = cgeti(B); gel(c,j) = z;
    1544       85008 :         z[1] = evalsigne(0)|evallgefint(B);
    1545             :       }
    1546             :     }
    1547             :   }
    1548       17717 :   T->smax = 511;
    1549       17717 :   T->normsol = new_chunk(T->smax+1);
    1550       17717 :   T->S[0] = T->n[1];
    1551       17717 :   T->next[0] = 1;
    1552       17717 :   T->sindex = 0;
    1553       17717 :   isintnorm_loop(T, 0); return 1;
    1554             : }
    1555             : 
    1556             : /* Look for unit of norm -1. Return 1 if it exists and set *unit, 0 otherwise */
    1557             : static long
    1558        3045 : get_unit_1(GEN bnf, GEN *unit)
    1559             : {
    1560        3045 :   GEN v, nf = bnf_get_nf(bnf);
    1561        3045 :   long i, n = nf_get_degree(nf);
    1562             : 
    1563        3045 :   if (DEBUGLEVEL > 2) err_printf("looking for a fundamental unit of norm -1\n");
    1564        3045 :   if (odd(n)) { *unit = gen_m1; return 1; }
    1565        1148 :   v = nfsign_units(bnf, NULL, 0);
    1566        1239 :   for (i = 1; i < lg(v); i++)
    1567        1162 :     if ( Flv_sum( gel(v,i), 2) ) { *unit = gel(bnf_get_fu(bnf), i); return 1; }
    1568          77 :   return 0;
    1569             : }
    1570             : 
    1571             : GEN
    1572       25851 : bnfisintnormabs(GEN bnf, GEN a)
    1573             : {
    1574             :   struct sol_abs T;
    1575             :   GEN nf, res, PR, F;
    1576             :   long i;
    1577             : 
    1578       25851 :   if ((F = check_arith_all(a,"bnfisintnormabs")))
    1579             :   {
    1580          56 :     a = typ(a) == t_VEC? gel(a,1): factorback(F);
    1581          56 :     if (signe(a) < 0) F = clean_Z_factor(F);
    1582             :   }
    1583       25851 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
    1584       25851 :   if (!signe(a)) return mkvec(gen_0);
    1585       25837 :   if (is_pm1(a)) return mkvec(gen_1);
    1586       25795 :   if (!F) F = absZ_factor(a);
    1587       25795 :   if (!get_sol_abs(&T, bnf, F, &PR)) return cgetg(1, t_VEC);
    1588             :   /* |a| > 1 => T.nPR > 0 */
    1589       17717 :   res = cgetg(T.sindex+1, t_VEC);
    1590       47789 :   for (i=1; i<=T.sindex; i++)
    1591             :   {
    1592       30072 :     GEN x = vecsmall_to_col( gel(T.normsol,i) );
    1593       30072 :     x = isprincipalfact(bnf, NULL, PR, x, nf_FORCE | nf_GEN_IF_PRINCIPAL);
    1594       30072 :     gel(res,i) = coltoliftalg(nf, x); /* x solution, up to sign */
    1595             :   }
    1596       17717 :   return res;
    1597             : }
    1598             : 
    1599             : /* z = bnfisintnormabs(bnf,a), sa = 1 or -1, return bnfisintnorm(bnf,sa*|a|) */
    1600             : static GEN
    1601       25858 : bnfisintnorm_i(GEN bnf, GEN a, long sa, GEN z)
    1602             : {
    1603       25858 :   GEN nf = checknf(bnf), T = nf_get_pol(nf), f = nf_get_index(nf), unit = NULL;
    1604       25858 :   GEN Tp, A = signe(a) == sa? a: negi(a);
    1605       25858 :   long sNx, i, j, N = degpol(T), l = lg(z);
    1606       25858 :   long norm_1 = 0; /* gcc -Wall */
    1607       25858 :   ulong p, Ap = 0; /* gcc -Wall */
    1608             :   forprime_t S;
    1609       25858 :   if (!signe(a)) return z;
    1610       25844 :   u_forprime_init(&S,3,ULONG_MAX);
    1611       25844 :   while((p = u_forprime_next(&S)))
    1612       34433 :     if (umodiu(f,p)) { Ap = umodiu(A,p); if (Ap) break; }
    1613       25844 :   Tp = ZX_to_Flx(T,p);
    1614             :   /* p > 2 doesn't divide A nor Q_denom(z in Z_K)*/
    1615             : 
    1616             :   /* update z in place to get correct signs: multiply by unit of norm -1 if
    1617             :    * it exists, otherwise delete solution with wrong sign */
    1618       55965 :   for (i = j = 1; i < l; i++)
    1619             :   {
    1620       30121 :     GEN x = gel(z,i);
    1621       30121 :     int xpol = (typ(x) == t_POL);
    1622             : 
    1623       30121 :     if (xpol)
    1624             :     {
    1625       30079 :       GEN dx, y = Q_remove_denom(x,&dx);
    1626       30079 :       ulong Np = Flx_resultant(Tp, ZX_to_Flx(y,p), p);
    1627       30079 :       ulong dA = dx? Fl_mul(Ap, Fl_powu(umodiu(dx,p), N, p), p): Ap;
    1628             :       /* Nx = Res(T,y) / dx^N = A or -A. Check mod p */
    1629       30079 :       sNx = dA == Np? sa: -sa;
    1630             :     }
    1631             :     else
    1632          42 :       sNx = gsigne(x) < 0 && odd(N) ? -1 : 1;
    1633       30121 :     if (sNx != sa)
    1634             :     {
    1635        5137 :       if (! unit) norm_1 = get_unit_1(bnf, &unit);
    1636        5137 :       if (!norm_1)
    1637             :       {
    1638          77 :         if (DEBUGLEVEL > 2) err_printf("%Ps eliminated because of sign\n",x);
    1639          77 :         continue;
    1640             :       }
    1641        5060 :       if (xpol) x = (unit == gen_m1)? RgX_neg(x): RgXQ_mul(unit,x,T);
    1642          14 :       else      x = (unit == gen_m1)? gneg(x): RgX_Rg_mul(unit,x);
    1643             :     }
    1644       30044 :     gel(z,j++) = x;
    1645             :   }
    1646       25844 :   setlg(z, j); return z;
    1647             : }
    1648             : GEN
    1649       25823 : bnfisintnorm(GEN bnf, GEN a)
    1650             : {
    1651       25823 :   pari_sp av = avma;
    1652       25823 :   GEN ne = bnfisintnormabs(bnf,a);
    1653       25823 :   switch(typ(a))
    1654             :   {
    1655           0 :     case t_VEC: a = gel(a,1); break;
    1656          56 :     case t_MAT: a = factorback(a); break;
    1657             :   }
    1658       25823 :   return gerepilecopy(av, bnfisintnorm_i(bnf,a,signe(a), ne));
    1659             : }

Generated by: LCOV version 1.11