Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - lfun.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 21947-4fc3047) Lines: 1322 1402 94.3 %
Date: 2018-02-24 06:16:21 Functions: 133 135 98.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2015  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             : /**                       L-functions                              **/
      17             : /**                                                                **/
      18             : /********************************************************************/
      19             : 
      20             : #include "pari.h"
      21             : #include "paripriv.h"
      22             : 
      23             : /*******************************************************************/
      24             : /*  Accessors                                                      */
      25             : /*******************************************************************/
      26             : 
      27             : static GEN
      28        8809 : mysercoeff(GEN x, long n)
      29             : {
      30        8809 :   long N = n - valp(x);
      31        8809 :   return (N < 0)? gen_0: gel(x, N+2);
      32             : }
      33             : 
      34             : long
      35        5243 : ldata_get_type(GEN ldata) { return mael3(ldata, 1, 1, 1); }
      36             : 
      37             : GEN
      38       12047 : ldata_get_an(GEN ldata) { return gel(ldata, 1); }
      39             : 
      40             : GEN
      41       22531 : ldata_get_dual(GEN ldata) { return gel(ldata, 2); }
      42             : 
      43             : long
      44        1741 : ldata_isreal(GEN ldata) { return isintzero(gel(ldata, 2)); }
      45             : 
      46             : GEN
      47      145414 : ldata_get_gammavec(GEN ldata) { return gel(ldata, 3); }
      48             : 
      49             : long
      50       11030 : ldata_get_degree(GEN ldata) { return lg(gel(ldata, 3))-1; }
      51             : 
      52             : long
      53       80093 : ldata_get_k(GEN ldata)
      54             : {
      55       80093 :   GEN w = gel(ldata,4);
      56       80093 :   if (typ(w) == t_VEC) w = gel(w,1);
      57       80093 :   return itos(w);
      58             : }
      59             : /* a_n = O(n^{k1 + epsilon}) */
      60             : static double
      61       50539 : ldata_get_k1(GEN ldata)
      62             : {
      63       50539 :   GEN w = gel(ldata,4);
      64             :   long k;
      65       50539 :   if (typ(w) == t_VEC) return gtodouble(gel(w,2));
      66             :   /* by default, assume that k1 = k-1 and even (k-1)/2 for entire functions */
      67       50224 :   k = itos(w);
      68       50224 :   return ldata_get_residue(ldata)? k-1: (k-1)/2.;
      69             : }
      70             : 
      71             : GEN
      72      107445 : ldata_get_conductor(GEN ldata) { return gel(ldata, 5); }
      73             : 
      74             : GEN
      75       40764 : ldata_get_rootno(GEN ldata) { return gel(ldata, 6); }
      76             : 
      77             : GEN
      78       90904 : ldata_get_residue(GEN ldata) { return lg(ldata) == 7 ? NULL: gel(ldata, 7); }
      79             : 
      80             : long
      81       68614 : linit_get_type(GEN linit) { return mael(linit, 1, 1); }
      82             : 
      83             : GEN
      84       99718 : linit_get_ldata(GEN linit) { return gel(linit, 2); }
      85             : 
      86             : GEN
      87      128891 : linit_get_tech(GEN linit) { return gel(linit, 3); }
      88             : 
      89             : long
      90      102641 : is_linit(GEN data)
      91             : {
      92      272631 :   return lg(data) == 4 && typ(data) == t_VEC
      93      169987 :                        && typ(gel(data, 1)) == t_VECSMALL;
      94             : }
      95             : 
      96             : GEN
      97       17853 : lfun_get_step(GEN tech) { return gmael(tech, 2, 1);}
      98             : 
      99             : GEN
     100       17853 : lfun_get_pol(GEN tech) { return gmael(tech, 2, 2);}
     101             : 
     102             : GEN
     103        4205 : lfun_get_Residue(GEN tech) { return gmael(tech, 2, 3);}
     104             : 
     105             : GEN
     106       28239 : lfun_get_k2(GEN tech) { return gmael(tech, 3, 1);}
     107             : 
     108             : GEN
     109       10806 : lfun_get_w2(GEN tech) { return gmael(tech, 3, 2);}
     110             : 
     111             : GEN
     112       10806 : lfun_get_expot(GEN tech) { return gmael(tech, 3, 3);}
     113             : 
     114             : GEN
     115        3794 : lfun_get_factgammavec(GEN tech) { return gmael(tech, 3, 4); }
     116             : 
     117             : static GEN
     118        4907 : theta_dual(GEN theta, GEN bn)
     119             : {
     120        4907 :   if (typ(bn)==t_INT) return NULL;
     121             :   else
     122             :   {
     123          42 :     GEN thetad = shallowcopy(theta);
     124          42 :     GEN tech = shallowcopy(linit_get_tech(theta));
     125          42 :     GEN an = theta_get_an(tech);
     126          42 :     long prec = nbits2prec(theta_get_bitprec(tech));
     127          42 :     gel(tech,1) = ldata_vecan(bn, lg(an)-1, prec);
     128          42 :     gel(thetad,3) = tech;
     129          42 :     return thetad;
     130             :   }
     131             : }
     132             : 
     133             : static GEN
     134       31102 : domain_get_dom(GEN domain)  { return gel(domain,1); }
     135             : static long
     136       13606 : domain_get_der(GEN domain)  { return mael2(domain, 2, 1); }
     137             : static long
     138       18254 : domain_get_bitprec(GEN domain)  { return mael2(domain, 2, 2); }
     139             : GEN
     140       31501 : lfun_get_domain(GEN tech) { return gel(tech,1); }
     141             : long
     142          42 : lfun_get_bitprec(GEN tech){ return domain_get_bitprec(lfun_get_domain(tech)); }
     143             : GEN
     144           0 : lfun_get_dom(GEN tech) { return domain_get_dom(lfun_get_domain(tech)); }
     145             : 
     146             : GEN
     147        1937 : lfunprod_get_fact(GEN tech)  { return gel(tech, 2); }
     148             : 
     149             : GEN
     150       30619 : theta_get_an(GEN tdata)        { return gel(tdata, 1);}
     151             : GEN
     152        4847 : theta_get_K(GEN tdata)         { return gel(tdata, 2);}
     153             : GEN
     154        5855 : theta_get_R(GEN tdata)         { return gel(tdata, 3);}
     155             : long
     156       40322 : theta_get_bitprec(GEN tdata)   { return itos(gel(tdata, 4));}
     157             : long
     158       31613 : theta_get_m(GEN tdata)         { return itos(gel(tdata, 5));}
     159             : GEN
     160       31613 : theta_get_tdom(GEN tdata)      { return gel(tdata, 6);}
     161             : GEN
     162       34812 : theta_get_sqrtN(GEN tdata)     { return gel(tdata, 7);}
     163             : 
     164             : /*******************************************************************/
     165             : /*  Helper functions related to Gamma products                     */
     166             : /*******************************************************************/
     167             : 
     168             : /* return -itos(s) >= 0 if s is (approximately) equal to a non-positive
     169             :  * integer, and -1 otherwise */
     170             : static long
     171       10150 : isnegint(GEN s)
     172             : {
     173       10150 :   GEN r = ground(real_i(s));
     174       10150 :   if (signe(r) <= 0 && gequal(s, r)) return -itos(r);
     175       10108 :   return -1;
     176             : }
     177             : 
     178             : /* pi^(-s/2) Gamma(s/2) */
     179             : static GEN
     180        6286 : gamma_R(GEN s, long prec)
     181             : {
     182        6286 :   GEN s2 = gdivgs(s, 2), pi = mppi(prec);
     183        6286 :   long ms = isnegint(s2);
     184        6286 :   if (ms >= 0)
     185             :   {
     186          42 :     GEN pr = gmul(powru(pi, ms), gdivsg(odd(ms)? -2: 2, mpfact(ms)));
     187          42 :     GEN S = scalarser(pr, 0, 1);
     188          42 :     setvalp(S,-1); return S;
     189             :   }
     190        6244 :   return gdiv(ggamma(s2,prec), gpow(pi,s2,prec));
     191             : }
     192             : 
     193             : /* gamma_R(s)gamma_R(s+1) = 2 (2pi)^(-s) Gamma(s) */
     194             : static GEN
     195        3864 : gamma_C(GEN s, long prec)
     196             : {
     197        3864 :   GEN pi2 = Pi2n(1,prec);
     198        3864 :   long ms = isnegint(s);
     199        3864 :   if (ms >= 0)
     200             :   {
     201           0 :     GEN pr = gmul(powrs(pi2, ms), gdivsg(odd(ms)? -2: 2, mpfact(ms)));
     202           0 :     GEN S = scalarser(pr, 0, 1);
     203           0 :     setvalp(S,-1); return S;
     204             :   }
     205        3864 :   return gmul2n(gdiv(ggamma(s,prec), gpow(pi2,s,prec)), 1);
     206             : }
     207             : 
     208             : static GEN
     209         399 : gammafrac(GEN r, long d)
     210             : {
     211         399 :   GEN pr, a = gmul2n(r, -1);
     212         399 :   GEN polj = cgetg(labs(d)+1, t_COL);
     213         399 :   long i, v=0;
     214         399 :   if (d > 0)
     215           0 :     for (i = 1; i <= d; ++i)
     216           0 :       gel(polj, i) = deg1pol_shallow(ghalf, gaddgs(a, i-1), v);
     217             :   else
     218         798 :     for (i = 1; i <= -d; ++i)
     219         399 :       gel(polj, i) = deg1pol_shallow(ghalf, gsubgs(a, i), v);
     220         399 :   pr = RgV_prod(polj);
     221         399 :   return d < 0 ? ginv(pr): pr;
     222             : }
     223             : 
     224             : static GEN
     225       10682 : gammafactor(GEN Vga)
     226             : {
     227       10682 :   pari_sp av = avma;
     228       10682 :   long i, m, d = lg(Vga)-1, dr, dc;
     229       10682 :   GEN pol = pol_1(0), pi = gen_0, R = cgetg(d+1,t_VEC);
     230             :   GEN P, F, FR, FC, E, ER, EC;
     231       28511 :   for (i = 1; i <= d; ++i)
     232             :   {
     233       17829 :     GEN a = gel(Vga,i), qr = gdiventres(real_i(a), gen_2);
     234       17829 :     long q = itos(gel(qr,1));
     235       17829 :     gel(R, i) = gadd(gel(qr,2), imag_i(a));
     236       17829 :     if (q)
     237             :     {
     238         399 :       pol = gmul(pol, gammafrac(gel(R,i), q));
     239         399 :       pi  = addis(pi, q);
     240             :     }
     241             :   }
     242       10682 :   gen_sort_inplace(R, (void*)cmp_universal, cmp_nodata, &P);
     243       10682 :   F = cgetg(d+1, t_VEC); E = cgetg(d+1, t_VECSMALL);
     244       36652 :   for (i = 1, m = 0; i <= d;)
     245             :   {
     246             :     long k;
     247       15288 :     GEN u = gel(R, i);
     248       17829 :     for(k = i + 1; k <= d; ++k)
     249        7147 :       if (cmp_universal(gel(R, k), u)) break;
     250       15288 :     m++;
     251       15288 :     E[m] = k - i;
     252       15288 :     gel(F, m) = u;
     253       15288 :     i = k;
     254             :   }
     255       10682 :   setlg(F, m+1); setlg(E, m+1);
     256       10682 :   R = cgetg(m+1, t_VEC);
     257       25970 :   for (i = 1; i <= m; i++)
     258             :   {
     259       15288 :     GEN qr = gdiventres(gel(F,i), gen_1);
     260       15288 :     gel(R, i) = mkvec2(gel(qr,2), stoi(E[i]));
     261             :   }
     262       10682 :   gen_sort_inplace(R, (void*)cmp_universal, cmp_nodata, &P);
     263       10682 :   FR = cgetg(m+1, t_VEC); ER = cgetg(m+1, t_VECSMALL);
     264       10682 :   FC = cgetg(m+1, t_VEC); EC = cgetg(m+1, t_VECSMALL);
     265       32599 :   for (i = 1, dr = 1, dc = 1; i <= m;)
     266             :   {
     267       11235 :     if (i==m || cmp_universal(gel(R,i), gel(R,i+1)))
     268             :     {
     269        7182 :       gel(FR, dr) = gel(F, P[i]);
     270        7182 :       ER[dr] = E[P[i]];
     271        7182 :       dr++; i++;
     272             :     } else
     273             :     {
     274        4053 :       if (gequal(gaddgs(gmael(R,i,1), 1), gmael(R,i+1,1)))
     275           0 :         gel(FC, dc) = gel(F, P[i+1]);
     276             :       else
     277        4053 :         gel(FC, dc) = gel(F, P[i]);
     278        4053 :       EC[dc] = E[P[i]];
     279        4053 :       dc++; i+=2;
     280             :     }
     281             :   }
     282       10682 :   setlg(FR, dr); setlg(ER, dr);
     283       10682 :   setlg(FC, dc); setlg(EC, dc);
     284       10682 :   return gerepilecopy(av, mkvec4(pol, pi, mkvec2(FR,ER), mkvec2(FC,EC)));
     285             : }
     286             : 
     287             : static GEN
     288        5243 : deg1ser_shallow(GEN a1, GEN a0, long v, long e)
     289             : {
     290        5243 :   return RgX_to_ser(deg1pol_shallow(a1, a0, v), e+2);
     291             : }
     292             : /*
     293             : To test:
     294             : GR(s)=Pi^-(s/2)*gamma(s/2);
     295             : GC(s)=2*(2*Pi)^-s*gamma(s)
     296             : gam_direct(F,s)=prod(i=1,#F,GR(s+F[i]))
     297             : gam_fact(F,s)=my([P,p,R,C]=gammafactor(F));subst(P,x,s)*Pi^-p*prod(i=1,#R[1],GR(s+R[1][i])^R[2][i])*prod(i=1,#C[1],GC(s+C[1][i])^C[2][i])
     298             : */
     299             : 
     300             : static GEN
     301        9982 : polgammaeval(GEN F, GEN s)
     302             : {
     303        9982 :   GEN r = poleval(F, s);
     304        9982 :   if (typ(s)!=t_SER && gequal0(r))
     305             :   {
     306           0 :     long e = gvaluation(F, deg1pol(gen_1, gneg(s), varn(F)));
     307           0 :     r = poleval(F, deg1ser_shallow(gen_1, s, 0, e+1));
     308             :   }
     309        9982 :   return r;
     310             : }
     311             : 
     312             : static GEN
     313        9555 : fracgammaeval(GEN F, GEN s)
     314             : {
     315        9555 :   if (typ(F)==t_POL)
     316        9128 :     return polgammaeval(F, s);
     317         427 :   else if (typ(F)==t_RFRAC)
     318         427 :     return gdiv(polgammaeval(gel(F,1), s), polgammaeval(gel(F,2), s));
     319           0 :   return F;
     320             : }
     321             : 
     322             : static GEN
     323        9555 : gammafactproduct(GEN F, GEN s, long prec)
     324             : {
     325        9555 :   pari_sp av = avma;
     326        9555 :   GEN P = fracgammaeval(gel(F,1), s);
     327        9555 :   GEN p = gpow(mppi(prec),gneg(gel(F,2)), prec), z = gmul(P, p);
     328        9555 :   GEN R = gel(F,3), Rw = gel(R,1), Re=gel(R,2);
     329        9555 :   GEN C = gel(F,4), Cw = gel(C,1), Ce=gel(C,2);
     330        9555 :   long i, lR = lg(Rw), lC = lg(Cw);
     331       15841 :   for (i=1; i< lR; i++)
     332        6286 :     z = gmul(z, gpowgs(gamma_R(gadd(s,gel(Rw, i)), prec), Re[i]));
     333       13419 :   for (i=1; i< lC; i++)
     334        3864 :     z = gmul(z, gpowgs(gamma_C(gadd(s,gel(Cw, i)), prec), Ce[i]));
     335        9555 :   return gerepileupto(av, z);
     336             : }
     337             : 
     338             : static int
     339        3850 : gammaordinary(GEN Vga, GEN s)
     340             : {
     341        3850 :   long i, d = lg(Vga)-1;
     342        9919 :   for (i = 1; i <= d; i++)
     343             :   {
     344        6153 :     GEN z = gadd(s, gel(Vga,i));
     345             :     long e;
     346        6153 :     if (gsigne(z) <= 0) { (void)grndtoi(z, &e); if (e < -10) return 0; }
     347             :   }
     348        3766 :   return 1;
     349             : }
     350             : 
     351             : /* Exponent A of t in asymptotic expansion; K(t) ~ C t^A exp(-pi d t^(2/d)).
     352             :  * suma = vecsum(Vga)*/
     353             : static double
     354       50532 : gammavec_expo(long d, double suma) { return (1 - d + suma) / d; }
     355             : 
     356             : /*******************************************************************/
     357             : /*       First part: computations only involving Theta(t)          */
     358             : /*******************************************************************/
     359             : 
     360             : static void
     361       77282 : get_cone(GEN t, double *r, double *a)
     362             : {
     363       77282 :   const long prec = LOWDEFAULTPREC;
     364       77282 :   if (typ(t) == t_COMPLEX)
     365             :   {
     366       14686 :     t  = gprec_w(t, prec);
     367       14686 :     *r = gtodouble(gabs(t, prec));
     368       14686 :     *a = fabs(gtodouble(garg(t, prec)));
     369             :   }
     370             :   else
     371             :   {
     372       62596 :     *r = fabs(gtodouble(t));
     373       62596 :     *a = 0.;
     374             :   }
     375       77282 :   if (!*r && !*a) pari_err_DOMAIN("lfunthetainit","t","=",gen_0,t);
     376       77275 : }
     377             : /* slightly larger cone than necessary, to avoid round-off problems */
     378             : static void
     379       45669 : get_cone_fuzz(GEN t, double *r, double *a)
     380       45669 : { get_cone(t, r, a); *r -= 1e-10; if (*a) *a += 1e-10; }
     381             : 
     382             : /* Initialization m-th Theta derivative. tdom is either
     383             :  * - [rho,alpha]: assume |t| >= rho and |arg(t)| <= alpha
     384             :  * - a positive real scalar: assume t real, t >= tdom;
     385             :  * - a complex number t: compute at t;
     386             :  * N is the conductor (either the true one from ldata or a guess from
     387             :  * lfunconductor) */
     388             : long
     389       35939 : lfunthetacost(GEN ldata, GEN tdom, long m, long bitprec)
     390             : {
     391       35939 :   pari_sp av = avma;
     392       35939 :   GEN Vga = ldata_get_gammavec(ldata);
     393       35939 :   long d = lg(Vga)-1;
     394       35939 :   long k1 = ldata_get_k1(ldata);
     395       35939 :   double c = d/2., a, A, B, logC, al, rho, T;
     396       35939 :   double N = gtodouble(ldata_get_conductor(ldata));
     397             : 
     398       35939 :   if (!N) pari_err_TYPE("lfunthetaneed [missing conductor]", ldata);
     399       35939 :   if (typ(tdom) == t_VEC && lg(tdom) == 3)
     400             :   {
     401           7 :     rho= gtodouble(gel(tdom,1));
     402           7 :     al = gtodouble(gel(tdom,2));
     403             :   }
     404             :   else
     405       35932 :     get_cone_fuzz(tdom, &rho, &al);
     406       35932 :   A = gammavec_expo(d, gtodouble(vecsum(Vga))); avma = av;
     407       35932 :   a = (A+k1+1) + (m-1)/c;
     408       35932 :   if (fabs(a) < 1e-10) a = 0.;
     409       35932 :   logC = c*LOG2 - log(c)/2;
     410             :   /* +1: fudge factor */
     411       35932 :   B = LOG2*bitprec+logC+m*log(2*M_PI) + 1 + (k1+1)*log(N)/2 - (k1+m+1)*log(rho);
     412       35932 :   if (al)
     413             :   { /* t = rho e^(i*al), T^(1/c) = Re(t^(1/c)) > 0, T = rho cos^c(al/c) */
     414        7343 :     double z = cos(al/c);
     415        7343 :     T = (d == 2 && typ(tdom) != t_VEC)? gtodouble(real_i(tdom)): rho*pow(z,c);
     416        7343 :     if (z <= 0)
     417           0 :       pari_err_DOMAIN("lfunthetaneed", "arg t", ">", dbltor(c*M_PI/2), tdom);
     418        7343 :     B -= log(z) * (c * (k1+A+1) + m);
     419             :   }
     420             :   else
     421       28589 :     T = rho;
     422       35932 :   return B <= 0? 0: floor(0.9 + dblcoro526(a,c,B) / T * sqrt(N));
     423             : }
     424             : long
     425          14 : lfunthetacost0(GEN L, GEN tdom, long m, long bitprec)
     426             : {
     427             :   long n;
     428          14 :   if (is_linit(L) && linit_get_type(L)==t_LDESC_THETA)
     429           7 :   {
     430           7 :     GEN tech = linit_get_tech(L);
     431           7 :     n = lg(theta_get_an(tech))-1;
     432             :   }
     433             :   else
     434             :   {
     435           7 :     pari_sp av = avma;
     436           7 :     GEN ldata = lfunmisc_to_ldata_shallow(L);
     437           7 :     n = lfunthetacost(ldata, tdom? tdom: gen_1, m, bitprec);
     438           7 :     avma = av;
     439             :   }
     440          14 :   return n;
     441             : }
     442             : 
     443             : static long
     444        2359 : fracgammadegree(GEN FVga)
     445        2359 : { GEN F = gel(FVga,1); return (typ(F)==t_RFRAC)? degpol(gel(F,2)): 0; }
     446             : 
     447             : /* Poles of a L-function can be represented in the following ways:
     448             :  * 1) Nothing (ldata has only 6 components, ldata_get_residue = NULL).
     449             :  * 2) a complex number (single pole at s = k with given residue, unknown if 0).
     450             :  * 3) A vector (possibly empty) of 2-component vectors [a, ra], where a is the
     451             :  * pole, ra a t_SER: its Taylor expansion at a. A t_VEC encodes the polar
     452             :  * part of L, a t_COL, the polar part of Lambda */
     453             : 
     454             : /* 'a' a complex number (pole), 'r' the polar part of L at 'a';
     455             :  * return 'R' the polar part of Lambda at 'a' */
     456             : static GEN
     457        1988 : rtoR(GEN a, GEN r, GEN FVga, GEN N, long prec)
     458             : {
     459        1988 :   long v = lg(r)-2;
     460        1988 :   GEN as = deg1ser_shallow(gen_1, a, varn(r), v);
     461        1988 :   GEN Na = gpow(N, gdivgs(as, 2), prec);
     462        1988 :   long d = fracgammadegree(FVga);
     463        1988 :   if (d) as = sertoser(as, v+d); /* make up for a possible loss of accuracy */
     464        1988 :   return gmul(gmul(r, Na), gammafactproduct(FVga, as, prec));
     465             : }
     466             : 
     467             : /* assume r in normalized form: t_VEC of pairs [be,re] */
     468             : GEN
     469        2051 : lfunrtopoles(GEN r)
     470             : {
     471        2051 :   long j, l = lg(r);
     472        2051 :   GEN v = cgetg(l, t_VEC);
     473        4144 :   for (j = 1; j < l; j++)
     474             :   {
     475        2093 :     GEN rj = gel(r,j), a = gel(rj,1);
     476        2093 :     gel(v,j) = a;
     477             :   }
     478        2051 :   gen_sort_inplace(v, (void*)&cmp_universal, cmp_nodata, NULL);
     479        2051 :   return v;
     480             : }
     481             : 
     482             : /* r / x + O(1) */
     483             : static GEN
     484        1687 : simple_pole(GEN r)
     485             : {
     486        1687 :   GEN S = deg1ser_shallow(gen_0, r, 0, 1);
     487        1687 :   setvalp(S, -1); return S;
     488             : }
     489             : static GEN
     490        1694 : normalize_simple_pole(GEN r, long k)
     491             : {
     492        1694 :   long tx = typ(r);
     493        1694 :   if (is_vec_t(tx)) return r;
     494        1687 :   if (!is_scalar_t(tx)) pari_err_TYPE("normalizepoles", r);
     495        1687 :   return mkvec(mkvec2(stoi(k), simple_pole(r)));
     496             : }
     497             : /* normalize the description of a polar part */
     498             : static GEN
     499        2170 : normalizepoles(GEN r, long k)
     500             : {
     501             :   long iv, j, l;
     502             :   GEN v;
     503        2170 :   if (!is_vec_t(typ(r))) return normalize_simple_pole(r, k);
     504         497 :   v = cgetg_copy(r, &l);
     505        1260 :   for (j = iv = 1; j < l; j++)
     506             :   {
     507         763 :     GEN rj = gel(r,j), a = gel(rj,1), ra = gel(rj,2);
     508         763 :     if (!is_scalar_t(typ(a)) || typ(ra) != t_SER)
     509           0 :       pari_err_TYPE("normalizepoles",r);
     510         763 :     if (valp(ra) >= 0) continue;
     511         763 :     gel(v,iv++) = rj;
     512             :   }
     513         497 :   setlg(v, iv); return v;
     514             : }
     515             : 
     516             : /* Compute R's from r's (r = Taylor devts of L(s), R of Lambda(s)).
     517             :  * 'r/eno' passed to override the one from ldata  */
     518             : static GEN
     519       10829 : lfunrtoR_i(GEN ldata, GEN r, GEN eno, long prec)
     520             : {
     521       10829 :   GEN Vga = ldata_get_gammavec(ldata), N = ldata_get_conductor(ldata);
     522             :   GEN R, vr, FVga;
     523       10829 :   pari_sp av = avma;
     524       10829 :   long lr, j, jR, k = ldata_get_k(ldata);
     525             : 
     526       10829 :   if (!r || isintzero(r) || isintzero(eno)) return gen_0;
     527        2170 :   r = normalizepoles(r, k);
     528        2170 :   if (typ(r) == t_COL) return gerepilecopy(av, r);
     529        1946 :   if (typ(ldata_get_dual(ldata)) != t_INT)
     530           0 :     pari_err(e_MISC,"please give the Taylor development of Lambda");
     531        1946 :   vr = lfunrtopoles(r); lr = lg(vr);
     532        1946 :   FVga = gammafactor(Vga);
     533        1946 :   R = cgetg(2*lr, t_VEC);
     534        3934 :   for (j = jR = 1; j < lr; j++)
     535             :   {
     536        1988 :     GEN rj = gel(r,j), a = gel(rj,1), ra = gel(rj,2);
     537        1988 :     GEN Ra = rtoR(a, ra, FVga, N, prec);
     538        1988 :     GEN b = gsubsg(k, gconj(a));
     539        1988 :     if (lg(Ra)-2 < -valp(Ra))
     540           0 :       pari_err(e_MISC,
     541             :         "please give more terms in L function's Taylor development at %Ps", a);
     542        1988 :     gel(R,jR++) = mkvec2(a, Ra);
     543        1988 :     if (!tablesearch(vr, b, (int (*)(GEN,GEN))&cmp_universal))
     544             :     {
     545        1967 :       GEN mX = gneg(pol_x(varn(Ra)));
     546        1967 :       GEN Rb = gmul(eno, gsubst(gconj(Ra), varn(Ra), mX));
     547        1967 :       gel(R,jR++) = mkvec2(b, Rb);
     548             :     }
     549             :   }
     550        1946 :   setlg(R, jR); return gerepilecopy(av, R);
     551             : }
     552             : static GEN
     553       10829 : lfunrtoR_eno(GEN ldata, GEN eno, long prec)
     554       10829 : { return lfunrtoR_i(ldata, ldata_get_residue(ldata), eno, prec); }
     555             : static GEN
     556       10801 : lfunrtoR(GEN ldata, long prec)
     557       10801 : { return lfunrtoR_eno(ldata, ldata_get_rootno(ldata), prec); }
     558             : 
     559             : /* thetainit using {an: n <= L} */
     560             : static GEN
     561        9744 : lfunthetainit0(GEN ldata, GEN tdom, GEN vecan, long m,
     562             :     long bitprec, long extrabit)
     563             : {
     564        9744 :   long prec = nbits2prec(bitprec);
     565        9744 :   GEN tech, N = ldata_get_conductor(ldata);
     566        9744 :   GEN Vga = ldata_get_gammavec(ldata);
     567        9744 :   GEN K = gammamellininvinit(Vga, m, bitprec + extrabit);
     568        9744 :   GEN R = lfunrtoR(ldata, prec);
     569        9744 :   if (!tdom) tdom = gen_1;
     570        9744 :   if (typ(tdom) != t_VEC)
     571             :   {
     572             :     double r, a;
     573        9737 :     get_cone_fuzz(tdom, &r, &a);
     574        9737 :     tdom = mkvec2(dbltor(r), a? dbltor(a): gen_0);
     575             :   }
     576        9744 :   tech = mkvecn(7, vecan,K,R, stoi(bitprec), stoi(m), tdom, gsqrt(N,prec));
     577        9744 :   return mkvec3(mkvecsmall(t_LDESC_THETA), ldata, tech);
     578             : }
     579             : 
     580             : /* tdom: 1) positive real number r, t real, t >= r; or
     581             :  *       2) [r,a], describing the cone |t| >= r, |arg(t)| <= a */
     582             : static GEN
     583        5495 : lfunthetainit_i(GEN data, GEN tdom, long m, long bitprec)
     584             : {
     585        5495 :   GEN ldata = lfunmisc_to_ldata_shallow(data);
     586        5495 :   long L = lfunthetacost(ldata, tdom, m, bitprec);
     587        5488 :   GEN vecan = ldata_vecan(ldata_get_an(ldata), L, nbits2prec(bitprec));
     588        5488 :   return lfunthetainit0(ldata, tdom, vecan, m, bitprec, 32);
     589             : }
     590             : 
     591             : GEN
     592         245 : lfunthetainit(GEN ldata, GEN tdom, long m, long bitprec)
     593             : {
     594         245 :   pari_sp av = avma;
     595         245 :   GEN S = lfunthetainit_i(ldata, tdom? tdom: gen_1, m, bitprec);
     596         245 :   return gerepilecopy(av, S);
     597             : }
     598             : 
     599             : GEN
     600         805 : lfunan(GEN ldata, long L, long prec)
     601             : {
     602         805 :   pari_sp av = avma;
     603             :   GEN an ;
     604         805 :   ldata = lfunmisc_to_ldata_shallow(ldata);
     605         805 :   an = gerepilecopy(av, ldata_vecan(ldata_get_an(ldata), L, prec));
     606         798 :   if (typ(an) != t_VEC) an = vecsmall_to_vec_inplace(an);
     607         798 :   return an;
     608             : }
     609             : 
     610             : /* [1^B,...,N^B] */
     611             : GEN
     612         112 : vecpowuu(long N, ulong B)
     613             : {
     614         112 :   GEN v = const_vec(N, NULL);
     615             :   long p, i;
     616             :   forprime_t T;
     617         112 :   u_forprime_init(&T, 3, N);
     618        2023 :   while ((p = u_forprime_next(&T)))
     619             :   {
     620             :     long m, pk, oldpk;
     621        1799 :     gel(v,p) = powuu(p, B);
     622        3997 :     for (pk = p, oldpk = p; pk <= N; oldpk = pk, pk *= p)
     623             :     {
     624        2198 :       if (pk != p) gel(v,pk) = mulii(gel(v,oldpk), gel(v,p));
     625        9107 :       for (m = N/pk; m > 1; m--)
     626        6909 :         if (gel(v,m) && m%p) gel(v, m*pk) = mulii(gel(v,m), gel(v,pk));
     627             :     }
     628             :   }
     629         112 :   gel(v,1) = gen_1;
     630        3479 :   for (i = 2; i <= N; i+=2)
     631             :   {
     632        3367 :     long vi = vals(i);
     633        3367 :     gel(v,i) = shifti(gel(v,i >> vi), B * vi);
     634             :   }
     635         112 :   return v;
     636             : }
     637             : /* [1^B,...,N^B] */
     638             : GEN
     639       11275 : vecpowug(long N, GEN B, long prec)
     640             : {
     641       11275 :   GEN v = const_vec(N, NULL);
     642       11275 :   long p, eB = gexpo(B);
     643       11275 :   long prec0 = eB < 5? prec: prec + nbits2extraprec(eB);
     644             :   forprime_t T;
     645       11275 :   u_forprime_init(&T, 2, N);
     646       11275 :   gel(v,1) = gen_1;
     647      291642 :   while ((p = u_forprime_next(&T)))
     648             :   {
     649             :     long m, pk, oldpk;
     650      269092 :     gel(v,p) = gpow(utor(p,prec0), B, prec);
     651      269092 :     if (prec0 != prec) gel(v,p) = gprec_wtrunc(gel(v,p), prec);
     652      609585 :     for (pk = p, oldpk = p; pk <= N; oldpk = pk, pk *= p)
     653             :     {
     654      340493 :       if (pk != p) gel(v,pk) = gmul(gel(v,oldpk), gel(v,p));
     655     4314778 :       for (m = N/pk; m > 1; m--)
     656     3974285 :         if (gel(v,m) && m%p) gel(v, m*pk) = gmul(gel(v,m), gel(v,pk));
     657             :     }
     658             :   }
     659       11275 :   return v;
     660             : }
     661             : 
     662             : /* return [1^(2/d), 2^(2/d),...,lim^(2/d)] */
     663             : static GEN
     664        4847 : mkvroots(long d, long lim, long prec)
     665             : {
     666        4847 :   if (d <= 4)
     667             :   {
     668        4763 :     GEN v = cgetg(lim+1,t_VEC);
     669             :     long n;
     670        4763 :     switch(d)
     671             :     {
     672             :       case 1:
     673        1855 :         for (n=1; n <= lim; n++) gel(v,n) = sqru(n);
     674        1855 :         return v;
     675             :       case 2:
     676         875 :         for (n=1; n <= lim; n++) gel(v,n) = utoipos(n);
     677         875 :         return v;
     678             :       case 4:
     679        1251 :         for (n=1; n <= lim; n++) gel(v,n) = sqrtr(utor(n, prec));
     680        1251 :         return v;
     681             :     }
     682             :   }
     683         866 :   return vecpowug(lim, gdivgs(gen_2,d), prec);
     684             : }
     685             : 
     686             : GEN
     687       35393 : lfunthetacheckinit(GEN data, GEN t, long m, long bitprec)
     688             : {
     689       35393 :   if (is_linit(data) && linit_get_type(data)==t_LDESC_THETA)
     690             :   {
     691       31613 :     GEN tdom, thetainit = linit_get_tech(data);
     692       31613 :     long bitprecnew = theta_get_bitprec(thetainit);
     693       31613 :     long m0 = theta_get_m(thetainit);
     694             :     double r, al, rt, alt;
     695       31613 :     if (m0 != m)
     696           0 :       pari_err_DOMAIN("lfuntheta","derivative order","!=", stoi(m),stoi(m0));
     697       31613 :     if (bitprec > bitprecnew) goto INIT;
     698       31613 :     get_cone(t, &rt, &alt);
     699       31613 :     tdom = theta_get_tdom(thetainit);
     700       31613 :     r = rtodbl(gel(tdom,1));
     701       31613 :     al= rtodbl(gel(tdom,2)); if (rt >= r && alt <= al) return data;
     702             :   }
     703             : INIT:
     704        5166 :   return lfunthetainit_i(data, t, m, bitprec);
     705             : }
     706             : 
     707             : long
     708       10304 : lfunisvgaell(GEN Vga, long flag)
     709             : {
     710             :   GEN al1, al2;
     711       10304 :   long d = lg(Vga)-1;
     712       10304 :   if (d != 2) return 0;
     713        6332 :   al1 = gel(Vga, 1); al2 = gel(Vga, 2);
     714        6332 :   if (flag) return gequal1(gabs(gsub(al1, al2), LOWDEFAULTPREC));
     715           0 :   else return (gequal0(al1) && gequal1(al2)) || (gequal0(al2) && gequal1(al1));
     716             : }
     717             : 
     718             : /* generic */
     719             : static GEN
     720       43400 : vecan_nv_cmul(void *E, GEN P, long a, GEN x)
     721             : {
     722       43400 :   GEN vroots = (GEN)E;
     723       43400 :   return (a==0 || !gel(P,a))? NULL: gmul(gmul(gel(vroots,a), gel(P,a)), x);
     724             : }
     725             : /* al = 1 */
     726             : static GEN
     727           0 : vecan_n_cmul(void *E, GEN P, long a, GEN x)
     728             : {
     729             :   (void)E;
     730           0 :   return (a==0 || !gel(P,a))? NULL: gmul(gmulsg(a,gel(P,a)), x);
     731             : }
     732             : /* al = 0 */
     733             : static GEN
     734    23235093 : vecan_cmul(void *E, GEN P, long a, GEN x)
     735             : {
     736             :   (void)E;
     737    23235093 :   return (a==0 || !gel(P,a))? NULL: gmul(gel(P,a), x);
     738             : }
     739             : /* d=2, 2 sum_{n <= limt} a_n (n t)^al q^n, q = exp(-2pi t) */
     740             : static GEN
     741      113075 : theta2(GEN vecan, long limt, GEN t, GEN al, long prec)
     742             : {
     743      113075 :   GEN S, q, pi2 = Pi2n(1,prec), vroots = NULL;
     744      113075 :   const struct bb_algebra *alg = get_Rg_algebra();
     745             :   GEN (*cmul)(void *, GEN, long, GEN);
     746             :   long flag;
     747      113075 :   if (gequal0(al))
     748             :   {
     749      109652 :     cmul = vecan_cmul;
     750      109652 :     flag = 0;
     751             :   }
     752        3423 :   else if (gequal1(al))
     753             :   {
     754           0 :     cmul = vecan_n_cmul;
     755           0 :     flag = 1;
     756             :   }
     757             :   else
     758             :   {
     759        3423 :     vroots = vecpowug(limt, al, prec);
     760        3423 :     cmul = vecan_nv_cmul;
     761        3423 :     flag = 2;
     762             :   }
     763      113075 :   setsigne(pi2,-1); q = gexp(gmul(pi2, t), prec);
     764      113075 :   S = gen_bkeval(vecan, limt, q, 1, (void*)vroots, alg, cmul);
     765      113075 :   switch (flag)
     766             :   {
     767           0 :     case 1: S = gmul(t, S); break;
     768        3423 :     case 2: S = gmul(gpow(t,al,prec), S);
     769             :   }
     770      113075 :   return gmul2n(S,1);
     771             : }
     772             : 
     773             : static GEN
     774    13536348 : get_an(GEN vecan, long n)
     775             : {
     776    13536348 :   if (typ(vecan) == t_VECSMALL)
     777             :   {
     778     4283458 :     long a = vecan[n];
     779     4283458 :     return a? stoi(a): NULL;
     780             :   }
     781             :   else
     782             :   {
     783     9252890 :     GEN a = gel(vecan,n);
     784     9252890 :     return (!a || gequal0(a))? NULL: a;
     785             :   }
     786             : }
     787             : 
     788             : /* d=1, 2 sum_{n <= limt} a_n (n t)^al q^(n^2), q = exp(-pi t^2) */
     789             : static GEN
     790       24445 : theta1(GEN vecan, long limt, GEN t, GEN al, long prec)
     791             : {
     792       24445 :   GEN q = gexp(gmul(negr(mppi(prec)), gsqr(t)), prec);
     793       24445 :   GEN vexp = gsqrpowers(q, limt), S = gen_0;
     794       24445 :   pari_sp av = avma;
     795             :   long n;
     796       24445 :   if (gequal0(al))
     797     2585220 :     for (n = 1; n <= limt; ++n)
     798             :     {
     799     2567736 :       GEN an = get_an(vecan, n);
     800     2567736 :       if (!an) continue;
     801     2293574 :       S = gadd(S, gmul(an, gel(vexp, n)));
     802     2293574 :       if (gc_needed(av, 3)) S = gerepileupto(av, S);
     803             :     }
     804        6961 :   else if (gequal1(al))
     805             :   {
     806     1504248 :     for (n = 1; n <= limt; ++n)
     807             :     {
     808     1497357 :       GEN an = get_an(vecan, n);
     809     1497357 :       if (!an) continue;
     810      754684 :       S = gadd(S, gmul(gmulgs(an, n), gel(vexp, n)));
     811      754684 :       if (gc_needed(av, 3)) S = gerepileupto(av, S);
     812             :     }
     813        6891 :     S = gmul(S,t);
     814             :   }
     815             :   else
     816             :   {
     817          70 :     GEN vroots = vecpowug(limt, al, prec);
     818          70 :     av = avma;
     819         483 :     for (n = 1; n <= limt; ++n)
     820             :     {
     821         413 :       GEN an = get_an(vecan, n);
     822         413 :       if (!an) continue;
     823         413 :       S = gadd(S, gmul(gmul(an, gel(vroots,n)), gel(vexp, n)));
     824         413 :       if (gc_needed(av, 3)) S = gerepileupto(av, S);
     825             :     }
     826          70 :     S = gmul(S, gpow(t,al,prec));
     827             :   }
     828       24445 :   return gmul2n(S,1);
     829             : }
     830             : 
     831             : /* If m > 0, compute m-th derivative of theta(t) = theta0(t/sqrt(N))
     832             :  * with absolute error 2^-bitprec; theta(t)=\sum_{n\ge1}a(n)K(nt/N^(1/2)) */
     833             : GEN
     834       30563 : lfuntheta(GEN data, GEN t, long m, long bitprec)
     835             : {
     836       30563 :   pari_sp ltop = avma;
     837             :   long limt, d;
     838             :   GEN sqN, vecan, Vga, ldata, theta, thetainit, S;
     839       30563 :   long n, prec = nbits2prec(bitprec);
     840       30563 :   t = gprec_w(t, prec);
     841       30563 :   theta = lfunthetacheckinit(data, t, m, bitprec);
     842       30556 :   ldata = linit_get_ldata(theta);
     843       30556 :   thetainit = linit_get_tech(theta);
     844       30556 :   vecan = theta_get_an(thetainit);
     845       30556 :   sqN = theta_get_sqrtN(thetainit);
     846       30556 :   limt = lg(vecan)-1;
     847       30556 :   if (theta == data)
     848       29170 :     limt = minss(limt, lfunthetacost(ldata, t, m, bitprec));
     849       30556 :   if (!limt)
     850             :   {
     851          14 :     avma = ltop; S = real_0_bit(-bitprec);
     852          14 :     if (!is_real_t(typ(t)) || !ldata_isreal(ldata))
     853           7 :       S = gerepilecopy(ltop, mkcomplex(S,S));
     854          14 :     return S;
     855             :   }
     856       30542 :   t = gdiv(t, sqN);
     857       30542 :   Vga = ldata_get_gammavec(ldata);
     858       30542 :   d = lg(Vga) - 1;
     859       30542 :   if (m == 0 && d == 1)
     860             :   {
     861       24445 :     S = theta1(vecan, limt, t, gel(Vga,1), prec);
     862       24445 :     return gerepileupto(ltop, S);
     863             :   }
     864        6097 :   if (m == 0 && lfunisvgaell(Vga, 1))
     865             :   {
     866        3630 :     S = theta2(vecan, limt, t, vecmin(Vga), prec);
     867        3630 :     return gerepileupto(ltop, S);
     868             :   }
     869             :   else
     870             :   {
     871        2467 :     GEN K = theta_get_K(thetainit);
     872        2467 :     GEN vroots = mkvroots(d, limt, prec);
     873        2467 :     t = gpow(t, gdivgs(gen_2,d), prec);
     874        2467 :     S = gen_0;
     875     4162598 :     for (n = 1; n <= limt; ++n)
     876             :     {
     877     4160131 :       GEN nt, an = get_an(vecan, n);
     878     4160131 :       if (!an) continue;
     879     1123144 :       nt = gmul(gel(vroots,n), t);
     880     1123144 :       if (m) an = gmul(an, powuu(n, m));
     881     1123144 :       S = gadd(S, gmul(an, gammamellininvrt(K, nt, bitprec)));
     882             :     }
     883        2467 :     if (m) S = gdiv(S, gpowgs(sqN, m));
     884        2467 :     return gerepileupto(ltop, S);
     885             :   }
     886             : }
     887             : 
     888             : /*******************************************************************/
     889             : /* Second part: Computation of L-Functions.                        */
     890             : /*******************************************************************/
     891             : 
     892             : struct lfunp {
     893             :   long precmax, Dmax, D, M, m0, nmax, d;
     894             :   double k1, E, logN2, logC, A, hd, dc, dw, dh, MAXs, sub;
     895             :   GEN L, vprec, an, bn;
     896             : };
     897             : 
     898             : static void
     899       14600 : lfunparams(GEN ldata, long der, long bitprec, struct lfunp *S)
     900             : {
     901       14600 :   const long derprec = (der > 1)? dbllog2(mpfact(der)): 0; /* log2(der!) */
     902             :   GEN Vga, N, L;
     903             :   long k, k1, d, m, M, flag, nmax;
     904             :   double a, E, hd, Ep, d2, suma, maxs, mins, sub, B0,B1, Lestimate, Mestimate;
     905             : 
     906       14600 :   Vga = ldata_get_gammavec(ldata);
     907       14600 :   S->d = d = lg(Vga)-1; d2 = d/2.;
     908       14600 :   suma = gtodouble(vecsum(Vga));
     909       14600 :   k = ldata_get_k(ldata);
     910       14600 :   N = ldata_get_conductor(ldata);
     911       14600 :   S->logN2 = log(gtodouble(N)) / 2;
     912       14600 :   maxs = S->dc + S->dw;
     913       14600 :   mins = S->dc - S->dw;
     914       14600 :   S->MAXs = maxss(maxs, k-mins);
     915             : 
     916             :   /* we compute Lambda^(der)(s) / der!; need to compensate for L^(der)(s)
     917             :    * ln |gamma(s)| ~ (pi/4) d |t|; max with 1: fudge factor */
     918       14600 :   S->D = (long)ceil(bitprec + derprec + maxdd((M_PI/(4*LOG2))*d*S->dh, 1));
     919       14600 :   S->E = E = LOG2*S->D; /* D:= required absolute bitprec */
     920             : 
     921       14600 :   Ep = E + maxdd(M_PI * S->dh * d2, (d*S->MAXs + suma - 1) * log(E));
     922       14600 :   hd = d2*M_PI*M_PI / Ep;
     923       14600 :   S->m0 = (long)ceil(LOG2/hd);
     924       14600 :   S->hd = LOG2/S->m0;
     925             : 
     926       14600 :   S->logC = d2*LOG2 - log(d2)/2;
     927       14600 :   k1 = ldata_get_k1(ldata);
     928       14600 :   S->k1 = k1; /* assume |a_n| << n^k1 with small implied constant */
     929       14600 :   S->A  = gammavec_expo(d, suma);
     930             : 
     931       14600 :   sub = 0.;
     932       14600 :   if (mins > 1)
     933             :   {
     934        3850 :     GEN sig = dbltor(mins);
     935        3850 :     sub += S->logN2*mins;
     936        3850 :     if (gammaordinary(Vga, sig))
     937             :     {
     938        3766 :       GEN FVga = gammafactor(Vga);
     939        3766 :       GEN gas = gammafactproduct(FVga, sig, LOWDEFAULTPREC);
     940        3766 :       if (typ(gas) != t_SER)
     941             :       {
     942        3766 :         double dg = dbllog2(gas);
     943        3766 :         if (dg > 0) sub += dg * LOG2;
     944             :       }
     945             :     }
     946             :   }
     947       14600 :   S->sub = sub;
     948       14600 :   M = 1000;
     949       14600 :   L = cgetg(M+2, t_VECSMALL);
     950       14600 :   a = S->k1 + S->A;
     951             : 
     952       14600 :   B0 = 5 + S->E - S->sub + S->logC + S->k1*S->logN2; /* 5 extra bits */
     953       14600 :   B1 = S->hd * (S->MAXs - S->k1);
     954       14600 :   Lestimate = dblcoro526(a + S->MAXs - 2./d, d/2.,
     955       14600 :     S->E - S->sub + S->logC - log(2*M_PI*S->hd) + S->MAXs*S->logN2);
     956       14600 :   Mestimate = ((Lestimate > 0? log(Lestimate): 0) + S->logN2) / S->hd;
     957       14600 :   nmax = 0;
     958       14600 :   flag = 0;
     959     1450883 :   for (m = 0;; m++)
     960             :   {
     961     1450883 :     double x, H = S->logN2 - m*S->hd, B = B0 + m*B1;
     962             :     long n;
     963     1450883 :     x = dblcoro526(a, d/2., B);
     964     1450883 :     n = floor(x*exp(H));
     965     1450883 :     if (n > nmax) nmax = n;
     966     1450883 :     if (m > M) { M *= 2; L = vecsmall_lengthen(L,M+2); }
     967     1450883 :     L[m+1] = n;
     968     1450883 :     if (n == 0) { if (++flag > 2 && m > Mestimate) break; } else flag = 0;
     969     1436283 :   }
     970             :   /* can happen for tiny bitprec */
     971       14600 :   if (m < 4) { nmax = 1; L[1] = 1; m = 1; }
     972             :   else
     973             :   {
     974       14600 :     m -= 2;
     975       14600 :     while (!L[m]) m--;
     976             :   }
     977       14600 :   setlg(L, m+1); S->M = m-1;
     978       14600 :   S->L = L;
     979       14600 :   S->nmax = nmax;
     980             : 
     981       14600 :   S->Dmax = S->D + (long)ceil((S->M * S->hd * S->MAXs - S->sub) / LOG2);
     982       14600 :   if (S->Dmax < S->D) S->Dmax = S->D;
     983       14600 :   S->precmax = nbits2prec(S->Dmax);
     984       14600 :   if (DEBUGLEVEL > 1)
     985           0 :     err_printf("Dmax=%ld, D=%ld, M = %ld, nmax = %ld, m0 = %ld\n",
     986             :                S->Dmax,S->D,S->M,S->nmax, S->m0);
     987       14600 : }
     988             : 
     989             : /* x0 * [1,x,..., x^n] */
     990             : static GEN
     991        1890 : powersshift(GEN x, long n, GEN x0)
     992             : {
     993        1890 :   long i, l = n+2;
     994        1890 :   GEN V = cgetg(l, t_VEC);
     995        1890 :   gel(V,1) = x0;
     996        1890 :   for(i = 2; i < l; i++) gel(V,i) = gmul(gel(V,i-1),x);
     997        1890 :   return V;
     998             : }
     999             : 
    1000             : static GEN
    1001        4396 : lfuninit_pol(GEN vecc, GEN poqk, long M, long prec)
    1002             : {
    1003             :   long m;
    1004        4396 :   GEN pol = cgetg(M+3, t_POL); pol[1] = evalsigne(1) | evalvarn(0);
    1005        4396 :   gel(pol, 2) = gprec_w(gmul2n(gel(vecc,1), -1), prec);
    1006      270088 :   for (m = 2; m <= M+1; m++)
    1007      265692 :     gel(pol, m+1) = gprec_w(gmul(gel(poqk,m), gel(vecc,m)), prec);
    1008        4396 :   return RgX_renormalize_lg(pol, M+3);
    1009             : }
    1010             : 
    1011             : static GEN
    1012        2009 : lfuninit_vecc2_sum(GEN an, GEN qk, GEN a, struct lfunp *Q, GEN poqk)
    1013             : {
    1014        2009 :   const long M = Q->M, prec = Q->precmax;
    1015        2009 :   GEN L = Q->L;
    1016        2009 :   long m, L0 = lg(an)-1;
    1017        2009 :   GEN v = cgetg(M + 2, t_VEC);
    1018      111454 :   for (m = 0; m <= M; m++)
    1019             :   {
    1020      109445 :     pari_sp av = avma;
    1021      109445 :     GEN t = gel(qk, m+1);
    1022      109445 :     GEN S = theta2(an, minss(L[m+1],L0), t, a, prec);
    1023      109445 :     gel(v, m+1) = gerepileupto(av, S); /* theta(exp(mh)) */
    1024             :   }
    1025        2009 :   return lfuninit_pol(v, poqk, M, prec);
    1026             : }
    1027             : 
    1028             : /* d=2 and Vga = [a,a+1] */
    1029             : static GEN
    1030        1890 : lfuninit_vecc2(GEN theta, GEN h, struct lfunp *S, GEN poqk)
    1031             : {
    1032        1890 :   GEN ldata = linit_get_ldata(theta);
    1033        1890 :   GEN a = vecmin(ldata_get_gammavec(ldata));
    1034        1890 :   GEN thetainit = linit_get_tech(theta);
    1035        1890 :   GEN sqN = theta_get_sqrtN(thetainit);
    1036        1890 :   GEN qk = powersshift(mpexp(h), S->M, ginv(sqN));
    1037        1890 :   if (S->bn)
    1038         119 :     return mkvec2(lfuninit_vecc2_sum(S->an, qk, a, S, poqk),
    1039             :                   lfuninit_vecc2_sum(S->bn, qk, a, S, poqk));
    1040             :   else
    1041        1771 :     return lfuninit_vecc2_sum(S->an, qk, a, S, poqk);
    1042             : }
    1043             : 
    1044             : /* theta(exp(mh)) ~ sum_{n <= L[m]} a(n) k[m,n] */
    1045             : static GEN
    1046        2387 : lfuninit_vecc_sum(GEN L, long M, GEN vecan, GEN vK, GEN pokq, long prec)
    1047             : {
    1048             :   long m;
    1049        2387 :   GEN vecc = cgetg(M+2, t_VEC);
    1050      163030 :   for (m = 0; m <= M; ++m)
    1051             :   {
    1052      160643 :     pari_sp av = avma;
    1053      160643 :     GEN s = gen_0;
    1054             :     long n;
    1055     5471354 :     for (n = 1; n <= L[m+1]; n++)
    1056             :     {
    1057     5310711 :       GEN an = get_an(vecan, n);
    1058     5310711 :       if (!an) continue;
    1059     1885079 :       s = gadd(s, gmul(an, gmael(vK,m+1,n)));
    1060     1885079 :       if (gc_needed(av, 3)) s = gerepileupto(av, s);
    1061             :     }
    1062      160643 :     gel(vecc,m+1) = gerepileupto(av, s);
    1063             :   }
    1064        2387 :   return lfuninit_pol(vecc, pokq, M, prec);
    1065             : }
    1066             : 
    1067             : /* return [\theta(exp(mh)), m=0..M], theta(t) = sum a(n) K(n/sqrt(N) t),
    1068             :  * h = log(2)/m0 */
    1069             : 
    1070             : static GEN
    1071        4256 : lfuninit_vecc(GEN theta, GEN h, struct lfunp *S, GEN poqk)
    1072             : {
    1073        4256 :   const long m0 = S->m0;
    1074             :   GEN thetainit, vK, L, K, an, bn, d2, sqN, vroots, eh2d, peh2d, vprec;
    1075             :   long d, M, prec, m, n, neval;
    1076             : 
    1077        4256 :   if (!S->vprec)
    1078        1890 :     return lfuninit_vecc2(theta, h, S, poqk);
    1079             : 
    1080        2366 :   d = S->d;
    1081        2366 :   L = S->L;
    1082        2366 :   M = S->M;
    1083        2366 :   an= S->an;
    1084        2366 :   bn= S->bn;
    1085        2366 :   vprec = S->vprec;
    1086        2366 :   prec = S->precmax;
    1087        2366 :   thetainit = linit_get_tech(theta);
    1088        2366 :   K = theta_get_K(thetainit);
    1089        2366 :   sqN = theta_get_sqrtN(thetainit);
    1090             : 
    1091             :   /* For all 0<= m <= M, and all n <= L[m+1] such that a_n!=0, we must compute
    1092             :    *   k[m,n] = K(n exp(mh)/sqrt(N))
    1093             :    * with ln(absolute error) <= E + max(mh sigma - sub, 0) + k1 * log(n).
    1094             :    * N.B. we use the 'rt' variant and pass argument (n exp(mh)/sqrt(N))^(2/d).
    1095             :    * Speedup: if n' = 2n and m' = m - m0 >= 0; then k[m,n] = k[m',n']. */
    1096             :   /* vroots[n] = n^(2/d) */
    1097        2366 :   vroots = mkvroots(d, S->nmax, prec);
    1098        2366 :   d2 = gdivgs(gen_2, d);
    1099        2366 :   eh2d = gexp(gmul(d2,h), prec); /* exp(2h/d) */
    1100             :   /* peh2d[m+1] = (exp(mh)/sqrt(N))^(2/d) */
    1101        2366 :   peh2d = gpowers0(eh2d, M, invr(gpow(sqN, d2, prec)));
    1102        2366 :   neval = 0;
    1103             :   /* vK[m+1,n] will contain k[m,n]. For each 0 <= m <= M, sum for n<=L[m+1] */
    1104        2366 :   vK = cgetg(M+2, t_VEC);
    1105      161994 :   for (m = 0; m <= M; m++)
    1106      159628 :     gel(vK,m+1) = const_vec(L[m+1], NULL);
    1107             : 
    1108      161994 :   for (m = M; m >= 0; m--)
    1109     5466244 :     for (n = 1; n <= L[m+1]; n++)
    1110             :     {
    1111     5306616 :       GEN t2d, kmn = gmael(vK,m+1,n);
    1112     5306616 :       long nn, mm, p = 0;
    1113             : 
    1114     5306616 :       if (kmn) continue; /* done already */
    1115             :       /* p = largest (absolute) accuracy to which we need k[m,n] */
    1116    11919257 :       for (mm=m,nn=n; mm>=0 && nn <= L[mm+1]; nn<<=1,mm-=m0)
    1117     7845859 :         if (gel(an, nn) || (bn && gel(bn, nn)))
    1118     1881166 :           p = maxuu(p, umael(vprec,mm+1,nn));
    1119     4073398 :       if (!p) continue; /* a_{n 2^v} = 0 for all v in range */
    1120     1231475 :       t2d = mpmul(gel(vroots, n), gel(peh2d,m+1)); /*(n exp(mh)/sqrt(N))^(2/d)*/
    1121     1231475 :       neval++;
    1122     1231475 :       kmn = gammamellininvrt(K, t2d, p);
    1123     3696168 :       for (mm=m,nn=n; mm>=0 && nn <= L[mm+1]; nn<<=1,mm-=m0)
    1124     2464693 :         gmael(vK,mm+1,nn) = kmn;
    1125             :     }
    1126        2366 :   if (DEBUGLEVEL >= 1) err_printf("true evaluations: %ld\n", neval);
    1127             : 
    1128        2366 :   if (bn)
    1129          21 :     return mkvec2(lfuninit_vecc_sum(L, M, an, vK, poqk, S->precmax),
    1130             :                   lfuninit_vecc_sum(L, M, bn, vK, poqk, S->precmax));
    1131             :   else
    1132        2345 :     return lfuninit_vecc_sum(L, M, an, vK, poqk, S->precmax);
    1133             : }
    1134             : 
    1135             : static void
    1136       76748 : parse_dom(long k, GEN dom, struct lfunp *S)
    1137             : {
    1138       76748 :   long l = lg(dom);
    1139       76748 :   if (typ(dom)!=t_VEC) pari_err_TYPE("lfuninit [domain]", dom);
    1140       76748 :   if (l == 2)
    1141             :   {
    1142       41286 :     S->dc = k/2.;
    1143       41286 :     S->dw = 0.;
    1144       41286 :     S->dh = gtodouble(gel(dom,1));
    1145             :   }
    1146       35462 :   else if (l == 3)
    1147             :   {
    1148         301 :     S->dc = k/2.;
    1149         301 :     S->dw = gtodouble(gel(dom,1));
    1150         301 :     S->dh = gtodouble(gel(dom,2));
    1151             :   }
    1152       35161 :   else if (l == 4)
    1153             :   {
    1154       35161 :     S->dc = gtodouble(gel(dom,1));
    1155       35161 :     S->dw = gtodouble(gel(dom,2));
    1156       35161 :     S->dh = gtodouble(gel(dom,3));
    1157             :   }
    1158             :   else
    1159             :   {
    1160           0 :     pari_err_TYPE("lfuninit [domain]", dom);
    1161           0 :     S->dc = S->dw = S->dh = 0; /*-Wall*/
    1162             :   }
    1163       76748 :   if (S->dw < 0 || S->dh < 0) pari_err_TYPE("lfuninit [domain]", dom);
    1164       76748 : }
    1165             : 
    1166             : /* do we have dom \subset dom0 ? dom = [center, width, height] */
    1167             : int
    1168       13221 : sdomain_isincl(long k, GEN dom, GEN dom0)
    1169             : {
    1170             :   struct lfunp S0, S;
    1171       13221 :   parse_dom(k, dom, &S);
    1172       13221 :   parse_dom(k, dom0, &S0);
    1173       26442 :   return S0.dc - S0.dw <= S.dc - S.dw
    1174       13221 :       && S0.dc + S0.dw >= S.dc + S.dw && S0.dh >= S.dh;
    1175             : }
    1176             : 
    1177             : static int
    1178       13221 : checklfuninit(GEN linit, GEN dom, long der, long bitprec)
    1179             : {
    1180       13221 :   GEN ldata = linit_get_ldata(linit);
    1181       13221 :   GEN domain = lfun_get_domain(linit_get_tech(linit));
    1182       26442 :   return domain_get_der(domain) >= der
    1183       13221 :     && domain_get_bitprec(domain) >= bitprec
    1184       26442 :     && sdomain_isincl(ldata_get_k(ldata), dom, domain_get_dom(domain));
    1185             : }
    1186             : 
    1187             : GEN
    1188        4963 : lfuninit_make(long t, GEN ldata, GEN molin, GEN domain)
    1189             : {
    1190        4963 :   GEN Vga = ldata_get_gammavec(ldata);
    1191        4963 :   long d = lg(Vga)-1;
    1192        4963 :   long k = ldata_get_k(ldata);
    1193        4963 :   GEN k2 = gdivgs(stoi(k), 2);
    1194        4963 :   GEN expot = gdivgs(gadd(gmulsg(d, gsubgs(k2, 1)), vecsum(Vga)), 4);
    1195        4963 :   GEN eno = ldata_get_rootno(ldata);
    1196        4963 :   long prec = nbits2prec( domain_get_bitprec(domain) );
    1197        4963 :   GEN w2 = ginv(gsqrt(eno, prec));
    1198        4963 :   GEN hardy = mkvec4(k2, w2, expot, gammafactor(Vga));
    1199        4963 :   return mkvec3(mkvecsmall(t),ldata, mkvec3(domain, molin, hardy));
    1200             : }
    1201             : 
    1202             : static void
    1203        4256 : lfunparams2(GEN ldata, GEN an, GEN bn, struct lfunp *S)
    1204             : {
    1205        4256 :   GEN vprec, L, Vga = ldata_get_gammavec(ldata);
    1206             :   double sig0, pmax, sub2;
    1207             :   long m, nan, nmax, neval, M;
    1208        4256 :   L = S->L;
    1209        4256 :   M = S->M;
    1210             : 
    1211             :   /* try to reduce parameters now we know the a_n (some may be 0) */
    1212        4256 :   nan = lg(an)-1;
    1213        4256 :   if (typ(an) == t_VEC) an = RgV_kill0(an);
    1214        4256 :   if (bn && typ(bn) == t_VEC) bn = RgV_kill0(bn);
    1215        4256 :   S->an = an; S->bn = bn;
    1216        8512 :   if (lfunisvgaell(Vga, 1)) { S->vprec = NULL; return; }
    1217        2366 :   nmax = neval = 0;
    1218      161994 :   for (m = 0; m <= M; m++)
    1219             :   {
    1220      159628 :     long n = minss(nan, L[m+1]);
    1221      159628 :     while (n > 0 && !gel(an, n)) n--;
    1222      159628 :     if (n > nmax) nmax = n;
    1223      159628 :     neval += n;
    1224      159628 :     L[m+1] = n; /* reduce S->L[m+1] */
    1225             :   }
    1226        2366 :   if (DEBUGLEVEL >= 1) err_printf("expected evaluations: %ld\n", neval);
    1227        2366 :   for (; M > 0; M--)
    1228        2366 :     if (L[M+1]) break;
    1229        2366 :   setlg(L, M+2);
    1230        2366 :   S->M = M;
    1231        2366 :   S->nmax = nmax;
    1232             : 
    1233        2366 :   pmax = 0;
    1234        2366 :   sig0 = S->MAXs/S->m0;
    1235        2366 :   sub2 = S->sub / LOG2;
    1236        2366 :   vprec = cgetg(S->M+2, t_VEC);
    1237             :   /* compute accuracy to which we will need k[m,n] = K(n*exp(mh/sqrt(N)))
    1238             :    * vprec[m+1,n] = absolute accuracy to which we need k[m,n] */
    1239      161994 :   for (m = 0; m <= S->M; m++)
    1240             :   {
    1241      159628 :     double c = S->D + maxdd(m*sig0 - sub2, 0);
    1242             :     GEN t;
    1243      159628 :     if (!S->k1)
    1244             :     {
    1245      152922 :       t = const_vecsmall(L[m+1]+1, c);
    1246      152922 :       pmax = maxdd(pmax,c);
    1247             :     }
    1248             :     else
    1249             :     {
    1250             :       long n;
    1251        6706 :       t = cgetg(L[m+1]+1, t_VECSMALL);
    1252      352807 :       for (n = 1; n <= L[m+1]; n++)
    1253             :       {
    1254      346101 :         t[n] = c + S->k1 * log2(n);
    1255      346101 :         pmax = maxdd(pmax, t[n]);
    1256             :       }
    1257             :     }
    1258      159628 :     gel(vprec,m+1) = t;
    1259             :   }
    1260        2366 :   S->vprec = vprec;
    1261        2366 :   S->Dmax = pmax;
    1262        2366 :   S->precmax = nbits2prec(pmax);
    1263             : }
    1264             : 
    1265             : static GEN
    1266        4256 : lfun_init_theta(GEN ldata, GEN eno, struct lfunp *S)
    1267             : {
    1268        4256 :   GEN an, bn, dual, tdom = NULL;
    1269             :   long L;
    1270        4256 :   if (eno)
    1271        3199 :     L = S->nmax;
    1272             :   else
    1273             :   {
    1274        1057 :     tdom = dbltor(sqrt(0.5));
    1275        1057 :     L = maxss(S->nmax, lfunthetacost(ldata, tdom, 0, S->D));
    1276             :   }
    1277        4256 :   an = ldata_vecan(ldata_get_an(ldata), L, S->precmax);
    1278        4256 :   dual = ldata_get_dual(ldata);
    1279        4256 :   bn = typ(dual)==t_INT? NULL: ldata_vecan(dual, L, S->precmax);
    1280        4256 :   lfunparams2(ldata, an, bn, S);
    1281        4256 :   return lfunthetainit0(ldata, tdom, an, 0, S->Dmax, 0);
    1282             : }
    1283             : 
    1284             : GEN
    1285       10344 : lfuncost(GEN L, GEN dom, long der, long bitprec)
    1286             : {
    1287       10344 :   pari_sp av = avma;
    1288       10344 :   GEN ldata = lfunmisc_to_ldata_shallow(L);
    1289       10344 :   long k = ldata_get_k(ldata);
    1290             :   struct lfunp S;
    1291             : 
    1292       10344 :   parse_dom(k, dom, &S);
    1293       10344 :   lfunparams(ldata, der, bitprec, &S);
    1294       10344 :   avma = av; return mkvecsmall2(S.nmax, S.Dmax);
    1295             : }
    1296             : GEN
    1297          42 : lfuncost0(GEN L, GEN dom, long der, long bitprec)
    1298             : {
    1299          42 :   pari_sp av = avma;
    1300             :   GEN C;
    1301             : 
    1302          42 :   if (is_linit(L))
    1303             :   {
    1304          28 :     GEN tech = linit_get_tech(L);
    1305          28 :     GEN domain = lfun_get_domain(tech);
    1306          28 :     dom = domain_get_dom(domain);
    1307          28 :     der = domain_get_der(domain);
    1308          28 :     bitprec = domain_get_bitprec(domain);
    1309          28 :     if (linit_get_type(L) == t_LDESC_PRODUCT)
    1310             :     {
    1311          21 :       GEN v = lfunprod_get_fact(linit_get_tech(L)), F = gel(v,1);
    1312          21 :       long i, l = lg(F);
    1313          21 :       C = cgetg(l, t_VEC);
    1314          77 :       for (i = 1; i < l; ++i)
    1315          56 :         gel(C, i) = zv_to_ZV( lfuncost(gel(F,i), dom, der, bitprec) );
    1316          21 :       return gerepileupto(av, C);
    1317             :     }
    1318             :   }
    1319          21 :   if (!dom) pari_err_TYPE("lfuncost [missing s domain]", L);
    1320          21 :   C = lfuncost(L,dom,der,bitprec);
    1321          21 :   return gerepileupto(av, zv_to_ZV(C));
    1322             : }
    1323             : 
    1324             : GEN
    1325       17974 : lfuninit(GEN lmisc, GEN dom, long der, long bitprec)
    1326             : {
    1327       17974 :   pari_sp ltop = avma;
    1328             :   GEN R, h, theta, ldata, qk, poqk, pol, eno, r, domain, molin;
    1329             :   long k;
    1330             :   struct lfunp S;
    1331             : 
    1332       17974 :   if (is_linit(lmisc))
    1333             :   {
    1334       13270 :     long t = linit_get_type(lmisc);
    1335       13270 :     if (t==t_LDESC_INIT || t==t_LDESC_PRODUCT)
    1336             :     {
    1337       13221 :       if (checklfuninit(lmisc, dom, der, bitprec)) return lmisc;
    1338          21 :       pari_warn(warner,"lfuninit: insufficient initialization");
    1339             :     }
    1340             :   }
    1341        4774 :   ldata = lfunmisc_to_ldata_shallow(lmisc);
    1342             : 
    1343        4774 :   if (ldata_get_type(ldata)==t_LFUN_NF)
    1344             :   {
    1345         518 :     GEN T = gel(ldata_get_an(ldata), 2);
    1346         518 :     return lfunzetakinit(T, dom, der, 0, bitprec);
    1347             :   }
    1348        4256 :   k = ldata_get_k(ldata);
    1349        4256 :   parse_dom(k, dom, &S);
    1350        4256 :   lfunparams(ldata, der, bitprec, &S);
    1351        4256 :   r = ldata_get_residue(ldata);
    1352             :   /* Note: all guesses should already have been performed (thetainit more
    1353             :    * expensive than needed: should be either tdom = 1 or bitprec = S.D).
    1354             :    * BUT if the root number / polar part do not have an algebraic
    1355             :    * expression, there is no way to do this until we know the
    1356             :    * precision, i.e. now. So we can't remove guessing code from here and
    1357             :    * lfun_init_theta */
    1358        4256 :   if (r && isintzero(r)) eno = NULL;
    1359             :   else
    1360             :   {
    1361        4256 :     eno = ldata_get_rootno(ldata);
    1362        4256 :     if (isintzero(eno)) eno = NULL;
    1363             :   }
    1364        4256 :   theta = lfun_init_theta(ldata, eno, &S);
    1365        4256 :   if (eno)
    1366        3199 :     R = theta_get_R(linit_get_tech(theta));
    1367             :   else
    1368             :   {
    1369        1057 :     GEN v = lfunrootres(theta, S.D);
    1370        1057 :     ldata = shallowcopy(ldata);
    1371        1057 :     gel(ldata, 6) = gel(v,3);
    1372        1057 :     r = gel(v,1);
    1373        1057 :     if (isintzero(r))
    1374        1043 :       setlg(ldata,7); /* no pole */
    1375             :     else
    1376          14 :       gel(ldata, 7) = r;
    1377        1057 :     R = lfunrtoR(ldata, nbits2prec(S.D));
    1378             :   }
    1379        4256 :   h = divru(mplog2(S.precmax), S.m0);
    1380        4256 :   k = ldata_get_k(ldata);
    1381        4256 :   qk = gprec_w(mpexp(gmul2n(gmulsg(k,h), -1)), S.precmax); /* exp(kh/2) */
    1382        4256 :   poqk = gpowers(qk, S.M);
    1383        4256 :   pol = lfuninit_vecc(theta, h, &S, poqk);
    1384        4256 :   molin = mkvec3(h, pol, R);
    1385        4256 :   domain = mkvec2(dom, mkvecsmall2(der, bitprec));
    1386        4256 :   return gerepilecopy(ltop, lfuninit_make(t_LDESC_INIT, ldata, molin, domain));
    1387             : }
    1388             : 
    1389             : GEN
    1390         385 : lfuninit0(GEN lmisc, GEN dom, long der, long bitprec)
    1391             : {
    1392         385 :   GEN z = lfuninit(lmisc, dom, der, bitprec);
    1393         385 :   return z == lmisc? gcopy(z): z;
    1394             : }
    1395             : 
    1396             : /* If s is a pole of Lambda, return polar part at s; else return NULL */
    1397             : static GEN
    1398        4205 : lfunpoleresidue(GEN R, GEN s)
    1399             : {
    1400             :   long j;
    1401       11782 :   for (j = 1; j < lg(R); j++)
    1402             :   {
    1403        8088 :     GEN Rj = gel(R, j), be = gel(Rj, 1);
    1404        8088 :     if (gequal(s, be)) return gel(Rj, 2);
    1405             :   }
    1406        3694 :   return NULL;
    1407             : }
    1408             : 
    1409             : /* Compute contribution of polar part at s when not a pole. */
    1410             : static GEN
    1411        6639 : veccothderivn(GEN a, long n)
    1412             : {
    1413             :   long i;
    1414        6639 :   pari_sp av = avma;
    1415        6639 :   GEN c = pol_x(0), cp = mkpoln(3, gen_m1, gen_0, gen_1);
    1416        6639 :   GEN v = cgetg(n+2, t_VEC);
    1417        6639 :   gel(v, 1) = poleval(c, a);
    1418       19980 :   for(i = 2; i <= n+1; i++)
    1419             :   {
    1420       13341 :     c = ZX_mul(ZX_deriv(c), cp);
    1421       13341 :     gel(v, i) = gdiv(poleval(c, a), mpfact(i-1));
    1422             :   }
    1423        6639 :   return gerepilecopy(av, v);
    1424             : }
    1425             : 
    1426             : static GEN
    1427        6702 : polepart(long n, GEN h, GEN C)
    1428             : {
    1429        6702 :   GEN h2n = gpowgs(gdiv(h, gen_2), n-1);
    1430        6702 :   GEN res = gmul(h2n, gel(C,n));
    1431        6702 :   return odd(n)? res : gneg(res);
    1432             : }
    1433             : 
    1434             : static GEN
    1435        3316 : lfunsumcoth(GEN R, GEN s, GEN h, long prec)
    1436             : {
    1437             :   long i,j;
    1438        3316 :   GEN S = gen_0;
    1439        9955 :   for (j = 1; j < lg(R); ++j)
    1440             :   {
    1441        6639 :     GEN r = gel(R,j), be = gel(r,1), Rj = gel(r, 2);
    1442        6639 :     long e = valp(Rj);
    1443        6639 :     GEN z1 = gexpm1(gmul(h, gsub(s,be)), prec); /* exp(h(s-beta))-1 */
    1444        6639 :     GEN c1 = gaddgs(gdivsg(2, z1), 1); /* coth((h/2)(s-beta)) */
    1445        6639 :     GEN C1 = veccothderivn(c1, 1-e);
    1446       13341 :     for (i = e; i < 0; i++)
    1447             :     {
    1448        6702 :       GEN Rbe = mysercoeff(Rj, i);
    1449        6702 :       GEN p1 = polepart(-i, h, C1);
    1450        6702 :       S = gadd(S, gmul(Rbe, p1));
    1451             :     }
    1452             :   }
    1453        3316 :   return gmul2n(S, -1);
    1454             : }
    1455             : 
    1456             : static GEN lfunlambda_OK(GEN linit, GEN s, GEN sdom, long bitprec);
    1457             : /* L is a t_LDESC_PRODUCT Linit */
    1458             : static GEN
    1459        1377 : lfunlambda_product(GEN L, GEN s, GEN sdom, long bitprec)
    1460             : {
    1461        1377 :   pari_sp av = avma;
    1462        1377 :   GEN ldata = linit_get_ldata(L), v = lfunprod_get_fact(linit_get_tech(L));
    1463        1377 :   GEN r = gen_1, F = gel(v, 1), E = gel(v, 2), C = gel(v, 3);
    1464        1377 :   long i, l = lg(F);
    1465        1377 :   GEN cs = gconj(s);
    1466        1377 :   long isreal = gequal(imag_i(s), imag_i(cs));
    1467        4635 :   for (i = 1; i < l; ++i)
    1468             :   {
    1469        3258 :     GEN f = lfunlambda_OK(gel(F, i), s, sdom, bitprec);
    1470        3258 :     if (E[i])
    1471        3258 :       r = gmul(r, gpowgs(f, E[i]));
    1472        3258 :     if (C[i])
    1473             :     {
    1474         476 :       GEN fc = isreal ? f: lfunlambda_OK(gel(F, i), cs, sdom, bitprec);
    1475         476 :       r = gmul(r, gpowgs(gconj(fc), C[i]));
    1476             :     }
    1477             :   }
    1478        1377 :   if ((ldata_isreal(ldata) && gequal0(gimag(s)))) r = greal(r);
    1479        1377 :   return gerepileupto(av, r);
    1480             : }
    1481             : 
    1482             : /* s a t_SER */
    1483             : static long
    1484        1058 : der_level(GEN s)
    1485        1058 : { return signe(s)? lg(s)-3: valp(s)-1; }
    1486             : 
    1487             : /* s a t_SER; return coeff(s, X^0) */
    1488             : static GEN
    1489         210 : ser_coeff0(GEN s) { return simplify_shallow(polcoeff_i(s, 0, -1)); }
    1490             : 
    1491             : static GEN
    1492        5026 : get_domain(GEN s, GEN *dom, long *der)
    1493             : {
    1494        5026 :   GEN sa = s;
    1495        5026 :   *der = 0;
    1496        5026 :   switch(typ(s))
    1497             :   {
    1498             :     case t_POL:
    1499           7 :     case t_RFRAC: s = toser_i(s);
    1500             :     case t_SER:
    1501         210 :       *der = der_level(s);
    1502         210 :       sa = ser_coeff0(s);
    1503             :   }
    1504        5026 :   *dom = mkvec3(real_i(sa), gen_0, gabs(imag_i(sa),DEFAULTPREC));
    1505        5026 :   return s;
    1506             : }
    1507             : 
    1508             : /* assume lmisc is an linit, s went through get_domain and s/bitprec belong
    1509             :  * to domain */
    1510             : static GEN
    1511       19230 : lfunlambda_OK(GEN linit, GEN s, GEN sdom, long bitprec)
    1512             : {
    1513             :   GEN eno, ldata, tech, h, pol;
    1514       19230 :   GEN S, S0 = NULL, k2, cost;
    1515             :   long prec, prec0;
    1516             :   struct lfunp D, D0;
    1517             : 
    1518       19230 :   if (linit_get_type(linit) == t_LDESC_PRODUCT)
    1519        1377 :     return lfunlambda_product(linit, s, sdom, bitprec);
    1520       17853 :   ldata = linit_get_ldata(linit);
    1521       17853 :   eno = ldata_get_rootno(ldata);
    1522       17853 :   tech = linit_get_tech(linit);
    1523       17853 :   h = lfun_get_step(tech); prec = realprec(h);
    1524             :   /* try to reduce accuracy */
    1525       17853 :   parse_dom(0, sdom, &D0);
    1526       17853 :   parse_dom(0, domain_get_dom(lfun_get_domain(tech)), &D);
    1527       17853 :   if (0.8 * D.dh > D0.dh)
    1528             :   {
    1529       10267 :     cost = lfuncost(linit, sdom, typ(s)==t_SER? der_level(s): 0, bitprec);
    1530       10267 :     prec0 = nbits2prec(cost[2]);
    1531       10267 :     if (prec0 < prec) { prec = prec0; h = gprec_w(h, prec); }
    1532             :   }
    1533       17853 :   pol = lfun_get_pol(tech);
    1534       17853 :   s = gprec_w(s, prec);
    1535       17853 :   if (ldata_get_residue(ldata))
    1536             :   {
    1537        3736 :     GEN R = lfun_get_Residue(tech);
    1538        3736 :     GEN Ra = lfunpoleresidue(R, s);
    1539        3736 :     if (Ra) return gprec_w(Ra, nbits2prec(bitprec));
    1540        3316 :     S0 = lfunsumcoth(R, s, h, prec);
    1541             :   }
    1542       17433 :   k2 = lfun_get_k2(tech);
    1543       17433 :   if (typ(pol)==t_POL && gequal(real_i(s), k2))
    1544       12267 :   { /* on critical line: shortcut */
    1545       12267 :     GEN polz, b = imag_i(s);
    1546       12267 :     polz = gequal0(b)? poleval(pol,gen_1): poleval(pol, expIr(gmul(h,b)));
    1547       12267 :     S = gadd(polz, gmul(eno, gconj(polz)));
    1548             :   }
    1549             :   else
    1550             :   {
    1551        5166 :     GEN z = gexp(gmul(h, gsub(s, k2)), prec);
    1552        5166 :     GEN zi = ginv(z), zc = gconj(zi);
    1553        5166 :     if (typ(pol)==t_POL)
    1554        4977 :       S = gadd(poleval(pol, z), gmul(eno, gconj(poleval(pol, zc))));
    1555             :     else
    1556         189 :       S = gadd(poleval(gel(pol,1), z), gmul(eno, poleval(gel(pol,2), zi)));
    1557             :   }
    1558       17433 :   if (S0) S = gadd(S,S0);
    1559       17433 :   return gprec_w(gmul(S,h), nbits2prec(bitprec));
    1560             : }
    1561             : GEN
    1562         784 : lfunlambda(GEN lmisc, GEN s, long bitprec)
    1563             : {
    1564         784 :   pari_sp av = avma;
    1565             :   GEN linit, dom, z;
    1566             :   long der;
    1567         784 :   s = get_domain(s, &dom, &der);
    1568         784 :   linit = lfuninit(lmisc, dom, der, bitprec);
    1569         784 :   z = lfunlambda_OK(linit,s, dom, bitprec);
    1570         784 :   return gerepilecopy(av, z);
    1571             : }
    1572             : 
    1573             : /* assume lmisc is an linit, s went through get_domain and s/bitprec belong
    1574             :  * to domain */
    1575             : static GEN
    1576        3794 : lfun_OK(GEN linit, GEN s, GEN sdom, long bitprec)
    1577             : {
    1578        3794 :   GEN N, gas, S, FVga, res, ss = s;
    1579        3794 :   long prec = nbits2prec(bitprec);
    1580             : 
    1581        3794 :   FVga = lfun_get_factgammavec(linit_get_tech(linit));
    1582        3794 :   S = lfunlambda_OK(linit, s, sdom, bitprec);
    1583        3794 :   if (typ(S)==t_SER && typ(s)!=t_SER)
    1584             :   {
    1585         371 :     long d = fracgammadegree(FVga);
    1586         371 :     ss = deg1ser_shallow(gen_1, s, varn(S), lg(S)+d-2);
    1587             :   }
    1588        3794 :   gas = gammafactproduct(FVga, ss, prec);
    1589        3794 :   N = ldata_get_conductor(linit_get_ldata(linit));
    1590        3794 :   res = gdiv(S, gmul(gpow(N, gdivgs(ss, 2), prec), gas));
    1591        3794 :   if (typ(s)!=t_SER && typ(res)==t_SER)
    1592             :   {
    1593         406 :     long v = valp(res);
    1594         406 :     if (v > 0) return gen_0;
    1595         371 :     if (v == 0) res = gel(res, 2);
    1596             :     else
    1597         259 :       setlg(res, minss(lg(res), 2-v));
    1598             :   }
    1599        3759 :   return gprec_w(res, prec);
    1600             : }
    1601             : 
    1602             : GEN
    1603        3045 : lfun(GEN lmisc, GEN s, long bitprec)
    1604             : {
    1605        3045 :   pari_sp av = avma;
    1606             :   GEN linit, dom, z;
    1607             :   long der;
    1608        3045 :   s = get_domain(s, &dom, &der);
    1609        3045 :   linit = lfuninit(lmisc, dom, der, bitprec);
    1610        3045 :   z = lfun_OK(linit, s, dom, bitprec);
    1611        3045 :   return gerepilecopy(av, z);
    1612             : }
    1613             : 
    1614             : /* given a t_SER a+x*s(x), return x*s(x), shallow */
    1615             : static GEN
    1616          42 : sersplit1(GEN s, GEN *head)
    1617             : {
    1618          42 :   long i, l = lg(s);
    1619             :   GEN y;
    1620          42 :   *head = simplify_shallow(mysercoeff(s, 0));
    1621          42 :   if (valp(s) > 0) return s;
    1622          28 :   y = cgetg(l-1, t_SER); y[1] = s[1];
    1623          28 :   setvalp(y, 1);
    1624          28 :   for (i=3; i < l; i++) gel(y,i-1) = gel(s,i);
    1625          28 :   return normalize(y);
    1626             : }
    1627             : 
    1628             : /* n-th derivative of t_SER x, n > 0 */
    1629             : static GEN
    1630         112 : derivnser(GEN x, long n)
    1631             : {
    1632         112 :   long i, vx = varn(x), e = valp(x), lx = lg(x);
    1633             :   GEN y;
    1634         112 :   if (ser_isexactzero(x))
    1635             :   {
    1636           0 :     x = gcopy(x);
    1637           0 :     if (e) setvalp(x,e-n);
    1638           0 :     return x;
    1639             :   }
    1640         210 :   if (e < 0 || e >= n)
    1641             :   {
    1642          98 :     y = cgetg(lx,t_SER);
    1643          98 :     y[1] = evalsigne(1)| evalvalp(e-n) | evalvarn(vx);
    1644         490 :     for (i=0; i<lx-2; i++)
    1645         392 :       gel(y,i+2) = gmul(muls_interval(i+e-n+1,i+e), gel(x,i+2));
    1646             :   } else {
    1647          14 :     if (lx <= n+2) return zeroser(vx, 0);
    1648          14 :     lx -= n;
    1649          14 :     y = cgetg(lx,t_SER);
    1650          14 :     y[1] = evalsigne(1)|_evalvalp(0) | evalvarn(vx);
    1651          70 :     for (i=0; i<lx-2; i++)
    1652          56 :       gel(y,i+2) = gmul(muls_interval(i+1,i+n),gel(x,i+2+n-e));
    1653             :   }
    1654         112 :   return normalize(y);
    1655             : }
    1656             : 
    1657             : /* order of pole of Lambda at s (0 if regular point) */
    1658             : static long
    1659        1799 : lfunlambdaord(GEN linit, GEN s)
    1660             : {
    1661        1799 :   GEN tech = linit_get_tech(linit);
    1662        1799 :   if (linit_get_type(linit)==t_LDESC_PRODUCT)
    1663             :   {
    1664         252 :     GEN v = lfunprod_get_fact(linit_get_tech(linit));
    1665         252 :     GEN F = gel(v, 1), E = gel(v, 2), C = gel(v, 3);
    1666         252 :     long i, ex = 0, l = lg(F);
    1667         896 :     for (i = 1; i < l; i++)
    1668         644 :       ex += lfunlambdaord(gel(F,i), s) * (E[i]+C[i]);
    1669         252 :     return ex;
    1670             :   }
    1671        1547 :   if (ldata_get_residue(linit_get_ldata(linit)))
    1672             :   {
    1673         469 :     GEN r = lfunpoleresidue(lfun_get_Residue(tech), s);
    1674         469 :     if (r) return lg(r)-2;
    1675             :   }
    1676        1456 :   return 0;
    1677             : }
    1678             : 
    1679             : /* derivative of order m > 0 of L (flag = 0) or Lambda (flag = 1) */
    1680             : static GEN
    1681        1204 : lfunderiv(GEN lmisc, long m, GEN s, long flag, long bitprec)
    1682             : {
    1683        1204 :   pari_sp ltop = avma;
    1684        1204 :   GEN res, S = NULL, linit, dom;
    1685        1204 :   long der, prec = nbits2prec(bitprec);
    1686        1204 :   if (m <= 0) pari_err_DOMAIN("lfun", "D", "<=", gen_0, stoi(m));
    1687        1197 :   s = get_domain(s, &dom, &der);
    1688        1197 :   linit = lfuninit(lmisc, dom, der + m, bitprec);
    1689        1197 :   if (typ(s) == t_SER)
    1690             :   {
    1691          42 :     long v, l = lg(s)-1;
    1692             :     GEN sh;
    1693          42 :     if (valp(s) < 0) pari_err_DOMAIN("lfun","valuation", "<", gen_0, s);
    1694          42 :     S = sersplit1(s, &sh);
    1695          42 :     v = valp(S);
    1696          42 :     s = deg1ser_shallow(gen_1, sh, varn(S), m + (l+v-1)/v);
    1697             :   }
    1698             :   else
    1699             :   {
    1700        1155 :     long ex = lfunlambdaord(linit, s);
    1701             :     /* HACK: pretend lfuninit was done to right accuracy */
    1702        1155 :     s = deg1ser_shallow(gen_1, s, 0, m+1+ex);
    1703             :   }
    1704        1946 :   res = flag ? lfunlambda_OK(linit, s, dom, bitprec):
    1705         749 :                lfun_OK(linit, s, dom, bitprec);
    1706        1197 :   if (S)
    1707          42 :     res = gsubst(derivnser(res, m), varn(S), S);
    1708        1155 :   else if (typ(res)==t_SER)
    1709             :   {
    1710        1155 :     long v = valp(res);
    1711        1155 :     if (v > m) { avma = ltop; return gen_0; }
    1712        1155 :     if (v >= 0)
    1713        1085 :       res = gmul(mysercoeff(res, m), mpfact(m));
    1714             :     else
    1715          70 :       res = derivnser(res, m);
    1716             :   }
    1717        1197 :   return gerepilecopy(ltop, gprec_w(res, prec));
    1718             : }
    1719             : 
    1720             : GEN
    1721        1155 : lfunlambda0(GEN lmisc, GEN s, long der, long bitprec)
    1722             : {
    1723        1155 :   return der ? lfunderiv(lmisc, der, s, 1, bitprec):
    1724             :                lfunlambda(lmisc, s, bitprec);
    1725             : }
    1726             : 
    1727             : GEN
    1728        3073 : lfun0(GEN lmisc, GEN s, long der, long bitprec)
    1729             : {
    1730        3073 :   return der ? lfunderiv(lmisc, der, s, 0, bitprec):
    1731             :                lfun(lmisc, s, bitprec);
    1732             : }
    1733             : 
    1734             : GEN
    1735       10813 : lfunhardy(GEN lmisc, GEN t, long bitprec)
    1736             : {
    1737       10813 :   pari_sp ltop = avma;
    1738       10813 :   long prec = nbits2prec(bitprec), k, d;
    1739             :   GEN argz, z, linit, ldata, tech, dom, w2, k2, expot, h, a;
    1740             : 
    1741       10813 :   switch(typ(t))
    1742             :   {
    1743       10806 :     case t_INT: case t_FRAC: case t_REAL: break;
    1744           7 :     default: pari_err_TYPE("lfunhardy",t);
    1745             :   }
    1746             : 
    1747       10806 :   ldata = lfunmisc_to_ldata_shallow(lmisc);
    1748       10806 :   if (!is_linit(lmisc)) lmisc = ldata;
    1749       10806 :   k = ldata_get_k(ldata);
    1750       10806 :   d = ldata_get_degree(ldata);
    1751       10806 :   dom = mkvec3(dbltor(k/2.), gen_0, gabs(t,LOWDEFAULTPREC));
    1752       10806 :   linit = lfuninit(lmisc, dom, 0, bitprec);
    1753       10806 :   tech = linit_get_tech(linit);
    1754       10806 :   w2 = lfun_get_w2(tech);
    1755       10806 :   k2 = lfun_get_k2(tech);
    1756       10806 :   expot = lfun_get_expot(tech);
    1757       10806 :   z = mkcomplex(k2, t);
    1758       10806 :   argz = gatan(gdiv(t, k2), prec); /* more accurate than garg since k/2 \in Q */
    1759             :   /* prec may have increased: don't lose accuracy if |z|^2 is exact */
    1760       10806 :   prec = precision(argz);
    1761       10806 :   a = gsub(gmulsg(d, gmul(t, gmul2n(argz,-1))),
    1762             :            gmul(expot,glog(gnorm(z),prec)));
    1763       10806 :   h = lfunlambda_OK(linit, z, mkvec(t), bitprec);
    1764       10806 :   if (typ(ldata_get_dual(ldata))==t_INT)
    1765       10778 :     h = mulreal(h, w2);
    1766             :   else
    1767          28 :     h = gmul(h, w2);
    1768       10806 :   if (typ(h) == t_COMPLEX && gexpo(imag_i(h)) < -(bitprec >> 1))
    1769           0 :     h = real_i(h);
    1770       10806 :   return gerepileupto(ltop, gmul(h, gexp(a, prec)));
    1771             : }
    1772             : 
    1773             : /* L = log(t); return  \sum_{i = 0}^{v-1}  R[-i-1] L^i/i! */
    1774             : static GEN
    1775         931 : theta_pole_contrib(GEN R, long v, GEN L)
    1776             : {
    1777         931 :   GEN s = mysercoeff(R,-v);
    1778             :   long i;
    1779         980 :   for (i = v-1; i >= 1; i--)
    1780          49 :     s = gadd(mysercoeff(R,-i), gdivgs(gmul(s,L), i));
    1781         931 :   return s;
    1782             : }
    1783             : /* subtract successively rather than adding everything then subtracting.
    1784             :  * The polar part is "large" and suffers from cancellation: a little stabler
    1785             :  * this way */
    1786             : static GEN
    1787        2898 : theta_add_polar_part(GEN S, GEN R, GEN t, long prec)
    1788             : {
    1789        2898 :   GEN logt = NULL;
    1790        2898 :   long j, l = lg(R);
    1791        3829 :   for (j = 1; j < l; j++)
    1792             :   {
    1793         931 :     GEN Rj = gel(R,j), b = gel(Rj,1), Rb = gel(Rj,2);
    1794         931 :     long v = -valp(Rb);
    1795         931 :     if (v > 1 && !logt) logt = glog(t, prec);
    1796         931 :     S = gsub(S, gmul(theta_pole_contrib(Rb,v,logt), gpow(t,b,prec)));
    1797             :   }
    1798        2898 :   return S;
    1799             : }
    1800             : 
    1801             : /* Check whether the coefficients, conductor, weight, polar part and root
    1802             :  * number are compatible with the functional equation at t0 and 1/t0.
    1803             :  * Different from lfunrootres. */
    1804             : long
    1805        2499 : lfuncheckfeq(GEN lmisc, GEN t0, long bitprec)
    1806             : {
    1807             :   GEN ldata, theta, thetad, t0i, S0, S0i, w, eno;
    1808             :   long e, prec;
    1809             :   pari_sp av;
    1810             : 
    1811        2499 :   if (is_linit(lmisc) && linit_get_type(lmisc)==t_LDESC_PRODUCT)
    1812             :   {
    1813         126 :     GEN v = lfunprod_get_fact(linit_get_tech(lmisc)), F = gel(v,1);
    1814         126 :     long i, b = -bitprec, l = lg(F);
    1815         441 :     for (i = 1; i < l; i++)
    1816         315 :       b = maxss(b, lfuncheckfeq(gel(F,i), t0, bitprec));
    1817         126 :     return b;
    1818             :   }
    1819        2373 :   av = avma;
    1820        2373 :   prec = nbits2prec(bitprec);
    1821        2373 :   if (!t0)
    1822             :   {
    1823        2303 :     t0 = gadd(gdivgs(mppi(prec), 3), gdivgs(gen_I(), 7));
    1824        2303 :     t0i = ginv(t0);
    1825             :   }
    1826          70 :   else if (gcmpgs(gnorm(t0), 1) < 0)
    1827             :   {
    1828           7 :     t0i = t0;
    1829           7 :     t0 = ginv(t0);
    1830             :   }
    1831             :   else
    1832          63 :     t0i = ginv(t0);
    1833             :   /* |t0| >= 1 */
    1834        2373 :   theta = lfunthetacheckinit(lmisc, t0i, 0, bitprec);
    1835        2373 :   ldata = linit_get_ldata(theta);
    1836        2373 :   thetad = theta_dual(theta, ldata_get_dual(ldata));
    1837        2373 :   if (thetad)
    1838          35 :     S0 = lfuntheta(thetad, t0, 0, bitprec);
    1839             :   else
    1840        2338 :     S0 = gconj(lfuntheta(theta, gconj(t0), 0, bitprec));
    1841        2373 :   S0i = lfuntheta(theta, t0i, 0, bitprec);
    1842             : 
    1843        2373 :   eno = ldata_get_rootno(ldata);
    1844        2373 :   if (ldata_get_residue(ldata))
    1845             :   {
    1846         462 :     GEN R = theta_get_R(linit_get_tech(theta));
    1847         462 :     if (gequal0(R))
    1848             :     {
    1849             :       GEN v, r;
    1850          21 :       if (ldata_get_type(ldata) == t_LFUN_NF)
    1851             :       { /* inefficient since theta not needed; no need to optimize for this
    1852             :            (artificial) query [e.g. lfuncheckfeq(t_POL)] */
    1853          21 :         GEN T = gel(ldata_get_an(ldata), 2);
    1854          21 :         GEN L = lfunzetakinit(T,zerovec(3),0,0,bitprec);
    1855          21 :         long e = lfuncheckfeq(L,t0,bitprec);
    1856          21 :         avma = av; return e;
    1857             :       }
    1858           0 :       v = lfunrootres(theta, bitprec);
    1859           0 :       r = gel(v,1);
    1860           0 :       if (gequal0(eno)) eno = gel(v,3);
    1861           0 :       R = lfunrtoR_i(ldata, r, eno, nbits2prec(bitprec));
    1862             :     }
    1863         441 :     S0i = theta_add_polar_part(S0i, R, t0, prec);
    1864             :   }
    1865        2352 :   if (gequal0(S0i) || gequal0(S0)) pari_err_PREC("lfuncheckfeq");
    1866        2352 :   w = gdiv(S0i, gmul(S0, gpowgs(t0, ldata_get_k(ldata))));
    1867             :   /* missing rootno: guess it */
    1868        2352 :   if (gequal0(eno)) eno = lfunrootno(theta, bitprec);
    1869        2352 :   w = gsub(w, eno);
    1870        2352 :   if (thetad) w = gdiv(w, eno); /* |eno| may be large in non-dual case */
    1871        2352 :   e = gexpo(w);
    1872        2352 :   avma = av; return e;
    1873             : }
    1874             : 
    1875             : /*******************************************************************/
    1876             : /*       Compute root number and residues                          */
    1877             : /*******************************************************************/
    1878             : /* round root number to \pm 1 if close to integer. */
    1879             : static GEN
    1880        2457 : ropm1(GEN eno, long prec)
    1881             : {
    1882             :   long e;
    1883        2457 :   GEN r = grndtoi(eno, &e);
    1884        2457 :   return (e < -prec2nbits(prec)/2)? r: eno;
    1885             : }
    1886             : 
    1887             : /* theta for t=1/sqrt(2) and t2==2t simultaneously, saving 25% of the work.
    1888             :  * Assume correct initialization (no thetacheck) */
    1889             : static void
    1890          14 : lfunthetaspec(GEN linit, long bitprec, GEN *pv, GEN *pv2)
    1891             : {
    1892          14 :   pari_sp av = avma;
    1893             :   GEN t, Vga, an, K, ldata, thetainit, v, v2, vroots;
    1894             :   long L, prec, n, d;
    1895             : 
    1896          14 :   ldata = linit_get_ldata(linit);
    1897          14 :   thetainit = linit_get_tech(linit);
    1898          14 :   prec = nbits2prec(bitprec);
    1899          14 :   Vga = ldata_get_gammavec(ldata); d = lg(Vga)-1;
    1900          14 :   if (d == 1 || lfunisvgaell(Vga, 1))
    1901             :   {
    1902           0 :     GEN v2 = sqrtr(real2n(1, nbits2prec(bitprec)));
    1903           0 :     GEN v = shiftr(v2,-1);
    1904           0 :     *pv = lfuntheta(linit, v,  0, bitprec);
    1905           0 :     *pv2= lfuntheta(linit, v2, 0, bitprec);
    1906          14 :     return;
    1907             :   }
    1908          14 :   an = RgV_kill0( theta_get_an(thetainit) );
    1909          14 :   L = lg(an)-1;
    1910             :   /* to compute theta(1/sqrt(2)) */
    1911          14 :   t = ginv(gsqrt(gmul2n(ldata_get_conductor(ldata), 1), prec));
    1912             :   /* t = 1/sqrt(2N) */
    1913             : 
    1914             :   /* From then on, the code is generic and could be used to compute
    1915             :    * theta(t) / theta(2t) without assuming t = 1/sqrt(2) */
    1916          14 :   K = theta_get_K(thetainit);
    1917          14 :   vroots = mkvroots(d, L, prec);
    1918          14 :   t = gpow(t, gdivgs(gen_2, d), prec); /* rt variant: t->t^(2/d) */
    1919             :   /* v = \sum_{n <= L, n odd} a_n K(nt) */
    1920       78407 :   for (v = gen_0, n = 1; n <= L; n+=2)
    1921             :   {
    1922       78393 :     GEN tn, Kn, a = gel(an, n);
    1923             : 
    1924       78393 :     if (!a) continue;
    1925        7371 :     tn = gmul(t, gel(vroots,n));
    1926        7371 :     Kn = gammamellininvrt(K, tn, bitprec);
    1927        7371 :     v = gadd(v, gmul(a,Kn));
    1928             :   }
    1929             :   /* v += \sum_{n <= L, n even} a_n K(nt), v2 = \sum_{n <= L/2} a_n K(2n t) */
    1930       78393 :   for (v2 = gen_0, n = 1; n <= L/2; n++)
    1931             :   {
    1932       78379 :     GEN t2n, K2n, a = gel(an, n), a2 = gel(an,2*n);
    1933             : 
    1934       78379 :     if (!a && !a2) continue;
    1935        4270 :     t2n = gmul(t, gel(vroots,2*n));
    1936        4270 :     K2n = gammamellininvrt(K, t2n, bitprec);
    1937        4270 :     if (a) v2 = gadd(v2, gmul(a, K2n));
    1938        4270 :     if (a2) v = gadd(v,  gmul(a2,K2n));
    1939             :   }
    1940          14 :   *pv = v;
    1941          14 :   *pv2 = v2;
    1942          14 :   gerepileall(av, 2, pv,pv2);
    1943             : }
    1944             : 
    1945             : static GEN
    1946           7 : Rtor(GEN a, GEN R, GEN ldata, long prec)
    1947             : {
    1948           7 :   GEN FVga = gammafactor(ldata_get_gammavec(ldata));
    1949           7 :   GEN Na = gpow(ldata_get_conductor(ldata), gdivgs(a,2), prec);
    1950           7 :   return gdiv(R, gmul(Na, gammafactproduct(FVga, a, prec)));
    1951             : }
    1952             : 
    1953             : /* v = theta~(t), vi = theta(1/t) */
    1954             : static GEN
    1955        2457 : get_eno(GEN R, long k, GEN t, GEN v, GEN vi, long vx, long bitprec)
    1956             : {
    1957        2457 :   long prec = nbits2prec(bitprec);
    1958             :   GEN a0, a1;
    1959        2457 :   GEN S = deg1pol(gmul(gpowgs(t,k), gneg(v)), vi, vx);
    1960             : 
    1961        2457 :   S = theta_add_polar_part(S, R, t, prec);
    1962        2457 :   if (typ(S) != t_POL || degpol(S) != 1) return NULL;
    1963        2457 :   a1 = gel(S,3); if (gexpo(a1) < -bitprec/4) return NULL;
    1964        2450 :   a0 = gel(S,2);
    1965        2450 :   return gdiv(a0, gneg(a1));
    1966             : 
    1967             : }
    1968             : /* Return w using theta(1/t) - w t^k \bar{theta}(t) = polar_part(t,w).
    1969             :  * The full Taylor development of L must be known */
    1970             : GEN
    1971        2450 : lfunrootno(GEN linit, long bitprec)
    1972             : {
    1973             :   GEN ldata, t, eno, v, vi, R, thetad;
    1974        2450 :   long k, prec = nbits2prec(bitprec), vx = fetch_var();
    1975             :   pari_sp av;
    1976             : 
    1977             :   /* initialize for t > 1/sqrt(2) */
    1978        2450 :   linit = lfunthetacheckinit(linit, dbltor(sqrt(0.5)), 0, bitprec);
    1979        2450 :   ldata = linit_get_ldata(linit);
    1980        2450 :   k = ldata_get_k(ldata);
    1981        4914 :   R = ldata_get_residue(ldata)? lfunrtoR_eno(ldata, pol_x(vx), prec)
    1982        2464 :                               : cgetg(1, t_VEC);
    1983        2450 :   t = gen_1;
    1984        2450 :   v = lfuntheta(linit, t, 0, bitprec);
    1985        2450 :   thetad = theta_dual(linit, ldata_get_dual(ldata));
    1986        2450 :   vi = !thetad ? gconj(v):
    1987             :        lfuntheta(thetad, t, 0, bitprec);
    1988        2450 :   eno = get_eno(R,k,t,vi,v, vx, bitprec);
    1989        2450 :   if (!eno && !thetad)
    1990             :   { /* t = sqrt(2), vi = theta(1/t), v = theta(t) */
    1991           7 :     lfunthetaspec(linit, bitprec, &vi, &v);
    1992           7 :     t = sqrtr(utor(2, prec));
    1993           7 :     eno = get_eno(R,k,t,gconj(v),vi, vx, bitprec);
    1994             :   }
    1995        2450 :   av = avma;
    1996        4900 :   while (!eno)
    1997             :   {
    1998           0 :     t = addsr(1, shiftr(utor(pari_rand(), prec), -66)); /* in [1,1.25[ */
    1999           0 :     v = !thetad ?  gconj(lfuntheta(linit, t, 0, bitprec)):
    2000             :           lfuntheta(thetad, t, 0, bitprec);
    2001           0 :     vi= lfuntheta(linit, ginv(t), 0, bitprec);
    2002           0 :     eno = get_eno(R,k,t,v,vi, vx, bitprec);
    2003           0 :     avma = av;
    2004             :   }
    2005        2450 :   delete_var(); return ropm1(eno,prec);
    2006             : }
    2007             : 
    2008             : static int
    2009          21 : residues_known(GEN r)
    2010             : {
    2011          21 :   long i, l = lg(r);
    2012          35 :   for (i = 1; i < l; i++)
    2013          21 :     if (gequal0(gmael(r, i, 2))) return 0;
    2014          14 :   return 1;
    2015             : }
    2016             : 
    2017             : /* Find root number and/or residues when L-function coefficients and
    2018             :    conductor are known. For the moment at most a single residue allowed. */
    2019             : GEN
    2020        1085 : lfunrootres(GEN data, long bitprec)
    2021             : {
    2022        1085 :   pari_sp ltop = avma;
    2023             :   GEN w, r, R, a, b, e, v, v2, be, ldata, linit;
    2024             :   long k, prec;
    2025             : 
    2026        1085 :   ldata = lfunmisc_to_ldata_shallow(data);
    2027        1085 :   r = ldata_get_residue(ldata);
    2028        1085 :   k = ldata_get_k(ldata);
    2029        1085 :   if (r) r = normalize_simple_pole(r, k);
    2030        1085 :   if (!r || residues_known(r))
    2031             :   {
    2032        1078 :     w = lfunrootno(data, bitprec);
    2033        1078 :     if (!r)
    2034        1064 :       r = R = gen_0;
    2035             :     else
    2036          14 :       R = lfunrtoR_eno(ldata, w, nbits2prec(bitprec));
    2037        1078 :     return gerepilecopy(ltop, mkvec3(r, R, w));
    2038             :   }
    2039           7 :   linit = lfunthetacheckinit(data, dbltor(sqrt(0.5)), 0, bitprec);
    2040           7 :   prec = nbits2prec(bitprec);
    2041           7 :   if (lg(r) > 2) pari_err_IMPL("multiple poles in lfunrootres");
    2042             :   /* Now residue unknown, and r = [[be,0]]. */
    2043           7 :   be = gmael(r, 1, 1);
    2044           7 :   w = ldata_get_rootno(ldata);
    2045           7 :   if (ldata_isreal(ldata) && gequalm1(w))
    2046             :   {
    2047           0 :     GEN R = lfuntheta(linit, gen_1, 0, bitprec);
    2048           0 :     r = Rtor(be, R, ldata, prec);
    2049           0 :     return gerepilecopy(ltop, mkvec3(r, R, gen_m1));
    2050             :   }
    2051           7 :   lfunthetaspec(linit, bitprec, &v2, &v);
    2052           7 :   if (gequalgs(gmulsg(2, be), k)) pari_err_IMPL("pole at k/2 in lfunrootres");
    2053           7 :   if (gequalgs(be, k))
    2054             :   {
    2055           7 :     GEN p2k = int2n(k);
    2056           7 :     a = gconj(gsub(gmul(p2k, v), v2));
    2057           7 :     b = subiu(p2k, 1);
    2058           7 :     e = gmul(gsqrt(p2k, prec), gsub(v2, v));
    2059             :   }
    2060             :   else
    2061             :   {
    2062           0 :     GEN tk2 = gsqrt(int2n(k), prec);
    2063           0 :     GEN tbe = gpow(gen_2, be, prec);
    2064           0 :     GEN tkbe = gpow(gen_2, gdivgs(gsubsg(k, be), 2), prec);
    2065           0 :     a = gconj(gsub(gmul(tbe, v), v2));
    2066           0 :     b = gsub(gdiv(tbe, tkbe), tkbe);
    2067           0 :     e = gsub(gmul(gdiv(tbe, tk2), v2), gmul(tk2, v));
    2068             :   }
    2069           7 :   if (!isintzero(w)) R = gdiv(gsub(e, gmul(a, w)), b);
    2070             :   else
    2071             :   { /* Now residue unknown, r = [[be,0]], and w unknown. */
    2072           0 :     GEN t0  = mkfrac(stoi(11),stoi(10));
    2073           0 :     GEN th1 = lfuntheta(linit, t0,  0, bitprec);
    2074           0 :     GEN th2 = lfuntheta(linit, ginv(t0), 0, bitprec);
    2075           0 :     GEN tbe = gpow(t0, gmulsg(2, be), prec);
    2076           0 :     GEN tkbe = gpow(t0, gsubsg(k, be), prec);
    2077           0 :     GEN tk2 = gpowgs(t0, k);
    2078           0 :     GEN c = gconj(gsub(gmul(tbe, th1), th2));
    2079           0 :     GEN d = gsub(gdiv(tbe, tkbe), tkbe);
    2080           0 :     GEN f = gsub(gmul(gdiv(tbe, tk2), th2), gmul(tk2, th1));
    2081           0 :     GEN D = gsub(gmul(a, d), gmul(b, c));
    2082           0 :     w = gdiv(gsub(gmul(d, e), gmul(b, f)), D);
    2083           0 :     R = gdiv(gsub(gmul(a, f), gmul(c, e)), D);
    2084             :   }
    2085           7 :   r = Rtor(be, R, ldata, prec);
    2086           7 :   return gerepilecopy(ltop, mkvec3(r, R, ropm1(w, prec)));
    2087             : }
    2088             : 
    2089             : /*******************************************************************/
    2090             : /*                           Zeros                                 */
    2091             : /*******************************************************************/
    2092             : struct lhardyz_t {
    2093             :   long bitprec, prec;
    2094             :   GEN linit;
    2095             : };
    2096             : 
    2097             : static GEN
    2098       10344 : lfunhardyzeros(void *E, GEN t)
    2099             : {
    2100       10344 :   struct lhardyz_t *S = (struct lhardyz_t*)E;
    2101       10344 :   long prec = S->prec;
    2102       10344 :   GEN h = lfunhardy(S->linit, t, S->bitprec);
    2103       10344 :   if (typ(h) == t_REAL && realprec(h) < prec) h = gprec_w(h, prec);
    2104       10344 :   return h;
    2105             : }
    2106             : 
    2107             : /* initialize for computation on critical line up to height h, zero
    2108             :  * of order <= m */
    2109             : static GEN
    2110         392 : lfuncenterinit(GEN lmisc, double h, long m, long bitprec)
    2111             : {
    2112         392 :   if (m < 0)
    2113             :   { /* choose a sensible default */
    2114         392 :     if (!is_linit(lmisc) || linit_get_type(lmisc) != t_LDESC_INIT) m = 4;
    2115             :     else
    2116             :     {
    2117         357 :       GEN domain = lfun_get_domain(linit_get_tech(lmisc));
    2118         357 :       m = domain_get_der(domain);
    2119             :     }
    2120             :   }
    2121         392 :   return lfuninit(lmisc, mkvec(dbltor(h)), m, bitprec);
    2122             : }
    2123             : 
    2124             : long
    2125         420 : lfunorderzero(GEN lmisc, long m, long bitprec)
    2126             : {
    2127         420 :   pari_sp ltop = avma;
    2128             :   GEN eno, ldata, linit, k2;
    2129             :   long G, c0, c, st, k;
    2130             : 
    2131         420 :   if (is_linit(lmisc) && linit_get_type(lmisc) == t_LDESC_PRODUCT)
    2132             :   {
    2133          70 :     GEN M = gmael(linit_get_tech(lmisc), 2,1);
    2134             :     long i;
    2135          70 :     for (c=0,i=1; i < lg(M); i++) c += lfunorderzero(gel(M,i), m, bitprec);
    2136          70 :     return c;
    2137             :   }
    2138         350 :   linit = lfuncenterinit(lmisc, 0, m, bitprec);
    2139         350 :   ldata = linit_get_ldata(linit);
    2140         350 :   eno = ldata_get_rootno(ldata);
    2141         350 :   G = -bitprec/2;
    2142         350 :   c0 = 0; st = 1;
    2143         350 :   if (ldata_isreal(ldata))
    2144             :   {
    2145         280 :     if (!gequal1(eno)) c0 = 1;
    2146         280 :     st = 2;
    2147             :   }
    2148         350 :   k = ldata_get_k(ldata);
    2149         350 :   k2 = gdivgs(stoi(k), 2);
    2150         364 :   for (c = c0;; c += st)
    2151         364 :     if (gexpo(lfun0(linit, k2, c, bitprec)) > G) break;
    2152          14 :   avma = ltop; return c;
    2153             : }
    2154             : 
    2155             : GEN
    2156          42 : lfunzeros(GEN ldata, GEN lim, long divz, long bitprec)
    2157             : {
    2158          42 :   pari_sp ltop = avma;
    2159             :   GEN ldataf, linit, N, pi2, cN, pi2div, w, T, Vga, h1, h2;
    2160          42 :   long i, d, W, NEWD, precinit, ct, s, prec = nbits2prec(bitprec);
    2161             :   double maxt;
    2162             :   GEN maxtr, maxtr1;
    2163             :   struct lhardyz_t S;
    2164             : 
    2165          42 :   if (typ(lim) == t_VEC)
    2166             :   {
    2167           7 :     if (lg(lim) != 3 || gcmp(gel(lim,1),gel(lim,2)) >= 0
    2168           7 :                      || gcmp(gel(lim,1),gen_0) <= 0)
    2169           0 :       pari_err_TYPE("lfunzeros",lim);
    2170           7 :     h1 = gel(lim,1); h2 = gel(lim,2);
    2171             :   }
    2172             :   else
    2173             :   {
    2174          35 :     if (gcmp(lim,gen_0) <= 0)
    2175           0 :       pari_err_TYPE("lfunzeros",lim);
    2176          35 :     h1 = gen_0; h2 = lim;
    2177             :   }
    2178          42 :   maxt = gtodouble(h2);
    2179             : 
    2180          42 :   if (is_linit(ldata) && linit_get_type(ldata) == t_LDESC_PRODUCT)
    2181             :   {
    2182           0 :     GEN v, M = gmael(linit_get_tech(ldata), 2,1);
    2183           0 :     long l = lg(M);
    2184           0 :     v = cgetg(l, t_VEC);
    2185           0 :     for (i = 1; i < l; i++)
    2186           0 :       gel(v,i) = lfunzeros(gel(M,i), lim, divz, bitprec);
    2187           0 :     return gerepileupto(ltop, vecsort0(shallowconcat1(v), NULL, 0));
    2188             :   }
    2189          42 :   S.linit = linit = lfuncenterinit(ldata, maxt + 1, -1, bitprec);
    2190          42 :   S.bitprec = bitprec;
    2191          42 :   S.prec = prec;
    2192          42 :   ldataf = linit_get_ldata(linit);
    2193          42 :   Vga = ldata_get_gammavec(ldataf); d = lg(Vga) - 1;
    2194          42 :   N = ldata_get_conductor(ldataf);
    2195          42 :   NEWD = minss((long) ceil(bitprec+(M_PI/(4*LOG2))*d*maxt),
    2196             :                lfun_get_bitprec(linit_get_tech(linit)));
    2197          42 :   precinit = prec; prec = nbits2prec(NEWD);
    2198          42 :   pi2 = Pi2n(1, prec);
    2199          42 :   cN = gdiv(N, gpowgs(Pi2n(-1, prec), d));
    2200          42 :   cN = gexpo(cN) >= 0? gaddsg(d, gmulsg(2, glog(cN, prec))): stoi(d);
    2201          42 :   pi2div = gdivgs(pi2, labs(divz));
    2202          42 :   ct = 0;
    2203          42 :   T = h1;
    2204          42 :   if (gequal0(h1))
    2205             :   {
    2206          35 :     GEN r = ldata_get_residue(ldataf);
    2207          35 :     if (!r || gequal0(r))
    2208             :     {
    2209          21 :       ct = lfunorderzero(linit, -1, bitprec);
    2210          21 :       if (ct) T = real2n(-prec2nbits(prec)/(2*ct), prec);
    2211             :     }
    2212             :   }
    2213             :   /* initialize for 100 further zeros, double later if needed */
    2214          42 :   W = 100 + ct; w = cgetg(W+1,t_VEC);
    2215          42 :   for (i=1; i<=ct; i++) gel(w,i) = gen_0;
    2216          42 :   s = gsigne(lfunhardyzeros(&S, T));
    2217          42 :   maxtr = h2; maxtr1 = gaddsg(1, maxtr);
    2218         427 :   while (gcmp(T, maxtr1) < 0)
    2219             :   {
    2220         385 :     pari_sp av = avma;
    2221         385 :     GEN T0 = T, z;
    2222             :     for(;;)
    2223             :     {
    2224             :       long s0;
    2225             :       GEN L;
    2226        6090 :       if (gcmp(T, pi2) >= 0)
    2227        4221 :         L = gadd(cN, gmulsg(d, glog(gdiv(T, pi2), prec)));
    2228             :       else
    2229        1869 :         L = cN;
    2230        6090 :       T = gadd(T, gdiv(pi2div, L));
    2231        6090 :       if (gcmp(T, maxtr1) > 0) goto END;
    2232        6069 :       s0 = gsigne(lfunhardyzeros(&S, T));
    2233        6069 :       if (s0 != s) { s = s0; break; }
    2234        5705 :     }
    2235         364 :     T = gerepileupto(av, T);
    2236         364 :     z = zbrent(&S, lfunhardyzeros, T0, T, prec);
    2237         364 :     if (gcmp(z, maxtr) > 0) break;
    2238         343 :     if (typ(z) == t_REAL) z  = rtor(z, precinit);
    2239             :     /* room for twice as many zeros */
    2240         343 :     if (ct >= W) { W *= 2; w = vec_lengthen(w, W); }
    2241         343 :     gel(w, ++ct) = z;
    2242             :   }
    2243             : END:
    2244          42 :   setlg(w, ct+1); return gerepilecopy(ltop, w);
    2245             : }
    2246             : 
    2247             : /*******************************************************************/
    2248             : /*       Guess conductor                                           */
    2249             : /*******************************************************************/
    2250             : struct huntcond_t {
    2251             :   long k;
    2252             :   GEN data, thetad;
    2253             :   GEN *pM, *psqrtM, *pMd, *psqrtMd;
    2254             : };
    2255             : 
    2256             : /* M should eventually converge to N, the conductor. L has no pole. */
    2257             : static GEN
    2258        6473 : wrap1(void *E, GEN M)
    2259             : {
    2260        6473 :   struct huntcond_t *S = (struct huntcond_t*)E;
    2261        6473 :   GEN data = S->data, thetainit, tk, p1, p1inv;
    2262        6473 :   GEN t = mkfrac(stoi(11), stoi(10));
    2263             :   long prec, bitprec;
    2264             : 
    2265        6473 :   thetainit = linit_get_tech(data);
    2266        6473 :   bitprec = theta_get_bitprec(thetainit);
    2267        6473 :   prec = nbits2prec(bitprec);
    2268        6473 :   *(S->pM) = M;
    2269        6473 :   *(S->psqrtM) = gsqrt(M, prec);
    2270        6473 :   tk = gpowgs(t, S->k);
    2271        6473 :   if (S->thetad)
    2272             :   {
    2273           0 :     *(S->pMd) = M;
    2274           0 :     *(S->psqrtMd) = *(S->psqrtM);
    2275           0 :     p1 = lfuntheta(S->thetad, t, 0, bitprec);
    2276             :   }
    2277             :   else
    2278        6473 :     p1 = lfuntheta(data, t, 0, bitprec);
    2279        6473 :   p1inv = lfuntheta(data, ginv(t), 0, bitprec);
    2280        6473 :   return glog(gabs(gmul(tk, gdiv(p1, p1inv)), prec), prec);
    2281             : }
    2282             : 
    2283             : /* M should eventually converge to N, the conductor. L has a pole. */
    2284             : static GEN
    2285        2194 : wrap2(void *E, GEN M)
    2286             : {
    2287        2194 :   struct huntcond_t *S = (struct huntcond_t*)E;
    2288        2194 :   GEN data = S->data, t1k, t2k, p1, p1inv, p2, p2inv;
    2289             :   GEN thetainit, R;
    2290        2194 :   GEN t1 = mkfrac(stoi(11), stoi(10)), t2 = mkfrac(stoi(13), stoi(11));
    2291             :   GEN t1be, t2be, t1bemk, t2bemk, t1kmbe, t2kmbe;
    2292             :   GEN F11, F12, F21, F22, P1, P2, res;
    2293        2194 :   long k = S->k, prec, bitprec;
    2294             : 
    2295        2194 :   thetainit = linit_get_tech(data);
    2296        2194 :   bitprec = theta_get_bitprec(thetainit);
    2297        2194 :   prec = nbits2prec(bitprec);
    2298        2194 :   *(S->pM) = M;
    2299        2194 :   *(S->psqrtM) = gsqrt(M, prec);
    2300             : 
    2301        2194 :   p1 = lfuntheta(data, t1, 0, bitprec);
    2302        2194 :   p2 = lfuntheta(data, t2, 0, bitprec);
    2303        2194 :   p1inv = lfuntheta(data, ginv(t1), 0, bitprec);
    2304        2194 :   p2inv = lfuntheta(data, ginv(t2), 0, bitprec);
    2305        2194 :   t1k = gpowgs(t1, k);
    2306        2194 :   t2k = gpowgs(t2, k);
    2307        2194 :   R = theta_get_R(thetainit);
    2308        2194 :   if (typ(R) == t_VEC)
    2309             :   {
    2310        2194 :     GEN be = gmael(R, 1, 1);
    2311        2194 :     t1be = gpow(t1, be, prec); t1bemk = gdiv(gsqr(t1be), t1k);
    2312        2194 :     t2be = gpow(t2, be, prec); t2bemk = gdiv(gsqr(t2be), t2k);
    2313        2194 :     t1kmbe = gdiv(t1k, t1be);
    2314        2194 :     t2kmbe = gdiv(t2k, t2be);
    2315             :   }
    2316             :   else
    2317             :   { /* be = k */
    2318           0 :     t1be = t1k; t1bemk = t1k; t1kmbe = gen_1;
    2319           0 :     t2be = t2k; t2bemk = t2k; t2kmbe = gen_1;
    2320             :   }
    2321        2194 :   F11 = gconj(gsub(gmul(gsqr(t1be), p1), p1inv));
    2322        2194 :   F12 = gconj(gsub(gmul(gsqr(t2be), p2), p2inv));
    2323        2194 :   F21 = gsub(gmul(t1k, p1), gmul(t1bemk, p1inv));
    2324        2194 :   F22 = gsub(gmul(t2k, p2), gmul(t2bemk, p2inv));
    2325        2194 :   P1 = gsub(gmul(t1bemk, t1be), t1kmbe);
    2326        2194 :   P2 = gsub(gmul(t2bemk, t2be), t2kmbe);
    2327        2194 :   res = gdiv(gsub(gmul(P2,F21), gmul(P1,F22)),
    2328             :              gsub(gmul(P2,F11), gmul(P1,F12)));
    2329        2194 :   return glog(gabs(res, prec), prec);
    2330             : }
    2331             : 
    2332             : /* If flag = 0 (default) return all conductors found as integers. If
    2333             : flag = 1, return the approximations, not the integers. If flag = 2,
    2334             : return all, even nonintegers. */
    2335             : 
    2336             : static GEN
    2337          84 : checkconductor(GEN v, long bit, long flag)
    2338             : {
    2339             :   GEN w;
    2340          84 :   long e, j, k, l = lg(v);
    2341          84 :   if (flag == 2) return v;
    2342          84 :   w = cgetg(l, t_VEC);
    2343         301 :   for (j = k = 1; j < l; j++)
    2344             :   {
    2345         217 :     GEN N = grndtoi(gel(v,j), &e);
    2346         217 :     if (e < -bit) gel(w,k++) = flag ? gel(v,j): N;
    2347             :   }
    2348          84 :   if (k == 2) return gel(w,1);
    2349           7 :   setlg(w,k); return w;
    2350             : }
    2351             : 
    2352             : static void
    2353          84 : parse_maxcond(GEN maxcond, GEN *pm, GEN *pM)
    2354             : {
    2355          84 :   GEN m = gen_1, M;
    2356          84 :   if (!maxcond)
    2357          49 :     M = utoipos(10000);
    2358          35 :   else if (typ(maxcond) == t_VEC)
    2359             :   {
    2360           7 :     if (lg(maxcond) != 3) pari_err_TYPE("lfunconductor", maxcond);
    2361           7 :     m = gel(maxcond,1);
    2362           7 :     M = gel(maxcond,2);
    2363             :   }
    2364             :   else
    2365          28 :     M = maxcond;
    2366          84 :   m = (typ(m) == t_INT)? gsub(m,ghalf): gfloor(m);
    2367          84 :   if (signe(m) <= 0) m = ghalf;
    2368          84 :   M = (typ(M) == t_INT)? addiu(M, 1): gceil(M);
    2369          84 :   *pm = m;
    2370          84 :   *pM = M;
    2371          84 : }
    2372             : 
    2373             : GEN
    2374          84 : lfunconductor(GEN data, GEN maxcond, long flag, long bitprec)
    2375             : {
    2376             :   struct huntcond_t S;
    2377          84 :   pari_sp ltop = avma;
    2378             :   GEN ld, r, v, ldata, theta, thetad, m, M, tdom;
    2379             :   GEN (*eval)(void *, GEN);
    2380          84 :   bitprec = 3*bitprec/2;
    2381          84 :   ldata = lfunmisc_to_ldata_shallow(data);
    2382          84 :   parse_maxcond(maxcond, &m,&M);
    2383          84 :   r = ldata_get_residue(ldata);
    2384          84 :   if (r && typ(r) == t_VEC)
    2385             :   {
    2386           0 :     if (lg(r) > 2) pari_err_IMPL("multiple poles in lfunconductor");
    2387           0 :     r = gmael(r,1,2);
    2388             :   }
    2389          84 :   if (!r)
    2390          63 :   { eval = wrap1; tdom = mkfrac(stoi(10), stoi(11)); }
    2391             :   else
    2392          21 :   { eval = wrap2; tdom = mkfrac(stoi(11), stoi(13)); }
    2393          84 :   ld = shallowcopy(ldata);
    2394          84 :   gel(ld, 5) = M;
    2395          84 :   theta = lfunthetainit_i(ld, tdom, 0, bitprec);
    2396          84 :   thetad = theta_dual(theta, ldata_get_dual(ldata));
    2397          84 :   gel(theta,3) = shallowcopy(linit_get_tech(theta));
    2398          84 :   S.k = ldata_get_k(ldata);
    2399          84 :   S.data = theta;
    2400          84 :   S.thetad = thetad;
    2401          84 :   S.pM = &gel(linit_get_ldata(theta),5);
    2402          84 :   S.psqrtM = &gel(linit_get_tech(theta),7);
    2403          84 :   if (thetad)
    2404             :   {
    2405           0 :     S.pMd = &gel(linit_get_ldata(thetad),5);
    2406           0 :     S.psqrtMd = &gel(linit_get_tech(thetad),7);
    2407             :   }
    2408          84 :   v = solvestep((void*)&S, eval, m, M, gen_2, 14, nbits2prec(bitprec));
    2409          84 :   return gerepilecopy(ltop, checkconductor(v, bitprec/2, flag));
    2410             : }
    2411             : 
    2412             : /* assume chi primitive */
    2413             : static GEN
    2414         553 : znchargauss_i(GEN G, GEN chi, long bitprec)
    2415             : {
    2416         553 :   GEN z, q, F = znstar_get_N(G);
    2417         553 :   long prec = nbits2prec(bitprec);
    2418             : 
    2419         553 :   q = sqrtr_abs(itor(F, prec));
    2420         553 :   z = lfuntheta(mkvec2(G,chi), gen_1, 0, bitprec);
    2421         553 :   if (gexpo(z) < 10 - bitprec)
    2422             :   {
    2423          28 :     if (equaliu(F,300))
    2424             :     {
    2425          14 :       GEN z = rootsof1u_cx(25, prec);
    2426          14 :       GEN n = znconreyexp(G, chi);
    2427          14 :       if (equaliu(n, 131)) return gmul(q, gpowgs(z,14));
    2428           7 :       if (equaliu(n, 71)) return gmul(q, gpowgs(z,11));
    2429             :     }
    2430          14 :     if (equaliu(F,600))
    2431             :     {
    2432          14 :       GEN z = rootsof1u_cx(25, prec);
    2433          14 :       GEN n = znconreyexp(G, chi);
    2434          14 :       if (equaliu(n, 491)) return gmul(q, gpowgs(z,7));
    2435           7 :       if (equaliu(n, 11)) return gmul(q, gpowgs(z,18));
    2436             :     }
    2437           0 :     pari_err_BUG("znchargauss [ Theta(chi,1) = 0 ]");
    2438             :   }
    2439         525 :   z = gmul(gdiv(z, gconj(z)), q);
    2440         525 :   if (zncharisodd(G,chi)) z = mulcxI(z);
    2441         525 :   return z;
    2442             : }
    2443             : GEN
    2444         553 : znchargauss(GEN G, GEN chi, GEN a, long bitprec)
    2445             : {
    2446             :   GEN T, N, F, GF, tau;
    2447         553 :   long prec = nbits2prec(bitprec);
    2448         553 :   pari_sp av = avma;
    2449             : 
    2450         553 :   if (typ(chi) != t_COL) chi = znconreylog(G,chi);
    2451         553 :   if (!a) a = gen_1;
    2452         497 :   else if (typ(a) != t_INT) pari_err_TYPE("znchargauss",a);
    2453         553 :   T = znchartoprimitive(G, chi);
    2454         553 :   GF = gel(T,1);
    2455         553 :   chi = gel(T,2); /* now primitive */
    2456         553 :   N = znstar_get_N(G);
    2457         553 :   F = znstar_get_N(GF);
    2458         553 :   tau = znchargauss_i(GF, chi, bitprec);
    2459         553 :   if (!equalii(N,F))
    2460             :   {
    2461          28 :     GEN NF = diviiexact(N,F), D = divisors(gcdii(NF, a));
    2462          28 :     GEN ord = zncharorder(GF, chi);
    2463          28 :     GEN z = mkvec2(rootsof1_cx(ord, prec), ord), S = gen_0;
    2464          28 :     long i, l = lg(D);
    2465          63 :     for (i = 1; i < l; i++)
    2466             :     {
    2467          35 :       GEN d = gel(D,i), ad, t, u, v;
    2468             :       long m;
    2469          35 :       t = diviiexact(NF, d);
    2470          35 :       if (!is_pm1(gcdii(t, F))) continue;
    2471          21 :       m = moebius(t);
    2472          21 :       if (!m) continue;
    2473          14 :       ad = diviiexact(a,d);
    2474          14 :       if (!is_pm1(gcdii(ad, F))) continue;
    2475             : 
    2476          14 :       u = znchareval(GF, chi, ad, z);
    2477          14 :       v = znchareval(GF, chi, t, z);
    2478          14 :       t = gmul(d, gmul(u, gconj(v)));
    2479          14 :       S = (m < 0)? gsub(S, t): gadd(S, t);
    2480             :     }
    2481          28 :     tau = gmul(tau, S);
    2482             :   }
    2483         553 :   return gerepilecopy(av, tau);
    2484             : }

Generated by: LCOV version 1.11