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

Generated by: LCOV version 1.13