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 - modules - stark.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 20459-9710128) Lines: 1806 1947 92.8 %
Date: 2017-03-30 05:32:39 Functions: 126 127 99.2 %
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             : /*        COMPUTATION OF STARK UNITS OF TOTALLY REAL FIELDS        */
      17             : /*                                                                 */
      18             : /*******************************************************************/
      19             : #include "pari.h"
      20             : #include "paripriv.h"
      21             : 
      22             : static const long EXTRA_PREC = DEFAULTPREC-2;
      23             : 
      24             : /* ComputeCoeff */
      25             : typedef struct {
      26             :   GEN L0, L1, L11, L2; /* VECSMALL of p */
      27             :   GEN L1ray, L11ray; /* precomputed isprincipalray(pr), pr | p */
      28             :   GEN rayZ; /* precomputed isprincipalray(i), i < condZ */
      29             :   long condZ; /* generates cond(bnr) \cap Z, assumed small */
      30             : } LISTray;
      31             : 
      32             : /* Char evaluation */
      33             : typedef struct {
      34             :   long ord;
      35             :   GEN *val, chi;
      36             : } CHI_t;
      37             : 
      38             : /* RecCoeff */
      39             : typedef struct {
      40             :   GEN M, beta, B, U, nB;
      41             :   long v, G, N;
      42             : } RC_data;
      43             : 
      44             : /********************************************************************/
      45             : /*                    Miscellaneous functions                       */
      46             : /********************************************************************/
      47             : static GEN
      48       16254 : chi_get_c(GEN chi) { return gmael(chi,1,2); }
      49             : static GEN
      50       50004 : chi_get_gdeg(GEN chi) { return gmael(chi,1,1); }
      51             : static long
      52       50004 : chi_get_deg(GEN chi) { return itou(chi_get_gdeg(chi)); }
      53             : 
      54             : /* Compute the image of logelt by character chi, as a complex number */
      55             : static ulong
      56       12744 : CharEval_n(GEN chi, GEN logelt)
      57             : {
      58       12744 :   GEN gn = ZV_dotproduct(chi_get_c(chi), logelt);
      59       12744 :   return umodiu(gn, chi_get_deg(chi));
      60             : }
      61             : /* Compute the image of logelt by character chi, as a complex number */
      62             : static GEN
      63       12630 : CharEval(GEN chi, GEN logelt)
      64             : {
      65       12630 :   ulong n = CharEval_n(chi, logelt), d = chi_get_deg(chi);
      66       12630 :   long nn = Fl_center(n,d,d>>1);
      67       12630 :   GEN x = gel(chi,2);
      68       12630 :   x = gpowgs(x, labs(nn));
      69       12630 :   if (nn < 0) x = gconj(x);
      70       12630 :   return x;
      71             : }
      72             : 
      73             : /* return n such that C(elt) = z^n */
      74             : static ulong
      75      580314 : CHI_eval_n(CHI_t *C, GEN logelt)
      76             : {
      77      580314 :   GEN n = ZV_dotproduct(C->chi, logelt);
      78      580314 :   return umodiu(n, C->ord);
      79             : }
      80             : /* return C(elt) */
      81             : static GEN
      82      578778 : CHI_eval(CHI_t *C, GEN logelt)
      83             : {
      84      578778 :   return C->val[CHI_eval_n(C, logelt)];
      85             : }
      86             : 
      87             : static void
      88        3510 : init_CHI(CHI_t *c, GEN CHI, GEN z)
      89             : {
      90        3510 :   long i, d = chi_get_deg(CHI);
      91        3510 :   GEN *v = (GEN*)new_chunk(d);
      92        3510 :   v[0] = gen_1;
      93        3510 :   if (d != 1)
      94             :   {
      95        3510 :     v[1] = z;
      96        3510 :     for (i=2; i<d; i++) v[i] = gmul(v[i-1], z);
      97             :   }
      98        3510 :   c->chi = chi_get_c(CHI);
      99        3510 :   c->ord = d;
     100        3510 :   c->val = v;
     101        3510 : }
     102             : /* as t_POLMOD */
     103             : static void
     104        2196 : init_CHI_alg(CHI_t *c, GEN CHI) {
     105        2196 :   long d = chi_get_deg(CHI);
     106             :   GEN z;
     107        2196 :   switch(d)
     108             :   {
     109           0 :     case 1: z = gen_1; break;
     110         888 :     case 2: z = gen_m1; break;
     111        1308 :     default: z = mkpolmod(pol_x(0), polcyclo(d,0));
     112             :   }
     113        2196 :   init_CHI(c,CHI, z);
     114        2196 : }
     115             : /* as t_COMPLEX */
     116             : static void
     117        1314 : init_CHI_C(CHI_t *c, GEN CHI) {
     118        1314 :   init_CHI(c,CHI, gel(CHI,2));
     119        1314 : }
     120             : 
     121             : typedef struct {
     122             :   long r; /* rank = lg(gen) */
     123             :   GEN j; /* current elt is gen[1]^j[1] ... gen[r]^j[r] */
     124             :   GEN cyc; /* t_VECSMALL of elementary divisors */
     125             : } GROUP_t;
     126             : 
     127             : static int
     128       60198 : NextElt(GROUP_t *G)
     129             : {
     130       60198 :   long i = 1;
     131       60198 :   if (G->r == 0) return 0; /* no more elt */
     132      120912 :   while (++G->j[i] == G->cyc[i]) /* from 0 to cyc[i]-1 */
     133             :   {
     134        1314 :     G->j[i] = 0;
     135        1314 :     if (++i > G->r) return 0; /* no more elt */
     136             :   }
     137       59514 :   return i; /* we have multiplied by gen[i] */
     138             : }
     139             : 
     140             : /* Compute all the elements of a group given by its SNF */
     141             : static GEN
     142         894 : EltsOfGroup(long order, GEN cyc)
     143             : {
     144             :   long i;
     145             :   GEN rep;
     146             :   GROUP_t G;
     147             : 
     148         894 :   G.cyc = gtovecsmall(cyc);
     149         894 :   G.r = lg(cyc)-1;
     150         894 :   G.j = zero_zv(G.r);
     151             : 
     152         894 :   rep = cgetg(order + 1, t_VEC);
     153         894 :   gel(rep,order) = vecsmall_to_col(G.j);
     154             : 
     155        6318 :   for  (i = 1; i < order; i++)
     156             :   {
     157        5424 :     (void)NextElt(&G);
     158        5424 :     gel(rep,i) = vecsmall_to_col(G.j);
     159             :   }
     160         894 :   return rep;
     161             : }
     162             : 
     163             : /* enumerate all group elements */
     164             : GEN
     165         342 : cyc2elts(GEN cyc)
     166             : {
     167             :   long i, n;
     168             :   GEN rep;
     169             :   GROUP_t G;
     170             : 
     171         342 :   G.cyc = typ(cyc)==t_VECSMALL? cyc: gtovecsmall(cyc);
     172         342 :   n = zv_prod(G.cyc);
     173         342 :   G.r = lg(cyc)-1;
     174         342 :   G.j = zero_zv(G.r);
     175             : 
     176         342 :   rep = cgetg(n+1, t_VEC);
     177         342 :   gel(rep,n) = leafcopy(G.j); /* trivial elt comes last */
     178        1110 :   for  (i = 1; i < n; i++)
     179             :   {
     180         768 :     (void)NextElt(&G);
     181         768 :     gel(rep,i) = leafcopy(G.j);
     182             :   }
     183         342 :   return rep;
     184             : }
     185             : 
     186             : /* Let Qt as given by InitQuotient, compute a system of
     187             :    representatives of the quotient */
     188             : static GEN
     189         570 : ComputeLift(GEN Qt)
     190             : {
     191         570 :   GEN e, U = gel(Qt,3);
     192         570 :   long i, h = itos(gel(Qt,1));
     193             : 
     194         570 :   e = EltsOfGroup(h, gel(Qt,2));
     195         570 :   if (!RgM_isidentity(U))
     196             :   {
     197          24 :     GEN Ui = ZM_inv(U, gen_1);
     198          24 :     for (i = 1; i <= h; i++) gel(e,i) = ZM_ZC_mul(Ui, gel(e,i));
     199             :   }
     200         570 :   return e;
     201             : }
     202             : 
     203             : /* nchi: a character given by a vector [d, (c_i)], e.g. from char_normalize
     204             :  * such that chi(x) = e((c . log(x)) / d) where log(x) on bnr.gen */
     205             : static GEN
     206        2424 : get_Char(GEN nchi, long prec)
     207        2424 : { return mkvec2(nchi, rootsof1_cx(gel(nchi,1), prec)); }
     208             : 
     209             : /* prime divisors of conductor */
     210             : static GEN
     211         288 : divcond(GEN bnr) {GEN bid = bnr_get_bid(bnr); return gel(bid_get_fact(bid),1);}
     212             : 
     213             : /* vector of prime ideals dividing bnr but not bnrc */
     214             : static GEN
     215         180 : get_prdiff(GEN bnr, GEN condc)
     216             : {
     217         180 :   GEN prdiff, M = gel(condc,1), D = divcond(bnr), nf = bnr_get_nf(bnr);
     218         180 :   long nd, i, l  = lg(D);
     219         180 :   prdiff = cgetg(l, t_COL);
     220         528 :   for (nd=1, i=1; i < l; i++)
     221         348 :     if (!idealval(nf, M, gel(D,i))) gel(prdiff,nd++) = gel(D,i);
     222         180 :   setlg(prdiff, nd); return prdiff;
     223             : }
     224             : 
     225             : #define ch_C(x)    gel(x,1)
     226             : #define ch_bnr(x)  gel(x,2)
     227             : #define ch_4(x)    gel(x,3)
     228             : #define ch_CHI(x)  gel(x,4)
     229             : #define ch_diff(x) gel(x,5)
     230             : #define ch_cond(x) gel(x,6)
     231             : #define ch_CHI0(x) gel(x,7)
     232             : #define ch_comp(x) gel(x,8)
     233             : static long
     234        3126 : ch_deg(GEN dtcr) { return chi_get_deg(ch_CHI(dtcr)); }
     235             : 
     236             : static GEN
     237         924 : GetDeg(GEN dataCR)
     238             : {
     239         924 :   long i, l = lg(dataCR);
     240         924 :   GEN degs = cgetg(l, t_VECSMALL);
     241         924 :   for (i = 1; i < l; i++) degs[i] = eulerphiu(ch_deg(gel(dataCR,i)));
     242         924 :   return degs;
     243             : }
     244             : 
     245             : /********************************************************************/
     246             : /*                    1rst part: find the field K                   */
     247             : /********************************************************************/
     248             : static GEN AllStark(GEN data, GEN nf, long flag, long prec);
     249             : 
     250             : /* Columns of C [HNF] give the generators of a subgroup of the finite abelian
     251             :  * group A [ in terms of implicit generators ], compute data to work in A/C:
     252             :  * 1) order
     253             :  * 2) structure
     254             :  * 3) the matrix A ->> A/C
     255             :  * 4) the subgroup C */
     256             : static GEN
     257        1764 : InitQuotient(GEN C)
     258             : {
     259        1764 :   GEN U, D = ZM_snfall_i(C, &U, NULL, 1), h = ZV_prod(D);
     260        1764 :   return mkvec5(h, D, U, C, cyc_normalize(D));
     261             : }
     262             : 
     263             : /* lift chi character on A/C [Qt from InitQuotient] to character on A [cyc]*/
     264             : static GEN
     265        1986 : LiftChar(GEN Qt, GEN cyc, GEN chi)
     266             : {
     267        1986 :   GEN ncyc = gel(Qt,5), U = gel(Qt,3);
     268        1986 :   GEN nchi = char_normalize(chi, ncyc);
     269        1986 :   GEN c = ZV_ZM_mul(gel(nchi,2), U), d = gel(nchi,1);
     270        1986 :   return char_denormalize(cyc, d, c);
     271             : }
     272             : 
     273             : /* Let s: A -> B given by P, and let cycA, cycB be the cyclic structure of
     274             :  * A and B, compute the kernel of s. */
     275             : static GEN
     276         360 : ComputeKernel0(GEN P, GEN cycA, GEN cycB)
     277             : {
     278         360 :   pari_sp av = avma;
     279         360 :   long nbA = lg(cycA)-1, rk;
     280         360 :   GEN U, DB = diagonal_shallow(cycB);
     281             : 
     282         360 :   rk = nbA + lg(cycB) - lg(ZM_hnfall_i(shallowconcat(P, DB), &U, 1));
     283         360 :   U = matslice(U, 1,nbA, 1,rk);
     284         360 :   return gerepileupto(av, ZM_hnfmodid(U, cycA));
     285             : }
     286             : 
     287             : /* Let m and n be two moduli such that n|m and let C be a congruence
     288             :    group modulo n, compute the corresponding congruence group modulo m
     289             :    ie the kernel of the map Clk(m) ->> Clk(n)/C */
     290             : static GEN
     291         360 : ComputeKernel(GEN bnrm, GEN bnrn, GEN dtQ)
     292             : {
     293         360 :   pari_sp av = avma;
     294         360 :   GEN P = ZM_mul(gel(dtQ,3), bnrsurjection(bnrm, bnrn));
     295         360 :   return gerepileupto(av, ComputeKernel0(P, bnr_get_cyc(bnrm), gel(dtQ,2)));
     296             : }
     297             : 
     298             : static long
     299        1110 : cyc_is_cyclic(GEN cyc) { return lg(cyc) <= 2 || equali1(gel(cyc,2)); }
     300             : 
     301             : /* Let H be a subgroup of cl(bnr)/sugbroup, return 1 if
     302             :    cl(bnr)/subgoup/H is cyclic and the signature of the
     303             :    corresponding field is equal to sig and no finite prime
     304             :    dividing cond(bnr) is totally split in this field. Return 0
     305             :    otherwise. */
     306             : static long
     307         552 : IsGoodSubgroup(GEN H, GEN bnr, GEN map)
     308             : {
     309         552 :   pari_sp av = avma;
     310             :   GEN mod, modH, p1, p2, U, P, PH, bnrH, iH, qH;
     311             :   long j;
     312             : 
     313         552 :   p1 = InitQuotient(H);
     314             :   /* quotient is non cyclic */
     315         552 :   if (!cyc_is_cyclic(gel(p1,2))) { avma = av; return 0; }
     316             : 
     317         252 :   p2 = ZM_hnfall_i(shallowconcat(map,H), &U, 0);
     318         252 :   setlg(U, lg(H));
     319         252 :   for (j = 1; j < lg(U); j++) setlg(gel(U,j), lg(H));
     320         252 :   p1 = ZM_hnfmodid(U, bnr_get_cyc(bnr)); /* H as a subgroup of bnr */
     321         252 :   modH = bnrconductor_i(bnr, p1, 0);
     322         252 :   mod  = bnr_get_mod(bnr);
     323             : 
     324             :   /* is the signature correct? */
     325         252 :   if (!gequal(gel(modH,2), gel(mod,2))) { avma = av; return 0; }
     326             : 
     327             :   /* finite part are the same: OK */
     328         174 :   if (gequal(gel(modH,1), gel(mod,1))) { avma = av; return 1; }
     329             : 
     330             :   /* need to check the splitting of primes dividing mod but not modH */
     331          54 :   bnrH = Buchray(bnr, modH, nf_INIT);
     332          54 :   P = divcond(bnr);
     333          54 :   PH = divcond(bnrH);
     334          54 :   p2 = ZM_mul(bnrsurjection(bnr, bnrH), p1);
     335             :   /* H as a subgroup of bnrH */
     336          54 :   iH = ZM_hnfmodid(p2,  bnr_get_cyc(bnrH));
     337          54 :   qH = InitQuotient(iH);
     338         174 :   for (j = 1; j < lg(P); j++)
     339             :   {
     340         138 :     GEN pr = gel(P, j), e;
     341             :     /* if pr divides modH, it is ramified, so it's good */
     342         138 :     if (tablesearch(PH, pr, cmp_prime_ideal)) continue;
     343             :     /* inertia degree of pr in bnr(modH)/H is charorder(e, cycH) */
     344          48 :     e = ZM_ZC_mul(gel(qH,3), isprincipalray(bnrH, pr));
     345          48 :     e = vecmodii(e, gel(qH,2));
     346          48 :     if (ZV_equal0(e)) { avma = av; return 0; } /* f = 1 */
     347             :   }
     348          36 :   avma = av; return 1;
     349             : }
     350             : 
     351             : /* compute the list of characters to consider for AllStark and
     352             :    initialize precision-independent data to compute with them */
     353             : static GEN
     354         276 : get_listCR(GEN bnr, GEN dtQ)
     355             : {
     356             :   GEN listCR, vecchi, Mr;
     357             :   long hD, h, nc, i, tnc;
     358             :   hashtable *S;
     359             : 
     360         276 :   Mr = bnr_get_cyc(bnr);
     361         276 :   hD = itos(gel(dtQ,1));
     362         276 :   h  = hD >> 1;
     363             : 
     364         276 :   listCR = cgetg(h+1, t_VEC); /* non-conjugate chars */
     365         276 :   nc = tnc = 1;
     366         276 :   vecchi = EltsOfGroup(hD, gel(dtQ,2));
     367         276 :   S = hash_create(h, (ulong(*)(void*))&hash_GEN,
     368             :                      (int(*)(void*,void*))&ZV_equal, 1);
     369        1962 :   for (i = 1; tnc <= h; i++)
     370             :   { /* lift a character of D in Clk(m) */
     371        1686 :     GEN cond, lchi = LiftChar(dtQ, Mr, gel(vecchi,i));
     372        1686 :     if (hash_search(S, lchi)) continue;
     373        1656 :     cond = bnrconductorofchar(bnr, lchi);
     374        1656 :     if (gequal0(gel(cond,2))) continue;
     375             :     /* the infinite part of chi is non trivial */
     376         900 :     gel(listCR,nc++) = mkvec2(lchi, cond);
     377             : 
     378             :     /* if chi is not real, add its conjugate character to S */
     379         900 :     if (absequaliu(charorder(Mr,lchi), 2)) tnc++;
     380             :     else
     381             :     {
     382         564 :       hash_insert(S, charconj(Mr, lchi), (void*)1);
     383         564 :       tnc+=2;
     384             :     }
     385             :   }
     386         276 :   setlg(listCR, nc); return listCR;
     387             : }
     388             : 
     389             : static GEN InitChar(GEN bnr, GEN listCR, long prec);
     390             : 
     391             : /* Given a conductor and a subgroups, return the corresponding
     392             :    complexity and precision required using quickpol. Fill data[5] with
     393             :    listCR */
     394             : static long
     395         276 : CplxModulus(GEN data, long *newprec)
     396             : {
     397         276 :   long pr, ex, dprec = DEFAULTPREC;
     398             :   pari_sp av;
     399         276 :   GEN pol, listCR, cpl, bnr = gel(data,1), nf = checknf(bnr);
     400             : 
     401         276 :   listCR = get_listCR(bnr, gel(data,3));
     402         276 :   for (av = avma;; avma = av)
     403             :   {
     404         276 :     gel(data,5) = InitChar(bnr, listCR, dprec);
     405         276 :     pol = AllStark(data, nf, -1, dprec);
     406         276 :     pr = nbits2extraprec( gexpo(pol) );
     407         276 :     if (pr < 0) pr = 0;
     408         276 :     dprec = maxss(dprec, pr) + EXTRA_PREC;
     409         276 :     if (!gequal0(leading_coeff(pol)))
     410             :     {
     411         276 :       cpl = RgX_fpnorml2(pol, DEFAULTPREC);
     412         276 :       if (!gequal0(cpl)) break;
     413             :     }
     414           0 :     if (DEBUGLEVEL>1) pari_warn(warnprec, "CplxModulus", dprec);
     415           0 :   }
     416         276 :   ex = gexpo(cpl); avma = av;
     417         276 :   if (DEBUGLEVEL>1) err_printf("cpl = 2^%ld\n", ex);
     418             : 
     419         276 :   gel(data,5) = listCR;
     420         276 :   *newprec = dprec; return ex;
     421             : }
     422             : 
     423             : /* return A \cap B in abelian group defined by cyc. NULL = whole group */
     424             : static GEN
     425         594 : subgp_intersect(GEN cyc, GEN A, GEN B)
     426             : {
     427             :   GEN H, U;
     428             :   long k, lH;
     429         594 :   if (!A) return B;
     430         258 :   if (!B) return A;
     431         258 :   H = ZM_hnfall_i(shallowconcat(A,B), &U, 1);
     432         258 :   setlg(U, lg(A)); lH = lg(H);
     433         258 :   for (k = 1; k < lg(U); k++) setlg(gel(U,k), lH);
     434         258 :   return ZM_hnfmodid(ZM_mul(A,U), cyc);
     435             : }
     436             : 
     437             :  /* Let f be a conductor without infinite part and let C be a
     438             :    congruence group modulo f, compute (m,D) such that D is a
     439             :    congruence group of conductor m where m is a multiple of f
     440             :    divisible by all the infinite places but one, D is a subgroup of
     441             :    index 2 of Im(C) in Clk(m), and m is such that the intersection
     442             :    of the subgroups H of Clk(m)/D such that the quotient is
     443             :    cyclic and no prime divding m, but the one infinite prime, is
     444             :    totally split in the extension corresponding to H is trivial.
     445             :    Return bnr(m), D, the quotient Ck(m)/D and Clk(m)/Im(C) */
     446             : static GEN
     447         276 : FindModulus(GEN bnr, GEN dtQ, long *newprec)
     448             : {
     449         276 :   const long limnorm = 400;
     450             :   long n, i, narch, maxnorm, minnorm, N;
     451         276 :   long first = 1, pr, rb, oldcpl = -1, iscyc;
     452         276 :   pari_sp av = avma;
     453         276 :   GEN bnf, nf, f, arch, m, rep = NULL;
     454             : 
     455         276 :   bnf = bnr_get_bnf(bnr);
     456         276 :   nf  = bnf_get_nf(bnf);
     457         276 :   N   = nf_get_degree(nf);
     458         276 :   f   = gel(bnr_get_mod(bnr), 1);
     459             : 
     460             :   /* if cpl < rb, it is not necessary to try another modulus */
     461         276 :   rb = expi( powii(mulii(nf_get_disc(nf), ZM_det_triangular(f)), gmul2n(bnr_get_no(bnr), 3)) );
     462             : 
     463             :   /* Initialization of the possible infinite part */
     464         276 :   arch = const_vec(N, gen_1);
     465             : 
     466             :   /* narch = (N == 2)? 1: N; -- if N=2, only one case is necessary */
     467         276 :   narch = N;
     468         276 :   m = mkvec2(NULL, arch);
     469             : 
     470             :   /* go from minnorm up to maxnorm. If necessary, increase these values.
     471             :    * If we cannot find a suitable conductor of norm < limnorm, stop */
     472         276 :   maxnorm = 50;
     473         276 :   minnorm = 1;
     474             : 
     475             :   /* if the extension is cyclic then we _must_ find a suitable conductor */
     476         276 :   iscyc = cyc_is_cyclic(gel(dtQ,2));
     477             : 
     478         276 :   if (DEBUGLEVEL>1)
     479           0 :     err_printf("Looking for a modulus of norm: ");
     480             : 
     481             :   for(;;)
     482             :   {
     483         276 :     GEN listid = ideallist0(nf, maxnorm, 4+8); /* ideals of norm <= maxnorm */
     484         276 :     pari_sp av1 = avma;
     485         966 :     for (n = minnorm; n <= maxnorm; n++, avma = av1)
     486             :     {
     487         966 :       GEN idnormn = gel(listid,n);
     488         966 :       long nbidnn  = lg(idnormn) - 1;
     489         966 :       if (DEBUGLEVEL>1) err_printf(" %ld", n);
     490        1554 :       for (i = 1; i <= nbidnn; i++)
     491             :       { /* finite part of the conductor */
     492             :         long s;
     493             : 
     494         864 :         gel(m,1) = idealmul(nf, f, gel(idnormn,i));
     495        2304 :         for (s = 1; s <= narch; s++)
     496             :         { /* infinite part */
     497             :           GEN candD, ImC, bnrm;
     498             :           long nbcand, c;
     499        1716 :           gel(arch,N+1-s) = gen_0;
     500             : 
     501             :           /* compute Clk(m), check if m is a conductor */
     502        1716 :           bnrm = Buchray(bnf, m, nf_INIT);
     503        1716 :           c = bnrisconductor(bnrm, NULL);
     504        1716 :           gel(arch,N+1-s) = gen_1;
     505        1716 :           if (!c) continue;
     506             : 
     507             :           /* compute Im(C) in Clk(m)... */
     508         360 :           ImC = ComputeKernel(bnrm, bnr, dtQ);
     509             : 
     510             :           /* ... and its subgroups of index 2 with conductor m */
     511         360 :           candD = subgrouplist_cond_sub(bnrm, ImC, mkvec(gen_2));
     512         360 :           nbcand = lg(candD) - 1;
     513         366 :           for (c = 1; c <= nbcand; c++)
     514             :           {
     515         282 :             GEN D  = gel(candD,c); /* check if the conductor is suitable */
     516             :             long cpl;
     517         282 :             GEN p1 = InitQuotient(D), p2;
     518         282 :             GEN ord = gel(p1,1), cyc = gel(p1,2), map = gel(p1,3);
     519             : 
     520         282 :             if (!cyc_is_cyclic(cyc)) /* cyclic => suitable, else test */
     521             :             {
     522          72 :               GEN lH = subgrouplist(cyc, NULL), IK = NULL;
     523          72 :               long j, ok = 0;
     524         600 :               for (j = 1; j < lg(lH); j++)
     525             :               {
     526         594 :                 GEN H = gel(lH, j), IH = subgp_intersect(cyc, IK, H);
     527             :                 /* if H > IK, no need to test H */
     528         594 :                 if (IK && gidentical(IH, IK)) continue;
     529         552 :                 if (IsGoodSubgroup(H, bnrm, map))
     530             :                 {
     531         156 :                   IK = IH;
     532         156 :                   if (equalii(ord, ZM_det_triangular(IK))) { ok = 1; break; }
     533             :                 }
     534             :               }
     535          72 :               if (!ok) continue;
     536             :             }
     537             : 
     538         276 :             p2 = cgetg(6, t_VEC); /* p2[5] filled in CplxModulus */
     539         276 :             gel(p2,1) = bnrm;
     540         276 :             gel(p2,2) = D;
     541         276 :             gel(p2,3) = InitQuotient(D);
     542         276 :             gel(p2,4) = InitQuotient(ImC);
     543         276 :             if (DEBUGLEVEL>1)
     544           0 :               err_printf("\nTrying modulus = %Ps and subgroup = %Ps\n",
     545             :                          bnr_get_mod(bnrm), D);
     546         276 :             cpl = CplxModulus(p2, &pr);
     547         276 :             if (oldcpl < 0 || cpl < oldcpl)
     548             :             {
     549         276 :               *newprec = pr;
     550         276 :               if (rep) gunclone(rep);
     551         276 :               rep    = gclone(p2);
     552         276 :               oldcpl = cpl;
     553             :             }
     554         276 :             if (oldcpl < rb) goto END; /* OK */
     555             : 
     556           0 :             if (DEBUGLEVEL>1) err_printf("Trying to find another modulus...");
     557           0 :             first = 0;
     558             :           }
     559             :         }
     560         588 :         if (!first) goto END; /* OK */
     561             :       }
     562             :     }
     563             :     /* if necessary compute more ideals */
     564           0 :     minnorm = maxnorm;
     565           0 :     maxnorm <<= 1;
     566           0 :     if (!iscyc && maxnorm > limnorm) return NULL;
     567             : 
     568           0 :   }
     569             : END:
     570         276 :   if (DEBUGLEVEL>1)
     571           0 :     err_printf("No, we're done!\nModulus = %Ps and subgroup = %Ps\n",
     572           0 :                bnr_get_mod(gel(rep,1)), gel(rep,2));
     573         276 :   gel(rep,5) = InitChar(gel(rep,1), gel(rep,5), *newprec);
     574         276 :   return gerepilecopy(av, rep);
     575             : }
     576             : 
     577             : /********************************************************************/
     578             : /*                      2nd part: compute W(X)                      */
     579             : /********************************************************************/
     580             : 
     581             : /* find ilambda s.t. Diff*f*ilambda integral and coprime to f
     582             :    and ilambda >> 0 at foo, fa = factorization of f */
     583             : static GEN
     584         684 : get_ilambda(GEN nf, GEN fa, GEN foo)
     585             : {
     586         684 :   GEN x, w, E2, P = gel(fa,1), E = gel(fa,2), D = nf_get_diff(nf);
     587         684 :   long i, l = lg(P);
     588         684 :   if (l == 1) return gen_1;
     589         570 :   w = cgetg(l, t_VEC);
     590         570 :   E2 = cgetg(l, t_COL);
     591        1254 :   for (i = 1; i < l; i++)
     592             :   {
     593         684 :     GEN pr = gel(P,i), t = pr_get_tau(pr);
     594         684 :     long e = itou(gel(E,i)), v = idealval(nf, D, pr);
     595         684 :     if (v) { D = idealdivpowprime(nf, D, pr, utoipos(v)); e += v; }
     596         684 :     gel(E2,i) = stoi(e+1);
     597         684 :     if (typ(t) == t_MAT) t = gel(t,1);
     598         684 :     gel(w,i) = gdiv(nfpow(nf, t, stoi(e)), powiu(pr_get_p(pr),e));
     599             :   }
     600         570 :   x = mkmat2(P, E2);
     601         570 :   return idealchinese(nf, mkvec2(x, foo), w);
     602             : }
     603             : /* compute the list of W(chi) such that Ld(s,chi) = W(chi) Ld(1 - s, chi*),
     604             :  * for all chi in LCHI. All chi have the same conductor (= cond(bnr)).
     605             :  * if check == 0 do not check the result */
     606             : static GEN
     607         852 : ArtinNumber(GEN bnr, GEN LCHI, long check, long prec)
     608             : {
     609         852 :   long ic, i, j, nz, nChar = lg(LCHI)-1;
     610         852 :   pari_sp av = avma, av2;
     611             :   GEN sqrtnc, cond, condZ, cond0, cond1, nf, T;
     612             :   GEN cyc, vN, vB, diff, vt, idh, zid, gen, z, nchi;
     613             :   GEN indW, W, classe, s0, s, den, ilambda, sarch;
     614             :   CHI_t **lC;
     615             :   GROUP_t G;
     616             : 
     617         852 :   lC = (CHI_t**)new_chunk(nChar + 1);
     618         852 :   indW = cgetg(nChar + 1, t_VECSMALL);
     619         852 :   W = cgetg(nChar + 1, t_VEC);
     620        2976 :   for (ic = 0, i = 1; i <= nChar; i++)
     621             :   {
     622        2124 :     GEN CHI = gel(LCHI,i);
     623        2124 :     if (chi_get_deg(CHI) <= 2) { gel(W,i) = gen_1; continue; }
     624        1314 :     ic++; indW[ic] = i;
     625        1314 :     lC[ic] = (CHI_t*)new_chunk(sizeof(CHI_t));
     626        1314 :     init_CHI_C(lC[ic], CHI);
     627             :   }
     628         852 :   if (!ic) return W;
     629         684 :   nChar = ic;
     630             : 
     631         684 :   nf    = bnr_get_nf(bnr);
     632         684 :   diff  = nf_get_diff(nf);
     633         684 :   T     = nf_get_Tr(nf);
     634         684 :   cond  = bnr_get_mod(bnr);
     635         684 :   cond0 = gel(cond,1); condZ = gcoeff(cond0,1,1);
     636         684 :   cond1 = gel(cond,2);
     637             : 
     638         684 :   sqrtnc = gsqrt(idealnorm(nf, cond0), prec);
     639         684 :   ilambda = get_ilambda(nf, bid_get_fact(bnr_get_bid(bnr)), cond1);
     640         684 :   idh = idealmul(nf, ilambda, idealmul(nf, diff, cond0)); /* integral */
     641         684 :   ilambda = Q_remove_denom(ilambda, &den);
     642         684 :   z = den? rootsof1_cx(den, prec): NULL;
     643             : 
     644             :   /* compute a system of generators of (Ok/cond)^*, we'll make them
     645             :    * cond1-positive in the main loop */
     646         684 :   zid = Idealstar(nf, cond0, nf_GEN);
     647         684 :   cyc = abgrp_get_cyc(zid);
     648         684 :   gen = abgrp_get_gen(zid);
     649         684 :   nz = lg(gen) - 1;
     650         684 :   sarch = nfarchstar(nf, cond0, vec01_to_indices(cond1));
     651             : 
     652         684 :   nchi = cgetg(nChar+1, t_VEC);
     653         684 :   for (ic = 1; ic <= nChar; ic++) gel(nchi,ic) = cgetg(nz + 1, t_VECSMALL);
     654        1446 :   for (i = 1; i <= nz; i++)
     655             :   {
     656         762 :     if (is_bigint(gel(cyc,i)))
     657           0 :       pari_err_OVERFLOW("ArtinNumber [conductor too large]");
     658         762 :     gel(gen,i) = set_sign_mod_divisor(nf, NULL, gel(gen,i), sarch);
     659         762 :     classe = isprincipalray(bnr, gel(gen,i));
     660        2298 :     for (ic = 1; ic <= nChar; ic++) {
     661        1536 :       GEN n = gel(nchi,ic);
     662        1536 :       n[i] = CHI_eval_n(lC[ic], classe);
     663             :     }
     664             :   }
     665             : 
     666             :   /* Sum chi(beta) * exp(2i * Pi * Tr(beta * ilambda) where beta
     667             :      runs through the classes of (Ok/cond0)^* and beta cond1-positive */
     668         684 :   vt = gel(T,1); /* ( Tr(w_i) )_i */
     669         684 :   if (typ(ilambda) == t_COL)
     670         570 :     vt = ZV_ZM_mul(vt, zk_multable(nf, ilambda));
     671             :   else
     672         114 :     vt = ZC_Z_mul(vt, ilambda);
     673             :   /*vt = den . (Tr(w_i * ilambda))_i */
     674         684 :   G.cyc = gtovecsmall(cyc);
     675         684 :   G.r = nz;
     676         684 :   G.j = zero_zv(nz);
     677         684 :   vN = zero_Flm_copy(nz, nChar);
     678             : 
     679         684 :   av2 = avma;
     680         684 :   vB = const_vec(nz, gen_1);
     681         684 :   s0 = z? powgi(z, modii(gel(vt,1), den)): gen_1; /* for beta = 1 */
     682         684 :   s = const_vec(nChar, s0);
     683             : 
     684       54690 :   while ( (i = NextElt(&G)) )
     685             :   {
     686       53322 :     GEN b = gel(vB,i);
     687       53322 :     b = nfmuli(nf, b, gel(gen,i));
     688       53322 :     b = typ(b) == t_COL? FpC_red(b, condZ): modii(b, condZ);
     689       53322 :     for (j=1; j<=i; j++) gel(vB,j) = b;
     690             : 
     691      220068 :     for (ic = 1; ic <= nChar; ic++)
     692             :     {
     693      166746 :       GEN v = gel(vN,ic), n = gel(nchi,ic);
     694      166746 :       v[i] = Fl_add(v[i], n[i], lC[ic]->ord);
     695      166746 :       for (j=1; j<i; j++) v[j] = v[i];
     696             :     }
     697             : 
     698       53322 :     gel(vB,i) = b = set_sign_mod_divisor(nf, NULL, b, sarch);
     699       53322 :     if (!z)
     700           0 :       s0 = gen_1;
     701             :     else
     702             :     {
     703       53322 :       b = typ(b) == t_COL? ZV_dotproduct(vt, b): mulii(gel(vt,1),b);
     704       53322 :       s0 = powgi(z, modii(b,den));
     705             :     }
     706      220068 :     for (ic = 1; ic <= nChar; ic++)
     707             :     {
     708      166746 :       GEN v = gel(vN,ic), val = lC[ic]->val[ v[i] ];
     709      166746 :       gel(s,ic) = gadd(gel(s,ic), gmul(val, s0));
     710             :     }
     711             : 
     712       53322 :     if (gc_needed(av2, 1))
     713             :     {
     714           0 :       if (DEBUGMEM > 1) pari_warn(warnmem,"ArtinNumber");
     715           0 :       gerepileall(av2, 2, &s, &vB);
     716             :     }
     717             :   }
     718             : 
     719         684 :   classe = isprincipalray(bnr, idh);
     720         684 :   z = powIs(- (lg(gel(sarch,1))-1));
     721             : 
     722        1998 :   for (ic = 1; ic <= nChar; ic++)
     723             :   {
     724        1314 :     s0 = gmul(gel(s,ic), CHI_eval(lC[ic], classe));
     725        1314 :     s0 = gdiv(s0, sqrtnc);
     726        1314 :     if (check && - expo(subrs(gnorm(s0), 1)) < prec2nbits(prec) >> 1)
     727           0 :       pari_err_BUG("ArtinNumber");
     728        1314 :     gel(W, indW[ic]) = gmul(s0, z);
     729             :   }
     730         684 :   return gerepilecopy(av, W);
     731             : }
     732             : 
     733             : static GEN
     734         618 : ComputeAllArtinNumbers(GEN dataCR, GEN vChar, int check, long prec)
     735             : {
     736         618 :   long j, k, cl = lg(dataCR) - 1, J = lg(vChar)-1;
     737         618 :   GEN W = cgetg(cl+1,t_VEC), WbyCond, LCHI;
     738             : 
     739        1404 :   for (j = 1; j <= J; j++)
     740             :   {
     741         786 :     GEN LChar = gel(vChar,j), ldata = vecpermute(dataCR, LChar);
     742         786 :     GEN dtcr = gel(ldata,1), bnr = ch_bnr(dtcr);
     743         786 :     long l = lg(LChar);
     744             : 
     745         786 :     if (DEBUGLEVEL>1)
     746           0 :       err_printf("* Root Number: cond. no %ld/%ld (%ld chars)\n", j, J, l-1);
     747         786 :     LCHI = cgetg(l, t_VEC);
     748         786 :     for (k = 1; k < l; k++) gel(LCHI,k) = ch_CHI0(gel(ldata,k));
     749         786 :     WbyCond = ArtinNumber(bnr, LCHI, check, prec);
     750         786 :     for (k = 1; k < l; k++) gel(W,LChar[k]) = gel(WbyCond,k);
     751             :   }
     752         618 :   return W;
     753             : }
     754             : static GEN
     755          66 : SingleArtinNumber(GEN bnr, GEN chi, long prec)
     756          66 : { return gel(ArtinNumber(bnr, mkvec(chi), 1, prec), 1); }
     757             : 
     758             : /* compute the constant W of the functional equation of
     759             :    Lambda(chi). If flag = 1 then chi is assumed to be primitive */
     760             : GEN
     761          66 : bnrrootnumber(GEN bnr, GEN chi, long flag, long prec)
     762             : {
     763          66 :   pari_sp av = avma;
     764             :   GEN cyc;
     765             : 
     766          66 :   if (flag < 0 || flag > 1) pari_err_FLAG("bnrrootnumber");
     767          66 :   checkbnr(bnr);
     768          66 :   if (flag)
     769             :   {
     770           0 :     cyc = bnr_get_cyc(bnr);
     771           0 :     if (!char_check(cyc,chi)) pari_err_TYPE("bnrrootnumber [character]", chi);
     772             :   }
     773             :   else
     774             :   {
     775          66 :     GEN z = bnrconductor_i(bnr, chi, 2);
     776          66 :     bnr = gel(z,2);
     777          66 :     chi = gel(z,3);
     778          66 :     cyc = bnr_get_cyc(bnr);
     779             :   }
     780          66 :   chi = char_normalize(chi, cyc_normalize(cyc));
     781          66 :   chi = get_Char(chi, prec);
     782          66 :   return gerepilecopy(av, SingleArtinNumber(bnr, chi, prec));
     783             : }
     784             : 
     785             : /********************************************************************/
     786             : /*               3rd part: initialize the characters                */
     787             : /********************************************************************/
     788             : 
     789             : /* Let chi be a character, A(chi) corresponding to the primes dividing diff
     790             :    at s = flag. If s = 0, returns [r, A] where r is the order of vanishing
     791             :    at s = 0 corresponding to diff. No GC */
     792             : static GEN
     793        1854 : ComputeAChi(GEN dtcr, long *r, long flag, long prec)
     794             : {
     795        1854 :   GEN A, diff = ch_diff(dtcr), bnrc = ch_bnr(dtcr), chi  = ch_CHI0(dtcr);
     796        1854 :   long i, l = lg(diff);
     797             : 
     798        1854 :   A = gen_1; *r = 0;
     799        1956 :   for (i = 1; i < l; i++)
     800             :   {
     801         102 :     GEN pr = gel(diff,i), B;
     802         102 :     GEN z = CharEval(chi, isprincipalray(bnrc, pr));
     803             : 
     804         102 :     if (flag)
     805           0 :       B = gsubsg(1, gdiv(z, pr_norm(pr)));
     806         102 :     else if (gequal1(z))
     807             :     {
     808          12 :       B = glog(pr_norm(pr), prec);
     809          12 :       (*r)++;
     810             :     }
     811             :     else
     812          90 :       B = gsubsg(1, z);
     813         102 :     A = gmul(A, B);
     814             :   }
     815        1854 :   return A;
     816             : }
     817             : /* simplified version of ComputeAchi: return 1 if L(0,chi) = 0 */
     818             : static int
     819        1866 : L_vanishes_at_0(GEN dtcr)
     820             : {
     821        1866 :   GEN diff = ch_diff(dtcr), bnrc = ch_bnr(dtcr), chi  = ch_CHI0(dtcr);
     822        1866 :   long i, l = lg(diff);
     823             : 
     824        1956 :   for (i = 1; i < l; i++)
     825             :   {
     826         114 :     GEN pr = gel(diff,i);
     827         114 :     if (! CharEval_n(chi, isprincipalray(bnrc, pr))) return 1;
     828             :   }
     829        1842 :   return 0;
     830             : }
     831             : 
     832             : static GEN
     833         762 : _data4(GEN arch, long r1, long r2)
     834             : {
     835         762 :   GEN z = cgetg(5, t_VECSMALL);
     836         762 :   long i, b, q = 0;
     837             : 
     838         762 :   for (i=1; i<=r1; i++) if (signe(gel(arch,i))) q++;
     839         762 :   z[1] = q; b = r1 - q;
     840         762 :   z[2] = b;
     841         762 :   z[3] = r2;
     842         762 :   z[4] = maxss(b+r2+1, r2+q);
     843         762 :   return z;
     844             : }
     845             : 
     846             : /* Given a list [chi, F = cond(chi)] of characters over Cl(bnr), compute a
     847             :    vector dataCR containing for each character:
     848             :    2: the constant C(F) [t_REAL]
     849             :    3: bnr(F)
     850             :    4: [q, r1 - q, r2, rc] where
     851             :         q = number of real places in F
     852             :         rc = max{r1 + r2 - q + 1, r2 + q}
     853             :    6: diff(chi) primes dividing m but not F
     854             :    7: finite part of F
     855             : 
     856             :    1: chi
     857             :    5: [(c_i), z, d] in bnr(m)
     858             :    8: [(c_i), z, d] in bnr(F)
     859             :    9: if NULL then does not compute (for AllStark) */
     860             : static GEN
     861         600 : InitChar(GEN bnr, GEN listCR, long prec)
     862             : {
     863         600 :   GEN bnf = checkbnf(bnr), nf = bnf_get_nf(bnf);
     864             :   GEN modul, dk, C, dataCR, chi, cond, ncyc;
     865             :   long N, r1, r2, prec2, i, j, l;
     866         600 :   pari_sp av = avma;
     867             : 
     868         600 :   modul = bnr_get_mod(bnr);
     869         600 :   dk    = nf_get_disc(nf);
     870         600 :   N     = nf_get_degree(nf);
     871         600 :   nf_get_sign(nf, &r1,&r2);
     872         600 :   prec2 = precdbl(prec) + EXTRA_PREC;
     873         600 :   C     = gmul2n(sqrtr_abs(divir(dk, powru(mppi(prec2),N))), -r2);
     874         600 :   ncyc = cyc_normalize( bnr_get_cyc(bnr) );
     875             : 
     876         600 :   dataCR = cgetg_copy(listCR, &l);
     877        2592 :   for (i = 1; i < l; i++)
     878             :   {
     879        1992 :     GEN bnrc, olddtcr, dtcr = cgetg(9, t_VEC);
     880        1992 :     gel(dataCR,i) = dtcr;
     881             : 
     882        1992 :     chi  = gmael(listCR, i, 1);
     883        1992 :     cond = gmael(listCR, i, 2);
     884             : 
     885             :     /* do we already know the invariants of chi? */
     886        1992 :     olddtcr = NULL;
     887        2724 :     for (j = 1; j < i; j++)
     888        1962 :       if (gequal(cond, gmael(listCR,j,2))) { olddtcr = gel(dataCR,j); break; }
     889             : 
     890        1992 :     if (!olddtcr)
     891             :     {
     892         762 :       ch_C(dtcr) = gmul(C, gsqrt(ZM_det_triangular(gel(cond,1)), prec2));
     893         762 :       ch_4(dtcr) = _data4(gel(cond,2),r1,r2);
     894         762 :       ch_cond(dtcr) = cond;
     895         762 :       if (gequal(cond,modul))
     896             :       {
     897         582 :         ch_bnr(dtcr) = bnr;
     898         582 :         ch_diff(dtcr) = cgetg(1, t_VEC);
     899             :       }
     900             :       else
     901             :       {
     902         180 :         ch_bnr(dtcr) = Buchray(bnf, cond, nf_INIT);
     903         180 :         ch_diff(dtcr) = get_prdiff(bnr, cond);
     904             :       }
     905             :     }
     906             :     else
     907             :     {
     908        1230 :       ch_C(dtcr) = ch_C(olddtcr);
     909        1230 :       ch_bnr(dtcr) = ch_bnr(olddtcr);
     910        1230 :       ch_4(dtcr) = ch_4(olddtcr);
     911        1230 :       ch_diff(dtcr) = ch_diff(olddtcr);
     912        1230 :       ch_cond(dtcr) = ch_cond(olddtcr);
     913             :     }
     914             : 
     915        1992 :     chi = char_normalize(chi,ncyc);
     916        1992 :     ch_CHI(dtcr) = get_Char(chi, prec2);
     917        1992 :     ch_comp(dtcr) = gen_1; /* compute this character (by default) */
     918             : 
     919        1992 :     bnrc = ch_bnr(dtcr);
     920        1992 :     if (gequal(bnr_get_mod(bnr), bnr_get_mod(bnrc)))
     921        1758 :       ch_CHI0(dtcr) = ch_CHI(dtcr);
     922             :     else
     923             :     {
     924         234 :       chi = bnrchar_primitive(bnr, chi, bnrc);
     925         234 :       ch_CHI0(dtcr) = get_Char(chi, prec2);
     926             :     }
     927             :   }
     928             : 
     929         600 :   return gerepilecopy(av, dataCR);
     930             : }
     931             : 
     932             : /* recompute dataCR with the new precision */
     933             : static GEN
     934          18 : CharNewPrec(GEN dataCR, GEN nf, long prec)
     935             : {
     936             :   GEN dk, C;
     937             :   long N, l, j, prec2;
     938             : 
     939          18 :   dk    =  nf_get_disc(nf);
     940          18 :   N     =  nf_get_degree(nf);
     941          18 :   prec2 = precdbl(prec) + EXTRA_PREC;
     942             : 
     943          18 :   C = sqrtr(divir(absi(dk), powru(mppi(prec2), N)));
     944             : 
     945          18 :   l = lg(dataCR);
     946          84 :   for (j = 1; j < l; j++)
     947             :   {
     948          66 :     GEN dtcr = gel(dataCR,j), f0 = gel(ch_cond(dtcr),1);
     949          66 :     ch_C(dtcr) = gmul(C, gsqrt(ZM_det_triangular(f0), prec2));
     950             : 
     951          66 :     gmael(ch_bnr(dtcr), 1, 7) = nf;
     952             : 
     953          66 :     ch_CHI( dtcr) = get_Char(gel(ch_CHI(dtcr), 1), prec2);
     954          66 :     ch_CHI0(dtcr) = get_Char(gel(ch_CHI0(dtcr),1), prec2);
     955             :   }
     956             : 
     957          18 :   return dataCR;
     958             : }
     959             : 
     960             : /********************************************************************/
     961             : /*             4th part: compute the coefficients an(chi)           */
     962             : /*                                                                  */
     963             : /* matan entries are arrays of ints containing the coefficients of  */
     964             : /* an(chi) as a polmod modulo polcyclo(order(chi))                     */
     965             : /********************************************************************/
     966             : 
     967             : static void
     968     1279788 : _0toCoeff(int *rep, long deg)
     969             : {
     970             :   long i;
     971     1279788 :   for (i=0; i<deg; i++) rep[i] = 0;
     972     1279788 : }
     973             : 
     974             : /* transform a polmod into Coeff */
     975             : static void
     976      328026 : Polmod2Coeff(int *rep, GEN polmod, long deg)
     977             : {
     978             :   long i;
     979      328026 :   if (typ(polmod) == t_POLMOD)
     980             :   {
     981      232314 :     GEN pol = gel(polmod,2);
     982      232314 :     long d = degpol(pol);
     983             : 
     984      232314 :     pol += 2;
     985      232314 :     for (i=0; i<=d; i++) rep[i] = itos(gel(pol,i));
     986      232314 :     for (   ; i<deg; i++) rep[i] = 0;
     987             :   }
     988             :   else
     989             :   {
     990       95712 :     rep[0] = itos(polmod);
     991       95712 :     for (i=1; i<deg; i++) rep[i] = 0;
     992             :   }
     993      328026 : }
     994             : 
     995             : /* initialize a deg * n matrix of ints */
     996             : static int**
     997        3564 : InitMatAn(long n, long deg, long flag)
     998             : {
     999             :   long i, j;
    1000        3564 :   int *a, **A = (int**)pari_malloc((n+1)*sizeof(int*));
    1001        3564 :   A[0] = NULL;
    1002     5033634 :   for (i = 1; i <= n; i++)
    1003             :   {
    1004     5030070 :     a = (int*)pari_malloc(deg*sizeof(int));
    1005     5030070 :     A[i] = a; a[0] = (i == 1 || flag);
    1006     5030070 :     for (j = 1; j < deg; j++) a[j] = 0;
    1007             :   }
    1008        3564 :   return A;
    1009             : }
    1010             : 
    1011             : static void
    1012        5610 : FreeMat(int **A, long n)
    1013             : {
    1014             :   long i;
    1015     5045622 :   for (i = 0; i <= n; i++)
    1016     5040012 :     if (A[i]) pari_free((void*)A[i]);
    1017        5610 :   pari_free((void*)A);
    1018        5610 : }
    1019             : 
    1020             : /* initialize Coeff reduction */
    1021             : static int**
    1022        2046 : InitReduction(long d, long deg)
    1023             : {
    1024             :   long j;
    1025        2046 :   pari_sp av = avma;
    1026             :   int **A;
    1027             :   GEN polmod, pol;
    1028             : 
    1029        2046 :   A   = (int**)pari_malloc(deg*sizeof(int*));
    1030        2046 :   pol = polcyclo(d, 0);
    1031        8424 :   for (j = 0; j < deg; j++)
    1032             :   {
    1033        6378 :     A[j] = (int*)pari_malloc(deg*sizeof(int));
    1034        6378 :     polmod = gmodulo(pol_xn(deg+j, 0), pol);
    1035        6378 :     Polmod2Coeff(A[j], polmod, deg);
    1036             :   }
    1037             : 
    1038        2046 :   avma = av; return A;
    1039             : }
    1040             : 
    1041             : #if 0
    1042             : void
    1043             : pan(int **an, long n, long deg)
    1044             : {
    1045             :   long i,j;
    1046             :   for (i = 1; i <= n; i++)
    1047             :   {
    1048             :     err_printf("n = %ld: ",i);
    1049             :     for (j = 0; j < deg; j++) err_printf("%d ",an[i][j]);
    1050             :     err_printf("\n");
    1051             :   }
    1052             : }
    1053             : #endif
    1054             : 
    1055             : /* returns 0 if c is zero, 1 otherwise. */
    1056             : static int
    1057     6166710 : IsZero(int* c, long deg)
    1058             : {
    1059             :   long i;
    1060    19654728 :   for (i = 0; i < deg; i++)
    1061    15093042 :     if (c[i]) return 0;
    1062     4561686 :   return 1;
    1063             : }
    1064             : 
    1065             : /* set c0 <-- c0 * c1 */
    1066             : static void
    1067     1534008 : MulCoeff(int *c0, int* c1, int** reduc, long deg)
    1068             : {
    1069             :   long i,j;
    1070             :   int c, *T;
    1071             : 
    1072     3068016 :   if (IsZero(c0,deg)) return;
    1073             : 
    1074      682938 :   T = (int*)new_chunk(2*deg);
    1075     6345438 :   for (i = 0; i < 2*deg; i++)
    1076             :   {
    1077     5662500 :     c = 0;
    1078    42780330 :     for (j = 0; j <= i; j++)
    1079    37117830 :       if (j < deg && j > i - deg) c += c0[j] * c1[i-j];
    1080     5662500 :     T[i] = c;
    1081             :   }
    1082     3514188 :   for (i = 0; i < deg; i++)
    1083             :   {
    1084     2831250 :     c = T[i];
    1085     2831250 :     for (j = 0; j < deg; j++) c += reduc[j][i] * T[deg+j];
    1086     2831250 :     c0[i] = c;
    1087             :   }
    1088             : }
    1089             : 
    1090             : /* c0 <- c0 + c1 * c2 */
    1091             : static void
    1092     4632702 : AddMulCoeff(int *c0, int *c1, int* c2, int** reduc, long deg)
    1093             : {
    1094             :   long i, j;
    1095             :   pari_sp av;
    1096             :   int c, *t;
    1097             : 
    1098     4632702 :   if (IsZero(c2,deg)) return;
    1099      922086 :   if (!c1) /* c1 == 1 */
    1100             :   {
    1101      248958 :     for (i = 0; i < deg; i++) c0[i] += c2[i];
    1102      248958 :     return;
    1103             :   }
    1104      673128 :   av = avma;
    1105      673128 :   t = (int*)new_chunk(2*deg); /* = c1 * c2, not reduced */
    1106     4656756 :   for (i = 0; i < 2*deg; i++)
    1107             :   {
    1108     3983628 :     c = 0;
    1109    24247350 :     for (j = 0; j <= i; j++)
    1110    20263722 :       if (j < deg && j > i - deg) c += c1[j] * c2[i-j];
    1111     3983628 :     t[i] = c;
    1112             :   }
    1113     2664942 :   for (i = 0; i < deg; i++)
    1114             :   {
    1115     1991814 :     c = t[i];
    1116     1991814 :     for (j = 0; j < deg; j++) c += reduc[j][i] * t[deg+j];
    1117     1991814 :     c0[i] += c;
    1118             :   }
    1119      673128 :   avma = av;
    1120             : }
    1121             : 
    1122             : /* evaluate the Coeff. No Garbage collector */
    1123             : static GEN
    1124     3162726 : EvalCoeff(GEN z, int* c, long deg)
    1125             : {
    1126             :   long i,j;
    1127             :   GEN e, r;
    1128             : 
    1129     3162726 :   if (!c) return gen_0;
    1130             : #if 0
    1131             :   /* standard Horner */
    1132             :   e = stoi(c[deg - 1]);
    1133             :   for (i = deg - 2; i >= 0; i--)
    1134             :     e = gadd(stoi(c[i]), gmul(z, e));
    1135             : #else
    1136             :   /* specific attention to sparse polynomials */
    1137     3162726 :   e = NULL;
    1138     4462596 :   for (i = deg-1; i >=0; i=j-1)
    1139             :   {
    1140     9970428 :     for (j=i; c[j] == 0; j--)
    1141     8670558 :       if (j==0)
    1142             :       {
    1143     2603910 :         if (!e) return NULL;
    1144      251268 :         if (i!=j) z = gpowgs(z,i-j+1);
    1145      251268 :         return gmul(e,z);
    1146             :       }
    1147     1299870 :     if (e)
    1148             :     {
    1149      489786 :       r = (i==j)? z: gpowgs(z,i-j+1);
    1150      489786 :       e = gadd(gmul(e,r), stoi(c[j]));
    1151             :     }
    1152             :     else
    1153      810084 :       e = stoi(c[j]);
    1154             :   }
    1155             : #endif
    1156      558816 :   return e;
    1157             : }
    1158             : 
    1159             : /* copy the n * (m+1) array matan */
    1160             : static void
    1161      272292 : CopyCoeff(int** a, int** a2, long n, long m)
    1162             : {
    1163             :   long i,j;
    1164             : 
    1165     3973728 :   for (i = 1; i <= n; i++)
    1166             :   {
    1167     3701436 :     int *b = a[i], *b2 = a2[i];
    1168     3701436 :     for (j = 0; j < m; j++) b2[j] = b[j];
    1169             :   }
    1170      272292 : }
    1171             : 
    1172             : /* return q*p if <= n. Beware overflow */
    1173             : static long
    1174      523146 : next_pow(long q, long p, long n)
    1175             : {
    1176      523146 :   const GEN x = muluu((ulong)q, (ulong)p);
    1177      523146 :   const ulong qp = uel(x,2);
    1178      523146 :   return (lgefint(x) > 3 || qp > (ulong)n)? 0: qp;
    1179             : }
    1180             : 
    1181             : static void
    1182      272292 : an_AddMul(int **an,int **an2, long np, long n, long deg, GEN chi, int **reduc)
    1183             : {
    1184      272292 :   GEN chi2 = chi;
    1185             :   long q, qk, k;
    1186      272292 :   int *c, *c2 = (int*)new_chunk(deg);
    1187             : 
    1188      272292 :   CopyCoeff(an, an2, n/np, deg);
    1189      272292 :   for (q=np;;)
    1190             :   {
    1191      299628 :     if (gequal1(chi2)) c = NULL; else { Polmod2Coeff(c2, chi2, deg); c = c2; }
    1192     4932330 :     for(k = 1, qk = q; qk <= n; k++, qk += q)
    1193     4632702 :       AddMulCoeff(an[qk], c, an2[k], reduc, deg);
    1194      299628 :     if (! (q = next_pow(q,np, n)) ) break;
    1195             : 
    1196       27336 :     chi2 = gmul(chi2, chi);
    1197       27336 :   }
    1198      272292 : }
    1199             : 
    1200             : /* correct the coefficients an(chi) according with diff(chi) in place */
    1201             : static void
    1202        2046 : CorrectCoeff(GEN dtcr, int** an, int** reduc, long n, long deg)
    1203             : {
    1204        2046 :   pari_sp av = avma;
    1205             :   long lg, j;
    1206             :   pari_sp av1;
    1207             :   int **an2;
    1208             :   GEN bnrc, diff;
    1209             :   CHI_t C;
    1210             : 
    1211        2046 :   diff = ch_diff(dtcr); lg = lg(diff) - 1;
    1212        4092 :   if (!lg) return;
    1213             : 
    1214         150 :   if (DEBUGLEVEL>2) err_printf("diff(CHI) = %Ps", diff);
    1215         150 :   bnrc = ch_bnr(dtcr);
    1216         150 :   init_CHI_alg(&C, ch_CHI0(dtcr));
    1217             : 
    1218         150 :   an2 = InitMatAn(n, deg, 0);
    1219         150 :   av1 = avma;
    1220         330 :   for (j = 1; j <= lg; j++)
    1221             :   {
    1222         180 :     GEN pr = gel(diff,j);
    1223         180 :     long Np = upr_norm(pr);
    1224         180 :     GEN chi  = CHI_eval(&C, isprincipalray(bnrc, pr));
    1225         180 :     an_AddMul(an,an2,Np,n,deg,chi,reduc);
    1226         180 :     avma = av1;
    1227             :   }
    1228         150 :   FreeMat(an2, n); avma = av;
    1229             : }
    1230             : 
    1231             : /* compute the coefficients an in the general case */
    1232             : static int**
    1233        1368 : ComputeCoeff(GEN dtcr, LISTray *R, long n, long deg)
    1234             : {
    1235        1368 :   pari_sp av = avma, av2;
    1236             :   long i, l;
    1237             :   int **an, **reduc, **an2;
    1238             :   GEN L;
    1239             :   CHI_t C;
    1240             : 
    1241        1368 :   init_CHI_alg(&C, ch_CHI(dtcr));
    1242        1368 :   an  = InitMatAn(n, deg, 0);
    1243        1368 :   an2 = InitMatAn(n, deg, 0);
    1244        1368 :   reduc  = InitReduction(C.ord, deg);
    1245        1368 :   av2 = avma;
    1246             : 
    1247        1368 :   L = R->L1; l = lg(L);
    1248      273480 :   for (i=1; i<l; i++, avma = av2)
    1249             :   {
    1250      272112 :     long np = L[i];
    1251      272112 :     GEN chi  = CHI_eval(&C, gel(R->L1ray,i));
    1252      272112 :     an_AddMul(an,an2,np,n,deg,chi,reduc);
    1253             :   }
    1254        1368 :   FreeMat(an2, n);
    1255             : 
    1256        1368 :   CorrectCoeff(dtcr, an, reduc, n, deg);
    1257        1368 :   FreeMat(reduc, deg-1);
    1258        1368 :   avma = av; return an;
    1259             : }
    1260             : 
    1261             : /********************************************************************/
    1262             : /*              5th part: compute L-functions at s=1                */
    1263             : /********************************************************************/
    1264             : static void
    1265         360 : deg11(LISTray *R, long p, GEN bnr, GEN pr) {
    1266         360 :   GEN z = isprincipalray(bnr, pr);
    1267         360 :   vecsmalltrunc_append(R->L1, p);
    1268         360 :   vectrunc_append(R->L1ray, z);
    1269         360 : }
    1270             : static void
    1271       27408 : deg12(LISTray *R, long p, GEN bnr, GEN Lpr) {
    1272       27408 :   GEN z = isprincipalray(bnr, gel(Lpr,1));
    1273       27408 :   vecsmalltrunc_append(R->L11, p);
    1274       27408 :   vectrunc_append(R->L11ray, z);
    1275       27408 : }
    1276             : static void
    1277          60 : deg0(LISTray *R, long p) { vecsmalltrunc_append(R->L0, p); }
    1278             : static void
    1279       29010 : deg2(LISTray *R, long p) { vecsmalltrunc_append(R->L2, p); }
    1280             : 
    1281             : static void
    1282         186 : InitPrimesQuad(GEN bnr, ulong N0, LISTray *R)
    1283             : {
    1284         186 :   pari_sp av = avma;
    1285         186 :   GEN bnf = bnr_get_bnf(bnr), cond = gel(bnr_get_mod(bnr), 1);
    1286         186 :   long p,i,l, condZ = itos(gcoeff(cond,1,1)), contZ = itos(content(cond));
    1287         186 :   GEN prime, Lpr, nf = bnf_get_nf(bnf), dk = nf_get_disc(nf);
    1288             :   forprime_t T;
    1289             : 
    1290         186 :   l = 1 + primepi_upper_bound(N0);
    1291         186 :   R->L0 = vecsmalltrunc_init(l);
    1292         186 :   R->L2 = vecsmalltrunc_init(l); R->condZ = condZ;
    1293         186 :   R->L1 = vecsmalltrunc_init(l); R->L1ray = vectrunc_init(l);
    1294         186 :   R->L11= vecsmalltrunc_init(l); R->L11ray= vectrunc_init(l);
    1295         186 :   prime = utoipos(2);
    1296         186 :   u_forprime_init(&T, 2, N0);
    1297       57210 :   while ( (p = u_forprime_next(&T)) )
    1298             :   {
    1299       56838 :     prime[2] = p;
    1300       56838 :     switch (kroiu(dk, p))
    1301             :     {
    1302             :     case -1: /* inert */
    1303       29028 :       if (condZ % p == 0) deg0(R,p); else deg2(R,p);
    1304       29028 :       break;
    1305             :     case 1: /* split */
    1306       27558 :       Lpr = idealprimedec(nf, prime);
    1307       27558 :       if      (condZ % p != 0) deg12(R, p, bnr, Lpr);
    1308         150 :       else if (contZ % p == 0) deg0(R,p);
    1309             :       else
    1310             :       {
    1311         150 :         GEN pr = idealval(nf, cond, gel(Lpr,1))? gel(Lpr,2): gel(Lpr,1);
    1312         150 :         deg11(R, p, bnr, pr);
    1313             :       }
    1314       27558 :       break;
    1315             :     default: /* ramified */
    1316         252 :       if (condZ % p == 0)
    1317          42 :         deg0(R,p);
    1318             :       else
    1319         210 :         deg11(R, p, bnr, idealprimedec_galois(nf,prime));
    1320         252 :       break;
    1321             :     }
    1322             :   }
    1323             :   /* precompute isprincipalray(x), x in Z */
    1324         186 :   R->rayZ = cgetg(condZ, t_VEC);
    1325        2904 :   for (i=1; i<condZ; i++)
    1326        2718 :     gel(R->rayZ,i) = (ugcd(i,condZ) == 1)? isprincipalray(bnr, utoipos(i)): gen_0;
    1327         186 :   gerepileall(av, 7, &(R->L0), &(R->L2), &(R->rayZ),
    1328             :               &(R->L1), &(R->L1ray), &(R->L11), &(R->L11ray) );
    1329         186 : }
    1330             : 
    1331             : static void
    1332         432 : InitPrimes(GEN bnr, ulong N0, LISTray *R)
    1333             : {
    1334         432 :   GEN bnf = bnr_get_bnf(bnr), cond = gel(bnr_get_mod(bnr), 1);
    1335         432 :   long p,j,k,l, condZ = itos(gcoeff(cond,1,1)), N = lg(cond)-1;
    1336         432 :   GEN tmpray, tabpr, prime, BOUND, nf = bnf_get_nf(bnf);
    1337             :   forprime_t T;
    1338             : 
    1339         432 :   R->condZ = condZ; l = primepi_upper_bound(N0) * N;
    1340         432 :   tmpray = cgetg(N+1, t_VEC);
    1341         432 :   R->L1 = vecsmalltrunc_init(l);
    1342         432 :   R->L1ray = vectrunc_init(l);
    1343         432 :   u_forprime_init(&T, 2, N0);
    1344         432 :   prime = utoipos(2);
    1345         432 :   BOUND = utoi(N0);
    1346      100638 :   while ( (p = u_forprime_next(&T)) )
    1347             :   {
    1348       99774 :     pari_sp av = avma;
    1349       99774 :     prime[2] = p;
    1350       99774 :     if (DEBUGLEVEL>1 && (p & 2047) == 1) err_printf("%ld ", p);
    1351       99774 :     tabpr = idealprimedec_limit_norm(nf, prime, BOUND);
    1352      198840 :     for (j = 1; j < lg(tabpr); j++)
    1353             :     {
    1354       99066 :       GEN pr  = gel(tabpr,j);
    1355       99066 :       if (condZ % p == 0 && idealval(nf, cond, pr))
    1356             :       {
    1357         396 :         gel(tmpray,j) = NULL; continue;
    1358             :       }
    1359       98670 :       vecsmalltrunc_append(R->L1, upowuu(p, pr_get_f(pr)));
    1360       98670 :       gel(tmpray,j) = gclone( isprincipalray(bnr, pr) );
    1361             :     }
    1362       99774 :     avma = av;
    1363      198840 :     for (k = 1; k < j; k++)
    1364             :     {
    1365       99066 :       if (!tmpray[k]) continue;
    1366       98670 :       vectrunc_append(R->L1ray, ZC_copy(gel(tmpray,k)));
    1367       98670 :       gunclone(gel(tmpray,k));
    1368             :     }
    1369             :   }
    1370         432 : }
    1371             : 
    1372             : static GEN /* cf polcoeff */
    1373      281520 : _sercoeff(GEN x, long n)
    1374             : {
    1375      281520 :   long i = n - valp(x);
    1376      281520 :   return (i < 0)? gen_0: gel(x,i+2);
    1377             : }
    1378             : 
    1379             : static void
    1380      281520 : affect_coeff(GEN q, long n, GEN y)
    1381             : {
    1382      281520 :   GEN x = _sercoeff(q,-n);
    1383      281520 :   if (x == gen_0) gel(y,n) = NULL; else affgr(x, gel(y,n));
    1384      281520 : }
    1385             : 
    1386             : typedef struct {
    1387             :   GEN c1, aij, bij, cS, cT, powracpi;
    1388             :   long i0, a,b,c, r, rc1, rc2;
    1389             : } ST_t;
    1390             : 
    1391             : /* compute the principal part at the integers s = 0, -1, -2, ..., -i0
    1392             :    of Gamma((s+1)/2)^a Gamma(s/2)^b Gamma(s)^c / (s - z) with z = 0 and 1 */
    1393             : /* NOTE: surely not the best way to do this, but it's fast enough! */
    1394             : static void
    1395         252 : ppgamma(ST_t *T, long prec)
    1396             : {
    1397             :   GEN eul, gam,gamun,gamdm, an,bn,cn_evn,cn_odd, x,x2,X,Y, cf, sqpi;
    1398             :   GEN p1, p2, aij, bij;
    1399         252 :   long a = T->a;
    1400         252 :   long b = T->b;
    1401         252 :   long c = T->c, r = T->r, i0 = T->i0;
    1402             :   long i,j, s,t;
    1403             :   pari_sp av;
    1404             : 
    1405         252 :   aij = cgetg(i0+1, t_VEC);
    1406         252 :   bij = cgetg(i0+1, t_VEC);
    1407       66960 :   for (i = 1; i <= i0; i++)
    1408             :   {
    1409       66708 :     gel(aij,i) = p1 = cgetg(r+1, t_VEC);
    1410       66708 :     gel(bij,i) = p2 = cgetg(r+1, t_VEC);
    1411       66708 :     for (j=1; j<=r; j++) { gel(p1,j) = cgetr(prec); gel(p2,j) = cgetr(prec); }
    1412             :   }
    1413         252 :   av = avma;
    1414             : 
    1415         252 :   x   = pol_x(0);
    1416         252 :   x2  = gmul2n(x, -1); /* x/2 */
    1417         252 :   eul = mpeuler(prec);
    1418         252 :   sqpi= sqrtr_abs(mppi(prec)); /* Gamma(1/2) */
    1419             : 
    1420             :   /* expansion of log(Gamma(u)) at u = 1 */
    1421         252 :   gamun = cgetg(r+3, t_SER);
    1422         252 :   gamun[1] = evalsigne(1) | _evalvalp(0) | evalvarn(0);
    1423         252 :   gel(gamun,2) = gen_0;
    1424         252 :   gel(gamun,3) = gneg(eul);
    1425         534 :   for (i = 2; i <= r; i++)
    1426         282 :     gel(gamun,i+2) = divrs(szeta(i,prec), odd(i)? -i: i);
    1427         252 :   gamun = gexp(gamun, prec); /* Gamma(1 + x) */
    1428         252 :   gam = gdiv(gamun,x); /* Gamma(x) */
    1429             : 
    1430             :   /* expansion of log(Gamma(u) / Gamma(1/2)) at u = 1/2 */
    1431         252 :   gamdm = cgetg(r+3, t_SER);
    1432         252 :   gamdm[1] = evalsigne(1) | _evalvalp(0) | evalvarn(0);
    1433         252 :   gel(gamdm,2) = gen_0;
    1434         252 :   gel(gamdm,3) = gneg(gadd(gmul2n(mplog2(prec), 1), eul));
    1435         252 :   for (i = 2; i <= r; i++) gel(gamdm,i+2) = mulri(gel(gamun,i+2), int2um1(i));
    1436         252 :   gamdm = gmul(sqpi, gexp(gamdm, prec)); /* Gamma(1/2 + x) */
    1437             : 
    1438             :  /* We simplify to get one of the following two expressions
    1439             :   * if (b > a) : sqrt{Pi}^a 2^{a-au} Gamma(u)^{a+c} Gamma(  u/2  )^{|b-a|}
    1440             :   * if (b <= a): sqrt{Pi}^b 2^{b-bu} Gamma(u)^{b+c} Gamma((u+1)/2)^{|b-a|} */
    1441         252 :   if (b > a)
    1442             :   {
    1443          42 :     t = a; s = b; X = x2; Y = gsub(x2,ghalf);
    1444          42 :     p1 = ser_unscale(gam, ghalf);
    1445          42 :     p2 = gdiv(ser_unscale(gamdm,ghalf), Y); /* Gamma((x-1)/2) */
    1446             :   }
    1447             :   else
    1448             :   {
    1449         210 :     t = b; s = a; X = gadd(x2,ghalf); Y = x2;
    1450         210 :     p1 = ser_unscale(gamdm,ghalf);
    1451         210 :     p2 = ser_unscale(gam,ghalf);
    1452             :   }
    1453         252 :   cf = powru(sqpi, t);
    1454         252 :   an = gpowgs(gpow(gen_2, gsubsg(1,x), prec), t); /* 2^{t-tx} */
    1455         252 :   bn = gpowgs(gam, t+c); /* Gamma(x)^{t+c} */
    1456         252 :   cn_evn = gpowgs(p1, s-t); /* Gamma(X)^{s-t} */
    1457         252 :   cn_odd = gpowgs(p2, s-t); /* Gamma(Y)^{s-t} */
    1458       33606 :   for (i = 0; i < i0/2; i++)
    1459             :   {
    1460       33354 :     GEN C1,q1, A1 = gel(aij,2*i+1), B1 = gel(bij,2*i+1);
    1461       33354 :     GEN C2,q2, A2 = gel(aij,2*i+2), B2 = gel(bij,2*i+2);
    1462             : 
    1463       33354 :     C1 = gmul(cf, gmul(bn, gmul(an, cn_evn)));
    1464       33354 :     p1 = gdiv(C1, gsubgs(x, 2*i));
    1465       33354 :     q1 = gdiv(C1, gsubgs(x, 2*i+1));
    1466             : 
    1467             :     /* an(x-u-1) = 2^t an(x-u) */
    1468       33354 :     an = gmul2n(an, t);
    1469             :     /* bn(x-u-1) = bn(x-u) / (x-u-1)^{t+c} */
    1470       33354 :     bn = gdiv(bn, gpowgs(gsubgs(x, 2*i+1), t+c));
    1471             : 
    1472       33354 :     C2 = gmul(cf, gmul(bn, gmul(an, cn_odd)));
    1473       33354 :     p2 = gdiv(C2, gsubgs(x, 2*i+1));
    1474       33354 :     q2 = gdiv(C2, gsubgs(x, 2*i+2));
    1475      103734 :     for (j = 1; j <= r; j++)
    1476             :     {
    1477       70380 :       affect_coeff(p1, j, A1); affect_coeff(q1, j, B1);
    1478       70380 :       affect_coeff(p2, j, A2); affect_coeff(q2, j, B2);
    1479             :     }
    1480             : 
    1481       33354 :     an = gmul2n(an, t);
    1482       33354 :     bn = gdiv(bn, gpowgs(gsubgs(x, 2*i+2), t+c));
    1483             :     /* cn_evn(x-2i-2) = cn_evn(x-2i)  / (X - (i+1))^{s-t} */
    1484             :     /* cn_odd(x-2i-3) = cn_odd(x-2i-1)/ (Y - (i+1))^{s-t} */
    1485       33354 :     cn_evn = gdiv(cn_evn, gpowgs(gsubgs(X,i+1), s-t));
    1486       33354 :     cn_odd = gdiv(cn_odd, gpowgs(gsubgs(Y,i+1), s-t));
    1487             :   }
    1488         252 :   T->aij = aij;
    1489         252 :   T->bij = bij; avma = av;
    1490         252 : }
    1491             : 
    1492             : static GEN
    1493        1992 : _cond(GEN dtcr) { return mkvec2(ch_cond(dtcr), ch_4(dtcr)); }
    1494             : 
    1495             : /* sort chars according to conductor */
    1496             : static GEN
    1497         600 : sortChars(GEN dataCR)
    1498             : {
    1499         600 :   const long cl = lg(dataCR) - 1;
    1500         600 :   GEN vCond  = cgetg(cl+1, t_VEC);
    1501         600 :   GEN CC     = cgetg(cl+1, t_VECSMALL);
    1502         600 :   GEN nvCond = cgetg(cl+1, t_VECSMALL);
    1503             :   long j,k, ncond;
    1504             :   GEN vChar;
    1505             : 
    1506         600 :   for (j = 1; j <= cl; j++) nvCond[j] = 0;
    1507             : 
    1508         600 :   ncond = 0;
    1509        2592 :   for (j = 1; j <= cl; j++)
    1510             :   {
    1511        1992 :     GEN cond = _cond(gel(dataCR,j));
    1512        2526 :     for (k = 1; k <= ncond; k++)
    1513        1764 :       if (gequal(cond, gel(vCond,k))) break;
    1514        1992 :     if (k > ncond) gel(vCond,++ncond) = cond;
    1515        1992 :     nvCond[k]++; CC[j] = k; /* char j has conductor number k */
    1516             :   }
    1517         600 :   vChar = cgetg(ncond+1, t_VEC);
    1518        1362 :   for (k = 1; k <= ncond; k++)
    1519             :   {
    1520         762 :     gel(vChar,k) = cgetg(nvCond[k]+1, t_VECSMALL);
    1521         762 :     nvCond[k] = 0;
    1522             :   }
    1523        2592 :   for (j = 1; j <= cl; j++)
    1524             :   {
    1525        1992 :     k = CC[j]; nvCond[k]++;
    1526        1992 :     mael(vChar,k,nvCond[k]) = j;
    1527             :   }
    1528         600 :   return vChar;
    1529             : }
    1530             : 
    1531             : /* Given W(chi), S(chi) and T(chi), return L(1, chi) if fl & 1, else
    1532             :    [r(chi), c(chi)] where L(s, chi) ~ c(chi) s^r(chi) at s = 0.
    1533             :    If fl & 2, adjust the value to get L_S(s, chi). */
    1534             : static GEN
    1535        1146 : GetValue(GEN dtcr, GEN W, GEN S, GEN T, long fl, long prec)
    1536             : {
    1537        1146 :   pari_sp av = avma;
    1538             :   GEN cf, z, p1;
    1539             :   long q, b, c, r;
    1540        1146 :   int isreal = (chi_get_deg(ch_CHI0(dtcr)) <= 2);
    1541             : 
    1542        1146 :   p1 = ch_4(dtcr);
    1543        1146 :   q = p1[1];
    1544        1146 :   b = p1[2];
    1545        1146 :   c = p1[3];
    1546             : 
    1547        1146 :   if (fl & 1)
    1548             :   { /* S(chi) + W(chi).T(chi)) / (C(chi) sqrt(Pi)^{r1 - q}) */
    1549         168 :     cf = gmul(ch_C(dtcr), powruhalf(mppi(prec), b));
    1550             : 
    1551         168 :     z = gadd(S, gmul(W, T));
    1552         168 :     if (isreal) z = real_i(z);
    1553         168 :     z = gdiv(z, cf);
    1554         168 :     if (fl & 2) z = gmul(z, ComputeAChi(dtcr, &r, 1, prec));
    1555             :   }
    1556             :   else
    1557             :   { /* (W(chi).S(conj(chi)) + T(chi)) / (sqrt(Pi)^q 2^{r1 - q}) */
    1558         978 :     cf = gmul2n(powruhalf(mppi(prec), q), b);
    1559             : 
    1560         978 :     z = gadd(gmul(W, gconj(S)), gconj(T));
    1561         978 :     if (isreal) z = real_i(z);
    1562         978 :     z = gdiv(z, cf); r = 0;
    1563         978 :     if (fl & 2) z = gmul(z, ComputeAChi(dtcr, &r, 0, prec));
    1564         978 :     z = mkvec2(utoi(b + c + r), z);
    1565             :   }
    1566        1146 :   return gerepilecopy(av, z);
    1567             : }
    1568             : 
    1569             : /* return the order and the first non-zero term of L(s, chi0)
    1570             :    at s = 0. If flag != 0, adjust the value to get L_S(s, chi0). */
    1571             : static GEN
    1572          18 : GetValue1(GEN bnr, long flag, long prec)
    1573             : {
    1574          18 :   GEN bnf = checkbnf(bnr), nf = bnf_get_nf(bnf);
    1575             :   GEN h, R, c, diff;
    1576             :   long i, l, r, r1, r2;
    1577          18 :   pari_sp av = avma;
    1578             : 
    1579          18 :   nf_get_sign(nf, &r1,&r2);
    1580          18 :   h = bnf_get_no(bnf);
    1581          18 :   R = bnf_get_reg(bnf);
    1582             : 
    1583          18 :   c = gneg_i(gdivgs(mpmul(h, R), bnf_get_tuN(bnf)));
    1584          18 :   r = r1 + r2 - 1;
    1585             : 
    1586          18 :   if (flag)
    1587             :   {
    1588           0 :     diff = divcond(bnr);
    1589           0 :     l = lg(diff) - 1; r += l;
    1590           0 :     for (i = 1; i <= l; i++)
    1591           0 :       c = gmul(c, glog(pr_norm(gel(diff,i)), prec));
    1592             :   }
    1593          18 :   return gerepilecopy(av, mkvec2(stoi(r), c));
    1594             : }
    1595             : 
    1596             : /********************************************************************/
    1597             : /*                6th part: recover the coefficients                */
    1598             : /********************************************************************/
    1599             : static long
    1600        2004 : TestOne(GEN plg, RC_data *d)
    1601             : {
    1602        2004 :   long j, v = d->v;
    1603        2004 :   GEN z = gsub(d->beta, gel(plg,v));
    1604        2004 :   if (expo(z) >= d->G) return 0;
    1605        5634 :   for (j = 1; j < lg(plg); j++)
    1606        3948 :     if (j != v && mpcmp(d->B, mpabs(gel(plg,j))) < 0) return 0;
    1607        1686 :   return 1;
    1608             : }
    1609             : 
    1610             : static GEN
    1611         354 : chk_reccoeff_init(FP_chk_fun *chk, GEN r, GEN mat)
    1612             : {
    1613         354 :   RC_data *d = (RC_data*)chk->data;
    1614         354 :   (void)r; d->U = mat; return d->nB;
    1615             : }
    1616             : 
    1617             : static GEN
    1618         300 : chk_reccoeff(void *data, GEN x)
    1619             : {
    1620         300 :   RC_data *d = (RC_data*)data;
    1621         300 :   GEN v = gmul(d->U, x), z = gel(v,1);
    1622             : 
    1623         300 :   if (!gequal1(z)) return NULL;
    1624         300 :   *++v = evaltyp(t_COL) | evallg( lg(d->M) );
    1625         300 :   if (TestOne(gmul(d->M, v), d)) return v;
    1626           0 :   return NULL;
    1627             : }
    1628             : 
    1629             : /* Using Cohen's method */
    1630             : static GEN
    1631         354 : RecCoeff3(GEN nf, RC_data *d, long prec)
    1632             : {
    1633             :   GEN A, M, nB, cand, p1, B2, C2, tB, beta2, nf2, Bd;
    1634         354 :   GEN beta = d->beta, B = d->B;
    1635         354 :   long N = d->N, v = d->v, e, BIG;
    1636         354 :   long i, j, k, ct = 0, prec2;
    1637         354 :   FP_chk_fun chk = { &chk_reccoeff, &chk_reccoeff_init, NULL, NULL, 0 };
    1638         354 :   chk.data = (void*)d;
    1639             : 
    1640         354 :   d->G = minss(-10, -prec2nbits(prec) >> 4);
    1641         354 :   BIG = maxss(32, -2*d->G);
    1642         354 :   tB  = sqrtnr(real2n(BIG-N,DEFAULTPREC), N-1);
    1643         354 :   Bd  = grndtoi(gmin(B, tB), &e);
    1644         354 :   if (e > 0) return NULL; /* failure */
    1645         354 :   Bd = addiu(Bd, 1);
    1646         354 :   prec2 = nbits2prec( expi(Bd) + 192 );
    1647         354 :   prec2 = maxss(precdbl(prec), prec2);
    1648         354 :   B2 = sqri(Bd);
    1649         354 :   C2 = shifti(B2, BIG<<1);
    1650             : 
    1651         354 : LABrcf: ct++;
    1652         354 :   beta2 = gprec_w(beta, prec2);
    1653         354 :   nf2 = nfnewprec_shallow(nf, prec2);
    1654         354 :   d->M = M = nf_get_M(nf2);
    1655             : 
    1656         354 :   A = cgetg(N+2, t_MAT);
    1657         354 :   for (i = 1; i <= N+1; i++) gel(A,i) = cgetg(N+2, t_COL);
    1658             : 
    1659         354 :   gcoeff(A, 1, 1) = gadd(gmul(C2, gsqr(beta2)), B2);
    1660        1140 :   for (j = 2; j <= N+1; j++)
    1661             :   {
    1662         786 :     p1 = gmul(C2, gmul(gneg_i(beta2), gcoeff(M, v, j-1)));
    1663         786 :     gcoeff(A, 1, j) = gcoeff(A, j, 1) = p1;
    1664             :   }
    1665        1140 :   for (i = 2; i <= N+1; i++)
    1666        2082 :     for (j = i; j <= N+1; j++)
    1667             :     {
    1668        1296 :       p1 = gen_0;
    1669        4356 :       for (k = 1; k <= N; k++)
    1670             :       {
    1671        3060 :         GEN p2 = gmul(gcoeff(M, k, j-1), gcoeff(M, k, i-1));
    1672        3060 :         if (k == v) p2 = gmul(C2, p2);
    1673        3060 :         p1 = gadd(p1,p2);
    1674             :       }
    1675        1296 :       gcoeff(A, i, j) = gcoeff(A, j, i) = p1;
    1676             :     }
    1677             : 
    1678         354 :   nB = mului(N+1, B2);
    1679         354 :   d->nB = nB;
    1680         354 :   cand = fincke_pohst(A, nB, -1, prec2, &chk);
    1681             : 
    1682         354 :   if (!cand)
    1683             :   {
    1684           0 :     if (ct > 3) return NULL;
    1685           0 :     prec2 = precdbl(prec2);
    1686           0 :     if (DEBUGLEVEL>1) pari_warn(warnprec,"RecCoeff", prec2);
    1687           0 :     goto LABrcf;
    1688             :   }
    1689             : 
    1690         354 :   cand = gel(cand,1);
    1691         354 :   if (lg(cand) == 2) return gel(cand,1);
    1692             : 
    1693         204 :   if (DEBUGLEVEL>1) err_printf("RecCoeff3: no solution found!\n");
    1694         204 :   return NULL;
    1695             : }
    1696             : 
    1697             : /* Using linear dependance relations */
    1698             : static GEN
    1699        1740 : RecCoeff2(GEN nf,  RC_data *d,  long prec)
    1700             : {
    1701             :   pari_sp av;
    1702        1740 :   GEN vec, M = nf_get_M(nf), beta = d->beta;
    1703        1740 :   long bit, min, max, lM = lg(M);
    1704             : 
    1705        1740 :   d->G = minss(-20, -prec2nbits(prec) >> 4);
    1706             : 
    1707        1740 :   vec  = shallowconcat(mkvec(gneg(beta)), row(M, d->v));
    1708        1740 :   min = (long)prec2nbits_mul(prec, 0.75);
    1709        1740 :   max = (long)prec2nbits_mul(prec, 0.98);
    1710        1740 :   av = avma;
    1711        2142 :   for (bit = max; bit >= min; bit-=32, avma = av)
    1712             :   {
    1713             :     long e;
    1714        1788 :     GEN v = lindep_bit(vec, bit), z = gel(v,1);
    1715        1788 :     if (!signe(z)) continue;
    1716        1704 :     *++v = evaltyp(t_COL) | evallg(lM);
    1717        1704 :     v = grndtoi(gdiv(v, z), &e);
    1718        1704 :     if (e > 0) break;
    1719        1704 :     if (TestOne(RgM_RgC_mul(M, v), d)) return v;
    1720             :   }
    1721             :   /* failure */
    1722         354 :   return RecCoeff3(nf,d,prec);
    1723             : }
    1724             : 
    1725             : /* Attempts to find a polynomial with coefficients in nf such that
    1726             :    its coefficients are close to those of pol at the place v and
    1727             :    less than B at all the other places */
    1728             : static GEN
    1729         498 : RecCoeff(GEN nf,  GEN pol,  long v, long prec)
    1730             : {
    1731         498 :   long j, md, cl = degpol(pol);
    1732         498 :   pari_sp av = avma;
    1733             :   RC_data d;
    1734             : 
    1735             :   /* if precision(pol) is too low, abort */
    1736        3186 :   for (j = 2; j <= cl+1; j++)
    1737             :   {
    1738        2706 :     GEN t = gel(pol, j);
    1739        2706 :     if (prec2nbits(gprecision(t)) - gexpo(t) < 34) return NULL;
    1740             :   }
    1741             : 
    1742         480 :   md = cl/2;
    1743         480 :   pol = leafcopy(pol);
    1744             : 
    1745         480 :   d.N = nf_get_degree(nf);
    1746         480 :   d.v = v;
    1747             : 
    1748        2016 :   for (j = 1; j <= cl; j++)
    1749             :   { /* start with the coefficients in the middle,
    1750             :        since they are the harder to recognize! */
    1751        1740 :     long cf = md + (j%2? j/2: -j/2);
    1752        1740 :     GEN t, bound = shifti(binomial(utoipos(cl), cf), cl-cf);
    1753             : 
    1754        1740 :     if (DEBUGLEVEL>1) err_printf("RecCoeff (cf = %ld, B = %Ps)\n", cf, bound);
    1755        1740 :     d.beta = real_i( gel(pol,cf+2) );
    1756        1740 :     d.B    = bound;
    1757        1740 :     if (! (t = RecCoeff2(nf, &d, prec)) ) return NULL;
    1758        1536 :     gel(pol, cf+2) = coltoalg(nf,t);
    1759             :   }
    1760         276 :   gel(pol,cl+2) = gen_1;
    1761         276 :   return gerepilecopy(av, pol);
    1762             : }
    1763             : 
    1764             : /* an[q * i] *= chi for all (i,p)=1 */
    1765             : static void
    1766      116934 : an_mul(int **an, long p, long q, long n, long deg, GEN chi, int **reduc)
    1767             : {
    1768             :   pari_sp av;
    1769             :   long c,i;
    1770             :   int *T;
    1771             : 
    1772      233868 :   if (gequal1(chi)) return;
    1773      107778 :   av = avma;
    1774      107778 :   T = (int*)new_chunk(deg); Polmod2Coeff(T,chi, deg);
    1775     2096304 :   for (c = 1, i = q; i <= n; i += q, c++)
    1776     1988526 :     if (c == p) c = 0; else MulCoeff(an[i], T, reduc, deg);
    1777      107778 :   avma = av;
    1778             : }
    1779             : /* an[q * i] = 0 for all (i,p)=1 */
    1780             : static void
    1781      106584 : an_set0_coprime(int **an, long p, long q, long n, long deg)
    1782             : {
    1783             :   long c,i;
    1784     1357464 :   for (c = 1, i = q; i <= n; i += q, c++)
    1785     1250880 :     if (c == p) c = 0; else _0toCoeff(an[i], deg);
    1786      106584 : }
    1787             : /* an[q * i] = 0 for all i */
    1788             : static void
    1789         240 : an_set0(int **an, long p, long n, long deg)
    1790             : {
    1791             :   long i;
    1792         240 :   for (i = p; i <= n; i += p) _0toCoeff(an[i], deg);
    1793         240 : }
    1794             : 
    1795             : /* compute the coefficients an for the quadratic case */
    1796             : static int**
    1797         678 : computean(GEN dtcr, LISTray *R, long n, long deg)
    1798             : {
    1799         678 :   pari_sp av = avma, av2;
    1800             :   long i, p, q, condZ, l;
    1801             :   int **an, **reduc;
    1802             :   GEN L, chi, chi1;
    1803             :   CHI_t C;
    1804             : 
    1805         678 :   init_CHI_alg(&C, ch_CHI(dtcr));
    1806         678 :   condZ= R->condZ;
    1807             : 
    1808         678 :   an = InitMatAn(n, deg, 1);
    1809         678 :   reduc = InitReduction(C.ord, deg);
    1810         678 :   av2 = avma;
    1811             : 
    1812             :   /* all pr | p divide cond */
    1813         678 :   L = R->L0; l = lg(L);
    1814         678 :   for (i=1; i<l; i++) an_set0(an,L[i],n,deg);
    1815             : 
    1816             :   /* 1 prime of degree 2 */
    1817         678 :   L = R->L2; l = lg(L);
    1818      105828 :   for (i=1; i<l; i++, avma = av2)
    1819             :   {
    1820      105150 :     p = L[i];
    1821      105150 :     if (condZ == 1) chi = C.val[0]; /* 1 */
    1822      105018 :     else            chi = CHI_eval(&C, gel(R->rayZ, p%condZ));
    1823      105150 :     chi1 = chi;
    1824      105150 :     for (q=p;;)
    1825             :     {
    1826      106584 :       an_set0_coprime(an, p,q,n,deg); /* v_p(q) odd */
    1827      106584 :       if (! (q = next_pow(q,p, n)) ) break;
    1828             : 
    1829        4596 :       an_mul(an,p,q,n,deg,chi,reduc);
    1830        4596 :       if (! (q = next_pow(q,p, n)) ) break;
    1831        1434 :       chi = gmul(chi, chi1);
    1832        1434 :     }
    1833             :   }
    1834             : 
    1835             :   /* 1 prime of degree 1 */
    1836         678 :   L = R->L1; l = lg(L);
    1837        2052 :   for (i=1; i<l; i++, avma = av2)
    1838             :   {
    1839        1374 :     p = L[i];
    1840        1374 :     chi = CHI_eval(&C, gel(R->L1ray,i));
    1841        1374 :     chi1 = chi;
    1842        1374 :     for(q=p;;)
    1843             :     {
    1844        6594 :       an_mul(an,p,q,n,deg,chi,reduc);
    1845        6594 :       if (! (q = next_pow(q,p, n)) ) break;
    1846        5220 :       chi = gmul(chi, chi1);
    1847        5220 :     }
    1848             :   }
    1849             : 
    1850             :   /* 2 primes of degree 1 */
    1851         678 :   L = R->L11; l = lg(L);
    1852      100068 :   for (i=1; i<l; i++, avma = av2)
    1853             :   {
    1854             :     GEN ray1, ray2, chi11, chi12, chi2;
    1855             : 
    1856       99390 :     p = L[i]; ray1 = gel(R->L11ray,i); /* use pr1 pr2 = (p) */
    1857       99390 :     if (condZ == 1)
    1858          96 :       ray2 = ZC_neg(ray1);
    1859             :     else
    1860       99294 :       ray2 = ZC_sub(gel(R->rayZ, p%condZ),  ray1);
    1861       99390 :     chi11 = CHI_eval(&C, ray1);
    1862       99390 :     chi12 = CHI_eval(&C, ray2);
    1863             : 
    1864       99390 :     chi1 = gadd(chi11, chi12);
    1865       99390 :     chi2 = chi12;
    1866       99390 :     for(q=p;;)
    1867             :     {
    1868      105744 :       an_mul(an,p,q,n,deg,chi1,reduc);
    1869      105744 :       if (! (q = next_pow(q,p, n)) ) break;
    1870        6354 :       chi2 = gmul(chi2, chi12);
    1871        6354 :       chi1 = gadd(chi2, gmul(chi1, chi11));
    1872        6354 :     }
    1873             :   }
    1874             : 
    1875         678 :   CorrectCoeff(dtcr, an, reduc, n, deg);
    1876         678 :   FreeMat(reduc, deg-1);
    1877         678 :   avma = av; return an;
    1878             : }
    1879             : 
    1880             : /* return the vector of A^i/i for i = 1...n */
    1881             : static GEN
    1882         216 : mpvecpowdiv(GEN A, long n)
    1883             : {
    1884         216 :   pari_sp av = avma;
    1885             :   long i;
    1886         216 :   GEN v = powersr(A, n);
    1887         216 :   GEN w = cgetg(n+1, t_VEC);
    1888         216 :   gel(w,1) = rcopy(gel(v,2));
    1889         216 :   for (i=2; i<=n; i++) gel(w,i) = divru(gel(v,i+1), i);
    1890         216 :   return gerepileupto(av, w);
    1891             : }
    1892             : 
    1893             : static void GetST0(GEN bnr, GEN *pS, GEN *pT, GEN dataCR, GEN vChar, long prec);
    1894             : 
    1895             : /* compute S and T for the quadratic case. The following cases (cs) are:
    1896             :    1) bnr complex;
    1897             :    2) bnr real and no infinite place divide cond_chi (TBD);
    1898             :    3) bnr real and one infinite place divide cond_chi;
    1899             :    4) bnr real and both infinite places divide cond_chi (TBD) */
    1900             : static void
    1901         216 : QuadGetST(GEN bnr, GEN *pS, GEN *pT, GEN dataCR, GEN vChar, long prec)
    1902             : {
    1903         216 :   pari_sp av = avma, av1, av2;
    1904             :   long ncond, n, j, k, n0;
    1905         216 :   GEN N0, C, T = *pT, S = *pS, an, degs, cs;
    1906             :   LISTray LIST;
    1907             : 
    1908             :   /* initializations */
    1909         216 :   degs = GetDeg(dataCR);
    1910         216 :   ncond = lg(vChar)-1;
    1911         216 :   C    = cgetg(ncond+1, t_VEC);
    1912         216 :   N0   = cgetg(ncond+1, t_VECSMALL);
    1913         216 :   cs   = cgetg(ncond+1, t_VECSMALL);
    1914         216 :   n0 = 0;
    1915         450 :   for (j = 1; j <= ncond; j++)
    1916             :   {
    1917             :     /* FIXME: make sure that this value of c is correct for the general case */
    1918             :     long r1, r2, q;
    1919         264 :     GEN dtcr = gel(dataCR, mael(vChar,j,1)), p1 = ch_4(dtcr), c = ch_C(dtcr);
    1920             : 
    1921         264 :     gel(C,j) = c;
    1922         264 :     q = p1[1];
    1923             : 
    1924         264 :     nf_get_sign(bnr_get_nf(ch_bnr(dtcr)), &r1, &r2);
    1925         264 :     if (r1 == 2) /* real quadratic */
    1926             :     {
    1927         252 :       cs[j] = 2 + q;
    1928             :       /* FIXME:
    1929             :          make sure that this value of N0 is correct for the general case */
    1930         252 :       N0[j] = (long)prec2nbits_mul(prec, 0.35 * gtodouble(c));
    1931         252 :       if (cs[j] == 2 || cs[j] == 4) /* NOT IMPLEMENTED YET */
    1932             :       {
    1933          30 :         GetST0(bnr, pS, pT, dataCR, vChar, prec);
    1934         246 :         return;
    1935             :       }
    1936             :     }
    1937             :     else /* complex quadratic */
    1938             :     {
    1939          12 :       cs[j] = 1;
    1940          12 :       N0[j] = (long)prec2nbits_mul(prec, 0.7 * gtodouble(c));
    1941             :     }
    1942         234 :     if (n0 < N0[j]) n0 = N0[j];
    1943             :   }
    1944         186 :   if (DEBUGLEVEL>1) err_printf("N0 = %ld\n", n0);
    1945         186 :   InitPrimesQuad(bnr, n0, &LIST);
    1946             : 
    1947         186 :   av1 = avma;
    1948             :   /* loop over conductors */
    1949         402 :   for (j = 1; j <= ncond; j++)
    1950             :   {
    1951         216 :     GEN c0 = gel(C,j), c1 = divur(1, c0), c2 = divur(2, c0);
    1952         216 :     GEN ec1 = mpexp(c1), ec2 = mpexp(c2), LChar = gel(vChar,j);
    1953             :     GEN vf0, vf1, cf0, cf1;
    1954         216 :     const long nChar = lg(LChar)-1, NN = N0[j];
    1955             : 
    1956         216 :     if (DEBUGLEVEL>1)
    1957           0 :       err_printf("* conductor no %ld/%ld (N = %ld)\n\tInit: ", j,ncond,NN);
    1958         216 :     if (realprec(ec1) > prec) ec1 = rtor(ec1, prec);
    1959         216 :     if (realprec(ec2) > prec) ec2 = rtor(ec2, prec);
    1960         216 :     switch(cs[j])
    1961             :     {
    1962             :     case 1:
    1963          12 :       cf0 = gen_1;
    1964          12 :       cf1 = c0;
    1965          12 :       vf0 = mpveceint1(rtor(c1, prec), ec1, NN);
    1966          12 :       vf1 = mpvecpowdiv(invr(ec1), NN); break;
    1967             : 
    1968             :     case 3:
    1969         204 :       cf0 = sqrtr(mppi(prec));
    1970         204 :       cf1 = gmul2n(cf0, 1);
    1971         204 :       cf0 = gmul(cf0, c0);
    1972         204 :       vf0 = mpvecpowdiv(invr(ec2), NN);
    1973         204 :       vf1 = mpveceint1(rtor(c2, prec), ec2, NN); break;
    1974             : 
    1975             :     default:
    1976           0 :       cf0 = cf1 = NULL; /* FIXME: not implemented */
    1977           0 :       vf0 = vf1 = NULL;
    1978             :     }
    1979         900 :     for (k = 1; k <= nChar; k++)
    1980             :     {
    1981         684 :       const long t = LChar[k], d = degs[t];
    1982         684 :       const GEN dtcr = gel(dataCR, t), z = gel(ch_CHI(dtcr), 2);
    1983         684 :       GEN p1 = gen_0, p2 = gen_0;
    1984             :       int **matan;
    1985         684 :       long c = 0;
    1986             : 
    1987         684 :       if (DEBUGLEVEL>1)
    1988           0 :         err_printf("\tcharacter no: %ld (%ld/%ld)\n", t,k,nChar);
    1989         684 :       if (isintzero( ch_comp(gel(dataCR, t)) ))
    1990             :       {
    1991           6 :         if (DEBUGLEVEL>1) err_printf("\t  no need to compute this character\n");
    1992           6 :         continue;
    1993             :       }
    1994         678 :       av2 = avma;
    1995         678 :       matan = computean(gel(dataCR,t), &LIST, NN, d);
    1996     1360914 :       for (n = 1; n <= NN; n++)
    1997     1360236 :         if ((an = EvalCoeff(z, matan[n], d)))
    1998             :         {
    1999      285414 :           p1 = gadd(p1, gmul(an, gel(vf0,n)));
    2000      285414 :           p2 = gadd(p2, gmul(an, gel(vf1,n)));
    2001      285414 :           if (++c == 256) { gerepileall(av2,2, &p1,&p2); c = 0; }
    2002             :         }
    2003         678 :       gaffect(gmul(cf0, p1), gel(S,t));
    2004         678 :       gaffect(gmul(cf1,  gconj(p2)), gel(T,t));
    2005         678 :       FreeMat(matan,NN); avma = av2;
    2006             :     }
    2007         216 :     if (DEBUGLEVEL>1) err_printf("\n");
    2008         216 :     avma = av1;
    2009             :   }
    2010         186 :   avma = av;
    2011             : }
    2012             : 
    2013             : /* s += t*u. All 3 of them t_REAL, except we allow s or u = NULL (for 0) */
    2014             : static GEN
    2015    42327588 : _addmulrr(GEN s, GEN t, GEN u)
    2016             : {
    2017    42327588 :   if (u)
    2018             :   {
    2019    42102366 :     GEN v = mulrr(t, u);
    2020    42102366 :     return s? addrr(s, v): v;
    2021             :   }
    2022      225222 :   return s;
    2023             : }
    2024             : /* s += t. Both real, except we allow s or t = NULL (for exact 0) */
    2025             : static GEN
    2026    85450428 : _addrr(GEN s, GEN t)
    2027    85450428 : { return t? (s? addrr(s, t): t) : s; }
    2028             : 
    2029             : /* S & T for the general case. This is time-critical: optimize */
    2030             : static void
    2031      431310 : get_cS_cT(ST_t *T, long n)
    2032             : {
    2033             :   pari_sp av;
    2034             :   GEN csurn, nsurc, lncsurn, A, B, s, t, Z, aij, bij;
    2035             :   long i, j, r, i0;
    2036             : 
    2037      862620 :   if (T->cS[n]) return;
    2038             : 
    2039      204948 :   av = avma;
    2040      204948 :   aij = T->aij; i0= T->i0;
    2041      204948 :   bij = T->bij; r = T->r;
    2042      204948 :   Z = cgetg(r+1, t_VEC);
    2043      204948 :   gel(Z,1) = NULL; /* unused */
    2044             : 
    2045      204948 :   csurn = divru(T->c1, n);
    2046      204948 :   nsurc = invr(csurn);
    2047      204948 :   lncsurn = logr_abs(csurn);
    2048             : 
    2049      204948 :   if (r > 1)
    2050             :   {
    2051      204894 :     gel(Z,2) = lncsurn; /* r >= 2 */
    2052      208404 :     for (i = 3; i <= r; i++)
    2053        3510 :       gel(Z,i) = divru(mulrr(gel(Z,i-1), lncsurn), i-1);
    2054             :     /* Z[i] = ln^(i-1)(c1/n) / (i-1)! */
    2055             :   }
    2056             : 
    2057             :   /* i = i0 */
    2058      204948 :     A = gel(aij,i0); t = _addrr(NULL, gel(A,1));
    2059      204948 :     B = gel(bij,i0); s = _addrr(NULL, gel(B,1));
    2060      413352 :     for (j = 2; j <= r; j++)
    2061             :     {
    2062      208404 :       s = _addmulrr(s, gel(Z,j),gel(B,j));
    2063      208404 :       t = _addmulrr(t, gel(Z,j),gel(A,j));
    2064             :     }
    2065    42417792 :   for (i = i0 - 1; i > 1; i--)
    2066             :   {
    2067    42212844 :     A = gel(aij,i); if (t) t = mulrr(t, nsurc);
    2068    42212844 :     B = gel(bij,i); if (s) s = mulrr(s, nsurc);
    2069    62959830 :     for (j = odd(i)? T->rc2: T->rc1; j > 1; j--)
    2070             :     {
    2071    20746986 :       s = _addmulrr(s, gel(Z,j),gel(B,j));
    2072    20746986 :       t = _addmulrr(t, gel(Z,j),gel(A,j));
    2073             :     }
    2074    42212844 :     s = _addrr(s, gel(B,1));
    2075    42212844 :     t = _addrr(t, gel(A,1));
    2076             :   }
    2077             :   /* i = 1 */
    2078      204948 :     A = gel(aij,1); if (t) t = mulrr(t, nsurc);
    2079      204948 :     B = gel(bij,1); if (s) s = mulrr(s, nsurc);
    2080      204948 :     s = _addrr(s, gel(B,1));
    2081      204948 :     t = _addrr(t, gel(A,1));
    2082      413352 :     for (j = 2; j <= r; j++)
    2083             :     {
    2084      208404 :       s = _addmulrr(s, gel(Z,j),gel(B,j));
    2085      208404 :       t = _addmulrr(t, gel(Z,j),gel(A,j));
    2086             :     }
    2087      204948 :   s = _addrr(s, T->b? mulrr(csurn, gel(T->powracpi,T->b+1)): csurn);
    2088      204948 :   if (!s) s = gen_0;
    2089      204948 :   if (!t) t = gen_0;
    2090      204948 :   gel(T->cS,n) = gclone(s);
    2091      204948 :   gel(T->cT,n) = gclone(t); avma = av;
    2092             : }
    2093             : 
    2094             : static void
    2095         408 : clear_cScT(ST_t *T, long N)
    2096             : {
    2097         408 :   GEN cS = T->cS, cT = T->cT;
    2098             :   long i;
    2099     1300344 :   for (i=1; i<=N; i++)
    2100     1299936 :     if (cS[i]) {
    2101      204948 :       gunclone(gel(cS,i));
    2102      204948 :       gunclone(gel(cT,i)); gel(cS,i) = gel(cT,i) = NULL;
    2103             :     }
    2104         408 : }
    2105             : 
    2106             : static void
    2107         252 : init_cScT(ST_t *T, GEN dtcr, long N, long prec)
    2108             : {
    2109         252 :   GEN p1 = ch_4(dtcr);
    2110         252 :   T->a = p1[1];
    2111         252 :   T->b = p1[2];
    2112         252 :   T->c = p1[3];
    2113         252 :   T->rc1 = T->a + T->c;
    2114         252 :   T->rc2 = T->b + T->c;
    2115         252 :   T->r   = maxss(T->rc2+1, T->rc1); /* >= 2 */
    2116         252 :   ppgamma(T, prec);
    2117         252 :   clear_cScT(T, N);
    2118         252 : }
    2119             : 
    2120             : /* return a t_REAL */
    2121             : static GEN
    2122         432 : zeta_get_limx(long r1, long r2, long bit)
    2123             : {
    2124         432 :   pari_sp av = avma;
    2125             :   GEN p1, p2, c0, c1, A0;
    2126         432 :   long r = r1 + r2, N = r + r2;
    2127             : 
    2128             :   /* c1 = N 2^(-2r2 / N) */
    2129         432 :   c1 = mulrs(powrfrac(real2n(1, DEFAULTPREC), -2*r2, N), N);
    2130             : 
    2131         432 :   p1 = powru(Pi2n(1, DEFAULTPREC), r - 1);
    2132         432 :   p2 = mulir(powuu(N,r), p1); shiftr_inplace(p2, -r2);
    2133         432 :   c0 = sqrtr( divrr(p2, powru(c1, r+1)) );
    2134             : 
    2135         432 :   A0 = logr_abs( gmul2n(c0, bit) ); p2 = divrr(A0, c1);
    2136         432 :   p1 = divrr(mulur(N*(r+1), logr_abs(p2)), addsr(2*(r+1), gmul2n(A0,2)));
    2137         432 :   return gerepileuptoleaf(av, divrr(addrs(p1, 1), powruhalf(p2, N)));
    2138             : }
    2139             : /* N_0 = floor( C_K / limx ). Large */
    2140             : static long
    2141         528 : zeta_get_N0(GEN C,  GEN limx)
    2142             : {
    2143             :   long e;
    2144         528 :   pari_sp av = avma;
    2145         528 :   GEN z = gcvtoi(gdiv(C, limx), &e); /* avoid truncation error */
    2146         528 :   if (e >= 0 || is_bigint(z))
    2147           0 :     pari_err_OVERFLOW("zeta_get_N0 [need too many primes]");
    2148         528 :   if (DEBUGLEVEL>1) err_printf("\ninitzeta: N0 = %Ps\n", z);
    2149         528 :   avma = av; return itos(z);
    2150             : }
    2151             : 
    2152             : /* even i such that limx^i ( (i\2)! )^r1 ( i! )^r2 ~ B. Small. */
    2153             : static long
    2154         156 : get_i0(long r1, long r2, GEN B, GEN limx)
    2155             : {
    2156         156 :   long imin = 1, imax = 1400;
    2157        1716 :   while(imax - imin >= 4)
    2158             :   {
    2159        1404 :     long i = (imax + imin) >> 1;
    2160        1404 :     GEN t = powru(limx, i);
    2161        1404 :     if (!r1)      t = mulrr(t, powru(mpfactr(i  , DEFAULTPREC), r2));
    2162        1404 :     else if (!r2) t = mulrr(t, powru(mpfactr(i/2, DEFAULTPREC), r1));
    2163             :     else {
    2164           0 :       GEN u1 = mpfactr(i/2, DEFAULTPREC);
    2165           0 :       GEN u2 = mpfactr(i,   DEFAULTPREC);
    2166           0 :       if (r1 == r2) t = mulrr(t, powru(mulrr(u1,u2), r1));
    2167           0 :       else t = mulrr(t, mulrr(powru(u1,r1), powru(u2,r2)));
    2168             :     }
    2169        1404 :     if (mpcmp(t, B) >= 0) imax = i; else imin = i;
    2170             :   }
    2171         156 :   return imax & ~1; /* make it even */
    2172             : }
    2173             : /* assume limx = zeta_get_limx(r1, r2, bit), a t_REAL */
    2174             : static long
    2175         156 : zeta_get_i0(long r1, long r2, long bit, GEN limx)
    2176             : {
    2177         156 :   pari_sp av = avma;
    2178         156 :   GEN B = gmul(sqrtr( divrr(powrs(mppi(DEFAULTPREC), r2-3), limx) ),
    2179             :                gmul2n(powuu(5, r1), bit + r2));
    2180         156 :   long i0 = get_i0(r1, r2, B, limx);
    2181         156 :   if (DEBUGLEVEL>1) { err_printf("i0 = %ld\n",i0); err_flush(); }
    2182         156 :   avma = av; return i0;
    2183             : }
    2184             : 
    2185             : static void
    2186         156 : GetST0(GEN bnr, GEN *pS, GEN *pT, GEN dataCR, GEN vChar, long prec)
    2187             : {
    2188         156 :   pari_sp av = avma, av1, av2;
    2189             :   long ncond, n, j, k, jc, n0, prec2, i0, r1, r2;
    2190         156 :   GEN nf = checknf(bnr), T = *pT, S = *pS;
    2191             :   GEN N0, C, an, degs, limx;
    2192             :   LISTray LIST;
    2193             :   ST_t cScT;
    2194             : 
    2195             :   /* initializations */
    2196         156 :   degs = GetDeg(dataCR);
    2197         156 :   ncond = lg(vChar)-1;
    2198         156 :   nf_get_sign(nf,&r1,&r2);
    2199             : 
    2200         156 :   C  = cgetg(ncond+1, t_VEC);
    2201         156 :   N0 = cgetg(ncond+1, t_VECSMALL);
    2202         156 :   n0 = 0;
    2203         156 :   limx = zeta_get_limx(r1, r2, prec2nbits(prec));
    2204         408 :   for (j = 1; j <= ncond; j++)
    2205             :   {
    2206         252 :     GEN dtcr = gel(dataCR, mael(vChar,j,1)), c = ch_C(dtcr);
    2207         252 :     gel(C,j) = c;
    2208         252 :     N0[j] = zeta_get_N0(c, limx);
    2209         252 :     if (n0 < N0[j]) n0  = N0[j];
    2210             :   }
    2211         156 :   i0 = zeta_get_i0(r1, r2, prec2nbits(prec), limx);
    2212         156 :   InitPrimes(bnr, n0, &LIST);
    2213             : 
    2214         156 :   prec2 = precdbl(prec) + EXTRA_PREC;
    2215         156 :   cScT.powracpi = powersr(sqrtr(mppi(prec2)), r1);
    2216             : 
    2217         156 :   cScT.cS = cgetg(n0+1, t_VEC);
    2218         156 :   cScT.cT = cgetg(n0+1, t_VEC);
    2219         156 :   for (j=1; j<=n0; j++) gel(cScT.cS,j) = gel(cScT.cT,j) = NULL;
    2220             : 
    2221         156 :   cScT.i0 = i0;
    2222             : 
    2223         156 :   av1 = avma;
    2224         408 :   for (jc = 1; jc <= ncond; jc++)
    2225             :   {
    2226         252 :     const GEN LChar = gel(vChar,jc);
    2227         252 :     const long nChar = lg(LChar)-1, NN = N0[jc];
    2228             : 
    2229         252 :     if (DEBUGLEVEL>1)
    2230           0 :       err_printf("* conductor no %ld/%ld (N = %ld)\n\tInit: ", jc,ncond,NN);
    2231             : 
    2232         252 :     cScT.c1 = gel(C,jc);
    2233         252 :     init_cScT(&cScT, gel(dataCR, LChar[1]), NN, prec2);
    2234         252 :     av2 = avma;
    2235         726 :     for (k = 1; k <= nChar; k++)
    2236             :     {
    2237         474 :       const long t = LChar[k];
    2238         474 :       if (DEBUGLEVEL>1)
    2239           0 :         err_printf("\tcharacter no: %ld (%ld/%ld)\n", t,k,nChar);
    2240             : 
    2241         474 :       if (!isintzero( ch_comp(gel(dataCR, t)) ))
    2242             :       {
    2243         468 :         const long d = degs[t];
    2244         468 :         const GEN dtcr = gel(dataCR, t), z = gel(ch_CHI(dtcr), 2);
    2245         468 :         GEN p1 = gen_0, p2 = gen_0;
    2246         468 :         long c = 0;
    2247         468 :         int **matan = ComputeCoeff(gel(dataCR,t), &LIST, NN, d);
    2248     1537830 :         for (n = 1; n <= NN; n++)
    2249     1537362 :           if ((an = EvalCoeff(z, matan[n], d)))
    2250             :           {
    2251      431310 :            get_cS_cT(&cScT, n);
    2252      431310 :            p1 = gadd(p1, gmul(an, gel(cScT.cS,n)));
    2253      431310 :            p2 = gadd(p2, gmul(an, gel(cScT.cT,n)));
    2254      431310 :            if (++c == 256) { gerepileall(av2,2, &p1,&p2); c = 0; }
    2255             :           }
    2256         468 :         gaffect(p1,        gel(S,t));
    2257         468 :         gaffect(gconj(p2), gel(T,t));
    2258         468 :         FreeMat(matan, NN); avma = av2;
    2259             :       }
    2260           6 :       else if (DEBUGLEVEL>1)
    2261           0 :         err_printf("\t  no need to compute this character\n");
    2262             :     }
    2263         252 :     if (DEBUGLEVEL>1) err_printf("\n");
    2264         252 :     avma = av1;
    2265             :   }
    2266         156 :   clear_cScT(&cScT, n0);
    2267         156 :   avma = av;
    2268         156 : }
    2269             : 
    2270             : static void
    2271         342 : GetST(GEN bnr, GEN *pS, GEN *pT, GEN dataCR, GEN vChar, long prec)
    2272             : {
    2273         342 :   const long cl = lg(dataCR) - 1;
    2274         342 :   GEN S, T, nf  = checknf(bnr);
    2275             :   long j;
    2276             : 
    2277             :   /* allocate memory for answer */
    2278         342 :   *pS = S = cgetg(cl+1, t_VEC);
    2279         342 :   *pT = T = cgetg(cl+1, t_VEC);
    2280        1500 :   for (j = 1; j <= cl; j++)
    2281             :   {
    2282        1158 :     gel(S,j) = cgetc(prec);
    2283        1158 :     gel(T,j) = cgetc(prec);
    2284             :   }
    2285         342 :   if (nf_get_degree(nf) == 2)
    2286         216 :     QuadGetST(bnr, pS, pT, dataCR, vChar, prec);
    2287             :   else
    2288         126 :     GetST0(bnr, pS, pT, dataCR, vChar, prec);
    2289         342 : }
    2290             : 
    2291             : /*******************************************************************/
    2292             : /*                                                                 */
    2293             : /*     Class fields of real quadratic fields using Stark units     */
    2294             : /*                                                                 */
    2295             : /*******************************************************************/
    2296             : /* compute the Hilbert class field using genus class field theory when
    2297             :    the exponent of the class group is exactly 2 (trivial group not covered) */
    2298             : /* Cf Herz, Construction of class fields, LNM 21, Theorem 1 (VII-6) */
    2299             : static GEN
    2300          12 : GenusFieldQuadReal(GEN disc)
    2301             : {
    2302          12 :   long i, i0 = 0, l;
    2303          12 :   pari_sp av = avma;
    2304          12 :   GEN T = NULL, p0 = NULL, P;
    2305             : 
    2306          12 :   P = gel(Z_factor(disc), 1);
    2307          12 :   l = lg(P);
    2308          36 :   for (i = 1; i < l; i++)
    2309             :   {
    2310          30 :     GEN p = gel(P,i);
    2311          30 :     if (mod4(p) == 3) { p0 = p; i0 = i; break; }
    2312             :   }
    2313          12 :   l--; /* remove last prime */
    2314          12 :   if (i0 == l) l--; /* ... remove p0 and last prime */
    2315          42 :   for (i = 1; i < l; i++)
    2316             :   {
    2317          30 :     GEN p = gel(P,i), d, t;
    2318          30 :     if (i == i0) continue;
    2319          24 :     if (absequaliu(p, 2))
    2320          12 :       switch (mod32(disc))
    2321             :       {
    2322          12 :         case  8: d = gen_2; break;
    2323           0 :         case 24: d = shifti(p0, 1); break;
    2324           0 :         default: d = p0; break;
    2325             :       }
    2326             :     else
    2327          12 :       d = (mod4(p) == 1)? p: mulii(p0, p);
    2328          24 :     t = mkpoln(3, gen_1, gen_0, negi(d)); /* x^2 - d */
    2329          24 :     T = T? ZX_compositum_disjoint(T, t): t;
    2330             :   }
    2331          12 :   return gerepileupto(av, polredbest(T, 0));
    2332             : }
    2333             : static GEN
    2334         348 : GenusFieldQuadImag(GEN disc)
    2335             : {
    2336             :   long i, l;
    2337         348 :   pari_sp av = avma;
    2338         348 :   GEN T = NULL, P;
    2339             : 
    2340         348 :   P = gel(absZ_factor(disc), 1);
    2341         348 :   l = lg(P);
    2342         348 :   l--; /* remove last prime */
    2343        1014 :   for (i = 1; i < l; i++)
    2344             :   {
    2345         666 :     GEN p = gel(P,i), d, t;
    2346         666 :     if (absequaliu(p, 2))
    2347         198 :       switch (mod32(disc))
    2348             :       {
    2349          48 :         case 24: d = gen_2; break;  /* disc = 8 mod 32 */
    2350          36 :         case  8: d = gen_m2; break; /* disc =-8 mod 32 */
    2351         114 :         default: d = gen_m1; break;
    2352             :       }
    2353             :     else
    2354         468 :       d = (mod4(p) == 1)? p: negi(p);
    2355         666 :     t = mkpoln(3, gen_1, gen_0, negi(d)); /* x^2 - d */
    2356         666 :     T = T? ZX_compositum_disjoint(T, t): t;
    2357             :   }
    2358         348 :   return gerepileupto(av, polredbest(T, 0));
    2359             : }
    2360             : 
    2361             : /* if flag != 0, computes a fast and crude approximation of the result */
    2362             : static GEN
    2363         552 : AllStark(GEN data,  GEN nf,  long flag,  long newprec)
    2364             : {
    2365         552 :   const long BND = 300;
    2366         552 :   long cl, i, j, cpt = 0, N, h, v, n, r1, r2, den;
    2367             :   pari_sp av, av2;
    2368             :   int **matan;
    2369         552 :   GEN bnr = gel(data,1), p1, p2, S, T, polrelnum, polrel, Lp, W, veczeta;
    2370             :   GEN vChar, degs, C, dataCR, cond1, L1, an;
    2371             :   LISTray LIST;
    2372             :   pari_timer ti;
    2373             : 
    2374         552 :   nf_get_sign(nf, &r1,&r2);
    2375         552 :   N     = nf_get_degree(nf);
    2376         552 :   cond1 = gel(bnr_get_mod(bnr), 2);
    2377         552 :   dataCR = gel(data,5);
    2378         552 :   vChar = sortChars(dataCR);
    2379             : 
    2380         552 :   v = 1;
    2381         552 :   while (gequal1(gel(cond1,v))) v++;
    2382             : 
    2383         552 :   cl = lg(dataCR)-1;
    2384         552 :   degs = GetDeg(dataCR);
    2385         552 :   h  = itos(ZM_det_triangular(gel(data,2))) >> 1;
    2386             : 
    2387             : LABDOUB:
    2388         570 :   if (DEBUGLEVEL) timer_start(&ti);
    2389         570 :   av = avma;
    2390             : 
    2391             :   /* characters with rank > 1 should not be computed */
    2392        2436 :   for (i = 1; i <= cl; i++)
    2393             :   {
    2394        1866 :     GEN chi = gel(dataCR, i);
    2395        1866 :     if (L_vanishes_at_0(chi)) ch_comp(chi) = gen_0;
    2396             :   }
    2397             : 
    2398         570 :   W = ComputeAllArtinNumbers(dataCR, vChar, (flag >= 0), newprec);
    2399         570 :   if (DEBUGLEVEL) timer_printf(&ti,"Compute W");
    2400         570 :   Lp = cgetg(cl + 1, t_VEC);
    2401         570 :   if (!flag)
    2402             :   {
    2403         294 :     GetST(bnr, &S, &T, dataCR, vChar, newprec);
    2404         294 :     if (DEBUGLEVEL) timer_printf(&ti, "S&T");
    2405        1260 :     for (i = 1; i <= cl; i++)
    2406             :     {
    2407         966 :       GEN chi = gel(dataCR, i), v = gen_0;
    2408         966 :       if (!isintzero( ch_comp(chi) ))
    2409         954 :         v = gel(GetValue(chi, gel(W,i), gel(S,i), gel(T,i), 2, newprec), 2);
    2410         966 :       gel(Lp, i) = v;
    2411             :     }
    2412             :   }
    2413             :   else
    2414             :   { /* compute a crude approximation of the result */
    2415         276 :     C = cgetg(cl + 1, t_VEC);
    2416         276 :     for (i = 1; i <= cl; i++) gel(C,i) = ch_C(gel(dataCR, i));
    2417         276 :     n = zeta_get_N0(vecmax(C), zeta_get_limx(r1, r2, prec2nbits(newprec)));
    2418         276 :     if (n > BND) n = BND;
    2419         276 :     if (DEBUGLEVEL) err_printf("N0 in QuickPol: %ld \n", n);
    2420         276 :     InitPrimes(bnr, n, &LIST);
    2421             : 
    2422         276 :     L1 = cgetg(cl+1, t_VEC);
    2423             :     /* use L(1) = sum (an / n) */
    2424        1176 :     for (i = 1; i <= cl; i++)
    2425             :     {
    2426         900 :       GEN dtcr = gel(dataCR,i);
    2427         900 :       matan = ComputeCoeff(dtcr, &LIST, n, degs[i]);
    2428         900 :       av2 = avma;
    2429         900 :       p1 = real_0(newprec); p2 = gel(ch_CHI(dtcr), 2);
    2430      266028 :       for (j = 1; j <= n; j++)
    2431      265128 :         if ( (an = EvalCoeff(p2, matan[j], degs[i])) )
    2432       93360 :           p1 = gadd(p1, gdivgs(an, j));
    2433         900 :       gel(L1,i) = gerepileupto(av2, p1);
    2434         900 :       FreeMat(matan, n);
    2435             :     }
    2436         276 :     p1 = gmul2n(powruhalf(mppi(newprec), N-2), 1);
    2437             : 
    2438        1176 :     for (i = 1; i <= cl; i++)
    2439             :     {
    2440             :       long r;
    2441         900 :       GEN WW, A = ComputeAChi(gel(dataCR,i), &r, 0, newprec);
    2442         900 :       WW = gmul(gel(C,i), gmul(A, gel(W,i)));
    2443         900 :       gel(Lp,i) = gdiv(gmul(WW, gconj(gel(L1,i))), p1);
    2444             :     }
    2445             :   }
    2446             : 
    2447         570 :   p1 = ComputeLift(gel(data,4));
    2448             : 
    2449         570 :   den = flag ? h: 2*h;
    2450         570 :   veczeta = cgetg(h + 1, t_VEC);
    2451        3612 :   for (i = 1; i <= h; i++)
    2452             :   {
    2453        3042 :     GEN z = gen_0, sig = gel(p1,i);
    2454       15570 :     for (j = 1; j <= cl; j++)
    2455             :     {
    2456       12528 :       GEN dtcr = gel(dataCR,j), CHI = ch_CHI(dtcr);
    2457       12528 :       GEN t = mulreal(gel(Lp,j), CharEval(CHI, sig));
    2458       12528 :       if (chi_get_deg(CHI) != 2) t = gmul2n(t, 1); /* character not real */
    2459       12528 :       z = gadd(z, t);
    2460             :     }
    2461        3042 :     gel(veczeta,i) = gdivgs(z, den);
    2462             :   }
    2463        3612 :   for (j = 1; j <= h; j++)
    2464        3042 :     gel(veczeta,j) = gmul2n(gcosh(gel(veczeta,j), newprec), 1);
    2465         570 :   polrelnum = roots_to_pol(veczeta, 0);
    2466         570 :   if (DEBUGLEVEL)
    2467             :   {
    2468           0 :     if (DEBUGLEVEL>1) {
    2469           0 :       err_printf("polrelnum = %Ps\n", polrelnum);
    2470           0 :       err_printf("zetavalues = %Ps\n", veczeta);
    2471           0 :       if (!flag)
    2472           0 :         err_printf("Checking the square-root of the Stark unit...\n");
    2473             :     }
    2474           0 :     timer_printf(&ti, "Compute %s", flag? "quickpol": "polrelnum");
    2475             :   }
    2476             : 
    2477         570 :   if (flag)
    2478         276 :     return gerepilecopy(av, polrelnum);
    2479             : 
    2480             :   /* try to recognize this polynomial */
    2481         294 :   polrel = RecCoeff(nf, polrelnum, v, newprec);
    2482         294 :   if (!polrel)
    2483             :   {
    2484        1428 :     for (j = 1; j <= h; j++)
    2485        1224 :       gel(veczeta,j) = gsubgs(gsqr(gel(veczeta,j)), 2);
    2486         204 :     polrelnum = roots_to_pol(veczeta, 0);
    2487         204 :     if (DEBUGLEVEL)
    2488             :     {
    2489           0 :       if (DEBUGLEVEL>1) {
    2490           0 :         err_printf("It's not a square...\n");
    2491           0 :         err_printf("polrelnum = %Ps\n", polrelnum);
    2492             :       }
    2493           0 :       timer_printf(&ti, "Compute polrelnum");
    2494             :     }
    2495         204 :     polrel = RecCoeff(nf, polrelnum, v, newprec);
    2496             :   }
    2497         294 :   if (!polrel) /* FAILED */
    2498             :   {
    2499          18 :     const long EXTRA_BITS = 64;
    2500             :     long incr_pr;
    2501          18 :     if (++cpt >= 3) pari_err_PREC( "stark (computation impossible)");
    2502             :     /* estimate needed precision */
    2503          18 :     incr_pr = prec2nbits(gprecision(polrelnum))- gexpo(polrelnum);
    2504          18 :     if (incr_pr < 0) incr_pr = -incr_pr + EXTRA_BITS;
    2505          18 :     newprec += nbits2extraprec(maxss(3*EXTRA_BITS, cpt*incr_pr));
    2506          18 :     if (DEBUGLEVEL) pari_warn(warnprec, "AllStark", newprec);
    2507             : 
    2508          18 :     nf = nfnewprec_shallow(nf, newprec);
    2509          18 :     dataCR = CharNewPrec(dataCR, nf, newprec);
    2510             : 
    2511          18 :     gerepileall(av, 2, &nf, &dataCR);
    2512          18 :     goto LABDOUB;
    2513             :   }
    2514             : 
    2515         276 :   if (DEBUGLEVEL) {
    2516           0 :     if (DEBUGLEVEL>1) err_printf("polrel = %Ps\n", polrel);
    2517           0 :     timer_printf(&ti, "Recpolnum");
    2518             :   }
    2519         276 :   return gerepilecopy(av, polrel);
    2520             : }
    2521             : 
    2522             : /********************************************************************/
    2523             : /*                        Main functions                            */
    2524             : /********************************************************************/
    2525             : 
    2526             : static GEN
    2527         222 : get_subgroup(GEN H, GEN cyc, const char *s)
    2528             : {
    2529         222 :   if (!H || gequal0(H)) return diagonal_shallow(cyc);
    2530          18 :   if (typ(H) != t_MAT) pari_err_TYPE(stack_strcat(s," [subgroup]"), H);
    2531          12 :   RgM_check_ZM(H, s);
    2532          12 :   return ZM_hnfmodid(H, cyc);
    2533             : }
    2534             : 
    2535             : GEN
    2536         174 : bnrstark(GEN bnr, GEN subgrp, long prec)
    2537             : {
    2538             :   long N, newprec;
    2539         174 :   pari_sp av = avma;
    2540             :   GEN bnf, p1, cycbnr, nf, data, dtQ;
    2541             : 
    2542             :   /* check the bnr */
    2543         174 :   checkbnr(bnr);
    2544         174 :   bnf = checkbnf(bnr);
    2545         174 :   nf  = bnf_get_nf(bnf);
    2546         174 :   N   = nf_get_degree(nf);
    2547         174 :   if (N == 1) return galoissubcyclo(bnr, subgrp, 0, 0);
    2548             : 
    2549             :   /* check the bnf */
    2550         174 :   if (!nf_get_varn(nf))
    2551           0 :     pari_err_PRIORITY("bnrstark", nf_get_pol(nf), "=", 0);
    2552         174 :   if (nf_get_r2(nf)) pari_err_DOMAIN("bnrstark", "r2", "!=", gen_0, nf);
    2553         168 :   subgrp = get_subgroup(subgrp,bnr_get_cyc(bnr),"bnrstark");
    2554             : 
    2555             :   /* compute bnr(conductor) */
    2556         168 :   p1     = bnrconductor_i(bnr, subgrp, 2);
    2557         168 :   bnr    = gel(p1,2); cycbnr = bnr_get_cyc(bnr);
    2558         168 :   subgrp = gel(p1,3);
    2559         168 :   if (gequal1( ZM_det_triangular(subgrp) )) { avma = av; return pol_x(0); }
    2560             : 
    2561             :   /* check the class field */
    2562         168 :   if (!gequal0(gel(bnr_get_mod(bnr), 2)))
    2563           6 :     pari_err_DOMAIN("bnrstark", "r2(class field)", "!=", gen_0, bnr);
    2564             : 
    2565             :   /* find a suitable extension N */
    2566         162 :   dtQ = InitQuotient(subgrp);
    2567         162 :   data  = FindModulus(bnr, dtQ, &newprec);
    2568         162 :   if (!data)
    2569             :   {
    2570           0 :     GEN vec, H, cyc = gel(dtQ,2), U = gel(dtQ,3), M = RgM_inv(U);
    2571           0 :     long i, j = 1, l = lg(M);
    2572             : 
    2573             :     /* M = indep. generators of Cl_f/subgp, restrict to cyclic components */
    2574           0 :     vec = cgetg(l, t_VEC);
    2575           0 :     for (i = 1; i < l; i++)
    2576             :     {
    2577           0 :       if (is_pm1(gel(cyc,i))) continue;
    2578           0 :       H = ZM_hnfmodid(vecsplice(M,i), cycbnr);
    2579           0 :       gel(vec,j++) = bnrstark(bnr, H, prec);
    2580             :     }
    2581           0 :     setlg(vec, j); return gerepilecopy(av, vec);
    2582             :   }
    2583             : 
    2584         162 :   if (newprec > prec)
    2585             :   {
    2586           0 :     if (DEBUGLEVEL>1) err_printf("new precision: %ld\n", newprec);
    2587           0 :     nf = nfnewprec_shallow(nf, newprec);
    2588             :   }
    2589         162 :   return gerepileupto(av, AllStark(data, nf, 0, newprec));
    2590             : }
    2591             : 
    2592             : /* For each character of Cl(bnr)/subgp, compute L(1, chi) (or equivalently
    2593             :  * the first non-zero term c(chi) of the expansion at s = 0).
    2594             :  * If flag & 1: compute the value at s = 1 (for non-trivial characters),
    2595             :  * else compute the term c(chi) and return [r(chi), c(chi)] where r(chi) is
    2596             :  *   the order of L(s, chi) at s = 0.
    2597             :  * If flag & 2: compute the value of the L-function L_S(s, chi) where S is the
    2598             :  *   set of places dividing the modulus of bnr (and the infinite places),
    2599             :  * else
    2600             :  *   compute the value of the primitive L-function attached to chi,
    2601             :  * If flag & 4: return also the character */
    2602             : GEN
    2603          54 : bnrL1(GEN bnr, GEN subgp, long flag, long prec)
    2604             : {
    2605             :   GEN cyc, L1, allCR, listCR;
    2606             :   GEN indCR, invCR, Qt;
    2607             :   long cl, i, nc;
    2608          54 :   pari_sp av = avma;
    2609             : 
    2610          54 :   checkbnr(bnr);
    2611          54 :   if (flag < 0 || flag > 8) pari_err_FLAG("bnrL1");
    2612             : 
    2613          54 :   cyc  = bnr_get_cyc(bnr);
    2614          54 :   subgp = get_subgroup(subgp, cyc, "bnrL1");
    2615             : 
    2616          48 :   Qt = InitQuotient(subgp);
    2617          48 :   cl = itou(gel(Qt,1));
    2618             : 
    2619             :   /* compute all characters */
    2620          48 :   allCR = EltsOfGroup(cl, gel(Qt,2));
    2621             : 
    2622             :   /* make a list of all non-trivial characters modulo conjugation */
    2623          48 :   listCR = cgetg(cl, t_VEC);
    2624          48 :   indCR = cgetg(cl, t_VECSMALL);
    2625          48 :   invCR = cgetg(cl, t_VECSMALL); nc = 0;
    2626         348 :   for (i = 1; i < cl; i++)
    2627             :   {
    2628             :     /* lift to a character on Cl(bnr) */
    2629         300 :     GEN lchi = LiftChar(Qt, cyc, gel(allCR,i));
    2630         300 :     GEN clchi = charconj(cyc, lchi);
    2631         300 :     long j, a = 0;
    2632        1158 :     for (j = 1; j <= nc; j++)
    2633         966 :       if (ZV_equal(gmael(listCR, j, 1), clchi)) { a = j; break; }
    2634             : 
    2635         300 :     if (!a)
    2636             :     {
    2637         192 :       nc++;
    2638         192 :       gel(listCR,nc) = mkvec2(lchi, bnrconductorofchar(bnr, lchi));
    2639         192 :       indCR[i]  = nc;
    2640         192 :       invCR[nc] = i;
    2641             :     }
    2642             :     else
    2643         108 :       indCR[i] = -invCR[a];
    2644             : 
    2645         300 :     gel(allCR,i) = lchi;
    2646             :   }
    2647          48 :   settyp(allCR[cl], t_VEC); /* set correct type for trivial character */
    2648             : 
    2649          48 :   setlg(listCR, nc + 1);
    2650          48 :   L1 = cgetg((flag&1)? cl: cl+1, t_VEC);
    2651          48 :   if (nc)
    2652             :   {
    2653          48 :     GEN dataCR = InitChar(bnr, listCR, prec);
    2654          48 :     GEN W, S, T, vChar = sortChars(dataCR);
    2655          48 :     GetST(bnr, &S, &T, dataCR, vChar, prec);
    2656          48 :     W = ComputeAllArtinNumbers(dataCR, vChar, 1, prec);
    2657         348 :     for (i = 1; i < cl; i++)
    2658             :     {
    2659         300 :       long a = indCR[i];
    2660         300 :       if (a > 0)
    2661         192 :         gel(L1,i) = GetValue(gel(dataCR,a), gel(W,a), gel(S,a), gel(T,a),
    2662             :                              flag, prec);
    2663             :       else
    2664         108 :         gel(L1,i) = gconj(gel(L1,-a));
    2665             :     }
    2666             :   }
    2667          48 :   if (!(flag & 1))
    2668          18 :     gel(L1,cl) = GetValue1(bnr, flag & 2, prec);
    2669             :   else
    2670          30 :     cl--;
    2671             : 
    2672          48 :   if (flag & 4) {
    2673          24 :     for (i = 1; i <= cl; i++) gel(L1,i) = mkvec2(gel(allCR,i), gel(L1,i));
    2674             :   }
    2675          48 :   return gerepilecopy(av, L1);
    2676             : }
    2677             : 
    2678             : /*******************************************************************/
    2679             : /*                                                                 */
    2680             : /*       Hilbert and Ray Class field using Stark                   */
    2681             : /*                                                                 */
    2682             : /*******************************************************************/
    2683             : /* P in A[x,y], deg_y P < 2, return P0 and P1 in A[x] such that P = P0 + P1 y */
    2684             : static void
    2685         114 : split_pol_quad(GEN P, GEN *gP0, GEN *gP1)
    2686             : {
    2687         114 :   long i, l = lg(P);
    2688         114 :   GEN P0 = cgetg(l, t_POL), P1 = cgetg(l, t_POL);
    2689         114 :   P0[1] = P1[1] = P[1];
    2690        1038 :   for (i = 2; i < l; i++)
    2691             :   {
    2692         924 :     GEN c = gel(P,i), c0 = c, c1 = gen_0;
    2693         924 :     if (typ(c) == t_POL) /* write c = c1 y + c0 */
    2694         810 :       switch(degpol(c))
    2695             :       {
    2696           0 :         case -1: c0 = gen_0; break;
    2697         810 :         default: c1 = gel(c,3); /* fall through */
    2698         810 :         case  0: c0 = gel(c,2); break;
    2699             :       }
    2700         924 :     gel(P0,i) = c0; gel(P1,i) = c1;
    2701             :   }
    2702         114 :   *gP0 = normalizepol_lg(P0, l);
    2703         114 :   *gP1 = normalizepol_lg(P1, l);
    2704         114 : }
    2705             : 
    2706             : /* k = nf quadratic field, P relative equation of H_k (Hilbert class field)
    2707             :  * return T in Z[X], such that H_k / Q is the compositum of Q[X]/(T) and k */
    2708             : static GEN
    2709         114 : makescind(GEN nf, GEN P)
    2710             : {
    2711         114 :   GEN Pp, p, pol, G, L, a, roo, P0,P1, Ny,Try, nfpol = nf_get_pol(nf);
    2712             :   long i, is_P;
    2713             : 
    2714         114 :   P = lift_shallow(P);
    2715         114 :   split_pol_quad(P, &P0, &P1);
    2716             :   /* P = P0 + y P1, Norm_{k/Q}(P) = P0^2 + Tr y P0P1 + Ny P1^2, irreducible/Q */
    2717         114 :   Ny = gel(nfpol, 2);
    2718         114 :   Try = negi(gel(nfpol, 3));
    2719         114 :   pol = RgX_add(RgX_sqr(P0), RgX_Rg_mul(RgX_sqr(P1), Ny));
    2720         114 :   if (signe(Try)) pol = RgX_add(pol, RgX_Rg_mul(RgX_mul(P0,P1), Try));
    2721             :   /* pol = rnfequation(nf, P); */
    2722         114 :   G = galoisinit(pol, NULL);
    2723         114 :   L = gal_get_group(G);
    2724         114 :   p = gal_get_p(G);
    2725         114 :   a = FpX_quad_root(nfpol, p, 0);
    2726             :   /* P mod a prime \wp above p (which splits) */
    2727         114 :   Pp = FpXY_evalx(P, a, p);
    2728         114 :   roo = gal_get_roots(G);
    2729         114 :   is_P = gequal0( FpX_eval(Pp, remii(gel(roo,1),p), p) );
    2730             :   /* each roo[i] mod p is a root of P or (exclusive) tau(P) mod \wp */
    2731             :   /* record whether roo[1] is a root of P or tau(P) */
    2732             : 
    2733         924 :   for (i = 1; i < lg(L); i++)
    2734             :   {
    2735         924 :     GEN perm = gel(L,i);
    2736         924 :     long k = perm[1]; if (k == 1) continue;
    2737         810 :     k = gequal0( FpX_eval(Pp, remii(gel(roo,k),p), p) );
    2738             :     /* roo[k] is a root of the other polynomial */
    2739         810 :     if (k != is_P)
    2740             :     {
    2741         114 :       long o = perm_order(perm);
    2742         114 :       if (o != 2) perm = perm_pow(perm, o >> 1);
    2743             :       /* perm has order two and doesn't belong to Gal(H_k/k) */
    2744         114 :       return galoisfixedfield(G, perm, 1, varn(P));
    2745             :     }
    2746             :   }
    2747           0 :   pari_err_BUG("makescind");
    2748             :   return NULL; /*LCOV_EXCL_LINE*/
    2749             : }
    2750             : 
    2751             : /* pbnf = NULL if no bnf is needed, f = NULL may be passed for a trivial
    2752             :  * conductor */
    2753             : static void
    2754         726 : quadray_init(GEN *pD, GEN f, GEN *pbnf, long prec)
    2755             : {
    2756         726 :   GEN D = *pD, nf, bnf = NULL;
    2757         726 :   if (typ(D) == t_INT)
    2758             :   {
    2759             :     int isfund;
    2760         696 :     if (pbnf) {
    2761         216 :       long v = f? gvar(f): NO_VARIABLE;
    2762         216 :       if (v == NO_VARIABLE) v = 1;
    2763         216 :       bnf = Buchall(quadpoly0(D, v), nf_FORCE, prec);
    2764         216 :       nf = bnf_get_nf(bnf);
    2765         216 :       isfund = equalii(D, nf_get_disc(nf));
    2766             :     }
    2767             :     else
    2768         480 :       isfund = Z_isfundamental(D);
    2769         696 :     if (!isfund) pari_err_DOMAIN("quadray", "isfundamental(D)", "=",gen_0, D);
    2770             :   }
    2771             :   else
    2772             :   {
    2773          30 :     bnf = checkbnf(D);
    2774          30 :     nf = bnf_get_nf(bnf);
    2775          30 :     if (nf_get_degree(nf) != 2)
    2776           6 :       pari_err_DOMAIN("quadray", "degree", "!=", gen_2, nf_get_pol(nf));
    2777          24 :     D = nf_get_disc(nf);
    2778             :   }
    2779         714 :   if (pbnf) *pbnf = bnf;
    2780         714 :   *pD = D;
    2781         714 : }
    2782             : 
    2783             : /* compute the polynomial over Q of the Hilbert class field of
    2784             :    Q(sqrt(D)) where D is a positive fundamental discriminant */
    2785             : static GEN
    2786         126 : quadhilbertreal(GEN D, long prec)
    2787             : {
    2788         126 :   pari_sp av = avma;
    2789             :   long newprec;
    2790             :   GEN bnf;
    2791             :   VOLATILE GEN bnr, dtQ, data, nf, cyc, M;
    2792             :   pari_timer ti;
    2793         126 :   if (DEBUGLEVEL) timer_start(&ti);
    2794             : 
    2795             :   (void)&prec; /* prevent longjmp clobbering it */
    2796             :   (void)&bnf;  /* prevent longjmp clobbering it, avoid warning due to
    2797             :                 * quadray_init call : discards qualifiers from pointer type */
    2798         126 :   quadray_init(&D, NULL, &bnf, prec);
    2799         126 :   cyc = bnf_get_cyc(bnf);
    2800         126 :   if (lg(cyc) == 1) { avma = av; return pol_x(0); }
    2801             :   /* if the exponent of the class group is 2, use Genus Theory */
    2802         126 :   if (absequaliu(gel(cyc,1), 2)) return gerepileupto(av, GenusFieldQuadReal(D));
    2803             : 
    2804         114 :   bnr  = Buchray(bnf, gen_1, nf_INIT);
    2805         114 :   M = diagonal_shallow(bnr_get_cyc(bnr));
    2806         114 :   dtQ = InitQuotient(M);
    2807         114 :   nf  = bnf_get_nf(bnf);
    2808             : 
    2809             :   for(;;) {
    2810         114 :     VOLATILE GEN pol = NULL;
    2811         114 :     pari_CATCH(e_PREC) {
    2812           0 :       prec += EXTRA_PREC;
    2813           0 :       if (DEBUGLEVEL) pari_warn(warnprec, "quadhilbertreal", prec);
    2814           0 :       bnr = bnrnewprec_shallow(bnr, prec);
    2815           0 :       bnf = bnr_get_bnf(bnr);
    2816           0 :       nf  = bnf_get_nf(bnf);
    2817             :     } pari_TRY {
    2818             :       /* find the modulus defining N */
    2819             :       pari_timer T;
    2820         114 :       if (DEBUGLEVEL) timer_start(&T);
    2821         114 :       data = FindModulus(bnr, dtQ, &newprec);
    2822         114 :       if (DEBUGLEVEL) timer_printf(&T,"FindModulus");
    2823         114 :       if (!data)
    2824             :       {
    2825           0 :         long i, l = lg(M);
    2826           0 :         GEN vec = cgetg(l, t_VEC);
    2827           0 :         for (i = 1; i < l; i++)
    2828             :         {
    2829           0 :           GEN t = gcoeff(M,i,i);
    2830           0 :           gcoeff(M,i,i) = gen_1;
    2831           0 :           gel(vec,i) = bnrstark(bnr, M, prec);
    2832           0 :           gcoeff(M,i,i) = t;
    2833             :         }
    2834           0 :         return gerepileupto(av, vec);
    2835             :       }
    2836             : 
    2837         114 :       if (newprec > prec)
    2838             :       {
    2839           0 :         if (DEBUGLEVEL>1) err_printf("new precision: %ld\n", newprec);
    2840           0 :         nf = nfnewprec_shallow(nf, newprec);
    2841             :       }
    2842         114 :       pol = AllStark(data, nf, 0, newprec);
    2843         114 :     } pari_ENDCATCH;
    2844         114 :     if (pol) {
    2845         114 :       pol = makescind(nf, pol);
    2846         114 :       return gerepileupto(av, polredbest(pol, 0));
    2847             :     }
    2848           0 :   }
    2849             : }
    2850             : 
    2851             : /*******************************************************************/
    2852             : /*                                                                 */
    2853             : /*       Hilbert and Ray Class field using CM (Schertz)            */
    2854             : /*                                                                 */
    2855             : /*******************************************************************/
    2856             : /* form^2 = 1 ? */
    2857             : static int
    2858         696 : hasexp2(GEN form)
    2859             : {
    2860         696 :   GEN a = gel(form,1), b = gel(form,2), c = gel(form,3);
    2861         696 :   return !signe(b) || absequalii(a,b) || equalii(a,c);
    2862             : }
    2863             : static int
    2864        1134 : uhasexp2(GEN form)
    2865             : {
    2866        1134 :   long a = form[1], b = form[2], c = form[3];
    2867        1134 :   return !b || a == labs(b) || a == c;
    2868             : }
    2869             : 
    2870             : GEN
    2871         390 : qfbforms(GEN D)
    2872             : {
    2873         390 :   ulong d = itou(D), dover3 = d/3, t, b2, a, b, c, h;
    2874         390 :   GEN L = cgetg((long)(sqrt((double)d) * log2(d)), t_VEC);
    2875         390 :   b2 = b = (d&1); h = 0;
    2876         390 :   if (!b) /* b = 0 treated separately to avoid special cases */
    2877             :   {
    2878         216 :     t = d >> 2; /* (b^2 - D) / 4*/
    2879        2532 :     for (a=1; a*a<=t; a++)
    2880        2316 :       if (c = t/a, t == c*a) gel(L,++h) = mkvecsmall3(a,0,c);
    2881         216 :     b = 2; b2 = 4;
    2882             :   }
    2883             :   /* now b > 0, b = D (mod 2) */
    2884        6924 :   for ( ; b2 <= dover3; b += 2, b2 = b*b)
    2885             :   {
    2886        6534 :     t = (b2 + d) >> 2; /* (b^2 - D) / 4*/
    2887             :     /* b = a */
    2888        6534 :     if (c = t/b, t == c*b) gel(L,++h) = mkvecsmall3(b,b,c);
    2889             :     /* b < a < c */
    2890     1638882 :     for (a = b+1; a*a < t; a++)
    2891     1632348 :       if (c = t/a, t == c*a)
    2892             :       {
    2893         906 :         gel(L,++h) = mkvecsmall3(a, b,c);
    2894         906 :         gel(L,++h) = mkvecsmall3(a,-b,c);
    2895             :       }
    2896             :     /* a = c */
    2897        6534 :     if (a * a == t) gel(L,++h) = mkvecsmall3(a,b,a);
    2898             :   }
    2899         390 :   setlg(L,h+1); return L;
    2900             : }
    2901             : 
    2902             : /* gcd(n, 24) */
    2903             : static long
    2904         696 : GCD24(long n)
    2905             : {
    2906         696 :   switch(n % 24)
    2907             :   {
    2908          30 :     case 0: return 24;
    2909          30 :     case 1: return 1;
    2910          24 :     case 2: return 2;
    2911           0 :     case 3: return 3;
    2912         102 :     case 4: return 4;
    2913           0 :     case 5: return 1;
    2914          90 :     case 6: return 6;
    2915           0 :     case 7: return 1;
    2916           0 :     case 8: return 8;
    2917           0 :     case 9: return 3;
    2918          78 :     case 10: return 2;
    2919           0 :     case 11: return 1;
    2920         102 :     case 12: return 12;
    2921           0 :     case 13: return 1;
    2922           0 :     case 14: return 2;
    2923           0 :     case 15: return 3;
    2924          78 :     case 16: return 8;
    2925           0 :     case 17: return 1;
    2926          78 :     case 18: return 6;
    2927           0 :     case 19: return 1;
    2928           0 :     case 20: return 4;
    2929           0 :     case 21: return 3;
    2930          84 :     case 22: return 2;
    2931           0 :     case 23: return 1;
    2932           0 :     default: return 0;
    2933             :   }
    2934             : }
    2935             : 
    2936             : struct gpq_data {
    2937             :   long p, q;
    2938             :   GEN sqd; /* sqrt(D), t_REAL */
    2939             :   GEN u, D;
    2940             :   GEN pq, pq2; /* p*q, 2*p*q */
    2941             :   GEN qfpq ; /* class of \P * \Q */
    2942             : };
    2943             : 
    2944             : /* find P and Q two non principal prime ideals (above p <= q) such that
    2945             :  *   cl(P) = cl(Q) if P,Q have order 2 in Cl(K).
    2946             :  *   Ensure that e = 24 / gcd(24, (p-1)(q-1)) = 1 */
    2947             : /* D t_INT, discriminant */
    2948             : static void
    2949          42 : init_pq(GEN D, struct gpq_data *T)
    2950             : {
    2951          42 :   const long Np = 6547; /* N.B. primepi(50000) = 5133 */
    2952          42 :   const ulong maxq = 50000;
    2953          42 :   GEN listp = cgetg(Np + 1, t_VECSMALL); /* primes p */
    2954          42 :   GEN listP = cgetg(Np + 1, t_VEC); /* primeform(p) if of order 2, else NULL */
    2955          42 :   GEN gcd24 = cgetg(Np + 1, t_VECSMALL); /* gcd(p-1, 24) */
    2956             :   forprime_t S;
    2957          42 :   long l = 1;
    2958          42 :   double best = 0.;
    2959             :   ulong q;
    2960             : 
    2961          42 :   u_forprime_init(&S, 2, ULONG_MAX);
    2962          42 :   T->D = D;
    2963          42 :   T->p = T->q = 0;
    2964             :   for(;;)
    2965             :   {
    2966             :     GEN Q;
    2967             :     long i, gcdq, mod;
    2968             :     int order2, store;
    2969             :     double t;
    2970             : 
    2971        1560 :     q = u_forprime_next(&S);
    2972        1560 :     if (best > 0 && q >= maxq)
    2973             :     {
    2974           0 :       if (DEBUGLEVEL)
    2975           0 :         pari_warn(warner,"possibly suboptimal (p,q) for D = %Ps", D);
    2976           0 :       break;
    2977             :     }
    2978        1560 :     if (kroiu(D, q) < 0) continue; /* inert */
    2979         762 :     Q = redimag(primeform_u(D, q));
    2980         762 :     if (is_pm1(gel(Q,1))) continue; /* Q | q is principal */
    2981             : 
    2982         696 :     store = 1;
    2983         696 :     order2 = hasexp2(Q);
    2984         696 :     gcd24[l] = gcdq = GCD24(q-1);
    2985         696 :     mod = 24 / gcdq; /* mod must divide p-1 otherwise e > 1 */
    2986         696 :     listp[l] = q;
    2987         696 :     gel(listP,l) = order2 ? Q : NULL;
    2988         696 :     t = (q+1)/(double)(q-1);
    2989        1824 :     for (i = 1; i < l; i++) /* try all (p, q), p < q in listp */
    2990             :     {
    2991        1422 :       long p = listp[i], gcdp = gcd24[i];
    2992             :       double b;
    2993             :       /* P,Q order 2 => cl(Q) = cl(P) */
    2994        1422 :       if (order2 && gel(listP,i) && !gequal(gel(listP,i), Q)) continue;
    2995        1416 :       if (gcdp % gcdq == 0) store = 0; /* already a better one in the list */
    2996        1416 :       if ((p-1) % mod) continue;
    2997             : 
    2998         294 :       b = (t*(p+1)) / (p-1); /* (p+1)(q+1) / (p-1)(q-1) */
    2999         294 :       if (b > best) {
    3000          84 :         store = 0; /* (p,q) always better than (q,r) for r >= q */
    3001          84 :         best = b; T->q = q; T->p = p;
    3002          84 :         if (DEBUGLEVEL>2) err_printf("p,q = %ld,%ld\n", p, q);
    3003             :       }
    3004             :       /* won't improve with this q as largest member */
    3005         294 :       if (best > 0) break;
    3006             :     }
    3007             :     /* if !store or (q,r) won't improve on current best pair, forget that q */
    3008         696 :     if (store && t*t > best)
    3009         102 :       if (++l >= Np) pari_err_BUG("quadhilbert (not enough primes)");
    3010         696 :     if (!best) /* (p,q) with p < q always better than (q,q) */
    3011             :     { /* try (q,q) */
    3012         120 :       if (gcdq >= 12 && umodiu(D, q)) /* e = 1 and unramified */
    3013             :       {
    3014           6 :         double b = (t*q) / (q-1); /* q(q+1) / (q-1)^2 */
    3015           6 :         if (b > best) {
    3016           6 :           best = b; T->q = T->p = q;
    3017           6 :           if (DEBUGLEVEL>2) err_printf("p,q = %ld,%ld\n", q, q);
    3018             :         }
    3019             :       }
    3020             :     }
    3021             :     /* If (p1+1)(q+1) / (p1-1)(q-1) <= best, we can no longer improve
    3022             :      * even with best p : stop */
    3023         696 :     if ((listp[1]+1)*t <= (listp[1]-1)*best) break;
    3024        1518 :   }
    3025          42 :   if (DEBUGLEVEL>1)
    3026           0 :     err_printf("(p, q) = %ld, %ld; gain = %f\n", T->p, T->q, 12*best);
    3027          42 : }
    3028             : 
    3029             : static GEN
    3030        3516 : gpq(GEN form, struct gpq_data *T)
    3031             : {
    3032        3516 :   pari_sp av = avma;
    3033        3516 :   long a = form[1], b = form[2], c = form[3];
    3034        3516 :   long p = T->p, q = T->q;
    3035             :   GEN form2, w, z;
    3036        3516 :   int fl, real = 0;
    3037             : 
    3038        3516 :   form2 = qficomp(T->qfpq, mkvec3s(a, -b, c));
    3039             :   /* form2 and form yield complex conjugate roots : only compute for the
    3040             :    * lexicographically smallest of the 2 */
    3041        3516 :   fl = cmpis(gel(form2,1), a);
    3042        3516 :   if (fl <= 0)
    3043             :   {
    3044        1848 :     if (fl < 0) return NULL;
    3045         180 :     fl = cmpis(gel(form2,2), b);
    3046         180 :     if (fl <= 0)
    3047             :     {
    3048         126 :       if (fl < 0) return NULL;
    3049             :       /* form == form2 : real root */
    3050          72 :       real = 1;
    3051             :     }
    3052             :   }
    3053             : 
    3054        1794 :   if (p == 2) { /* (a,b,c) = (1,1,0) mod 2 ? */
    3055         174 :     if (a % q == 0 && (a & b & 1) && !(c & 1))
    3056             :     { /* apply S : make sure that (a,b,c) represents odd values */
    3057           0 :       lswap(a,c); b = -b;
    3058             :     }
    3059             :   }
    3060        1794 :   if (a % p == 0 || a % q == 0)
    3061             :   { /* apply T^k, look for c' = a k^2 + b k + c coprime to N */
    3062         936 :     while (c % p == 0 || c % q == 0)
    3063             :     {
    3064          84 :       c += a + b;
    3065          84 :       b += a << 1;
    3066             :     }
    3067         426 :     lswap(a, c); b = -b; /* apply S */
    3068             :   }
    3069             :   /* now (a,b,c) ~ form and (a,pq) = 1 */
    3070             : 
    3071             :   /* gcd(2a, u) = 2,  w = u mod 2pq, -b mod 2a */
    3072        1794 :   w = Z_chinese(T->u, stoi(-b), T->pq2, utoipos(a << 1));
    3073        1794 :   z = double_eta_quotient(utoipos(a), w, T->D, T->p, T->q, T->pq, T->sqd);
    3074        1794 :   if (real && typ(z) == t_COMPLEX) z = gcopy(gel(z, 1));
    3075        1794 :   return gerepileupto(av, z);
    3076             : }
    3077             : 
    3078             : /* returns an equation for the Hilbert class field of Q(sqrt(D)), D < 0
    3079             :  * fundamental discriminant */
    3080             : static GEN
    3081         396 : quadhilbertimag(GEN D)
    3082             : {
    3083             :   GEN L, P, Pi, Pr, qfp, u;
    3084         396 :   pari_sp av = avma;
    3085             :   long h, i, prec;
    3086             :   struct gpq_data T;
    3087             :   pari_timer ti;
    3088             : 
    3089         396 :   if (DEBUGLEVEL>1) timer_start(&ti);
    3090         396 :   if (lgefint(D) == 3)
    3091         396 :     switch (D[2]) { /* = |D|; special cases where e > 1 */
    3092             :       case 3:
    3093             :       case 4:
    3094             :       case 7:
    3095             :       case 8:
    3096             :       case 11:
    3097             :       case 19:
    3098             :       case 43:
    3099             :       case 67:
    3100           6 :       case 163: return pol_x(0);
    3101             :     }
    3102         390 :   L = qfbforms(D);
    3103         390 :   h = lg(L)-1;
    3104         390 :   if ((1L << vals(h)) == h) /* power of 2 */
    3105             :   { /* check whether > |Cl|/2 elements have order <= 2 ==> 2-elementary */
    3106         354 :     long lim = (h>>1) + 1;
    3107        1482 :     for (i=1; i <= lim; i++)
    3108        1134 :       if (!uhasexp2(gel(L,i))) break;
    3109         354 :     if (i > lim) return GenusFieldQuadImag(D);
    3110             :   }
    3111          42 :   if (DEBUGLEVEL>1) timer_printf(&ti,"class number = %ld",h);
    3112          42 :   init_pq(D, &T);
    3113          42 :   qfp = primeform_u(D, T.p);
    3114          42 :   T.pq =  muluu(T.p, T.q);
    3115          42 :   T.pq2 = shifti(T.pq,1);
    3116          42 :   if (T.p == T.q)
    3117             :   {
    3118           0 :     GEN qfbp2 = qficompraw(qfp, qfp);
    3119           0 :     u = gel(qfbp2,2);
    3120           0 :     T.u = modii(u, T.pq2);
    3121           0 :     T.qfpq = redimag(qfbp2);
    3122             :   }
    3123             :   else
    3124             :   {
    3125          42 :     GEN qfq = primeform_u(D, T.q), bp = gel(qfp,2), bq = gel(qfq,2);
    3126          42 :     T.u = Z_chinese(bp, bq, utoipos(T.p << 1), utoipos(T.q << 1));
    3127             :     /* T.u = bp (mod 2p), T.u = bq (mod 2q) */
    3128          42 :     T.qfpq = qficomp(qfp, qfq);
    3129             :   }
    3130             :   /* u modulo 2pq */
    3131          42 :   prec = LOWDEFAULTPREC;
    3132          42 :   Pr = cgetg(h+1,t_VEC);
    3133          42 :   Pi = cgetg(h+1,t_VEC);
    3134             :   for(;;)
    3135             :   {
    3136          54 :     long ex, exmax = 0, r1 = 0, r2 = 0;
    3137          54 :     pari_sp av0 = avma;
    3138          54 :     T.sqd = sqrtr_abs(itor(D, prec));
    3139        3570 :     for (i=1; i<=h; i++)
    3140             :     {
    3141        3516 :       GEN s = gpq(gel(L,i), &T);
    3142        3516 :       if (DEBUGLEVEL>3) err_printf("%ld ", i);
    3143        3516 :       if (!s) continue;
    3144        1794 :       if (typ(s) != t_COMPLEX) gel(Pr, ++r1) = s; /* real root */
    3145        1722 :       else                     gel(Pi, ++r2) = s;
    3146        1794 :       ex = gexpo(s); if (ex > 0) exmax += ex;
    3147             :     }
    3148          54 :     if (DEBUGLEVEL>1) timer_printf(&ti,"roots");
    3149          54 :     setlg(Pr, r1+1);
    3150          54 :     setlg(Pi, r2+1);
    3151          54 :     P = roots_to_pol_r1(shallowconcat(Pr,Pi), 0, r1);
    3152          54 :     P = grndtoi(P,&exmax);
    3153          54 :     if (DEBUGLEVEL>1) timer_printf(&ti,"product, error bits = %ld",exmax);
    3154          54 :     if (exmax <= -10) break;
    3155          12 :     avma = av0; prec += nbits2extraprec(prec2nbits(DEFAULTPREC)+exmax);
    3156          12 :     if (DEBUGLEVEL) pari_warn(warnprec,"quadhilbertimag",prec);
    3157          12 :   }
    3158          42 :   return gerepileupto(av,P);
    3159             : }
    3160             : 
    3161             : GEN
    3162         492 : quadhilbert(GEN D, long prec)
    3163             : {
    3164         492 :   GEN d = D;
    3165         492 :   quadray_init(&d, NULL, NULL, 0);
    3166         960 :   return (signe(d)>0)? quadhilbertreal(D,prec)
    3167         480 :                      : quadhilbertimag(d);
    3168             : }
    3169             : 
    3170             : /* return a vector of all roots of 1 in bnf [not necessarily quadratic] */
    3171             : static GEN
    3172          60 : getallrootsof1(GEN bnf)
    3173             : {
    3174          60 :   GEN T, u, nf = bnf_get_nf(bnf), tu;
    3175          60 :   long i, n = bnf_get_tuN(bnf);
    3176             : 
    3177          60 :   if (n == 2) {
    3178          48 :     long N = nf_get_degree(nf);
    3179          48 :     return mkvec2(scalarcol_shallow(gen_m1, N),
    3180             :                   scalarcol_shallow(gen_1, N));
    3181             :   }
    3182          12 :   tu = poltobasis(nf, bnf_get_tuU(bnf));
    3183          12 :   T = zk_multable(nf, tu);
    3184          12 :   u = cgetg(n+1, t_VEC); gel(u,1) = tu;
    3185          12 :   for (i=2; i <= n; i++) gel(u,i) = ZM_ZC_mul(T, gel(u,i-1));
    3186          12 :   return u;
    3187             : }
    3188             : /* assume bnr has the right conductor */
    3189             : static GEN
    3190          60 : get_lambda(GEN bnr)
    3191             : {
    3192          60 :   GEN bnf = bnr_get_bnf(bnr), nf = bnf_get_nf(bnf), pol = nf_get_pol(nf);
    3193          60 :   GEN f = gel(bnr_get_mod(bnr), 1), labas, lamodf, u;
    3194          60 :   long a, b, f2, i, lu, v = varn(pol);
    3195             : 
    3196          60 :   f2 = 2 * itos(gcoeff(f,1,1));
    3197          60 :   u = getallrootsof1(bnf); lu = lg(u);
    3198         204 :   for (i=1; i<lu; i++)
    3199         144 :     gel(u,i) = ZC_hnfrem(gel(u,i), f); /* roots of 1, mod f */
    3200          60 :   if (DEBUGLEVEL>1)
    3201           0 :     err_printf("quadray: looking for [a,b] != unit mod 2f\n[a,b] = ");
    3202         144 :   for (a=0; a<f2; a++)
    3203        2208 :     for (b=0; b<f2; b++)
    3204             :     {
    3205        2124 :       GEN la = deg1pol_shallow(stoi(a), stoi(b), v); /* ax + b */
    3206        2124 :       if (umodiu(gnorm(mkpolmod(la, pol)), f2) != 1) continue;
    3207         192 :       if (DEBUGLEVEL>1) err_printf("[%ld,%ld] ",a,b);
    3208             : 
    3209         192 :       labas = poltobasis(nf, la);
    3210         192 :       lamodf = ZC_hnfrem(labas, f);
    3211         402 :       for (i=1; i<lu; i++)
    3212         342 :         if (ZV_equal(lamodf, gel(u,i))) break;
    3213         192 :       if (i < lu) continue; /* la = unit mod f */
    3214          60 :       if (DEBUGLEVEL)
    3215             :       {
    3216           0 :         if (DEBUGLEVEL>1) err_printf("\n");
    3217           0 :         err_printf("lambda = %Ps\n",la);
    3218             :       }
    3219          60 :       return labas;
    3220             :     }
    3221           0 :   pari_err_BUG("get_lambda");
    3222           0 :   return NULL;
    3223             : }
    3224             : 
    3225             : static GEN
    3226        7524 : to_approx(GEN nf, GEN a)
    3227             : {
    3228        7524 :   GEN M = nf_get_M(nf);
    3229        7524 :   return gadd(gel(a,1), gmul(gcoeff(M,1,2),gel(a,2)));
    3230             : }
    3231             : /* Z-basis for a (over C) */
    3232             : static GEN
    3233        3732 : get_om(GEN nf, GEN a) {
    3234        3732 :   return mkvec2(to_approx(nf,gel(a,2)),
    3235        3732 :                 to_approx(nf,gel(a,1)));
    3236             : }
    3237             : 
    3238             : /* Compute all elts in class group G = [|G|,c,g], c=cyclic factors, g=gens.
    3239             :  * Set list[j + 1] = g1^e1...gk^ek where j is the integer
    3240             :  *   ek + ck [ e(k-1) + c(k-1) [... + c2 [e1]]...] */
    3241             : static GEN
    3242          60 : getallelts(GEN bnr)
    3243             : {
    3244             :   GEN nf, C, c, g, list, pows, gk;
    3245             :   long lc, i, j, no;
    3246             : 
    3247          60 :   nf = bnr_get_nf(bnr);
    3248          60 :   no = itos( bnr_get_no(bnr) );
    3249          60 :   c = bnr_get_cyc(bnr);
    3250          60 :   g = bnr_get_gen_nocheck(bnr); lc = lg(c)-1;
    3251          60 :   list = cgetg(no+1,t_VEC);
    3252          60 :   gel(list,1) = matid(nf_get_degree(nf)); /* (1) */
    3253          60 :   if (!no) return list;
    3254             : 
    3255          60 :   pows = cgetg(lc+1,t_VEC);
    3256          60 :   c = leafcopy(c); settyp(c, t_VECSMALL);
    3257         120 :   for (i=1; i<=lc; i++)
    3258             :   {
    3259          60 :     long k = itos(gel(c,i));
    3260          60 :     c[i] = k;
    3261          60 :     gk = cgetg(k, t_VEC); gel(gk,1) = gel(g,i);
    3262        3672 :     for (j=2; j<k; j++)
    3263        3612 :       gel(gk,j) = idealmoddivisor(bnr, idealmul(nf, gel(gk,j-1), gel(gk,1)));
    3264          60 :     gel(pows,i) = gk; /* powers of g[i] */
    3265             :   }
    3266             : 
    3267          60 :   C = cgetg(lc+1, t_VECSMALL); C[1] = c[lc];
    3268          60 :   for (i=2; i<=lc; i++) C[i] = C[i-1] * c[lc-i+1];
    3269             :   /* C[i] = c(k-i+1) * ... * ck */
    3270             :   /* j < C[i+1] <==> j only involves g(k-i)...gk */
    3271          60 :   i = 1;
    3272        3732 :   for (j=1; j < C[1]; j++)
    3273        3672 :     gel(list, j+1) = gmael(pows,lc,j);
    3274         120 :   while(j<no)
    3275             :   {
    3276             :     long k;
    3277             :     GEN a;
    3278           0 :     if (j == C[i+1]) i++;
    3279           0 :     a = gmael(pows,lc-i,j/C[i]);
    3280           0 :     k = j%C[i] + 1;
    3281           0 :     if (k > 1) a = idealmoddivisor(bnr, idealmul(nf, a, gel(list,k)));
    3282           0 :     gel(list, ++j) = a;
    3283             :   }
    3284          60 :   return list;
    3285             : }
    3286             : 
    3287             : /* x quadratic integer (approximate), recognize it. If error return NULL */
    3288             : static GEN
    3289        3792 : findbezk(GEN nf, GEN x)
    3290             : {
    3291        3792 :   GEN a,b, M = nf_get_M(nf), u = gcoeff(M,1,2);
    3292             :   long ea, eb;
    3293             : 
    3294             :   /* u t_COMPLEX generator of nf.zk, write x ~ a + b u, a,b in Z */
    3295        3792 :   b = grndtoi(mpdiv(imag_i(x), gel(u,2)), &eb);
    3296        3792 :   if (eb > -20) return NULL;
    3297        3792 :   a = grndtoi(mpsub(real_i(x), mpmul(b,gel(u,1))), &ea);
    3298        3792 :   if (ea > -20) return NULL;
    3299        3792 :   return signe(b)? coltoalg(nf, mkcol2(a,b)): a;
    3300             : }
    3301             : 
    3302             : static GEN
    3303          60 : findbezk_pol(GEN nf, GEN x)
    3304             : {
    3305          60 :   long i, lx = lg(x);
    3306          60 :   GEN y = cgetg(lx,t_POL);
    3307        3852 :   for (i=2; i<lx; i++)
    3308        3792 :     if (! (gel(y,i) = findbezk(nf,gel(x,i))) ) return NULL;
    3309          60 :   y[1] = x[1]; return y;
    3310             : }
    3311             : 
    3312             : /* P approximation computed at initial precision prec. Compute needed prec
    3313             :  * to know P with 1 word worth of trailing decimals */
    3314             : static long
    3315           0 : get_prec(GEN P, long prec)
    3316             : {
    3317           0 :   long k = gprecision(P);
    3318           0 :   if (k == 3) return precdbl(prec); /* approximation not trustworthy */
    3319           0 :   k = prec - k; /* lost precision when computing P */
    3320           0 :   if (k < 0) k = 0;
    3321           0 :   k += nbits2prec(gexpo(P) + 128);
    3322           0 :   if (k <= prec) k = precdbl(prec); /* dubious: old prec should have worked */
    3323           0 :   return k;
    3324             : }
    3325             : 
    3326             : /* Compute data for ellphist */
    3327             : static GEN
    3328        3732 : ellphistinit(GEN om, long prec)
    3329             : {
    3330        3732 :   GEN res,om1b,om2b, om1 = gel(om,1), om2 = gel(om,2);
    3331             : 
    3332        3732 :   if (gsigne(imag_i(gdiv(om1,om2))) < 0) { swap(om1,om2); om = mkvec2(om1,om2); }
    3333        3732 :   om1b = gconj(om1);
    3334        3732 :   om2b = gconj(om2); res = cgetg(4,t_VEC);
    3335        3732 :   gel(res,1) = gdivgs(elleisnum(om,2,0,prec),12);
    3336        3732 :   gel(res,2) = gdiv(PiI2(prec), gmul(om2, imag_i(gmul(om1b,om2))));
    3337        3732 :   gel(res,3) = om2b; return res;
    3338             : }
    3339             : 
    3340             : /* Computes log(phi^*(z,om)), using res computed by ellphistinit */
    3341             : static GEN
    3342        7464 : ellphist(GEN om, GEN res, GEN z, long prec)
    3343             : {
    3344        7464 :   GEN u = imag_i(gmul(z, gel(res,3)));
    3345        7464 :   GEN zst = gsub(gmul(u, gel(res,2)), gmul(z,gel(res,1)));
    3346        7464 :   return gsub(ellsigma(om,z,1,prec),gmul2n(gmul(z,zst),-1));
    3347             : }
    3348             : 
    3349             : /* Computes phi^*(la,om)/phi^*(1,om) where (1,om) is an oriented basis of the
    3350             :    ideal gf*gc^{-1} */
    3351             : static GEN
    3352        3732 : computeth2(GEN om, GEN la, long prec)
    3353             : {
    3354        3732 :   GEN p1,p2,res = ellphistinit(om,prec);
    3355             : 
    3356        3732 :   p1 = gsub(ellphist(om,res,la,prec), ellphist(om,res,gen_1,prec));
    3357        3732 :   p2 = imag_i(p1);
    3358        3732 :   if (gexpo(real_i(p1))>20 || gexpo(p2)> prec2nbits(minss(prec,realprec(p2)))-10)
    3359           0 :     return NULL;
    3360        3732 :   return gexp(p1,prec);
    3361             : }
    3362             : 
    3363             : /* Computes P_2(X)=polynomial in Z_K[X] closest to prod_gc(X-th2(gc)) where
    3364             :    the product is over the ray class group bnr.*/
    3365             : static GEN
    3366          60 : computeP2(GEN bnr, long prec)
    3367             : {
    3368          60 :   long clrayno, i, first = 1;
    3369          60 :   pari_sp av=avma, av2;
    3370          60 :   GEN listray, P0, P, lanum, la = get_lambda(bnr);
    3371          60 :   GEN nf = bnr_get_nf(bnr), f = gel(bnr_get_mod(bnr), 1);
    3372          60 :   listray = getallelts(bnr);
    3373          60 :   clrayno = lg(listray)-1; av2 = avma;
    3374             : PRECPB:
    3375          60 :   if (!first)
    3376             :   {
    3377           0 :     if (DEBUGLEVEL) pari_warn(warnprec,"computeP2",prec);
    3378           0 :     nf = gerepilecopy(av2, nfnewprec_shallow(checknf(bnr),prec));
    3379             :   }
    3380          60 :   first = 0; lanum = to_approx(nf,la);
    3381          60 :   P = cgetg(clrayno+1,t_VEC);
    3382        3792 :   for (i=1; i<=clrayno; i++)
    3383             :   {
    3384        3732 :     GEN om = get_om(nf, idealdiv(nf,f,gel(listray,i)));
    3385        3732 :     GEN s = computeth2(om,lanum,prec);
    3386        3732 :     if (!s) { prec = precdbl(prec); goto PRECPB; }
    3387        3732 :     gel(P,i) = s;
    3388             :   }
    3389          60 :   P0 = roots_to_pol(P, 0);
    3390          60 :   P = findbezk_pol(nf, P0);
    3391          60 :   if (!P) { prec = get_prec(P0, prec); goto PRECPB; }
    3392          60 :   return gerepilecopy(av, P);
    3393             : }
    3394             : 
    3395             : #define nexta(a) (a>0 ? -a : 1-a)
    3396             : static GEN
    3397          42 : do_compo(GEN A0, GEN B)
    3398             : {
    3399          42 :   long a, i, l = lg(B), v = fetch_var_higher();
    3400             :   GEN A, z;
    3401             :   /* now v > x = pol_x(0) > nf variable */
    3402          42 :   B = leafcopy(B); setvarn(B, v);
    3403          42 :   for (i = 2; i < l; i++) gel(B,i) = monomial(gel(B,i), l-i-1, 0);
    3404             :   /* B := x^deg(B) B(v/x) */
    3405          42 :   A = A0 = leafcopy(A0); setvarn(A0, v);
    3406          48 :   for  (a = 0;; a = nexta(a))
    3407             :   {
    3408          48 :     if (a) A = RgX_translate(A0, stoi(a));
    3409          48 :     z = resultant(A,B); /* in variable 0 */
    3410          48 :     if (issquarefree(z)) break;
    3411           6 :   }
    3412          42 :   (void)delete_var(); return z;
    3413             : }
    3414             : #undef nexta
    3415             : 
    3416             : static GEN
    3417          12 : galoisapplypol(GEN nf, GEN s, GEN x)
    3418             : {
    3419          12 :   long i, lx = lg(x);
    3420          12 :   GEN y = cgetg(lx,t_POL);
    3421             : 
    3422          12 :   for (i=2; i<lx; i++) gel(y,i) = galoisapply(nf,s,gel(x,i));
    3423          12 :   y[1] = x[1]; return y;
    3424             : }
    3425             : /* x quadratic, write it as ua + v, u,v rational */
    3426             : static GEN
    3427          60 : findquad(GEN a, GEN x, GEN p)
    3428             : {
    3429             :   long tu, tv;
    3430          60 :   pari_sp av = avma;
    3431             :   GEN u,v;
    3432          60 :   if (typ(x) == t_POLMOD) x = gel(x,2);
    3433          60 :   if (typ(a) == t_POLMOD) a = gel(a,2);
    3434          60 :   u = poldivrem(x, a, &v);
    3435          60 :   u = simplify_shallow(u); tu = typ(u);
    3436          60 :   v = simplify_shallow(v); tv = typ(v);
    3437          60 :   if (!is_scalar_t(tu)) pari_err_TYPE("findquad", u);
    3438          60 :   if (!is_scalar_t(tv)) pari_err_TYPE("findquad", v);
    3439          60 :   x = deg1pol(u, v, varn(a));
    3440          60 :   if (typ(x) == t_POL) x = gmodulo(x,p);
    3441          60 :   return gerepileupto(av, x);
    3442             : }
    3443             : static GEN
    3444          12 : findquad_pol(GEN p, GEN a, GEN x)
    3445             : {
    3446          12 :   long i, lx = lg(x);
    3447          12 :   GEN y = cgetg(lx,t_POL);
    3448          12 :   for (i=2; i<lx; i++) gel(y,i) = findquad(a, gel(x,i), p);
    3449          12 :   y[1] = x[1]; return y;
    3450             : }
    3451             : static GEN
    3452          42 : compocyclo(GEN nf, long m, long d)
    3453             : {
    3454          42 :   GEN sb,a,b,s,p1,p2,p3,res,polL,polLK,nfL, D = nf_get_disc(nf);
    3455             :   long ell,vx;
    3456             : 
    3457          42 :   p1 = quadhilbertimag(D);
    3458          42 :   p2 = polcyclo(m,0);
    3459          42 :   if (d==1) return do_compo(p1,p2);
    3460             : 
    3461          12 :   ell = m&1 ? m : (m>>2);
    3462          12 :   if (absequalui(ell,D)) /* ell = |D| */
    3463             :   {
    3464           0 :     p2 = gcoeff(nffactor(nf,p2),1,1);
    3465           0 :     return do_compo(p1,p2);
    3466             :   }
    3467          12 :   if (ell%4 == 3) ell = -ell;
    3468             :   /* nf = K = Q(a), L = K(b) quadratic extension = Q(t) */
    3469          12 :   polLK = quadpoly(stoi(ell)); /* relative polynomial */
    3470          12 :   res = rnfequation2(nf, polLK);
    3471          12 :   vx = nf_get_varn(nf);
    3472          12 :   polL = gsubst(gel(res,1),0,pol_x(vx)); /* = charpoly(t) */
    3473          12 :   a = gsubst(lift_shallow(gel(res,2)), 0,pol_x(vx));
    3474          12 :   b = gsub(pol_x(vx), gmul(gel(res,3), a));
    3475          12 :   nfL = nfinit(polL, DEFAULTPREC);
    3476          12 :   p1 = gcoeff(nffactor(nfL,p1),1,1);
    3477          12 :   p2 = gcoeff(nffactor(nfL,p2),1,1);
    3478          12 :   p3 = do_compo(p1,p2); /* relative equation over L */
    3479             :   /* compute non trivial s in Gal(L / K) */
    3480          12 :   sb= gneg(gadd(b, RgX_coeff(polLK,1))); /* s(b) = Tr(b) - b */
    3481          12 :   s = gadd(pol_x(vx), gsub(sb, b)); /* s(t) = t + s(b) - b */
    3482          12 :   p3 = gmul(p3, galoisapplypol(nfL, s, p3));
    3483          12 :   return findquad_pol(nf_get_pol(nf), a, p3);
    3484             : }
    3485             : 
    3486             : /* I integral ideal in HNF. (x) = I, x small in Z ? */
    3487             : static long
    3488         102 : isZ(GEN I)
    3489             : {
    3490         102 :   GEN x = gcoeff(I,1,1);
    3491         102 :   if (signe(gcoeff(I,1,2)) || !equalii(x, gcoeff(I,2,2))) return 0;
    3492          90 :   return is_bigint(x)? -1: itos(x);
    3493             : }
    3494             : 
    3495             : /* Treat special cases directly. return NULL if not special case */
    3496             : static GEN
    3497         102 : treatspecialsigma(GEN bnr)
    3498             : {
    3499         102 :   GEN bnf = bnr_get_bnf(bnr), nf = bnf_get_nf(bnf);
    3500         102 :   GEN f = gel(bnr_get_mod(bnr), 1),  D = nf_get_disc(nf);
    3501             :   GEN p1, p2;
    3502         102 :   long Ds, fl, tryf, i = isZ(f);
    3503             : 
    3504         102 :   if (i == 1) return quadhilbertimag(D); /* f = 1 */
    3505             : 
    3506         102 :   if (absequaliu(D,3)) /* Q(j) */
    3507             :   {
    3508           0 :     if (i == 4 || i == 5 || i == 7) return polcyclo(i,0);
    3509           0 :     if (!absequaliu(gcoeff(f,1,1),9) || !absequaliu(Q_content(f),3)) return NULL;
    3510             :     /* f = P_3^3 */
    3511           0 :     p1 = mkpolmod(bnf_get_tuU(bnf), nf_get_pol(nf));
    3512           0 :     return gadd(pol_xn(3,0), p1); /* x^3+j */
    3513             :   }
    3514         102 :   if (absequaliu(D,4)) /* Q(i) */
    3515             :   {
    3516          12 :     if (i == 3 || i == 5) return polcyclo(i,0);
    3517          12 :     if (i != 4) return NULL;
    3518           0 :     p1 = mkpolmod(bnf_get_tuU(bnf), nf_get_pol(nf));
    3519           0 :     return gadd(pol_xn(2,0), p1); /* x^2+i */
    3520             :   }
    3521          90 :   Ds = smodis(D,48);
    3522          90 :   if (i)
    3523             :   {
    3524          78 :     if (i==2 && Ds%16== 8) return compocyclo(nf, 4,1);
    3525          72 :     if (i==3 && Ds% 3== 1) return compocyclo(nf, 3,1);
    3526          60 :     if (i==4 && Ds% 8== 1) return compocyclo(nf, 4,1);
    3527          54 :     if (i==6 && Ds   ==40) return compocyclo(nf,12,1);
    3528          48 :     return NULL;
    3529             :   }
    3530             : 
    3531          12 :   p1 = gcoeff(f,1,1); /* integer > 0 */
    3532          12 :   tryf = itou_or_0(p1); if (!tryf) return NULL;
    3533          12 :   p2 = gcoeff(f,2,2); /* integer > 0 */
    3534          12 :   if (is_pm1(p2)) fl = 0;
    3535             :   else {
    3536           0 :     if (Ds % 16 != 8 || !absequaliu(Q_content(f),2)) return NULL;
    3537           0 :     fl = 1; tryf >>= 1;
    3538             :   }
    3539          12 :   if (tryf <= 3 || umodiu(D, tryf) || !uisprime(tryf)) return NULL;
    3540          12 :   if (fl) tryf <<= 2;
    3541          12 :   return compocyclo(nf,tryf,2);
    3542             : }
    3543             : 
    3544             : GEN
    3545         138 : quadray(GEN D, GEN f, long prec)
    3546             : {
    3547             :   GEN bnr, y, bnf;
    3548         138 :   pari_sp av = avma;
    3549             : 
    3550         138 :   if (isint1(f)) return quadhilbert(D, prec);
    3551         108 :   quadray_init(&D, f, &bnf, prec);
    3552         108 :   bnr = Buchray(bnf, f, nf_INIT|nf_GEN);
    3553         108 :   if (is_pm1(bnr_get_no(bnr))) { avma = av; return pol_x(0); }
    3554         108 :   if (signe(D) > 0)
    3555           6 :     y = bnrstark(bnr,NULL,prec);
    3556             :   else
    3557             :   {
    3558         102 :     bnr = gel(bnrconductor_i(bnr,NULL,2), 2);
    3559         102 :     y = treatspecialsigma(bnr);
    3560         102 :     if (!y) y = computeP2(bnr, prec);
    3561             :   }
    3562         108 :   return gerepileupto(av, y);
    3563             : }

Generated by: LCOV version 1.11