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

Generated by: LCOV version 1.11