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

Generated by: LCOV version 1.13