Code coverage tests

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

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

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

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

Generated by: LCOV version 1.13