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 - buch3.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.8.0 lcov report (development 19350-bd5f220) Lines: 1391 1489 93.4 %
Date: 2016-08-24 06:11:24 Functions: 95 104 91.3 %
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             : /*                       RAY CLASS FIELDS                          */
      17             : /*                                                                 */
      18             : /*******************************************************************/
      19             : #include "pari.h"
      20             : #include "paripriv.h"
      21             : 
      22             : /* Faster than Buchray (because it can use nfsign_units: easier nfarchstar) */
      23             : GEN
      24          14 : buchnarrow(GEN bnf)
      25             : {
      26             :   GEN nf, cyc, gen, A, NO, GD, v, invpi, logs, R, basecl, met, u1, archp;
      27             :   long r1, j, ngen, t, RU;
      28          14 :   pari_sp av = avma;
      29             : 
      30          14 :   bnf = checkbnf(bnf);
      31          14 :   nf = bnf_get_nf(bnf); r1 = nf_get_r1(nf);
      32             : 
      33          14 :   if (!r1) return gcopy( bnf_get_clgp(bnf) );
      34             : 
      35             :   /* simplified version of nfsign_units; r1 > 0 so bnf.tu = -1 */
      36          14 :   archp = identity_perm(r1);
      37          14 :   A = bnf_get_logfu(bnf); RU = lg(A)+1;
      38          14 :   invpi = invr( mppi(nf_get_prec(nf)) );
      39          14 :   v = cgetg(RU,t_MAT); gel(v, 1) = const_vecsmall(r1, 1); /* nfsign(-1) */
      40          14 :   for (j=2; j<RU; j++) gel(v,j) = nfsign_from_logarch(gel(A,j-1), invpi, archp);
      41             :   /* up to here */
      42             : 
      43          14 :   cyc = bnf_get_cyc(bnf);
      44          14 :   gen = bnf_get_gen(bnf);
      45          14 :   v = Flm_image(v, 2);
      46          14 :   t = lg(v)-1;
      47          14 :   if (t == r1) { avma = av; return gcopy( bnf_get_clgp(bnf) ); }
      48           7 :   NO = shifti(bnf_get_no(bnf), r1-t);
      49             : 
      50           7 :   ngen = lg(gen)-1;
      51           7 :   gen = vec_lengthen(gen, r1 + (ngen-t));
      52           7 :   v = archstar_full_rk(NULL, nf_get_M(nf), v, gen + (ngen-t));
      53           7 :   v = rowslice(v, t+1, r1);
      54             : 
      55           7 :   logs = cgetg(ngen+1,t_MAT); GD = gmael(bnf,9,3);
      56          14 :   for (j=1; j<=ngen; j++)
      57             :   {
      58           7 :     GEN z = nfsign_from_logarch(gel(GD,j), invpi, archp);
      59           7 :     gel(logs,j) = zc_to_ZC( Flm_Flc_mul(v, z, 2) );
      60             :   }
      61             :   /* [ cyc  0 ]
      62             :    * [ logs 2 ] = relation matrix for Cl_f */
      63           7 :   R = shallowconcat(
      64             :     vconcat(diagonal_shallow(cyc), logs),
      65             :     vconcat(zeromat(ngen, r1-t), scalarmat(gen_2,r1-t))
      66             :   );
      67           7 :   met = ZM_snf_group(R,NULL,&u1);
      68           7 :   t = lg(met); basecl = cgetg(t,t_VEC);
      69          21 :   for (j=1; j<t; j++)
      70          14 :     gel(basecl,j) = Q_primpart( idealfactorback(nf,gen,gel(u1,j),0) );
      71           7 :   return gerepilecopy(av, mkvec3(NO, met, basecl));
      72             : }
      73             : 
      74             : /********************************************************************/
      75             : /**                                                                **/
      76             : /**                  REDUCTION MOD IDELE                           **/
      77             : /**                                                                **/
      78             : /********************************************************************/
      79             : 
      80             : static GEN
      81        5173 : compute_fact(GEN nf, GEN u1, GEN gen)
      82             : {
      83             :   GEN G, basecl;
      84        5173 :   long i, j, l = lg(u1), h = lgcols(u1); /* l > 1 */
      85             : 
      86        5173 :   basecl = cgetg(l,t_VEC);
      87        5173 :   G = cgetg(3,t_VEC);
      88        5173 :   gel(G,2) = cgetg(1,t_MAT);
      89             : 
      90       12131 :   for (j=1; j<l; j++)
      91             :   {
      92        6958 :     GEN g,e, z = NULL;
      93       31808 :     for (i=1; i<h; i++)
      94             :     {
      95       24850 :       e = gcoeff(u1,i,j); if (!signe(e)) continue;
      96             : 
      97        7791 :       g = gel(gen,i);
      98        7791 :       if (typ(g) != t_MAT)
      99             :       {
     100        3346 :         if (z)
     101         693 :           gel(z,2) = famat_mul(gel(z,2), to_famat_shallow(g, e));
     102             :         else
     103        2653 :           z = mkvec2(NULL, to_famat_shallow(g, e));
     104        3346 :         continue;
     105             :       }
     106             : 
     107        4445 :       gel(G,1) = g;
     108        4445 :       g = idealpowred(nf,G,e);
     109        4445 :       z = z? idealmulred(nf,z,g): g;
     110             :     }
     111        6958 :     gel(z,2) = famat_reduce(gel(z,2));
     112        6958 :     gel(basecl,j) = z;
     113             :   }
     114        5173 :   return basecl;
     115             : }
     116             : 
     117             : static int
     118        6440 : too_big(GEN nf, GEN bet)
     119             : {
     120        6440 :   GEN x = nfnorm(nf,bet);
     121        6440 :   switch (typ(x))
     122             :   {
     123         826 :     case t_INT: return abscmpii(x, gen_1);
     124        5614 :     case t_FRAC: return abscmpii(gel(x,1), gel(x,2));
     125             :   }
     126           0 :   pari_err_BUG("wrong type in too_big");
     127           0 :   return 0; /* not reached */
     128             : }
     129             : 
     130             : /* GTM 193: Algo 4.3.4. Reduce x mod divisor */
     131             : static GEN
     132        5803 : idealmoddivisor_aux(GEN nf, GEN x, GEN divisor, GEN sarch)
     133             : {
     134        5803 :   pari_sp av = avma;
     135        5803 :   GEN a,A,D,G, f = gel(divisor,1);
     136             : 
     137        5803 :   if ( is_pm1(gcoeff(f,1,1)) ) /* f = 1 */
     138             :   {
     139          42 :     G = idealred_elt(nf, x);
     140          42 :     D = idealred_elt(nf, idealdiv(nf,G,x));
     141             :   }
     142             :   else
     143             :   {/* given coprime integral ideals x and f (f HNF), compute "small"
     144             :     * G in x, such that G = 1 mod (f). GTM 193: Algo 4.3.3 */
     145        5761 :     G = idealaddtoone_i(nf, x, f);
     146        5761 :     D = idealaddtoone_i(nf, idealdiv(nf,G,x), f);
     147             :   }
     148        5803 :   A = nfdiv(nf,D,G);
     149        5803 :   if (too_big(nf,A) > 0) { avma = av; return x; }
     150        5460 :   a = set_sign_mod_divisor(nf, NULL, A, divisor, sarch);
     151        5460 :   if (a != A && too_big(nf,A) > 0) { avma = av; return x; }
     152        5460 :   return idealmul(nf, a, x);
     153             : }
     154             : 
     155             : GEN
     156        4214 : idealmoddivisor(GEN bnr, GEN x)
     157             : {
     158        4214 :   GEN bid = bnr_get_bid(bnr), fa2 = gel(bid,4);
     159        4214 :   GEN sarch = gel(fa2,lg(fa2)-1);
     160        4214 :   return idealmoddivisor_aux(checknf(bnr), x, bid_get_mod(bid), sarch);
     161             : }
     162             : 
     163             : /* v_pr(L0 * cx) */
     164             : static long
     165        6188 : fast_val(GEN nf,GEN L0,GEN cx,GEN pr)
     166             : {
     167        6188 :   pari_sp av = avma;
     168        6188 :   long v = typ(L0) == t_INT? 0: ZC_nfval(nf,L0,pr);
     169        6188 :   if (cx)
     170             :   {
     171        4977 :     long w = Q_pval(cx, pr_get_p(pr));
     172        4977 :     if (w) v += w * pr_get_e(pr);
     173             :   }
     174        6188 :   avma = av; return v;
     175             : }
     176             : 
     177             : /* x coprime to fZ, return y = x mod fZ, y integral */
     178             : static GEN
     179         763 : make_integral_Z(GEN x, GEN fZ)
     180             : {
     181         763 :   GEN d, y = Q_remove_denom(x, &d);
     182         763 :   if (d) y = FpC_Fp_mul(y, Fp_inv(d, fZ), fZ);
     183         763 :   return y;
     184             : }
     185             : 
     186             : /* p pi^(-1) mod f */
     187             : static GEN
     188         931 : get_pinvpi(GEN nf, GEN fZ, GEN p, GEN pi, GEN *v)
     189             : {
     190         931 :   if (!*v) {
     191         763 :     GEN invpi = nfinv(nf, pi);
     192         763 :     *v = make_integral_Z(RgC_Rg_mul(invpi, p), mulii(p, fZ));
     193             :   }
     194         931 :   return *v;
     195             : }
     196             : /* p pi^(-1) mod f */
     197             : static GEN
     198        1841 : get_pi(GEN F, GEN pr, GEN *v)
     199             : {
     200        1841 :   if (!*v) *v = unif_mod_fZ(pr, F);
     201        1841 :   return *v;
     202             : }
     203             : 
     204             : static GEN
     205        5201 : compute_raygen(GEN nf, GEN u1, GEN gen, GEN bid)
     206             : {
     207             :   GEN f, fZ, basecl, module, fa, fa2, pr, t, EX, sarch, cyc, F;
     208             :   GEN listpr, vecpi, vecpinvpi;
     209             :   long i,j,l,lp;
     210             : 
     211        5201 :   if (lg(u1) == 1) return cgetg(1, t_VEC);
     212             : 
     213             :   /* basecl = generators in factored form */
     214        5173 :   basecl = compute_fact(nf,u1,gen);
     215             : 
     216        5173 :   module = bid_get_mod(bid);
     217        5173 :   cyc = bid_get_cyc(bid); EX = gel(cyc,1); /* exponent of (O/f)^* */
     218        5173 :   f   = gel(module,1); fZ = gcoeff(f,1,1);
     219        5173 :   fa  = gel(bid,3);
     220        5173 :   fa2 = gel(bid,4); sarch = gel(fa2, lg(fa2)-1);
     221        5173 :   listpr = gel(fa,1); F = init_unif_mod_fZ(listpr);
     222             : 
     223        5173 :   lp = lg(listpr);
     224        5173 :   vecpinvpi = cgetg(lp, t_VEC);
     225        5173 :   vecpi  = cgetg(lp, t_VEC);
     226       10773 :   for (i=1; i<lp; i++)
     227             :   {
     228        5600 :     pr = gel(listpr,i);
     229        5600 :     gel(vecpi,i)    = NULL; /* to be computed if needed */
     230        5600 :     gel(vecpinvpi,i) = NULL; /* to be computed if needed */
     231             :   }
     232             : 
     233        5173 :   l = lg(basecl);
     234       12131 :   for (i=1; i<l; i++)
     235             :   {
     236             :     GEN p, pi, pinvpi, dmulI, mulI, G, I, A, e, L, newL;
     237             :     long la, v, k;
     238             :     pari_sp av;
     239             :     /* G = [I, A=famat(L,e)] is a generator, I integral */
     240        6958 :     G = gel(basecl,i);
     241        6958 :     I = gel(G,1);
     242        6958 :     A = gel(G,2);
     243        6958 :       L = gel(A,1);
     244        6958 :       e = gel(A,2);
     245             :     /* if no reduction took place in compute_fact, everybody is still coprime
     246             :      * to f + no denominators */
     247        6958 :     if (!I)
     248             :     {
     249        2653 :       gel(basecl,i) = famat_to_nf_moddivisor(nf, L, e, bid);
     250        2653 :       continue;
     251             :     }
     252        4305 :     if (lg(A) == 1)
     253             :     {
     254        2716 :       gel(basecl,i) = I;
     255        2716 :       continue;
     256             :     }
     257             : 
     258             :     /* compute mulI so that mulI * I coprime to f
     259             :      * FIXME: use idealcoprime ??? (Less efficient. Fix idealcoprime!) */
     260        1589 :     dmulI = mulI = NULL;
     261        3738 :     for (j=1; j<lp; j++)
     262             :     {
     263        2149 :       pr = gel(listpr,j);
     264        2149 :       v  = idealval(nf, I, pr);
     265        2149 :       if (!v) continue;
     266         749 :       p  = pr_get_p(pr);
     267         749 :       pi = get_pi(F, pr, &gel(vecpi,j));
     268         749 :       pinvpi = get_pinvpi(nf, fZ, p, pi, &gel(vecpinvpi,j));
     269         749 :       t = nfpow_u(nf, pinvpi, (ulong)v);
     270         749 :       mulI = mulI? nfmuli(nf, mulI, t): t;
     271         749 :       t = powiu(p, v);
     272         749 :       dmulI = dmulI? mulii(dmulI, t): t;
     273             :     }
     274             : 
     275             :     /* make all components of L coprime to f.
     276             :      * Assuming (L^e * I, f) = 1, then newL^e * mulI = L^e */
     277        1589 :     la = lg(e); newL = cgetg(la, t_VEC);
     278        4970 :     for (k=1; k<la; k++)
     279             :     {
     280        3381 :       GEN cx, LL = nf_to_scalar_or_basis(nf, gel(L,k));
     281        3381 :       GEN L0 = Q_primitive_part(LL, &cx); /* LL = L0*cx (faster nfval) */
     282        9569 :       for (j=1; j<lp; j++)
     283             :       {
     284        6188 :         pr = gel(listpr,j);
     285        6188 :         v  = fast_val(nf, L0,cx, pr); /* = val_pr(LL) */
     286        6188 :         if (!v) continue;
     287        1092 :         p  = pr_get_p(pr);
     288        1092 :         pi = get_pi(F, pr, &gel(vecpi,j));
     289        1092 :         if (v > 0)
     290             :         {
     291         182 :           pinvpi = get_pinvpi(nf, fZ, p, pi, &gel(vecpinvpi,j));
     292         182 :           t = nfpow_u(nf,pinvpi, (ulong)v);
     293         182 :           LL = nfmul(nf, LL, t);
     294         182 :           LL = RgC_Rg_div(LL, powiu(p, v));
     295             :         }
     296             :         else
     297             :         {
     298         910 :           t = nfpow_u(nf,pi,(ulong)(-v));
     299         910 :           LL = nfmul(nf, LL, t);
     300             :         }
     301             :       }
     302        3381 :       LL = make_integral(nf,LL,f,listpr);
     303        3381 :       gel(newL,k) = typ(LL) == t_INT? LL: FpC_red(LL, fZ);
     304             :     }
     305             : 
     306        1589 :     av = avma;
     307             :     /* G in nf, = L^e mod f */
     308        1589 :     G = famat_to_nf_modideal_coprime(nf, newL, e, f, EX);
     309        1589 :     if (mulI)
     310             :     {
     311         735 :       G = nfmuli(nf, G, mulI);
     312         735 :       G = ZC_hnfrem(G, ZM_Z_mul(f, dmulI));
     313             :     }
     314        1589 :     G = set_sign_mod_divisor(nf,A,G,module,sarch);
     315        1589 :     I = idealmul(nf,I,G);
     316        1589 :     if (dmulI) I = ZM_Z_divexact(I, dmulI);
     317             :     /* more or less useless, but cheap at this point */
     318        1589 :     I = idealmoddivisor_aux(nf,I,module,sarch);
     319        1589 :     gel(basecl,i) = gerepilecopy(av, I);
     320             :   }
     321        5173 :   return basecl;
     322             : }
     323             : 
     324             : /********************************************************************/
     325             : /**                                                                **/
     326             : /**                   INIT RAY CLASS GROUP                         **/
     327             : /**                                                                **/
     328             : /********************************************************************/
     329             : static GEN
     330        7847 : check_subgroup(GEN bnr, GEN H, GEN *clhray)
     331             : {
     332        7847 :   GEN cyc = bnr_get_cyc(bnr);
     333        7847 :   *clhray = bnr_get_no(bnr);
     334        7847 :   if (H && isintzero(H)) H = NULL;
     335        7847 :   if (H) switch(typ(H))
     336             :   {
     337             :     case t_MAT:
     338        3416 :       RgM_check_ZM(H, "check_subgroup");
     339        3416 :       H = ZM_hnfmodid(H, cyc);
     340        3416 :       break;
     341             :     case t_VEC:
     342         868 :       if (char_check(cyc, H)) { H = charker(cyc, H); break; }
     343           0 :     default: pari_err_TYPE("check_subgroup", H);
     344             :   }
     345        7847 :   if (H)
     346             :   {
     347        4284 :     GEN h = ZM_det_triangular(H);
     348        4284 :     if (equalii(h, *clhray)) H = NULL; else *clhray = h;
     349             :   }
     350        7847 :   return H;
     351             : }
     352             : 
     353             : static GEN
     354        5467 : get_dataunit(GEN bnf, GEN bid)
     355             : {
     356        5467 :   GEN D, cyc = bid_get_cyc(bid), U = init_units(bnf), nf = bnf_get_nf(bnf);
     357             :   long i, l;
     358        5467 :   zlog_S S; init_zlog_bid(&S, bid);
     359        5467 :   D = nfsign_units(bnf, S.archp, 1); l = lg(D);
     360       16975 :   for (i = 1; i < l; i++)
     361             :   {
     362       11508 :     GEN v = zlog(nf, gel(U,i),gel(D,i), &S);
     363       11508 :     gel(D,i) = vecmodii(ZM_ZC_mul(S.U, v), cyc);
     364             :   }
     365        5467 :   return D;
     366             : }
     367             : 
     368             : GEN
     369        6125 : Buchray(GEN bnf, GEN module, long flag)
     370             : {
     371             :   GEN nf, cyc, gen, Gen, u, clg, logs, p1, h, met, u1, u2, U, cycgen;
     372             :   GEN bid, cycbid, genbid, y, funits, H, Hi, c1, c2, El;
     373             :   long RU, Ri, j, ngen, lh;
     374        6125 :   const long add_gen = flag & nf_GEN;
     375        6125 :   const long do_init = flag & nf_INIT;
     376        6125 :   pari_sp av = avma;
     377             : 
     378        6125 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
     379        6125 :   funits = bnf_get_fu(bnf); RU = lg(funits);
     380        6125 :   El = Gen = NULL; /* gcc -Wall */
     381        6125 :   cyc = bnf_get_cyc(bnf);
     382        6125 :   gen = bnf_get_gen(bnf); ngen = lg(cyc)-1;
     383             : 
     384        6125 :   bid = checkbid_i(module);
     385        6125 :   if (!bid) bid = Idealstar(nf,module,nf_GEN|nf_INIT);
     386        6125 :   cycbid = bid_get_cyc(bid);
     387        6125 :   genbid = bid_get_gen(bid);
     388        6125 :   Ri = lg(cycbid)-1; lh = ngen+Ri;
     389        6125 :   if (Ri || add_gen || do_init)
     390             :   {
     391        6125 :     GEN fx = gel(bid,3);
     392        6125 :     El = cgetg(ngen+1,t_VEC);
     393       10773 :     for (j=1; j<=ngen; j++)
     394             :     {
     395        4648 :       p1 = idealcoprimefact(nf, gel(gen,j), fx);
     396        4648 :       if (RgV_isscalar(p1)) p1 = gel(p1,1);
     397        4648 :       gel(El,j) = p1;
     398             :     }
     399             :   }
     400        6125 :   if (add_gen)
     401             :   {
     402        5915 :     Gen = cgetg(lh+1,t_VEC);
     403        5915 :     for (j=1; j<=ngen; j++) gel(Gen,j) = idealmul(nf, gel(El,j), gel(gen,j));
     404        5915 :     for (   ; j<=lh; j++)   gel(Gen,j) = gel(genbid, j-ngen);
     405             :   }
     406        6125 :   if (!Ri)
     407             :   {
     408         721 :     clg = cgetg(add_gen? 4: 3,t_VEC);
     409         721 :     if (add_gen) gel(clg,3) = Gen;
     410         721 :     gel(clg,1) = bnf_get_no(bnf);
     411         721 :     gel(clg,2) = cyc;
     412         721 :     if (!do_init) return gerepilecopy(av,clg);
     413         721 :     y = cgetg(7,t_VEC);
     414         721 :     gel(y,1) = bnf;
     415         721 :     gel(y,2) = bid;
     416         721 :     gel(y,3) = El;
     417         721 :     gel(y,4) = matid(ngen);
     418         721 :     gel(y,5) = clg;
     419         721 :     gel(y,6) = mkvec3(cgetg(1,t_MAT), matid(RU), gen_1);
     420         721 :     return gerepilecopy(av,y);
     421             :   }
     422             : 
     423        5404 :   cycgen = check_and_build_cycgen(bnf);
     424             :   /* (log(Units)|D) * u = (0 | H) */
     425        5404 :   if (do_init)
     426             :   {
     427        5404 :     GEN D = shallowconcat(get_dataunit(bnf, bid), diagonal_shallow(cycbid));
     428        5404 :     H = ZM_hnfall(D, do_init? &u: NULL, 1);
     429             :   }
     430             :   else
     431           0 :     H = ZM_hnfmodid(get_dataunit(bnf, bid), cycbid);
     432        5404 :   logs = cgetg(ngen+1, t_MAT);
     433             :   /* FIXME: cycgen[j] is not necessarily coprime to bid, but it is made coprime
     434             :    * in famat_zlog using canonical uniformizers [from bid data]: no need to
     435             :    * correct it here. The same ones will be used in bnrisprincipal. Hence
     436             :    * modification by El is useless. */
     437        9653 :   for (j=1; j<=ngen; j++)
     438             :   {
     439        4249 :     p1 = gel(cycgen,j);
     440        4249 :     if (typ(gel(El,j)) != t_INT) /* <==> != 1 */
     441             :     {
     442        1001 :       GEN F = to_famat_shallow(gel(El,j), gel(cyc,j));
     443        1001 :       p1 = famat_mul(F, p1);
     444             :     }
     445        4249 :     gel(logs,j) = ideallog(nf, p1, bid); /* = log(Gen[j]) */
     446             :   }
     447             :   /* [ cyc  0 ]
     448             :    * [-logs H ] = relation matrix for Cl_f */
     449        5404 :   h = shallowconcat(
     450             :     vconcat(diagonal_shallow(cyc), gneg_i(logs)),
     451             :     vconcat(zeromat(ngen, Ri), H)
     452             :   );
     453        5404 :   met = ZM_snf_group(ZM_hnf(h), &U, add_gen? &u1: NULL);
     454        5404 :   clg = cgetg(add_gen? 4: 3, t_VEC);
     455        5404 :   gel(clg,1) = detcyc(met, &j);
     456        5404 :   gel(clg,2) = met;
     457        5404 :   if (add_gen) gel(clg,3) = compute_raygen(nf,u1,Gen,bid);
     458        5404 :   if (!do_init) return gerepilecopy(av, clg);
     459             : 
     460        5404 :   u2 = cgetg(Ri+1,t_MAT);
     461        5404 :   u1 = cgetg(RU+1,t_MAT);
     462        5404 :   for (j=1; j<=RU; j++) { gel(u1,j) = gel(u,j); setlg(u[j],RU+1); }
     463        5404 :   u += RU;
     464        5404 :   for (j=1; j<=Ri; j++) { gel(u2,j) = gel(u,j); setlg(u[j],RU+1); }
     465             : 
     466             :   /* log(Units) U2 = H (mod D)
     467             :    * log(Units) U1 = 0 (mod D) */
     468        5404 :   u1 = ZM_lll(u1, 0.99, LLL_INPLACE);
     469        5404 :   Hi = Q_primitive_part(RgM_inv_upper(H), &c1);
     470        5404 :   u2 = Q_primitive_part(ZM_mul(ZM_reducemodmatrix(u2,u1), Hi), &c2);
     471        5404 :   c1 = mul_content(c1, c2);
     472        5404 :   if (!c1)
     473        2436 :     c2 = gen_1;
     474        2968 :   else if (typ(c1) == t_INT)
     475             :   {
     476        1778 :     if (!is_pm1(c1)) u2 = ZM_Z_mul(u2, c1);
     477        1778 :     c2 = gen_1;
     478             :   }
     479             :   else /* t_FRAC */
     480             :   {
     481        1190 :     c2 = gel(c1,2);
     482        1190 :     c1 = gel(c1,1);
     483        1190 :     if (!is_pm1(c1)) u2 = ZM_Z_mul(u2, c1);
     484             :   }
     485        5404 :   y = cgetg(7,t_VEC);
     486        5404 :   gel(y,1) = bnf;
     487        5404 :   gel(y,2) = bid;
     488        5404 :   gel(y,3) = El;
     489        5404 :   gel(y,4) = U;
     490        5404 :   gel(y,5) = clg;
     491        5404 :   gel(y,6) = mkvec3(u2,u1,c2); /* u2/c2 = H^(-1) (mod Im u1) */
     492        5404 :   return gerepilecopy(av,y);
     493             : }
     494             : 
     495             : GEN
     496         623 : bnrinit0(GEN bnf, GEN ideal, long flag)
     497             : {
     498         623 :   switch(flag)
     499             :   {
     500          49 :     case 0: flag = nf_INIT; break;
     501         574 :     case 1: flag = nf_INIT | nf_GEN; break;
     502           0 :     default: pari_err_FLAG("bnrinit");
     503             :   }
     504         623 :   return Buchray(bnf,ideal,flag);
     505             : }
     506             : 
     507             : GEN
     508          77 : bnrclassno(GEN bnf,GEN ideal)
     509             : {
     510             :   GEN nf, h, D, bid, cycbid;
     511          77 :   pari_sp av = avma;
     512             : 
     513          77 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
     514          77 :   h = bnf_get_no(bnf); /* class number */
     515          77 :   bid = checkbid_i(ideal);
     516          77 :   if (!bid) bid = Idealstar(nf,ideal,nf_INIT);
     517          77 :   cycbid = bid_get_cyc(bid);
     518          77 :   if (lg(cycbid) == 1) { avma = av; return icopy(h); }
     519          63 :   D = get_dataunit(bnf, bid); /* (Z_K/f)^* / units ~ Z^n / D */
     520          63 :   D = ZM_hnfmodid(D,cycbid);
     521          63 :   return gerepileuptoint(av, mulii(h, ZM_det_triangular(D)));
     522             : }
     523             : GEN
     524          70 : bnrclassno0(GEN A, GEN B, GEN C)
     525             : {
     526          70 :   pari_sp av = avma;
     527          70 :   GEN h, H = NULL;
     528             :   /* adapted from ABC_to_bnr, avoid costly bnrinit if possible */
     529          70 :   if (typ(A) == t_VEC)
     530          70 :     switch(lg(A))
     531             :     {
     532             :       case 7: /* bnr */
     533          14 :         checkbnr(A); H = B;
     534          14 :         break;
     535             :       case 11: /* bnf */
     536          56 :         if (!B) pari_err_TYPE("bnrclassno [bnf+missing conductor]",A);
     537          56 :         if (!C) return bnrclassno(A, B);
     538           7 :         A = Buchray(A, B, nf_INIT); H = C;
     539           7 :         break;
     540           0 :       default: checkbnf(A);/*error*/
     541             :     }
     542           0 :   else checkbnf(A);/*error*/
     543             : 
     544          21 :   H = check_subgroup(A, H, &h);
     545          21 :   if (!H) { avma = av; return icopy(h); }
     546          14 :   return gerepileuptoint(av, h);
     547             : }
     548             : 
     549             : GEN
     550      184777 : bnrisprincipal(GEN bnr, GEN x, long flag)
     551             : {
     552      184777 :   pari_sp av = avma;
     553             :   GEN bnf, nf, bid, U, El, ep, L, idep, ex, cycray, cycbid, alpha;
     554             : 
     555      184777 :   checkbnr(bnr);
     556      184777 :   cycray = bnr_get_cyc(bnr);
     557      184777 :   if (lg(cycray) == 1 && !(flag & nf_GEN)) return cgetg(1,t_COL);
     558             : 
     559      184455 :   bnf = bnr_get_bnf(bnr); nf = bnf_get_nf(bnf);
     560      184455 :   bid = bnr_get_bid(bnr);
     561      184455 :   cycbid = bid_get_cyc(bid);
     562      184455 :   El  = gel(bnr,3);
     563      184455 :   U   = gel(bnr,4);
     564             : 
     565      184455 :   if (typ(x) == t_VEC && lg(x) == 3)
     566           0 :   { idep = gel(x,2); x = gel(x,1); }  /* precomputed */
     567             :   else
     568      184455 :     idep = bnfisprincipal0(bnf, x, nf_FORCE|nf_GENMAT);
     569      184455 :   ep  = gel(idep,1);
     570      184455 :   if (lg(cycbid) > 1)
     571             :   {
     572      182138 :     GEN beta = gel(idep,2);
     573      182138 :     long i, j = lg(ep);
     574      313889 :     for (i=1; i<j; i++) /* modify beta as if gen -> El.gen (coprime to bid) */
     575      131751 :       if (typ(gel(El,i)) != t_INT && signe(gel(ep,i))) /* <==> != 1 */
     576       43079 :         beta = famat_mul(to_famat_shallow(gel(El,i), negi(gel(ep,i))), beta);
     577      182138 :     ep = shallowconcat(ep, ideallog(nf,beta,bid));
     578             :   }
     579      184455 :   ex = vecmodii(ZM_ZC_mul(U, ep), cycray);
     580      184455 :   if (!(flag & nf_GEN)) return gerepileupto(av, ex);
     581             : 
     582             :   /* compute generator */
     583         546 :   L = isprincipalfact(bnf, x, bnr_get_gen(bnr), ZC_neg(ex),
     584             :                       nf_GENMAT|nf_GEN_IF_PRINCIPAL|nf_FORCE);
     585         546 :   if (L == gen_0) pari_err_BUG("isprincipalray");
     586         546 :   alpha = nffactorback(nf, L, NULL);
     587         546 :   if (lg(cycbid) > 1)
     588             :   {
     589           7 :     GEN v = gel(bnr,6), u2 = gel(v,1), u1 = gel(v,2), du2 = gel(v,3);
     590           7 :     GEN y = ZM_ZC_mul(u2, ideallog(nf, L, bid));
     591           7 :     if (!is_pm1(du2)) y = ZC_Z_divexact(y,du2);
     592           7 :     y = ZC_reducemodmatrix(y, u1);
     593           7 :     alpha = nfdiv(nf, alpha, nffactorback(nf, init_units(bnf), y));
     594             :   }
     595         546 :   return gerepilecopy(av, mkvec2(ex,alpha));
     596             : }
     597             : 
     598             : GEN
     599      178071 : isprincipalray(GEN bnr, GEN x)
     600             : {
     601      178071 :   return bnrisprincipal(bnr,x,0);
     602             : }
     603             : 
     604             : GEN
     605           0 : isprincipalraygen(GEN bnr, GEN x)
     606             : {
     607           0 :   return bnrisprincipal(bnr,x,nf_GEN);
     608             : }
     609             : 
     610             : /* N! / N^N * (4/pi)^r2 * sqrt(|D|) */
     611             : GEN
     612           0 : minkowski_bound(GEN D, long N, long r2, long prec)
     613             : {
     614           0 :   pari_sp av = avma;
     615           0 :   GEN c = divri(mpfactr(N,prec), powuu(N,N));
     616           0 :   if (r2) c = mulrr(c, powru(divur(4,mppi(prec)), r2));
     617           0 :   c = mulrr(c, gsqrt(absi(D),prec));
     618           0 :   return gerepileuptoleaf(av, c);
     619             : }
     620             : 
     621             : /* DK = |dK| */
     622             : static GEN
     623          49 : zimmertbound(long N,long R2,GEN DK)
     624             : {
     625          49 :   pari_sp av = avma;
     626             :   GEN w;
     627             : 
     628          49 :   if (N < 2) return gen_1;
     629          49 :   if (N < 21)
     630             :   {
     631          49 :     const double c[19][11] = {
     632             : {/*2*/  0.6931,     0.45158},
     633             : {/*3*/  1.71733859, 1.37420604},
     634             : {/*4*/  2.91799837, 2.50091538, 2.11943331},
     635             : {/*5*/  4.22701425, 3.75471588, 3.31196660},
     636             : {/*6*/  5.61209925, 5.09730381, 4.60693851, 4.14303665},
     637             : {/*7*/  7.05406203, 6.50550021, 5.97735406, 5.47145968},
     638             : {/*8*/  8.54052636, 7.96438858, 7.40555445, 6.86558259, 6.34608077},
     639             : {/*9*/ 10.0630022,  9.46382812, 8.87952524, 8.31139202, 7.76081149},
     640             : {/*10*/11.6153797, 10.9966020, 10.3907654,  9.79895170, 9.22232770, 8.66213267},
     641             : {/*11*/13.1930961, 12.5573772, 11.9330458, 11.3210061, 10.7222412, 10.1378082},
     642             : {/*12*/14.7926394, 14.1420915, 13.5016616, 12.8721114, 12.2542699, 11.6490374,
     643             :        11.0573775},
     644             : {/*13*/16.4112395, 15.7475710, 15.0929680, 14.4480777, 13.8136054, 13.1903162,
     645             :        12.5790381},
     646             : {/*14*/18.0466672, 17.3712806, 16.7040780, 16.0456127, 15.3964878, 14.7573587,
     647             :        14.1289364, 13.5119848},
     648             : {/*15*/19.6970961, 19.0111606, 18.3326615, 17.6620757, 16.9999233, 16.3467686,
     649             :        15.7032228, 15.0699480},
     650             : {/*16*/21.3610081, 20.6655103, 19.9768082, 19.2953176, 18.6214885, 17.9558093,
     651             :        17.2988108, 16.6510652, 16.0131906},
     652             : 
     653             : {/*17*/23.0371259, 22.3329066, 21.6349299, 20.9435607, 20.2591899, 19.5822454,
     654             :        18.9131878, 18.2525157, 17.6007672},
     655             : 
     656             : {/*18*/24.7243611, 24.0121449, 23.3056902, 22.6053167, 21.9113705, 21.2242247,
     657             :        20.5442836, 19.8719830, 19.2077941, 18.5522234},
     658             : 
     659             : {/*19*/26.4217792, 25.7021950, 24.9879497, 24.2793271, 23.5766321, 22.8801952,
     660             :        22.1903709, 21.5075437, 20.8321263, 20.1645647},
     661             : {/*20*/28.1285704, 27.4021674, 26.6807314, 25.9645140, 25.2537867, 24.5488420,
     662             :        23.8499943, 23.1575823, 22.4719720, 21.7935548, 21.1227537}
     663             :     };
     664          49 :     w = mulrr(dbltor(exp(-c[N-2][R2])), gsqrt(DK,DEFAULTPREC));
     665             :   }
     666             :   else
     667             :   {
     668           0 :     w = minkowski_bound(DK, N, R2, DEFAULTPREC);
     669             :   }
     670          49 :   return gerepileuptoint(av, ceil_safe(w));
     671             : }
     672             : 
     673             : /* return \gamma_n^n if known, an upper bound otherwise */
     674             : static GEN
     675          49 : hermiteconstant(long n)
     676             : {
     677             :   GEN h,h1;
     678             :   pari_sp av;
     679             : 
     680          49 :   switch(n)
     681             :   {
     682          28 :     case 1: return gen_1;
     683          14 :     case 2: return mkfrac(utoipos(4), utoipos(3));
     684           0 :     case 3: return gen_2;
     685           7 :     case 4: return utoipos(4);
     686           0 :     case 5: return utoipos(8);
     687           0 :     case 6: return mkfrac(utoipos(64), utoipos(3));
     688           0 :     case 7: return utoipos(64);
     689           0 :     case 8: return utoipos(256);
     690             :   }
     691           0 :   av = avma;
     692           0 :   h  = powru(divur(2,mppi(DEFAULTPREC)), n);
     693           0 :   h1 = sqrr(ggamma(gdivgs(utoipos(n+4),2),DEFAULTPREC));
     694           0 :   return gerepileuptoleaf(av, mulrr(h,h1));
     695             : }
     696             : 
     697             : /* 1 if L (= nf != Q) primitive for sure, 0 if MAYBE imprimitive (may have a
     698             :  * subfield K) */
     699             : static long
     700          28 : isprimitive(GEN nf)
     701             : {
     702          28 :   long p, i, l, ep, N = nf_get_degree(nf);
     703             :   GEN D, fa;
     704             : 
     705          28 :   p = ucoeff(factoru(N), 1,1); /* smallest prime | N */
     706          28 :   if (p == N) return 1; /* prime degree */
     707             : 
     708             :   /* N = [L:Q] = product of primes >= p, same is true for [L:K]
     709             :    * d_L = t d_K^[L:K] --> check that some q^p divides d_L */
     710           0 :   D = nf_get_disc(nf);
     711           0 :   fa = gel(absZ_factor_limit(D,0),2); /* list of v_q(d_L). Don't check large primes */
     712           0 :   if (mod2(D)) i = 1;
     713             :   else
     714             :   { /* q = 2 */
     715           0 :     ep = itos(gel(fa,1));
     716           0 :     if ((ep>>1) >= p) return 0; /* 2 | d_K ==> 4 | d_K */
     717           0 :     i = 2;
     718             :   }
     719           0 :   l = lg(fa);
     720           0 :   for ( ; i < l; i++)
     721             :   {
     722           0 :     ep = itos(gel(fa,i));
     723           0 :     if (ep >= p) return 0;
     724             :   }
     725           0 :   return 1;
     726             : }
     727             : 
     728             : static GEN
     729           0 : dft_bound(void)
     730             : {
     731           0 :   if (DEBUGLEVEL>1) err_printf("Default bound for regulator: 0.2\n");
     732           0 :   return dbltor(0.2);
     733             : }
     734             : 
     735             : static GEN
     736          28 : regulatorbound(GEN bnf)
     737             : {
     738             :   long N, R1, R2, R;
     739             :   GEN nf, dK, p1, c1;
     740             : 
     741          28 :   nf = bnf_get_nf(bnf); N = nf_get_degree(nf);
     742          28 :   if (!isprimitive(nf)) return dft_bound();
     743             : 
     744          28 :   dK = absi(nf_get_disc(nf));
     745          28 :   nf_get_sign(nf, &R1, &R2); R = R1+R2-1;
     746          28 :   c1 = (!R2 && N<12)? int2n(N & (~1UL)): powuu(N,N);
     747          28 :   if (cmpii(dK,c1) <= 0) return dft_bound();
     748             : 
     749          28 :   p1 = sqrr(glog(gdiv(dK,c1),DEFAULTPREC));
     750          28 :   p1 = divru(gmul2n(powru(divru(mulru(p1,3),N*(N*N-1)-6*R2),R),R2), N);
     751          28 :   p1 = sqrtr(gdiv(p1, hermiteconstant(R)));
     752          28 :   if (DEBUGLEVEL>1) err_printf("Mahler bound for regulator: %Ps\n",p1);
     753          28 :   return gmax(p1, dbltor(0.2));
     754             : }
     755             : 
     756             : static int
     757       70483 : is_unit(GEN M, long r1, GEN x)
     758             : {
     759       70483 :   pari_sp av = avma;
     760       70483 :   GEN Nx = ground( embed_norm(RgM_zc_mul(M,x), r1) );
     761       70483 :   int ok = is_pm1(Nx);
     762       70483 :   avma = av; return ok;
     763             : }
     764             : 
     765             : /* FIXME: should use smallvectors */
     766             : static double
     767          35 : minimforunits(GEN nf, long BORNE, ulong w)
     768             : {
     769          35 :   const long prec = MEDDEFAULTPREC;
     770          35 :   long n, r1, i, j, k, *x, cnt = 0;
     771          35 :   pari_sp av = avma;
     772             :   GEN r, M;
     773             :   double p, norme, normin, normax;
     774             :   double **q,*v,*y,*z;
     775          35 :   double eps=0.000001, BOUND = BORNE * 1.00001;
     776             : 
     777          35 :   if (DEBUGLEVEL>=2)
     778             :   {
     779           0 :     err_printf("Searching minimum of T2-form on units:\n");
     780           0 :     if (DEBUGLEVEL>2) err_printf("   BOUND = %ld\n",BORNE);
     781           0 :     err_flush();
     782             :   }
     783          35 :   n = nf_get_degree(nf); r1 = nf_get_r1(nf);
     784          35 :   minim_alloc(n+1, &q, &x, &y, &z, &v);
     785          35 :   M = gprec_w(nf_get_M(nf), prec);
     786          35 :   r = gaussred_from_QR(nf_get_G(nf), prec);
     787         168 :   for (j=1; j<=n; j++)
     788             :   {
     789         133 :     v[j] = gtodouble(gcoeff(r,j,j));
     790         133 :     for (i=1; i<j; i++) q[i][j] = gtodouble(gcoeff(r,i,j));
     791             :   }
     792          35 :   normax = 0.; normin = (double)BORNE*(1-eps);
     793          35 :   k=n; y[n]=z[n]=0;
     794          35 :   x[n] = (long)(sqrt(BOUND/v[n]));
     795             : 
     796       70483 :   for(;;x[1]--)
     797             :   {
     798             :     do
     799             :     {
     800       71526 :       if (k>1)
     801             :       {
     802        1043 :         long l = k-1;
     803        1043 :         z[l] = 0;
     804        1043 :         for (j=k; j<=n; j++) z[l] += q[l][j]*x[j];
     805        1043 :         p = (double)x[k] + z[k];
     806        1043 :         y[l] = y[k] + p*p*v[k];
     807        1043 :         x[l] = (long)floor(sqrt((BOUND-y[l])/v[l])-z[l]);
     808        1043 :         k = l;
     809             :       }
     810             :       for(;;)
     811             :       {
     812       72471 :         p = (double)x[k] + z[k];
     813       72471 :         if (y[k] + p*p*v[k] <= BOUND) break;
     814         945 :         k++; x[k]--;
     815         945 :       }
     816             :     }
     817       71526 :     while (k>1);
     818       70518 :     if (!x[1] && y[1]<=eps) break;
     819             : 
     820       70497 :     if (DEBUGLEVEL>8){ err_printf("."); err_flush(); }
     821       70497 :     if (++cnt == 5000) return -1.; /* too expensive */
     822             : 
     823       70483 :     p = (double)x[1] + z[1]; norme = y[1] + p*p*v[1];
     824       70483 :     if (norme+eps > normax) normax = norme;
     825       70483 :     if (is_unit(M, r1, x)
     826          56 :     && (norme > 2*n  /* exclude roots of unity */
     827          21 :         || !ZV_isscalar(nfpow_u(nf, zc_to_ZC(x), w))))
     828             :     {
     829          35 :       if (norme < normin) normin = norme*(1-eps);
     830          35 :       if (DEBUGLEVEL>=2) { err_printf("*"); err_flush(); }
     831             :     }
     832             : 
     833       70483 :   }
     834          21 :   if (DEBUGLEVEL>=2){ err_printf("\n"); err_flush(); }
     835          21 :   avma = av;
     836          21 :   return normin;
     837             : }
     838             : 
     839             : #undef NBMAX
     840             : static int
     841         910 : is_zero(GEN x, long bitprec) { return (gexpo(x) < -bitprec); }
     842             : 
     843             : static int
     844         616 : is_complex(GEN x, long bitprec) { return !is_zero(imag_i(x), bitprec); }
     845             : 
     846             : /* assume M_star t_REAL
     847             :  * FIXME: what does this do ? To be rewritten */
     848             : static GEN
     849          21 : compute_M0(GEN M_star,long N)
     850             : {
     851             :   long m1,m2,n1,n2,n3,lr,lr1,lr2,i,j,l,vx,vy,vz,vM;
     852             :   GEN pol,p1,p2,p3,p4,p5,p6,p7,p8,p9,u,v,w,r,r1,r2,M0,M0_pro,S,P,M;
     853             :   GEN f1,f2,f3,g1,g2,g3,pg1,pg2,pg3,pf1,pf2,pf3,X,Y,Z;
     854          21 :   long bitprec = 24;
     855             : 
     856          21 :   if (N == 2) return gmul2n(sqrr(gacosh(gmul2n(M_star,-1),0)), -1);
     857          14 :   vx = fetch_var(); X = pol_x(vx);
     858          14 :   vy = fetch_var(); Y = pol_x(vy);
     859          14 :   vz = fetch_var(); Z = pol_x(vz);
     860          14 :   vM = fetch_var(); M = pol_x(vM);
     861             : 
     862          14 :   M0 = NULL; m1 = N/3;
     863          35 :   for (n1=1; n1<=m1; n1++) /* 1 <= n1 <= n2 <= n3 < N */
     864             :   {
     865          21 :     m2 = (N-n1)>>1;
     866          63 :     for (n2=n1; n2<=m2; n2++)
     867             :     {
     868          42 :       pari_sp av = avma; n3=N-n1-n2;
     869          42 :       if (n1==n2 && n1==n3) /* n1 = n2 = n3 = m1 = N/3 */
     870             :       {
     871           7 :         p1 = divru(M_star, m1);
     872           7 :         p4 = sqrtr_abs( mulrr(addsr(1,p1),subrs(p1,3)) );
     873           7 :         p5 = subrs(p1,1);
     874           7 :         u = gen_1;
     875           7 :         v = gmul2n(addrr(p5,p4),-1);
     876           7 :         w = gmul2n(subrr(p5,p4),-1);
     877           7 :         M0_pro=gmul2n(mulur(m1,addrr(sqrr(logr_abs(v)),sqrr(logr_abs(w)))), -2);
     878           7 :         if (DEBUGLEVEL>2)
     879             :         {
     880           0 :           err_printf("[ %ld, %ld, %ld ]: %.28Pg\n",n1,n2,n3,M0_pro);
     881           0 :           err_flush();
     882             :         }
     883           7 :         if (!M0 || gcmp(M0_pro,M0) < 0) M0 = M0_pro;
     884             :       }
     885          35 :       else if (n1==n2 || n2==n3)
     886          21 :       { /* n3 > N/3 >= n1 */
     887          21 :         long k = N - 2*n2;
     888          21 :         p2 = deg1pol_shallow(stoi(-n2), M_star, vx); /* M* - n2 X */
     889          21 :         p3 = gmul(powuu(k,k),
     890             :                   gpowgs(gsubgs(RgX_Rg_mul(p2, M_star), k*k), n2));
     891          21 :         pol = gsub(p3, RgX_mul(monomial(powuu(n2,n2), n2, vx),
     892             :                                gpowgs(p2, N-n2)));
     893          21 :         r = roots(pol, DEFAULTPREC); lr = lg(r);
     894         189 :         for (i=1; i<lr; i++)
     895             :         {
     896             :           GEN n2S;
     897         168 :           S = real_i(gel(r,i));
     898         168 :           if (is_complex(gel(r,i), bitprec) || signe(S) <= 0) continue;
     899             : 
     900          91 :           n2S = mulur(n2,S);
     901          91 :           p4 = subrr(M_star, n2S);
     902          91 :           P = divrr(mulrr(n2S,p4), subrs(mulrr(M_star,p4),k*k));
     903          91 :           p5 = subrr(sqrr(S), gmul2n(P,2));
     904          91 :           if (gsigne(p5) < 0) continue;
     905             : 
     906          70 :           p6 = sqrtr(p5);
     907          70 :           v = gmul2n(subrr(S,p6),-1);
     908          70 :           if (gsigne(v) <= 0) continue;
     909             : 
     910          63 :           u = gmul2n(addrr(S,p6),-1);
     911          63 :           w = gpow(P, gdivgs(utoineg(n2),k), 0);
     912          63 :           p6 = mulur(n2, addrr(sqrr(logr_abs(u)), sqrr(logr_abs(v))));
     913          63 :           M0_pro = gmul2n(addrr(p6, mulur(k, sqrr(logr_abs(w)))),-2);
     914          63 :           if (DEBUGLEVEL>2)
     915             :           {
     916           0 :             err_printf("[ %ld, %ld, %ld ]: %.28Pg\n",n1,n2,n3,M0_pro);
     917           0 :             err_flush();
     918             :           }
     919          63 :           if (!M0 || gcmp(M0_pro,M0) < 0) M0 = M0_pro;
     920             :         }
     921             :       }
     922             :       else
     923             :       {
     924          14 :         f1 = gsub(gadd(gmulsg(n1,X),gadd(gmulsg(n2,Y),gmulsg(n3,Z))), M);
     925          14 :         f2 =         gmulsg(n1,gmul(Y,Z));
     926          14 :         f2 = gadd(f2,gmulsg(n2,gmul(X,Z)));
     927          14 :         f2 = gadd(f2,gmulsg(n3,gmul(X,Y)));
     928          14 :         f2 = gsub(f2,gmul(M,gmul(X,gmul(Y,Z))));
     929          14 :         f3 = gsub(gmul(gpowgs(X,n1),gmul(gpowgs(Y,n2),gpowgs(Z,n3))), gen_1);
     930             :         /* f1 = n1 X + n2 Y + n3 Z - M */
     931             :         /* f2 = n1 YZ + n2 XZ + n3 XY */
     932             :         /* f3 = X^n1 Y^n2 Z^n3 - 1*/
     933          14 :         g1=resultant(f1,f2); g1=primpart(g1);
     934          14 :         g2=resultant(f1,f3); g2=primpart(g2);
     935          14 :         g3=resultant(g1,g2); g3=primpart(g3);
     936          14 :         pf1=gsubst(f1,vM,M_star); pg1=gsubst(g1,vM,M_star);
     937          14 :         pf2=gsubst(f2,vM,M_star); pg2=gsubst(g2,vM,M_star);
     938          14 :         pf3=gsubst(f3,vM,M_star); pg3=gsubst(g3,vM,M_star);
     939             :         /* g3 = Res_Y,Z(f1,f2,f3) */
     940          14 :         r = roots(pg3,DEFAULTPREC); lr = lg(r);
     941         238 :         for (i=1; i<lr; i++)
     942             :         {
     943         224 :           w = real_i(gel(r,i));
     944         224 :           if (is_complex(gel(r,i), bitprec) || signe(w) <= 0) continue;
     945          70 :           p1=gsubst(pg1,vz,w);
     946          70 :           p2=gsubst(pg2,vz,w);
     947          70 :           p3=gsubst(pf1,vz,w);
     948          70 :           p4=gsubst(pf2,vz,w);
     949          70 :           p5=gsubst(pf3,vz,w);
     950          70 :           r1 = roots(p1, DEFAULTPREC); lr1 = lg(r1);
     951         210 :           for (j=1; j<lr1; j++)
     952             :           {
     953         140 :             v = real_i(gel(r1,j));
     954         140 :             if (is_complex(gel(r1,j), bitprec) || signe(v) <= 0
     955         126 :              || !is_zero(gsubst(p2,vy,v), bitprec)) continue;
     956             : 
     957          84 :             p7=gsubst(p3,vy,v);
     958          84 :             p8=gsubst(p4,vy,v);
     959          84 :             p9=gsubst(p5,vy,v);
     960          84 :             r2 = roots(p7, DEFAULTPREC); lr2 = lg(r2);
     961         168 :             for (l=1; l<lr2; l++)
     962             :             {
     963          84 :               u = real_i(gel(r2,l));
     964          84 :               if (is_complex(gel(r2,l), bitprec) || signe(u) <= 0
     965          84 :                || !is_zero(gsubst(p8,vx,u), bitprec)
     966          84 :                || !is_zero(gsubst(p9,vx,u), bitprec)) continue;
     967             : 
     968          84 :               M0_pro =              mulur(n1, sqrr(logr_abs(u)));
     969          84 :               M0_pro = gadd(M0_pro, mulur(n2, sqrr(logr_abs(v))));
     970          84 :               M0_pro = gadd(M0_pro, mulur(n3, sqrr(logr_abs(w))));
     971          84 :               M0_pro = gmul2n(M0_pro,-2);
     972          84 :               if (DEBUGLEVEL>2)
     973             :               {
     974           0 :                err_printf("[ %ld, %ld, %ld ]: %.28Pg\n",n1,n2,n3,M0_pro);
     975           0 :                err_flush();
     976             :               }
     977          84 :               if (!M0 || gcmp(M0_pro,M0) < 0) M0 = M0_pro;
     978             :             }
     979             :           }
     980             :         }
     981             :       }
     982          42 :       if (!M0) avma = av; else M0 = gerepilecopy(av, M0);
     983             :     }
     984             :   }
     985          14 :   for (i=1;i<=4;i++) (void)delete_var();
     986          14 :   return M0? M0: gen_0;
     987             : }
     988             : 
     989             : static GEN
     990          49 : lowerboundforregulator(GEN bnf, GEN units)
     991             : {
     992          49 :   long i, N, R2, RU = lg(units)-1;
     993             :   GEN nf, M0, M, G, minunit;
     994             :   double bound;
     995             : 
     996          49 :   if (!RU) return gen_1;
     997          49 :   nf = bnf_get_nf(bnf);
     998          49 :   N = nf_get_degree(nf);
     999          49 :   R2 = nf_get_r2(nf);
    1000             : 
    1001          49 :   G = nf_get_G(nf);
    1002          49 :   minunit = gnorml2(RgM_RgC_mul(G, gel(units,1))); /* T2(units[1]) */
    1003          84 :   for (i=2; i<=RU; i++)
    1004             :   {
    1005          35 :     GEN t = gnorml2(RgM_RgC_mul(G, gel(units,i)));
    1006          35 :     if (gcmp(t,minunit) < 0) minunit = t;
    1007             :   }
    1008          49 :   if (gexpo(minunit) > 30) return NULL;
    1009             : 
    1010          35 :   bound = minimforunits(nf, itos(gceil(minunit)), bnf_get_tuN(bnf));
    1011          35 :   if (bound < 0) return NULL;
    1012          21 :   if (DEBUGLEVEL>1) err_printf("M* = %Ps\n", dbltor(bound));
    1013          21 :   M0 = compute_M0(dbltor(bound), N);
    1014          21 :   if (DEBUGLEVEL>1) { err_printf("M0 = %.28Pg\n",M0); err_flush(); }
    1015          21 :   M = gmul2n(divru(gdiv(powrs(M0,RU),hermiteconstant(RU)),N),R2);
    1016          21 :   if (cmprr(M, dbltor(0.04)) < 0) return NULL;
    1017          21 :   M = sqrtr(M);
    1018          21 :   if (DEBUGLEVEL>1)
    1019           0 :     err_printf("(lower bound for regulator) M = %.28Pg\n",M);
    1020          21 :   return M;
    1021             : }
    1022             : 
    1023             : /* upper bound for the index of bnf.fu in the full unit group */
    1024             : static GEN
    1025          49 : bound_unit_index(GEN bnf, GEN units)
    1026             : {
    1027          49 :   pari_sp av = avma;
    1028          49 :   GEN x = lowerboundforregulator(bnf, units);
    1029          49 :   if (!x) { avma = av; x = regulatorbound(bnf); }
    1030          49 :   return gerepileuptoint(av, ground(gdiv(bnf_get_reg(bnf), x)));
    1031             : }
    1032             : 
    1033             : /* Compute a square matrix of rank #beta attached to a family
    1034             :  * (P_i), 1<=i<=#beta, of primes s.t. N(P_i) = 1 mod p, and
    1035             :  * (P_i,beta[j]) = 1 for all i,j. nf = true nf */
    1036             : static void
    1037        1540 : primecertify(GEN nf, GEN beta, ulong p, GEN bad)
    1038             : {
    1039        1540 :   long lb = lg(beta), rmax = lb - 1;
    1040             :   GEN M, vQ, L;
    1041             :   ulong q;
    1042             :   forprime_t T;
    1043             : 
    1044        1540 :   if (p == 2)
    1045          35 :     L = cgetg(1,t_VECSMALL);
    1046             :   else
    1047        1505 :     L = mkvecsmall(p);
    1048        1540 :   (void)u_forprime_arith_init(&T, 1, ULONG_MAX, 1, p);
    1049        1540 :   M = cgetg(lb,t_MAT); setlg(M,1);
    1050        4454 :   while ((q = u_forprime_next(&T)))
    1051             :   {
    1052             :     GEN qq, gg, og;
    1053             :     long lQ, i, j;
    1054             :     ulong g, m;
    1055        3910 :     if (!umodiu(bad,q)) continue;
    1056             : 
    1057        2774 :     qq = utoipos(q);
    1058        2774 :     vQ = idealprimedec_limit_f(nf,qq,1);
    1059        2774 :     lQ = lg(vQ); if (lQ == 1) continue;
    1060             : 
    1061             :     /* cf rootsof1_Fl */
    1062        1918 :     g = pgener_Fl_local(q, L);
    1063        1918 :     (void)u_lvalrem((q-1) / p, p, &m);
    1064        1918 :     gg = utoipos( Fl_powu(g, m, q) ); /* order p in (Z/q)^* */
    1065        1918 :     og = mkmat2(mkcol(utoi(p)), mkcol(gen_1)); /* order of g */
    1066             : 
    1067        1918 :     if (DEBUGLEVEL>3) err_printf("       generator of (Zk/Q)^*: %lu\n", g);
    1068        2561 :     for (i = 1; i < lQ; i++)
    1069             :     {
    1070        2183 :       GEN C = cgetg(lb, t_VECSMALL);
    1071        2183 :       GEN Q = gel(vQ,i); /* degree 1 */
    1072        2183 :       GEN modpr = zkmodprinit(nf, Q);
    1073             :       long r;
    1074             : 
    1075        6467 :       for (j = 1; j < lb; j++)
    1076             :       {
    1077        4284 :         GEN t = nf_to_Fp_coprime(nf, gel(beta,j), modpr);
    1078        4284 :         t = utoipos( Fl_powu(t[2], m, q) );
    1079             :         /* FIXME: implement Fl_log_Shanks */
    1080        4284 :         C[j] = itou( Fp_log(t, gg, og, qq) ) % p;
    1081             :       }
    1082        2183 :       r = lg(M);
    1083        2183 :       gel(M,r) = C; setlg(M, r+1);
    1084        2183 :       if (Flm_rank(M, p) != r) { setlg(M,r); continue; }
    1085             : 
    1086        1967 :       if (DEBUGLEVEL>2)
    1087             :       {
    1088           0 :         if (DEBUGLEVEL>3)
    1089             :         {
    1090           0 :           err_printf("       prime ideal Q: %Ps\n",Q);
    1091           0 :           err_printf("       matrix log(b_j mod Q_i): %Ps\n", M);
    1092             :         }
    1093           0 :         err_printf("       new rank: %ld\n",r);
    1094             :       }
    1095        5047 :       if (r == rmax) return;
    1096             :     }
    1097             :   }
    1098           0 :   pari_err_BUG("primecertify");
    1099             : }
    1100             : 
    1101             : struct check_pr {
    1102             :   long w; /* #mu(K) */
    1103             :   GEN mu; /* generator of mu(K) */
    1104             :   GEN fu;
    1105             :   GEN cyc;
    1106             :   GEN cycgen;
    1107             :   GEN bad; /* p | bad <--> p | some element occurring in cycgen */
    1108             : };
    1109             : 
    1110             : static void
    1111        1540 : check_prime(ulong p, GEN nf, struct check_pr *S)
    1112             : {
    1113        1540 :   pari_sp av = avma;
    1114        1540 :   long i,b, lc = lg(S->cyc), lf = lg(S->fu);
    1115        1540 :   GEN beta = cgetg(lf+lc, t_VEC);
    1116             : 
    1117        1540 :   if (DEBUGLEVEL>1) err_printf("  *** testing p = %lu\n",p);
    1118        1603 :   for (b=1; b<lc; b++)
    1119             :   {
    1120        1323 :     if (umodiu(gel(S->cyc,b), p)) break; /* p \nmid cyc[b] */
    1121          63 :     if (b==1 && DEBUGLEVEL>2) err_printf("     p divides h(K)\n");
    1122          63 :     gel(beta,b) = gel(S->cycgen,b);
    1123             :   }
    1124        1540 :   if (S->w % p == 0)
    1125             :   {
    1126          35 :     if (DEBUGLEVEL>2) err_printf("     p divides w(K)\n");
    1127          35 :     gel(beta,b++) = S->mu;
    1128             :   }
    1129        1540 :   for (i=1; i<lf; i++) gel(beta,b++) = gel(S->fu,i);
    1130        1540 :   setlg(beta, b); /* beta = [cycgen[i] if p|cyc[i], tu if p|w, fu] */
    1131        1540 :   if (DEBUGLEVEL>3) {err_printf("     Beta list = %Ps\n",beta); err_flush();}
    1132        1540 :   primecertify(nf, beta, p, S->bad); avma = av;
    1133        1540 : }
    1134             : 
    1135             : static void
    1136          49 : init_bad(struct check_pr *S, GEN nf, GEN gen)
    1137             : {
    1138          49 :   long i, l = lg(gen);
    1139          49 :   GEN bad = gen_1;
    1140             : 
    1141         105 :   for (i=1; i < l; i++)
    1142          56 :     bad = lcmii(bad, gcoeff(gel(gen,i),1,1));
    1143         105 :   for (i = 1; i < l; i++)
    1144             :   {
    1145          56 :     GEN c = gel(S->cycgen,i);
    1146             :     long j;
    1147          56 :     if (typ(c) == t_MAT)
    1148             :     {
    1149          56 :       GEN g = gel(c,1);
    1150         147 :       for (j = 1; j < lg(g); j++)
    1151             :       {
    1152          91 :         GEN h = idealhnf_shallow(nf, gel(g,j));
    1153          91 :         bad = lcmii(bad, gcoeff(h,1,1));
    1154             :       }
    1155             :     }
    1156             :   }
    1157          49 :   S->bad = bad;
    1158          49 : }
    1159             : 
    1160             : long
    1161          49 : bnfcertify0(GEN bnf, long flag)
    1162             : {
    1163          49 :   pari_sp av = avma;
    1164             :   long i, N;
    1165             :   GEN nf, cyc, B;
    1166             :   ulong bound, p;
    1167             :   struct check_pr S;
    1168             :   forprime_t T;
    1169             : 
    1170          49 :   bnf = checkbnf(bnf);
    1171          49 :   nf = bnf_get_nf(bnf);
    1172          49 :   N = nf_get_degree(nf); if (N==1) return 1;
    1173          49 :   testprimes(bnf, zimmertbound(N, nf_get_r2(nf), absi(nf_get_disc(nf))));
    1174          49 :   if (flag) return 1;
    1175             : 
    1176          49 :   cyc = bnf_get_cyc(bnf);
    1177          49 :   S.w = bnf_get_tuN(bnf);
    1178          49 :   S.mu = nf_to_scalar_or_basis(nf, bnf_get_tuU(bnf));
    1179          49 :   S.fu= matalgtobasis(nf, bnf_get_fu(bnf));
    1180          49 :   S.cyc = cyc;
    1181          49 :   S.cycgen = check_and_build_cycgen(bnf);
    1182          49 :   init_bad(&S, nf, bnf_get_gen(bnf));
    1183             : 
    1184          49 :   B = bound_unit_index(bnf, S.fu);
    1185          49 :   if (DEBUGLEVEL)
    1186             :   {
    1187           0 :     err_printf("PHASE 2 [UNITS]: are all primes good ?\n");
    1188           0 :     err_printf("  Testing primes <= %Ps\n", B); err_flush();
    1189             :   }
    1190          49 :   bound = itou_or_0(B);
    1191          49 :   if (!bound) pari_err_OVERFLOW("bnfcertify [too many primes to check]");
    1192          49 :   if (u_forprime_init(&T, 2, bound))
    1193          35 :     while ( (p = u_forprime_next(&T)) ) check_prime(p, nf, &S);
    1194          49 :   if (lg(cyc) > 1)
    1195             :   {
    1196          21 :     GEN f = Z_factor(gel(cyc,1)), P = gel(f,1);
    1197          21 :     long l = lg(P);
    1198          21 :     if (DEBUGLEVEL>1) { err_printf("  Testing primes | h(K)\n\n"); err_flush(); }
    1199          49 :     for (i=1; i<l; i++)
    1200             :     {
    1201          28 :       p = itou(gel(P,i));
    1202          28 :       if (p > bound) check_prime(p, nf, &S);
    1203             :     }
    1204             :   }
    1205          49 :   avma = av; return 1;
    1206             : }
    1207             : long
    1208          28 : bnfcertify(GEN bnf) { return bnfcertify0(bnf, 0); }
    1209             : 
    1210             : /*******************************************************************/
    1211             : /*                                                                 */
    1212             : /*        RAY CLASS FIELDS: CONDUCTORS AND DISCRIMINANTS           */
    1213             : /*                                                                 */
    1214             : /*******************************************************************/
    1215             : /* \chi(gen[i]) = zeta_D^chic[i])
    1216             :  * denormalize: express chi(gen[i]) in terms of zeta_{cyc[i]} */
    1217             : GEN
    1218        3976 : char_denormalize(GEN cyc, GEN D, GEN chic)
    1219             : {
    1220        3976 :   long i, l = lg(chic);
    1221        3976 :   GEN chi = cgetg(l, t_VEC);
    1222             :   /* \chi(gen[i]) = e(chic[i] / D) = e(chi[i] / cyc[i])
    1223             :    * hence chi[i] = chic[i]cyc[i]/ D  mod cyc[i] */
    1224        9492 :   for (i = 1; i < l; ++i)
    1225             :   {
    1226        5516 :     GEN di = gel(cyc, i), t = diviiexact(mulii(di, gel(chic,i)), D);
    1227        5516 :     gel(chi, i) = modii(t, di);
    1228             :   }
    1229        3976 :   return chi;
    1230             : }
    1231             : static GEN
    1232         371 : bnrchar_i(GEN bnr, GEN g, GEN v)
    1233             : {
    1234         371 :   long i, h, l = lg(g);
    1235             :   GEN CH, D, U, U2, H, cyc, cycD, dv, dchi;
    1236         371 :   checkbnr(bnr);
    1237         371 :   switch(typ(g))
    1238             :   {
    1239             :     GEN G;
    1240             :     case t_VEC:
    1241          14 :       G = cgetg(l, t_MAT);
    1242          14 :       for (i = 1; i < l; i++) gel(G,i) = isprincipalray(bnr, gel(g,i));
    1243          14 :       g = G; break;
    1244             :     case t_MAT:
    1245         357 :       if (RgM_is_ZM(g)) break;
    1246             :     default:
    1247           0 :       pari_err_TYPE("bnrchar",g);
    1248             :   }
    1249         371 :   cyc = bnr_get_cyc(bnr);
    1250         371 :   H = ZM_hnfall(shallowconcat(g,diagonal_shallow(cyc)), v? &U: NULL, 1);
    1251         371 :   dv = NULL;
    1252         371 :   if (v)
    1253             :   {
    1254          28 :     GEN w = Q_remove_denom(v, &dv);
    1255          28 :     if (typ(v)!=t_VEC || lg(v)!=l || !RgV_is_ZV(w)) pari_err_TYPE("bnrchar",v);
    1256          28 :     if (!dv) v = NULL;
    1257             :     else
    1258             :     {
    1259          28 :       U = rowslice(U, 1, l-1);
    1260          28 :       w = FpV_red(ZV_ZM_mul(w, U), dv);
    1261         105 :       for (i = 1; i < l; i++)
    1262          84 :         if (signe(gel(w,i))) pari_err_TYPE("bnrchar [inconsistent values]",v);
    1263          21 :       v = vecslice(w,l,lg(w)-1);
    1264             :     }
    1265             :   }
    1266             :   /* chi defined on subgroup H, chi(H[i]) = e(v[i] / dv)
    1267             :    * unless v = NULL: chi|H = 1*/
    1268         364 :   h = itos( ZM_det_triangular(H) ); /* #(clgp/H) = number of chars */
    1269         364 :   if (h == 1) /* unique character, H = Id */
    1270             :   {
    1271           7 :     if (v)
    1272           7 :       v = char_denormalize(cyc,dv,v);
    1273             :     else
    1274           0 :       v = zerovec(lg(cyc)-1); /* trivial char */
    1275           7 :     return mkvec(v);
    1276             :   }
    1277             : 
    1278             :   /* chi defined on a subgroup of index h > 1; U H V = D diagonal,
    1279             :    * Z^#H / (H) = Z^#H / (D) ~ \oplus (Z/diZ) */
    1280         357 :   D = ZM_snfall_i(H, &U, NULL, 1);
    1281         357 :   cycD = cyc_normalize(D); gel(cycD,1) = gen_1; /* cycD[i] = d1/di */
    1282         357 :   dchi = gel(D,1);
    1283         357 :   U2 = ZM_diag_mul(cycD, U);
    1284         357 :   if (v)
    1285             :   {
    1286          14 :     GEN Ui = ZM_inv(U,gen_1); /* U^(-1) */
    1287          14 :     GEN Z = hnf_solve(H, ZM_mul_diag(Ui, D));
    1288          14 :     v = ZV_ZM_mul(ZV_ZM_mul(v, Z), U2);
    1289          14 :     dchi = mulii(dchi, dv);
    1290          14 :     U2 = ZM_Z_mul(U2, dv);
    1291             :   }
    1292         357 :   CH = cyc2elts(D);
    1293        1561 :   for (i = 1; i <= h; i++)
    1294             :   {
    1295        1204 :     GEN c = zv_ZM_mul(gel(CH,i), U2);
    1296        1204 :     if (v) c = ZC_add(c, v);
    1297        1204 :     gel(CH,i) = char_denormalize(cyc, dchi, c);
    1298             :   }
    1299         357 :   return CH;
    1300             : }
    1301             : GEN
    1302         371 : bnrchar(GEN bnr, GEN g, GEN v)
    1303             : {
    1304         371 :   pari_sp av = avma;
    1305         371 :   return gerepilecopy(av, bnrchar_i(bnr,g,v));
    1306             : }
    1307             : 
    1308             : /* Let bnr1 with generators, bnr2 be such that mod(bnr2) | mod(bnr1), compute
    1309             :  * the matrix of the surjective map Cl(bnr1) ->> Cl(bnr2) */
    1310             : GEN
    1311        1414 : bnrsurjection(GEN bnr1, GEN bnr2)
    1312             : {
    1313             :   long l, i;
    1314        1414 :   GEN M, gen = bnr_get_gen(bnr1);
    1315        1414 :   l = lg(gen); M = cgetg(l, t_MAT);
    1316        1414 :   for (i = 1; i < l; i++) gel(M,i) = isprincipalray(bnr2, gel(gen,i));
    1317        1414 :   return M;
    1318             : }
    1319             : 
    1320             : /* Given normalized chi on bnr.clgp of conductor bnrc.mod,
    1321             :  * compute primitive character chic on bnrc.clgp equivalent to chi,
    1322             :  * still normalized wrt. bnr:
    1323             :  *   chic(genc[i]) = zeta_C^chic[i]), C = cyc_normalize(bnr.cyc)[1] */
    1324             : GEN
    1325         630 : bnrchar_primitive(GEN bnr, GEN nchi, GEN bnrc)
    1326             : {
    1327         630 :   GEN Mc, U, M = bnrsurjection(bnr, bnrc);
    1328         630 :   long l = lg(M);
    1329             : 
    1330         630 :   Mc = diagonal_shallow(bnr_get_cyc(bnrc));
    1331         630 :   (void)ZM_hnfall(shallowconcat(M, Mc), &U, 1); /* identity */
    1332         630 :   U = matslice(U,1,l-1, l,lg(U)-1);
    1333         630 :   return char_simplify(gel(nchi,1), ZV_ZM_mul(gel(nchi,2), U));
    1334             : }
    1335             : 
    1336             : /* s: <gen> = Cl_f --> Cl_f2 --> 0, H subgroup of Cl_f (generators given as
    1337             :  * HNF on [gen]). Return subgroup s(H) in Cl_f2. bnr must include generators */
    1338             : static GEN
    1339         301 : imageofgroup(GEN bnr, GEN bnr2, GEN H)
    1340             : {
    1341         301 :   GEN H2, cyc2 = bnr_get_cyc(bnr2);
    1342         301 :   if (!H) return diagonal_shallow(cyc2);
    1343         273 :   H2 = ZM_mul(bnrsurjection(bnr, bnr2), H);
    1344         273 :   return ZM_hnfmodid(H2, cyc2); /* s(H) in Cl_n */
    1345             : }
    1346             : static GEN
    1347         357 : imageofchar(GEN bnr, GEN bnrc, GEN chi)
    1348             : {
    1349         357 :   GEN nchi = char_normalize(chi, cyc_normalize(bnr_get_cyc(bnr)));
    1350         357 :   GEN DC = bnrchar_primitive(bnr, nchi, bnrc);
    1351         357 :   return char_denormalize(bnr_get_cyc(bnrc), gel(DC,1), gel(DC,2));
    1352             : }
    1353             : 
    1354             : /* convert A,B,C to [bnr, H] */
    1355             : GEN
    1356         259 : ABC_to_bnr(GEN A, GEN B, GEN C, GEN *H, int gen)
    1357             : {
    1358         259 :   if (typ(A) == t_VEC)
    1359         259 :     switch(lg(A))
    1360             :     {
    1361             :       case 7: /* bnr */
    1362         105 :         *H = B; return A;
    1363             :       case 11: /* bnf */
    1364         154 :         if (!B) pari_err_TYPE("ABC_to_bnr [bnf+missing conductor]",A);
    1365         154 :         *H = C; return Buchray(A,B, gen? nf_INIT | nf_GEN: nf_INIT);
    1366             :     }
    1367           0 :   pari_err_TYPE("ABC_to_bnr",A);
    1368           0 :   *H = NULL; return NULL; /* not reached */
    1369             : }
    1370             : 
    1371             : GEN
    1372          56 : bnrconductor0(GEN A, GEN B, GEN C, long flag)
    1373             : {
    1374          56 :   pari_sp av = avma;
    1375          56 :   GEN H, bnr = ABC_to_bnr(A,B,C,&H, flag > 0);
    1376          56 :   return gerepilecopy(av, bnrconductor_i(bnr, H, flag));
    1377             : }
    1378             : 
    1379             : long
    1380          28 : bnrisconductor0(GEN A,GEN B,GEN C)
    1381             : {
    1382          28 :   GEN H, bnr = ABC_to_bnr(A,B,C,&H, 0);
    1383          28 :   return bnrisconductor(bnr, H);
    1384             : }
    1385             : 
    1386             : /* return bnrisprincipal(bnr, (x)), assuming z = ideallog(x) */
    1387             : static GEN
    1388       16037 : ideallog_to_bnr(GEN bnr, GEN z)
    1389             : {
    1390       16037 :   GEN U = gel(bnr,4), divray = bnr_get_cyc(bnr);
    1391             :   long j, l, lU, lz;
    1392             :   int col;
    1393             : 
    1394       16037 :   if (lg(z) == 1) return z;
    1395       16037 :   col = (typ(z) == t_COL); /* else t_MAT */
    1396       16037 :   lz = col? lg(z): lgcols(z);
    1397       16037 :   lU = lg(U);
    1398       16037 :   if (lz != lU)
    1399             :   {
    1400       11340 :     if (lz == 1) return zerocol(nbrows(U)); /* lU != 1 */
    1401       11326 :     U = vecslice(U, lU-lz+1, lU-1); /* remove Cl(K) part */
    1402             :   }
    1403       16023 :   if (col) {
    1404        6216 :     z = ZM_ZC_mul(U, z);
    1405        6216 :     z = vecmodii(z, divray);
    1406             :   } else {
    1407        9807 :     z = ZM_mul(U, z); l = lg(z);
    1408        9807 :     for (j = 1; j < l; j++) gel(z,j) = vecmodii(gel(z,j), divray);
    1409             :   }
    1410       16023 :   return z;
    1411             : }
    1412             : static GEN
    1413        9821 : bnr_log_gen_pr(GEN bnr, zlog_S *S, GEN nf, long e, long index)
    1414        9821 : { return ideallog_to_bnr(bnr, log_gen_pr(S, index, nf, e)); }
    1415             : static GEN
    1416        6216 : bnr_log_gen_arch(GEN bnr, zlog_S *S, long index)
    1417        6216 : { return ideallog_to_bnr(bnr, log_gen_arch(S, index)); }
    1418             : 
    1419             : /* A \subset H ? Allow H = NULL = trivial subgroup */
    1420             : static int
    1421       14714 : contains(GEN H, GEN A)
    1422       14714 : { return H? (hnf_solve(H, A) != NULL): gequal0(A); }
    1423             : 
    1424             : /* (see also Discrayrel). Given a number field bnf=bnr[1], a ray class
    1425             :  * group structure bnr (with generators if flag > 0), and a subgroup H of the
    1426             :  * ray class group, compute the conductor of H if flag=0. If flag > 0, compute
    1427             :  * furthermore the corresponding H' and output
    1428             :  * if flag = 1: [[ideal,arch],[hm,cyc,gen],H']
    1429             :  * if flag = 2: [[ideal,arch],newbnr,H'] */
    1430             : GEN
    1431        4354 : bnrconductor_i(GEN bnr, GEN H0, long flag)
    1432             : {
    1433             :   long j, k, l;
    1434             :   GEN bnf, nf, bid, ideal, archp, clhray, bnrc, e2, e, cond, H;
    1435        4354 :   int iscond0 = 1, iscondinf = 1, ischi;
    1436             :   zlog_S S;
    1437             : 
    1438        4354 :   checkbnr(bnr);
    1439        4354 :   bnf = bnr_get_bnf(bnr);
    1440        4354 :   bid = bnr_get_bid(bnr); init_zlog_bid(&S, bid);
    1441        4354 :   nf = bnf_get_nf(bnf);
    1442        4354 :   H = check_subgroup(bnr, H0, &clhray);
    1443             : 
    1444        4354 :   archp = S.archp;
    1445        4354 :   e     = S.e; l = lg(e);
    1446        4354 :   e2 = cgetg(l, t_COL);
    1447        9310 :   for (k = 1; k < l; k++)
    1448             :   {
    1449        7742 :     for (j = itos(gel(e,k)); j > 0; j--)
    1450             :     {
    1451        6244 :       if (!contains(H, bnr_log_gen_pr(bnr, &S, nf, j, k))) break;
    1452        2786 :       iscond0 = 0;
    1453             :     }
    1454        4956 :     gel(e2,k) = stoi(j);
    1455             :   }
    1456        4354 :   l = lg(archp);
    1457        8246 :   for (k = 1; k < l; k++)
    1458             :   {
    1459        3892 :     if (!contains(H, bnr_log_gen_arch(bnr, &S, k))) continue;
    1460        1701 :     archp[k] = 0;
    1461        1701 :     iscondinf = 0;
    1462             :   }
    1463        4354 :   if (!iscondinf)
    1464             :   {
    1465        3157 :     for (j = k = 1; k < l; k++)
    1466        1764 :       if (archp[k]) archp[j++] = archp[k];
    1467        1393 :     setlg(archp, j);
    1468             :   }
    1469        4354 :   ideal = iscond0? bid_get_ideal(bid): factorbackprime(nf, S.P, e2);
    1470        4354 :   cond = mkvec2(ideal, indices_to_vec01(archp, nf_get_r1(nf)));
    1471        4354 :   if (!flag) return cond;
    1472             : 
    1473             :   /* character or subgroup ? */
    1474        1813 :   ischi = H0 && typ(H0) == t_VEC;
    1475        1813 :   if (iscond0 && iscondinf)
    1476             :   {
    1477        1155 :     bnrc = bnr;
    1478        2310 :     if (ischi)
    1479         497 :       H = H0;
    1480         658 :     else if (!H)
    1481         476 :       H = diagonal_shallow(bnr_get_cyc(bnr));
    1482             :   }
    1483             :   else
    1484             :   {
    1485         658 :     bnrc = Buchray(bnf, cond, nf_INIT | nf_GEN);
    1486         658 :     if (ischi)
    1487         357 :       H = imageofchar(bnr, bnrc, H0);
    1488             :     else
    1489         301 :       H = imageofgroup(bnr, bnrc, H);
    1490             :   }
    1491             : 
    1492        1813 :   if (flag == 1) bnrc = bnr_get_clgp(bnrc);
    1493        1813 :   return mkvec3(cond, bnrc, H);
    1494             : }
    1495             : GEN
    1496           0 : bnrconductor(GEN bnr, GEN H0, long flag)
    1497             : {
    1498           0 :   pari_sp av = avma;
    1499           0 :   return gerepilecopy(av, bnrconductor_i(bnr,H0,flag));
    1500             : }
    1501             : 
    1502             : long
    1503        3297 : bnrisconductor(GEN bnr, GEN H0)
    1504             : {
    1505        3297 :   pari_sp av = avma;
    1506             :   long j, k, l;
    1507             :   GEN bnf, nf, bid, archp, clhray, e, H;
    1508             :   zlog_S S;
    1509             : 
    1510        3297 :   checkbnr(bnr);
    1511        3297 :   bnf = bnr_get_bnf(bnr);
    1512        3297 :   bid = bnr_get_bid(bnr); init_zlog_bid(&S, bid);
    1513        3297 :   nf = bnf_get_nf(bnf);
    1514        3297 :   H = check_subgroup(bnr, H0, &clhray);
    1515             : 
    1516        3297 :   archp = S.archp;
    1517        3297 :   e     = S.e; l = lg(e);
    1518        4228 :   for (k = 1; k < l; k++)
    1519             :   {
    1520        2842 :     j = itos(gel(e,k));
    1521        2842 :     if (contains(H, bnr_log_gen_pr(bnr, &S, nf, j, k))) { avma = av; return 0; }
    1522             :   }
    1523        1386 :   l = lg(archp);
    1524        2072 :   for (k = 1; k < l; k++)
    1525        1589 :     if (contains(H, bnr_log_gen_arch(bnr, &S, k))) { avma = av; return 0; }
    1526         483 :   avma = av; return 1;
    1527             : }
    1528             : 
    1529             : /* return the norm group corresponding to the relative extension given by
    1530             :  * polrel over bnr.bnf, assuming it is abelian and the modulus of bnr is a
    1531             :  * multiple of the conductor */
    1532             : static GEN
    1533         672 : rnfnormgroup_i(GEN bnr, GEN polrel)
    1534             : {
    1535             :   long i, j, degrel, degnf, k;
    1536             :   GEN bnf, index, discnf, nf, G, detG, fa, gdegrel;
    1537             :   GEN fac, col, cnd;
    1538             :   forprime_t S;
    1539             :   ulong p;
    1540             : 
    1541         672 :   checkbnr(bnr); bnf = bnr_get_bnf(bnr);
    1542         672 :   nf = bnf_get_nf(bnf);
    1543         672 :   cnd = gel(bnr_get_mod(bnr), 1);
    1544         672 :   polrel = RgX_nffix("rnfnormgroup", nf_get_pol(nf),polrel,1);
    1545         672 :   if (!gequal1(leading_coeff(polrel)))
    1546           0 :     pari_err_IMPL("rnfnormgroup for non-monic polynomials");
    1547             : 
    1548         672 :   degrel = degpol(polrel);
    1549         672 :   if (umodiu(bnr_get_no(bnr), degrel)) return NULL;
    1550             :   /* degrel-th powers are in norm group */
    1551         665 :   gdegrel = utoipos(degrel);
    1552         665 :   G = FpC_red(bnr_get_cyc(bnr), gdegrel);
    1553        1729 :   for (i=1; i<lg(G); i++)
    1554        1064 :     if (!signe(gel(G,i))) gel(G,i) = gdegrel;
    1555         665 :   detG = ZV_prod(G);
    1556         665 :   k = abscmpiu(detG,degrel);
    1557         665 :   if (k < 0) return NULL;
    1558         665 :   if (!k) return diagonal(G);
    1559             : 
    1560         329 :   G = diagonal_shallow(G);
    1561         329 :   discnf = nf_get_disc(nf);
    1562         329 :   index  = nf_get_index(nf);
    1563         329 :   degnf = nf_get_degree(nf);
    1564         329 :   u_forprime_init(&S, 2, ULONG_MAX);
    1565        3871 :   while ( (p = u_forprime_next(&S)) )
    1566             :   {
    1567             :     long oldf, nfa;
    1568             :     /* If all pr are unramified and have the same residue degree, p =prod pr
    1569             :      * and including last pr^f or p^f is the same, but the last isprincipal
    1570             :      * is much easier! oldf is used to track this */
    1571             : 
    1572        3542 :     if (!umodiu(index, p)) continue; /* can't be treated efficiently */
    1573             : 
    1574             :     /* primes of degree 1 are enough, and simpler */
    1575        3444 :     fa = idealprimedec_limit_f(nf, utoipos(p), 1);
    1576        3444 :     nfa = lg(fa)-1;
    1577        3444 :     if (!nfa) continue;
    1578             :     /* all primes above p included ? */
    1579        1680 :     oldf = (nfa == degnf)? -1: 0;
    1580        3703 :     for (i=1; i<=nfa; i++)
    1581             :     {
    1582        2352 :       GEN pr = gel(fa,i), pp, T, polr, modpr;
    1583             :       long f, nfac;
    1584             :       /* if pr (probably) ramified, we have to use all (non-ram) P | pr */
    1585        4221 :       if (idealval(nf,cnd,pr)) { oldf = 0; continue; }
    1586        2121 :       modpr = zk_to_Fq_init(nf, &pr, &T, &pp); /* T = NULL, pp ignored */
    1587        2121 :       polr = nfX_to_FqX(polrel, nf, modpr); /* in Fp[X] */
    1588        2121 :       polr = ZX_to_Flx(polr, p);
    1589        2121 :       if (!Flx_is_squarefree(polr, p)) { oldf = 0; continue; }
    1590             : 
    1591        1911 :       fac = gel(Flx_factor(polr, p), 1);
    1592        1911 :       f = degpol(gel(fac,1));
    1593        1911 :       if (f == degrel) continue; /* degrel-th powers already included */
    1594         483 :       nfac = lg(fac)-1;
    1595             :       /* check decomposition of pr has Galois type */
    1596        1414 :       for (j=2; j<=nfac; j++)
    1597        1267 :         if (degpol(gel(fac,j)) != f) return NULL;
    1598         476 :       if (oldf < 0) oldf = f; else if (oldf != f) oldf = 0;
    1599             : 
    1600             :       /* last prime & all pr^f, pr | p, included. Include p^f instead */
    1601         476 :       if (oldf && i == nfa && degrel == nfa*f && !umodiu(discnf, p))
    1602           0 :         pr = utoipos(p);
    1603             : 
    1604             :       /* pr^f = N P, P | pr, hence is in norm group */
    1605         476 :       col = bnrisprincipal(bnr,pr,0);
    1606         476 :       if (f > 1) col = ZC_z_mul(col, f);
    1607         476 :       G = ZM_hnf(shallowconcat(G, col));
    1608         476 :       detG = ZM_det_triangular(G);
    1609         476 :       k = abscmpiu(detG,degrel);
    1610         476 :       if (k < 0) return NULL;
    1611         476 :       if (!k) { cgiv(detG); return G; }
    1612             :     }
    1613             :   }
    1614           0 :   return NULL;
    1615             : }
    1616             : GEN
    1617         329 : rnfnormgroup(GEN bnr, GEN polrel)
    1618             : {
    1619         329 :   pari_sp av = avma;
    1620         329 :   GEN G = rnfnormgroup_i(bnr, polrel);
    1621         329 :   if (!G) { avma = av; return cgetg(1,t_MAT); }
    1622         322 :   return gerepileupto(av, G);
    1623             : }
    1624             : 
    1625             : GEN
    1626          14 : nf_deg1_prime(GEN nf)
    1627             : {
    1628          14 :   GEN z, T = nf_get_pol(nf), D = nf_get_disc(nf), f = nf_get_index(nf);
    1629          14 :   long degnf = degpol(T);
    1630             :   forprime_t S;
    1631             :   pari_sp av;
    1632             :   ulong p;
    1633          14 :   u_forprime_init(&S, degnf, ULONG_MAX);
    1634          14 :   av = avma;
    1635         672 :   while ( (p = u_forprime_next(&S)) )
    1636             :   {
    1637             :     ulong r;
    1638         658 :     if (!umodiu(D, p) || !umodiu(f, p)) continue;
    1639         630 :     r = Flx_oneroot(ZX_to_Flx(T,p), p);
    1640         630 :     if (r != p)
    1641             :     {
    1642          14 :       z = utoi(Fl_neg(r, p));
    1643          14 :       z = deg1pol_shallow(gen_1, z, varn(T));
    1644          14 :       return primedec_apply_kummer(nf, z, 1, utoipos(p));
    1645             :     }
    1646         616 :     avma = av;
    1647             :   }
    1648           0 :   return NULL;
    1649             : }
    1650             : 
    1651             : long
    1652          42 : rnfisabelian(GEN nf, GEN pol)
    1653             : {
    1654             :   GEN modpr, pr, T, Tnf, pp, ro, nfL, C, z, a, sig, eq;
    1655             :   long i, j, l, v;
    1656             :   ulong p, k, ka;
    1657             : 
    1658          42 :   if (typ(nf) == t_POL)
    1659          35 :     Tnf = nf;
    1660             :   else {
    1661           7 :     nf = checknf(nf);
    1662           7 :     Tnf = nf_get_pol(nf);
    1663             :   }
    1664          42 :   v = varn(Tnf);
    1665          42 :   pol = RgX_nffix("rnfisabelian",Tnf,pol,1);
    1666          42 :   eq = nf_rnfeq(nf,pol); /* init L := K[x]/(pol), nf attached to K */
    1667          42 :   C = gel(eq,1); setvarn(C, v); /* L = Q[t]/(C) */
    1668          42 :   a = gel(eq,2); setvarn(a, v); /* root of K.pol in L */
    1669          42 :   z = nfroots_split(C, QXX_QXQ_eval(pol, a, C));
    1670          42 :   if (!z) return 0;
    1671          35 :   ro = gel(z,1); l = lg(ro)-1;
    1672             :   /* small groups are abelian, as are groups of prime order */
    1673          35 :   if (l < 6 || uisprime(l)) return 1;
    1674             : 
    1675          14 :   nfL = gel(z,2);
    1676          14 :   pr = nf_deg1_prime(nfL);
    1677          14 :   modpr = nf_to_Fq_init(nfL, &pr, &T, &pp);
    1678          14 :   p = itou(pp);
    1679          14 :   k = umodiu(gel(eq,3), p);
    1680          14 :   ka = (k * itou(nf_to_Fq(nfL, a, modpr))) % p;
    1681          14 :   sig= cgetg(l+1, t_VECSMALL);
    1682             :   /* image of c = ro[1] + k a [distinguished root of C] by the l automorphisms
    1683             :    * sig[i]: ro[1] -> ro[i] */
    1684          98 :   for (i = 1; i <= l; i++)
    1685          84 :     sig[i] = Fl_add(ka, itou(nf_to_Fq(nfL, gel(ro,i), modpr)), p);
    1686          14 :   ro = Q_primpart(ro);
    1687          84 :   for (i=2; i<=l; i++) { /* start at 2, since sig[1] = identity */
    1688          70 :     gel(ro,i) = ZX_to_Flx(gel(ro,i), p);
    1689         210 :     for (j=2; j<i; j++)
    1690         280 :       if (Flx_eval(gel(ro,j), sig[i], p)
    1691         140 :        != Flx_eval(gel(ro,i), sig[j], p)) return 0;
    1692             :   }
    1693          14 :   return 1;
    1694             : }
    1695             : 
    1696             : /* Given bnf and polrel defining an abelian relative extension, compute the
    1697             :  * corresponding conductor and congruence subgroup. Return
    1698             :  * [cond,bnr(cond),group] where cond=[ideal,arch] is the conductor. */
    1699             : GEN
    1700         343 : rnfconductor(GEN bnf, GEN polrel)
    1701             : {
    1702         343 :   pari_sp av = avma;
    1703             :   GEN nf, module, bnr, group, den, D;
    1704             : 
    1705         343 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
    1706         343 :   if (typ(polrel) != t_POL) pari_err_TYPE("rnfconductor",polrel);
    1707         343 :   den = Q_denom( RgX_to_nfX(nf, polrel) );
    1708         343 :   if (!is_pm1(den)) polrel = RgX_rescale(polrel, den);
    1709         343 :   (void)rnfallbase(nf,&polrel, &D, NULL, NULL);
    1710         343 :   module = mkvec2(D, const_vec(nf_get_r1(nf), gen_1));
    1711         343 :   bnr   = Buchray(bnf,module,nf_INIT | nf_GEN);
    1712         343 :   group = rnfnormgroup_i(bnr,polrel);
    1713         343 :   if (!group) { avma = av; return gen_0; }
    1714         336 :   return gerepilecopy(av, bnrconductor_i(bnr,group,2));
    1715             : }
    1716             : 
    1717             : /* Given a number field bnf=bnr[1], a ray class group structure bnr, and a
    1718             :  * subgroup H (HNF form) of the ray class group, compute [n, r1, dk]
    1719             :  * attached to H. If flcond = 1, abort (return gen_0) if
    1720             :  * module is not the conductor If flrel = 0, compute only N(dk) instead of
    1721             :  * the ideal dk proper */
    1722             : static GEN
    1723         175 : Discrayrel(GEN bnr, GEN H0, long flag)
    1724             : {
    1725         175 :   pari_sp av = avma;
    1726         175 :   long j, k, l, nz, flrel = flag & rnf_REL, flcond = flag & rnf_COND;
    1727             :   GEN bnf, nf, bid, ideal, archp, clhray, clhss, P, e, dlk;
    1728             :   zlog_S S;
    1729             : 
    1730         175 :   checkbnr(bnr);
    1731         175 :   bnf = bnr_get_bnf(bnr);
    1732         175 :   bid = bnr_get_bid(bnr); init_zlog_bid(&S, bid);
    1733         175 :   nf = bnf_get_nf(bnf);
    1734         175 :   ideal= bid_get_ideal(bid);
    1735         175 :   H0 = check_subgroup(bnr, H0, &clhray);
    1736         175 :   if (!H0) H0 = diagonal_shallow(bnr_get_cyc(bnr));
    1737         175 :   archp = S.archp;
    1738         175 :   P     = S.P;
    1739         175 :   e     = S.e; l = lg(e);
    1740         224 :   dlk = flrel? idealpow(nf,ideal,clhray)
    1741         224 :              : powii(ZM_det_triangular(ideal),clhray);
    1742         308 :   for (k = 1; k < l; k++)
    1743             :   {
    1744         147 :     GEN pr = gel(P,k), sum = gen_0, H = H0;
    1745         147 :     long ep = itos(gel(e,k));
    1746         266 :     for (j = ep; j > 0; j--)
    1747             :     {
    1748         182 :       GEN z = bnr_log_gen_pr(bnr, &S, nf, j, k);
    1749         182 :       H = ZM_hnf(shallowconcat(H, z));
    1750         182 :       clhss = ZM_det_triangular(H);
    1751         182 :       if (flcond && j==ep && equalii(clhss,clhray)) { avma = av; return gen_0; }
    1752         168 :       if (is_pm1(clhss)) { sum = addis(sum, j); break; }
    1753         119 :       sum = addii(sum, clhss);
    1754             :     }
    1755         133 :     dlk = flrel? idealdivpowprime(nf, dlk, pr, sum)
    1756         133 :                : diviiexact(dlk, powii(pr_norm(pr),sum));
    1757             :   }
    1758         161 :   l = lg(archp); nz = nf_get_r1(nf) - (l-1);
    1759         280 :   for (k = 1; k < l; k++)
    1760             :   {
    1761         147 :     if (!contains(H0, bnr_log_gen_arch(bnr, &S, k))) continue;
    1762          98 :     if (flcond) { avma = av; return gen_0; }
    1763          70 :     nz++;
    1764             :   }
    1765         133 :   return gerepilecopy(av, mkvec3(clhray, stoi(nz), dlk));
    1766             : }
    1767             : 
    1768             : GEN
    1769         175 : bnrdisc(GEN bnr, GEN H, long flag)
    1770             : {
    1771         175 :   pari_sp av = avma;
    1772             :   long clhray, n, R1;
    1773             :   GEN z, p1, D, dk, nf, dkabs;
    1774             : 
    1775         175 :   D = Discrayrel(bnr, H, flag);
    1776         175 :   if ((flag & rnf_REL) || D == gen_0) return D;
    1777             : 
    1778          98 :   nf = checknf(bnr);
    1779          98 :   dkabs = absi(nf_get_disc(nf));
    1780          98 :   clhray = itos(gel(D,1)); p1 = powiu(dkabs, clhray);
    1781          98 :   n = clhray * nf_get_degree(nf);
    1782          98 :   R1= clhray * itos(gel(D,2));
    1783          98 :   dk = gel(D,3);
    1784          98 :   if (((n-R1)&3) == 2) dk = negi(dk); /* (2r2) mod 4 = 2 : r2(relext) is odd */
    1785          98 :   z = cgetg(4,t_VEC);
    1786          98 :   gel(z,1) = utoipos(n);
    1787          98 :   gel(z,2) = stoi(R1);
    1788          98 :   gel(z,3) = mulii(dk,p1); return gerepileupto(av, z);
    1789             : }
    1790             : 
    1791             : GEN
    1792         175 : bnrdisc0(GEN A, GEN B, GEN C, long flag)
    1793             : {
    1794         175 :   GEN H, bnr = ABC_to_bnr(A,B,C,&H, 0);
    1795         175 :   return bnrdisc(bnr,H,flag);
    1796             : }
    1797             : GEN
    1798           0 : discrayrel(GEN bnr, GEN H)
    1799           0 : { return bnrdisc(bnr,H,rnf_REL); }
    1800             : GEN
    1801           0 : discrayrelcond(GEN bnr, GEN H)
    1802           0 : { return bnrdisc(bnr,H,rnf_REL | rnf_COND); }
    1803             : GEN
    1804           0 : discrayabs(GEN bnr, GEN H)
    1805           0 : { return bnrdisc(bnr,H,0); }
    1806             : GEN
    1807           0 : discrayabscond(GEN bnr, GEN H)
    1808           0 : { return bnrdisc(bnr,H,rnf_COND); }
    1809             : 
    1810             : /* Given a number field bnf=bnr[1], a ray class group structure bnr and a
    1811             :  * vector chi representing a character on the generators bnr[2][3], compute
    1812             :  * the conductor of chi. */
    1813             : GEN
    1814        2079 : bnrconductorofchar(GEN bnr, GEN chi)
    1815             : {
    1816        2079 :   pari_sp av = avma;
    1817             :   GEN cyc, K;
    1818        2079 :   checkbnr(bnr);
    1819        2079 :   cyc = bnr_get_cyc(bnr);
    1820        2079 :   if (!char_check(cyc,chi)) pari_err_TYPE("bnrconductorofchar",chi);
    1821        2079 :   K = charker(cyc,chi); if (lg(K) == 1) K = NULL;
    1822        2079 :   return gerepilecopy(av, bnrconductor_i(bnr, K, 0));
    1823             : }
    1824             : 
    1825             : /* t = [bid,U], h = #Cl(K) */
    1826             : static GEN
    1827         938 : get_classno(GEN t, GEN h)
    1828             : {
    1829         938 :   GEN bid = gel(t,1), m = gel(t,2), cyc = bid_get_cyc(bid);
    1830         938 :   return mulii(h, ZM_det_triangular(ZM_hnfmodid(m, cyc)));
    1831             : }
    1832             : 
    1833             : static void
    1834          28 : chk_listBU(GEN L, const char *s) {
    1835          28 :   if (typ(L) != t_VEC) pari_err_TYPE(s,L);
    1836          28 :   if (lg(L) > 1) {
    1837          28 :     GEN z = gel(L,1);
    1838          28 :     if (typ(z) != t_VEC) pari_err_TYPE(s,z);
    1839          56 :     if (lg(z) == 1) return;
    1840          28 :     z = gel(z,1); /* [bid,U] */
    1841          28 :     if (typ(z) != t_VEC || lg(z) != 3) pari_err_TYPE(s,z);
    1842          28 :     checkbid(gel(z,1));
    1843             :   }
    1844             : }
    1845             : 
    1846             : /* Given lists of [bid, unit ideallogs], return lists of ray class
    1847             :  * numbers */
    1848             : GEN
    1849           7 : bnrclassnolist(GEN bnf,GEN L)
    1850             : {
    1851           7 :   pari_sp av = avma;
    1852           7 :   long i, j, lz, l = lg(L);
    1853             :   GEN v, z, V, h;
    1854             : 
    1855           7 :   chk_listBU(L, "bnrclassnolist");
    1856           7 :   if (l == 1) return cgetg(1, t_VEC);
    1857           7 :   bnf = checkbnf(bnf); h = bnf_get_no(bnf);
    1858           7 :   V = cgetg(l,t_VEC);
    1859         392 :   for (i = 1; i < l; i++)
    1860             :   {
    1861         385 :     z = gel(L,i); lz = lg(z);
    1862         385 :     gel(V,i) = v = cgetg(lz,t_VEC);
    1863         385 :     for (j=1; j<lz; j++) gel(v,j) = get_classno(gel(z,j), h);
    1864             :   }
    1865           7 :   return gerepilecopy(av, V);
    1866             : }
    1867             : 
    1868             : static GEN
    1869        1267 : Lbnrclassno(GEN L, GEN fac)
    1870             : {
    1871        1267 :   long i, l = lg(L);
    1872        1778 :   for (i=1; i<l; i++)
    1873        1778 :     if (gequal(gmael(L,i,1),fac)) return gmael(L,i,2);
    1874           0 :   pari_err_BUG("Lbnrclassno");
    1875           0 :   return NULL; /* not reached */
    1876             : }
    1877             : 
    1878             : static GEN
    1879         420 : factordivexact(GEN fa1,GEN fa2)
    1880             : {
    1881             :   long i, j, k, c, l;
    1882             :   GEN P, E, P1, E1, P2, E2, p1;
    1883             : 
    1884         420 :   P1 = gel(fa1,1); E1 = gel(fa1,2); l = lg(P1);
    1885         420 :   P2 = gel(fa2,1); E2 = gel(fa2,2);
    1886         420 :   P = cgetg(l,t_COL);
    1887         420 :   E = cgetg(l,t_COL);
    1888         931 :   for (c = i = 1; i < l; i++)
    1889             :   {
    1890         511 :     j = RgV_isin(P2,gel(P1,i));
    1891         511 :     if (!j) { gel(P,c) = gel(P1,i); gel(E,c) = gel(E1,i); c++; }
    1892             :     else
    1893             :     {
    1894         511 :       p1 = subii(gel(E1,i), gel(E2,j)); k = signe(p1);
    1895         511 :       if (k < 0) pari_err_BUG("factordivexact [not exact]");
    1896         511 :       if (k > 0) { gel(P,c) = gel(P1,i); gel(E,c) = p1; c++; }
    1897             :     }
    1898             :   }
    1899         420 :   setlg(P, c);
    1900         420 :   setlg(E, c); return mkmat2(P, E);
    1901             : }
    1902             : /* remove index k */
    1903             : static GEN
    1904        1015 : factorsplice(GEN fa, long k)
    1905             : {
    1906        1015 :   GEN p = gel(fa,1), e = gel(fa,2), P, E;
    1907        1015 :   long i, l = lg(p) - 1;
    1908        1015 :   P = cgetg(l, typ(p));
    1909        1015 :   E = cgetg(l, typ(e));
    1910        1015 :   for (i=1; i<k; i++) { P[i] = p[i]; E[i] = e[i]; }
    1911        1015 :   p++; e++;
    1912        1015 :   for (   ; i<l; i++) { P[i] = p[i]; E[i] = e[i]; }
    1913        1015 :   return mkmat2(P,E);
    1914             : }
    1915             : static GEN
    1916         840 : factorpow(GEN fa, long n)
    1917             : {
    1918         840 :   if (!n) return trivial_fact();
    1919         840 :   return mkmat2(gel(fa,1), gmulsg(n, gel(fa,2)));
    1920             : }
    1921             : static GEN
    1922        1071 : factormul(GEN fa1,GEN fa2)
    1923             : {
    1924        1071 :   GEN p, pnew, e, enew, v, P, y = famat_mul_shallow(fa1,fa2);
    1925             :   long i, c, lx;
    1926             : 
    1927        1071 :   p = gel(y,1); v = indexsort(p); lx = lg(p);
    1928        1071 :   e = gel(y,2);
    1929        1071 :   pnew = vecpermute(p, v);
    1930        1071 :   enew = vecpermute(e, v);
    1931        1071 :   P = gen_0; c = 0;
    1932        3101 :   for (i=1; i<lx; i++)
    1933             :   {
    1934        2030 :     if (gequal(gel(pnew,i),P))
    1935          49 :       gel(e,c) = addii(gel(e,c),gel(enew,i));
    1936             :     else
    1937             :     {
    1938        1981 :       c++; P = gel(pnew,i);
    1939        1981 :       gel(p,c) = P;
    1940        1981 :       gel(e,c) = gel(enew,i);
    1941             :     }
    1942             :   }
    1943        1071 :   setlg(p, c+1);
    1944        1071 :   setlg(e, c+1); return y;
    1945             : }
    1946             : 
    1947             : 
    1948             : static long
    1949         175 : get_nz(GEN bnf, GEN ideal, GEN arch, long clhray)
    1950             : {
    1951             :   GEN arch2, mod;
    1952         175 :   long nz = 0, l = lg(arch), k, clhss;
    1953         175 :   if (typ(arch) == t_VECSMALL)
    1954          14 :     arch2 = indices_to_vec01(arch,nf_get_r1(bnf_get_nf(bnf)));
    1955             :   else
    1956         161 :     arch2 = leafcopy(arch);
    1957         175 :   mod = mkvec2(ideal, arch2);
    1958         455 :   for (k = 1; k < l; k++)
    1959             :   { /* FIXME: this is wasteful. Use the same algorithm as bnrconductor */
    1960         308 :     if (signe(gel(arch2,k)))
    1961             :     {
    1962          28 :       gel(arch2,k) = gen_0; clhss = itos(bnrclassno(bnf,mod));
    1963          28 :       gel(arch2,k) = gen_1;
    1964          28 :       if (clhss == clhray) return -1;
    1965             :     }
    1966         280 :     else nz++;
    1967             :   }
    1968         147 :   return nz;
    1969             : }
    1970             : 
    1971             : static GEN
    1972         420 : get_NR1D(long Nf, long clhray, long degk, long nz, GEN fadkabs, GEN idealrel)
    1973             : {
    1974             :   long n, R1;
    1975             :   GEN dlk;
    1976         420 :   if (nz < 0) mkvec3(gen_0,gen_0,gen_0); /*EMPTY*/
    1977         420 :   n  = clhray * degk;
    1978         420 :   R1 = clhray * nz;
    1979         420 :   dlk = factordivexact(factorpow(Z_factor(utoipos(Nf)),clhray), idealrel);
    1980             :   /* r2 odd, set dlk = -dlk */
    1981         420 :   if (((n-R1)&3)==2) dlk = factormul(to_famat_shallow(gen_m1,gen_1), dlk);
    1982         420 :   return mkvec3(utoipos(n),
    1983             :                 stoi(R1),
    1984             :                 factormul(dlk,factorpow(fadkabs,clhray)));
    1985             : }
    1986             : 
    1987             : /* t = [bid,U], h = #Cl(K) */
    1988             : static GEN
    1989         497 : get_discdata(GEN t, GEN h)
    1990             : {
    1991         497 :   GEN bid = gel(t,1), fa = gel(bid,3);
    1992         994 :   return mkvec3(mkmat2(gel(fa,1), vec_to_vecsmall(gel(fa,2))),
    1993         497 :                 (GEN)itou(get_classno(t, h)),
    1994             :                 bid_get_mod(bid));
    1995             : }
    1996             : typedef struct _disc_data {
    1997             :   long degk;
    1998             :   GEN bnf, fadk, idealrelinit, V;
    1999             : } disc_data;
    2000             : 
    2001             : static GEN
    2002         497 : get_discray(disc_data *D, GEN V, GEN z, long N)
    2003             : {
    2004         497 :   GEN idealrel = D->idealrelinit;
    2005         497 :   GEN mod = gel(z,3), Fa = gel(z,1);
    2006         497 :   GEN P = gel(Fa,1), E = gel(Fa,2);
    2007         497 :   long k, nz, clhray = z[2], lP = lg(P);
    2008         735 :   for (k=1; k<lP; k++)
    2009             :   {
    2010         574 :     GEN pr = gel(P,k), p = pr_get_p(pr);
    2011         574 :     long e, ep = E[k], f = pr_get_f(pr);
    2012         574 :     long S = 0, norm = N, Npr = upowuu(p[2],f), clhss;
    2013         826 :     for (e=1; e<=ep; e++)
    2014             :     {
    2015             :       GEN fad;
    2016         602 :       if (e < ep) { E[k] = ep-e; fad = Fa; }
    2017         476 :       else fad = factorsplice(Fa, k);
    2018         602 :       norm /= Npr;
    2019         602 :       clhss = (long)Lbnrclassno(gel(V,norm), fad);
    2020         602 :       if (e==1 && clhss==clhray) { E[k] = ep; return cgetg(1, t_VEC); }
    2021         266 :       if (clhss == 1) { S += ep-e+1; break; }
    2022         252 :       S += clhss;
    2023             :     }
    2024         238 :     E[k] = ep;
    2025         238 :     idealrel = factormul(idealrel, to_famat_shallow(p, utoi(f * S)));
    2026             :   }
    2027         161 :   nz = get_nz(D->bnf, gel(mod,1), gel(mod,2), clhray);
    2028         161 :   return get_NR1D(N, clhray, D->degk, nz, D->fadk, idealrel);
    2029             : }
    2030             : 
    2031             : /* Given a list of bids and attached unit log matrices, return the
    2032             :  * list of discrayabs. Only keep moduli which are conductors. */
    2033             : GEN
    2034          21 : discrayabslist(GEN bnf, GEN L)
    2035             : {
    2036          21 :   pari_sp av = avma;
    2037          21 :   long i, l = lg(L);
    2038             :   GEN nf, V, D, h;
    2039             :   disc_data ID;
    2040             : 
    2041          21 :   chk_listBU(L, "discrayabslist");
    2042          21 :   if (l == 1) return cgetg(1, t_VEC);
    2043          21 :   ID.bnf = bnf = checkbnf(bnf);
    2044          21 :   nf = bnf_get_nf(bnf);
    2045          21 :   h = bnf_get_no(bnf);
    2046          21 :   ID.degk = nf_get_degree(nf);
    2047          21 :   ID.fadk = absZ_factor(nf_get_disc(nf));
    2048          21 :   ID.idealrelinit = trivial_fact();
    2049          21 :   V = cgetg(l, t_VEC);
    2050          21 :   D = cgetg(l, t_VEC);
    2051         462 :   for (i = 1; i < l; i++)
    2052             :   {
    2053         441 :     GEN z = gel(L,i), v, d;
    2054         441 :     long j, lz = lg(z);
    2055         441 :     gel(V,i) = v = cgetg(lz,t_VEC);
    2056         441 :     gel(D,i) = d = cgetg(lz,t_VEC);
    2057         938 :     for (j=1; j<lz; j++) {
    2058         497 :       gel(d,j) = get_discdata(gel(z,j), h);
    2059         497 :       gel(v,j) = get_discray(&ID, D, gel(d,j), i);
    2060             :     }
    2061             :   }
    2062          21 :   return gerepilecopy(av, V);
    2063             : }
    2064             : 
    2065             : /* BIG VECTOR:
    2066             :  * Interface: a container v whose length is arbitrary (< 2^30), bigel(v,i)
    2067             :  * refers to the i-th component. It is an lvalue.
    2068             :  *
    2069             :  * Implementation: a vector v whose components have exactly 2^LGVINT entries
    2070             :  * but for the last one which is allowed to be shorter. v[i][j]
    2071             :  * (where j<=2^LGVINT) is understood as component number I = (i-1)*2^LGVINT+j
    2072             :  * in a unique huge vector V. */
    2073             : static const int SHLGVINT = 15;
    2074             : static const long LGVINT = 1L << 15;
    2075        3388 : INLINE long vext0(ulong i) { return ((i-1)>>SHLGVINT) + 1; }
    2076        3388 : INLINE long vext1(ulong i) { return i & (LGVINT-1); }
    2077             : #define bigel(v,i) gmael((v), vext0(i), vext1(i))
    2078             : 
    2079             : /* allocate an extended vector (t_VEC of t_VEC) for N _true_ components */
    2080             : static GEN
    2081          77 : bigcgetvec(long N)
    2082             : {
    2083          77 :   long i, nv = vext0(N);
    2084          77 :   GEN v = cgetg(nv+1,t_VEC);
    2085          77 :   for (i=1; i<nv; i++) gel(v,i) = cgetg(LGVINT+1,t_VEC);
    2086          77 :   gel(v,nv) = cgetg(vext1(N)+1,t_VEC); return v;
    2087             : }
    2088             : 
    2089             : /* a zsimp is [fa, cyc, U, v]
    2090             :  * fa: vecsmall factorisation,
    2091             :  * cyc: ZV (abelian group)
    2092             :  * U: ZM (base change)
    2093             :  * v: ZV (log of units) */
    2094             : static GEN
    2095          21 : zsimp(GEN bid, GEN embunit)
    2096             : {
    2097          21 :   GEN empty = cgetg(1, t_VECSMALL);
    2098          21 :   return mkvec4(mkmat2(empty,empty), bid_get_cyc(bid), gel(bid,5), embunit);
    2099             : }
    2100             : 
    2101             : /* fa a vecsmall factorization, append p^e */
    2102             : static GEN
    2103         308 : fasmall_append(GEN fa, long p, long e)
    2104             : {
    2105         308 :   GEN P = gel(fa,1), E = gel(fa,2);
    2106         308 :   retmkmat2(vecsmall_append(P,p), vecsmall_append(E,e));
    2107             : }
    2108             : 
    2109             : static GEN
    2110         308 : zsimpjoin(GEN b, GEN bid, GEN embunit, long prcode, long e)
    2111             : {
    2112             :   long l1, l2, nbgen;
    2113         308 :   pari_sp av = avma;
    2114             :   GEN fa, U, U1, U2, cyc1, cyc2, cyc;
    2115             : 
    2116         308 :   fa = gel(b,1);
    2117         308 :   U1 = gel(b,3);   cyc1 = gel(b,2);         l1 = lg(cyc1);
    2118         308 :   U2 = gel(bid,5); cyc2 = bid_get_cyc(bid); l2 = lg(cyc2);
    2119         308 :   nbgen = l1+l2-2;
    2120         308 :   if (nbgen)
    2121             :   {
    2122         287 :     cyc = ZV_snfall(shallowconcat(cyc1,cyc2), &U, NULL);
    2123         287 :     ZM_snfclean(cyc, U, NULL);
    2124         861 :     U = shallowconcat(
    2125         287 :       l1==1   ? zeromat(nbgen, lg(U1)-1): ZM_mul(vecslice(U, 1,   l1-1), U1),
    2126         287 :       l1>nbgen? zeromat(nbgen, lg(U2)-1): ZM_mul(vecslice(U, l1, nbgen), U2)
    2127             :     );
    2128             :   }
    2129             :   else
    2130             :   {
    2131          21 :     U = zeromat(0, lg(U1)+lg(U2)-2);
    2132          21 :     cyc = cgetg(1,t_VEC);
    2133             :   }
    2134         308 :   fa = fasmall_append(fa, prcode, e);
    2135         308 :   return gerepilecopy(av, mkvec4(fa, cyc, U, vconcat(gel(b,4),embunit)));
    2136             : }
    2137             : /* B a zsimp */
    2138             : static GEN
    2139          28 : bnrclassnointern(GEN B, GEN h)
    2140             : {
    2141          28 :   long lx = lg(B), j;
    2142          28 :   GEN L = cgetg(lx,t_VEC);
    2143          56 :   for (j=1; j<lx; j++)
    2144             :   {
    2145          28 :     GEN b = gel(B,j), qm = ZM_mul(gel(b,3),gel(b,4));
    2146          28 :     GEN m = ZM_det_triangular( ZM_hnfmodid(qm, gel(b,2)) );
    2147          28 :     gel(L,j) = mkvec2(gel(b,1), mkvecsmall( itou( mulii(h, m) ) ));
    2148             :   }
    2149          28 :   return L;
    2150             : }
    2151             : 
    2152             : static void
    2153        1344 : vecselect_p(GEN A, GEN B, GEN p, long init, long lB)
    2154             : {
    2155        1344 :   long i; setlg(B, lB);
    2156        1344 :   for (i=init; i<lB; i++) B[i] = A[p[i]];
    2157        1344 : }
    2158             : /* B := p . A = row selection according to permutation p. Treat only lower
    2159             :  * right corner init x init */
    2160             : static void
    2161         805 : rowselect_p(GEN A, GEN B, GEN p, long init)
    2162             : {
    2163         805 :   long i, lB = lg(A), lp = lg(p);
    2164         805 :   for (i=1; i<init; i++) setlg(B[i],lp);
    2165         805 :   for (   ; i<lB;   i++) vecselect_p(gel(A,i),gel(B,i),p,init,lp);
    2166         805 : }
    2167             : 
    2168             : static GEN
    2169         280 : bnrclassnointernarch(GEN B, GEN h, GEN matU)
    2170             : {
    2171             :   long lx, nc, k, kk, j, r1, jj, nba, nbarch;
    2172             :   GEN _2, b, qm, L, cyc, m, H, mm, rowsel;
    2173             : 
    2174         280 :   if (!matU) return bnrclassnointern(B,h);
    2175         252 :   lx = lg(B); if (lx == 1) return B;
    2176             : 
    2177         154 :   r1 = nbrows(matU); _2 = const_vec(r1, gen_2);
    2178         154 :   L = cgetg(lx,t_VEC); nbarch = 1L<<r1;
    2179         455 :   for (j=1; j<lx; j++)
    2180             :   {
    2181         301 :     b = gel(B,j); qm = ZM_mul(gel(b,3),gel(b,4));
    2182         301 :     cyc = gel(b,2); nc = lg(cyc)-1;
    2183             :     /* [ qm   cyc 0 ]
    2184             :      * [ matU  0  2 ] */
    2185         301 :     m = ZM_hnfmodid(vconcat(qm, matU), shallowconcat(cyc,_2));
    2186         301 :     mm = RgM_shallowcopy(m);
    2187         301 :     H = cgetg(nbarch+1,t_VECSMALL);
    2188         301 :     rowsel = cgetg(nc+r1+1,t_VECSMALL);
    2189        1106 :     for (k = 0; k < nbarch; k++)
    2190             :     {
    2191         805 :       nba = nc+1;
    2192        2149 :       for (kk=k,jj=1; jj<=r1; jj++,kk>>=1)
    2193        1344 :         if (kk&1) rowsel[nba++] = nc + jj;
    2194         805 :       setlg(rowsel, nba);
    2195         805 :       rowselect_p(m, mm, rowsel, nc+1);
    2196         805 :       H[k+1] = itou( mulii(h, ZM_det_triangular(ZM_hnf(mm))) );
    2197             :     }
    2198         301 :     gel(L,j) = mkvec2(gel(b,1), H);
    2199             :   }
    2200         154 :   return L;
    2201             : }
    2202             : 
    2203             : GEN
    2204          21 : decodemodule(GEN nf, GEN fa)
    2205             : {
    2206             :   long n, nn, k;
    2207          21 :   pari_sp av = avma;
    2208             :   GEN G, E, id, pr;
    2209             : 
    2210          21 :   nf = checknf(nf);
    2211          21 :   if (typ(fa)!=t_MAT || lg(fa)!=3)
    2212           0 :     pari_err_TYPE("decodemodule [not a factorization]", fa);
    2213          21 :   n = nf_get_degree(nf); nn = n*n; id = NULL;
    2214          21 :   G = gel(fa,1);
    2215          21 :   E = gel(fa,2);
    2216          35 :   for (k=1; k<lg(G); k++)
    2217             :   {
    2218          14 :     long code = G[k], p = code / nn, j = (code%n)+1;
    2219          14 :     GEN P = idealprimedec(nf, utoipos(p)), e = stoi(E[k]);
    2220          14 :     if (lg(P) <= j) pari_err_BUG("decodemodule [incorrect hash code]");
    2221          14 :     pr = gel(P,j);
    2222          14 :     id = id? idealmulpowprime(nf,id, pr,e)
    2223          14 :            : idealpow(nf, pr,e);
    2224             :   }
    2225          21 :   if (!id) { avma = av; return matid(n); }
    2226          14 :   return gerepileupto(av,id);
    2227             : }
    2228             : 
    2229             : /* List of ray class fields. Do all from scratch, bound < 2^30. No subgroups.
    2230             :  *
    2231             :  * Output: a "big vector" V (cf bigcgetvec). V[k] is a vector indexed by
    2232             :  * the ideals of norm k. Given such an ideal m, the component is as follows:
    2233             :  *
    2234             :  * + if arch = NULL, run through all possible archimedean parts; archs are
    2235             :  * ordered using inverse lexicographic order, [0,..,0], [1,0,..,0], [0,1,..,0],
    2236             :  * Component is [m,V] where V is a vector with 2^r1 entries, giving for each
    2237             :  * arch the triple [N,R1,D], with N, R1, D as in discrayabs; D is in factored
    2238             :  * form.
    2239             :  *
    2240             :  * + otherwise [m,N,R1,D] */
    2241             : GEN
    2242          21 : discrayabslistarch(GEN bnf, GEN arch, ulong bound)
    2243             : {
    2244          21 :   int allarch = (arch==NULL), flbou = 0;
    2245             :   long degk, j, k, l, nba, nbarch, r1, c;
    2246          21 :   pari_sp av0 = avma,  av,  av1;
    2247             :   GEN nf, p, Z, fa, bidp, matarchunit, Disc, U, sgnU, EMPTY, empty;
    2248             :   GEN res, embunit, h, Ray, discall, idealrel, idealrelinit, fadkabs, BOUND;
    2249             :   ulong i, ii, sqbou;
    2250             :   forprime_t S;
    2251             : 
    2252          21 :   if (bound == 0)
    2253           0 :     pari_err_DOMAIN("discrayabslistarch","bound","==",gen_0,utoi(bound));
    2254          21 :   res = discall = NULL; /* -Wall */
    2255             : 
    2256          21 :   bnf = checkbnf(bnf);
    2257          21 :   nf = bnf_get_nf(bnf); r1 = nf_get_r1(nf);
    2258          21 :   degk = nf_get_degree(nf);
    2259          21 :   fadkabs = absZ_factor(nf_get_disc(nf));
    2260          21 :   h = bnf_get_no(bnf);
    2261          21 :   U = init_units(bnf);
    2262          21 :   sgnU = nfsign_units(bnf, NULL, 1);
    2263             : 
    2264          21 :   if (allarch) arch = const_vec(r1, gen_1);
    2265          21 :   bidp = Idealstar(nf, mkvec2(gen_1, arch), nf_INIT);
    2266          21 :   if (allarch) {
    2267          14 :     matarchunit = zlog_units(nf, U, sgnU, bidp);
    2268          14 :     bidp = Idealstar(nf,matid(degk), nf_INIT);
    2269          14 :     if (r1>15) pari_err_IMPL("r1>15 in discrayabslistarch");
    2270          14 :     nba = r1;
    2271             :   } else {
    2272           7 :     matarchunit = NULL;
    2273           7 :     for (nba=0,k=1; k<=r1; k++) if (signe(gel(arch,k))) nba++;
    2274             :   }
    2275             : 
    2276          21 :   empty = cgetg(1,t_VEC);
    2277             :   /* what follows was rewritten from Ideallist */
    2278          21 :   BOUND = utoipos(bound);
    2279          21 :   p = cgetipos(3);
    2280          21 :   u_forprime_init(&S, 2, bound);
    2281          21 :   av = avma;
    2282          21 :   sqbou = (ulong)sqrt((double)bound) + 1;
    2283          21 :   Z = bigcgetvec(bound);
    2284          21 :   for (i=2; i<=bound; i++) bigel(Z,i) = empty;
    2285          21 :   embunit = zlog_units(nf, U, sgnU, bidp);
    2286          21 :   bigel(Z,1) = mkvec(zsimp(bidp,embunit));
    2287          21 :   if (DEBUGLEVEL>1) err_printf("Starting zidealstarunits computations\n");
    2288             :   /* The goal is to compute Ray (lists of bnrclassno). Z contains "zsimps",
    2289             :    * simplified bid, from which bnrclassno is easy to compute.
    2290             :    * Once p > sqbou, delete Z[i] for i > sqbou and compute directly Ray */
    2291          21 :   Ray = Z;
    2292         140 :   while ((p[2] = u_forprime_next(&S)))
    2293             :   {
    2294          98 :     if (!flbou && uel(p,2) > sqbou)
    2295             :     {
    2296             :       GEN z;
    2297          14 :       flbou = 1;
    2298          14 :       if (DEBUGLEVEL>1) err_printf("\nStarting bnrclassno computations\n");
    2299          14 :       Z = gerepilecopy(av,Z); av1 = avma;
    2300          14 :       Ray = bigcgetvec(bound);
    2301         224 :       for (i=1; i<=bound; i++)
    2302         210 :         bigel(Ray,i) = bnrclassnointernarch(bigel(Z,i),h,matarchunit);
    2303          14 :       Ray = gerepilecopy(av1,Ray);
    2304          14 :       z = bigcgetvec(sqbou);
    2305          14 :       for (i=1; i<=sqbou; i++) bigel(z,i) = bigel(Z,i);
    2306          14 :       Z = z;
    2307             :     }
    2308          98 :     fa = idealprimedec_limit_norm(nf,p,BOUND);
    2309         217 :     for (j=1; j<lg(fa); j++)
    2310             :     {
    2311         119 :       GEN pr = gel(fa,j);
    2312         119 :       long prcode, f = pr_get_f(pr);
    2313         119 :       ulong q, Q = upowuu(p[2], f);
    2314             : 
    2315             :       /* p, f-1, j-1 as a single integer in "base degk" (f,j <= degk)*/
    2316         119 :       prcode = (p[2]*degk + f-1)*degk + j-1;
    2317         119 :       q = Q;
    2318         189 :       for (l=1;; l++) /* Q <= bound */
    2319             :       {
    2320             :         ulong iQ;
    2321         189 :         bidp = Idealstarprk(nf, pr, l, nf_INIT);
    2322         189 :         embunit = zlog_units_noarch(nf, U, bidp);
    2323         637 :         for (iQ = Q, i = 1; iQ <= bound; iQ += Q, i++)
    2324             :         {
    2325         448 :           GEN pz, p2, p1 = bigel(Z,i);
    2326         448 :           long lz = lg(p1);
    2327         448 :           if (lz == 1) continue;
    2328             : 
    2329         322 :           p2 = cgetg(lz,t_VEC); c = 0;
    2330         630 :           for (k=1; k<lz; k++)
    2331             :           {
    2332         385 :             GEN z = gel(p1,k), v = gmael(z,1,1); /* primes in zsimp's fact. */
    2333         385 :             long lv = lg(v);
    2334             :             /* If z has a power of pr in its modulus, skip it */
    2335         385 :             if (i != 1 && lv > 1 && v[lv-1] == prcode) break;
    2336         308 :             gel(p2,++c) = zsimpjoin(z,bidp,embunit,prcode,l);
    2337             :           }
    2338             : 
    2339         322 :           setlg(p2, c+1);
    2340         322 :           pz = bigel(Ray,iQ);
    2341         322 :           if (flbou) p2 = bnrclassnointernarch(p2,h,matarchunit);
    2342         322 :           if (lg(pz) > 1) p2 = shallowconcat(pz,p2);
    2343         322 :           bigel(Ray,iQ) = p2;
    2344             :         }
    2345         189 :         Q = itou_or_0( muluu(Q, q) );
    2346         189 :         if (!Q || Q > bound) break;
    2347             : 
    2348          70 :       }
    2349             :     }
    2350          98 :     if (gc_needed(av,1))
    2351             :     {
    2352           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"[1]: discrayabslistarch");
    2353           0 :       gerepileall(av, flbou? 2: 1, &Z, &Ray);
    2354             :     }
    2355             :   }
    2356          21 :   if (!flbou) /* occurs iff bound = 1,2,4 */
    2357             :   {
    2358           7 :     if (DEBUGLEVEL>1) err_printf("\nStarting bnrclassno computations\n");
    2359           7 :     Ray = bigcgetvec(bound);
    2360          35 :     for (i=1; i<=bound; i++)
    2361          28 :       bigel(Ray,i) = bnrclassnointernarch(bigel(Z,i),h,matarchunit);
    2362             :   }
    2363          21 :   Ray = gerepilecopy(av, Ray);
    2364             : 
    2365          21 :   if (DEBUGLEVEL>1) err_printf("Starting discrayabs computations\n");
    2366          21 :   if (allarch) nbarch = 1L<<r1;
    2367             :   else
    2368             :   {
    2369           7 :     nbarch = 1;
    2370           7 :     discall = cgetg(2,t_VEC);
    2371             :   }
    2372          21 :   EMPTY = mkvec3(gen_0,gen_0,gen_0);
    2373          21 :   idealrelinit = trivial_fact();
    2374          21 :   av1 = avma;
    2375          21 :   Disc = bigcgetvec(bound);
    2376          21 :   for (i=1; i<=bound; i++) bigel(Disc,i) = empty;
    2377         259 :   for (ii=1; ii<=bound; ii++)
    2378             :   {
    2379             :     GEN sous, sousdisc;
    2380             :     long ls;
    2381         238 :     i = ii;
    2382         238 :     sous = bigel(Ray,i);
    2383         238 :     ls = lg(sous); bigel(Disc,ii) = sousdisc = cgetg(ls,t_VEC);
    2384         567 :     for (j=1; j<ls; j++)
    2385             :     {
    2386         329 :       GEN b = gel(sous,j), clhrayall = gel(b,2), Fa = gel(b,1);
    2387         329 :       GEN P = gel(Fa,1), E = gel(Fa,2);
    2388         329 :       long lP = lg(P), karch;
    2389             : 
    2390         329 :       if (allarch) discall = cgetg(nbarch+1,t_VEC);
    2391        1162 :       for (karch=0; karch<nbarch; karch++)
    2392             :       {
    2393         833 :         long nz, clhray = clhrayall[karch+1];
    2394         833 :         if (allarch)
    2395             :         {
    2396             :           long ka, k2;
    2397         805 :           nba = 0;
    2398        2149 :           for (ka=karch,k=1; k<=r1; k++,ka>>=1)
    2399        1344 :             if (ka & 1) nba++;
    2400        1743 :           for (k2=1,k=1; k<=r1; k++,k2<<=1)
    2401        1232 :             if (karch&k2 && clhrayall[karch-k2+1] == clhray)
    2402         294 :               { res = EMPTY; goto STORE; }
    2403             :         }
    2404         539 :         idealrel = idealrelinit;
    2405         861 :         for (k=1; k<lP; k++) /* cf get_discray */
    2406             :         {
    2407         602 :           long e, ep = E[k], pf = P[k] / degk, f = (pf%degk) + 1, S = 0;
    2408         602 :           ulong normi = i, Npr;
    2409         602 :           p = utoipos(pf / degk);
    2410         602 :           Npr = upowuu(p[2],f);
    2411         980 :           for (e=1; e<=ep; e++)
    2412             :           {
    2413             :             long clhss;
    2414             :             GEN fad;
    2415         665 :             if (e < ep) { E[k] = ep-e; fad = Fa; }
    2416         539 :             else fad = factorsplice(Fa, k);
    2417         665 :             normi /= Npr;
    2418         665 :             clhss = Lbnrclassno(bigel(Ray,normi),fad)[karch+1];
    2419         665 :             if (e==1 && clhss==clhray) { E[k] = ep; res = EMPTY; goto STORE; }
    2420         385 :             if (clhss == 1) { S += ep-e+1; break; }
    2421         378 :             S += clhss;
    2422             :           }
    2423         322 :           E[k] = ep;
    2424         322 :           idealrel = factormul(idealrel, to_famat_shallow(p, utoi(f * S)));
    2425             :         }
    2426         259 :         if (!allarch && nba)
    2427          14 :           nz = get_nz(bnf, decodemodule(nf,Fa), arch, clhray);
    2428             :         else
    2429         245 :           nz = r1 - nba;
    2430         259 :         res = get_NR1D(i, clhray, degk, nz, fadkabs, idealrel);
    2431         833 : STORE:  gel(discall,karch+1) = res;
    2432             :       }
    2433         329 :       res = allarch? mkvec2(Fa, discall)
    2434         329 :                    : mkvec4(Fa, gel(res,1), gel(res,2), gel(res,3));
    2435         329 :       gel(sousdisc,j) = res;
    2436         329 :       if (gc_needed(av1,1))
    2437             :       {
    2438             :         long jj;
    2439           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"[2]: discrayabslistarch");
    2440           0 :         for (jj=j+1; jj<ls; jj++) gel(sousdisc,jj) = gen_0; /* dummy */
    2441           0 :         Disc = gerepilecopy(av1, Disc);
    2442           0 :         sousdisc = bigel(Disc,ii);
    2443             :       }
    2444             :     }
    2445             :   }
    2446          21 :   return gerepilecopy(av0, Disc);
    2447             : }
    2448             : GEN
    2449           0 : discrayabslistlong(GEN bnf, ulong bound) {
    2450           0 :   GEN nf = checknf(bnf);
    2451           0 :   long r1 = nf_get_r1(nf);
    2452           0 :   return discrayabslistarch(bnf,zerovec(r1),bound);
    2453             : }
    2454             : 
    2455             : int
    2456        1099 : subgroup_conductor_ok(GEN H, GEN L)
    2457             : { /* test conductor */
    2458        1099 :   long i, l = lg(L);
    2459        3108 :   for (i = 1; i < l; i++)
    2460        2415 :     if ( hnf_solve(H, gel(L,i)) ) return 0;
    2461         693 :   return 1;
    2462             : }
    2463             : static GEN
    2464         476 : conductor_elts(GEN bnr)
    2465             : {
    2466         476 :   GEN e, L, nf = bnf_get_nf( bnr_get_bnf(bnr) );
    2467             :   long le, la, i, k;
    2468             :   zlog_S S;
    2469             : 
    2470         476 :   init_zlog_bid(&S, bnr_get_bid(bnr));
    2471         476 :   e = S.e; le = lg(e); la = lg(S.archp);
    2472         476 :   L = cgetg(le + la - 1, t_VEC);
    2473         476 :   i = 1;
    2474        1029 :   for (k = 1; k < le; k++)
    2475         553 :     gel(L,i++) = bnr_log_gen_pr(bnr, &S, nf, itos(gel(e,k)), k);
    2476        1064 :   for (k = 1; k < la; k++)
    2477         588 :     gel(L,i++) = bnr_log_gen_arch(bnr, &S, k);
    2478         476 :   return L;
    2479             : }
    2480             : 
    2481             : /* Let C a congruence group in bnr, compute its subgroups whose index is
    2482             :  * described by bound (see subgrouplist) as subgroups of Clk(bnr).
    2483             :  * Restrict to subgroups having the same conductor as bnr */
    2484             : GEN
    2485         448 : subgrouplist_cond_sub(GEN bnr, GEN C, GEN bound)
    2486             : {
    2487         448 :   pari_sp av = avma;
    2488             :   long l, i, j;
    2489         448 :   GEN D, Mr, U, T, subgrp, L, cyc = bnr_get_cyc(bnr);
    2490             : 
    2491         448 :   Mr = diagonal_shallow(cyc);
    2492         448 :   D = ZM_snfall_i(hnf_solve(C, Mr), &U, NULL, 1);
    2493         448 :   T = ZM_mul(C, RgM_inv(U));
    2494         448 :   L = conductor_elts(bnr);
    2495         448 :   subgrp  = subgrouplist(D, bound);
    2496         448 :   l = lg(subgrp);
    2497         952 :   for (i = j = 1; i < l; i++)
    2498             :   {
    2499         504 :     GEN H = ZM_hnfmodid(ZM_mul(T, gel(subgrp,i)), cyc);
    2500         504 :     if (subgroup_conductor_ok(H, L)) gel(subgrp, j++) = H;
    2501             :   }
    2502         448 :   setlg(subgrp, j);
    2503         448 :   return gerepilecopy(av, subgrp);
    2504             : }
    2505             : 
    2506             : static GEN
    2507          28 : subgroupcond(GEN bnr, GEN indexbound)
    2508             : {
    2509          28 :   pari_sp av = avma;
    2510          28 :   GEN li = subgroupcondlist(bnr_get_cyc(bnr), indexbound, conductor_elts(bnr));
    2511          28 :   if (indexbound && typ(indexbound) != t_VEC)
    2512             :   { /* sort by increasing index if not single value */
    2513          14 :     long i, l = lg(li);
    2514          14 :     GEN D = cgetg(l,t_VEC);
    2515          14 :     for (i=1; i<l; i++) gel(D,i) = ZM_det_triangular(gel(li,i));
    2516          14 :     li = vecreverse( vecpermute(li, indexsort(D)) );
    2517             :   }
    2518          28 :   return gerepilecopy(av,li);
    2519             : }
    2520             : 
    2521             : GEN
    2522          70 : subgrouplist0(GEN bnr, GEN indexbound, long all)
    2523             : {
    2524          70 :   if (typ(bnr)!=t_VEC) pari_err_TYPE("subgrouplist",bnr);
    2525          63 :   if (lg(bnr)!=1 && typ(gel(bnr,1))!=t_INT)
    2526             :   {
    2527          35 :     checkbnr(bnr);
    2528          35 :     if (!all) return subgroupcond(bnr,indexbound);
    2529           7 :     bnr = bnr_get_cyc(bnr);
    2530             :   }
    2531          35 :   return subgrouplist(bnr,indexbound);
    2532             : }
    2533             : 
    2534             : GEN
    2535          42 : bnrdisclist0(GEN bnf, GEN L, GEN arch)
    2536             : {
    2537          42 :   if (typ(L)!=t_INT) return discrayabslist(bnf,L);
    2538          21 :   return discrayabslistarch(bnf,arch,itos(L));
    2539             : }
    2540             : 
    2541             : /****************************************************************************/
    2542             : /*                                Galois action on a BNR                    */
    2543             : /****************************************************************************/
    2544             : 
    2545             : GEN
    2546         462 : bnrautmatrix(GEN bnr, GEN aut)
    2547             : {
    2548         462 :   pari_sp av=avma;
    2549             :   GEN gen, mat, nf;
    2550             :   long i, l;
    2551         462 :   nf = bnr_get_nf(bnr);
    2552         462 :   gen = bnr_get_gen(bnr); l = lg(gen);
    2553         462 :   aut = algtobasis(nf, aut);
    2554         462 :   mat = cgetg(l,t_MAT);
    2555        2310 :   for (i=1; i<l; i++)
    2556        1848 :     gel(mat, i) = bnrisprincipal(bnr,galoisapply(nf,aut,gel(gen,i)),0);
    2557         462 :   return gerepilecopy(av, mat);
    2558             : }
    2559             : 
    2560             : GEN
    2561         238 : bnrgaloismatrix(GEN bnr, GEN aut)
    2562             : {
    2563         238 :   checkbnr(bnr);
    2564         238 :   switch (typ(aut))
    2565             :   {
    2566             :     case t_POL:
    2567             :     case t_COL:
    2568           0 :       return bnrautmatrix(bnr, aut);
    2569             :     case t_VEC:
    2570             :     {
    2571         238 :       long i, l = lg(aut);
    2572             :       GEN V;
    2573         238 :       if (l==9 && typ(gal_get_gen(aut))==t_VEC)
    2574             :       {
    2575           7 :         pari_sp av = avma;
    2576           7 :         V = galoispermtopol(aut, gal_get_gen(aut));
    2577           7 :         return gerepileupto(av, bnrgaloismatrix(bnr, V));
    2578             :       }
    2579         231 :       V = cgetg(l, t_VEC);
    2580         693 :       for(i=1; i<l; i++)
    2581         462 :         gel(V,i) = bnrautmatrix(bnr, gel(aut,i));
    2582         231 :       return V;
    2583             :     }
    2584             :     default:
    2585           0 :       pari_err_TYPE("bnrgaloismatrix", aut);
    2586           0 :       return NULL; /*NOT REACHED*/
    2587             :   }
    2588             : }
    2589             : 
    2590             : GEN
    2591        1008 : bnrgaloisapply(GEN bnr, GEN mat, GEN x)
    2592             : {
    2593        1008 :   pari_sp av=avma;
    2594             :   GEN cyc;
    2595        1008 :   checkbnr(bnr);
    2596        1008 :   cyc = bnr_get_cyc(bnr);
    2597        1008 :   if (typ(mat)!=t_MAT || !RgM_is_ZM(mat))
    2598           0 :     pari_err_TYPE("bnrgaloisapply",mat);
    2599        1008 :   if (typ(x)!=t_MAT || !RgM_is_ZM(x))
    2600           0 :     pari_err_TYPE("bnrgaloisapply",x);
    2601        1008 :   return gerepileupto(av, ZM_hnfmodid(ZM_mul(mat, x), cyc));
    2602             : }
    2603             : 
    2604             : static GEN
    2605         448 : check_bnrgal(GEN bnr, GEN M)
    2606             : {
    2607         448 :   checkbnr(bnr);
    2608         448 :   if (typ(M)==t_MAT)
    2609           0 :     return mkvec(M);
    2610         448 :   else if (typ(M)==t_VEC && lg(M)==9 && typ(gal_get_gen(M))==t_VEC)
    2611             :   {
    2612         224 :     pari_sp av = avma;
    2613         224 :     GEN V = galoispermtopol(M, gal_get_gen(M));
    2614         224 :     return gerepileupto(av, bnrgaloismatrix(bnr, V));
    2615             :   }
    2616         224 :   else if (!is_vec_t(typ(M)))
    2617           0 :     pari_err_TYPE("bnrisgalois",M);
    2618         224 :   return M;
    2619             : }
    2620             : 
    2621             : long
    2622         448 : bnrisgalois(GEN bnr, GEN M, GEN H)
    2623             : {
    2624         448 :   pari_sp av = avma;
    2625             :   long i, l;
    2626         448 :   if (typ(H)!=t_MAT || !RgM_is_ZM(H))
    2627           0 :     pari_err_TYPE("bnrisgalois",H);
    2628         448 :   M = check_bnrgal(bnr, M); l = lg(M);
    2629         616 :   for (i=1; i<l; i++)
    2630             :   {
    2631         560 :     long res = ZM_equal(bnrgaloisapply(bnr,gel(M,i), H), H);
    2632         560 :     if (!res) { avma = av; return 0; }
    2633             :   }
    2634          56 :   avma = av;
    2635          56 :   return 1;
    2636             : }

Generated by: LCOV version 1.11