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

Generated by: LCOV version 1.11