Code coverage tests

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

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

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

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

Generated by: LCOV version 1.13