Code coverage tests

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

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

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

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

Generated by: LCOV version 1.13