Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - modules - stark.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 20704-afbaa81) Lines: 1812 1947 93.1 %
Date: 2017-05-26 05:56:59 Functions: 126 127 99.2 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11