Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - buch4.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.8.0 lcov report (development 19623-dc26710) Lines: 398 489 81.4 %
Date: 2016-09-30 05:54:20 Functions: 27 33 81.8 %
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)) { avma = av; return 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 :   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); 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;
      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 :   for (i=0; i < itos(p); i++)
      89             :   {
      90           0 :     x = addii(x,pnu);
      91           0 :     if (zpsol(T,p,nu+1,pnup,x)) { avma = av; return 1; }
      92             :   }
      93           0 :   avma = av; return 0;
      94             : }
      95             : 
      96             : /* return 1 if equation y^2=T(x) has a rational p-adic solution (possibly
      97             :  * infinite), 0 otherwise. */
      98             : long
      99           0 : hyperell_locally_soluble(GEN T,GEN p)
     100             : {
     101           0 :   pari_sp av = avma;
     102             :   long res;
     103           0 :   if (typ(T)!=t_POL) pari_err_TYPE("zpsoluble",T);
     104           0 :   if (typ(p)!=t_INT) pari_err_TYPE("zpsoluble",p);
     105           0 :   RgX_check_ZX(T, "zpsoluble");
     106           0 :   res = zpsol(T,p,0,gen_1,gen_0) || zpsol(RgX_recip_shallow(T), p, 1, p, gen_0);
     107           0 :   avma = av; return res;
     108             : }
     109             : 
     110             : /* is t a square in (O_K/pr) ? Assume v_pr(t) = 0 */
     111             : static long
     112         140 : quad_char(GEN nf, GEN t, GEN pr)
     113             : {
     114         140 :   GEN ord, ordp, T, p, modpr = zk_to_Fq_init(nf, &pr,&T,&p);
     115         140 :   t = nf_to_Fq(nf,t,modpr);
     116         140 :   if (T)
     117             :   {
     118         140 :     ord = subis( pr_norm(pr), 1 ); /* |(O_K / pr)^*| */
     119         140 :     ordp= subis( p, 1);            /* |F_p^*|        */
     120         140 :     t = Fq_pow(t, diviiexact(ord, ordp), T,p); /* in F_p^* */
     121         140 :     if (typ(t) == t_POL)
     122             :     {
     123         140 :       if (degpol(t)) pari_err_BUG("nfhilbertp");
     124         140 :       t = gel(t,2);
     125             :     }
     126             :   }
     127         140 :   return kronecker(t, p);
     128             : }
     129             : /* quad_char(x), x in Z, non-zero mod p */
     130             : static long
     131         154 : Z_quad_char(GEN x, GEN pr)
     132             : {
     133         154 :   long f = pr_get_f(pr);
     134         154 :   if (!odd(f)) return 1;
     135         154 :   return kronecker(x, pr_get_p(pr));
     136             : }
     137             : 
     138             : /* (pr,2) = 1. return 1 if x in Z_K is a square in Z_{K_pr}, 0 otherwise.
     139             :  * modpr = zkmodprinit(nf,pr) */
     140             : static long
     141           0 : psquarenf(GEN nf,GEN x,GEN pr,GEN modpr)
     142             : {
     143           0 :   pari_sp av = avma;
     144           0 :   GEN p = pr_get_p(pr);
     145             :   long v;
     146             : 
     147           0 :   x = nf_to_scalar_or_basis(nf, x);
     148           0 :   if (typ(x) == t_INT) {
     149           0 :     if (!signe(x)) return 1;
     150           0 :     v = Z_pvalrem(x, p, &x) * pr_get_e(pr);
     151           0 :     if (v&1) return 0;
     152           0 :     v = (Z_quad_char(x, pr) == 1);
     153             :   } else {
     154           0 :     v = ZC_nfvalrem(nf, x, pr, &x);
     155           0 :     if (v&1) return 0;
     156           0 :     v = (quad_char(nf, x, modpr) == 1);
     157             :   }
     158           0 :   avma = av; return v;
     159             : }
     160             : 
     161             : /* Is  x a square in (ZK / pr^(1+2e))^* ?  pr | 2 */
     162             : static long
     163       10983 : check2(GEN nf, GEN x, GEN zinit)
     164             : {
     165       10983 :   GEN zlog = ideallog(nf, x, zinit);
     166       10983 :   long i, l = lg(zlog);
     167       40726 :   for (i=1; i<l; i++) /* all elementary divisors are even (1+2e > 1) */
     168       40278 :     if (mpodd(gel(zlog,i))) return 0;
     169         448 :   return 1;
     170             : }
     171             : 
     172             : /* pr | 2. Return 1 if x in Z_K is square in Z_{K_pr}, 0 otherwise */
     173             : static int
     174        6279 : psquare2nf_i(GEN nf,GEN x,GEN pr,GEN zinit)
     175             : {
     176        6279 :   long v = nfvalrem(nf, x, pr, &x);
     177             :   /* now (x,pr) = 1 */
     178        6279 :   return v == LONG_MAX || (!odd(v) && check2(nf,x,zinit));
     179             : }
     180             : static int
     181        6279 : psquare2nf(GEN nf,GEN x,GEN pr,GEN zinit)
     182             : {
     183        6279 :   pari_sp av = avma;
     184        6279 :   long v = psquare2nf_i(nf,x,pr,zinit);
     185        6279 :   avma = av; return v;
     186             : }
     187             : 
     188             : /* pr above an odd prime */
     189             : static long
     190           0 : lemma6nf(GEN nf, GEN T, GEN pr, long nu, GEN x, GEN modpr)
     191             : {
     192           0 :   pari_sp av = avma;
     193             :   long la, mu;
     194           0 :   GEN gpx, gx = nfpoleval(nf, T, x);
     195             : 
     196           0 :   if (psquarenf(nf,gx,pr,modpr)) return 1;
     197             : 
     198           0 :   la = nfval(nf,gx,pr);
     199           0 :   gpx = nfpoleval(nf, RgX_deriv(T), x);
     200           0 :   mu = gequal0(gpx)? la+nu+1: nfval(nf,gpx,pr);
     201           0 :   avma = av;
     202           0 :   if (la > (mu<<1)) return 1;
     203           0 :   if (la >= (nu<<1)  && mu >= nu) return 0;
     204           0 :   return -1;
     205             : }
     206             : /* pr above 2 */
     207             : static long
     208        5775 : lemma7nf(GEN nf, GEN T, GEN pr, long nu, GEN x, GEN zinit)
     209             : {
     210             :   long res, la, mu, q;
     211        5775 :   GEN gpx, gx = nfpoleval(nf, T, x);
     212             : 
     213        5775 :   if (psquare2nf(nf,gx,pr,zinit)) return 1;
     214             : 
     215        5642 :   gpx = nfpoleval(nf, RgX_deriv(T), x);
     216             :   /* gx /= pi^la, pi a pr-uniformizer */
     217        5642 :   la = nfvalrem(nf, gx, pr, &gx);
     218        5642 :   mu = gequal0(gpx)? la+nu+1: nfval(nf,gpx,pr);
     219             : 
     220        5642 :   if (la > (mu<<1)) return 1;
     221        5642 :   if (nu > mu)
     222             :   {
     223         119 :     if (la&1) return -1;
     224         119 :     q = mu+nu-la; res = 1;
     225             :   }
     226             :   else
     227             :   {
     228        5523 :     long nu2 = nu<<1;
     229        5523 :     if (la >= nu2) return 0;
     230        5222 :     if (odd(la)) return -1;
     231        5026 :     q = nu2-la; res = 0;
     232             :   }
     233        5145 :   if (q > pr_get_e(pr)<<1)  return -1;
     234        4949 :   if (q == 1) return res;
     235             : 
     236             :   /* is gx a square mod pi^q ? FIXME : highly inefficient */
     237        4949 :   zinit = Idealstarprk(nf, pr, q, nf_INIT);
     238        4949 :   if (!check2(nf, gx, zinit)) res = -1;
     239        4949 :   return res;
     240             : }
     241             : /* zinit either a bid (pr | 2) or a modpr structure (pr | p odd).
     242             :    pnu = pi^nu, pi a uniformizer */
     243             : static long
     244        5775 : zpsolnf(GEN nf,GEN T,GEN pr,long nu,GEN pnu,GEN x0,GEN repr,GEN zinit)
     245             : {
     246             :   long i, res;
     247        5775 :   pari_sp av = avma;
     248             :   GEN pnup;
     249             : 
     250       11550 :   res = typ(zinit) == t_VEC? lemma7nf(nf,T,pr,nu,x0,zinit)
     251        5775 :                            : lemma6nf(nf,T,pr,nu,x0,zinit);
     252        5775 :   avma = av;
     253        5775 :   if (res== 1) return 1;
     254        5642 :   if (res==-1) return 0;
     255         623 :   pnup = nfmul(nf, pnu, pr_get_gen(pr));
     256         623 :   nu++;
     257        5691 :   for (i=1; i<lg(repr); i++)
     258             :   {
     259        5383 :     GEN x = nfadd(nf, x0, nfmul(nf,pnu,gel(repr,i)));
     260        5383 :     if (zpsolnf(nf,T,pr,nu,pnup,x,repr,zinit)) { avma = av; return 1; }
     261             :   }
     262         308 :   avma = av; return 0;
     263             : }
     264             : 
     265             : /* Let y = copy(x); y[k] := j; return y */
     266             : static GEN
     267        3206 : ZC_add_coeff(GEN x, long k, long j)
     268        3206 : { GEN y = shallowcopy(x); gel(y, k) = utoi(j); return y; }
     269             : 
     270             : /* system of representatives for Zk/pr */
     271             : static GEN
     272         252 : repres(GEN nf, GEN pr)
     273             : {
     274         252 :   long f = pr_get_f(pr), N = nf_get_degree(nf), p = itos(pr_get_p(pr));
     275         252 :   long i, j, k, pi, pf = upowuu(p, f);
     276         252 :   GEN rep, perm = cgetg(f+1, t_VECSMALL);
     277             : 
     278         252 :   perm[1] = 1;
     279         252 :   if (f > 1) {
     280         217 :     GEN H = idealhnf_two(nf,pr);
     281         854 :     for (i = k = 2; k <= f; i++)
     282             :     {
     283         637 :       if (is_pm1(gcoeff(H,i,i))) continue;
     284         637 :       perm[k++] = i;
     285             :     }
     286             :   }
     287         252 :   rep = cgetg(pf+1,t_VEC);
     288         252 :   gel(rep,1) = zerocol(N);
     289        1141 :   for (pi=i=1; i<=f; i++,pi*=p)
     290             :   {
     291         889 :     long t = perm[i];
     292        1778 :     for (j=1; j<p; j++)
     293         889 :       for (k=1; k<=pi; k++) gel(rep, j*pi+k) = ZC_add_coeff(gel(rep,k), t, j);
     294             :   }
     295         252 :   return rep;
     296             : }
     297             : 
     298             : /* = 1 if equation y^2 = z^deg(T) * T(x/z) has a pr-adic rational solution
     299             :  * (possibly (1,y,0) = oo), 0 otherwise.
     300             :  * coeffs of T are algebraic integers in nf */
     301             : long
     302         252 : nf_hyperell_locally_soluble(GEN nf,GEN T,GEN pr)
     303             : {
     304             :   GEN repr, zinit, p1;
     305         252 :   pari_sp av = avma;
     306             : 
     307         252 :   if (typ(T)!=t_POL) pari_err_TYPE("nf_hyperell_locally_soluble",T);
     308         252 :   if (gequal0(T)) return 1;
     309         252 :   checkprid(pr); nf = checknf(nf);
     310         252 :   if (absequaliu(pr_get_p(pr), 2))
     311             :   { /* tough case */
     312         252 :     zinit = Idealstarprk(nf, pr, 1+2*pr_get_e(pr), nf_INIT);
     313         252 :     if (psquare2nf(nf,constant_coeff(T),pr,zinit)) return 1;
     314         252 :     if (psquare2nf(nf, leading_coeff(T),pr,zinit)) return 1;
     315             :   }
     316             :   else
     317             :   {
     318           0 :     zinit = zkmodprinit(nf, pr);
     319           0 :     if (psquarenf(nf,constant_coeff(T),pr,zinit)) return 1;
     320           0 :     if (psquarenf(nf, leading_coeff(T),pr,zinit)) return 1;
     321             :   }
     322         252 :   repr = repres(nf,pr);
     323         252 :   if (zpsolnf(nf,T,pr,0,gen_1,gen_0,repr,zinit)) { avma=av; return 1; }
     324         140 :   p1 = pr_get_gen(pr);
     325         140 :   if (zpsolnf(nf,RgX_recip_shallow(T),pr,1,p1,gen_0,repr,zinit)) { avma=av; return 1; }
     326             : 
     327         119 :   avma = av; return 0;
     328             : }
     329             : 
     330             : /* return a * denom(a)^2, as an 'liftalg' */
     331             : static GEN
     332         504 : den_remove(GEN nf, GEN a)
     333             : {
     334             :   GEN da;
     335         504 :   a = nf_to_scalar_or_basis(nf, a);
     336         504 :   switch(typ(a))
     337             :   {
     338          42 :     case t_INT: return a;
     339           0 :     case t_FRAC: return mulii(gel(a,1), gel(a,2));
     340             :     case t_COL:
     341         462 :       a = Q_remove_denom(a, &da);
     342         462 :       if (da) a = ZC_Z_mul(a, da);
     343         462 :       a = coltoliftalg(nf, a);
     344         462 :       return a;
     345           0 :     default: pari_err_TYPE("nfhilbert",a);
     346           0 :       return NULL;/*not reached*/
     347             :   }
     348             : }
     349             : 
     350             : static long
     351         252 : hilb2nf(GEN nf,GEN a,GEN b,GEN p)
     352             : {
     353         252 :   pari_sp av = avma;
     354             :   long rep;
     355             :   GEN pol;
     356             : 
     357         252 :   a = den_remove(nf, a);
     358         252 :   b = den_remove(nf, b);
     359         252 :   pol = mkpoln(3, a, gen_0, b);
     360             :   /* varn(nf.pol) = 0, pol is not a valid GEN  [as in Pol([x,x], x)].
     361             :    * But it is only used as a placeholder, hence it is not a problem */
     362             : 
     363         252 :   rep = nf_hyperell_locally_soluble(nf,pol,p)? 1: -1;
     364         252 :   avma = av; return rep;
     365             : }
     366             : 
     367             : /* local quadratic Hilbert symbol (a,b)_pr, for a,b (non-zero) in nf */
     368             : static long
     369         546 : nfhilbertp(GEN nf, GEN a, GEN b, GEN pr)
     370             : {
     371             :   GEN t;
     372             :   long va, vb, rep;
     373         546 :   pari_sp av = avma;
     374             : 
     375         546 :   if (absequaliu(pr_get_p(pr), 2)) return hilb2nf(nf,a,b,pr);
     376             : 
     377             :   /* pr not above 2, compute t = tame symbol */
     378         294 :   va = nfval(nf,a,pr);
     379         294 :   vb = nfval(nf,b,pr);
     380         294 :   if (!odd(va) && !odd(vb)) { avma = av; return 1; }
     381             :   /* Trick: pretend the exponent is 2, result is OK up to squares ! */
     382         294 :   t = famat_makecoprime(nf, mkvec2(a,b), mkvec2s(vb, -va),
     383             :                         pr, idealhnf_two(nf, pr), gen_2);
     384         294 :   if (typ(t) == t_INT) {
     385         154 :     if (odd(va) && odd(vb)) t = negi(t);
     386             :     /* t = (-1)^(v(a)v(b)) a^v(b) b^(-v(a)) */
     387         154 :     rep = Z_quad_char(t, pr);
     388             :   }
     389         140 :   else if (ZV_isscalar(t)) {
     390           0 :     t = gel(t,1);
     391           0 :     if (odd(va) && odd(vb)) t = negi(t);
     392             :     /* t = (-1)^(v(a)v(b)) a^v(b) b^(-v(a)) */
     393           0 :     rep = Z_quad_char(t, pr);
     394             :   } else {
     395         140 :     if (odd(va) && odd(vb)) t = ZC_neg(t);
     396             :     /* t = (-1)^(v(a)v(b)) a^v(b) b^(-v(a)) */
     397         140 :     rep = quad_char(nf, t, pr);
     398             :   }
     399             :   /* quad. symbol is image of t by the quadratic character  */
     400         294 :   avma = av; return rep;
     401             : }
     402             : 
     403             : /* Global quadratic Hilbert symbol (a,b):
     404             :  *  =  1 if X^2 - aY^2 - bZ^2 has a point in projective plane
     405             :  *  = -1 otherwise
     406             :  * a, b should be non-zero */
     407             : long
     408           7 : nfhilbert(GEN nf, GEN a, GEN b)
     409             : {
     410           7 :   pari_sp av = avma;
     411             :   long i, l;
     412             :   GEN S, S2, Sa, Sb, sa, sb;
     413             : 
     414           7 :   nf = checknf(nf);
     415           7 :   a = nf_to_scalar_or_basis(nf, a);
     416           7 :   b = nf_to_scalar_or_basis(nf, b);
     417             :   /* local solutions in real completions ? [ error in nfsign if arg is 0 ]*/
     418           7 :   sa = nfsign(nf, a);
     419           7 :   sb = nfsign(nf, b); l = lg(sa);
     420          14 :   for (i=1; i<l; i++)
     421           7 :     if (sa[i] && sb[i])
     422             :     {
     423           0 :       if (DEBUGLEVEL>3)
     424           0 :         err_printf("nfhilbert not soluble at real place %ld\n",i);
     425           0 :       avma = av; return -1;
     426             :     }
     427             : 
     428             :   /* local solutions in finite completions ? (pr | 2ab)
     429             :    * primes above 2 are toughest. Try the others first */
     430           7 :   Sa = idealfactor(nf, a);
     431           7 :   Sb = idealfactor(nf, b);
     432           7 :   S2 = idealfactor(nf, gen_2);
     433           7 :   S = merge_factor(Sa, Sb, (void*)&cmp_prime_ideal, &cmp_nodata);
     434           7 :   S = merge_factor(S,  S2, (void*)&cmp_prime_ideal, &cmp_nodata);
     435           7 :   S = gel(S,1);
     436             :   /* product of all hilbertp is 1 ==> remove one prime (above 2!) */
     437           7 :   for (i=lg(S)-1; i>1; i--)
     438           7 :     if (nfhilbertp(nf,a,b,gel(S,i)) < 0)
     439             :     {
     440           7 :       if (DEBUGLEVEL>3)
     441           0 :         err_printf("nfhilbert not soluble at finite place %Ps\n",S[i]);
     442           7 :       avma = av; return -1;
     443             :     }
     444           0 :   avma = av; return 1;
     445             : }
     446             : 
     447             : long
     448         560 : nfhilbert0(GEN nf,GEN a,GEN b,GEN p)
     449             : {
     450         560 :   nf = checknf(nf);
     451         560 :   if (p) {
     452         553 :     checkprid(p);
     453         553 :     if (gequal0(a)) pari_err_DOMAIN("nfhilbert", "a", "=", gen_0, a);
     454         546 :     if (gequal0(b)) pari_err_DOMAIN("nfhilbert", "b", "=", gen_0, b);
     455         539 :     return nfhilbertp(nf,a,b,p);
     456             :   }
     457           7 :   return nfhilbert(nf,a,b);
     458             : }
     459             : 
     460             : /* S a list of prime ideal in idealprimedec format. Return res:
     461             :  * res[1] = generators of (S-units / units), as polynomials
     462             :  * res[2] = [perm, HB, den], for bnfissunit
     463             :  * res[3] = [] (was: log. embeddings of res[1])
     464             :  * res[4] = S-regulator ( = R * det(res[2]) * \prod log(Norm(S[i])))
     465             :  * res[5] = S class group
     466             :  * res[6] = S */
     467             : GEN
     468         315 : bnfsunit0(GEN bnf, GEN S, long flag, long prec)
     469             : {
     470         315 :   pari_sp av = avma;
     471             :   long i,j,ls;
     472             :   GEN p1,nf,gen,M,U,H;
     473             :   GEN sunit,card,sreg,res,pow;
     474             : 
     475         315 :   if (!is_vec_t(typ(S))) pari_err_TYPE("bnfsunit",S);
     476         315 :   bnf = checkbnf(bnf);
     477         315 :   nf = bnf_get_nf(bnf);
     478         315 :   gen = bnf_get_gen(bnf);
     479             : 
     480         315 :   sreg = bnf_get_reg(bnf);
     481         315 :   res=cgetg(7,t_VEC);
     482         315 :   gel(res,1) = gel(res,2) = gel(res,3) = cgetg(1,t_VEC);
     483         315 :   gel(res,4) = sreg;
     484         315 :   gel(res,5) = bnf_get_clgp(bnf);
     485         315 :   gel(res,6) = S; ls=lg(S);
     486             : 
     487             :  /* M = relation matrix for the S class group (in terms of the class group
     488             :   * generators given by gen)
     489             :   * 1) ideals in S
     490             :   */
     491         315 :   M = cgetg(ls,t_MAT);
     492        2583 :   for (i=1; i<ls; i++)
     493             :   {
     494        2268 :     p1 = gel(S,i); checkprid(p1);
     495        2268 :     gel(M,i) = isprincipal(bnf,p1);
     496             :   }
     497             :   /* 2) relations from bnf class group */
     498         315 :   M = shallowconcat(M, diagonal_shallow(bnf_get_cyc(bnf)));
     499             : 
     500             :   /* S class group */
     501         315 :   H = ZM_hnfall(M, &U, 1);
     502         315 :   card = gen_1;
     503         315 :   if (lg(H) > 1)
     504             :   { /* non trivial (rare!) */
     505         217 :     GEN A, u, D = ZM_snfall_i(H, &u, NULL, 1);
     506             :     long l;
     507         217 :     ZV_snf_trunc(D); l = lg(D);
     508         217 :     card = ZV_prod(D);
     509         217 :     A = cgetg(l, t_VEC); pow = ZM_inv(u,gen_1);
     510         217 :     for(i = 1; i < l; i++) gel(A,i) = idealfactorback(nf, gen, gel(pow,i), 1);
     511         217 :     gel(res,5) = mkvec3(card, D, A);
     512             :   }
     513             : 
     514             :   /* S-units */
     515         315 :   if (ls>1)
     516             :   {
     517         315 :     GEN den, Sperm, perm, dep, B, A, U1 = U;
     518         315 :     long lH, lB, FLAG = flag|nf_FORCE;
     519             : 
     520             :    /* U1 = upper left corner of U, invertible. S * U1 = principal ideals
     521             :     * whose generators generate the S-units */
     522         315 :     setlg(U1,ls); p1 = cgetg(ls, t_MAT); /* p1 is junk for mathnfspec */
     523         315 :     for (i=1; i<ls; i++) { setlg(U1[i],ls); gel(p1,i) = cgetg(1,t_COL); }
     524         315 :     H = mathnfspec(U1,&perm,&dep,&B,&p1);
     525         315 :     lH = lg(H);
     526         315 :     lB = lg(B);
     527         315 :     if (lg(dep) > 1 && lgcols(dep) > 1) pari_err_BUG("bnfsunit");
     528             :    /*                   [ H B  ]            [ H^-1   - H^-1 B ]
     529             :     * perm o HNF(U1) =  [ 0 Id ], inverse = [  0         Id   ]
     530             :     * (permute the rows)
     531             :     * S * HNF(U1) = _integral_ generators for S-units  = sunit */
     532         315 :     Sperm = cgetg(ls, t_VEC); sunit = cgetg(ls, t_VEC);
     533         315 :     for (i=1; i<ls; i++) Sperm[i] = S[perm[i]]; /* S o perm */
     534             : 
     535         315 :     setlg(Sperm, lH);
     536         469 :     for (i=1; i<lH; i++)
     537             :     {
     538         154 :       GEN v = isprincipalfact(bnf, NULL,Sperm,gel(H,i), FLAG);
     539         154 :       v = gel(v,2); if (flag == nf_GEN) v = nf_to_scalar_or_alg(nf, v);
     540         154 :       gel(sunit,i) = v;
     541             :     }
     542        2429 :     for (j=1; j<lB; j++,i++)
     543             :     {
     544        2114 :       GEN v = isprincipalfact(bnf, gel(Sperm,i),Sperm,gel(B,j),FLAG);
     545        2114 :       v = gel(v,2); if (flag == nf_GEN) v = nf_to_scalar_or_alg(nf, v);
     546        2114 :       gel(sunit,i) = v;
     547             :    }
     548         315 :     den = ZM_det_triangular(H); H = ZM_inv(H,den);
     549         315 :     A = shallowconcat(H, ZM_neg(ZM_mul(H,B))); /* top part of inverse * den */
     550             :     /* HNF in split form perm + (H B) [0 Id missing] */
     551         315 :     gel(res,1) = sunit;
     552         315 :     gel(res,2) = mkvec3(perm,A,den);
     553             :   }
     554             : 
     555             :   /* S-regulator */
     556         315 :   sreg = mpmul(sreg,card);
     557        2583 :   for (i=1; i<ls; i++)
     558             :   {
     559        2268 :     GEN p = pr_get_p( gel(S,i) );
     560        2268 :     sreg = mpmul(sreg, logr_abs(itor(p,prec)));
     561             :   }
     562         315 :   gel(res,4) = sreg;
     563         315 :   return gerepilecopy(av,res);
     564             : }
     565             : GEN
     566         147 : bnfsunit(GEN bnf,GEN S,long prec) { return bnfsunit0(bnf,S,nf_GEN,prec); }
     567             : 
     568             : static GEN
     569        1470 : make_unit(GEN nf, GEN bnfS, GEN *px)
     570             : {
     571             :   long lB, cH, i, ls;
     572             :   GEN den, gen, S, v, p1, xp, xb, N, N0, HB, perm;
     573             : 
     574        1470 :   if (gequal0(*px)) return NULL;
     575        1470 :   S = gel(bnfS,6); ls = lg(S);
     576        1470 :   if (ls==1) return cgetg(1, t_COL);
     577             : 
     578        1470 :   xb = nf_to_scalar_or_basis(nf,*px);
     579        1470 :   switch(typ(xb))
     580             :   {
     581         497 :     case t_INT:  N = xb; break;
     582           0 :     case t_FRAC: N = mulii(gel(xb,1),gel(xb,2)); break;
     583         973 :     default: { GEN d = Q_denom(xb); N = mulii(idealnorm(nf,gmul(*px,d)), d); }
     584             :   } /* relevant primes divide N */
     585        1470 :   if (is_pm1(N)) return zerocol(ls -1);
     586             : 
     587        1197 :   p1 = gel(bnfS,2);
     588        1197 :   perm = gel(p1,1);
     589        1197 :   HB   = gel(p1,2);
     590        1197 :   den  = gel(p1,3);
     591        1197 :   cH = nbrows(HB);
     592        1197 :   lB = lg(HB) - cH;
     593        1197 :   v = zero_zv(ls-1);
     594        1197 :   N0 = N;
     595       36988 :   for (i=1; i<ls; i++)
     596             :   {
     597       35791 :     GEN P = gel(S,i), p = pr_get_p(P);
     598       35791 :     if ( remii(N, p) == gen_0 )
     599             :     {
     600        2499 :       v[i] = nfval(nf,xb,P);
     601        2499 :       (void)Z_pvalrem(N0, p, &N0);
     602             :     }
     603             :   }
     604        1197 :   if (!is_pm1(N0)) return NULL;
     605             :   /* here, x = S v */
     606        1190 :   p1 = vecsmallpermute(v, perm);
     607        1190 :   v = ZM_zc_mul(HB, p1);
     608        1554 :   for (i=1; i<=cH; i++)
     609             :   {
     610         364 :     GEN r, w = dvmdii(gel(v,i), den, &r);
     611         364 :     if (r != gen_0) return NULL;
     612         364 :     gel(v,i) = w;
     613             :   }
     614        1190 :   p1 += cH; p1[0] = evaltyp(t_VECSMALL) | evallg(lB);
     615        1190 :   v = shallowconcat(v, zc_to_ZC(p1)); /* append bottom of p1 (= [0 Id] part) */
     616             : 
     617        1190 :   gen = gel(bnfS,1);
     618        1190 :   xp = cgetg(1, t_MAT);
     619       36946 :   for (i=1; i<ls; i++)
     620             :   {
     621       35756 :     GEN e = gel(v,i);
     622       35756 :     if (!signe(e)) continue;
     623        1631 :     xp = famat_mul(xp, to_famat_shallow(gel(gen,i), negi(e)));
     624             :   }
     625        1190 :   if (lg(xp) > 1) *px = famat_mul(xp, to_famat_shallow(xb, gen_1));
     626        1190 :   return v;
     627             : }
     628             : 
     629             : /* Analog to bnfisunit, for S-units. Let v the result
     630             :  * If x not an S-unit, v = []~, else
     631             :  * x = \prod_{i=0}^r e_i^v[i] * prod{i=r+1}^{r+s} s_i^v[i]
     632             :  * where the e_i are the field units (cf bnfisunit), and the s_i are
     633             :  * the S-units computed by bnfsunit (in the same order) */
     634             : GEN
     635        1470 : bnfissunit(GEN bnf,GEN bnfS,GEN x)
     636             : {
     637        1470 :   pari_sp av = avma;
     638             :   GEN v, w, nf;
     639             : 
     640        1470 :   bnf = checkbnf(bnf);
     641        1470 :   nf = bnf_get_nf(bnf);
     642        1470 :   if (typ(bnfS)!=t_VEC || lg(bnfS)!=7) pari_err_TYPE("bnfissunit",bnfS);
     643        1470 :   x = nf_to_scalar_or_alg(nf,x);
     644        1470 :   v = NULL;
     645        1470 :   if ( (w = make_unit(nf, bnfS, &x)) ) v = bnfisunit(bnf, x);
     646        1470 :   if (!v || lg(v) == 1) { avma = av; return cgetg(1,t_COL); }
     647        1463 :   return gerepileupto(av, gconcat(v, w));
     648             : }
     649             : 
     650             : static void
     651         609 : p_append(GEN p, hashtable *H, hashtable *H2)
     652             : {
     653         609 :   ulong h = H->hash(p);
     654         609 :   hashentry *e = hash_search2(H, (void*)p, h);
     655         609 :   if (!e)
     656             :   {
     657         539 :     hash_insert2(H, (void*)p, NULL, h);
     658         539 :     if (H2) hash_insert2(H2, (void*)p, NULL, h);
     659             :   }
     660         609 : }
     661             : 
     662             : /* N a t_INT */
     663             : static void
     664         196 : Zfa_append(GEN N, hashtable *H, hashtable *H2)
     665             : {
     666         196 :   if (!is_pm1(N))
     667             :   {
     668         126 :     GEN v = gel(absZ_factor(N),1);
     669         126 :     long i, l = lg(v);
     670         126 :     for (i=1; i<l; i++) p_append(gel(v,i), H, H2);
     671             :   }
     672         196 : }
     673             : /* N a t_INT or t_FRAC or ideal in HNF*/
     674             : static void
     675         140 : fa_append(GEN N, hashtable *H, hashtable *H2)
     676             : {
     677         140 :   switch(typ(N))
     678             :   {
     679             :     case t_INT:
     680         112 :       Zfa_append(N,H,H2);
     681         112 :       break;
     682             :     case t_FRAC:
     683           0 :       Zfa_append(gel(N,1),H,H2);
     684           0 :       Zfa_append(gel(N,2),H,H2);
     685           0 :       break;
     686             :     default: /*t_MAT*/
     687          28 :       Zfa_append(gcoeff(N,1,1),H,H2);
     688          28 :       break;
     689             :   }
     690         140 : }
     691             : 
     692             : /* apply lift(rnfeltup) to all coeffs, without rnf structure */
     693             : static GEN
     694           7 : nfX_eltup(GEN nf, GEN rnfeq, GEN x)
     695             : {
     696             :   long i, l;
     697           7 :   GEN zknf, czknf, y = cgetg_copy(x, &l);
     698           7 :   y[1] = x[1]; nf_nfzk(nf, rnfeq, &zknf, &czknf);
     699           7 :   for (i=2; i<l; i++) gel(y,i) = nfeltup(nf, gel(x,i), zknf, czknf);
     700           7 :   return y;
     701             : }
     702             : 
     703             : static hashtable *
     704         196 : hash_create_INT(ulong s)
     705         196 : { return hash_create(s, (ulong(*)(void*))&hash_GEN,
     706             :                         (int(*)(void*,void*))&equalii, 1); }
     707             : GEN
     708          56 : rnfisnorminit(GEN T, GEN relpol, int galois)
     709             : {
     710          56 :   pari_sp av = avma;
     711             :   long i, l, drel;
     712             :   GEN S, gen, cyc, bnf, nf, nfabs, rnfeq, bnfabs, k, polabs;
     713          56 :   GEN y = cgetg(9, t_VEC);
     714          56 :   hashtable *H = hash_create_INT(100UL);
     715             : 
     716          56 :   if (galois < 0 || galois > 2) pari_err_FLAG("rnfisnorminit");
     717          56 :   T = get_bnfpol(T, &bnf, &nf);
     718          56 :   if (!bnf) bnf = Buchall(nf? nf: T, nf_FORCE, DEFAULTPREC);
     719          56 :   if (!nf) nf = bnf_get_nf(bnf);
     720             : 
     721          56 :   relpol = get_bnfpol(relpol, &bnfabs, &nfabs);
     722          56 :   if (!gequal1(leading_coeff(relpol))) pari_err_IMPL("non monic relative equation");
     723          56 :   drel = degpol(relpol);
     724          56 :   if (drel <= 2) galois = 1;
     725             : 
     726          56 :   relpol = RgX_nffix("rnfisnorminit", T, relpol, 1);
     727          56 :   if (nf_get_degree(nf) == 1) /* over Q */
     728          35 :     rnfeq = mkvec5(relpol,gen_0,gen_0,T,relpol);
     729          21 :   else if (galois == 2) /* needs eltup+abstorel */
     730           7 :     rnfeq = nf_rnfeq(nf, relpol);
     731             :   else /* needs abstorel */
     732          14 :     rnfeq = nf_rnfeqsimple(nf, relpol);
     733          56 :   polabs = gel(rnfeq,1);
     734          56 :   k = gel(rnfeq,3);
     735          56 :   if (!bnfabs || !gequal0(k))
     736          28 :     bnfabs = Buchall(polabs, nf_FORCE, nf_get_prec(nf));
     737          56 :   if (!nfabs) nfabs = bnf_get_nf(bnfabs);
     738             : 
     739          56 :   if (galois == 2)
     740             :   {
     741          21 :     GEN P = polabs==relpol? leafcopy(relpol): nfX_eltup(nf, rnfeq, relpol);
     742          21 :     setvarn(P, fetch_var_higher());
     743          21 :     galois = !!nfroots_if_split(&nfabs, P);
     744          21 :     (void)delete_var();
     745             :   }
     746             : 
     747          56 :   cyc = bnf_get_cyc(bnfabs);
     748          56 :   gen = bnf_get_gen(bnfabs); l = lg(cyc);
     749          84 :   for(i=1; i<l; i++)
     750             :   {
     751          35 :     GEN g = gel(gen,i);
     752          35 :     if (ugcd(umodiu(gel(cyc,i), drel), drel) == 1) break;
     753          28 :     Zfa_append(gcoeff(g,1,1), H, NULL);
     754             :   }
     755          56 :   if (!galois)
     756             :   {
     757          21 :     GEN Ndiscrel = diviiexact(nf_get_disc(nfabs), powiu(nf_get_disc(nf), drel));
     758          21 :     Zfa_append(Ndiscrel, H, NULL);
     759             :   }
     760          56 :   S = hash_keys(H); settyp(S,t_VEC);
     761          56 :   gel(y,1) = bnf;
     762          56 :   gel(y,2) = bnfabs;
     763          56 :   gel(y,3) = relpol;
     764          56 :   gel(y,4) = rnfeq;
     765          56 :   gel(y,5) = S;
     766          56 :   gel(y,6) = nf_pV_to_prV(nf, S);
     767          56 :   gel(y,7) = nf_pV_to_prV(nfabs, S);
     768          56 :   gel(y,8) = stoi(galois); return gerepilecopy(av, y);
     769             : }
     770             : 
     771             : /* T as output by rnfisnorminit
     772             :  * if flag=0 assume extension is Galois (==> answer is unconditional)
     773             :  * if flag>0 add to S all primes dividing p <= flag
     774             :  * if flag<0 add to S all primes dividing abs(flag)
     775             : 
     776             :  * answer is a vector v = [a,b] such that
     777             :  * x = N(a)*b and x is a norm iff b = 1  [assuming S large enough] */
     778             : GEN
     779          70 : rnfisnorm(GEN T, GEN x, long flag)
     780             : {
     781          70 :   pari_sp av = avma;
     782             :   GEN bnf, rel, relpol, rnfeq, nfpol;
     783             :   GEN nf, aux, H, U, Y, M, A, bnfS, sunitrel, futu, S, S1, S2, Sx;
     784             :   long L, i, drel, itu;
     785             :   hashtable *H0, *H2;
     786          70 :   if (typ(T) != t_VEC || lg(T) != 9)
     787           0 :     pari_err_TYPE("rnfisnorm [please apply rnfisnorminit()]", T);
     788          70 :   bnf = gel(T,1);
     789          70 :   rel = gel(T,2);
     790          70 :   bnf = checkbnf(bnf);
     791          70 :   rel = checkbnf(rel);
     792          70 :   nf = bnf_get_nf(bnf);
     793          70 :   x = nf_to_scalar_or_alg(nf,x);
     794          70 :   if (gequal0(x)) { avma = av; return mkvec2(gen_0, gen_1); }
     795          70 :   if (gequal1(x)) { avma = av; return mkvec2(gen_1, gen_1); }
     796          70 :   relpol = gel(T,3);
     797          70 :   rnfeq = gel(T,4);
     798          70 :   drel = degpol(relpol);
     799          70 :   if (gequalm1(x) && odd(drel)) { avma = av; return mkvec2(gen_m1, gen_1); }
     800             : 
     801             :   /* build set T of ideals involved in the solutions */
     802          70 :   nfpol = nf_get_pol(nf);
     803          70 :   S = gel(T,5);
     804          70 :   H0 = hash_create_INT(100UL);
     805          70 :   H2 = hash_create_INT(100UL);
     806          70 :   L = lg(S);
     807          70 :   for (i = 1; i < L; i++) p_append(gel(S,i),H0,NULL);
     808          70 :   S1 = gel(T,6);
     809          70 :   S2 = gel(T,7);
     810          70 :   if (flag && !gequal0(gel(T,8)))
     811           7 :     pari_warn(warner,"useless flag in rnfisnorm: the extension is Galois");
     812          70 :   if (flag > 0)
     813             :   {
     814             :     forprime_t T;
     815             :     ulong p;
     816          14 :     u_forprime_init(&T, 2, flag);
     817          14 :     while ((p = u_forprime_next(&T))) p_append(utoipos(p), H0,H2);
     818             :   }
     819          56 :   else if (flag < 0)
     820           7 :     Zfa_append(utoipos(-flag),H0,H2);
     821             :   /* overkill: prime ideals dividing x would be enough */
     822          70 :   A = idealnumden(nf, x);
     823          70 :   fa_append(gel(A,1), H0,H2);
     824          70 :   fa_append(gel(A,2), H0,H2);
     825          70 :   Sx = hash_keys(H2); L = lg(Sx);
     826          70 :   if (L > 1)
     827             :   { /* new primes */
     828          49 :     settyp(Sx, t_VEC);
     829          49 :     S1 = shallowconcat(S1, nf_pV_to_prV(nf, Sx));
     830          49 :     S2 = shallowconcat(S2, nf_pV_to_prV(rel, Sx));
     831             :   }
     832             : 
     833             :   /* computation on T-units */
     834          70 :   futu = shallowconcat(bnf_get_fu(rel), bnf_get_tuU(rel));
     835          70 :   bnfS = bnfsunit(bnf,S1,LOWDEFAULTPREC);
     836          70 :   sunitrel = shallowconcat(futu, gel(bnfsunit(rel,S2,LOWDEFAULTPREC), 1));
     837             : 
     838          70 :   A = lift_shallow(bnfissunit(bnf,bnfS,x));
     839          70 :   L = lg(sunitrel);
     840          70 :   itu = lg(nf_get_roots(nf))-1; /* index of torsion unit in bnfsunit(nf) output */
     841          70 :   M = cgetg(L+1,t_MAT);
     842        1449 :   for (i=1; i<L; i++)
     843             :   {
     844        1379 :     GEN u = eltabstorel(rnfeq, gel(sunitrel,i));
     845        1379 :     gel(sunitrel,i) = u;
     846        1379 :     u = bnfissunit(bnf,bnfS, gnorm(u));
     847        1379 :     if (lg(u) == 1) pari_err_BUG("rnfisnorm");
     848        1379 :     gel(u,itu) = lift_shallow(gel(u,itu)); /* lift root of 1 part */
     849        1379 :     gel(M,i) = u;
     850             :   }
     851          70 :   aux = zerocol(lg(A)-1); gel(aux,itu) = utoipos( bnf_get_tuN(rel) );
     852          70 :   gel(M,L) = aux;
     853          70 :   H = ZM_hnfall(M, &U, 2);
     854          70 :   Y = RgM_RgC_mul(U, inverseimage(H,A));
     855             :   /* Y: sols of MY = A over Q */
     856          70 :   setlg(Y, L);
     857          70 :   aux = factorback2(sunitrel, gfloor(Y));
     858          70 :   x = mkpolmod(x,nfpol);
     859          70 :   if (!gequal1(aux)) x = gdiv(x, gnorm(aux));
     860          70 :   x = lift_if_rational(x);
     861          70 :   if (typ(aux) == t_POLMOD && degpol(nfpol) == 1)
     862          28 :     gel(aux,2) = lift_if_rational(gel(aux,2));
     863          70 :   return gerepilecopy(av, mkvec2(aux, x));
     864             : }
     865             : 
     866             : GEN
     867          28 : bnfisnorm(GEN bnf, GEN x, long flag)
     868             : {
     869          28 :   pari_sp av = avma;
     870          28 :   GEN T = rnfisnorminit(pol_x(fetch_var()), bnf, flag == 0? 1: 2);
     871          28 :   GEN r = rnfisnorm(T, x, flag == 1? 0: flag);
     872          28 :   (void)delete_var();
     873          28 :   return gerepileupto(av,r);
     874             : }

Generated by: LCOV version 1.11