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

Generated by: LCOV version 1.13