Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is to exceed 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - basemath - buch3.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.1 lcov report (development 24988-2584e74448) Lines: 1400 1478 94.7 %
Date: 2020-01-26 05:57:03 Functions: 104 108 96.3 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13