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

Generated by: LCOV version 1.11