Code coverage tests

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

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

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

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

Generated by: LCOV version 1.11