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

Generated by: LCOV version 1.11