Code coverage tests

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

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

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

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

Generated by: LCOV version 1.13