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

Generated by: LCOV version 1.13