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 19199-979caa2) Lines: 1388 1484 93.5 %
Date: 2016-07-24 07:10:30 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        7770 :       g = gel(gen,i);
      98        7770 :       if (typ(g) != t_MAT)
      99             :       {
     100        3353 :         if (z)
     101         693 :           gel(z,2) = famat_mul(gel(z,2), to_famat_shallow(g, e));
     102             :         else
     103        2660 :           z = mkvec2(NULL, to_famat_shallow(g, e));
     104        3353 :         continue;
     105             :       }
     106             : 
     107        4417 :       gel(G,1) = g;
     108        4417 :       g = idealpowred(nf,G,e);
     109        4417 :       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        6685 : too_big(GEN nf, GEN bet)
     119             : {
     120        6685 :   GEN x = nfnorm(nf,bet);
     121        6685 :   switch (typ(x))
     122             :   {
     123         805 :     case t_INT: return absi_cmp(x, gen_1);
     124        5880 :     case t_FRAC: return absi_cmp(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        5978 : idealmoddivisor_aux(GEN nf, GEN x, GEN divisor, GEN sarch)
     133             : {
     134        5978 :   pari_sp av = avma;
     135        5978 :   GEN a,A,D,G, f = gel(divisor,1);
     136             : 
     137        5978 :   if ( is_pm1(gcoeff(f,1,1)) ) /* f = 1 */
     138             :   {
     139          84 :     G = idealred_elt(nf, x);
     140          84 :     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        5894 :     G = idealaddtoone_i(nf, x, f);
     146        5894 :     D = idealaddtoone_i(nf, idealdiv(nf,G,x), f);
     147             :   }
     148        5978 :   A = nfdiv(nf,D,G);
     149        5978 :   if (too_big(nf,A) > 0) { avma = av; return x; }
     150        5642 :   a = set_sign_mod_divisor(nf, NULL, A, divisor, sarch);
     151        5642 :   if (a != A && too_big(nf,A) > 0) { avma = av; return x; }
     152        5642 :   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        6601 : fast_val(GEN nf,GEN L0,GEN cx,GEN pr)
     166             : {
     167        6601 :   pari_sp av = avma;
     168        6601 :   long v = typ(L0) == t_INT? 0: ZC_nfval(nf,L0,pr);
     169        6601 :   if (cx)
     170             :   {
     171        5691 :     long w = Q_pval(cx, pr_get_p(pr));
     172        5691 :     if (w) v += w * pr_get_e(pr);
     173             :   }
     174        6601 :   avma = av; return v;
     175             : }
     176             : 
     177             : /* x coprime to fZ, return y = x mod fZ, y integral */
     178             : static GEN
     179         980 : make_integral_Z(GEN x, GEN fZ)
     180             : {
     181         980 :   GEN d, y = Q_remove_denom(x, &d);
     182         980 :   if (d) y = FpC_Fp_mul(y, Fp_inv(d, fZ), fZ);
     183         980 :   return y;
     184             : }
     185             : 
     186             : /* p pi^(-1) mod f */
     187             : static GEN
     188        1309 : get_pinvpi(GEN nf, GEN fZ, GEN p, GEN pi, GEN *v)
     189             : {
     190        1309 :   if (!*v) {
     191         980 :     GEN invpi = nfinv(nf, pi);
     192         980 :     *v = make_integral_Z(RgC_Rg_mul(invpi, p), mulii(p, fZ));
     193             :   }
     194        1309 :   return *v;
     195             : }
     196             : /* p pi^(-1) mod f */
     197             : static GEN
     198        2583 : get_pi(GEN F, GEN pr, GEN *v)
     199             : {
     200        2583 :   if (!*v) *v = unif_mod_fZ(pr, F);
     201        2583 :   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        2660 :       gel(basecl,i) = famat_to_nf_moddivisor(nf, L, e, bid);
     250        2660 :       continue;
     251             :     }
     252        4298 :     if (lg(A) == 1)
     253             :     {
     254        2534 :       gel(basecl,i) = I;
     255        2534 :       continue;
     256             :     }
     257             : 
     258             :     /* compute mulI so that mulI * I coprime to f
     259             :      * FIXME: use idealcoprime ??? (Less efficient. Fix idealcoprime!) */
     260        1764 :     dmulI = mulI = NULL;
     261        4018 :     for (j=1; j<lp; j++)
     262             :     {
     263        2254 :       pr = gel(listpr,j);
     264        2254 :       v  = idealval(nf, I, pr);
     265        2254 :       if (!v) continue;
     266         980 :       p  = pr_get_p(pr);
     267         980 :       pi = get_pi(F, pr, &gel(vecpi,j));
     268         980 :       pinvpi = get_pinvpi(nf, fZ, p, pi, &gel(vecpinvpi,j));
     269         980 :       t = nfpow_u(nf, pinvpi, (ulong)v);
     270         980 :       mulI = mulI? nfmuli(nf, mulI, t): t;
     271         980 :       t = powiu(p, v);
     272         980 :       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        1764 :     la = lg(e); newL = cgetg(la, t_VEC);
     278        5432 :     for (k=1; k<la; k++)
     279             :     {
     280        3668 :       GEN cx, LL = nf_to_scalar_or_basis(nf, gel(L,k));
     281        3668 :       GEN L0 = Q_primitive_part(LL, &cx); /* LL = L0*cx (faster nfval) */
     282       10269 :       for (j=1; j<lp; j++)
     283             :       {
     284        6601 :         pr = gel(listpr,j);
     285        6601 :         v  = fast_val(nf, L0,cx, pr); /* = val_pr(LL) */
     286        6601 :         if (!v) continue;
     287        1603 :         p  = pr_get_p(pr);
     288        1603 :         pi = get_pi(F, pr, &gel(vecpi,j));
     289        1603 :         if (v > 0)
     290             :         {
     291         329 :           pinvpi = get_pinvpi(nf, fZ, p, pi, &gel(vecpinvpi,j));
     292         329 :           t = nfpow_u(nf,pinvpi, (ulong)v);
     293         329 :           LL = nfmul(nf, LL, t);
     294         329 :           LL = RgC_Rg_div(LL, powiu(p, v));
     295             :         }
     296             :         else
     297             :         {
     298        1274 :           t = nfpow_u(nf,pi,(ulong)(-v));
     299        1274 :           LL = nfmul(nf, LL, t);
     300             :         }
     301             :       }
     302        3668 :       LL = make_integral(nf,LL,f,listpr);
     303        3668 :       gel(newL,k) = typ(LL) == t_INT? LL: FpC_red(LL, fZ);
     304             :     }
     305             : 
     306        1764 :     av = avma;
     307             :     /* G in nf, = L^e mod f */
     308        1764 :     G = famat_to_nf_modideal_coprime(nf, newL, e, f, EX);
     309        1764 :     if (mulI)
     310             :     {
     311         931 :       G = nfmuli(nf, G, mulI);
     312         931 :       G = ZC_hnfrem(G, ZM_Z_mul(f, dmulI));
     313             :     }
     314        1764 :     G = set_sign_mod_divisor(nf,A,G,module,sarch);
     315        1764 :     I = idealmul(nf,I,G);
     316        1764 :     if (dmulI) I = ZM_Z_divexact(I, dmulI);
     317             :     /* more or less useless, but cheap at this point */
     318        1764 :     I = idealmoddivisor_aux(nf,I,module,sarch);
     319        1764 :     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         959 :       GEN F = to_famat_shallow(gel(El,j), gel(cyc,j));
     443         959 :       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      184364 : bnrisprincipal(GEN bnr, GEN x, long flag)
     551             : {
     552      184364 :   pari_sp av = avma;
     553             :   GEN bnf, nf, bid, U, El, ep, L, idep, ex, cycray, cycbid, alpha;
     554             : 
     555      184364 :   checkbnr(bnr);
     556      184364 :   cycray = bnr_get_cyc(bnr);
     557      184364 :   if (lg(cycray) == 1 && !(flag & nf_GEN)) return cgetg(1,t_COL);
     558             : 
     559      184042 :   bnf = bnr_get_bnf(bnr); nf = bnf_get_nf(bnf);
     560      184042 :   bid = bnr_get_bid(bnr);
     561      184042 :   cycbid = bid_get_cyc(bid);
     562      184042 :   El  = gel(bnr,3);
     563      184042 :   U   = gel(bnr,4);
     564             : 
     565      184042 :   if (typ(x) == t_VEC && lg(x) == 3)
     566           0 :   { idep = gel(x,2); x = gel(x,1); }  /* precomputed */
     567             :   else
     568      184042 :     idep = bnfisprincipal0(bnf, x, nf_FORCE|nf_GENMAT);
     569      184042 :   ep  = gel(idep,1);
     570      184042 :   if (lg(cycbid) > 1)
     571             :   {
     572      181725 :     GEN beta = gel(idep,2);
     573      181725 :     long i, j = lg(ep);
     574      312594 :     for (i=1; i<j; i++) /* modify beta as if gen -> El.gen (coprime to bid) */
     575      130869 :       if (typ(gel(El,i)) != t_INT && signe(gel(ep,i))) /* <==> != 1 */
     576       37689 :         beta = famat_mul(to_famat_shallow(gel(El,i), negi(gel(ep,i))), beta);
     577      181725 :     ep = shallowconcat(ep, ideallog(nf,beta,bid));
     578             :   }
     579      184042 :   ex = vecmodii(ZM_ZC_mul(U, ep), cycray);
     580      184042 :   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(absi_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 */
    1036             : static void
    1037        1540 : primecertify(GEN bnf, GEN beta, ulong p, GEN bad)
    1038             : {
    1039             :   long i, j, nbcol, lb, nbqq, ra;
    1040             :   GEN nf,mat,gq,LQ,newcol,g,ord,modpr;
    1041             :   ulong q;
    1042             : 
    1043        1540 :   ord = NULL; /* gcc -Wall */
    1044        1540 :   nbcol = 0; nf = bnf_get_nf(bnf);
    1045        1540 :   lb = lg(beta)-1; mat = cgetg(1,t_MAT); q = 1UL;
    1046             :   for(;;)
    1047             :   {
    1048        8106 :     q += 2*p;
    1049        8106 :     if (!umodiu(bad,q) || !uisprime(q)) continue;
    1050             : 
    1051        2541 :     gq = utoipos(q);
    1052        2541 :     LQ = idealprimedec_limit_f(bnf,gq,1); nbqq = lg(LQ)-1;
    1053        2541 :     g = NULL;
    1054        2982 :     for (i=1; i<=nbqq; i++)
    1055             :     {
    1056        1981 :       GEN mat1, Q = gel(LQ,i); /* degree 1 */
    1057        1981 :       if (!g)
    1058             :       {
    1059        1778 :         g = gener_Flxq(pol_x(0), q, &ord);
    1060        1778 :         g = utoipos(g[2]); /* from Flx of degree 0 to t_INT */
    1061             :       }
    1062        1981 :       modpr = zkmodprinit(nf, Q);
    1063        1981 :       newcol = cgetg(lb+1,t_COL);
    1064        5425 :       for (j=1; j<=lb; j++)
    1065             :       {
    1066        3444 :         GEN t = to_Fp_simple(nf, gel(beta,j), modpr);
    1067        3444 :         gel(newcol,j) = Fp_log(t,g,ord,gq);
    1068             :       }
    1069        1981 :       if (DEBUGLEVEL>3)
    1070             :       {
    1071           0 :         if (i==1) err_printf("       generator of (Zk/Q)^*: %Ps\n", g);
    1072           0 :         err_printf("       prime ideal Q: %Ps\n",Q);
    1073           0 :         err_printf("       column #%ld of the matrix log(b_j/Q): %Ps\n",
    1074             :                    nbcol, newcol);
    1075             :       }
    1076        1981 :       mat1 = shallowconcat(mat,newcol); ra = ZM_rank(mat1);
    1077        1981 :       if (ra==nbcol) continue;
    1078             : 
    1079        1967 :       if (DEBUGLEVEL>2) err_printf("       new rank: %ld\n",ra);
    1080        3507 :       if (++nbcol == lb) return;
    1081         427 :       mat = mat1;
    1082             :     }
    1083        6566 :   }
    1084             : }
    1085             : 
    1086             : struct check_pr {
    1087             :   long w; /* #mu(K) */
    1088             :   GEN mu; /* generator of mu(K) */
    1089             :   GEN fu;
    1090             :   GEN cyc;
    1091             :   GEN cycgen;
    1092             :   GEN bad; /* p | bad <--> p | some element occurring in cycgen */
    1093             : };
    1094             : 
    1095             : static void
    1096        1540 : check_prime(ulong p, GEN bnf, struct check_pr *S)
    1097             : {
    1098        1540 :   pari_sp av = avma;
    1099        1540 :   long i,b, lc = lg(S->cyc), lf = lg(S->fu);
    1100        1540 :   GEN beta = cgetg(lf+lc, t_VEC);
    1101             : 
    1102        1540 :   if (DEBUGLEVEL>1) err_printf("  *** testing p = %lu\n",p);
    1103        1603 :   for (b=1; b<lc; b++)
    1104             :   {
    1105        1323 :     if (umodiu(gel(S->cyc,b), p)) break; /* p \nmid cyc[b] */
    1106          63 :     if (b==1 && DEBUGLEVEL>2) err_printf("     p divides h(K)\n");
    1107          63 :     gel(beta,b) = gel(S->cycgen,b);
    1108             :   }
    1109        1540 :   if (S->w % p == 0)
    1110             :   {
    1111          35 :     if (DEBUGLEVEL>2) err_printf("     p divides w(K)\n");
    1112          35 :     gel(beta,b++) = S->mu;
    1113             :   }
    1114        1540 :   for (i=1; i<lf; i++) gel(beta,b++) = gel(S->fu,i);
    1115        1540 :   setlg(beta, b); /* beta = [cycgen[i] if p|cyc[i], tu if p|w, fu] */
    1116        1540 :   if (DEBUGLEVEL>3) {err_printf("     Beta list = %Ps\n",beta); err_flush();}
    1117        1540 :   primecertify(bnf,beta,p,S->bad); avma = av;
    1118        1540 : }
    1119             : 
    1120             : static void
    1121          49 : init_bad(struct check_pr *S, GEN nf, GEN gen)
    1122             : {
    1123          49 :   long i, l = lg(gen);
    1124          49 :   GEN bad = gen_1;
    1125             : 
    1126         105 :   for (i=1; i < l; i++)
    1127          56 :     bad = lcmii(bad, gcoeff(gel(gen,i),1,1));
    1128         105 :   for (i = 1; i < l; i++)
    1129             :   {
    1130          56 :     GEN c = gel(S->cycgen,i);
    1131             :     long j;
    1132          56 :     if (typ(c) == t_MAT)
    1133             :     {
    1134          56 :       GEN g = gel(c,1);
    1135         147 :       for (j = 1; j < lg(g); j++)
    1136             :       {
    1137          91 :         GEN h = idealhnf_shallow(nf, gel(g,j));
    1138          91 :         bad = lcmii(bad, gcoeff(h,1,1));
    1139             :       }
    1140             :     }
    1141             :   }
    1142          49 :   S->bad = bad;
    1143          49 : }
    1144             : 
    1145             : long
    1146          49 : bnfcertify0(GEN bnf, long flag)
    1147             : {
    1148          49 :   pari_sp av = avma;
    1149             :   long i, N;
    1150             :   GEN nf, cyc, B;
    1151             :   ulong bound, p;
    1152             :   struct check_pr S;
    1153             :   forprime_t T;
    1154             : 
    1155          49 :   bnf = checkbnf(bnf);
    1156          49 :   nf = bnf_get_nf(bnf);
    1157          49 :   N = nf_get_degree(nf); if (N==1) return 1;
    1158          49 :   testprimes(bnf, zimmertbound(N, nf_get_r2(nf), absi(nf_get_disc(nf))));
    1159          49 :   if (flag) return 1;
    1160             : 
    1161          49 :   cyc = bnf_get_cyc(bnf);
    1162          49 :   S.w = bnf_get_tuN(bnf);
    1163          49 :   S.mu = nf_to_scalar_or_basis(nf, bnf_get_tuU(bnf));
    1164          49 :   S.fu= matalgtobasis(nf, bnf_get_fu(bnf));
    1165          49 :   S.cyc = cyc;
    1166          49 :   S.cycgen = check_and_build_cycgen(bnf);
    1167          49 :   init_bad(&S, nf, bnf_get_gen(bnf));
    1168             : 
    1169          49 :   B = bound_unit_index(bnf, S.fu);
    1170          49 :   if (DEBUGLEVEL)
    1171             :   {
    1172           0 :     err_printf("PHASE 2 [UNITS]: are all primes good ?\n");
    1173           0 :     err_printf("  Testing primes <= %Ps\n", B); err_flush();
    1174             :   }
    1175          49 :   bound = itou_or_0(B);
    1176          49 :   if (!bound) pari_err_OVERFLOW("bnfcertify [too many primes to check]");
    1177          49 :   if (u_forprime_init(&T, 2, bound))
    1178          35 :     while ( (p = u_forprime_next(&T)) ) check_prime(p,bnf, &S);
    1179          49 :   if (lg(cyc) > 1)
    1180             :   {
    1181          21 :     GEN f = Z_factor(gel(cyc,1)), P = gel(f,1);
    1182          21 :     long l = lg(P);
    1183          21 :     if (DEBUGLEVEL>1) { err_printf("  Testing primes | h(K)\n\n"); err_flush(); }
    1184          49 :     for (i=1; i<l; i++)
    1185             :     {
    1186          28 :       p = itou(gel(P,i));
    1187          28 :       if (p > bound) check_prime(p,bnf, &S);
    1188             :     }
    1189             :   }
    1190          49 :   avma = av; return 1;
    1191             : }
    1192             : long
    1193          28 : bnfcertify(GEN bnf) { return bnfcertify0(bnf, 0); }
    1194             : 
    1195             : /*******************************************************************/
    1196             : /*                                                                 */
    1197             : /*        RAY CLASS FIELDS: CONDUCTORS AND DISCRIMINANTS           */
    1198             : /*                                                                 */
    1199             : /*******************************************************************/
    1200             : /* \chi(gen[i]) = zeta_D^chic[i])
    1201             :  * denormalize: express chi(gen[i]) in terms of zeta_{cyc[i]} */
    1202             : GEN
    1203        3976 : char_denormalize(GEN cyc, GEN D, GEN chic)
    1204             : {
    1205        3976 :   long i, l = lg(chic);
    1206        3976 :   GEN chi = cgetg(l, t_VEC);
    1207             :   /* \chi(gen[i]) = e(chic[i] / D) = e(chi[i] / cyc[i])
    1208             :    * hence chi[i] = chic[i]cyc[i]/ D  mod cyc[i] */
    1209        9492 :   for (i = 1; i < l; ++i)
    1210             :   {
    1211        5516 :     GEN di = gel(cyc, i), t = diviiexact(mulii(di, gel(chic,i)), D);
    1212        5516 :     gel(chi, i) = modii(t, di);
    1213             :   }
    1214        3976 :   return chi;
    1215             : }
    1216             : static GEN
    1217         371 : bnrchar_i(GEN bnr, GEN g, GEN v)
    1218             : {
    1219         371 :   long i, h, l = lg(g);
    1220             :   GEN CH, D, U, U2, H, cyc, cycD, dv, dchi;
    1221         371 :   checkbnr(bnr);
    1222         371 :   switch(typ(g))
    1223             :   {
    1224             :     GEN G;
    1225             :     case t_VEC:
    1226          14 :       G = cgetg(l, t_MAT);
    1227          14 :       for (i = 1; i < l; i++) gel(G,i) = isprincipalray(bnr, gel(g,i));
    1228          14 :       g = G; break;
    1229             :     case t_MAT:
    1230         357 :       if (RgM_is_ZM(g)) break;
    1231             :     default:
    1232           0 :       pari_err_TYPE("bnrchar",g);
    1233             :   }
    1234         371 :   cyc = bnr_get_cyc(bnr);
    1235         371 :   H = ZM_hnfall(shallowconcat(g,diagonal_shallow(cyc)), v? &U: NULL, 1);
    1236         371 :   dv = NULL;
    1237         371 :   if (v)
    1238             :   {
    1239          28 :     GEN w = Q_remove_denom(v, &dv);
    1240          28 :     if (typ(v)!=t_VEC || lg(v)!=l || !RgV_is_ZV(w)) pari_err_TYPE("bnrchar",v);
    1241          28 :     if (!dv) v = NULL;
    1242             :     else
    1243             :     {
    1244          28 :       U = rowslice(U, 1, l-1);
    1245          28 :       w = FpV_red(ZV_ZM_mul(w, U), dv);
    1246         105 :       for (i = 1; i < l; i++)
    1247          84 :         if (signe(gel(w,i))) pari_err_TYPE("bnrchar [inconsistent values]",v);
    1248          21 :       v = vecslice(w,l,lg(w)-1);
    1249             :     }
    1250             :   }
    1251             :   /* chi defined on subgroup H, chi(H[i]) = e(v[i] / dv)
    1252             :    * unless v = NULL: chi|H = 1*/
    1253         364 :   h = itos( ZM_det_triangular(H) ); /* #(clgp/H) = number of chars */
    1254         364 :   if (h == 1) /* unique character, H = Id */
    1255             :   {
    1256           7 :     if (v)
    1257           7 :       v = char_denormalize(cyc,dv,v);
    1258             :     else
    1259           0 :       v = zerovec(lg(cyc)-1); /* trivial char */
    1260           7 :     return mkvec(v);
    1261             :   }
    1262             : 
    1263             :   /* chi defined on a subgroup of index h > 1; U H V = D diagonal,
    1264             :    * Z^#H / (H) = Z^#H / (D) ~ \oplus (Z/diZ) */
    1265         357 :   D = ZM_snfall_i(H, &U, NULL, 1);
    1266         357 :   cycD = cyc_normalize(D); gel(cycD,1) = gen_1; /* cycD[i] = d1/di */
    1267         357 :   dchi = gel(D,1);
    1268         357 :   U2 = ZM_diag_mul(cycD, U);
    1269         357 :   if (v)
    1270             :   {
    1271          14 :     GEN Ui = ZM_inv(U,gen_1); /* U^(-1) */
    1272          14 :     GEN Z = hnf_solve(H, ZM_mul_diag(Ui, D));
    1273          14 :     v = ZV_ZM_mul(ZV_ZM_mul(v, Z), U2);
    1274          14 :     dchi = mulii(dchi, dv);
    1275          14 :     U2 = ZM_Z_mul(U2, dv);
    1276             :   }
    1277         357 :   CH = cyc2elts(D);
    1278        1561 :   for (i = 1; i <= h; i++)
    1279             :   {
    1280        1204 :     GEN c = zv_ZM_mul(gel(CH,i), U2);
    1281        1204 :     if (v) c = ZC_add(c, v);
    1282        1204 :     gel(CH,i) = char_denormalize(cyc, dchi, c);
    1283             :   }
    1284         357 :   return CH;
    1285             : }
    1286             : GEN
    1287         371 : bnrchar(GEN bnr, GEN g, GEN v)
    1288             : {
    1289         371 :   pari_sp av = avma;
    1290         371 :   return gerepilecopy(av, bnrchar_i(bnr,g,v));
    1291             : }
    1292             : 
    1293             : /* Let bnr1 with generators, bnr2 be such that mod(bnr2) | mod(bnr1), compute
    1294             :  * the matrix of the surjective map Cl(bnr1) ->> Cl(bnr2) */
    1295             : GEN
    1296        1414 : bnrsurjection(GEN bnr1, GEN bnr2)
    1297             : {
    1298             :   long l, i;
    1299        1414 :   GEN M, gen = bnr_get_gen(bnr1);
    1300        1414 :   l = lg(gen); M = cgetg(l, t_MAT);
    1301        1414 :   for (i = 1; i < l; i++) gel(M,i) = isprincipalray(bnr2, gel(gen,i));
    1302        1414 :   return M;
    1303             : }
    1304             : 
    1305             : /* Given normalized chi on bnr.clgp of conductor bnrc.mod,
    1306             :  * compute primitive character chic on bnrc.clgp equivalent to chi,
    1307             :  * still normalized wrt. bnr:
    1308             :  *   chic(genc[i]) = zeta_C^chic[i]), C = cyc_normalize(bnr.cyc)[1] */
    1309             : GEN
    1310         630 : bnrchar_primitive(GEN bnr, GEN nchi, GEN bnrc)
    1311             : {
    1312         630 :   GEN Mc, U, M = bnrsurjection(bnr, bnrc);
    1313         630 :   long l = lg(M);
    1314             : 
    1315         630 :   Mc = diagonal_shallow(bnr_get_cyc(bnrc));
    1316         630 :   (void)ZM_hnfall(shallowconcat(M, Mc), &U, 1); /* identity */
    1317         630 :   U = matslice(U,1,l-1, l,lg(U)-1);
    1318         630 :   return char_simplify(gel(nchi,1), ZV_ZM_mul(gel(nchi,2), U));
    1319             : }
    1320             : 
    1321             : /* s: <gen> = Cl_f --> Cl_f2 --> 0, H subgroup of Cl_f (generators given as
    1322             :  * HNF on [gen]). Return subgroup s(H) in Cl_f2. bnr must include generators */
    1323             : static GEN
    1324         301 : imageofgroup(GEN bnr, GEN bnr2, GEN H)
    1325             : {
    1326         301 :   GEN H2, cyc2 = bnr_get_cyc(bnr2);
    1327         301 :   if (!H) return diagonal_shallow(cyc2);
    1328         273 :   H2 = ZM_mul(bnrsurjection(bnr, bnr2), H);
    1329         273 :   return ZM_hnfmodid(H2, cyc2); /* s(H) in Cl_n */
    1330             : }
    1331             : static GEN
    1332         357 : imageofchar(GEN bnr, GEN bnrc, GEN chi)
    1333             : {
    1334         357 :   GEN nchi = char_normalize(chi, cyc_normalize(bnr_get_cyc(bnr)));
    1335         357 :   GEN DC = bnrchar_primitive(bnr, nchi, bnrc);
    1336         357 :   return char_denormalize(bnr_get_cyc(bnrc), gel(DC,1), gel(DC,2));
    1337             : }
    1338             : 
    1339             : /* convert A,B,C to [bnr, H] */
    1340             : GEN
    1341         259 : ABC_to_bnr(GEN A, GEN B, GEN C, GEN *H, int gen)
    1342             : {
    1343         259 :   if (typ(A) == t_VEC)
    1344         259 :     switch(lg(A))
    1345             :     {
    1346             :       case 7: /* bnr */
    1347         105 :         *H = B; return A;
    1348             :       case 11: /* bnf */
    1349         154 :         if (!B) pari_err_TYPE("ABC_to_bnr [bnf+missing conductor]",A);
    1350         154 :         *H = C; return Buchray(A,B, gen? nf_INIT | nf_GEN: nf_INIT);
    1351             :     }
    1352           0 :   pari_err_TYPE("ABC_to_bnr",A);
    1353           0 :   *H = NULL; return NULL; /* not reached */
    1354             : }
    1355             : 
    1356             : GEN
    1357          56 : bnrconductor0(GEN A, GEN B, GEN C, long flag)
    1358             : {
    1359          56 :   pari_sp av = avma;
    1360          56 :   GEN H, bnr = ABC_to_bnr(A,B,C,&H, flag > 0);
    1361          56 :   return gerepilecopy(av, bnrconductor_i(bnr, H, flag));
    1362             : }
    1363             : 
    1364             : long
    1365          28 : bnrisconductor0(GEN A,GEN B,GEN C)
    1366             : {
    1367          28 :   GEN H, bnr = ABC_to_bnr(A,B,C,&H, 0);
    1368          28 :   return bnrisconductor(bnr, H);
    1369             : }
    1370             : 
    1371             : /* return bnrisprincipal(bnr, (x)), assuming z = ideallog(x) */
    1372             : static GEN
    1373       16051 : ideallog_to_bnr(GEN bnr, GEN z)
    1374             : {
    1375       16051 :   GEN U = gel(bnr,4), divray = bnr_get_cyc(bnr);
    1376             :   long j, l, lU, lz;
    1377             :   int col;
    1378             : 
    1379       16051 :   if (lg(z) == 1) return z;
    1380       16051 :   col = (typ(z) == t_COL); /* else t_MAT */
    1381       16051 :   lz = col? lg(z): lgcols(z);
    1382       16051 :   lU = lg(U);
    1383       16051 :   if (lz != lU)
    1384             :   {
    1385       11354 :     if (lz == 1) return zerocol(nbrows(U)); /* lU != 1 */
    1386       11340 :     U = vecslice(U, lU-lz+1, lU-1); /* remove Cl(K) part */
    1387             :   }
    1388       16037 :   if (col) {
    1389        6216 :     z = ZM_ZC_mul(U, z);
    1390        6216 :     z = vecmodii(z, divray);
    1391             :   } else {
    1392        9821 :     z = ZM_mul(U, z); l = lg(z);
    1393        9821 :     for (j = 1; j < l; j++) gel(z,j) = vecmodii(gel(z,j), divray);
    1394             :   }
    1395       16037 :   return z;
    1396             : }
    1397             : static GEN
    1398        9835 : bnr_log_gen_pr(GEN bnr, zlog_S *S, GEN nf, long e, long index)
    1399        9835 : { return ideallog_to_bnr(bnr, log_gen_pr(S, index, nf, e)); }
    1400             : static GEN
    1401        6216 : bnr_log_gen_arch(GEN bnr, zlog_S *S, long index)
    1402        6216 : { return ideallog_to_bnr(bnr, log_gen_arch(S, index)); }
    1403             : 
    1404             : /* A \subset H ? Allow H = NULL = trivial subgroup */
    1405             : static int
    1406       14714 : contains(GEN H, GEN A)
    1407       14714 : { return H? (hnf_solve(H, A) != NULL): gequal0(A); }
    1408             : 
    1409             : /* (see also Discrayrel). Given a number field bnf=bnr[1], a ray class
    1410             :  * group structure bnr (with generators if flag > 0), and a subgroup H of the
    1411             :  * ray class group, compute the conductor of H if flag=0. If flag > 0, compute
    1412             :  * furthermore the corresponding H' and output
    1413             :  * if flag = 1: [[ideal,arch],[hm,cyc,gen],H']
    1414             :  * if flag = 2: [[ideal,arch],newbnr,H'] */
    1415             : GEN
    1416        4354 : bnrconductor_i(GEN bnr, GEN H0, long flag)
    1417             : {
    1418             :   long j, k, l;
    1419             :   GEN bnf, nf, bid, ideal, archp, clhray, bnrc, e2, e, cond, H;
    1420        4354 :   int iscond0 = 1, iscondinf = 1, ischi;
    1421             :   zlog_S S;
    1422             : 
    1423        4354 :   checkbnr(bnr);
    1424        4354 :   bnf = bnr_get_bnf(bnr);
    1425        4354 :   bid = bnr_get_bid(bnr); init_zlog_bid(&S, bid);
    1426        4354 :   nf = bnf_get_nf(bnf);
    1427        4354 :   H = check_subgroup(bnr, H0, &clhray);
    1428             : 
    1429        4354 :   archp = S.archp;
    1430        4354 :   e     = S.e; l = lg(e);
    1431        4354 :   e2 = cgetg(l, t_COL);
    1432        9310 :   for (k = 1; k < l; k++)
    1433             :   {
    1434        7742 :     for (j = itos(gel(e,k)); j > 0; j--)
    1435             :     {
    1436        6244 :       if (!contains(H, bnr_log_gen_pr(bnr, &S, nf, j, k))) break;
    1437        2786 :       iscond0 = 0;
    1438             :     }
    1439        4956 :     gel(e2,k) = stoi(j);
    1440             :   }
    1441        4354 :   l = lg(archp);
    1442        8246 :   for (k = 1; k < l; k++)
    1443             :   {
    1444        3892 :     if (!contains(H, bnr_log_gen_arch(bnr, &S, k))) continue;
    1445        1701 :     archp[k] = 0;
    1446        1701 :     iscondinf = 0;
    1447             :   }
    1448        4354 :   if (!iscondinf)
    1449             :   {
    1450        3157 :     for (j = k = 1; k < l; k++)
    1451        1764 :       if (archp[k]) archp[j++] = archp[k];
    1452        1393 :     setlg(archp, j);
    1453             :   }
    1454        4354 :   ideal = iscond0? bid_get_ideal(bid): factorbackprime(nf, S.P, e2);
    1455        4354 :   cond = mkvec2(ideal, indices_to_vec01(archp, nf_get_r1(nf)));
    1456        4354 :   if (!flag) return cond;
    1457             : 
    1458             :   /* character or subgroup ? */
    1459        1813 :   ischi = H0 && typ(H0) == t_VEC;
    1460        1813 :   if (iscond0 && iscondinf)
    1461             :   {
    1462        1155 :     bnrc = bnr;
    1463        2310 :     if (ischi)
    1464         497 :       H = H0;
    1465         658 :     else if (!H)
    1466         476 :       H = diagonal_shallow(bnr_get_cyc(bnr));
    1467             :   }
    1468             :   else
    1469             :   {
    1470         658 :     bnrc = Buchray(bnf, cond, nf_INIT | nf_GEN);
    1471         658 :     if (ischi)
    1472         357 :       H = imageofchar(bnr, bnrc, H0);
    1473             :     else
    1474         301 :       H = imageofgroup(bnr, bnrc, H);
    1475             :   }
    1476             : 
    1477        1813 :   if (flag == 1) bnrc = bnr_get_clgp(bnrc);
    1478        1813 :   return mkvec3(cond, bnrc, H);
    1479             : }
    1480             : GEN
    1481           0 : bnrconductor(GEN bnr, GEN H0, long flag)
    1482             : {
    1483           0 :   pari_sp av = avma;
    1484           0 :   return gerepilecopy(av, bnrconductor_i(bnr,H0,flag));
    1485             : }
    1486             : 
    1487             : long
    1488        3297 : bnrisconductor(GEN bnr, GEN H0)
    1489             : {
    1490        3297 :   pari_sp av = avma;
    1491             :   long j, k, l;
    1492             :   GEN bnf, nf, bid, archp, clhray, e, H;
    1493             :   zlog_S S;
    1494             : 
    1495        3297 :   checkbnr(bnr);
    1496        3297 :   bnf = bnr_get_bnf(bnr);
    1497        3297 :   bid = bnr_get_bid(bnr); init_zlog_bid(&S, bid);
    1498        3297 :   nf = bnf_get_nf(bnf);
    1499        3297 :   H = check_subgroup(bnr, H0, &clhray);
    1500             : 
    1501        3297 :   archp = S.archp;
    1502        3297 :   e     = S.e; l = lg(e);
    1503        4228 :   for (k = 1; k < l; k++)
    1504             :   {
    1505        2842 :     j = itos(gel(e,k));
    1506        2842 :     if (contains(H, bnr_log_gen_pr(bnr, &S, nf, j, k))) { avma = av; return 0; }
    1507             :   }
    1508        1386 :   l = lg(archp);
    1509        2072 :   for (k = 1; k < l; k++)
    1510        1589 :     if (contains(H, bnr_log_gen_arch(bnr, &S, k))) { avma = av; return 0; }
    1511         483 :   avma = av; return 1;
    1512             : }
    1513             : 
    1514             : /* return the norm group corresponding to the relative extension given by
    1515             :  * polrel over bnr.bnf, assuming it is abelian and the modulus of bnr is a
    1516             :  * multiple of the conductor */
    1517             : static GEN
    1518         672 : rnfnormgroup_i(GEN bnr, GEN polrel)
    1519             : {
    1520             :   long i, j, degrel, degnf, k;
    1521             :   GEN bnf, index, discnf, nf, G, detG, fa, gdegrel;
    1522             :   GEN fac, col, cnd;
    1523             :   forprime_t S;
    1524             :   ulong p;
    1525             : 
    1526         672 :   checkbnr(bnr); bnf = bnr_get_bnf(bnr);
    1527         672 :   nf = bnf_get_nf(bnf);
    1528         672 :   cnd = gel(bnr_get_mod(bnr), 1);
    1529         672 :   polrel = RgX_nffix("rnfnormgroup", nf_get_pol(nf),polrel,1);
    1530         672 :   if (!gequal1(leading_coeff(polrel)))
    1531           0 :     pari_err_IMPL("rnfnormgroup for non-monic polynomials");
    1532             : 
    1533         672 :   degrel = degpol(polrel);
    1534         672 :   if (umodiu(bnr_get_no(bnr), degrel)) return NULL;
    1535             :   /* degrel-th powers are in norm group */
    1536         665 :   gdegrel = utoipos(degrel);
    1537         665 :   G = FpC_red(bnr_get_cyc(bnr), gdegrel);
    1538        1729 :   for (i=1; i<lg(G); i++)
    1539        1064 :     if (!signe(gel(G,i))) gel(G,i) = gdegrel;
    1540         665 :   detG = ZV_prod(G);
    1541         665 :   k = cmpiu(detG,degrel);
    1542         665 :   if (k < 0) return NULL;
    1543         665 :   if (!k) return diagonal(G);
    1544             : 
    1545         329 :   G = diagonal_shallow(G);
    1546         329 :   discnf = nf_get_disc(nf);
    1547         329 :   index  = nf_get_index(nf);
    1548         329 :   degnf = nf_get_degree(nf);
    1549         329 :   u_forprime_init(&S, 2, ULONG_MAX);
    1550        3829 :   while ( (p = u_forprime_next(&S)) )
    1551             :   {
    1552             :     long oldf, nfa;
    1553             :     /* If all pr are unramified and have the same residue degree, p =prod pr
    1554             :      * and including last pr^f or p^f is the same, but the last isprincipal
    1555             :      * is much easier! oldf is used to track this */
    1556             : 
    1557        3500 :     if (!umodiu(index, p)) continue; /* can't be treated efficiently */
    1558             : 
    1559             :     /* primes of degree 1 are enough, and simpler */
    1560        3402 :     fa = idealprimedec_limit_f(nf, utoipos(p), 1);
    1561        3402 :     nfa = lg(fa)-1;
    1562        3402 :     if (!nfa) continue;
    1563             :     /* all primes above p included ? */
    1564        1652 :     oldf = (nfa == degnf)? -1: 0;
    1565        3619 :     for (i=1; i<=nfa; i++)
    1566             :     {
    1567        2296 :       GEN pr = gel(fa,i), pp, T, polr, modpr;
    1568             :       long f, nfac;
    1569             :       /* if pr (probably) ramified, we have to use all (non-ram) P | pr */
    1570        4102 :       if (idealval(nf,cnd,pr)) { oldf = 0; continue; }
    1571        2065 :       modpr = zk_to_Fq_init(nf, &pr, &T, &pp); /* T = NULL, pp ignored */
    1572        2065 :       polr = nfX_to_FqX(polrel, nf, modpr); /* in Fp[X] */
    1573        2065 :       polr = ZX_to_Flx(polr, p);
    1574        2065 :       if (!Flx_is_squarefree(polr, p)) { oldf = 0; continue; }
    1575             : 
    1576        1841 :       fac = gel(Flx_factor(polr, p), 1);
    1577        1841 :       f = degpol(gel(fac,1));
    1578        1841 :       if (f == degrel) continue; /* degrel-th powers already included */
    1579         490 :       nfac = lg(fac)-1;
    1580             :       /* check decomposition of pr has Galois type */
    1581        1449 :       for (j=2; j<=nfac; j++)
    1582        1295 :         if (degpol(gel(fac,j)) != f) return NULL;
    1583         483 :       if (oldf < 0) oldf = f; else if (oldf != f) oldf = 0;
    1584             : 
    1585             :       /* last prime & all pr^f, pr | p, included. Include p^f instead */
    1586         483 :       if (oldf && i == nfa && degrel == nfa*f && !umodiu(discnf, p))
    1587           0 :         pr = utoipos(p);
    1588             : 
    1589             :       /* pr^f = N P, P | pr, hence is in norm group */
    1590         483 :       col = bnrisprincipal(bnr,pr,0);
    1591         483 :       if (f > 1) col = ZC_z_mul(col, f);
    1592         483 :       G = ZM_hnf(shallowconcat(G, col));
    1593         483 :       detG = ZM_det_triangular(G);
    1594         483 :       k = cmpiu(detG,degrel);
    1595         483 :       if (k < 0) return NULL;
    1596         483 :       if (!k) { cgiv(detG); return G; }
    1597             :     }
    1598             :   }
    1599           0 :   return NULL;
    1600             : }
    1601             : GEN
    1602         329 : rnfnormgroup(GEN bnr, GEN polrel)
    1603             : {
    1604         329 :   pari_sp av = avma;
    1605         329 :   GEN G = rnfnormgroup_i(bnr, polrel);
    1606         329 :   if (!G) { avma = av; return cgetg(1,t_MAT); }
    1607         322 :   return gerepileupto(av, G);
    1608             : }
    1609             : 
    1610             : GEN
    1611          14 : nf_deg1_prime(GEN nf)
    1612             : {
    1613          14 :   GEN z, T = nf_get_pol(nf), D = nf_get_disc(nf), f = nf_get_index(nf);
    1614          14 :   long degnf = degpol(T);
    1615             :   forprime_t S;
    1616             :   pari_sp av;
    1617             :   ulong p;
    1618          14 :   u_forprime_init(&S, degnf, ULONG_MAX);
    1619          14 :   av = avma;
    1620         672 :   while ( (p = u_forprime_next(&S)) )
    1621             :   {
    1622             :     ulong r;
    1623         658 :     if (!umodiu(D, p) || !umodiu(f, p)) continue;
    1624         630 :     r = Flx_oneroot(ZX_to_Flx(T,p), p);
    1625         630 :     if (r != p)
    1626             :     {
    1627          14 :       z = utoi(Fl_neg(r, p));
    1628          14 :       z = deg1pol_shallow(gen_1, z, varn(T));
    1629          14 :       return primedec_apply_kummer(nf, z, 1, utoipos(p));
    1630             :     }
    1631         616 :     avma = av;
    1632             :   }
    1633           0 :   return NULL;
    1634             : }
    1635             : 
    1636             : long
    1637          42 : rnfisabelian(GEN nf, GEN pol)
    1638             : {
    1639             :   GEN modpr, pr, T, Tnf, pp, ro, nfL, C, z, a, sig, eq;
    1640             :   long i, j, l, v;
    1641             :   ulong p, k, ka;
    1642             : 
    1643          42 :   if (typ(nf) == t_POL)
    1644          35 :     Tnf = nf;
    1645             :   else {
    1646           7 :     nf = checknf(nf);
    1647           7 :     Tnf = nf_get_pol(nf);
    1648             :   }
    1649          42 :   v = varn(Tnf);
    1650          42 :   pol = RgX_nffix("rnfisabelian",Tnf,pol,1);
    1651          42 :   eq = nf_rnfeq(nf,pol); /* init L := K[x]/(pol), nf attached to K */
    1652          42 :   C = gel(eq,1); setvarn(C, v); /* L = Q[t]/(C) */
    1653          42 :   a = gel(eq,2); setvarn(a, v); /* root of K.pol in L */
    1654          42 :   z = nfroots_split(C, QXX_QXQ_eval(pol, a, C));
    1655          42 :   if (!z) return 0;
    1656          35 :   ro = gel(z,1); l = lg(ro)-1;
    1657             :   /* small groups are abelian, as are groups of prime order */
    1658          35 :   if (l < 6 || uisprime(l)) return 1;
    1659             : 
    1660          14 :   nfL = gel(z,2);
    1661          14 :   pr = nf_deg1_prime(nfL);
    1662          14 :   modpr = nf_to_Fq_init(nfL, &pr, &T, &pp);
    1663          14 :   p = itou(pp);
    1664          14 :   k = umodiu(gel(eq,3), p);
    1665          14 :   ka = (k * itou(nf_to_Fq(nfL, a, modpr))) % p;
    1666          14 :   sig= cgetg(l+1, t_VECSMALL);
    1667             :   /* image of c = ro[1] + k a [distinguished root of C] by the l automorphisms
    1668             :    * sig[i]: ro[1] -> ro[i] */
    1669          98 :   for (i = 1; i <= l; i++)
    1670          84 :     sig[i] = Fl_add(ka, itou(nf_to_Fq(nfL, gel(ro,i), modpr)), p);
    1671          14 :   ro = Q_primpart(ro);
    1672          84 :   for (i=2; i<=l; i++) { /* start at 2, since sig[1] = identity */
    1673          70 :     gel(ro,i) = ZX_to_Flx(gel(ro,i), p);
    1674         210 :     for (j=2; j<i; j++)
    1675         280 :       if (Flx_eval(gel(ro,j), sig[i], p)
    1676         140 :        != Flx_eval(gel(ro,i), sig[j], p)) return 0;
    1677             :   }
    1678          14 :   return 1;
    1679             : }
    1680             : 
    1681             : /* Given bnf and polrel defining an abelian relative extension, compute the
    1682             :  * corresponding conductor and congruence subgroup. Return
    1683             :  * [cond,bnr(cond),group] where cond=[ideal,arch] is the conductor. */
    1684             : GEN
    1685         343 : rnfconductor(GEN bnf, GEN polrel)
    1686             : {
    1687         343 :   pari_sp av = avma;
    1688             :   GEN nf, module, bnr, group, den, D;
    1689             : 
    1690         343 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
    1691         343 :   if (typ(polrel) != t_POL) pari_err_TYPE("rnfconductor",polrel);
    1692         343 :   den = Q_denom( RgX_to_nfX(nf, polrel) );
    1693         343 :   if (!is_pm1(den)) polrel = RgX_rescale(polrel, den);
    1694         343 :   (void)rnfallbase(nf,&polrel, &D, NULL, NULL);
    1695         343 :   module = mkvec2(D, const_vec(nf_get_r1(nf), gen_1));
    1696         343 :   bnr   = Buchray(bnf,module,nf_INIT | nf_GEN);
    1697         343 :   group = rnfnormgroup_i(bnr,polrel);
    1698         343 :   if (!group) { avma = av; return gen_0; }
    1699         336 :   return gerepilecopy(av, bnrconductor_i(bnr,group,2));
    1700             : }
    1701             : 
    1702             : /* Given a number field bnf=bnr[1], a ray class group structure bnr, and a
    1703             :  * subgroup H (HNF form) of the ray class group, compute [n, r1, dk]
    1704             :  * attached to H. If flcond = 1, abort (return gen_0) if
    1705             :  * module is not the conductor If flrel = 0, compute only N(dk) instead of
    1706             :  * the ideal dk proper */
    1707             : static GEN
    1708         175 : Discrayrel(GEN bnr, GEN H0, long flag)
    1709             : {
    1710         175 :   pari_sp av = avma;
    1711         175 :   long j, k, l, nz, flrel = flag & rnf_REL, flcond = flag & rnf_COND;
    1712             :   GEN bnf, nf, bid, ideal, archp, clhray, clhss, P, e, dlk;
    1713             :   zlog_S S;
    1714             : 
    1715         175 :   checkbnr(bnr);
    1716         175 :   bnf = bnr_get_bnf(bnr);
    1717         175 :   bid = bnr_get_bid(bnr); init_zlog_bid(&S, bid);
    1718         175 :   nf = bnf_get_nf(bnf);
    1719         175 :   ideal= bid_get_ideal(bid);
    1720         175 :   H0 = check_subgroup(bnr, H0, &clhray);
    1721         175 :   if (!H0) H0 = diagonal_shallow(bnr_get_cyc(bnr));
    1722         175 :   archp = S.archp;
    1723         175 :   P     = S.P;
    1724         175 :   e     = S.e; l = lg(e);
    1725         224 :   dlk = flrel? idealpow(nf,ideal,clhray)
    1726         224 :              : powii(ZM_det_triangular(ideal),clhray);
    1727         308 :   for (k = 1; k < l; k++)
    1728             :   {
    1729         147 :     GEN pr = gel(P,k), sum = gen_0, H = H0;
    1730         147 :     long ep = itos(gel(e,k));
    1731         266 :     for (j = ep; j > 0; j--)
    1732             :     {
    1733         182 :       GEN z = bnr_log_gen_pr(bnr, &S, nf, j, k);
    1734         182 :       H = ZM_hnf(shallowconcat(H, z));
    1735         182 :       clhss = ZM_det_triangular(H);
    1736         182 :       if (flcond && j==ep && equalii(clhss,clhray)) { avma = av; return gen_0; }
    1737         168 :       if (is_pm1(clhss)) { sum = addis(sum, j); break; }
    1738         119 :       sum = addii(sum, clhss);
    1739             :     }
    1740         133 :     dlk = flrel? idealdivpowprime(nf, dlk, pr, sum)
    1741         133 :                : diviiexact(dlk, powii(pr_norm(pr),sum));
    1742             :   }
    1743         161 :   l = lg(archp); nz = nf_get_r1(nf) - (l-1);
    1744         280 :   for (k = 1; k < l; k++)
    1745             :   {
    1746         147 :     if (!contains(H0, bnr_log_gen_arch(bnr, &S, k))) continue;
    1747          98 :     if (flcond) { avma = av; return gen_0; }
    1748          70 :     nz++;
    1749             :   }
    1750         133 :   return gerepilecopy(av, mkvec3(clhray, stoi(nz), dlk));
    1751             : }
    1752             : 
    1753             : GEN
    1754         175 : bnrdisc(GEN bnr, GEN H, long flag)
    1755             : {
    1756         175 :   pari_sp av = avma;
    1757             :   long clhray, n, R1;
    1758             :   GEN z, p1, D, dk, nf, dkabs;
    1759             : 
    1760         175 :   D = Discrayrel(bnr, H, flag);
    1761         175 :   if ((flag & rnf_REL) || D == gen_0) return D;
    1762             : 
    1763          98 :   nf = checknf(bnr);
    1764          98 :   dkabs = absi(nf_get_disc(nf));
    1765          98 :   clhray = itos(gel(D,1)); p1 = powiu(dkabs, clhray);
    1766          98 :   n = clhray * nf_get_degree(nf);
    1767          98 :   R1= clhray * itos(gel(D,2));
    1768          98 :   dk = gel(D,3);
    1769          98 :   if (((n-R1)&3) == 2) dk = negi(dk); /* (2r2) mod 4 = 2 : r2(relext) is odd */
    1770          98 :   z = cgetg(4,t_VEC);
    1771          98 :   gel(z,1) = utoipos(n);
    1772          98 :   gel(z,2) = stoi(R1);
    1773          98 :   gel(z,3) = mulii(dk,p1); return gerepileupto(av, z);
    1774             : }
    1775             : 
    1776             : GEN
    1777         175 : bnrdisc0(GEN A, GEN B, GEN C, long flag)
    1778             : {
    1779         175 :   GEN H, bnr = ABC_to_bnr(A,B,C,&H, 0);
    1780         175 :   return bnrdisc(bnr,H,flag);
    1781             : }
    1782             : GEN
    1783           0 : discrayrel(GEN bnr, GEN H)
    1784           0 : { return bnrdisc(bnr,H,rnf_REL); }
    1785             : GEN
    1786           0 : discrayrelcond(GEN bnr, GEN H)
    1787           0 : { return bnrdisc(bnr,H,rnf_REL | rnf_COND); }
    1788             : GEN
    1789           0 : discrayabs(GEN bnr, GEN H)
    1790           0 : { return bnrdisc(bnr,H,0); }
    1791             : GEN
    1792           0 : discrayabscond(GEN bnr, GEN H)
    1793           0 : { return bnrdisc(bnr,H,rnf_COND); }
    1794             : 
    1795             : /* Given a number field bnf=bnr[1], a ray class group structure bnr and a
    1796             :  * vector chi representing a character on the generators bnr[2][3], compute
    1797             :  * the conductor of chi. */
    1798             : GEN
    1799        2079 : bnrconductorofchar(GEN bnr, GEN chi)
    1800             : {
    1801        2079 :   pari_sp av = avma;
    1802             :   GEN cyc, K;
    1803        2079 :   checkbnr(bnr);
    1804        2079 :   cyc = bnr_get_cyc(bnr);
    1805        2079 :   if (!char_check(cyc,chi)) pari_err_TYPE("bnrconductorofchar",chi);
    1806        2079 :   K = charker(cyc,chi); if (lg(K) == 1) K = NULL;
    1807        2079 :   return gerepilecopy(av, bnrconductor_i(bnr, K, 0));
    1808             : }
    1809             : 
    1810             : /* t = [bid,U], h = #Cl(K) */
    1811             : static GEN
    1812         938 : get_classno(GEN t, GEN h)
    1813             : {
    1814         938 :   GEN bid = gel(t,1), m = gel(t,2), cyc = bid_get_cyc(bid);
    1815         938 :   return mulii(h, ZM_det_triangular(ZM_hnfmodid(m, cyc)));
    1816             : }
    1817             : 
    1818             : static void
    1819          28 : chk_listBU(GEN L, const char *s) {
    1820          28 :   if (typ(L) != t_VEC) pari_err_TYPE(s,L);
    1821          28 :   if (lg(L) > 1) {
    1822          28 :     GEN z = gel(L,1);
    1823          28 :     if (typ(z) != t_VEC) pari_err_TYPE(s,z);
    1824          56 :     if (lg(z) == 1) return;
    1825          28 :     z = gel(z,1); /* [bid,U] */
    1826          28 :     if (typ(z) != t_VEC || lg(z) != 3) pari_err_TYPE(s,z);
    1827          28 :     checkbid(gel(z,1));
    1828             :   }
    1829             : }
    1830             : 
    1831             : /* Given lists of [bid, unit ideallogs], return lists of ray class
    1832             :  * numbers */
    1833             : GEN
    1834           7 : bnrclassnolist(GEN bnf,GEN L)
    1835             : {
    1836           7 :   pari_sp av = avma;
    1837           7 :   long i, j, lz, l = lg(L);
    1838             :   GEN v, z, V, h;
    1839             : 
    1840           7 :   chk_listBU(L, "bnrclassnolist");
    1841           7 :   if (l == 1) return cgetg(1, t_VEC);
    1842           7 :   bnf = checkbnf(bnf); h = bnf_get_no(bnf);
    1843           7 :   V = cgetg(l,t_VEC);
    1844         392 :   for (i = 1; i < l; i++)
    1845             :   {
    1846         385 :     z = gel(L,i); lz = lg(z);
    1847         385 :     gel(V,i) = v = cgetg(lz,t_VEC);
    1848         385 :     for (j=1; j<lz; j++) gel(v,j) = get_classno(gel(z,j), h);
    1849             :   }
    1850           7 :   return gerepilecopy(av, V);
    1851             : }
    1852             : 
    1853             : static GEN
    1854        1267 : Lbnrclassno(GEN L, GEN fac)
    1855             : {
    1856        1267 :   long i, l = lg(L);
    1857        1778 :   for (i=1; i<l; i++)
    1858        1778 :     if (gequal(gmael(L,i,1),fac)) return gmael(L,i,2);
    1859           0 :   pari_err_BUG("Lbnrclassno");
    1860           0 :   return NULL; /* not reached */
    1861             : }
    1862             : 
    1863             : static GEN
    1864         420 : factordivexact(GEN fa1,GEN fa2)
    1865             : {
    1866             :   long i, j, k, c, l;
    1867             :   GEN P, E, P1, E1, P2, E2, p1;
    1868             : 
    1869         420 :   P1 = gel(fa1,1); E1 = gel(fa1,2); l = lg(P1);
    1870         420 :   P2 = gel(fa2,1); E2 = gel(fa2,2);
    1871         420 :   P = cgetg(l,t_COL);
    1872         420 :   E = cgetg(l,t_COL);
    1873         931 :   for (c = i = 1; i < l; i++)
    1874             :   {
    1875         511 :     j = RgV_isin(P2,gel(P1,i));
    1876         511 :     if (!j) { gel(P,c) = gel(P1,i); gel(E,c) = gel(E1,i); c++; }
    1877             :     else
    1878             :     {
    1879         511 :       p1 = subii(gel(E1,i), gel(E2,j)); k = signe(p1);
    1880         511 :       if (k < 0) pari_err_BUG("factordivexact [not exact]");
    1881         511 :       if (k > 0) { gel(P,c) = gel(P1,i); gel(E,c) = p1; c++; }
    1882             :     }
    1883             :   }
    1884         420 :   setlg(P, c);
    1885         420 :   setlg(E, c); return mkmat2(P, E);
    1886             : }
    1887             : /* remove index k */
    1888             : static GEN
    1889        1015 : factorsplice(GEN fa, long k)
    1890             : {
    1891        1015 :   GEN p = gel(fa,1), e = gel(fa,2), P, E;
    1892        1015 :   long i, l = lg(p) - 1;
    1893        1015 :   P = cgetg(l, typ(p));
    1894        1015 :   E = cgetg(l, typ(e));
    1895        1015 :   for (i=1; i<k; i++) { P[i] = p[i]; E[i] = e[i]; }
    1896        1015 :   p++; e++;
    1897        1015 :   for (   ; i<l; i++) { P[i] = p[i]; E[i] = e[i]; }
    1898        1015 :   return mkmat2(P,E);
    1899             : }
    1900             : static GEN
    1901         840 : factorpow(GEN fa, long n)
    1902             : {
    1903         840 :   if (!n) return trivial_fact();
    1904         840 :   return mkmat2(gel(fa,1), gmulsg(n, gel(fa,2)));
    1905             : }
    1906             : static GEN
    1907        1071 : factormul(GEN fa1,GEN fa2)
    1908             : {
    1909        1071 :   GEN p, pnew, e, enew, v, P, y = famat_mul_shallow(fa1,fa2);
    1910             :   long i, c, lx;
    1911             : 
    1912        1071 :   p = gel(y,1); v = indexsort(p); lx = lg(p);
    1913        1071 :   e = gel(y,2);
    1914        1071 :   pnew = vecpermute(p, v);
    1915        1071 :   enew = vecpermute(e, v);
    1916        1071 :   P = gen_0; c = 0;
    1917        3101 :   for (i=1; i<lx; i++)
    1918             :   {
    1919        2030 :     if (gequal(gel(pnew,i),P))
    1920          49 :       gel(e,c) = addii(gel(e,c),gel(enew,i));
    1921             :     else
    1922             :     {
    1923        1981 :       c++; P = gel(pnew,i);
    1924        1981 :       gel(p,c) = P;
    1925        1981 :       gel(e,c) = gel(enew,i);
    1926             :     }
    1927             :   }
    1928        1071 :   setlg(p, c+1);
    1929        1071 :   setlg(e, c+1); return y;
    1930             : }
    1931             : 
    1932             : 
    1933             : static long
    1934         175 : get_nz(GEN bnf, GEN ideal, GEN arch, long clhray)
    1935             : {
    1936             :   GEN arch2, mod;
    1937         175 :   long nz = 0, l = lg(arch), k, clhss;
    1938         175 :   if (typ(arch) == t_VECSMALL)
    1939          14 :     arch2 = indices_to_vec01(arch,nf_get_r1(bnf_get_nf(bnf)));
    1940             :   else
    1941         161 :     arch2 = leafcopy(arch);
    1942         175 :   mod = mkvec2(ideal, arch2);
    1943         455 :   for (k = 1; k < l; k++)
    1944             :   { /* FIXME: this is wasteful. Use the same algorithm as bnrconductor */
    1945         308 :     if (signe(gel(arch2,k)))
    1946             :     {
    1947          28 :       gel(arch2,k) = gen_0; clhss = itos(bnrclassno(bnf,mod));
    1948          28 :       gel(arch2,k) = gen_1;
    1949          28 :       if (clhss == clhray) return -1;
    1950             :     }
    1951         280 :     else nz++;
    1952             :   }
    1953         147 :   return nz;
    1954             : }
    1955             : 
    1956             : static GEN
    1957         420 : get_NR1D(long Nf, long clhray, long degk, long nz, GEN fadkabs, GEN idealrel)
    1958             : {
    1959             :   long n, R1;
    1960             :   GEN dlk;
    1961         420 :   if (nz < 0) mkvec3(gen_0,gen_0,gen_0); /*EMPTY*/
    1962         420 :   n  = clhray * degk;
    1963         420 :   R1 = clhray * nz;
    1964         420 :   dlk = factordivexact(factorpow(Z_factor(utoipos(Nf)),clhray), idealrel);
    1965             :   /* r2 odd, set dlk = -dlk */
    1966         420 :   if (((n-R1)&3)==2) dlk = factormul(to_famat_shallow(gen_m1,gen_1), dlk);
    1967         420 :   return mkvec3(utoipos(n),
    1968             :                 stoi(R1),
    1969             :                 factormul(dlk,factorpow(fadkabs,clhray)));
    1970             : }
    1971             : 
    1972             : /* t = [bid,U], h = #Cl(K) */
    1973             : static GEN
    1974         497 : get_discdata(GEN t, GEN h)
    1975             : {
    1976         497 :   GEN bid = gel(t,1), fa = gel(bid,3);
    1977         994 :   return mkvec3(mkmat2(gel(fa,1), vec_to_vecsmall(gel(fa,2))),
    1978         497 :                 (GEN)itou(get_classno(t, h)),
    1979             :                 bid_get_mod(bid));
    1980             : }
    1981             : typedef struct _disc_data {
    1982             :   long degk;
    1983             :   GEN bnf, fadk, idealrelinit, V;
    1984             : } disc_data;
    1985             : 
    1986             : static GEN
    1987         497 : get_discray(disc_data *D, GEN V, GEN z, long N)
    1988             : {
    1989         497 :   GEN idealrel = D->idealrelinit;
    1990         497 :   GEN mod = gel(z,3), Fa = gel(z,1);
    1991         497 :   GEN P = gel(Fa,1), E = gel(Fa,2);
    1992         497 :   long k, nz, clhray = z[2], lP = lg(P);
    1993         735 :   for (k=1; k<lP; k++)
    1994             :   {
    1995         574 :     GEN pr = gel(P,k), p = pr_get_p(pr);
    1996         574 :     long e, ep = E[k], f = pr_get_f(pr);
    1997         574 :     long S = 0, norm = N, Npr = upowuu(p[2],f), clhss;
    1998         826 :     for (e=1; e<=ep; e++)
    1999             :     {
    2000             :       GEN fad;
    2001         602 :       if (e < ep) { E[k] = ep-e; fad = Fa; }
    2002         476 :       else fad = factorsplice(Fa, k);
    2003         602 :       norm /= Npr;
    2004         602 :       clhss = (long)Lbnrclassno(gel(V,norm), fad);
    2005         602 :       if (e==1 && clhss==clhray) { E[k] = ep; return cgetg(1, t_VEC); }
    2006         266 :       if (clhss == 1) { S += ep-e+1; break; }
    2007         252 :       S += clhss;
    2008             :     }
    2009         238 :     E[k] = ep;
    2010         238 :     idealrel = factormul(idealrel, to_famat_shallow(p, utoi(f * S)));
    2011             :   }
    2012         161 :   nz = get_nz(D->bnf, gel(mod,1), gel(mod,2), clhray);
    2013         161 :   return get_NR1D(N, clhray, D->degk, nz, D->fadk, idealrel);
    2014             : }
    2015             : 
    2016             : /* Given a list of bids and attached unit log matrices, return the
    2017             :  * list of discrayabs. Only keep moduli which are conductors. */
    2018             : GEN
    2019          21 : discrayabslist(GEN bnf, GEN L)
    2020             : {
    2021          21 :   pari_sp av = avma;
    2022          21 :   long i, l = lg(L);
    2023             :   GEN nf, V, D, h;
    2024             :   disc_data ID;
    2025             : 
    2026          21 :   chk_listBU(L, "discrayabslist");
    2027          21 :   if (l == 1) return cgetg(1, t_VEC);
    2028          21 :   ID.bnf = bnf = checkbnf(bnf);
    2029          21 :   nf = bnf_get_nf(bnf);
    2030          21 :   h = bnf_get_no(bnf);
    2031          21 :   ID.degk = nf_get_degree(nf);
    2032          21 :   ID.fadk = absi_factor(nf_get_disc(nf));
    2033          21 :   ID.idealrelinit = trivial_fact();
    2034          21 :   V = cgetg(l, t_VEC);
    2035          21 :   D = cgetg(l, t_VEC);
    2036         462 :   for (i = 1; i < l; i++)
    2037             :   {
    2038         441 :     GEN z = gel(L,i), v, d;
    2039         441 :     long j, lz = lg(z);
    2040         441 :     gel(V,i) = v = cgetg(lz,t_VEC);
    2041         441 :     gel(D,i) = d = cgetg(lz,t_VEC);
    2042         938 :     for (j=1; j<lz; j++) {
    2043         497 :       gel(d,j) = get_discdata(gel(z,j), h);
    2044         497 :       gel(v,j) = get_discray(&ID, D, gel(d,j), i);
    2045             :     }
    2046             :   }
    2047          21 :   return gerepilecopy(av, V);
    2048             : }
    2049             : 
    2050             : /* BIG VECTOR:
    2051             :  * Interface: a container v whose length is arbitrary (< 2^30), bigel(v,i)
    2052             :  * refers to the i-th component. It is an lvalue.
    2053             :  *
    2054             :  * Implementation: a vector v whose components have exactly 2^LGVINT entries
    2055             :  * but for the last one which is allowed to be shorter. v[i][j]
    2056             :  * (where j<=2^LGVINT) is understood as component number I = (i-1)*2^LGVINT+j
    2057             :  * in a unique huge vector V. */
    2058             : static const int SHLGVINT = 15;
    2059             : static const long LGVINT = 1L << 15;
    2060        3388 : INLINE long vext0(ulong i) { return ((i-1)>>SHLGVINT) + 1; }
    2061        3388 : INLINE long vext1(ulong i) { return i & (LGVINT-1); }
    2062             : #define bigel(v,i) gmael((v), vext0(i), vext1(i))
    2063             : 
    2064             : /* allocate an extended vector (t_VEC of t_VEC) for N _true_ components */
    2065             : static GEN
    2066          77 : bigcgetvec(long N)
    2067             : {
    2068          77 :   long i, nv = vext0(N);
    2069          77 :   GEN v = cgetg(nv+1,t_VEC);
    2070          77 :   for (i=1; i<nv; i++) gel(v,i) = cgetg(LGVINT+1,t_VEC);
    2071          77 :   gel(v,nv) = cgetg(vext1(N)+1,t_VEC); return v;
    2072             : }
    2073             : 
    2074             : /* a zsimp is [fa, cyc, U, v]
    2075             :  * fa: vecsmall factorisation,
    2076             :  * cyc: ZV (abelian group)
    2077             :  * U: ZM (base change)
    2078             :  * v: ZV (log of units) */
    2079             : static GEN
    2080          21 : zsimp(GEN bid, GEN embunit)
    2081             : {
    2082          21 :   GEN empty = cgetg(1, t_VECSMALL);
    2083          21 :   return mkvec4(mkmat2(empty,empty), bid_get_cyc(bid), gel(bid,5), embunit);
    2084             : }
    2085             : 
    2086             : /* fa a vecsmall factorization, append p^e */
    2087             : static GEN
    2088         308 : fasmall_append(GEN fa, long p, long e)
    2089             : {
    2090         308 :   GEN P = gel(fa,1), E = gel(fa,2);
    2091         308 :   retmkmat2(vecsmall_append(P,p), vecsmall_append(E,e));
    2092             : }
    2093             : 
    2094             : static GEN
    2095         308 : zsimpjoin(GEN b, GEN bid, GEN embunit, long prcode, long e)
    2096             : {
    2097             :   long l1, l2, nbgen;
    2098         308 :   pari_sp av = avma;
    2099             :   GEN fa, U, U1, U2, cyc1, cyc2, cyc;
    2100             : 
    2101         308 :   fa = gel(b,1);
    2102         308 :   U1 = gel(b,3);   cyc1 = gel(b,2);         l1 = lg(cyc1);
    2103         308 :   U2 = gel(bid,5); cyc2 = bid_get_cyc(bid); l2 = lg(cyc2);
    2104         308 :   nbgen = l1+l2-2;
    2105         308 :   if (nbgen)
    2106             :   {
    2107         287 :     cyc = ZV_snfall(shallowconcat(cyc1,cyc2), &U, NULL);
    2108         287 :     ZM_snfclean(cyc, U, NULL);
    2109         861 :     U = shallowconcat(
    2110         287 :       l1==1   ? zeromat(nbgen, lg(U1)-1): ZM_mul(vecslice(U, 1,   l1-1), U1),
    2111         287 :       l1>nbgen? zeromat(nbgen, lg(U2)-1): ZM_mul(vecslice(U, l1, nbgen), U2)
    2112             :     );
    2113             :   }
    2114             :   else
    2115             :   {
    2116          21 :     U = zeromat(0, lg(U1)+lg(U2)-2);
    2117          21 :     cyc = cgetg(1,t_VEC);
    2118             :   }
    2119         308 :   fa = fasmall_append(fa, prcode, e);
    2120         308 :   return gerepilecopy(av, mkvec4(fa, cyc, U, vconcat(gel(b,4),embunit)));
    2121             : }
    2122             : /* B a zsimp */
    2123             : static GEN
    2124          28 : bnrclassnointern(GEN B, GEN h)
    2125             : {
    2126          28 :   long lx = lg(B), j;
    2127          28 :   GEN L = cgetg(lx,t_VEC);
    2128          56 :   for (j=1; j<lx; j++)
    2129             :   {
    2130          28 :     GEN b = gel(B,j), qm = ZM_mul(gel(b,3),gel(b,4));
    2131          28 :     GEN m = ZM_det_triangular( ZM_hnfmodid(qm, gel(b,2)) );
    2132          28 :     gel(L,j) = mkvec2(gel(b,1), mkvecsmall( itou( mulii(h, m) ) ));
    2133             :   }
    2134          28 :   return L;
    2135             : }
    2136             : 
    2137             : static void
    2138        1344 : vecselect_p(GEN A, GEN B, GEN p, long init, long lB)
    2139             : {
    2140        1344 :   long i; setlg(B, lB);
    2141        1344 :   for (i=init; i<lB; i++) B[i] = A[p[i]];
    2142        1344 : }
    2143             : /* B := p . A = row selection according to permutation p. Treat only lower
    2144             :  * right corner init x init */
    2145             : static void
    2146         805 : rowselect_p(GEN A, GEN B, GEN p, long init)
    2147             : {
    2148         805 :   long i, lB = lg(A), lp = lg(p);
    2149         805 :   for (i=1; i<init; i++) setlg(B[i],lp);
    2150         805 :   for (   ; i<lB;   i++) vecselect_p(gel(A,i),gel(B,i),p,init,lp);
    2151         805 : }
    2152             : 
    2153             : static GEN
    2154         280 : bnrclassnointernarch(GEN B, GEN h, GEN matU)
    2155             : {
    2156             :   long lx, nc, k, kk, j, r1, jj, nba, nbarch;
    2157             :   GEN _2, b, qm, L, cyc, m, H, mm, rowsel;
    2158             : 
    2159         280 :   if (!matU) return bnrclassnointern(B,h);
    2160         252 :   lx = lg(B); if (lx == 1) return B;
    2161             : 
    2162         154 :   r1 = nbrows(matU); _2 = const_vec(r1, gen_2);
    2163         154 :   L = cgetg(lx,t_VEC); nbarch = 1L<<r1;
    2164         455 :   for (j=1; j<lx; j++)
    2165             :   {
    2166         301 :     b = gel(B,j); qm = ZM_mul(gel(b,3),gel(b,4));
    2167         301 :     cyc = gel(b,2); nc = lg(cyc)-1;
    2168             :     /* [ qm   cyc 0 ]
    2169             :      * [ matU  0  2 ] */
    2170         301 :     m = ZM_hnfmodid(vconcat(qm, matU), shallowconcat(cyc,_2));
    2171         301 :     mm = RgM_shallowcopy(m);
    2172         301 :     H = cgetg(nbarch+1,t_VECSMALL);
    2173         301 :     rowsel = cgetg(nc+r1+1,t_VECSMALL);
    2174        1106 :     for (k = 0; k < nbarch; k++)
    2175             :     {
    2176         805 :       nba = nc+1;
    2177        2149 :       for (kk=k,jj=1; jj<=r1; jj++,kk>>=1)
    2178        1344 :         if (kk&1) rowsel[nba++] = nc + jj;
    2179         805 :       setlg(rowsel, nba);
    2180         805 :       rowselect_p(m, mm, rowsel, nc+1);
    2181         805 :       H[k+1] = itou( mulii(h, ZM_det_triangular(ZM_hnf(mm))) );
    2182             :     }
    2183         301 :     gel(L,j) = mkvec2(gel(b,1), H);
    2184             :   }
    2185         154 :   return L;
    2186             : }
    2187             : 
    2188             : GEN
    2189          21 : decodemodule(GEN nf, GEN fa)
    2190             : {
    2191             :   long n, nn, k;
    2192          21 :   pari_sp av = avma;
    2193             :   GEN G, E, id, pr;
    2194             : 
    2195          21 :   nf = checknf(nf);
    2196          21 :   if (typ(fa)!=t_MAT || lg(fa)!=3)
    2197           0 :     pari_err_TYPE("decodemodule [not a factorization]", fa);
    2198          21 :   n = nf_get_degree(nf); nn = n*n; id = NULL;
    2199          21 :   G = gel(fa,1);
    2200          21 :   E = gel(fa,2);
    2201          35 :   for (k=1; k<lg(G); k++)
    2202             :   {
    2203          14 :     long code = G[k], p = code / nn, j = (code%n)+1;
    2204          14 :     GEN P = idealprimedec(nf, utoipos(p)), e = stoi(E[k]);
    2205          14 :     if (lg(P) <= j) pari_err_BUG("decodemodule [incorrect hash code]");
    2206          14 :     pr = gel(P,j);
    2207          14 :     id = id? idealmulpowprime(nf,id, pr,e)
    2208          14 :            : idealpow(nf, pr,e);
    2209             :   }
    2210          21 :   if (!id) { avma = av; return matid(n); }
    2211          14 :   return gerepileupto(av,id);
    2212             : }
    2213             : 
    2214             : /* List of ray class fields. Do all from scratch, bound < 2^30. No subgroups.
    2215             :  *
    2216             :  * Output: a "big vector" V (cf bigcgetvec). V[k] is a vector indexed by
    2217             :  * the ideals of norm k. Given such an ideal m, the component is as follows:
    2218             :  *
    2219             :  * + if arch = NULL, run through all possible archimedean parts; archs are
    2220             :  * ordered using inverse lexicographic order, [0,..,0], [1,0,..,0], [0,1,..,0],
    2221             :  * Component is [m,V] where V is a vector with 2^r1 entries, giving for each
    2222             :  * arch the triple [N,R1,D], with N, R1, D as in discrayabs; D is in factored
    2223             :  * form.
    2224             :  *
    2225             :  * + otherwise [m,N,R1,D] */
    2226             : GEN
    2227          21 : discrayabslistarch(GEN bnf, GEN arch, ulong bound)
    2228             : {
    2229          21 :   int allarch = (arch==NULL), flbou = 0;
    2230             :   long degk, j, k, l, nba, nbarch, r1, c;
    2231          21 :   pari_sp av0 = avma,  av,  av1;
    2232             :   GEN nf, p, Z, fa, ideal, bidp, matarchunit, Disc, U, sgnU, EMPTY, empty;
    2233             :   GEN res, embunit, h, Ray, discall, idealrel, idealrelinit, fadkabs, BOUND;
    2234             :   ulong i, ii, sqbou;
    2235             :   forprime_t S;
    2236             : 
    2237          21 :   if (bound == 0)
    2238           0 :     pari_err_DOMAIN("discrayabslistarch","bound","==",gen_0,utoi(bound));
    2239          21 :   res = discall = NULL; /* -Wall */
    2240             : 
    2241          21 :   bnf = checkbnf(bnf);
    2242          21 :   nf = bnf_get_nf(bnf); r1 = nf_get_r1(nf);
    2243          21 :   degk = nf_get_degree(nf);
    2244          21 :   fadkabs = absi_factor(nf_get_disc(nf));
    2245          21 :   h = bnf_get_no(bnf);
    2246          21 :   U = init_units(bnf);
    2247          21 :   sgnU = nfsign_units(bnf, NULL, 1);
    2248             : 
    2249          21 :   if (allarch) arch = const_vec(r1, gen_1);
    2250          21 :   bidp = Idealstar(nf, mkvec2(gen_1, arch), nf_INIT);
    2251          21 :   if (allarch) {
    2252          14 :     matarchunit = zlog_units(nf, U, sgnU, bidp);
    2253          14 :     bidp = Idealstar(nf,matid(degk), nf_INIT);
    2254          14 :     if (r1>15) pari_err_IMPL("r1>15 in discrayabslistarch");
    2255          14 :     nba = r1;
    2256             :   } else {
    2257           7 :     matarchunit = NULL;
    2258           7 :     for (nba=0,k=1; k<=r1; k++) if (signe(gel(arch,k))) nba++;
    2259             :   }
    2260             : 
    2261          21 :   empty = cgetg(1,t_VEC);
    2262             :   /* what follows was rewritten from Ideallist */
    2263          21 :   BOUND = utoipos(bound);
    2264          21 :   p = cgetipos(3);
    2265          21 :   u_forprime_init(&S, 2, bound);
    2266          21 :   av = avma;
    2267          21 :   sqbou = (ulong)sqrt((double)bound) + 1;
    2268          21 :   Z = bigcgetvec(bound);
    2269          21 :   for (i=2; i<=bound; i++) bigel(Z,i) = empty;
    2270          21 :   embunit = zlog_units(nf, U, sgnU, bidp);
    2271          21 :   bigel(Z,1) = mkvec(zsimp(bidp,embunit));
    2272          21 :   if (DEBUGLEVEL>1) err_printf("Starting zidealstarunits computations\n");
    2273             :   /* The goal is to compute Ray (lists of bnrclassno). Z contains "zsimps",
    2274             :    * simplified bid, from which bnrclassno is easy to compute.
    2275             :    * Once p > sqbou, delete Z[i] for i > sqbou and compute directly Ray */
    2276          21 :   Ray = Z;
    2277         140 :   while ((p[2] = u_forprime_next(&S)))
    2278             :   {
    2279          98 :     if (!flbou && uel(p,2) > sqbou)
    2280             :     {
    2281             :       GEN z;
    2282          14 :       flbou = 1;
    2283          14 :       if (DEBUGLEVEL>1) err_printf("\nStarting bnrclassno computations\n");
    2284          14 :       Z = gerepilecopy(av,Z); av1 = avma;
    2285          14 :       Ray = bigcgetvec(bound);
    2286         224 :       for (i=1; i<=bound; i++)
    2287         210 :         bigel(Ray,i) = bnrclassnointernarch(bigel(Z,i),h,matarchunit);
    2288          14 :       Ray = gerepilecopy(av1,Ray);
    2289          14 :       z = bigcgetvec(sqbou);
    2290          14 :       for (i=1; i<=sqbou; i++) bigel(z,i) = bigel(Z,i);
    2291          14 :       Z = z;
    2292             :     }
    2293          98 :     fa = idealprimedec_limit_norm(nf,p,BOUND);
    2294         217 :     for (j=1; j<lg(fa); j++)
    2295             :     {
    2296         119 :       GEN pr = gel(fa,j);
    2297         119 :       long prcode, f = pr_get_f(pr);
    2298         119 :       ulong q, Q = upowuu(p[2], f);
    2299             : 
    2300             :       /* p, f-1, j-1 as a single integer in "base degk" (f,j <= degk)*/
    2301         119 :       prcode = (p[2]*degk + f-1)*degk + j-1;
    2302         119 :       q = Q; ideal = pr;
    2303         189 :       for (l=1;; l++) /* Q <= bound */
    2304             :       {
    2305             :         ulong iQ;
    2306         189 :         bidp = Idealstar(nf,ideal, nf_INIT);
    2307         189 :         embunit = zlog_units_noarch(nf, U, bidp);
    2308         637 :         for (iQ = Q, i = 1; iQ <= bound; iQ += Q, i++)
    2309             :         {
    2310         448 :           GEN pz, p2, p1 = bigel(Z,i);
    2311         448 :           long lz = lg(p1);
    2312         448 :           if (lz == 1) continue;
    2313             : 
    2314         322 :           p2 = cgetg(lz,t_VEC); c = 0;
    2315         630 :           for (k=1; k<lz; k++)
    2316             :           {
    2317         385 :             GEN z = gel(p1,k), v = gmael(z,1,1); /* primes in zsimp's fact. */
    2318         385 :             long lv = lg(v);
    2319             :             /* If z has a power of pr in its modulus, skip it */
    2320         385 :             if (i != 1 && lv > 1 && v[lv-1] == prcode) break;
    2321         308 :             gel(p2,++c) = zsimpjoin(z,bidp,embunit,prcode,l);
    2322             :           }
    2323             : 
    2324         322 :           setlg(p2, c+1);
    2325         322 :           pz = bigel(Ray,iQ);
    2326         322 :           if (flbou) p2 = bnrclassnointernarch(p2,h,matarchunit);
    2327         322 :           if (lg(pz) > 1) p2 = shallowconcat(pz,p2);
    2328         322 :           bigel(Ray,iQ) = p2;
    2329             :         }
    2330         189 :         Q = itou_or_0( muluu(Q, q) );
    2331         189 :         if (!Q || Q > bound) break;
    2332             : 
    2333          70 :         ideal = idealmul(nf,ideal,pr);
    2334          70 :       }
    2335             :     }
    2336          98 :     if (gc_needed(av,1))
    2337             :     {
    2338           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"[1]: discrayabslistarch");
    2339           0 :       gerepileall(av, flbou? 2: 1, &Z, &Ray);
    2340             :     }
    2341             :   }
    2342          21 :   if (!flbou) /* occurs iff bound = 1,2,4 */
    2343             :   {
    2344           7 :     if (DEBUGLEVEL>1) err_printf("\nStarting bnrclassno computations\n");
    2345           7 :     Ray = bigcgetvec(bound);
    2346          35 :     for (i=1; i<=bound; i++)
    2347          28 :       bigel(Ray,i) = bnrclassnointernarch(bigel(Z,i),h,matarchunit);
    2348             :   }
    2349          21 :   Ray = gerepilecopy(av, Ray);
    2350             : 
    2351          21 :   if (DEBUGLEVEL>1) err_printf("Starting discrayabs computations\n");
    2352          21 :   if (allarch) nbarch = 1L<<r1;
    2353             :   else
    2354             :   {
    2355           7 :     nbarch = 1;
    2356           7 :     discall = cgetg(2,t_VEC);
    2357             :   }
    2358          21 :   EMPTY = mkvec3(gen_0,gen_0,gen_0);
    2359          21 :   idealrelinit = trivial_fact();
    2360          21 :   av1 = avma;
    2361          21 :   Disc = bigcgetvec(bound);
    2362          21 :   for (i=1; i<=bound; i++) bigel(Disc,i) = empty;
    2363         259 :   for (ii=1; ii<=bound; ii++)
    2364             :   {
    2365             :     GEN sous, sousdisc;
    2366             :     long ls;
    2367         238 :     i = ii;
    2368         238 :     sous = bigel(Ray,i);
    2369         238 :     ls = lg(sous); bigel(Disc,ii) = sousdisc = cgetg(ls,t_VEC);
    2370         567 :     for (j=1; j<ls; j++)
    2371             :     {
    2372         329 :       GEN b = gel(sous,j), clhrayall = gel(b,2), Fa = gel(b,1);
    2373         329 :       GEN P = gel(Fa,1), E = gel(Fa,2);
    2374         329 :       long lP = lg(P), karch;
    2375             : 
    2376         329 :       if (allarch) discall = cgetg(nbarch+1,t_VEC);
    2377        1162 :       for (karch=0; karch<nbarch; karch++)
    2378             :       {
    2379         833 :         long nz, clhray = clhrayall[karch+1];
    2380         833 :         if (allarch)
    2381             :         {
    2382             :           long ka, k2;
    2383         805 :           nba = 0;
    2384        2149 :           for (ka=karch,k=1; k<=r1; k++,ka>>=1)
    2385        1344 :             if (ka & 1) nba++;
    2386        1743 :           for (k2=1,k=1; k<=r1; k++,k2<<=1)
    2387        1232 :             if (karch&k2 && clhrayall[karch-k2+1] == clhray)
    2388         294 :               { res = EMPTY; goto STORE; }
    2389             :         }
    2390         539 :         idealrel = idealrelinit;
    2391         861 :         for (k=1; k<lP; k++) /* cf get_discray */
    2392             :         {
    2393         602 :           long e, ep = E[k], pf = P[k] / degk, f = (pf%degk) + 1, S = 0;
    2394         602 :           ulong normi = i, Npr;
    2395         602 :           p = utoipos(pf / degk);
    2396         602 :           Npr = upowuu(p[2],f);
    2397         980 :           for (e=1; e<=ep; e++)
    2398             :           {
    2399             :             long clhss;
    2400             :             GEN fad;
    2401         665 :             if (e < ep) { E[k] = ep-e; fad = Fa; }
    2402         539 :             else fad = factorsplice(Fa, k);
    2403         665 :             normi /= Npr;
    2404         665 :             clhss = Lbnrclassno(bigel(Ray,normi),fad)[karch+1];
    2405         665 :             if (e==1 && clhss==clhray) { E[k] = ep; res = EMPTY; goto STORE; }
    2406         385 :             if (clhss == 1) { S += ep-e+1; break; }
    2407         378 :             S += clhss;
    2408             :           }
    2409         322 :           E[k] = ep;
    2410         322 :           idealrel = factormul(idealrel, to_famat_shallow(p, utoi(f * S)));
    2411             :         }
    2412         259 :         if (!allarch && nba)
    2413          14 :           nz = get_nz(bnf, decodemodule(nf,Fa), arch, clhray);
    2414             :         else
    2415         245 :           nz = r1 - nba;
    2416         259 :         res = get_NR1D(i, clhray, degk, nz, fadkabs, idealrel);
    2417         833 : STORE:  gel(discall,karch+1) = res;
    2418             :       }
    2419         329 :       res = allarch? mkvec2(Fa, discall)
    2420         329 :                    : mkvec4(Fa, gel(res,1), gel(res,2), gel(res,3));
    2421         329 :       gel(sousdisc,j) = res;
    2422         329 :       if (gc_needed(av1,1))
    2423             :       {
    2424             :         long jj;
    2425           0 :         if(DEBUGMEM>1) pari_warn(warnmem,"[2]: discrayabslistarch");
    2426           0 :         for (jj=j+1; jj<ls; jj++) gel(sousdisc,jj) = gen_0; /* dummy */
    2427           0 :         Disc = gerepilecopy(av1, Disc);
    2428           0 :         sousdisc = bigel(Disc,ii);
    2429             :       }
    2430             :     }
    2431             :   }
    2432          21 :   return gerepilecopy(av0, Disc);
    2433             : }
    2434             : GEN
    2435           0 : discrayabslistlong(GEN bnf, ulong bound) {
    2436           0 :   GEN nf = checknf(bnf);
    2437           0 :   long r1 = nf_get_r1(nf);
    2438           0 :   return discrayabslistarch(bnf,zerovec(r1),bound);
    2439             : }
    2440             : 
    2441             : int
    2442        1435 : subgroup_conductor_ok(GEN H, GEN L)
    2443             : { /* test conductor */
    2444        1435 :   long i, l = lg(L);
    2445        3920 :   for (i = 1; i < l; i++)
    2446        3003 :     if ( hnf_solve(H, gel(L,i)) ) return 0;
    2447         917 :   return 1;
    2448             : }
    2449             : static GEN
    2450         483 : conductor_elts(GEN bnr)
    2451             : {
    2452         483 :   GEN e, L, nf = bnf_get_nf( bnr_get_bnf(bnr) );
    2453             :   long le, la, i, k;
    2454             :   zlog_S S;
    2455             : 
    2456         483 :   init_zlog_bid(&S, bnr_get_bid(bnr));
    2457         483 :   e = S.e; le = lg(e); la = lg(S.archp);
    2458         483 :   L = cgetg(le + la - 1, t_VEC);
    2459         483 :   i = 1;
    2460        1050 :   for (k = 1; k < le; k++)
    2461         567 :     gel(L,i++) = bnr_log_gen_pr(bnr, &S, nf, itos(gel(e,k)), k);
    2462        1071 :   for (k = 1; k < la; k++)
    2463         588 :     gel(L,i++) = bnr_log_gen_arch(bnr, &S, k);
    2464         483 :   return L;
    2465             : }
    2466             : 
    2467             : /* Let C a congruence group in bnr, compute its subgroups whose index is
    2468             :  * described by bound (see subgrouplist) as subgroups of Clk(bnr).
    2469             :  * Restrict to subgroups having the same conductor as bnr */
    2470             : GEN
    2471         448 : subgrouplist_cond_sub(GEN bnr, GEN C, GEN bound)
    2472             : {
    2473         448 :   pari_sp av = avma;
    2474             :   long l, i, j;
    2475         448 :   GEN D, Mr, U, T, subgrp, L, cyc = bnr_get_cyc(bnr);
    2476             : 
    2477         448 :   Mr = diagonal_shallow(cyc);
    2478         448 :   D = ZM_snfall_i(hnf_solve(C, Mr), &U, NULL, 1);
    2479         448 :   T = ZM_mul(C, RgM_inv(U));
    2480         448 :   L = conductor_elts(bnr);
    2481         448 :   subgrp  = subgrouplist(D, bound);
    2482         448 :   l = lg(subgrp);
    2483         952 :   for (i = j = 1; i < l; i++)
    2484             :   {
    2485         504 :     GEN H = ZM_hnfmodid(ZM_mul(T, gel(subgrp,i)), cyc);
    2486         504 :     if (subgroup_conductor_ok(H, L)) gel(subgrp, j++) = H;
    2487             :   }
    2488         448 :   setlg(subgrp, j);
    2489         448 :   return gerepilecopy(av, subgrp);
    2490             : }
    2491             : 
    2492             : static GEN
    2493          35 : subgroupcond(GEN bnr, GEN indexbound)
    2494             : {
    2495          35 :   pari_sp av = avma;
    2496          35 :   GEN li = subgroupcondlist(bnr_get_cyc(bnr), indexbound, conductor_elts(bnr));
    2497          35 :   if (indexbound && typ(indexbound) != t_VEC)
    2498             :   { /* sort by increasing index if not single value */
    2499          21 :     long i, l = lg(li);
    2500          21 :     GEN D = cgetg(l,t_VEC);
    2501          21 :     for (i=1; i<l; i++) gel(D,i) = ZM_det_triangular(gel(li,i));
    2502          21 :     li = vecreverse( vecpermute(li, indexsort(D)) );
    2503             :   }
    2504          35 :   return gerepilecopy(av,li);
    2505             : }
    2506             : 
    2507             : GEN
    2508          77 : subgrouplist0(GEN bnr, GEN indexbound, long all)
    2509             : {
    2510          77 :   if (typ(bnr)!=t_VEC) pari_err_TYPE("subgrouplist",bnr);
    2511          70 :   if (lg(bnr)!=1 && typ(gel(bnr,1))!=t_INT)
    2512             :   {
    2513          42 :     checkbnr(bnr);
    2514          42 :     if (!all) return subgroupcond(bnr,indexbound);
    2515           7 :     bnr = bnr_get_cyc(bnr);
    2516             :   }
    2517          35 :   return subgrouplist(bnr,indexbound);
    2518             : }
    2519             : 
    2520             : GEN
    2521          42 : bnrdisclist0(GEN bnf, GEN L, GEN arch)
    2522             : {
    2523          42 :   if (typ(L)!=t_INT) return discrayabslist(bnf,L);
    2524          21 :   return discrayabslistarch(bnf,arch,itos(L));
    2525             : }
    2526             : 
    2527             : /****************************************************************************/
    2528             : /*                                Galois action on a BNR                    */
    2529             : /****************************************************************************/
    2530             : 
    2531             : GEN
    2532         462 : bnrautmatrix(GEN bnr, GEN aut)
    2533             : {
    2534         462 :   pari_sp av=avma;
    2535             :   GEN gen, mat, nf;
    2536             :   long i, l;
    2537         462 :   nf = bnr_get_nf(bnr);
    2538         462 :   gen = bnr_get_gen(bnr); l = lg(gen);
    2539         462 :   aut = algtobasis(nf, aut);
    2540         462 :   mat = cgetg(l,t_MAT);
    2541        2310 :   for (i=1; i<l; i++)
    2542        1848 :     gel(mat, i) = bnrisprincipal(bnr,galoisapply(nf,aut,gel(gen,i)),0);
    2543         462 :   return gerepilecopy(av, mat);
    2544             : }
    2545             : 
    2546             : GEN
    2547         238 : bnrgaloismatrix(GEN bnr, GEN aut)
    2548             : {
    2549         238 :   checkbnr(bnr);
    2550         238 :   switch (typ(aut))
    2551             :   {
    2552             :     case t_POL:
    2553             :     case t_COL:
    2554           0 :       return bnrautmatrix(bnr, aut);
    2555             :     case t_VEC:
    2556             :     {
    2557         238 :       long i, l = lg(aut);
    2558             :       GEN V;
    2559         238 :       if (l==9 && typ(gal_get_gen(aut))==t_VEC)
    2560             :       {
    2561           7 :         pari_sp av = avma;
    2562           7 :         V = galoispermtopol(aut, gal_get_gen(aut));
    2563           7 :         return gerepileupto(av, bnrgaloismatrix(bnr, V));
    2564             :       }
    2565         231 :       V = cgetg(l, t_VEC);
    2566         693 :       for(i=1; i<l; i++)
    2567         462 :         gel(V,i) = bnrautmatrix(bnr, gel(aut,i));
    2568         231 :       return V;
    2569             :     }
    2570             :     default:
    2571           0 :       pari_err_TYPE("bnrgaloismatrix", aut);
    2572           0 :       return NULL; /*NOT REACHED*/
    2573             :   }
    2574             : }
    2575             : 
    2576             : GEN
    2577         560 : bnrgaloisapply(GEN bnr, GEN mat, GEN x)
    2578             : {
    2579         560 :   pari_sp av=avma;
    2580             :   GEN cyc;
    2581         560 :   checkbnr(bnr);
    2582         560 :   cyc = bnr_get_cyc(bnr);
    2583         560 :   if (typ(mat)!=t_MAT || !RgM_is_ZM(mat))
    2584           0 :     pari_err_TYPE("bnrgaloisapply",mat);
    2585         560 :   if (typ(x)!=t_MAT || !RgM_is_ZM(x))
    2586           0 :     pari_err_TYPE("bnrgaloisapply",x);
    2587         560 :   return gerepileupto(av, ZM_hnfmodid(ZM_mul(mat, x), cyc));
    2588             : }
    2589             : 
    2590             : static GEN
    2591         448 : check_bnrgal(GEN bnr, GEN M)
    2592             : {
    2593         448 :   checkbnr(bnr);
    2594         448 :   if (typ(M)==t_MAT)
    2595           0 :     return mkvec(M);
    2596         448 :   else if (typ(M)==t_VEC && lg(M)==9 && typ(gal_get_gen(M))==t_VEC)
    2597             :   {
    2598         224 :     pari_sp av = avma;
    2599         224 :     GEN V = galoispermtopol(M, gal_get_gen(M));
    2600         224 :     return gerepileupto(av, bnrgaloismatrix(bnr, V));
    2601             :   }
    2602         224 :   else if (!is_vec_t(typ(M)))
    2603           0 :     pari_err_TYPE("bnrisgalois",M);
    2604         224 :   return M;
    2605             : }
    2606             : 
    2607             : long
    2608         448 : bnrisgalois(GEN bnr, GEN M, GEN H)
    2609             : {
    2610         448 :   pari_sp av = avma;
    2611             :   long i, l;
    2612         448 :   if (typ(H)!=t_MAT || !RgM_is_ZM(H))
    2613           0 :     pari_err_TYPE("bnrisgalois",H);
    2614         448 :   M = check_bnrgal(bnr, M); l = lg(M);
    2615         616 :   for (i=1; i<l; i++)
    2616             :   {
    2617         560 :     long res = ZM_equal(bnrgaloisapply(bnr,gel(M,i), H), H);
    2618         560 :     if (!res) { avma = av; return 0; }
    2619             :   }
    2620          56 :   avma = av;
    2621          56 :   return 1;
    2622             : }

Generated by: LCOV version 1.11