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 23339-b1c33c51a) Lines: 1372 1431 95.9 %
Date: 2018-12-11 05:41:34 Functions: 141 142 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       11749 : mysercoeff(GEN x, long n)
      29             : {
      30       11749 :   long N = n - valp(x);
      31       11749 :   return (N < 0)? gen_0: gel(x, N+2);
      32             : }
      33             : 
      34             : long
      35        5537 : ldata_get_type(GEN ldata) { return mael3(ldata, 1, 1, 1); }
      36             : 
      37             : GEN
      38       13860 : ldata_get_an(GEN ldata) { return gel(ldata, 1); }
      39             : 
      40             : GEN
      41       28677 : 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      169536 : 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       89774 : ldata_get_k(GEN ldata)
      54             : {
      55       89774 :   GEN w = gel(ldata,4);
      56       89774 :   if (typ(w) == t_VEC) w = gel(w,1);
      57       89774 :   return itos(w);
      58             : }
      59             : /* a_n = O(n^{k1 + epsilon}) */
      60             : static double
      61       54179 : ldata_get_k1(GEN ldata)
      62             : {
      63       54179 :   GEN w = gel(ldata,4);
      64             :   long k;
      65       54179 :   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       53850 :   k = itos(w);
      68       53850 :   return ldata_get_residue(ldata)? k-1: (k-1)/2.;
      69             : }
      70             : 
      71             : GEN
      72      122306 : ldata_get_conductor(GEN ldata) { return gel(ldata, 5); }
      73             : 
      74             : GEN
      75       44579 : ldata_get_rootno(GEN ldata) { return gel(ldata, 6); }
      76             : 
      77             : GEN
      78      103581 : ldata_get_residue(GEN ldata) { return lg(ldata) == 7 ? NULL: gel(ldata, 7); }
      79             : 
      80             : long
      81       72128 : linit_get_type(GEN linit) { return mael(linit, 1, 1); }
      82             : 
      83             : GEN
      84      109091 : linit_get_ldata(GEN linit) { return gel(linit, 2); }
      85             : 
      86             : GEN
      87      131523 : linit_get_tech(GEN linit) { return gel(linit, 3); }
      88             : 
      89             : long
      90      109725 : is_linit(GEN data)
      91             : {
      92      197655 :   return lg(data) == 4 && typ(data) == t_VEC
      93      197652 :                        && typ(gel(data, 1)) == t_VECSMALL;
      94             : }
      95             : 
      96             : GEN
      97       18084 : lfun_get_step(GEN tech) { return gmael(tech, 2, 1);}
      98             : 
      99             : GEN
     100       18084 : lfun_get_pol(GEN tech) { return gmael(tech, 2, 2);}
     101             : 
     102             : GEN
     103        4212 : lfun_get_Residue(GEN tech) { return gmael(tech, 2, 3);}
     104             : 
     105             : GEN
     106       28498 : 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        3969 : lfun_get_factgammavec(GEN tech) { return gmael(tech, 3, 4); }
     116             : 
     117             : /* Handle complex Vga whose sum is real */
     118             : static GEN
     119       59289 : sumVga(GEN Vga) { return real_i(vecsum(Vga)); }
     120             : /* sum_i max (Im v[i],0) */
     121             : static double
     122       14859 : sumVgaimpos(GEN v)
     123             : {
     124       14859 :   double d = 0.;
     125       14859 :   long i, l = lg(v);
     126       38750 :   for (i = 1; i < l; i++)
     127             :   {
     128       23891 :     GEN c = imag_i(gel(v,i));
     129       23891 :     if (gsigne(c) > 0) d += gtodouble(c);
     130             :   }
     131       14859 :   return d;
     132             : }
     133             : 
     134             : static long
     135       13902 : vgaell(GEN Vga)
     136             : {
     137             :   GEN c;
     138       13902 :   long d = lg(Vga)-1;
     139       13902 :   if (d != 2) return 0;
     140        9006 :   c = gsub(gel(Vga,1), gel(Vga,2));
     141        9006 :   return gequal1(c) || gequalm1(c);
     142             : }
     143             : static long
     144       39327 : 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        9912 : antwist(GEN an, GEN Vga, long prec)
     148             : {
     149             :   long l, i;
     150        9912 :   GEN b, c = vecmin(Vga);
     151        9912 :   if (gequal0(c)) return an;
     152        1043 :   l = lg(an); b = cgetg(l, t_VEC);
     153        1043 :   if (gequal1(c))
     154             :   {
     155         672 :     if (typ(an) == t_VECSMALL)
     156         231 :       for (i = 1; i < l; i++) gel(b,i) = mulss(an[i], i);
     157             :     else
     158         441 :       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        1043 :   return b;
     169             : }
     170             : 
     171             : static GEN
     172        6335 : theta_dual(GEN theta, GEN bn)
     173             : {
     174        6335 :   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       31452 : 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       18527 : domain_get_bitprec(GEN domain)  { return mael2(domain, 2, 2); }
     195             : GEN
     196       31865 : 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       32537 : theta_get_an(GEN tdata)        { return gel(tdata, 1);}
     207             : GEN
     208        5358 : theta_get_K(GEN tdata)         { return gel(tdata, 2);}
     209             : GEN
     210        2663 : theta_get_R(GEN tdata)         { return gel(tdata, 3);}
     211             : long
     212       43577 : theta_get_bitprec(GEN tdata)   { return itos(gel(tdata, 4));}
     213             : long
     214       64455 : theta_get_m(GEN tdata)         { return itos(gel(tdata, 5));}
     215             : GEN
     216       34805 : theta_get_tdom(GEN tdata)      { return gel(tdata, 6);}
     217             : GEN
     218       36835 : 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       15302 : isnegint(GEN s)
     228             : {
     229       15302 :   GEN r = ground(real_i(s));
     230       15302 :   if (signe(r) <= 0 && gequal(s, r)) return -itos(r);
     231       15260 :   return -1;
     232             : }
     233             : 
     234             : /* pi^(-s/2) Gamma(s/2) */
     235             : static GEN
     236       10843 : gamma_R(GEN s, long prec)
     237             : {
     238       10843 :   GEN s2 = gdivgs(s, 2), pi = mppi(prec);
     239       10843 :   long ms = isnegint(s2);
     240       10843 :   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       10801 :   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        4459 : gamma_C(GEN s, long prec)
     252             : {
     253        4459 :   GEN pi2 = Pi2n(1,prec);
     254        4459 :   long ms = isnegint(s);
     255        4459 :   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        4459 :   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       15533 : gammafactor(GEN Vga)
     282             : {
     283       15533 :   pari_sp av = avma;
     284       15533 :   long i, m, d = lg(Vga)-1, dr, dc;
     285       15533 :   GEN pol = pol_1(0), pi = gen_0, R = cgetg(d+1,t_VEC);
     286             :   GEN P, F, FR, FC, E, ER, EC;
     287       39746 :   for (i = 1; i <= d; ++i)
     288             :   {
     289       24213 :     GEN a = gel(Vga,i), qr = gdiventres(real_i(a), gen_2);
     290       24213 :     long q = itos(gel(qr,1));
     291       24213 :     gel(R, i) = gadd(gel(qr,2), imag_i(a));
     292       24213 :     if (q)
     293             :     {
     294        1099 :       pol = gmul(pol, gammafrac(gel(R,i), q));
     295        1099 :       pi  = addis(pi, q);
     296             :     }
     297             :   }
     298       15533 :   gen_sort_inplace(R, (void*)cmp_universal, cmp_nodata, &P);
     299       15533 :   F = cgetg(d+1, t_VEC); E = cgetg(d+1, t_VECSMALL);
     300       51891 :   for (i = 1, m = 0; i <= d;)
     301             :   {
     302             :     long k;
     303       20825 :     GEN u = gel(R, i);
     304       24213 :     for(k = i + 1; k <= d; ++k)
     305        8680 :       if (cmp_universal(gel(R, k), u)) break;
     306       20825 :     m++;
     307       20825 :     E[m] = k - i;
     308       20825 :     gel(F, m) = u;
     309       20825 :     i = k;
     310             :   }
     311       15533 :   setlg(F, m+1); setlg(E, m+1);
     312       15533 :   R = cgetg(m+1, t_VEC);
     313       36358 :   for (i = 1; i <= m; i++)
     314             :   {
     315       20825 :     GEN qr = gdiventres(gel(F,i), gen_1);
     316       20825 :     gel(R, i) = mkvec2(gel(qr,2), stoi(E[i]));
     317             :   }
     318       15533 :   gen_sort_inplace(R, (void*)cmp_universal, cmp_nodata, &P);
     319       15533 :   FR = cgetg(m+1, t_VEC); ER = cgetg(m+1, t_VECSMALL);
     320       15533 :   FC = cgetg(m+1, t_VEC); EC = cgetg(m+1, t_VECSMALL);
     321       47320 :   for (i = 1, dr = 1, dc = 1; i <= m;)
     322             :   {
     323       16254 :     if (i==m || cmp_universal(gel(R,i), gel(R,i+1)))
     324             :     {
     325       11683 :       gel(FR, dr) = gel(F, P[i]);
     326       11683 :       ER[dr] = E[P[i]];
     327       11683 :       dr++; i++;
     328             :     } else
     329             :     {
     330        4571 :       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        4571 :         gel(FC, dc) = gel(F, P[i]);
     334        4571 :       EC[dc] = E[P[i]];
     335        4571 :       dc++; i+=2;
     336             :     }
     337             :   }
     338       15533 :   setlg(FR, dr); setlg(ER, dr);
     339       15533 :   setlg(FC, dc); setlg(EC, dc);
     340       15533 :   return gerepilecopy(av, mkvec4(pol, pi, mkvec2(FR,ER), mkvec2(FC,EC)));
     341             : }
     342             : 
     343             : static GEN
     344       13559 : deg1ser_shallow(GEN a1, GEN a0, long v, long e)
     345             : {
     346       13559 :   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       15729 : polgammaeval(GEN F, GEN s)
     358             : {
     359       15729 :   GEN r = poleval(F, s);
     360       15729 :   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       15729 :   return r;
     366             : }
     367             : 
     368             : static GEN
     369       14539 : fracgammaeval(GEN F, GEN s)
     370             : {
     371       14539 :   if (typ(F)==t_POL)
     372       13349 :     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       14539 : gammafactproduct(GEN F, GEN s, long prec)
     380             : {
     381       14539 :   pari_sp av = avma;
     382       14539 :   GEN P = fracgammaeval(gel(F,1), s);
     383       14539 :   GEN p = gpow(mppi(prec),gneg(gel(F,2)), prec), z = gmul(P, p);
     384       14539 :   GEN R = gel(F,3), Rw = gel(R,1), Re=gel(R,2);
     385       14539 :   GEN C = gel(F,4), Cw = gel(C,1), Ce=gel(C,2);
     386       14539 :   long i, lR = lg(Rw), lC = lg(Cw);
     387       25382 :   for (i=1; i< lR; i++)
     388       10843 :     z = gmul(z, gpowgs(gamma_R(gadd(s,gel(Rw, i)), prec), Re[i]));
     389       18998 :   for (i=1; i< lC; i++)
     390        4459 :     z = gmul(z, gpowgs(gamma_C(gadd(s,gel(Cw, i)), prec), Ce[i]));
     391       14539 :   return gerepileupto(av, z);
     392             : }
     393             : 
     394             : static int
     395        4032 : gammaordinary(GEN Vga, GEN s)
     396             : {
     397        4032 :   long i, d = lg(Vga)-1;
     398       10759 :   for (i = 1; i <= d; i++)
     399             :   {
     400        6811 :     GEN z = gadd(s, gel(Vga,i));
     401             :     long e;
     402        6811 :     if (gsigne(z) <= 0) { (void)grndtoi(z, &e); if (e < -10) return 0; }
     403             :   }
     404        3948 :   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       54172 : 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       85549 : get_cone(GEN t, double *r, double *a)
     418             : {
     419       85549 :   const long prec = LOWDEFAULTPREC;
     420       85549 :   if (typ(t) == t_COMPLEX)
     421             :   {
     422       15162 :     t  = gprec_w(t, prec);
     423       15162 :     *r = gtodouble(gabs(t, prec));
     424       15162 :     *a = fabs(gtodouble(garg(t, prec)));
     425             :   }
     426             :   else
     427             :   {
     428       70387 :     *r = fabs(gtodouble(t));
     429       70387 :     *a = 0.;
     430             :   }
     431       85549 :   if (!*r && !*a) pari_err_DOMAIN("lfunthetainit","t","=",gen_0,t);
     432       85542 : }
     433             : /* slightly larger cone than necessary, to avoid round-off problems */
     434             : static void
     435       50744 : get_cone_fuzz(GEN t, double *r, double *a)
     436       50744 : { 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       39320 : lfunthetacost(GEN ldata, GEN tdom, long m, long bitprec)
     446             : {
     447       39320 :   pari_sp av = avma;
     448       39320 :   GEN Vga = ldata_get_gammavec(ldata);
     449       39320 :   long d = lg(Vga)-1;
     450       39320 :   long k1 = ldata_get_k1(ldata);
     451       39320 :   double c = d/2., a, A, B, logC, al, rho, T;
     452       39320 :   double N = gtodouble(ldata_get_conductor(ldata));
     453             : 
     454       39320 :   if (!N) pari_err_TYPE("lfunthetaneed [missing conductor]", ldata);
     455       39320 :   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       39313 :     get_cone_fuzz(tdom, &rho, &al);
     462       39313 :   A = gammavec_expo(d, gtodouble(sumVga(Vga))); set_avma(av);
     463       39313 :   a = (A+k1+1) + (m-1)/c;
     464       39313 :   if (fabs(a) < 1e-10) a = 0.;
     465       39313 :   logC = c*M_LN2 - log(c)/2;
     466             :   /* +1: fudge factor */
     467       39313 :   B = M_LN2*bitprec+logC+m*log(2*M_PI) + 1 + (k1+1)*log(N)/2 - (k1+m+1)*log(rho);
     468       39313 :   if (al)
     469             :   { /* t = rho e^(i*al), T^(1/c) = Re(t^(1/c)) > 0, T = rho cos^c(al/c) */
     470        7581 :     double z = cos(al/c);
     471        7581 :     T = (d == 2 && typ(tdom) != t_VEC)? gtodouble(real_i(tdom)): rho*pow(z,c);
     472        7581 :     if (z <= 0)
     473           0 :       pari_err_DOMAIN("lfunthetaneed", "arg t", ">", dbltor(c*M_PI/2), tdom);
     474        7581 :     B -= log(z) * (c * (k1+A+1) + m);
     475             :   }
     476             :   else
     477       31732 :     T = rho;
     478       39313 :   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        7896 : fracgammadegree(GEN FVga)
     501        7896 : { 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        6566 : rtoR(GEN a, GEN r, GEN FVga, GEN N, long prec)
     514             : {
     515        6566 :   long v = lg(r)-2;
     516        6566 :   GEN as = deg1ser_shallow(gen_1, a, varn(r), v);
     517        6566 :   GEN Na = gpow(N, gdivgs(as, 2), prec);
     518        6566 :   long d = fracgammadegree(FVga);
     519        6566 :   if (d) as = sertoser(as, v+d); /* make up for a possible loss of accuracy */
     520        6566 :   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        6517 : lfunrtopoles(GEN r)
     526             : {
     527        6517 :   long j, l = lg(r);
     528        6517 :   GEN v = cgetg(l, t_VEC);
     529       13188 :   for (j = 1; j < l; j++)
     530             :   {
     531        6671 :     GEN rj = gel(r,j), a = gel(rj,1);
     532        6671 :     gel(v,j) = a;
     533             :   }
     534        6517 :   gen_sort_inplace(v, (void*)&cmp_universal, cmp_nodata, NULL);
     535        6517 :   return v;
     536             : }
     537             : 
     538             : /* r / x + O(1) */
     539             : static GEN
     540        5369 : simple_pole(GEN r)
     541             : {
     542             :   GEN S;
     543        5369 :   if (isintzero(r)) return gen_0;
     544        5362 :   S = deg1ser_shallow(gen_0, r, 0, 1);
     545        5362 :   setvalp(S, -1); return S;
     546             : }
     547             : static GEN
     548        5747 : normalize_simple_pole(GEN r, GEN k)
     549             : {
     550        5747 :   long tx = typ(r);
     551        5747 :   if (is_vec_t(tx)) return r;
     552        5369 :   if (!is_scalar_t(tx)) pari_err_TYPE("lfunrootres [poles]", r);
     553        5369 :   return mkvec(mkvec2(k, simple_pole(r)));
     554             : }
     555             : /* normalize the description of a polar part */
     556             : static GEN
     557        7308 : normalizepoles(GEN r, long k)
     558             : {
     559             :   long iv, j, l;
     560             :   GEN v;
     561        7308 :   if (!is_vec_t(typ(r))) return normalize_simple_pole(r, stoi(k));
     562        3010 :   v = cgetg_copy(r, &l);
     563        7070 :   for (j = iv = 1; j < l; j++)
     564             :   {
     565        4060 :     GEN rj = gel(r,j), a = gel(rj,1), ra = gel(rj,2);
     566        4060 :     if (!is_scalar_t(typ(a)) || typ(ra) != t_SER)
     567           0 :       pari_err_TYPE("lfunrootres [poles]",r);
     568        4060 :     if (valp(ra) >= 0) continue;
     569        4060 :     gel(v,iv++) = rj;
     570             :   }
     571        3010 :   setlg(v, iv); return v;
     572             : }
     573             : static int
     574        8925 : residues_known(GEN r)
     575             : {
     576        8925 :   long i, l = lg(r);
     577        8925 :   if (isintzero(r)) return 0;
     578        8799 :   if (!is_vec_t(typ(r))) return 1;
     579       10094 :   for (i = 1; i < l; i++)
     580             :   {
     581        5747 :     GEN ri = gel(r,i);
     582        5747 :     if (!is_vec_t(typ(ri)) || lg(ri)!=3)
     583           0 :       pari_err_TYPE("lfunrootres [poles]",r);
     584        5747 :     if (isintzero(gel(ri, 2))) return 0;
     585             :   }
     586        4347 :   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       16618 : lfunrtoR_i(GEN ldata, GEN r, GEN eno, long prec)
     593             : {
     594       16618 :   GEN Vga = ldata_get_gammavec(ldata), N = ldata_get_conductor(ldata);
     595             :   GEN R, vr, FVga;
     596       16618 :   pari_sp av = avma;
     597       16618 :   long lr, j, jR, k = ldata_get_k(ldata);
     598             : 
     599       16618 :   if (!r || isintzero(eno) || !residues_known(r))
     600        9310 :     return gen_0;
     601        7308 :   r = normalizepoles(r, k);
     602        7308 :   if (typ(r) == t_COL) return gerepilecopy(av, r);
     603        6412 :   if (typ(ldata_get_dual(ldata)) != t_INT)
     604           0 :     pari_err(e_MISC,"please give the Taylor development of Lambda");
     605        6412 :   vr = lfunrtopoles(r); lr = lg(vr);
     606        6412 :   FVga = gammafactor(Vga);
     607        6412 :   R = cgetg(2*lr, t_VEC);
     608       12978 :   for (j = jR = 1; j < lr; j++)
     609             :   {
     610        6566 :     GEN rj = gel(r,j), a = gel(rj,1), ra = gel(rj,2);
     611        6566 :     GEN Ra = rtoR(a, ra, FVga, N, prec);
     612        6566 :     GEN b = gsubsg(k, conj_i(a));
     613        6566 :     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        6566 :     gel(R,jR++) = mkvec2(a, Ra);
     617        6566 :     if (!tablesearch(vr, b, (int (*)(GEN,GEN))&cmp_universal))
     618             :     {
     619        6489 :       GEN mX = gneg(pol_x(varn(Ra)));
     620        6489 :       GEN Rb = gmul(eno, gsubst(conj_i(Ra), varn(Ra), mX));
     621        6489 :       gel(R,jR++) = mkvec2(b, Rb);
     622             :     }
     623             :   }
     624        6412 :   setlg(R, jR); return gerepilecopy(av, R);
     625             : }
     626             : static GEN
     627       16541 : lfunrtoR_eno(GEN ldata, GEN eno, long prec)
     628       16541 : { return lfunrtoR_i(ldata, ldata_get_residue(ldata), eno, prec); }
     629             : static GEN
     630       13867 : lfunrtoR(GEN ldata, long prec)
     631       13867 : { 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       11438 : lfunthetainit0(GEN ldata, GEN tdom, GEN an2, long m,
     636             :     long bitprec, long extrabit)
     637             : {
     638       11438 :   long prec = nbits2prec(bitprec);
     639       11438 :   GEN tech, N = ldata_get_conductor(ldata);
     640       11438 :   GEN Vga = ldata_get_gammavec(ldata);
     641       11438 :   GEN K = gammamellininvinit(Vga, m, bitprec + extrabit);
     642       11438 :   GEN R = lfunrtoR(ldata, prec);
     643       11438 :   if (!tdom) tdom = gen_1;
     644       11438 :   if (typ(tdom) != t_VEC)
     645             :   {
     646             :     double r, a;
     647       11431 :     get_cone_fuzz(tdom, &r, &a);
     648       11431 :     tdom = mkvec2(dbltor(r), a? dbltor(a): gen_0);
     649             :   }
     650       11438 :   tech = mkvecn(7, an2,K,R, stoi(bitprec), stoi(m), tdom, gsqrt(N,prec));
     651       11438 :   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        7014 : lfunthetainit_i(GEN data, GEN tdom, long m, long bitprec)
     658             : {
     659        7014 :   GEN ldata = lfunmisc_to_ldata_shallow(data);
     660        7014 :   long L = lfunthetacost(ldata, tdom, m, bitprec), prec = nbits2prec(bitprec);
     661        7007 :   GEN an = ldata_vecan(ldata_get_an(ldata), L, prec);
     662        7007 :   GEN Vga = ldata_get_gammavec(ldata);
     663        7007 :   if (m == 0 && vgaeasytheta(Vga)) an = antwist(an, Vga, prec);
     664        7007 :   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        8433 : vecpowug(long N, GEN B, long prec)
     729             : {
     730        8433 :   GEN v = const_vec(N, NULL);
     731        8433 :   long p, eB = gexpo(B);
     732        8433 :   long prec0 = eB < 5? prec: prec + nbits2extraprec(eB);
     733             :   forprime_t T;
     734        8433 :   u_forprime_init(&T, 2, N);
     735        8433 :   gel(v,1) = gen_1;
     736      341296 :   while ((p = u_forprime_next(&T)))
     737             :   {
     738             :     long m, pk, oldpk;
     739      324430 :     gel(v,p) = gpow(utor(p,prec0), B, prec);
     740      324430 :     if (prec0 != prec) gel(v,p) = gprec_wtrunc(gel(v,p), prec);
     741      718992 :     for (pk = p, oldpk = p; pk; oldpk = pk, pk = umuluu_le(pk,p,N))
     742             :     {
     743      394562 :       if (pk != p) gel(v,pk) = gmul(gel(v,oldpk), gel(v,p));
     744     5987746 :       for (m = N/pk; m > 1; m--)
     745     5593184 :         if (gel(v,m) && m%p) gel(v, m*pk) = gmul(gel(v,m), gel(v,pk));
     746             :     }
     747             :   }
     748        8433 :   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        5358 : mkvroots(long d, long lim, long prec)
     770             : {
     771        5358 :   if (d <= 4)
     772             :   {
     773        5204 :     GEN v = cgetg(lim+1,t_VEC);
     774             :     long n;
     775        5204 :     switch(d)
     776             :     {
     777             :       case 1:
     778        1862 :         for (n=1; n <= lim; n++) gel(v,n) = sqru(n);
     779        1862 :         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       38711 : lfunthetacheckinit(GEN data, GEN t, long m, long bitprec)
     793             : {
     794       38711 :   if (is_linit(data) && linit_get_type(data)==t_LDESC_THETA)
     795             :   {
     796       34805 :     GEN tdom, thetainit = linit_get_tech(data);
     797       34805 :     long bitprecnew = theta_get_bitprec(thetainit);
     798       34805 :     long m0 = theta_get_m(thetainit);
     799             :     double r, al, rt, alt;
     800       34805 :     if (m0 != m)
     801           0 :       pari_err_DOMAIN("lfuntheta","derivative order","!=", stoi(m),stoi(m0));
     802       34805 :     if (bitprec > bitprecnew) goto INIT;
     803       34805 :     get_cone(t, &rt, &alt);
     804       34805 :     tdom = theta_get_tdom(thetainit);
     805       34805 :     r = rtodbl(gel(tdom,1));
     806       34805 :     al= rtodbl(gel(tdom,2)); if (rt >= r && alt <= al) return data;
     807             :   }
     808             : INIT:
     809        6664 :   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    11279188 : mul_an(GEN an, long n, GEN x)
     822             : {
     823    11279188 :   if (typ(an) == t_VECSMALL) { long a = an[n]; if (a) return gmulsg(a,x); }
     824     8476343 :   else { GEN a = gel(an,n); if (a && !gequal0(a)) return gmul(a,x); }
     825     5688089 :   return NULL;
     826             : }
     827             : /* 2*t^a * x **/
     828             : static GEN
     829      140530 : mulT(GEN t, GEN a, GEN x, long prec)
     830             : {
     831      140530 :   if (gequal0(a)) return gmul2n(x,1);
     832       10566 :   return gmul(x, gmul2n(gequal1(a)? t: gpow(t,a,prec), 1));
     833             : }
     834             : 
     835             : static GEN
     836    23339281 : vecan_cmul(void *E, GEN P, long a, GEN x)
     837             : {
     838             :   (void)E;
     839    23339281 :   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      115175 : theta2(GEN an2, long limt, GEN t, GEN al, long prec)
     845             : {
     846      115175 :   GEN S, q, pi2 = Pi2n(1,prec);
     847      115175 :   const struct bb_algebra *alg = get_Rg_algebra();
     848      115175 :   setsigne(pi2,-1); q = gexp(gmul(pi2, t), prec);
     849             :   /* Brent-Kung in case the a_n are small integers */
     850      115175 :   S = gen_bkeval(an2, limt, q, 1, NULL, alg, vecan_cmul);
     851      115175 :   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       25355 : theta1(GEN an2, long limt, GEN t, GEN al, long prec)
     858             : {
     859       25355 :   GEN q = gexp(gmul(negr(mppi(prec)), gsqr(t)), prec);
     860       25355 :   GEN vexp = gsqrpowers(q, limt), S = gen_0;
     861       25355 :   pari_sp av = avma;
     862             :   long n;
     863     4097056 :   for (n = 1; n <= limt; n++)
     864             :   {
     865     4071701 :     GEN c = mul_an(an2, n, gel(vexp,n));
     866     4071701 :     if (c)
     867             :     {
     868     3054845 :       S = gadd(S, c);
     869     3054845 :       if (gc_needed(av, 3)) S = gerepileupto(av, S);
     870             :     }
     871             :   }
     872       25355 :   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       32411 : lfuntheta(GEN data, GEN t, long m, long bitprec)
     879             : {
     880       32411 :   pari_sp ltop = avma;
     881             :   long limt, d;
     882             :   GEN sqN, vecan, Vga, ldata, theta, thetainit, S;
     883       32411 :   long n, prec = nbits2prec(bitprec);
     884       32411 :   t = gprec_w(t, prec);
     885       32411 :   theta = lfunthetacheckinit(data, t, m, bitprec);
     886       32404 :   ldata = linit_get_ldata(theta);
     887       32404 :   thetainit = linit_get_tech(theta);
     888       32404 :   vecan = theta_get_an(thetainit);
     889       32404 :   sqN = theta_get_sqrtN(thetainit);
     890       32404 :   limt = lg(vecan)-1;
     891       32404 :   if (theta == data)
     892       30969 :     limt = minss(limt, lfunthetacost(ldata, t, m, bitprec));
     893       32404 :   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       32390 :   t = gdiv(t, sqN);
     901       32390 :   Vga = ldata_get_gammavec(ldata);
     902       32390 :   d = lg(Vga)-1;
     903       32390 :   if (m == 0 && vgaeasytheta(Vga))
     904             :   {
     905       29545 :     if (theta_get_m(thetainit) > 0) vecan = antwist(vecan, Vga, prec);
     906       59090 :     if (d == 1) S = theta1(vecan, limt, t, gel(Vga,1), prec);
     907        4190 :     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       32390 :   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       14859 : lfunparams(GEN ldata, long der, long bitprec, struct lfunp *S)
     942             : {
     943       14859 :   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       14859 :   Vga = ldata_get_gammavec(ldata);
     949       14859 :   S->d = d = lg(Vga)-1; d2 = d/2.;
     950             : 
     951       14859 :   suma = gtodouble(sumVga(Vga));
     952       14859 :   k = ldata_get_k(ldata);
     953       14859 :   N = ldata_get_conductor(ldata);
     954       14859 :   S->logN2 = log(gtodouble(N)) / 2;
     955       14859 :   maxs = S->dc + S->dw;
     956       14859 :   mins = S->dc - S->dw;
     957       14859 :   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       14859 :   a = (M_PI/(4*M_LN2))*(d*S->dh + sumVgaimpos(Vga));
     962       14859 :   S->D = (long)ceil(bitprec + derprec + maxdd(a, 1));
     963       14859 :   S->E = E = M_LN2*S->D; /* D:= required absolute bitprec */
     964             : 
     965       14859 :   Ep = E + maxdd(M_PI * S->dh * d2, (d*S->MAXs + suma - 1) * log(E));
     966       14859 :   hd = d2*M_PI*M_PI / Ep;
     967       14859 :   S->m0 = (long)ceil(M_LN2/hd);
     968       14859 :   S->hd = M_LN2/S->m0;
     969             : 
     970       14859 :   S->logC = d2*M_LN2 - log(d2)/2;
     971       14859 :   k1 = ldata_get_k1(ldata);
     972       14859 :   S->k1 = k1; /* assume |a_n| << n^k1 with small implied constant */
     973       14859 :   S->A  = gammavec_expo(d, suma);
     974             : 
     975       14859 :   sub = 0.;
     976       14859 :   if (mins > 1)
     977             :   {
     978        4032 :     GEN sig = dbltor(mins);
     979        4032 :     sub += S->logN2*mins;
     980        4032 :     if (gammaordinary(Vga, sig))
     981             :     {
     982        3948 :       GEN FVga = gammafactor(Vga);
     983        3948 :       GEN gas = gammafactproduct(FVga, sig, LOWDEFAULTPREC);
     984        3948 :       if (typ(gas) != t_SER)
     985             :       {
     986        3948 :         double dg = dbllog2(gas);
     987        3948 :         if (dg > 0) sub += dg * M_LN2;
     988             :       }
     989             :     }
     990             :   }
     991       14859 :   S->sub = sub;
     992       14859 :   M = 1000;
     993       14859 :   L = cgetg(M+2, t_VECSMALL);
     994       14859 :   a = S->k1 + S->A;
     995             : 
     996       14859 :   B0 = 5 + S->E - S->sub + S->logC + S->k1*S->logN2; /* 5 extra bits */
     997       14859 :   B1 = S->hd * (S->MAXs - S->k1);
     998       14859 :   Lestimate = dblcoro526(a + S->MAXs - 2./d, d/2.,
     999       14859 :     S->E - S->sub + S->logC - log(2*M_PI*S->hd) + S->MAXs*S->logN2);
    1000       14859 :   Mestimate = ((Lestimate > 0? log(Lestimate): 0) + S->logN2) / S->hd;
    1001       14859 :   nmax = 0;
    1002       14859 :   flag = 0;
    1003     1464085 :   for (m = 0;; m++)
    1004     1449226 :   {
    1005     1464085 :     double x, H = S->logN2 - m*S->hd, B = B0 + m*B1;
    1006             :     long n;
    1007     1464085 :     x = dblcoro526(a, d/2., B);
    1008     1464085 :     n = floor(x*exp(H));
    1009     1464085 :     if (n > nmax) nmax = n;
    1010     1464085 :     if (m > M) { M *= 2; L = vecsmall_lengthen(L,M+2); }
    1011     1464085 :     L[m+1] = n;
    1012     1464085 :     if (n == 0) { if (++flag > 2 && m > Mestimate) break; } else flag = 0;
    1013             :   }
    1014       14859 :   m -= 2; while (m > 0 && !L[m]) m--;
    1015       14859 :   if (m == 0) { nmax = 1; L[1] = 1; m = 1; } /* can happen for tiny bitprec */
    1016       14859 :   setlg(L, m+1); S->M = m-1;
    1017       14859 :   S->L = L;
    1018       14859 :   S->nmax = nmax;
    1019             : 
    1020       14859 :   S->Dmax = S->D + (long)ceil((S->M * S->hd * S->MAXs - S->sub) / M_LN2);
    1021       14859 :   if (S->Dmax < S->D) S->Dmax = S->D;
    1022       14859 :   S->precmax = nbits2prec(S->Dmax);
    1023       14859 :   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       14859 : }
    1027             : 
    1028             : /* x0 * [1,x,..., x^n] */
    1029             : static GEN
    1030        1939 : powersshift(GEN x, long n, GEN x0)
    1031             : {
    1032        1939 :   long i, l = n+2;
    1033        1939 :   GEN V = cgetg(l, t_VEC);
    1034        1939 :   gel(V,1) = x0;
    1035        1939 :   for(i = 2; i < l; i++) gel(V,i) = gmul(gel(V,i-1),x);
    1036        1939 :   return V;
    1037             : }
    1038             : 
    1039             : static GEN
    1040        4571 : lfuninit_pol(GEN vecc, GEN poqk, long M, long prec)
    1041             : {
    1042             :   long m;
    1043        4571 :   GEN pol = cgetg(M+3, t_POL); pol[1] = evalsigne(1) | evalvarn(0);
    1044        4571 :   gel(pol, 2) = gprec_w(gmul2n(gel(vecc,1), -1), prec);
    1045      277788 :   for (m = 2; m <= M+1; m++)
    1046      273217 :     gel(pol, m+1) = gprec_w(gmul(gel(poqk,m), gel(vecc,m)), prec);
    1047        4571 :   return RgX_renormalize_lg(pol, M+3);
    1048             : }
    1049             : 
    1050             : static GEN
    1051        2058 : lfuninit_vecc2_sum(GEN an, GEN qk, GEN a, struct lfunp *Q, GEN poqk)
    1052             : {
    1053        2058 :   const long M = Q->M, prec = Q->precmax;
    1054        2058 :   GEN L = Q->L;
    1055        2058 :   long m, L0 = lg(an)-1;
    1056        2058 :   GEN v = cgetg(M + 2, t_VEC);
    1057        2058 :   if (typ(an) == t_VEC) an = RgV_kill0(an);
    1058      113043 :   for (m = 0; m <= M; m++)
    1059             :   {
    1060      110985 :     pari_sp av = avma;
    1061      110985 :     GEN t = gel(qk, m+1), S = theta2(an, minss(L[m+1],L0), t, a, prec);
    1062      110985 :     gel(v, m+1) = gerepileupto(av, S); /* theta(exp(mh)) */
    1063             :   }
    1064        2058 :   return lfuninit_pol(v, poqk, M, prec);
    1065             : }
    1066             : 
    1067             : /* theta(exp(mh)) ~ sum_{n <= L[m]} a(n) k[m,n] */
    1068             : static GEN
    1069        2513 : lfuninit_vecc_sum(GEN L, long M, GEN vecan, GEN vK, GEN pokq, long prec)
    1070             : {
    1071             :   long m;
    1072        2513 :   GEN vecc = cgetg(M+2, t_VEC);
    1073      169316 :   for (m = 0; m <= M; ++m)
    1074             :   {
    1075      166803 :     pari_sp av = avma;
    1076      166803 :     GEN s = gen_0, vKm = gel(vK,m+1);
    1077             :     long n;
    1078     7374290 :     for (n = 1; n <= L[m+1]; n++)
    1079             :     {
    1080     7207487 :       GEN c = mul_an(vecan, n, gel(vKm,n));
    1081     7207487 :       if (c)
    1082             :       {
    1083     2536254 :         s = gadd(s, c);
    1084     2536254 :         if (gc_needed(av, 3)) s = gerepileupto(av, s);
    1085             :       }
    1086             :     }
    1087      166803 :     gel(vecc,m+1) = gerepileupto(av, s);
    1088             :   }
    1089        2513 :   return lfuninit_pol(vecc, pokq, M, prec);
    1090             : }
    1091             : 
    1092             : /* return [\theta(exp(mh)), m=0..M], theta(t) = sum a(n) K(n/sqrt(N) t),
    1093             :  * h = log(2)/m0 */
    1094             : static GEN
    1095        4431 : lfuninit_vecc(GEN theta, GEN h, struct lfunp *S, GEN poqk)
    1096             : {
    1097        4431 :   const long m0 = S->m0, M = S->M;
    1098        4431 :   GEN tech = linit_get_tech(theta);
    1099             :   GEN va, vK, L, K, d2, vroots, eh2d, peh2d;
    1100        4431 :   GEN sqN = theta_get_sqrtN(tech), an = S->an, bn = S->bn, vprec = S->vprec;
    1101             :   long d, prec, m, n, neval;
    1102             : 
    1103        4431 :   if (!vprec)
    1104             :   { /* d=2 and Vga = [a,a+1] */
    1105        1939 :     GEN ldata = linit_get_ldata(theta);
    1106        1939 :     GEN a = vecmin(ldata_get_gammavec(ldata));
    1107        1939 :     GEN qk = powersshift(mpexp(h), M, ginv(sqN));
    1108        1939 :     va = lfuninit_vecc2_sum(an, qk, a, S, poqk);
    1109        1939 :     return bn? mkvec2(va, lfuninit_vecc2_sum(bn, qk, a, S, poqk)): va;
    1110             :   }
    1111        2492 :   d = S->d;
    1112        2492 :   L = S->L;
    1113        2492 :   prec = S->precmax;
    1114        2492 :   K = theta_get_K(tech);
    1115             : 
    1116             :   /* For all 0<= m <= M, and all n <= L[m+1] such that a_n!=0, we must compute
    1117             :    *   k[m,n] = K(n exp(mh)/sqrt(N))
    1118             :    * with ln(absolute error) <= E + max(mh sigma - sub, 0) + k1 * log(n).
    1119             :    * N.B. we use the 'rt' variant and pass argument (n exp(mh)/sqrt(N))^(2/d).
    1120             :    * Speedup: if n' = 2n and m' = m - m0 >= 0; then k[m,n] = k[m',n']. */
    1121             :   /* vroots[n] = n^(2/d) */
    1122        2492 :   vroots = mkvroots(d, S->nmax, prec);
    1123        2492 :   d2 = gdivgs(gen_2, d);
    1124        2492 :   eh2d = gexp(gmul(d2,h), prec); /* exp(2h/d) */
    1125             :   /* peh2d[m+1] = (exp(mh)/sqrt(N))^(2/d) */
    1126        2492 :   peh2d = gpowers0(eh2d, M, invr(gpow(sqN, d2, prec)));
    1127        2492 :   neval = 0;
    1128             :   /* vK[m+1,n] will contain k[m,n]. For each 0 <= m <= M, sum for n<=L[m+1] */
    1129        2492 :   vK = cgetg(M+2, t_VEC);
    1130      168280 :   for (m = 0; m <= M; m++)
    1131      165788 :     gel(vK,m+1) = const_vec(L[m+1], NULL);
    1132             : 
    1133      168280 :   for (m = M; m >= 0; m--)
    1134     7369180 :     for (n = 1; n <= L[m+1]; n++)
    1135             :     {
    1136     7203392 :       GEN t2d, kmn = gmael(vK,m+1,n);
    1137     7203392 :       long nn, mm, p = 0;
    1138             : 
    1139     7203392 :       if (kmn) continue; /* done already */
    1140             :       /* p = largest (absolute) accuracy to which we need k[m,n] */
    1141    10867717 :       for (mm=m,nn=n; mm>=0 && nn <= L[mm+1]; nn<<=1,mm-=m0)
    1142     7203602 :         if (gel(an, nn) || (bn && gel(bn, nn)))
    1143     7198751 :           p = maxuu(p, umael(vprec,mm+1,nn));
    1144     3664115 :       if (!p) continue; /* a_{n 2^v} = 0 for all v in range */
    1145     3663114 :       t2d = mpmul(gel(vroots, n), gel(peh2d,m+1)); /*(n exp(mh)/sqrt(N))^(2/d)*/
    1146     3663114 :       neval++;
    1147     3663114 :       kmn = gammamellininvrt(K, t2d, p);
    1148    10865505 :       for (mm=m,nn=n; mm>=0 && nn <= L[mm+1]; nn<<=1,mm-=m0)
    1149     7202391 :         gmael(vK,mm+1,nn) = kmn;
    1150             :     }
    1151        2492 :   if (DEBUGLEVEL >= 1) err_printf("true evaluations: %ld\n", neval);
    1152        2492 :   va = lfuninit_vecc_sum(L, M, an, vK, poqk, S->precmax);
    1153        2492 :   return bn? mkvec2(va, lfuninit_vecc_sum(L, M, bn, vK, poqk, S->precmax)): va;
    1154             : }
    1155             : 
    1156             : static void
    1157       77707 : parse_dom(long k, GEN dom, struct lfunp *S)
    1158             : {
    1159       77707 :   long l = lg(dom);
    1160       77707 :   if (typ(dom)!=t_VEC) pari_err_TYPE("lfuninit [domain]", dom);
    1161       77707 :   if (l == 2)
    1162             :   {
    1163       41314 :     S->dc = k/2.;
    1164       41314 :     S->dw = 0.;
    1165       41314 :     S->dh = gtodouble(gel(dom,1));
    1166             :   }
    1167       36393 :   else if (l == 3)
    1168             :   {
    1169         301 :     S->dc = k/2.;
    1170         301 :     S->dw = gtodouble(gel(dom,1));
    1171         301 :     S->dh = gtodouble(gel(dom,2));
    1172             :   }
    1173       36092 :   else if (l == 4)
    1174             :   {
    1175       36092 :     S->dc = gtodouble(gel(dom,1));
    1176       36092 :     S->dw = gtodouble(gel(dom,2));
    1177       36092 :     S->dh = gtodouble(gel(dom,3));
    1178             :   }
    1179             :   else
    1180             :   {
    1181           0 :     pari_err_TYPE("lfuninit [domain]", dom);
    1182           0 :     S->dc = S->dw = S->dh = 0; /*-Wall*/
    1183             :   }
    1184       77707 :   if (S->dw < 0 || S->dh < 0) pari_err_TYPE("lfuninit [domain]", dom);
    1185       77707 : }
    1186             : 
    1187             : /* do we have dom \subset dom0 ? dom = [center, width, height] */
    1188             : int
    1189       13340 : sdomain_isincl(long k, GEN dom, GEN dom0)
    1190             : {
    1191             :   struct lfunp S0, S;
    1192       13340 :   parse_dom(k, dom, &S);
    1193       13340 :   parse_dom(k, dom0, &S0);
    1194       13340 :   return S0.dc - S0.dw <= S.dc - S.dw
    1195       13340 :       && S0.dc + S0.dw >= S.dc + S.dw && S0.dh >= S.dh;
    1196             : }
    1197             : 
    1198             : static int
    1199       13340 : checklfuninit(GEN linit, GEN dom, long der, long bitprec)
    1200             : {
    1201       13340 :   GEN ldata = linit_get_ldata(linit);
    1202       13340 :   GEN domain = lfun_get_domain(linit_get_tech(linit));
    1203       13340 :   return domain_get_der(domain) >= der
    1204       13340 :     && domain_get_bitprec(domain) >= bitprec
    1205       26680 :     && sdomain_isincl(ldata_get_k(ldata), dom, domain_get_dom(domain));
    1206             : }
    1207             : 
    1208             : GEN
    1209        5117 : lfuninit_make(long t, GEN ldata, GEN molin, GEN domain)
    1210             : {
    1211        5117 :   GEN Vga = ldata_get_gammavec(ldata);
    1212        5117 :   long d = lg(Vga)-1;
    1213        5117 :   long k = ldata_get_k(ldata);
    1214        5117 :   GEN k2 = gdivgs(stoi(k), 2);
    1215        5117 :   GEN expot = gdivgs(gadd(gmulsg(d, gsubgs(k2, 1)), sumVga(Vga)), 4);
    1216        5117 :   GEN eno = ldata_get_rootno(ldata);
    1217        5117 :   long prec = nbits2prec( domain_get_bitprec(domain) );
    1218        5117 :   GEN w2 = ginv(gsqrt(eno, prec));
    1219        5117 :   GEN hardy = mkvec4(k2, w2, expot, gammafactor(Vga));
    1220        5117 :   return mkvec3(mkvecsmall(t),ldata, mkvec3(domain, molin, hardy));
    1221             : }
    1222             : 
    1223             : static void
    1224        2492 : lfunparams2(struct lfunp *S)
    1225             : {
    1226        2492 :   GEN vprec, L = S->L, an = S->an, bn = S->bn;
    1227             :   double sig0, pmax, sub2;
    1228        2492 :   long m, nan, nmax, neval, M = S->M;
    1229             : 
    1230             :   /* try to reduce parameters now we know the a_n (some may be 0) */
    1231        2492 :   if (typ(an) == t_VEC) an = RgV_kill0(an);
    1232        2492 :   nan = S->nmax; /* lg(an)-1 may be large than this */
    1233        2492 :   nmax = neval = 0;
    1234        2492 :   if (!bn)
    1235      167244 :     for (m = 0; m <= M; m++)
    1236             :     {
    1237      164773 :       long n = minss(nan, L[m+1]);
    1238      164773 :       while (n > 0 && !gel(an,n)) n--;
    1239      164773 :       if (n > nmax) nmax = n;
    1240      164773 :       neval += n;
    1241      164773 :       L[m+1] = n; /* reduce S->L[m+1] */
    1242             :     }
    1243             :   else
    1244             :   {
    1245          21 :     if (typ(bn) == t_VEC) bn = RgV_kill0(bn);
    1246        1036 :     for (m = 0; m <= M; m++)
    1247             :     {
    1248        1015 :       long n = minss(nan, L[m+1]);
    1249        1015 :       while (n > 0 && !gel(an,n) && !gel(bn,n)) n--;
    1250        1015 :       if (n > nmax) nmax = n;
    1251        1015 :       neval += n;
    1252        1015 :       L[m+1] = n; /* reduce S->L[m+1] */
    1253             :     }
    1254             :   }
    1255        2492 :   if (DEBUGLEVEL >= 1) err_printf("expected evaluations: %ld\n", neval);
    1256        2492 :   for (; M > 0; M--)
    1257        2492 :     if (L[M+1]) break;
    1258        2492 :   setlg(L, M+2);
    1259        2492 :   S->M = M;
    1260        2492 :   S->nmax = nmax;
    1261             : 
    1262        2492 :   pmax = 0;
    1263        2492 :   sig0 = S->MAXs/S->m0;
    1264        2492 :   sub2 = S->sub / M_LN2;
    1265        2492 :   vprec = cgetg(S->M+2, t_VEC);
    1266             :   /* compute accuracy to which we will need k[m,n] = K(n*exp(mh)/sqrt(N))
    1267             :    * vprec[m+1,n] = absolute accuracy to which we need k[m,n] */
    1268      168280 :   for (m = 0; m <= S->M; m++)
    1269             :   {
    1270      165788 :     double c = S->D + maxdd(m*sig0 - sub2, 0);
    1271             :     GEN t;
    1272      165788 :     if (!S->k1)
    1273             :     {
    1274      153146 :       t = const_vecsmall(L[m+1]+1, c);
    1275      153146 :       pmax = maxdd(pmax,c);
    1276             :     }
    1277             :     else
    1278             :     {
    1279             :       long n;
    1280       12642 :       t = cgetg(L[m+1]+1, t_VECSMALL);
    1281     2255022 :       for (n = 1; n <= L[m+1]; n++)
    1282             :       {
    1283     2242380 :         t[n] = c + S->k1 * log2(n);
    1284     2242380 :         pmax = maxdd(pmax, t[n]);
    1285             :       }
    1286             :     }
    1287      165788 :     gel(vprec,m+1) = t;
    1288             :   }
    1289        2492 :   S->vprec = vprec;
    1290        2492 :   S->Dmax = pmax;
    1291        2492 :   S->precmax = nbits2prec(pmax);
    1292        2492 : }
    1293             : 
    1294             : static GEN
    1295        4438 : lfun_init_theta(GEN ldata, GEN eno, struct lfunp *S)
    1296             : {
    1297        4438 :   GEN an2, dual, tdom = NULL, Vga = ldata_get_gammavec(ldata);
    1298             :   long L;
    1299        4438 :   if (eno)
    1300        3360 :     L = S->nmax;
    1301             :   else
    1302             :   {
    1303        1078 :     tdom = dbltor(sqrt(0.5));
    1304        1078 :     L = maxss(S->nmax, lfunthetacost(ldata, tdom, 0, S->D));
    1305             :   }
    1306        4438 :   dual = ldata_get_dual(ldata);
    1307        4438 :   S->an = ldata_vecan(ldata_get_an(ldata), L, S->precmax);
    1308        4431 :   S->bn = typ(dual)==t_INT? NULL: ldata_vecan(dual, S->nmax, S->precmax);
    1309        4431 :   if (!vgaell(Vga)) lfunparams2(S);
    1310             :   else
    1311             :   {
    1312        1939 :     S->an = antwist(S->an, Vga, S->precmax);
    1313        1939 :     if (S->bn) S->bn = antwist(S->bn, Vga, S->precmax);
    1314        1939 :     S->vprec = NULL;
    1315             :   }
    1316        4431 :   an2 = lg(Vga)-1 == 1? antwist(S->an, Vga, S->precmax): S->an;
    1317        4431 :   return lfunthetainit0(ldata, tdom, an2, 0, S->Dmax, 0);
    1318             : }
    1319             : 
    1320             : GEN
    1321       10421 : lfuncost(GEN L, GEN dom, long der, long bitprec)
    1322             : {
    1323       10421 :   pari_sp av = avma;
    1324       10421 :   GEN ldata = lfunmisc_to_ldata_shallow(L);
    1325       10421 :   long k = ldata_get_k(ldata);
    1326             :   struct lfunp S;
    1327             : 
    1328       10421 :   parse_dom(k, dom, &S);
    1329       10421 :   lfunparams(ldata, der, bitprec, &S);
    1330       10421 :   set_avma(av); return mkvecsmall2(S.nmax, S.Dmax);
    1331             : }
    1332             : GEN
    1333          42 : lfuncost0(GEN L, GEN dom, long der, long bitprec)
    1334             : {
    1335          42 :   pari_sp av = avma;
    1336             :   GEN C;
    1337             : 
    1338          42 :   if (is_linit(L))
    1339             :   {
    1340          28 :     GEN tech = linit_get_tech(L);
    1341          28 :     GEN domain = lfun_get_domain(tech);
    1342          28 :     dom = domain_get_dom(domain);
    1343          28 :     der = domain_get_der(domain);
    1344          28 :     bitprec = domain_get_bitprec(domain);
    1345          28 :     if (linit_get_type(L) == t_LDESC_PRODUCT)
    1346             :     {
    1347          21 :       GEN v = lfunprod_get_fact(linit_get_tech(L)), F = gel(v,1);
    1348          21 :       long i, l = lg(F);
    1349          21 :       C = cgetg(l, t_VEC);
    1350          77 :       for (i = 1; i < l; ++i)
    1351          56 :         gel(C, i) = zv_to_ZV( lfuncost(gel(F,i), dom, der, bitprec) );
    1352          21 :       return gerepileupto(av, C);
    1353             :     }
    1354             :   }
    1355          21 :   if (!dom) pari_err_TYPE("lfuncost [missing s domain]", L);
    1356          21 :   C = lfuncost(L,dom,der,bitprec);
    1357          21 :   return gerepileupto(av, zv_to_ZV(C));
    1358             : }
    1359             : 
    1360             : GEN
    1361       18254 : lfuninit(GEN lmisc, GEN dom, long der, long bitprec)
    1362             : {
    1363       18254 :   pari_sp ltop = avma;
    1364             :   GEN R, h, theta, ldata, qk, poqk, pol, eno, r, domain, molin;
    1365             :   long k;
    1366             :   struct lfunp S;
    1367             : 
    1368       18254 :   if (is_linit(lmisc))
    1369             :   {
    1370       13389 :     long t = linit_get_type(lmisc);
    1371       13389 :     if (t==t_LDESC_INIT || t==t_LDESC_PRODUCT)
    1372             :     {
    1373       13340 :       if (checklfuninit(lmisc, dom, der, bitprec)) return lmisc;
    1374          21 :       pari_warn(warner,"lfuninit: insufficient initialization");
    1375             :     }
    1376             :   }
    1377        4935 :   ldata = lfunmisc_to_ldata_shallow(lmisc);
    1378             : 
    1379        4935 :   if (ldata_get_type(ldata)==t_LFUN_NF)
    1380             :   {
    1381         497 :     GEN T = gel(ldata_get_an(ldata), 2);
    1382         497 :     return lfunzetakinit(T, dom, der, 0, bitprec);
    1383             :   }
    1384        4438 :   k = ldata_get_k(ldata);
    1385        4438 :   parse_dom(k, dom, &S);
    1386        4438 :   lfunparams(ldata, der, bitprec, &S);
    1387        4438 :   r = ldata_get_residue(ldata);
    1388             :   /* Note: all guesses should already have been performed (thetainit more
    1389             :    * expensive than needed: should be either tdom = 1 or bitprec = S.D).
    1390             :    * BUT if the root number / polar part do not have an algebraic
    1391             :    * expression, there is no way to do this until we know the
    1392             :    * precision, i.e. now. So we can't remove guessing code from here and
    1393             :    * lfun_init_theta */
    1394        4438 :   if (r && isintzero(r)) eno = NULL;
    1395             :   else
    1396             :   {
    1397        4438 :     eno = ldata_get_rootno(ldata);
    1398        4438 :     if (isintzero(eno)) eno = NULL;
    1399             :   }
    1400        4438 :   theta = lfun_init_theta(ldata, eno, &S);
    1401        4431 :   if (eno && lg(ldata)==7)
    1402        2002 :     R = gen_0;
    1403             :   else
    1404             :   {
    1405        2429 :     GEN v = lfunrootres(theta, S.D);
    1406        2429 :     ldata = shallowcopy(ldata);
    1407        2429 :     gel(ldata, 6) = gel(v,3);
    1408        2429 :     r = gel(v,1);
    1409        2429 :     if (isintzero(r))
    1410        1064 :       setlg(ldata,7); /* no pole */
    1411             :     else
    1412        1365 :       gel(ldata, 7) = r;
    1413        2429 :     R = lfunrtoR(ldata, nbits2prec(S.D));
    1414             :   }
    1415        4431 :   h = divru(mplog2(S.precmax), S.m0);
    1416        4431 :   k = ldata_get_k(ldata);
    1417        4431 :   qk = gprec_w(mpexp(gmul2n(gmulsg(k,h), -1)), S.precmax); /* exp(kh/2) */
    1418        4431 :   poqk = gpowers(qk, S.M);
    1419        4431 :   pol = lfuninit_vecc(theta, h, &S, poqk);
    1420        4431 :   molin = mkvec3(h, pol, R);
    1421        4431 :   domain = mkvec2(dom, mkvecsmall2(der, bitprec));
    1422        4431 :   return gerepilecopy(ltop, lfuninit_make(t_LDESC_INIT, ldata, molin, domain));
    1423             : }
    1424             : 
    1425             : GEN
    1426         406 : lfuninit0(GEN lmisc, GEN dom, long der, long bitprec)
    1427             : {
    1428         406 :   GEN z = lfuninit(lmisc, dom, der, bitprec);
    1429         406 :   return z == lmisc? gcopy(z): z;
    1430             : }
    1431             : 
    1432             : /* If s is a pole of Lambda, return polar part at s; else return NULL */
    1433             : static GEN
    1434        4212 : lfunpoleresidue(GEN R, GEN s)
    1435             : {
    1436             :   long j;
    1437       11803 :   for (j = 1; j < lg(R); j++)
    1438             :   {
    1439        8102 :     GEN Rj = gel(R, j), be = gel(Rj, 1);
    1440        8102 :     if (gequal(s, be)) return gel(Rj, 2);
    1441             :   }
    1442        3701 :   return NULL;
    1443             : }
    1444             : 
    1445             : /* Compute contribution of polar part at s when not a pole. */
    1446             : static GEN
    1447        6653 : veccothderivn(GEN a, long n)
    1448             : {
    1449             :   long i;
    1450        6653 :   pari_sp av = avma;
    1451        6653 :   GEN c = pol_x(0), cp = mkpoln(3, gen_m1, gen_0, gen_1);
    1452        6653 :   GEN v = cgetg(n+2, t_VEC);
    1453        6653 :   gel(v, 1) = poleval(c, a);
    1454       20022 :   for(i = 2; i <= n+1; i++)
    1455             :   {
    1456       13369 :     c = ZX_mul(ZX_deriv(c), cp);
    1457       13369 :     gel(v, i) = gdiv(poleval(c, a), mpfact(i-1));
    1458             :   }
    1459        6653 :   return gerepilecopy(av, v);
    1460             : }
    1461             : 
    1462             : static GEN
    1463        6716 : polepart(long n, GEN h, GEN C)
    1464             : {
    1465        6716 :   GEN h2n = gpowgs(gdiv(h, gen_2), n-1);
    1466        6716 :   GEN res = gmul(h2n, gel(C,n));
    1467        6716 :   return odd(n)? res : gneg(res);
    1468             : }
    1469             : 
    1470             : static GEN
    1471        3323 : lfunsumcoth(GEN R, GEN s, GEN h, long prec)
    1472             : {
    1473             :   long i,j;
    1474        3323 :   GEN S = gen_0;
    1475        9976 :   for (j = 1; j < lg(R); ++j)
    1476             :   {
    1477        6653 :     GEN r = gel(R,j), be = gel(r,1), Rj = gel(r, 2);
    1478        6653 :     long e = valp(Rj);
    1479        6653 :     GEN z1 = gexpm1(gmul(h, gsub(s,be)), prec); /* exp(h(s-beta))-1 */
    1480        6653 :     GEN c1 = gaddgs(gdivsg(2, z1), 1); /* coth((h/2)(s-beta)) */
    1481        6653 :     GEN C1 = veccothderivn(c1, 1-e);
    1482       13369 :     for (i = e; i < 0; i++)
    1483             :     {
    1484        6716 :       GEN Rbe = mysercoeff(Rj, i);
    1485        6716 :       GEN p1 = polepart(-i, h, C1);
    1486        6716 :       S = gadd(S, gmul(Rbe, p1));
    1487             :     }
    1488             :   }
    1489        3323 :   return gmul2n(S, -1);
    1490             : }
    1491             : 
    1492             : static GEN lfunlambda_OK(GEN linit, GEN s, GEN sdom, long bitprec);
    1493             : /* L is a t_LDESC_PRODUCT Linit */
    1494             : static GEN
    1495        1279 : lfunlambda_product(GEN L, GEN s, GEN sdom, long bitprec)
    1496             : {
    1497        1279 :   GEN ldata = linit_get_ldata(L), v = lfunprod_get_fact(linit_get_tech(L));
    1498        1279 :   GEN r = gen_1, F = gel(v,1), E = gel(v,2), C = gel(v,3), cs = conj_i(s);
    1499        1279 :   long i, l = lg(F), isreal = gequal(imag_i(s), imag_i(cs));
    1500        4439 :   for (i = 1; i < l; ++i)
    1501             :   {
    1502        3160 :     GEN f = lfunlambda_OK(gel(F, i), s, sdom, bitprec);
    1503        3160 :     if (E[i])
    1504        3160 :       r = gmul(r, gpowgs(f, E[i]));
    1505        3160 :     if (C[i])
    1506             :     {
    1507         378 :       GEN fc = isreal? f: lfunlambda_OK(gel(F, i), cs, sdom, bitprec);
    1508         378 :       r = gmul(r, gpowgs(conj_i(fc), C[i]));
    1509             :     }
    1510             :   }
    1511        1279 :   return (ldata_isreal(ldata) && gequal0(imag_i(s)))? real_i(r): r;
    1512             : }
    1513             : 
    1514             : /* s a t_SER */
    1515             : static long
    1516        1093 : der_level(GEN s)
    1517        1093 : { return signe(s)? lg(s)-3: valp(s)-1; }
    1518             : 
    1519             : /* s a t_SER; return coeff(s, X^0) */
    1520             : static GEN
    1521         217 : ser_coeff0(GEN s) { return simplify_shallow(polcoef_i(s, 0, -1)); }
    1522             : 
    1523             : static GEN
    1524        5264 : get_domain(GEN s, GEN *dom, long *der)
    1525             : {
    1526        5264 :   GEN sa = s;
    1527        5264 :   *der = 0;
    1528        5264 :   switch(typ(s))
    1529             :   {
    1530             :     case t_POL:
    1531           7 :     case t_RFRAC: s = toser_i(s);
    1532             :     case t_SER:
    1533         217 :       *der = der_level(s);
    1534         217 :       sa = ser_coeff0(s);
    1535             :   }
    1536        5264 :   *dom = mkvec3(real_i(sa), gen_0, gabs(imag_i(sa),DEFAULTPREC));
    1537        5264 :   return s;
    1538             : }
    1539             : 
    1540             : /* assume lmisc is an linit, s went through get_domain and s/bitprec belong
    1541             :  * to domain */
    1542             : static GEN
    1543       19363 : lfunlambda_OK(GEN linit, GEN s, GEN sdom, long bitprec)
    1544             : {
    1545             :   GEN eno, ldata, tech, h, pol;
    1546       19363 :   GEN S, S0 = NULL, k2, cost;
    1547             :   long prec, prec0;
    1548             :   struct lfunp D, D0;
    1549             : 
    1550       19363 :   if (linit_get_type(linit) == t_LDESC_PRODUCT)
    1551        1279 :     return lfunlambda_product(linit, s, sdom, bitprec);
    1552       18084 :   ldata = linit_get_ldata(linit);
    1553       18084 :   eno = ldata_get_rootno(ldata);
    1554       18084 :   tech = linit_get_tech(linit);
    1555       18084 :   h = lfun_get_step(tech); prec = realprec(h);
    1556             :   /* try to reduce accuracy */
    1557       18084 :   parse_dom(0, sdom, &D0);
    1558       18084 :   parse_dom(0, domain_get_dom(lfun_get_domain(tech)), &D);
    1559       18084 :   if (0.8 * D.dh > D0.dh)
    1560             :   {
    1561       10344 :     cost = lfuncost(linit, sdom, typ(s)==t_SER? der_level(s): 0, bitprec);
    1562       10344 :     prec0 = nbits2prec(cost[2]);
    1563       10344 :     if (prec0 < prec) { prec = prec0; h = gprec_w(h, prec); }
    1564             :   }
    1565       18084 :   pol = lfun_get_pol(tech);
    1566       18084 :   s = gprec_w(s, prec);
    1567       18084 :   if (ldata_get_residue(ldata))
    1568             :   {
    1569        3743 :     GEN R = lfun_get_Residue(tech);
    1570        3743 :     GEN Ra = lfunpoleresidue(R, s);
    1571        3743 :     if (Ra) return gprec_w(Ra, nbits2prec(bitprec));
    1572        3323 :     S0 = lfunsumcoth(R, s, h, prec);
    1573             :   }
    1574       17664 :   k2 = lfun_get_k2(tech);
    1575       17664 :   if (typ(pol)==t_POL && typ(s) != t_SER && gequal(real_i(s), k2))
    1576       12386 :   { /* on critical line: shortcut */
    1577       12386 :     GEN polz, b = imag_i(s);
    1578       12386 :     polz = gequal0(b)? poleval(pol,gen_1): poleval(pol, expIr(gmul(h,b)));
    1579       12386 :     S = gadd(polz, gmul(eno, conj_i(polz)));
    1580             :   }
    1581             :   else
    1582             :   {
    1583        5278 :     GEN z = gexp(gmul(h, gsub(s, k2)), prec);
    1584        5278 :     GEN zi = ginv(z), zc = conj_i(zi);
    1585        5278 :     if (typ(pol)==t_POL)
    1586        5089 :       S = gadd(poleval(pol, z), gmul(eno, conj_i(poleval(pol, zc))));
    1587             :     else
    1588         189 :       S = gadd(poleval(gel(pol,1), z), gmul(eno, poleval(gel(pol,2), zi)));
    1589             :   }
    1590       17664 :   if (S0) S = gadd(S,S0);
    1591       17664 :   return gprec_w(gmul(S,h), nbits2prec(bitprec));
    1592             : }
    1593             : GEN
    1594         812 : lfunlambda(GEN lmisc, GEN s, long bitprec)
    1595             : {
    1596         812 :   pari_sp av = avma;
    1597             :   GEN linit, dom, z;
    1598             :   long der;
    1599         812 :   s = get_domain(s, &dom, &der);
    1600         812 :   linit = lfuninit(lmisc, dom, der, bitprec);
    1601         812 :   z = lfunlambda_OK(linit,s, dom, bitprec);
    1602         812 :   return gerepilecopy(av, z);
    1603             : }
    1604             : 
    1605             : /* assume lmisc is an linit, s went through get_domain and s/bitprec belong
    1606             :  * to domain */
    1607             : static GEN
    1608        3969 : lfun_OK(GEN linit, GEN s, GEN sdom, long bitprec)
    1609             : {
    1610        3969 :   GEN N, gas, S, FVga, res, ss = s;
    1611        3969 :   long prec = nbits2prec(bitprec);
    1612             : 
    1613        3969 :   FVga = lfun_get_factgammavec(linit_get_tech(linit));
    1614        3969 :   S = lfunlambda_OK(linit, s, sdom, bitprec);
    1615        3969 :   if (typ(S)==t_SER)
    1616             :   {
    1617        1330 :     long d = lg(S) - 2 + fracgammadegree(FVga);
    1618        1330 :     if (typ(s) == t_SER)
    1619         959 :       ss = sertoser(s, d);
    1620             :     else
    1621         371 :       ss = deg1ser_shallow(gen_1, s, varn(S), d);
    1622             :   }
    1623        3969 :   gas = gammafactproduct(FVga, ss, prec);
    1624        3969 :   N = ldata_get_conductor(linit_get_ldata(linit));
    1625        3969 :   res = gdiv(S, gmul(gpow(N, gdivgs(ss, 2), prec), gas));
    1626        3969 :   if (typ(s)!=t_SER && typ(res)==t_SER)
    1627             :   {
    1628         406 :     long v = valp(res);
    1629         406 :     if (v > 0) return gen_0;
    1630         371 :     if (v == 0) res = gel(res, 2);
    1631             :     else
    1632         259 :       setlg(res, minss(lg(res), 2-v));
    1633             :   }
    1634        3934 :   return gprec_w(res, prec);
    1635             : }
    1636             : 
    1637             : GEN
    1638        3192 : lfun(GEN lmisc, GEN s, long bitprec)
    1639             : {
    1640        3192 :   pari_sp av = avma;
    1641             :   GEN linit, dom, z;
    1642             :   long der;
    1643        3192 :   s = get_domain(s, &dom, &der);
    1644        3192 :   linit = lfuninit(lmisc, dom, der, bitprec);
    1645        3185 :   z = lfun_OK(linit, s, dom, bitprec);
    1646        3185 :   return gerepilecopy(av, z);
    1647             : }
    1648             : 
    1649             : /* given a t_SER a+x*s(x), return x*s(x), shallow */
    1650             : static GEN
    1651          42 : sersplit1(GEN s, GEN *head)
    1652             : {
    1653          42 :   long i, l = lg(s);
    1654             :   GEN y;
    1655          42 :   *head = simplify_shallow(mysercoeff(s, 0));
    1656          42 :   if (valp(s) > 0) return s;
    1657          28 :   y = cgetg(l-1, t_SER); y[1] = s[1];
    1658          28 :   setvalp(y, 1);
    1659          28 :   for (i=3; i < l; i++) gel(y,i-1) = gel(s,i);
    1660          28 :   return normalize(y);
    1661             : }
    1662             : 
    1663             : /* n-th derivative of t_SER x, n > 0 */
    1664             : static GEN
    1665         112 : derivnser(GEN x, long n)
    1666             : {
    1667         112 :   long i, vx = varn(x), e = valp(x), lx = lg(x);
    1668             :   GEN y;
    1669         112 :   if (ser_isexactzero(x))
    1670             :   {
    1671           0 :     x = gcopy(x);
    1672           0 :     if (e) setvalp(x,e-n);
    1673           0 :     return x;
    1674             :   }
    1675         210 :   if (e < 0 || e >= n)
    1676             :   {
    1677          98 :     y = cgetg(lx,t_SER);
    1678          98 :     y[1] = evalsigne(1)| evalvalp(e-n) | evalvarn(vx);
    1679         490 :     for (i=0; i<lx-2; i++)
    1680         392 :       gel(y,i+2) = gmul(muls_interval(i+e-n+1,i+e), gel(x,i+2));
    1681             :   } else {
    1682          14 :     if (lx <= n+2) return zeroser(vx, 0);
    1683          14 :     lx -= n;
    1684          14 :     y = cgetg(lx,t_SER);
    1685          14 :     y[1] = evalsigne(1)|_evalvalp(0) | evalvarn(vx);
    1686          70 :     for (i=0; i<lx-2; i++)
    1687          56 :       gel(y,i+2) = gmul(muls_interval(i+1,i+n),gel(x,i+2+n-e));
    1688             :   }
    1689         112 :   return normalize(y);
    1690             : }
    1691             : 
    1692             : /* order of pole of Lambda at s (0 if regular point) */
    1693             : static long
    1694        1834 : lfunlambdaord(GEN linit, GEN s)
    1695             : {
    1696        1834 :   GEN tech = linit_get_tech(linit);
    1697        1834 :   if (linit_get_type(linit)==t_LDESC_PRODUCT)
    1698             :   {
    1699         224 :     GEN v = lfunprod_get_fact(linit_get_tech(linit));
    1700         224 :     GEN F = gel(v, 1), E = gel(v, 2), C = gel(v, 3);
    1701         224 :     long i, ex = 0, l = lg(F);
    1702         840 :     for (i = 1; i < l; i++)
    1703         616 :       ex += lfunlambdaord(gel(F,i), s) * (E[i]+C[i]);
    1704         224 :     return ex;
    1705             :   }
    1706        1610 :   if (ldata_get_residue(linit_get_ldata(linit)))
    1707             :   {
    1708         469 :     GEN r = lfunpoleresidue(lfun_get_Residue(tech), s);
    1709         469 :     if (r) return lg(r)-2;
    1710             :   }
    1711        1519 :   return 0;
    1712             : }
    1713             : 
    1714             : /* derivative of order m > 0 of L (flag = 0) or Lambda (flag = 1) */
    1715             : static GEN
    1716        1267 : lfunderiv(GEN lmisc, long m, GEN s, long flag, long bitprec)
    1717             : {
    1718        1267 :   pari_sp ltop = avma;
    1719        1267 :   GEN res, S = NULL, linit, dom;
    1720        1267 :   long der, prec = nbits2prec(bitprec);
    1721        1267 :   if (m <= 0) pari_err_DOMAIN("lfun", "D", "<=", gen_0, stoi(m));
    1722        1260 :   s = get_domain(s, &dom, &der);
    1723        1260 :   linit = lfuninit(lmisc, dom, der + m, bitprec);
    1724        1260 :   if (typ(s) == t_SER)
    1725             :   {
    1726          42 :     long v, l = lg(s)-1;
    1727             :     GEN sh;
    1728          42 :     if (valp(s) < 0) pari_err_DOMAIN("lfun","valuation", "<", gen_0, s);
    1729          42 :     S = sersplit1(s, &sh);
    1730          42 :     v = valp(S);
    1731          42 :     s = deg1ser_shallow(gen_1, sh, varn(S), m + (l+v-1)/v);
    1732             :   }
    1733             :   else
    1734             :   {
    1735        1218 :     long ex = lfunlambdaord(linit, s);
    1736             :     /* HACK: pretend lfuninit was done to right accuracy */
    1737        1218 :     s = deg1ser_shallow(gen_1, s, 0, m+1+ex);
    1738             :   }
    1739        2044 :   res = flag ? lfunlambda_OK(linit, s, dom, bitprec):
    1740         784 :                lfun_OK(linit, s, dom, bitprec);
    1741        1260 :   if (S)
    1742          42 :     res = gsubst(derivnser(res, m), varn(S), S);
    1743        1218 :   else if (typ(res)==t_SER)
    1744             :   {
    1745        1218 :     long v = valp(res);
    1746        1218 :     if (v > m) { set_avma(ltop); return gen_0; }
    1747        1211 :     if (v >= 0)
    1748        1141 :       res = gmul(mysercoeff(res, m), mpfact(m));
    1749             :     else
    1750          70 :       res = derivnser(res, m);
    1751             :   }
    1752        1253 :   return gerepilecopy(ltop, gprec_w(res, prec));
    1753             : }
    1754             : 
    1755             : GEN
    1756        1211 : lfunlambda0(GEN lmisc, GEN s, long der, long bitprec)
    1757             : {
    1758        1211 :   return der ? lfunderiv(lmisc, der, s, 1, bitprec):
    1759             :                lfunlambda(lmisc, s, bitprec);
    1760             : }
    1761             : 
    1762             : GEN
    1763        3206 : lfun0(GEN lmisc, GEN s, long der, long bitprec)
    1764             : {
    1765        3206 :   return der ? lfunderiv(lmisc, der, s, 0, bitprec):
    1766             :                lfun(lmisc, s, bitprec);
    1767             : }
    1768             : 
    1769             : GEN
    1770       10841 : lfunhardy(GEN lmisc, GEN t, long bitprec)
    1771             : {
    1772       10841 :   pari_sp ltop = avma;
    1773       10841 :   long prec = nbits2prec(bitprec), k, d;
    1774             :   GEN argz, z, linit, ldata, tech, dom, w2, k2, expot, h, a;
    1775             : 
    1776       10841 :   switch(typ(t))
    1777             :   {
    1778       10834 :     case t_INT: case t_FRAC: case t_REAL: break;
    1779           7 :     default: pari_err_TYPE("lfunhardy",t);
    1780             :   }
    1781             : 
    1782       10834 :   ldata = lfunmisc_to_ldata_shallow(lmisc);
    1783       10834 :   if (!is_linit(lmisc)) lmisc = ldata;
    1784       10834 :   k = ldata_get_k(ldata);
    1785       10834 :   d = ldata_get_degree(ldata);
    1786       10834 :   dom = mkvec3(dbltor(k/2.), gen_0, gabs(t,LOWDEFAULTPREC));
    1787       10834 :   linit = lfuninit(lmisc, dom, 0, bitprec);
    1788       10834 :   tech = linit_get_tech(linit);
    1789       10834 :   w2 = lfun_get_w2(tech);
    1790       10834 :   k2 = lfun_get_k2(tech);
    1791       10834 :   expot = lfun_get_expot(tech);
    1792       10834 :   z = mkcomplex(k2, t);
    1793       10834 :   argz = gatan(gdiv(t, k2), prec); /* more accurate than garg since k/2 \in Q */
    1794             :   /* prec may have increased: don't lose accuracy if |z|^2 is exact */
    1795       10834 :   prec = precision(argz);
    1796       10834 :   a = gsub(gmulsg(d, gmul(t, gmul2n(argz,-1))),
    1797             :            gmul(expot,glog(gnorm(z),prec)));
    1798       10834 :   h = lfunlambda_OK(linit, z, mkvec(t), bitprec);
    1799       10834 :   if (typ(ldata_get_dual(ldata))==t_INT)
    1800       10806 :     h = mulreal(h, w2);
    1801             :   else
    1802          28 :     h = gmul(h, w2);
    1803       10834 :   if (typ(h) == t_COMPLEX && gexpo(imag_i(h)) < -(bitprec >> 1))
    1804           0 :     h = real_i(h);
    1805       10834 :   return gerepileupto(ltop, gmul(h, gexp(a, prec)));
    1806             : }
    1807             : 
    1808             : /* L = log(t); return  \sum_{i = 0}^{v-1}  R[-i-1] L^i/i! */
    1809             : static GEN
    1810        3689 : theta_pole_contrib(GEN R, long v, GEN L)
    1811             : {
    1812        3689 :   GEN s = mysercoeff(R,-v);
    1813             :   long i;
    1814        3850 :   for (i = v-1; i >= 1; i--)
    1815         161 :     s = gadd(mysercoeff(R,-i), gdivgs(gmul(s,L), i));
    1816        3689 :   return s;
    1817             : }
    1818             : /* subtract successively rather than adding everything then subtracting.
    1819             :  * The polar part is "large" and suffers from cancellation: a little stabler
    1820             :  * this way */
    1821             : static GEN
    1822        4277 : theta_add_polar_part(GEN S, GEN R, GEN t, long prec)
    1823             : {
    1824        4277 :   GEN logt = NULL;
    1825        4277 :   long j, l = lg(R);
    1826        7966 :   for (j = 1; j < l; j++)
    1827             :   {
    1828        3689 :     GEN Rj = gel(R,j), b = gel(Rj,1), Rb = gel(Rj,2);
    1829        3689 :     long v = -valp(Rb);
    1830        3689 :     if (v > 1 && !logt) logt = glog(t, prec);
    1831        3689 :     S = gsub(S, gmul(theta_pole_contrib(Rb,v,logt), gpow(t,b,prec)));
    1832             :   }
    1833        4277 :   return S;
    1834             : }
    1835             : 
    1836             : static long
    1837        2492 : lfuncheckfeq_i(GEN theta, GEN thetad, GEN t0, GEN t0i, long bitprec)
    1838             : {
    1839        2492 :   GEN ldata = linit_get_ldata(theta);
    1840             :   GEN S0, S0i, w, eno;
    1841        2492 :   long prec = nbits2prec(bitprec);
    1842        2492 :   if (thetad)
    1843          35 :     S0 = lfuntheta(thetad, t0, 0, bitprec);
    1844             :   else
    1845        2457 :     S0 = conj_i(lfuntheta(theta, conj_i(t0), 0, bitprec));
    1846        2492 :   S0i = lfuntheta(theta, t0i, 0, bitprec);
    1847             : 
    1848        2492 :   eno = ldata_get_rootno(ldata);
    1849        2492 :   if (ldata_get_residue(ldata))
    1850             :   {
    1851         469 :     GEN R = theta_get_R(linit_get_tech(theta));
    1852         469 :     if (gequal0(R))
    1853             :     {
    1854             :       GEN v, r;
    1855          42 :       if (ldata_get_type(ldata) == t_LFUN_NF)
    1856             :       { /* inefficient since theta not needed; no need to optimize for this
    1857             :            (artificial) query [e.g. lfuncheckfeq(t_POL)] */
    1858          21 :         GEN T = gel(ldata_get_an(ldata), 2);
    1859          21 :         GEN L = lfunzetakinit(T,zerovec(3),0,0,bitprec);
    1860          21 :         return lfuncheckfeq(L,t0,bitprec);
    1861             :       }
    1862          21 :       v = lfunrootres(theta, bitprec);
    1863          21 :       r = gel(v,1);
    1864          21 :       if (gequal0(eno)) eno = gel(v,3);
    1865          21 :       R = lfunrtoR_i(ldata, r, eno, nbits2prec(bitprec));
    1866             :     }
    1867         448 :     S0i = theta_add_polar_part(S0i, R, t0, prec);
    1868             :   }
    1869        2471 :   if (gequal0(S0i) || gequal0(S0)) pari_err_PREC("lfuncheckfeq");
    1870        2471 :   w = gdiv(S0i, gmul(S0, gpowgs(t0, ldata_get_k(ldata))));
    1871             :   /* missing rootno: guess it */
    1872        2471 :   if (gequal0(eno)) eno = lfunrootno(theta, bitprec);
    1873        2471 :   w = gsub(w, eno);
    1874        2471 :   if (thetad) w = gdiv(w, eno); /* |eno| may be large in non-dual case */
    1875        2471 :   return gexpo(w);
    1876             : }
    1877             : 
    1878             : /* Check whether the coefficients, conductor, weight, polar part and root
    1879             :  * number are compatible with the functional equation at t0 and 1/t0.
    1880             :  * Different from lfunrootres. */
    1881             : long
    1882        2562 : lfuncheckfeq(GEN lmisc, GEN t0, long bitprec)
    1883             : {
    1884             :   GEN ldata, theta, thetad, t0i;
    1885             :   pari_sp av;
    1886             : 
    1887        2562 :   if (is_linit(lmisc) && linit_get_type(lmisc)==t_LDESC_PRODUCT)
    1888             :   {
    1889         112 :     GEN v = lfunprod_get_fact(linit_get_tech(lmisc)), F = gel(v,1);
    1890         112 :     long i, b = -bitprec, l = lg(F);
    1891         112 :     for (i = 1; i < l; i++) b = maxss(b, lfuncheckfeq(gel(F,i), t0, bitprec));
    1892         112 :     return b;
    1893             :   }
    1894        2450 :   av = avma;
    1895        2450 :   if (!t0)
    1896             :   { /* Pi/3 + I/7, some random complex number */
    1897        2380 :     long prec = nbits2prec(bitprec);
    1898        2380 :     t0 = mkcomplex(gdivgs(mppi(prec), 3), sstoQ(1,7));
    1899        2380 :     t0i = ginv(t0);
    1900             :   }
    1901          70 :   else if (gcmpgs(gnorm(t0), 1) < 0) { t0i = t0; t0 = ginv(t0); }
    1902          63 :   else t0i = ginv(t0);
    1903             :   /* |t0| >= 1 */
    1904        2450 :   theta = lfunthetacheckinit(lmisc, t0i, 0, bitprec);
    1905        2450 :   ldata = linit_get_ldata(theta);
    1906        2450 :   thetad = theta_dual(theta, ldata_get_dual(ldata));
    1907        2450 :   return gc_long(av, lfuncheckfeq_i(theta, thetad, t0, t0i, bitprec));
    1908             : }
    1909             : 
    1910             : /*******************************************************************/
    1911             : /*       Compute root number and residues                          */
    1912             : /*******************************************************************/
    1913             : /* round root number to \pm 1 if close to integer. */
    1914             : static GEN
    1915        3850 : ropm1(GEN eno, long prec)
    1916             : {
    1917             :   long e;
    1918        3850 :   GEN r = grndtoi(eno, &e);
    1919        3850 :   return (e < -prec2nbits(prec)/2)? r: eno;
    1920             : }
    1921             : 
    1922             : /* theta for t=1/sqrt(2) and t2==2t simultaneously, saving 25% of the work.
    1923             :  * Assume correct initialization (no thetacheck) */
    1924             : static void
    1925          91 : lfunthetaspec(GEN linit, long bitprec, GEN *pv, GEN *pv2)
    1926             : {
    1927          91 :   pari_sp av = avma;
    1928             :   GEN t, Vga, an, K, ldata, thetainit, v, v2, vroots;
    1929             :   long L, prec, n, d;
    1930             : 
    1931          91 :   ldata = linit_get_ldata(linit);
    1932          91 :   thetainit = linit_get_tech(linit);
    1933          91 :   prec = nbits2prec(bitprec);
    1934          91 :   Vga = ldata_get_gammavec(ldata); d = lg(Vga)-1;
    1935          91 :   if (vgaeasytheta(Vga))
    1936             :   {
    1937          70 :     GEN v2 = sqrtr(real2n(1, nbits2prec(bitprec)));
    1938          70 :     GEN v = shiftr(v2,-1);
    1939          70 :     *pv = lfuntheta(linit, v,  0, bitprec);
    1940          70 :     *pv2= lfuntheta(linit, v2, 0, bitprec);
    1941          70 :     return;
    1942             :   }
    1943          21 :   an = RgV_kill0( theta_get_an(thetainit) );
    1944          21 :   L = lg(an)-1;
    1945             :   /* to compute theta(1/sqrt(2)) */
    1946          21 :   t = ginv(gsqrt(gmul2n(ldata_get_conductor(ldata), 1), prec));
    1947             :   /* t = 1/sqrt(2N) */
    1948             : 
    1949             :   /* From then on, the code is generic and could be used to compute
    1950             :    * theta(t) / theta(2t) without assuming t = 1/sqrt(2) */
    1951          21 :   K = theta_get_K(thetainit);
    1952          21 :   vroots = mkvroots(d, L, prec);
    1953          21 :   t = gpow(t, gdivgs(gen_2, d), prec); /* rt variant: t->t^(2/d) */
    1954             :   /* v = \sum_{n <= L, n odd} a_n K(nt) */
    1955       83342 :   for (v = gen_0, n = 1; n <= L; n+=2)
    1956             :   {
    1957       83321 :     GEN tn, Kn, a = gel(an, n);
    1958             : 
    1959       83321 :     if (!a) continue;
    1960       12299 :     tn = gmul(t, gel(vroots,n));
    1961       12299 :     Kn = gammamellininvrt(K, tn, bitprec);
    1962       12299 :     v = gadd(v, gmul(a,Kn));
    1963             :   }
    1964             :   /* v += \sum_{n <= L, n even} a_n K(nt), v2 = \sum_{n <= L/2} a_n K(2n t) */
    1965       83328 :   for (v2 = gen_0, n = 1; n <= L/2; n++)
    1966             :   {
    1967       83307 :     GEN t2n, K2n, a = gel(an, n), a2 = gel(an,2*n);
    1968             : 
    1969       83307 :     if (!a && !a2) continue;
    1970        9198 :     t2n = gmul(t, gel(vroots,2*n));
    1971        9198 :     K2n = gammamellininvrt(K, t2n, bitprec);
    1972        9198 :     if (a) v2 = gadd(v2, gmul(a, K2n));
    1973        9198 :     if (a2) v = gadd(v,  gmul(a2,K2n));
    1974             :   }
    1975          21 :   *pv = v;
    1976          21 :   *pv2 = v2;
    1977          21 :   gerepileall(av, 2, pv,pv2);
    1978             : }
    1979             : 
    1980             : static GEN
    1981          56 : Rtor(GEN a, GEN R, GEN ldata, long prec)
    1982             : {
    1983          56 :   GEN FVga = gammafactor(ldata_get_gammavec(ldata));
    1984          56 :   GEN Na = gpow(ldata_get_conductor(ldata), gdivgs(a,2), prec);
    1985          56 :   return gdiv(R, gmul(Na, gammafactproduct(FVga, a, prec)));
    1986             : }
    1987             : 
    1988             : /* v = theta~(t), vi = theta(1/t) */
    1989             : static GEN
    1990        3829 : get_eno(GEN R, long k, GEN t, GEN v, GEN vi, long vx, long bitprec, long force)
    1991             : {
    1992        3829 :   GEN a0, a1, S = deg1pol(gmul(gpowgs(t,k), gneg(v)), vi, vx);
    1993        3829 :   long prec = nbits2prec(bitprec);
    1994             : 
    1995        3829 :   S = theta_add_polar_part(S, R, t, prec);
    1996        3829 :   if (typ(S) != t_POL || degpol(S) != 1) return NULL;
    1997        3829 :   a1 = gel(S,3); if (!force && gexpo(a1) < -bitprec/4) return NULL;
    1998        3794 :   a0 = gel(S,2);
    1999        3794 :   return gdiv(a0, gneg(a1));
    2000             : 
    2001             : }
    2002             : /* Return w using theta(1/t) - w t^k \bar{theta}(t) = polar_part(t,w).
    2003             :  * The full Taylor development of L must be known */
    2004             : GEN
    2005        3794 : lfunrootno(GEN linit, long bitprec)
    2006             : {
    2007             :   GEN ldata, t, eno, v, vi, R, thetad;
    2008        3794 :   long k, c = 0, prec = nbits2prec(bitprec), vx = fetch_var();
    2009             :   pari_sp av;
    2010             : 
    2011             :   /* initialize for t > 1/sqrt(2) */
    2012        3794 :   linit = lfunthetacheckinit(linit, dbltor(sqrt(0.5)), 0, bitprec);
    2013        3794 :   ldata = linit_get_ldata(linit);
    2014        3794 :   k = ldata_get_k(ldata);
    2015        8925 :   R = ldata_get_residue(ldata)? lfunrtoR_eno(ldata, pol_x(vx), prec)
    2016        5131 :                               : cgetg(1, t_VEC);
    2017        3794 :   t = gen_1;
    2018        3794 :   v = lfuntheta(linit, t, 0, bitprec);
    2019        3794 :   thetad = theta_dual(linit, ldata_get_dual(ldata));
    2020        3794 :   vi = !thetad ? conj_i(v): lfuntheta(thetad, t, 0, bitprec);
    2021        3794 :   eno = get_eno(R,k,t,vi,v, vx, bitprec, 0);
    2022        3794 :   if (!eno && !thetad)
    2023             :   { /* t = sqrt(2), vi = theta(1/t), v = theta(t) */
    2024          35 :     lfunthetaspec(linit, bitprec, &vi, &v);
    2025          35 :     t = sqrtr(utor(2, prec));
    2026          35 :     eno = get_eno(R,k,t,conj_i(v),vi, vx, bitprec, 0);
    2027             :   }
    2028        3794 :   av = avma;
    2029        7588 :   while (!eno)
    2030             :   {
    2031           0 :     t = addsr(1, shiftr(utor(pari_rand(), prec), -2-BITS_IN_LONG));
    2032             :     /* t in [1,1.25[ */
    2033           0 :     v = thetad? lfuntheta(thetad, t, 0, bitprec)
    2034           0 :               : conj_i(lfuntheta(linit, t, 0, bitprec));
    2035           0 :     vi = lfuntheta(linit, ginv(t), 0, bitprec);
    2036           0 :     eno = get_eno(R,k,t,v,vi, vx, bitprec, c++ == 5);
    2037           0 :     set_avma(av);
    2038             :   }
    2039        3794 :   delete_var(); return ropm1(eno,prec);
    2040             : }
    2041             : 
    2042             : /* Find root number and/or residues when L-function coefficients and
    2043             :    conductor are known. For the moment at most a single residue allowed. */
    2044             : GEN
    2045        2478 : lfunrootres(GEN data, long bitprec)
    2046             : {
    2047        2478 :   pari_sp ltop = avma;
    2048             :   GEN w, r, R, a, b, e, v, v2, be, ldata, linit;
    2049             :   long k, prec;
    2050             : 
    2051        2478 :   ldata = lfunmisc_to_ldata_shallow(data);
    2052        2478 :   r = ldata_get_residue(ldata);
    2053        2478 :   k = ldata_get_k(ldata);
    2054        2478 :   if (r) r = normalize_simple_pole(r, stoi(k));
    2055        2478 :   if (!r || residues_known(r))
    2056             :   {
    2057        2422 :     w = lfunrootno(data, bitprec);
    2058        2422 :     if (!r)
    2059        1085 :       r = R = gen_0;
    2060             :     else
    2061        1337 :       R = lfunrtoR_eno(ldata, w, nbits2prec(bitprec));
    2062        2422 :     return gerepilecopy(ltop, mkvec3(r, R, w));
    2063             :   }
    2064          56 :   linit = lfunthetacheckinit(data, dbltor(sqrt(0.5)), 0, bitprec);
    2065          56 :   prec = nbits2prec(bitprec);
    2066          56 :   if (lg(r) > 2) pari_err_IMPL("multiple poles in lfunrootres");
    2067             :   /* Now residue unknown, and r = [[be,0]]. */
    2068          56 :   be = gmael(r, 1, 1);
    2069          56 :   w = ldata_get_rootno(ldata);
    2070          56 :   if (ldata_isreal(ldata) && gequalm1(w))
    2071           0 :     R = lfuntheta(linit, gen_1, 0, bitprec);
    2072             :   else
    2073             :   {
    2074          56 :     lfunthetaspec(linit, bitprec, &v2, &v);
    2075          56 :     if (gequalgs(gmulsg(2, be), k)) pari_err_IMPL("pole at k/2 in lfunrootres");
    2076          56 :     if (gequalgs(be, k))
    2077             :     {
    2078           7 :       GEN p2k = int2n(k);
    2079           7 :       a = conj_i(gsub(gmul(p2k, v), v2));
    2080           7 :       b = subiu(p2k, 1);
    2081           7 :       e = gmul(gsqrt(p2k, prec), gsub(v2, v));
    2082             :     }
    2083             :     else
    2084             :     {
    2085          49 :       GEN tk2 = gsqrt(int2n(k), prec);
    2086          49 :       GEN tbe = gpow(gen_2, be, prec);
    2087          49 :       GEN tkbe = gpow(gen_2, gdivgs(gsubsg(k, be), 2), prec);
    2088          49 :       a = conj_i(gsub(gmul(tbe, v), v2));
    2089          49 :       b = gsub(gdiv(tbe, tkbe), tkbe);
    2090          49 :       e = gsub(gmul(gdiv(tbe, tk2), v2), gmul(tk2, v));
    2091             :     }
    2092          56 :     if (!isintzero(w)) R = gdiv(gsub(e, gmul(a, w)), b);
    2093             :     else
    2094             :     { /* Now residue unknown, r = [[be,0]], and w unknown. */
    2095           0 :       GEN t0  = mkfrac(stoi(11),stoi(10));
    2096           0 :       GEN th1 = lfuntheta(linit, t0,  0, bitprec);
    2097           0 :       GEN th2 = lfuntheta(linit, ginv(t0), 0, bitprec);
    2098           0 :       GEN tbe = gpow(t0, gmulsg(2, be), prec);
    2099           0 :       GEN tkbe = gpow(t0, gsubsg(k, be), prec);
    2100           0 :       GEN tk2 = gpowgs(t0, k);
    2101           0 :       GEN c = conj_i(gsub(gmul(tbe, th1), th2));
    2102           0 :       GEN d = gsub(gdiv(tbe, tkbe), tkbe);
    2103           0 :       GEN f = gsub(gmul(gdiv(tbe, tk2), th2), gmul(tk2, th1));
    2104           0 :       GEN D = gsub(gmul(a, d), gmul(b, c));
    2105           0 :       w = gdiv(gsub(gmul(d, e), gmul(b, f)), D);
    2106           0 :       R = gdiv(gsub(gmul(a, f), gmul(c, e)), D);
    2107             :     }
    2108             :   }
    2109          56 :   r = normalize_simple_pole(Rtor(be, R, ldata, prec), be);
    2110          56 :   R = lfunrtoR_i(ldata, r, w, prec);
    2111          56 :   return gerepilecopy(ltop, mkvec3(r, R, ropm1(w, prec)));
    2112             : }
    2113             : 
    2114             : /*******************************************************************/
    2115             : /*                           Zeros                                 */
    2116             : /*******************************************************************/
    2117             : struct lhardyz_t {
    2118             :   long bitprec, prec;
    2119             :   GEN linit;
    2120             : };
    2121             : 
    2122             : static GEN
    2123       10344 : lfunhardyzeros(void *E, GEN t)
    2124             : {
    2125       10344 :   struct lhardyz_t *S = (struct lhardyz_t*)E;
    2126       10344 :   long prec = S->prec;
    2127       10344 :   GEN h = lfunhardy(S->linit, t, S->bitprec);
    2128       10344 :   if (typ(h) == t_REAL && realprec(h) < prec) h = gprec_w(h, prec);
    2129       10344 :   return h;
    2130             : }
    2131             : 
    2132             : /* initialize for computation on critical line up to height h, zero
    2133             :  * of order <= m */
    2134             : static GEN
    2135         406 : lfuncenterinit(GEN lmisc, double h, long m, long bitprec)
    2136             : {
    2137         406 :   if (m < 0)
    2138             :   { /* choose a sensible default */
    2139         406 :     if (!is_linit(lmisc) || linit_get_type(lmisc) != t_LDESC_INIT) m = 4;
    2140             :     else
    2141             :     {
    2142         371 :       GEN domain = lfun_get_domain(linit_get_tech(lmisc));
    2143         371 :       m = domain_get_der(domain);
    2144             :     }
    2145             :   }
    2146         406 :   return lfuninit(lmisc, mkvec(dbltor(h)), m, bitprec);
    2147             : }
    2148             : 
    2149             : long
    2150         427 : lfunorderzero(GEN lmisc, long m, long bitprec)
    2151             : {
    2152         427 :   pari_sp ltop = avma;
    2153             :   GEN eno, ldata, linit, k2;
    2154             :   long G, c0, c, st, k;
    2155             : 
    2156         427 :   if (is_linit(lmisc) && linit_get_type(lmisc) == t_LDESC_PRODUCT)
    2157             :   {
    2158          63 :     GEN M = gmael(linit_get_tech(lmisc), 2,1);
    2159             :     long i;
    2160          63 :     for (c=0,i=1; i < lg(M); i++) c += lfunorderzero(gel(M,i), m, bitprec);
    2161          63 :     return c;
    2162             :   }
    2163         364 :   linit = lfuncenterinit(lmisc, 0, m, bitprec);
    2164         364 :   ldata = linit_get_ldata(linit);
    2165         364 :   eno = ldata_get_rootno(ldata);
    2166         364 :   G = -bitprec/2;
    2167         364 :   c0 = 0; st = 1;
    2168         364 :   if (ldata_isreal(ldata))
    2169             :   {
    2170         301 :     if (!gequal1(eno)) c0 = 1;
    2171         301 :     st = 2;
    2172             :   }
    2173         364 :   k = ldata_get_k(ldata);
    2174         364 :   k2 = gdivgs(stoi(k), 2);
    2175         378 :   for (c = c0;; c += st)
    2176         392 :     if (gexpo(lfun0(linit, k2, c, bitprec)) > G) return gc_long(ltop, c);
    2177             : }
    2178             : 
    2179             : GEN
    2180          42 : lfunzeros(GEN ldata, GEN lim, long divz, long bitprec)
    2181             : {
    2182          42 :   pari_sp ltop = avma;
    2183             :   GEN ldataf, linit, N, pi2, cN, pi2div, w, T, Vga, h1, h2;
    2184          42 :   long i, d, W, NEWD, precinit, ct, s, prec = nbits2prec(bitprec);
    2185             :   double maxt;
    2186             :   GEN maxtr, maxtr1;
    2187             :   struct lhardyz_t S;
    2188             : 
    2189          42 :   if (typ(lim) == t_VEC)
    2190             :   {
    2191           7 :     if (lg(lim) != 3 || gcmp(gel(lim,1),gel(lim,2)) >= 0
    2192           7 :                      || gcmp(gel(lim,1),gen_0) <= 0)
    2193           0 :       pari_err_TYPE("lfunzeros",lim);
    2194           7 :     h1 = gel(lim,1); h2 = gel(lim,2);
    2195             :   }
    2196             :   else
    2197             :   {
    2198          35 :     if (gcmp(lim,gen_0) <= 0)
    2199           0 :       pari_err_TYPE("lfunzeros",lim);
    2200          35 :     h1 = gen_0; h2 = lim;
    2201             :   }
    2202          42 :   maxt = gtodouble(h2);
    2203             : 
    2204          42 :   if (is_linit(ldata) && linit_get_type(ldata) == t_LDESC_PRODUCT)
    2205             :   {
    2206           0 :     GEN v, M = gmael(linit_get_tech(ldata), 2,1);
    2207           0 :     long l = lg(M);
    2208           0 :     v = cgetg(l, t_VEC);
    2209           0 :     for (i = 1; i < l; i++)
    2210           0 :       gel(v,i) = lfunzeros(gel(M,i), lim, divz, bitprec);
    2211           0 :     return gerepileupto(ltop, vecsort0(shallowconcat1(v), NULL, 0));
    2212             :   }
    2213          42 :   S.linit = linit = lfuncenterinit(ldata, maxt + 1, -1, bitprec);
    2214          42 :   S.bitprec = bitprec;
    2215          42 :   S.prec = prec;
    2216          42 :   ldataf = linit_get_ldata(linit);
    2217          42 :   Vga = ldata_get_gammavec(ldataf); d = lg(Vga) - 1;
    2218          42 :   N = ldata_get_conductor(ldataf);
    2219          42 :   NEWD = minss((long) ceil(bitprec+(M_PI/(4*M_LN2))*d*maxt),
    2220             :                lfun_get_bitprec(linit_get_tech(linit)));
    2221          42 :   precinit = prec; prec = nbits2prec(NEWD);
    2222          42 :   pi2 = Pi2n(1, prec);
    2223          42 :   cN = gdiv(N, gpowgs(Pi2n(-1, prec), d));
    2224          42 :   cN = gexpo(cN) >= 0? gaddsg(d, gmulsg(2, glog(cN, prec))): stoi(d);
    2225          42 :   pi2div = gdivgs(pi2, labs(divz));
    2226          42 :   ct = 0;
    2227          42 :   T = h1;
    2228          42 :   if (gequal0(h1))
    2229             :   {
    2230          35 :     GEN r = ldata_get_residue(ldataf);
    2231          35 :     if (!r || gequal0(r))
    2232             :     {
    2233          21 :       ct = lfunorderzero(linit, -1, bitprec);
    2234          21 :       if (ct) T = real2n(-prec2nbits(prec)/(2*ct), prec);
    2235             :     }
    2236             :   }
    2237             :   /* initialize for 100 further zeros, double later if needed */
    2238          42 :   W = 100 + ct; w = cgetg(W+1,t_VEC);
    2239          42 :   for (i=1; i<=ct; i++) gel(w,i) = gen_0;
    2240          42 :   s = gsigne(lfunhardyzeros(&S, T));
    2241          42 :   maxtr = h2; maxtr1 = gaddsg(1, maxtr);
    2242         427 :   while (gcmp(T, maxtr1) < 0)
    2243             :   {
    2244         385 :     pari_sp av = avma;
    2245         385 :     GEN T0 = T, z;
    2246             :     for(;;)
    2247        5705 :     {
    2248             :       long s0;
    2249             :       GEN L;
    2250        6090 :       if (gcmp(T, pi2) >= 0)
    2251        4221 :         L = gadd(cN, gmulsg(d, glog(gdiv(T, pi2), prec)));
    2252             :       else
    2253        1869 :         L = cN;
    2254        6090 :       T = gadd(T, gdiv(pi2div, L));
    2255        6090 :       if (gcmp(T, maxtr1) > 0) goto END;
    2256        6069 :       s0 = gsigne(lfunhardyzeros(&S, T));
    2257        6069 :       if (s0 != s) { s = s0; break; }
    2258             :     }
    2259         364 :     T = gerepileupto(av, T);
    2260         364 :     z = zbrent(&S, lfunhardyzeros, T0, T, prec);
    2261         364 :     if (gcmp(z, maxtr) > 0) break;
    2262         343 :     if (typ(z) == t_REAL) z  = rtor(z, precinit);
    2263             :     /* room for twice as many zeros */
    2264         343 :     if (ct >= W) { W *= 2; w = vec_lengthen(w, W); }
    2265         343 :     gel(w, ++ct) = z;
    2266             :   }
    2267             : END:
    2268          42 :   setlg(w, ct+1); return gerepilecopy(ltop, w);
    2269             : }
    2270             : 
    2271             : /*******************************************************************/
    2272             : /*       Guess conductor                                           */
    2273             : /*******************************************************************/
    2274             : struct huntcond_t {
    2275             :   long k;
    2276             :   GEN theta, thetad;
    2277             :   GEN *pM, *psqrtM, *pMd, *psqrtMd;
    2278             : };
    2279             : 
    2280             : static void
    2281        8709 : condset(struct huntcond_t *S, GEN M, long prec)
    2282             : {
    2283        8709 :   *(S->pM) = M;
    2284        8709 :   *(S->psqrtM) = gsqrt(M, prec);
    2285        8709 :   if (S->thetad != S->theta)
    2286             :   {
    2287           0 :     *(S->pMd) = *(S->pM);
    2288           0 :     *(S->psqrtMd) = *(S->psqrtM);
    2289             :   }
    2290        8709 : }
    2291             : 
    2292             : /* M should eventually converge to N, the conductor. L has no pole. */
    2293             : static GEN
    2294        6473 : wrap1(void *E, GEN M)
    2295             : {
    2296        6473 :   struct huntcond_t *S = (struct huntcond_t*)E;
    2297             :   GEN thetainit, tk, p1, p1inv;
    2298        6473 :   GEN t = mkfrac(stoi(11), stoi(10));
    2299             :   long prec, bitprec;
    2300             : 
    2301        6473 :   thetainit = linit_get_tech(S->theta);
    2302        6473 :   bitprec = theta_get_bitprec(thetainit);
    2303        6473 :   prec = nbits2prec(bitprec);
    2304        6473 :   condset(S, M, prec);
    2305        6473 :   tk = gpowgs(t, S->k);
    2306        6473 :   p1 = lfuntheta(S->thetad, t, 0, bitprec);
    2307        6473 :   p1inv = lfuntheta(S->theta, ginv(t), 0, bitprec);
    2308        6473 :   return glog(gabs(gmul(tk, gdiv(p1, p1inv)), prec), prec);
    2309             : }
    2310             : 
    2311             : /* M should eventually converge to N, the conductor. L has a pole. */
    2312             : static GEN
    2313        2194 : wrap2(void *E, GEN M)
    2314             : {
    2315        2194 :   struct huntcond_t *S = (struct huntcond_t*)E;
    2316             :   GEN t1k, t2k, p1, p1inv, p2, p2inv, thetainit, R;
    2317        2194 :   GEN t1 = mkfrac(stoi(11), stoi(10)), t2 = mkfrac(stoi(13), stoi(11));
    2318             :   GEN t1be, t2be, t1bemk, t2bemk, t1kmbe, t2kmbe;
    2319             :   GEN F11, F12, F21, F22, P1, P2, res;
    2320        2194 :   long k = S->k, prec, bitprec;
    2321             : 
    2322        2194 :   thetainit = linit_get_tech(S->theta);
    2323        2194 :   bitprec = theta_get_bitprec(thetainit);
    2324        2194 :   prec = nbits2prec(bitprec);
    2325        2194 :   condset(S, M, prec);
    2326             : 
    2327        2194 :   p1 = lfuntheta(S->thetad, t1, 0, bitprec);
    2328        2194 :   p2 = lfuntheta(S->thetad, t2, 0, bitprec);
    2329        2194 :   p1inv = lfuntheta(S->theta, ginv(t1), 0, bitprec);
    2330        2194 :   p2inv = lfuntheta(S->theta, ginv(t2), 0, bitprec);
    2331        2194 :   t1k = gpowgs(t1, k);
    2332        2194 :   t2k = gpowgs(t2, k);
    2333        2194 :   R = theta_get_R(thetainit);
    2334        2194 :   if (typ(R) == t_VEC)
    2335             :   {
    2336        2194 :     GEN be = gmael(R, 1, 1);
    2337        2194 :     t1be = gpow(t1, be, prec); t1bemk = gdiv(gsqr(t1be), t1k);
    2338        2194 :     t2be = gpow(t2, be, prec); t2bemk = gdiv(gsqr(t2be), t2k);
    2339        2194 :     t1kmbe = gdiv(t1k, t1be);
    2340        2194 :     t2kmbe = gdiv(t2k, t2be);
    2341             :   }
    2342             :   else
    2343             :   { /* be = k */
    2344           0 :     t1be = t1k; t1bemk = t1k; t1kmbe = gen_1;
    2345           0 :     t2be = t2k; t2bemk = t2k; t2kmbe = gen_1;
    2346             :   }
    2347        2194 :   F11 = conj_i(gsub(gmul(gsqr(t1be), p1), p1inv));
    2348        2194 :   F12 = conj_i(gsub(gmul(gsqr(t2be), p2), p2inv));
    2349        2194 :   F21 = gsub(gmul(t1k, p1), gmul(t1bemk, p1inv));
    2350        2194 :   F22 = gsub(gmul(t2k, p2), gmul(t2bemk, p2inv));
    2351        2194 :   P1 = gsub(gmul(t1bemk, t1be), t1kmbe);
    2352        2194 :   P2 = gsub(gmul(t2bemk, t2be), t2kmbe);
    2353        2194 :   res = gdiv(gsub(gmul(P2,F21), gmul(P1,F22)),
    2354             :              gsub(gmul(P2,F11), gmul(P1,F12)));
    2355        2194 :   return glog(gabs(res, prec), prec);
    2356             : }
    2357             : 
    2358             : /* If flag = 0 (default) return all conductors found as integers. If
    2359             : flag = 1, return the approximations, not the integers. If flag = 2,
    2360             : return all, even nonintegers. */
    2361             : 
    2362             : static GEN
    2363          84 : checkconductor(GEN v, long bit, long flag)
    2364             : {
    2365             :   GEN w;
    2366          84 :   long e, j, k, l = lg(v);
    2367          84 :   if (flag == 2) return v;
    2368          84 :   w = cgetg(l, t_VEC);
    2369         301 :   for (j = k = 1; j < l; j++)
    2370             :   {
    2371         217 :     GEN N = grndtoi(gel(v,j), &e);
    2372         217 :     if (e < -bit) gel(w,k++) = flag ? gel(v,j): N;
    2373             :   }
    2374          84 :   if (k == 2) return gel(w,1);
    2375           7 :   setlg(w,k); return w;
    2376             : }
    2377             : 
    2378             : static GEN
    2379          91 : parse_maxcond(GEN maxN)
    2380             : {
    2381             :   GEN M;
    2382          91 :   if (!maxN)
    2383          49 :     M = utoipos(10000);
    2384          42 :   else if (typ(maxN) == t_VEC)
    2385             :   {
    2386           7 :     if (!RgV_is_ZV(maxN)) pari_err_TYPE("lfunconductor",maxN);
    2387           7 :     return ZV_sort(maxN);
    2388             :   }
    2389             :   else
    2390          35 :     M = maxN;
    2391          84 :   return (typ(M) == t_INT)? addiu(M, 1): gceil(M);
    2392             : }
    2393             : 
    2394             : GEN
    2395          91 : lfunconductor(GEN data, GEN maxcond, long flag, long bitprec)
    2396             : {
    2397             :   struct huntcond_t S;
    2398          91 :   pari_sp av = avma;
    2399          91 :   GEN ldata = lfunmisc_to_ldata_shallow(data);
    2400          91 :   GEN ld, r, v, theta, thetad, M, tdom, t0 = NULL, t0i = NULL;
    2401             :   GEN (*eval)(void *, GEN);
    2402             :   long prec;
    2403          91 :   M = parse_maxcond(maxcond);
    2404          91 :   r = ldata_get_residue(ldata);
    2405          91 :   if (typ(M) == t_VEC) /* select in list */
    2406           7 :   { eval = NULL; tdom = dbltor(0.7); }
    2407          84 :   else if (!r) { eval = wrap1; tdom = sstoQ(10,11); }
    2408             :   else
    2409             :   {
    2410          21 :     if (typ(r) == t_VEC && lg(r) > 2)
    2411           0 :       pari_err_IMPL("multiple poles in lfunconductor");
    2412          21 :     eval = wrap2; tdom = sstoQ(11,13);
    2413             :   }
    2414          91 :   if (eval) bitprec += bitprec/2;
    2415          91 :   prec = nbits2prec(bitprec);
    2416          91 :   ld = shallowcopy(ldata);
    2417          91 :   gel(ld, 5) = eval? M: gel(M,lg(M)-1);
    2418          91 :   theta = lfunthetainit_i(ld, tdom, 0, bitprec);
    2419          91 :   thetad = theta_dual(theta, ldata_get_dual(ldata));
    2420          91 :   gel(theta,3) = shallowcopy(linit_get_tech(theta));
    2421          91 :   S.k = ldata_get_k(ldata);
    2422          91 :   S.theta = theta;
    2423          91 :   S.thetad = thetad? thetad: theta;
    2424          91 :   S.pM = &gel(linit_get_ldata(theta),5);
    2425          91 :   S.psqrtM = &gel(linit_get_tech(theta),7);
    2426          91 :   if (thetad)
    2427             :   {
    2428           0 :     S.pMd = &gel(linit_get_ldata(thetad),5);
    2429           0 :     S.psqrtMd = &gel(linit_get_tech(thetad),7);
    2430             :   }
    2431          91 :   if (!eval)
    2432             :   {
    2433           7 :     long i, besti = 0, beste = -10, l = lg(M);
    2434           7 :     t0 = sstoQ(11,10); t0i = sstoQ(10,11);
    2435          49 :     for (i = 1; i < l; i++)
    2436             :     {
    2437          42 :       pari_sp av2 = avma;
    2438             :       long e;
    2439          42 :       condset(&S, gel(M,i), prec);
    2440          42 :       e = lfuncheckfeq_i(theta, thetad, t0, t0i, bitprec);
    2441          42 :       set_avma(av2);
    2442          42 :       if (e < beste) { beste = e; besti = i; }
    2443          35 :       else if (e == beste) beste = besti = 0; /* tie: forget */
    2444             :     }
    2445           7 :     if (!besti) { set_avma(av); return cgetg(1,t_VEC); }
    2446           7 :     return gerepilecopy(av, mkvec2(gel(M,besti), stoi(beste)));
    2447             :   }
    2448          84 :   v = solvestep((void*)&S, eval, ghalf, M, gen_2, 14, prec);
    2449          84 :   return gerepilecopy(av, checkconductor(v, bitprec/2, flag));
    2450             : }
    2451             : 
    2452             : /* assume chi primitive */
    2453             : static GEN
    2454         588 : znchargauss_i(GEN G, GEN chi, long bitprec)
    2455             : {
    2456         588 :   GEN z, q, F = znstar_get_N(G);
    2457             :   long prec;
    2458             : 
    2459         588 :   if (equali1(F)) return gen_1;
    2460         343 :   prec = nbits2prec(bitprec);
    2461         343 :   q = sqrtr_abs(itor(F, prec));
    2462         343 :   z = lfuntheta(mkvec2(G,chi), gen_1, 0, bitprec);
    2463         343 :   if (gexpo(z) < 10 - bitprec)
    2464             :   {
    2465          28 :     if (equaliu(F,300))
    2466             :     {
    2467          14 :       GEN z = rootsof1u_cx(25, prec);
    2468          14 :       GEN n = znconreyexp(G, chi);
    2469          14 :       if (equaliu(n, 131)) return gmul(q, gpowgs(z,14));
    2470           7 :       if (equaliu(n, 71)) return gmul(q, gpowgs(z,11));
    2471             :     }
    2472          14 :     if (equaliu(F,600))
    2473             :     {
    2474          14 :       GEN z = rootsof1u_cx(25, prec);
    2475          14 :       GEN n = znconreyexp(G, chi);
    2476          14 :       if (equaliu(n, 491)) return gmul(q, gpowgs(z,7));
    2477           7 :       if (equaliu(n, 11)) return gmul(q, gpowgs(z,18));
    2478             :     }
    2479           0 :     pari_err_BUG("znchargauss [ Theta(chi,1) = 0 ]");
    2480             :   }
    2481         315 :   z = gmul(gdiv(z, conj_i(z)), q);
    2482         315 :   if (zncharisodd(G,chi)) z = mulcxI(z);
    2483         315 :   return z;
    2484             : }
    2485             : static GEN
    2486         588 : Z_radical(GEN N, long *om)
    2487             : {
    2488         588 :   GEN P = gel(Z_factor(N), 1);
    2489         588 :   *om = lg(P)-1; return ZV_prod(P);
    2490             : }
    2491             : GEN
    2492         805 : znchargauss(GEN G, GEN chi, GEN a, long bitprec)
    2493             : {
    2494             :   GEN v, T, N, F, b0, b1, b2, bF, a1, aF, A, r, GF, tau, B, faB, u, S;
    2495         805 :   long omb0, prec = nbits2prec(bitprec);
    2496         805 :   pari_sp av = avma;
    2497             : 
    2498         805 :   if (typ(chi) != t_COL) chi = znconreylog(G,chi);
    2499         805 :   T = znchartoprimitive(G, chi);
    2500         805 :   GF  = gel(T,1);
    2501         805 :   chi = gel(T,2); /* now primitive */
    2502         805 :   N = znstar_get_N(G);
    2503         805 :   F = znstar_get_N(GF);
    2504         805 :   if (equalii(N,F)) b1 = bF = gen_1;
    2505             :   else
    2506             :   {
    2507         231 :     v = Z_ppio(diviiexact(N,F), F);
    2508         231 :     bF = gel(v,2); /* (N/F, F^oo) */
    2509         231 :     b1 = gel(v,3); /* cofactor */
    2510             :   }
    2511         805 :   if (!a) a = a1 = aF = gen_1;
    2512             :   else
    2513             :   {
    2514         756 :     if (typ(a) != t_INT) pari_err_TYPE("znchargauss",a);
    2515         756 :     a = modii(a, N);
    2516         756 :     v = Z_ppio(a, F);
    2517         756 :     aF = gel(v,2);
    2518         756 :     a1 = gel(v,3);
    2519             :   }
    2520         805 :   if (!equalii(aF, bF)) { set_avma(av); return gen_0; }
    2521         588 :   b0 = Z_radical(b1, &omb0);
    2522         588 :   b2 = diviiexact(b1, b0);
    2523         588 :   A = dvmdii(a1, b2, &r);
    2524         588 :   if (r != gen_0) { set_avma(av); return gen_0; }
    2525         588 :   B = gcdii(A,b0); faB = Z_factor(B); /* squarefree */
    2526         588 :   S = eulerphi(mkvec2(B,faB));
    2527         588 :   if (odd(omb0 + lg(gel(faB,1))-1)) S = negi(S); /* moebius(b0/B) * phi(B) */
    2528         588 :   S = mulii(S, mulii(aF,b2));
    2529         588 :   tau = znchargauss_i(GF, chi, bitprec);
    2530         588 :   u = Fp_div(b0, A, F);
    2531         588 :   if (!equali1(u))
    2532             :   {
    2533         252 :     GEN ord = zncharorder(GF, chi), z = rootsof1_cx(ord, prec);
    2534         252 :     tau = gmul(tau, znchareval(GF, chi, u, mkvec2(z,ord)));
    2535             :   }
    2536         588 :   return gerepileupto(av, gmul(tau, S));
    2537             : }

Generated by: LCOV version 1.13