Code coverage tests

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

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

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

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

Generated by: LCOV version 1.13