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

Generated by: LCOV version 1.11