Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - buch4.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.1 lcov report (development 24988-2584e74448) Lines: 281 385 73.0 %
Date: 2020-01-26 05:57:03 Functions: 23 30 76.7 %
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             : /*******************************************************************/
      15             : /*                                                                 */
      16             : /*               S-CLASS GROUP AND NORM SYMBOLS                    */
      17             : /*          (Denis Simon, desimon@math.u-bordeaux.fr)              */
      18             : /*                                                                 */
      19             : /*******************************************************************/
      20             : #include "pari.h"
      21             : #include "paripriv.h"
      22             : 
      23             : /* p > 2, T ZX, p prime, x t_INT */
      24             : static long
      25           0 : lemma6(GEN T, GEN p, long nu, GEN x)
      26             : {
      27             :   long la, mu;
      28           0 :   pari_sp av = avma;
      29           0 :   GEN gpx, gx = poleval(T, x);
      30             : 
      31           0 :   if (Zp_issquare(gx, p)) return gc_long(av,1);
      32             : 
      33           0 :   la = Z_pval(gx, p);
      34           0 :   gpx = poleval(ZX_deriv(T), x);
      35           0 :   mu = signe(gpx)? Z_pval(gpx,p)
      36           0 :                  : la+nu+1; /* mu = +oo */
      37           0 :   set_avma(av);
      38           0 :   if (la > mu<<1) return 1;
      39           0 :   if (la >= nu<<1 && mu >= nu) return 0;
      40           0 :   return -1;
      41             : }
      42             : /* p = 2, T ZX, x t_INT: return 1 = yes, -1 = no, 0 = inconclusive */
      43             : static long
      44           0 : lemma7(GEN T, long nu, GEN x)
      45             : {
      46             :   long odd4, la, mu;
      47           0 :   pari_sp av = avma;
      48           0 :   GEN gpx, oddgx, gx = poleval(T, x);
      49             : 
      50           0 :   if (Zp_issquare(gx,gen_2)) return 1;
      51             : 
      52           0 :   gpx = poleval(ZX_deriv(T), x);
      53           0 :   la = Z_lvalrem(gx, 2, &oddgx);
      54           0 :   odd4 = umodiu(oddgx,4); set_avma(av);
      55             : 
      56           0 :   mu = vali(gpx);
      57           0 :   if (mu < 0) mu = la+nu+1; /* mu = +oo */
      58             : 
      59           0 :   if (la > mu<<1) return 1;
      60           0 :   if (nu > mu)
      61             :   {
      62           0 :     long mnl = mu+nu-la;
      63           0 :     if (odd(la)) return -1;
      64           0 :     if (mnl==1) return 1;
      65           0 :     if (mnl==2 && odd4==1) return 1;
      66             :   }
      67             :   else
      68             :   {
      69           0 :     long nu2 = nu << 1;
      70           0 :     if (la >= nu2) return 0;
      71           0 :     if (la == nu2 - 2 && odd4==1) return 0;
      72             :   }
      73           0 :   return -1;
      74             : }
      75             : 
      76             : /* T a ZX, p a prime, pnu = p^nu, x0 t_INT */
      77             : static long
      78           0 : zpsol(GEN T, GEN p, long nu, GEN pnu, GEN x0)
      79             : {
      80             :   long i, res;
      81           0 :   pari_sp av = avma, btop;
      82             :   GEN x, pnup;
      83             : 
      84           0 :   res = absequaliu(p,2)? lemma7(T,nu,x0): lemma6(T,p,nu,x0);
      85           0 :   if (res== 1) return 1;
      86           0 :   if (res==-1) return 0;
      87           0 :   x = x0; pnup = mulii(pnu,p);
      88           0 :   btop = avma;
      89           0 :   for (i=0; i < itos(p); i++)
      90             :   {
      91           0 :     x = addii(x,pnu);
      92           0 :     if (zpsol(T,p,nu+1,pnup,x)) return gc_long(av,1);
      93           0 :     if (gc_needed(btop, 2))
      94             :     {
      95           0 :       x = gerepileupto(btop, x);
      96           0 :       if (DEBUGMEM > 1)
      97           0 :         pari_warn(warnmem, "hyperell_locally_soluble: %ld/%Ps",i,p);
      98             :     }
      99             :   }
     100           0 :   return gc_long(av,0);
     101             : }
     102             : 
     103             : /* return 1 if equation y^2=T(x) has a rational p-adic solution (possibly
     104             :  * infinite), 0 otherwise. */
     105             : long
     106           0 : hyperell_locally_soluble(GEN T,GEN p)
     107             : {
     108           0 :   pari_sp av = avma;
     109             :   long res;
     110           0 :   if (typ(T)!=t_POL) pari_err_TYPE("hyperell_locally_soluble",T);
     111           0 :   if (typ(p)!=t_INT) pari_err_TYPE("hyperell_locally_soluble",p);
     112           0 :   RgX_check_ZX(T, "hyperell_locally_soluble");
     113           0 :   res = zpsol(T,p,0,gen_1,gen_0) || zpsol(RgX_recip_shallow(T), p, 1, p, gen_0);
     114           0 :   return gc_long(av, res);
     115             : }
     116             : 
     117             : /* is t a square in (O_K/pr) ? Assume v_pr(t) = 0 */
     118             : static long
     119         140 : quad_char(GEN nf, GEN t, GEN pr)
     120             : {
     121         140 :   GEN ord, ordp, T, p, modpr = zk_to_Fq_init(nf, &pr,&T,&p);
     122         140 :   t = nf_to_Fq(nf,t,modpr);
     123         140 :   if (T)
     124             :   {
     125         140 :     ord = subiu( pr_norm(pr), 1 ); /* |(O_K / pr)^*| */
     126         140 :     ordp= subiu( p, 1);            /* |F_p^*|        */
     127         140 :     t = Fq_pow(t, diviiexact(ord, ordp), T,p); /* in F_p^* */
     128         140 :     if (typ(t) == t_POL)
     129             :     {
     130         140 :       if (degpol(t)) pari_err_BUG("nfhilbertp");
     131         140 :       t = gel(t,2);
     132             :     }
     133             :   }
     134         140 :   return kronecker(t, p);
     135             : }
     136             : /* quad_char(x), x in Z, non-zero mod p */
     137             : static long
     138         154 : Z_quad_char(GEN x, GEN pr)
     139             : {
     140         154 :   long f = pr_get_f(pr);
     141         154 :   if (!odd(f)) return 1;
     142         154 :   return kronecker(x, pr_get_p(pr));
     143             : }
     144             : 
     145             : /* (pr,2) = 1. return 1 if x in Z_K is a square in Z_{K_pr}, 0 otherwise.
     146             :  * modpr = zkmodprinit(nf,pr) */
     147             : static long
     148           0 : psquarenf(GEN nf,GEN x,GEN pr,GEN modpr)
     149             : {
     150           0 :   pari_sp av = avma;
     151           0 :   GEN p = pr_get_p(pr);
     152             :   long v;
     153             : 
     154           0 :   x = nf_to_scalar_or_basis(nf, x);
     155           0 :   if (typ(x) == t_INT) {
     156           0 :     if (!signe(x)) return 1;
     157           0 :     v = Z_pvalrem(x, p, &x) * pr_get_e(pr);
     158           0 :     if (v&1) return 0;
     159           0 :     v = (Z_quad_char(x, pr) == 1);
     160             :   } else {
     161           0 :     v = ZC_nfvalrem(x, pr, &x);
     162           0 :     if (v&1) return 0;
     163           0 :     v = (quad_char(nf, x, modpr) == 1);
     164             :   }
     165           0 :   return gc_long(av,v);
     166             : }
     167             : 
     168             : /* Is  x a square in (ZK / pr^(1+2e))^* ?  pr | 2 */
     169             : static long
     170       10836 : check2(GEN nf, GEN x, GEN sprk)
     171             : {
     172       10836 :   GEN zlog = log_prk(nf, x, sprk);
     173       10836 :   long i, l = lg(zlog);
     174       39347 :   for (i=1; i<l; i++) /* all elementary divisors are even (1+2e > 1) */
     175       38948 :     if (mpodd(gel(zlog,i))) return 0;
     176         399 :   return 1;
     177             : }
     178             : 
     179             : /* pr | 2. Return 1 if x in Z_K is square in Z_{K_pr}, 0 otherwise */
     180             : static int
     181        6146 : psquare2nf_i(GEN nf,GEN x,GEN pr,GEN sprk)
     182             : {
     183        6146 :   long v = nfvalrem(nf, x, pr, &x);
     184             :   /* now (x,pr) = 1 */
     185        6146 :   return v == LONG_MAX || (!odd(v) && check2(nf,x,sprk));
     186             : }
     187             : static int
     188        6146 : psquare2nf(GEN nf,GEN x,GEN pr,GEN sprk)
     189             : {
     190        6146 :   pari_sp av = avma;
     191        6146 :   return gc_long(av, psquare2nf_i(nf,x,pr,sprk));
     192             : }
     193             : 
     194             : /*
     195             : For z in nf, z != 0.
     196             : quadratic characters modulo the prime ideal pr in nf.
     197             : pr output by nfmodprinit
     198             : pstar output by idealstar (only for p | 2).
     199             : For p odd, the output is a vector [v,c]*Mod(1,2), where
     200             : v = valuation(z,pr)
     201             : c = !issquare( z/pr^v mod pr)
     202             : For p | 2, the output is similar, with a longer sequence of 0,1 for c.
     203             : */
     204             : 
     205             : GEN
     206           0 : nf_quadchar_modpr(GEN nf, GEN z, GEN modpr, GEN pstar)
     207             : {
     208           0 :   pari_sp av = avma;
     209           0 :   GEN pr = modpr_get_pr(modpr);
     210           0 :   GEN v = stoi(nfvalrem(nf, z, pr, &z));
     211           0 :   if( equaliu(pr_get_p(pr),2))
     212             :   {
     213           0 :     GEN c = ideallog(nf, z, pstar);
     214           0 :     return gerepilecopy(av, shallowconcat(v, shallowtrans(c)));
     215             :   }
     216             :   else
     217             :   {
     218           0 :     GEN c = quad_char(nf, z, modpr)==1? gen_0: gen_1;
     219           0 :     return gerepilecopy(av, mkvec2(v,c));
     220             :   }
     221             : }
     222             : 
     223             : /* pr above an odd prime */
     224             : static long
     225           0 : lemma6nf(GEN nf, GEN T, GEN pr, long nu, GEN x, GEN modpr)
     226             : {
     227           0 :   pari_sp av = avma;
     228             :   long la, mu;
     229           0 :   GEN gpx, gx = nfpoleval(nf, T, x);
     230             : 
     231           0 :   if (psquarenf(nf,gx,pr,modpr)) return 1;
     232             : 
     233           0 :   la = nfval(nf,gx,pr);
     234           0 :   gpx = nfpoleval(nf, RgX_deriv(T), x);
     235           0 :   mu = gequal0(gpx)? la+nu+1: nfval(nf,gpx,pr);
     236           0 :   set_avma(av);
     237           0 :   if (la > (mu<<1)) return 1;
     238           0 :   if (la >= (nu<<1)  && mu >= nu) return 0;
     239           0 :   return -1;
     240             : }
     241             : /* pr above 2 */
     242             : static long
     243        5642 : lemma7nf(GEN nf, GEN T, GEN pr, long nu, GEN x, GEN sprk)
     244             : {
     245             :   long res, la, mu, q;
     246        5642 :   GEN gpx, gx = nfpoleval(nf, T, x);
     247             : 
     248        5642 :   if (psquare2nf(nf,gx,pr,sprk)) return 1;
     249             : 
     250        5509 :   gpx = nfpoleval(nf, RgX_deriv(T), x);
     251             :   /* gx /= pi^la, pi a pr-uniformizer */
     252        5509 :   la = nfvalrem(nf, gx, pr, &gx);
     253        5509 :   mu = gequal0(gpx)? la+nu+1: nfval(nf,gpx,pr);
     254             : 
     255        5509 :   if (la > (mu<<1)) return 1;
     256        5509 :   if (nu > mu)
     257             :   {
     258          35 :     if (la&1) return -1;
     259          35 :     q = mu+nu-la; res = 1;
     260             :   }
     261             :   else
     262             :   {
     263        5474 :     long nu2 = nu<<1;
     264        5474 :     if (la >= nu2) return 0;
     265        5173 :     if (odd(la)) return -1;
     266        4977 :     q = nu2-la; res = 0;
     267             :   }
     268        5012 :   if (q > pr_get_e(pr)<<1)  return -1;
     269        4935 :   if (q == 1) return res;
     270             : 
     271             :   /* is gx a square mod pi^q ? FIXME : highly inefficient */
     272        4935 :   sprk = log_prk_init(nf, pr, q);
     273        4935 :   if (!check2(nf, gx, sprk)) res = -1;
     274        4935 :   return res;
     275             : }
     276             : /* zinit either a sprk (pr | 2) or a modpr structure (pr | p odd).
     277             :    pnu = pi^nu, pi a uniformizer */
     278             : static long
     279        5642 : zpsolnf(GEN nf,GEN T,GEN pr,long nu,GEN pnu,GEN x0,GEN repr,GEN zinit)
     280             : {
     281             :   long i, res;
     282        5642 :   pari_sp av = avma;
     283             :   GEN pnup;
     284             : 
     285       11284 :   res = typ(zinit) == t_VEC? lemma7nf(nf,T,pr,nu,x0,zinit)
     286        5642 :                            : lemma6nf(nf,T,pr,nu,x0,zinit);
     287        5642 :   set_avma(av);
     288        5642 :   if (res== 1) return 1;
     289        5509 :   if (res==-1) return 0;
     290         574 :   pnup = nfmul(nf, pnu, pr_get_gen(pr));
     291         574 :   nu++;
     292        5558 :   for (i=1; i<lg(repr); i++)
     293             :   {
     294        5250 :     GEN x = nfadd(nf, x0, nfmul(nf,pnu,gel(repr,i)));
     295        5250 :     if (zpsolnf(nf,T,pr,nu,pnup,x,repr,zinit)) return gc_long(av,1);
     296             :   }
     297         308 :   return gc_long(av,0);
     298             : }
     299             : 
     300             : /* Let y = copy(x); y[k] := j; return y */
     301             : static GEN
     302        3206 : ZC_add_coeff(GEN x, long k, long j)
     303        3206 : { GEN y = shallowcopy(x); gel(y, k) = utoipos(j); return y; }
     304             : 
     305             : /* system of representatives for Zk/pr */
     306             : static GEN
     307         252 : repres(GEN nf, GEN pr)
     308             : {
     309         252 :   long f = pr_get_f(pr), N = nf_get_degree(nf), p = itos(pr_get_p(pr));
     310         252 :   long i, j, k, pi, pf = upowuu(p, f);
     311         252 :   GEN perm = pr_basis_perm(nf, pr), rep = cgetg(pf+1,t_VEC);
     312             : 
     313         252 :   gel(rep,1) = zerocol(N);
     314        1141 :   for (pi=i=1; i<=f; i++,pi*=p)
     315             :   {
     316         889 :     long t = perm[i];
     317        1778 :     for (j=1; j<p; j++)
     318         889 :       for (k=1; k<=pi; k++) gel(rep, j*pi+k) = ZC_add_coeff(gel(rep,k), t, j);
     319             :   }
     320         252 :   return rep;
     321             : }
     322             : 
     323             : /* = 1 if equation y^2 = z^deg(T) * T(x/z) has a pr-adic rational solution
     324             :  * (possibly (1,y,0) = oo), 0 otherwise.
     325             :  * coeffs of T are algebraic integers in nf */
     326             : long
     327         252 : nf_hyperell_locally_soluble(GEN nf,GEN T,GEN pr)
     328             : {
     329             :   GEN repr, zinit, p1;
     330         252 :   pari_sp av = avma;
     331             : 
     332         252 :   if (typ(T)!=t_POL) pari_err_TYPE("nf_hyperell_locally_soluble",T);
     333         252 :   if (gequal0(T)) return 1;
     334         252 :   checkprid(pr); nf = checknf(nf);
     335         252 :   if (absequaliu(pr_get_p(pr), 2))
     336             :   { /* tough case */
     337         252 :     zinit = log_prk_init(nf, pr, 1+2*pr_get_e(pr));
     338         252 :     if (psquare2nf(nf,constant_coeff(T),pr,zinit)) return 1;
     339         252 :     if (psquare2nf(nf, leading_coeff(T),pr,zinit)) return 1;
     340             :   }
     341             :   else
     342             :   {
     343           0 :     zinit = zkmodprinit(nf, pr);
     344           0 :     if (psquarenf(nf,constant_coeff(T),pr,zinit)) return 1;
     345           0 :     if (psquarenf(nf, leading_coeff(T),pr,zinit)) return 1;
     346             :   }
     347         252 :   repr = repres(nf,pr);
     348         252 :   if (zpsolnf(nf,T,pr,0,gen_1,gen_0,repr,zinit)) return gc_long(av,1);
     349         140 :   p1 = pr_get_gen(pr);
     350         140 :   if (zpsolnf(nf,RgX_recip_shallow(T),pr,1,p1,gen_0,repr,zinit))
     351          21 :     return gc_long(av,1);
     352         119 :   return gc_long(av,0);
     353             : }
     354             : 
     355             : /* return a * denom(a)^2, as an 'liftalg' */
     356             : static GEN
     357         504 : den_remove(GEN nf, GEN a)
     358             : {
     359             :   GEN da;
     360         504 :   a = nf_to_scalar_or_basis(nf, a);
     361         504 :   switch(typ(a))
     362             :   {
     363          42 :     case t_INT: return a;
     364           0 :     case t_FRAC: return mulii(gel(a,1), gel(a,2));
     365             :     case t_COL:
     366         462 :       a = Q_remove_denom(a, &da);
     367         462 :       if (da) a = ZC_Z_mul(a, da);
     368         462 :       return nf_to_scalar_or_alg(nf, a);
     369           0 :     default: pari_err_TYPE("nfhilbert",a);
     370             :       return NULL;/*LCOV_EXCL_LINE*/
     371             :   }
     372             : }
     373             : 
     374             : static long
     375         252 : hilb2nf(GEN nf,GEN a,GEN b,GEN p)
     376             : {
     377         252 :   pari_sp av = avma;
     378             :   GEN pol;
     379         252 :   a = den_remove(nf, a);
     380         252 :   b = den_remove(nf, b);
     381         252 :   pol = mkpoln(3, a, gen_0, b);
     382             :   /* varn(nf.pol) = 0, pol is not a valid GEN  [as in Pol([x,x], x)].
     383             :    * But it is only used as a placeholder, hence it is not a problem */
     384         252 :   return gc_long(av, nf_hyperell_locally_soluble(nf,pol,p)? 1: -1);
     385             : }
     386             : 
     387             : /* local quadratic Hilbert symbol (a,b)_pr, for a,b (non-zero) in nf */
     388             : static long
     389         546 : nfhilbertp(GEN nf, GEN a, GEN b, GEN pr)
     390             : {
     391             :   GEN t;
     392             :   long va, vb, rep;
     393         546 :   pari_sp av = avma;
     394             : 
     395         546 :   if (absequaliu(pr_get_p(pr), 2)) return hilb2nf(nf,a,b,pr);
     396             : 
     397             :   /* pr not above 2, compute t = tame symbol */
     398         294 :   va = nfval(nf,a,pr);
     399         294 :   vb = nfval(nf,b,pr);
     400         294 :   if (!odd(va) && !odd(vb)) return gc_long(av,1);
     401             :   /* Trick: pretend the exponent is 2, result is OK up to squares ! */
     402         294 :   t = famat_makecoprime(nf, mkvec2(a,b), mkvec2s(vb, -va),
     403             :                         pr, pr_hnf(nf, pr), gen_2);
     404         294 :   if (typ(t) == t_INT) {
     405         154 :     if (odd(va) && odd(vb)) t = negi(t);
     406             :     /* t = (-1)^(v(a)v(b)) a^v(b) b^(-v(a)) */
     407         154 :     rep = Z_quad_char(t, pr);
     408             :   }
     409         140 :   else if (ZV_isscalar(t)) {
     410           0 :     t = gel(t,1);
     411           0 :     if (odd(va) && odd(vb)) t = negi(t);
     412             :     /* t = (-1)^(v(a)v(b)) a^v(b) b^(-v(a)) */
     413           0 :     rep = Z_quad_char(t, pr);
     414             :   } else {
     415         140 :     if (odd(va) && odd(vb)) t = ZC_neg(t);
     416             :     /* t = (-1)^(v(a)v(b)) a^v(b) b^(-v(a)) */
     417         140 :     rep = quad_char(nf, t, pr);
     418             :   }
     419             :   /* quad. symbol is image of t by the quadratic character  */
     420         294 :   return gc_long(av,rep);
     421             : }
     422             : 
     423             : /* Global quadratic Hilbert symbol (a,b):
     424             :  *  =  1 if X^2 - aY^2 - bZ^2 has a point in projective plane
     425             :  *  = -1 otherwise
     426             :  * a, b should be non-zero */
     427             : long
     428           7 : nfhilbert(GEN nf, GEN a, GEN b)
     429             : {
     430           7 :   pari_sp av = avma;
     431             :   long i, l;
     432             :   GEN S, S2, Sa, Sb, sa, sb;
     433             : 
     434           7 :   nf = checknf(nf);
     435           7 :   a = nf_to_scalar_or_basis(nf, a);
     436           7 :   b = nf_to_scalar_or_basis(nf, b);
     437             :   /* local solutions in real completions ? [ error in nfsign if arg is 0 ]*/
     438           7 :   sa = nfsign(nf, a);
     439           7 :   sb = nfsign(nf, b); l = lg(sa);
     440          14 :   for (i=1; i<l; i++)
     441           7 :     if (sa[i] && sb[i])
     442             :     {
     443           0 :       if (DEBUGLEVEL>3)
     444           0 :         err_printf("nfhilbert not soluble at real place %ld\n",i);
     445           0 :       return gc_long(av,-1);
     446             :     }
     447             : 
     448             :   /* local solutions in finite completions ? (pr | 2ab)
     449             :    * primes above 2 are toughest. Try the others first */
     450           7 :   Sa = idealfactor(nf, a);
     451           7 :   Sb = idealfactor(nf, b);
     452           7 :   S2 = idealfactor(nf, gen_2);
     453           7 :   S = merge_factor(Sa, Sb, (void*)&cmp_prime_ideal, &cmp_nodata);
     454           7 :   S = merge_factor(S,  S2, (void*)&cmp_prime_ideal, &cmp_nodata);
     455           7 :   S = gel(S,1);
     456             :   /* product of all hilbertp is 1 ==> remove one prime (above 2!) */
     457           7 :   for (i=lg(S)-1; i>1; i--)
     458           7 :     if (nfhilbertp(nf,a,b,gel(S,i)) < 0)
     459             :     {
     460           7 :       if (DEBUGLEVEL>3)
     461           0 :         err_printf("nfhilbert not soluble at finite place %Ps\n",S[i]);
     462           7 :       return gc_long(av,-1);
     463             :     }
     464           0 :   return gc_long(av,1);
     465             : }
     466             : 
     467             : long
     468         560 : nfhilbert0(GEN nf,GEN a,GEN b,GEN p)
     469             : {
     470         560 :   nf = checknf(nf);
     471         560 :   if (p) {
     472         553 :     checkprid(p);
     473         553 :     if (gequal0(a)) pari_err_DOMAIN("nfhilbert", "a", "=", gen_0, a);
     474         546 :     if (gequal0(b)) pari_err_DOMAIN("nfhilbert", "b", "=", gen_0, b);
     475         539 :     return nfhilbertp(nf,a,b,p);
     476             :   }
     477           7 :   return nfhilbert(nf,a,b);
     478             : }
     479             : 
     480             : static void
     481         609 : p_append(GEN p, hashtable *H, hashtable *H2)
     482             : {
     483         609 :   ulong h = H->hash(p);
     484         609 :   hashentry *e = hash_search2(H, (void*)p, h);
     485         609 :   if (!e)
     486             :   {
     487         539 :     hash_insert2(H, (void*)p, NULL, h);
     488         539 :     if (H2) hash_insert2(H2, (void*)p, NULL, h);
     489             :   }
     490         609 : }
     491             : 
     492             : /* N a t_INT */
     493             : static void
     494         196 : Zfa_append(GEN N, hashtable *H, hashtable *H2)
     495             : {
     496         196 :   if (!is_pm1(N))
     497             :   {
     498         126 :     GEN v = gel(absZ_factor(N),1);
     499         126 :     long i, l = lg(v);
     500         126 :     for (i=1; i<l; i++) p_append(gel(v,i), H, H2);
     501             :   }
     502         196 : }
     503             : /* N a t_INT or t_FRAC or ideal in HNF*/
     504             : static void
     505         140 : fa_append(GEN N, hashtable *H, hashtable *H2)
     506             : {
     507         140 :   switch(typ(N))
     508             :   {
     509             :     case t_INT:
     510         112 :       Zfa_append(N,H,H2);
     511         112 :       break;
     512             :     case t_FRAC:
     513           0 :       Zfa_append(gel(N,1),H,H2);
     514           0 :       Zfa_append(gel(N,2),H,H2);
     515           0 :       break;
     516             :     default: /*t_MAT*/
     517          28 :       Zfa_append(gcoeff(N,1,1),H,H2);
     518          28 :       break;
     519             :   }
     520         140 : }
     521             : 
     522             : /* apply lift(rnfeltup) to all coeffs, without rnf structure */
     523             : static GEN
     524           7 : nfX_eltup(GEN nf, GEN rnfeq, GEN x)
     525             : {
     526             :   long i, l;
     527           7 :   GEN y = cgetg_copy(x, &l), zknf = nf_nfzk(nf, rnfeq);
     528           7 :   for (i=2; i<l; i++) gel(y,i) = nfeltup(nf, gel(x,i), zknf);
     529           7 :   y[1] = x[1]; return y;
     530             : }
     531             : 
     532             : static hashtable *
     533         196 : hash_create_INT(ulong s)
     534         196 : { return hash_create(s, (ulong(*)(void*))&hash_GEN,
     535             :                         (int(*)(void*,void*))&equalii, 1); }
     536             : GEN
     537          56 : rnfisnorminit(GEN T, GEN R, int galois)
     538             : {
     539          56 :   pari_sp av = avma;
     540             :   long i, l, dR;
     541             :   GEN S, gen, cyc, bnf, nf, nfabs, rnfeq, bnfabs, k, polabs;
     542          56 :   GEN y = cgetg(9, t_VEC);
     543          56 :   hashtable *H = hash_create_INT(100UL);
     544             : 
     545          56 :   if (galois < 0 || galois > 2) pari_err_FLAG("rnfisnorminit");
     546          56 :   T = get_bnfpol(T, &bnf, &nf);
     547          56 :   if (!bnf) bnf = Buchall(nf? nf: T, nf_FORCE, DEFAULTPREC);
     548          56 :   if (!nf) nf = bnf_get_nf(bnf);
     549             : 
     550          56 :   R = get_bnfpol(R, &bnfabs, &nfabs);
     551          56 :   if (!gequal1(leading_coeff(R))) pari_err_IMPL("non monic relative equation");
     552          56 :   dR = degpol(R);
     553          56 :   if (dR <= 2) galois = 1;
     554             : 
     555          56 :   R = RgX_nffix("rnfisnorminit", T, R, 1);
     556          56 :   if (nf_get_degree(nf) == 1) /* over Q */
     557          35 :     rnfeq = mkvec5(R,gen_0,gen_0,T,R);
     558          21 :   else if (galois == 2) /* needs eltup+abstorel */
     559           7 :     rnfeq = nf_rnfeq(nf, R);
     560             :   else /* needs abstorel */
     561          14 :     rnfeq = nf_rnfeqsimple(nf, R);
     562          56 :   polabs = gel(rnfeq,1);
     563          56 :   k = gel(rnfeq,3);
     564          56 :   if (!bnfabs || !gequal0(k))
     565          28 :     bnfabs = Buchall(polabs, nf_FORCE, nf_get_prec(nf));
     566          56 :   if (!nfabs) nfabs = bnf_get_nf(bnfabs);
     567             : 
     568          56 :   if (galois == 2)
     569             :   {
     570          21 :     GEN P = polabs==R? leafcopy(R): nfX_eltup(nf, rnfeq, R);
     571          21 :     setvarn(P, fetch_var_higher());
     572          21 :     galois = !!nfroots_if_split(&nfabs, P);
     573          21 :     (void)delete_var();
     574             :   }
     575             : 
     576          56 :   cyc = bnf_get_cyc(bnfabs);
     577          56 :   gen = bnf_get_gen(bnfabs); l = lg(cyc);
     578          84 :   for(i=1; i<l; i++)
     579             :   {
     580          35 :     GEN g = gel(gen,i);
     581          35 :     if (ugcdiu(gel(cyc,i), dR) == 1) break;
     582          28 :     Zfa_append(gcoeff(g,1,1), H, NULL);
     583             :   }
     584          56 :   if (!galois)
     585             :   {
     586          21 :     GEN Ndiscrel = diviiexact(nf_get_disc(nfabs), powiu(nf_get_disc(nf), dR));
     587          21 :     Zfa_append(Ndiscrel, H, NULL);
     588             :   }
     589          56 :   S = hash_keys(H); settyp(S,t_VEC);
     590          56 :   gel(y,1) = bnf;
     591          56 :   gel(y,2) = bnfabs;
     592          56 :   gel(y,3) = R;
     593          56 :   gel(y,4) = rnfeq;
     594          56 :   gel(y,5) = S;
     595          56 :   gel(y,6) = nf_pV_to_prV(nf, S);
     596          56 :   gel(y,7) = nf_pV_to_prV(nfabs, S);
     597          56 :   gel(y,8) = stoi(galois); return gerepilecopy(av, y);
     598             : }
     599             : 
     600             : /* T as output by rnfisnorminit
     601             :  * if flag=0 assume extension is Galois (==> answer is unconditional)
     602             :  * if flag>0 add to S all primes dividing p <= flag
     603             :  * if flag<0 add to S all primes dividing abs(flag)
     604             : 
     605             :  * answer is a vector v = [a,b] such that
     606             :  * x = N(a)*b and x is a norm iff b = 1  [assuming S large enough] */
     607             : GEN
     608          70 : rnfisnorm(GEN T, GEN x, long flag)
     609             : {
     610          70 :   pari_sp av = avma;
     611             :   GEN bnf, rel, R, rnfeq, nfpol;
     612             :   GEN nf, aux, H, U, Y, M, A, bnfS, sunitrel, futu, S, S1, S2, Sx;
     613             :   long L, i, itu;
     614             :   hashtable *H0, *H2;
     615          70 :   if (typ(T) != t_VEC || lg(T) != 9)
     616           0 :     pari_err_TYPE("rnfisnorm [please apply rnfisnorminit()]", T);
     617          70 :   bnf = gel(T,1);
     618          70 :   rel = gel(T,2);
     619          70 :   bnf = checkbnf(bnf);
     620          70 :   rel = checkbnf(rel);
     621          70 :   nf = bnf_get_nf(bnf);
     622          70 :   x = nf_to_scalar_or_alg(nf,x);
     623          70 :   if (gequal0(x)) { set_avma(av); return mkvec2(gen_0, gen_1); }
     624          70 :   if (gequal1(x)) { set_avma(av); return mkvec2(gen_1, gen_1); }
     625          70 :   R = gel(T,3);
     626          70 :   rnfeq = gel(T,4);
     627          70 :   if (gequalm1(x) && odd(degpol(R)))
     628           0 :   { set_avma(av); return mkvec2(gen_m1, gen_1); }
     629             : 
     630             :   /* build set T of ideals involved in the solutions */
     631          70 :   nfpol = nf_get_pol(nf);
     632          70 :   S = gel(T,5);
     633          70 :   H0 = hash_create_INT(100UL);
     634          70 :   H2 = hash_create_INT(100UL);
     635          70 :   L = lg(S);
     636          70 :   for (i = 1; i < L; i++) p_append(gel(S,i),H0,NULL);
     637          70 :   S1 = gel(T,6);
     638          70 :   S2 = gel(T,7);
     639          70 :   if (flag && !gequal0(gel(T,8)))
     640           7 :     pari_warn(warner,"useless flag in rnfisnorm: the extension is Galois");
     641          70 :   if (flag > 0)
     642             :   {
     643             :     forprime_t T;
     644             :     ulong p;
     645          14 :     u_forprime_init(&T, 2, flag);
     646          14 :     while ((p = u_forprime_next(&T))) p_append(utoipos(p), H0,H2);
     647             :   }
     648          56 :   else if (flag < 0)
     649           7 :     Zfa_append(utoipos(-flag),H0,H2);
     650             :   /* overkill: prime ideals dividing x would be enough */
     651          70 :   A = idealnumden(nf, x);
     652          70 :   fa_append(gel(A,1), H0,H2);
     653          70 :   fa_append(gel(A,2), H0,H2);
     654          70 :   Sx = hash_keys(H2); L = lg(Sx);
     655          70 :   if (L > 1)
     656             :   { /* new primes */
     657          49 :     settyp(Sx, t_VEC);
     658          49 :     S1 = shallowconcat(S1, nf_pV_to_prV(nf, Sx));
     659          49 :     S2 = shallowconcat(S2, nf_pV_to_prV(rel, Sx));
     660             :   }
     661             : 
     662             :   /* computation on T-units */
     663          70 :   futu = shallowconcat(bnf_get_fu(rel), bnf_get_tuU(rel));
     664          70 :   bnfS = bnfsunit(bnf,S1,LOWDEFAULTPREC);
     665          70 :   sunitrel = shallowconcat(futu, gel(bnfsunit(rel,S2,LOWDEFAULTPREC), 1));
     666             : 
     667          70 :   A = lift_shallow(bnfissunit(bnf,bnfS,x));
     668          70 :   L = lg(sunitrel);
     669          70 :   itu = lg(nf_get_roots(nf))-1; /* index of torsion unit in bnfsunit(nf) output */
     670          70 :   M = cgetg(L+1,t_MAT);
     671        1449 :   for (i=1; i<L; i++)
     672             :   {
     673        1379 :     GEN u = eltabstorel(rnfeq, gel(sunitrel,i));
     674        1379 :     gel(sunitrel,i) = u;
     675        1379 :     u = bnfissunit(bnf,bnfS, gnorm(u));
     676        1379 :     if (lg(u) == 1) pari_err_BUG("rnfisnorm");
     677        1379 :     gel(u,itu) = lift_shallow(gel(u,itu)); /* lift root of 1 part */
     678        1379 :     gel(M,i) = u;
     679             :   }
     680          70 :   aux = zerocol(lg(A)-1); gel(aux,itu) = utoipos( bnf_get_tuN(rel) );
     681          70 :   gel(M,L) = aux;
     682          70 :   H = ZM_hnfall(M, &U, 2);
     683          70 :   Y = RgM_RgC_mul(U, inverseimage(H,A));
     684             :   /* Y: sols of MY = A over Q */
     685          70 :   setlg(Y, L);
     686          70 :   aux = factorback2(sunitrel, gfloor(Y));
     687          70 :   x = mkpolmod(x,nfpol);
     688          70 :   if (!gequal1(aux)) x = gdiv(x, gnorm(aux));
     689          70 :   x = lift_if_rational(x);
     690          70 :   if (typ(aux) == t_POLMOD && degpol(nfpol) == 1)
     691          21 :     gel(aux,2) = lift_if_rational(gel(aux,2));
     692          70 :   return gerepilecopy(av, mkvec2(aux, x));
     693             : }
     694             : 
     695             : GEN
     696          28 : bnfisnorm(GEN bnf, GEN x, long flag)
     697             : {
     698          28 :   pari_sp av = avma;
     699          28 :   GEN T = rnfisnorminit(pol_x(fetch_var()), bnf, flag == 0? 1: 2);
     700          28 :   GEN r = rnfisnorm(T, x, flag == 1? 0: flag);
     701          28 :   (void)delete_var();
     702          28 :   return gerepileupto(av,r);
     703             : }

Generated by: LCOV version 1.13