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 25406-bf255ab81b) Lines: 1455 1512 96.2 %
Date: 2020-06-04 05:59:24 Functions: 156 157 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       10762 : mysercoeff(GEN x, long n)
      29             : {
      30       10762 :   long N = n - valp(x);
      31       10762 :   return (N < 0)? gen_0: gel(x, N+2);
      32             : }
      33             : 
      34             : long
      35       12222 : ldata_get_type(GEN ldata) { return mael3(ldata, 1, 1, 1); }
      36             : 
      37             : GEN
      38       32900 : ldata_get_an(GEN ldata) { return gel(ldata, 1); }
      39             : 
      40             : GEN
      41       31962 : ldata_get_dual(GEN ldata) { return gel(ldata, 2); }
      42             : 
      43             : long
      44        1895 : ldata_isreal(GEN ldata) { return isintzero(gel(ldata, 2)); }
      45             : 
      46             : GEN
      47      210736 : ldata_get_gammavec(GEN ldata) { return gel(ldata, 3); }
      48             : 
      49             : long
      50       13193 : ldata_get_degree(GEN ldata) { return lg(gel(ldata, 3))-1; }
      51             : 
      52             : GEN
      53       92113 : ldata_get_k(GEN ldata)
      54             : {
      55       92113 :   GEN w = gel(ldata,4);
      56       92113 :   if (typ(w) == t_VEC) w = gel(w,1);
      57       92113 :   return w;
      58             : }
      59             : 
      60             : /* a_n = O(n^{k1 + epsilon}) */
      61             : GEN
      62          42 : ldata_get_k1(GEN ldata)
      63             : {
      64          42 :   GEN w = gel(ldata,4);
      65          42 :   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          42 :   w = gaddgs(w,-1);
      68          42 :   return ldata_get_residue(ldata)? w: gmul2n(w, -1);
      69             : }
      70             : 
      71             : /* a_n = O(n^{k1 + epsilon}) */
      72             : static double
      73       65581 : ldata_get_k1_dbl(GEN ldata)
      74             : {
      75       65581 :   GEN w = gel(ldata,4);
      76             :   double k;
      77       65581 :   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       64356 :   k = gtodouble(w);
      80       64356 :   return ldata_get_residue(ldata)? k-1: (k-1)/2.;
      81             : }
      82             : 
      83             : GEN
      84      150799 : ldata_get_conductor(GEN ldata) { return gel(ldata, 5); }
      85             : 
      86             : GEN
      87       50970 : ldata_get_rootno(GEN ldata) { return gel(ldata, 6); }
      88             : 
      89             : GEN
      90      115032 : ldata_get_residue(GEN ldata) { return lg(ldata) == 7 ? NULL: gel(ldata, 7); }
      91             : 
      92             : long
      93       86256 : linit_get_type(GEN linit) { return mael(linit, 1, 1); }
      94             : 
      95             : GEN
      96      128389 : linit_get_ldata(GEN linit) { return gel(linit, 2); }
      97             : 
      98             : GEN
      99      161595 : linit_get_tech(GEN linit) { return gel(linit, 3); }
     100             : 
     101             : long
     102      151752 : is_linit(GEN data)
     103             : {
     104      104050 :   return lg(data) == 4 && typ(data) == t_VEC
     105      255802 :                        && typ(gel(data, 1)) == t_VECSMALL;
     106             : }
     107             : 
     108             : GEN
     109       21031 : lfun_get_step(GEN tech) { return gmael(tech, 2, 1);}
     110             : 
     111             : GEN
     112       21031 : lfun_get_pol(GEN tech) { return gmael(tech, 2, 2);}
     113             : 
     114             : GEN
     115        5010 : lfun_get_Residue(GEN tech) { return gmael(tech, 2, 3);}
     116             : 
     117             : GEN
     118       33237 : 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        4858 : lfun_get_factgammavec(GEN tech) { return gmael(tech, 3, 4); }
     128             : 
     129             : /* Handle complex Vga whose sum is real */
     130             : static GEN
     131       71741 : sumVga(GEN Vga) { return real_i(vecsum(Vga)); }
     132             : /* sum_i max (Im v[i],0) */
     133             : static double
     134       17152 : sumVgaimpos(GEN v)
     135             : {
     136       17152 :   double d = 0.;
     137       17152 :   long i, l = lg(v);
     138       45807 :   for (i = 1; i < l; i++)
     139             :   {
     140       28655 :     GEN c = imag_i(gel(v,i));
     141       28655 :     if (gsigne(c) > 0) d += gtodouble(c);
     142             :   }
     143       17152 :   return d;
     144             : }
     145             : 
     146             : static long
     147       21637 : vgaell(GEN Vga)
     148             : {
     149       21637 :   long d = lg(Vga)-1;
     150             :   GEN c;
     151       21637 :   if (d != 2) return 0;
     152       15296 :   c = gsub(gel(Vga,1), gel(Vga,2));
     153       15296 :   return gequal1(c) || gequalm1(c);
     154             : }
     155             : int
     156       60574 : 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       10108 : antwist(GEN an, GEN Vga, long prec)
     160             : {
     161             :   long l, i;
     162       10108 :   GEN b, c = vecmin(Vga);
     163       10108 :   if (gequal0(c)) return an;
     164        1638 :   l = lg(an); b = cgetg(l, t_VEC);
     165        1638 :   if (gequal1(c))
     166             :   {
     167         931 :     if (typ(an) == t_VECSMALL)
     168        8449 :       for (i = 1; i < l; i++) gel(b,i) = mulss(an[i], i);
     169             :     else
     170       15190 :       for (i = 1; i < l; i++) gel(b,i) = gmulgs(gel(an,i), i);
     171             :   }
     172             :   else
     173             :   {
     174         707 :     GEN v = vecpowug(l-1, c, prec);
     175         707 :     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       19985 :       for (i = 1; i < l; i++) gel(b,i) = gmul(gel(an,i), gel(v,i));
     179             :   }
     180        1638 :   return b;
     181             : }
     182             : 
     183             : static GEN
     184        5488 : theta_dual(GEN theta, GEN bn)
     185             : {
     186        5488 :   if (typ(bn)==t_INT) return NULL;
     187             :   else
     188             :   {
     189          49 :     GEN thetad = shallowcopy(theta), ldata = linit_get_ldata(theta);
     190          49 :     GEN Vga = ldata_get_gammavec(ldata);
     191          49 :     GEN tech = shallowcopy(linit_get_tech(theta));
     192          49 :     GEN an = theta_get_an(tech);
     193          49 :     long prec = nbits2prec(theta_get_bitprec(tech));
     194          49 :     GEN vb = ldata_vecan(bn, lg(an)-1, prec);
     195          49 :     if (!theta_get_m(tech) && Vgaeasytheta(Vga)) vb = antwist(vb, Vga, prec);
     196          49 :     gel(tech,1) = vb;
     197          49 :     gel(thetad,3) = tech; return thetad;
     198             :   }
     199             : }
     200             : 
     201             : static GEN
     202       36527 : 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       21586 : domain_get_bitprec(GEN domain)  { return mael2(domain, 2, 2); }
     207             : GEN
     208       37031 : 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        1790 : lfunprod_get_fact(GEN tech)  { return gel(tech, 2); }
     216             : 
     217             : GEN
     218       42206 : theta_get_an(GEN tdata)      { return gel(tdata, 1);}
     219             : GEN
     220        5515 : theta_get_K(GEN tdata)       { return gel(tdata, 2);}
     221             : GEN
     222        5265 : theta_get_R(GEN tdata)       { return gel(tdata, 3);}
     223             : long
     224       54635 : theta_get_bitprec(GEN tdata) { return itos(gel(tdata, 4));}
     225             : long
     226       82901 : theta_get_m(GEN tdata)       { return itos(gel(tdata, 5));}
     227             : GEN
     228       43466 : theta_get_tdom(GEN tdata)    { return gel(tdata, 6);}
     229             : GEN
     230       47582 : 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       13769 : isnegint(GEN s)
     240             : {
     241       13769 :   GEN r = ground(real_i(s));
     242       13769 :   if (signe(r) <= 0 && gequal(s, r)) return -itos(r);
     243       13727 :   return -1;
     244             : }
     245             : 
     246             : /* r/x + O(1), r != 0 */
     247             : static GEN
     248        3626 : serpole(GEN r)
     249             : {
     250        3626 :   GEN s = cgetg(3, t_SER);
     251        3626 :   s[1] = evalsigne(1)|evalvalp(-1)|evalvarn(0);
     252        3626 :   gel(s,2) = r; return s;
     253             : }
     254             : /* a0 +  a1 x + O(x^e), e >= 0 */
     255             : static GEN
     256        5348 : deg1ser_shallow(GEN a1, GEN a0, long v, long e)
     257        5348 : { return RgX_to_ser(deg1pol_shallow(a1, a0, v), e+2); }
     258             : 
     259             : /* pi^(-s/2) Gamma(s/2) */
     260             : static GEN
     261        7798 : gamma_R(GEN s, long prec)
     262             : {
     263        7798 :   GEN s2 = gmul2n(s, -1);
     264        7798 :   long ms = isnegint(s2);
     265        7798 :   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        7756 :   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        5971 : gamma_C(GEN s, long prec)
     275             : {
     276        5971 :   long ms = isnegint(s);
     277        5971 :   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        5971 :   return gmul2n(gdiv(ggamma(s,prec), pow2Pis(s,prec)), 1);
     283             : }
     284             : 
     285             : static GEN
     286        1519 : gammafrac(GEN r, long d)
     287             : {
     288        1519 :   long i, l = labs(d) + 1, j = (d > 0)? 0: 2*d;
     289        1519 :   GEN T, v = cgetg(l, t_COL);
     290        3059 :   for (i = 1; i < l; i++, j += 2)
     291        1540 :     gel(v,i) = deg1pol_shallow(gen_1, gaddgs(r, j), 0);
     292        1519 :   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       14070 : gammafactor(GEN Vga)
     306             : {
     307       14070 :   long i, r, c, l = lg(Vga);
     308       14070 :   GEN v, P, a, b, e, f, E, F = cgetg(l, t_VEC), R = gen_1;
     309       38703 :   for (i = 1; i < l; ++i)
     310             :   {
     311       24633 :     GEN a = gel(Vga,i), r = gmul2n(real_i(a), -1);
     312       24633 :     long q = itos(gfloor(r)); /* [Re a/2] */
     313       24633 :     r = gmul2n(gsubgs(r, q), 1);
     314       24633 :     gel(F,i) = gadd(r, imag_i(a)); /* 2{Re a/2} + Im a/2 */
     315       24633 :     if (q) R = gmul(R, gammafrac(gel(F,i), q));
     316             :   }
     317       14070 :   E = RgV_count(&F); l = lg(E);
     318       14070 :   v = cgetg(l, t_VEC);
     319       34888 :   for (i = 1; i < l; i++) gel(v,i) = mkvec2(gfrac(gel(F,i)), stoi(E[i]));
     320       14070 :   gen_sort_inplace(v, (void*)cmp_universal, cmp_nodata, &P);
     321       14070 :   a = cgetg(l, t_VEC); e = cgetg(l, t_VECSMALL);
     322       14070 :   b = cgetg(l, t_VEC); f = cgetg(l, t_VECSMALL);
     323       28889 :   for (i = r = c = 1; i < l;)
     324       14819 :     if (i==l-1 || cmp_universal(gel(v,i), gel(v,i+1)))
     325        8820 :     { gel(a, r) = gel(F, P[i]); e[r++] = E[P[i]]; i++; }
     326             :     else
     327        5999 :     { gel(b, c) = gel(F, P[i]); f[c++] = E[P[i]]; i+=2; }
     328       14070 :   setlg(a, r); setlg(e, r);
     329       14070 :   setlg(b, c); setlg(f, c); return mkvec3(R, mkvec2(a,e), mkvec2(b,f));
     330             : }
     331             : 
     332             : static GEN
     333        3346 : polgammaeval(GEN F, GEN s)
     334             : {
     335        3346 :   GEN r = poleval(F, s);
     336        3346 :   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        3346 :   return r;
     348             : }
     349             : static long
     350        1673 : rfrac_degree(GEN R)
     351             : {
     352        1673 :   GEN a = gel(R,1), b = gel(R,2);
     353        1673 :   return ((typ(a) == t_POL)? degpol(a): 0) - degpol(b);
     354             : }
     355             : static GEN
     356       12978 : fracgammaeval(GEN F, GEN s, long prec)
     357             : {
     358       12978 :   GEN R = gel(F,1);
     359             :   long d;
     360       12978 :   switch(typ(R))
     361             :   {
     362           0 :     case t_POL:
     363           0 :       d = degpol(R);
     364           0 :       R = polgammaeval(R, s); break;
     365        1673 :     case t_RFRAC:
     366        1673 :       d = rfrac_degree(R);
     367        1673 :       R = gdiv(polgammaeval(gel(R,1), s), polgammaeval(gel(R,2), s)); break;
     368       11305 :     default: return R;
     369             :   }
     370        1673 :   return gmul(R, powrs(Pi2n(1,prec), -d));
     371             : }
     372             : 
     373             : static GEN
     374       12978 : gammafactproduct(GEN F, GEN s, long prec)
     375             : {
     376       12978 :   pari_sp av = avma;
     377       12978 :   GEN R = gel(F,2), Rw = gel(R,1), Re = gel(R,2);
     378       12978 :   GEN C = gel(F,3), Cw = gel(C,1), Ce = gel(C,2), z = fracgammaeval(F, s, prec);
     379       12978 :   long i, lR = lg(Rw), lC = lg(Cw);
     380       20776 :   for (i = 1; i < lR; i++)
     381        7798 :     z = gmul(z, gpowgs(gamma_R(gadd(s,gel(Rw, i)), prec), Re[i]));
     382       18949 :   for (i = 1; i < lC; i++)
     383        5971 :     z = gmul(z, gpowgs(gamma_C(gadd(s,gel(Cw, i)), prec), Ce[i]));
     384       12978 :   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       65574 : 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      103599 : get_cone(GEN t, double *r, double *a)
     411             : {
     412      103599 :   const long prec = LOWDEFAULTPREC;
     413      103599 :   if (typ(t) == t_COMPLEX)
     414             :   {
     415       15883 :     t  = gprec_w(t, prec);
     416       15883 :     *r = gtodouble(gabs(t, prec));
     417       15883 :     *a = fabs(gtodouble(garg(t, prec)));
     418             :   }
     419             :   else
     420             :   {
     421       87716 :     *r = fabs(gtodouble(t));
     422       87716 :     *a = 0.;
     423             :   }
     424      103599 :   if (!*r && !*a) pari_err_DOMAIN("lfunthetainit","t","=",gen_0,t);
     425      103592 : }
     426             : /* slightly larger cone than necessary, to avoid round-off problems */
     427             : static void
     428       60133 : get_cone_fuzz(GEN t, double *r, double *a)
     429       60133 : { 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       48429 : lfunthetacost(GEN ldata, GEN tdom, long m, long bitprec)
     439             : {
     440       48429 :   pari_sp av = avma;
     441       48429 :   GEN Vga = ldata_get_gammavec(ldata);
     442       48429 :   long d = lg(Vga)-1;
     443       48429 :   long k1 = ldata_get_k1_dbl(ldata);
     444       48429 :   double c = d/2., a, A, B, logC, al, rho, T;
     445       48429 :   double N = gtodouble(ldata_get_conductor(ldata));
     446             : 
     447       48429 :   if (!N) pari_err_TYPE("lfunthetaneed [missing conductor]", ldata);
     448       48429 :   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       48422 :     get_cone_fuzz(tdom, &rho, &al);
     455       48422 :   A = gammavec_expo(d, gtodouble(sumVga(Vga))); set_avma(av);
     456       48422 :   a = (A+k1+1) + (m-1)/c;
     457       48422 :   if (fabs(a) < 1e-10) a = 0.;
     458       48422 :   logC = c*M_LN2 - log(c)/2;
     459             :   /* +1: fudge factor */
     460       48422 :   B = M_LN2*bitprec+logC+m*log(2*M_PI) + 1 + (k1+1)*log(N)/2 - (k1+m+1)*log(rho);
     461       48422 :   if (al)
     462             :   { /* t = rho e^(i*al), T^(1/c) = Re(t^(1/c)) > 0, T = rho cos^c(al/c) */
     463        7945 :     double z = cos(al/c);
     464        7945 :     T = (d == 2 && typ(tdom) != t_VEC)? gtodouble(real_i(tdom)): rho*pow(z,c);
     465        7945 :     if (z <= 0)
     466           0 :       pari_err_DOMAIN("lfunthetaneed", "arg t", ">", dbltor(c*M_PI/2), tdom);
     467        7945 :     B -= log(z) * (c * (k1+A+1) + m);
     468             :   }
     469             :   else
     470       40477 :     T = rho;
     471       48422 :   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        5215 : fracgammadegree(GEN FVga)
     494        5215 : { 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        3514 : rtoR(GEN a, GEN r, GEN FVga, GEN N, long prec)
     507             : {
     508        3514 :   long v = lg(r)-2;
     509        3514 :   GEN as = deg1ser_shallow(gen_1, a, varn(r), v);
     510        3514 :   GEN Na = gpow(N, gdivgs(as, 2), prec);
     511        3514 :   long d = fracgammadegree(FVga);
     512        3514 :   if (d) as = sertoser(as, v+d); /* make up for a possible loss of accuracy */
     513        3514 :   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        3437 : lfunrtopoles(GEN r)
     519             : {
     520        3437 :   long j, l = lg(r);
     521        3437 :   GEN v = cgetg(l, t_VEC);
     522        7105 :   for (j = 1; j < l; j++)
     523             :   {
     524        3668 :     GEN rj = gel(r,j), a = gel(rj,1);
     525        3668 :     gel(v,j) = a;
     526             :   }
     527        3437 :   gen_sort_inplace(v, (void*)&cmp_universal, cmp_nodata, NULL);
     528        3437 :   return v;
     529             : }
     530             : 
     531             : /* r / x + O(1) */
     532             : static GEN
     533        3584 : simple_pole(GEN r)
     534        3584 : { return isintzero(r)? gen_0: serpole(r); }
     535             : static GEN
     536        4312 : normalize_simple_pole(GEN r, GEN k)
     537             : {
     538        4312 :   long tx = typ(r);
     539        4312 :   if (is_vec_t(tx)) return r;
     540        3584 :   if (!is_scalar_t(tx)) pari_err_TYPE("lfunrootres [poles]", r);
     541        3584 :   return mkvec(mkvec2(k, simple_pole(r)));
     542             : }
     543             : /* normalize the description of a polar part */
     544             : static GEN
     545        3976 : normalizepoles(GEN r, GEN k)
     546             : {
     547             :   long iv, j, l;
     548             :   GEN v;
     549        3976 :   if (!is_vec_t(typ(r))) return normalize_simple_pole(r, k);
     550        1638 :   v = cgetg_copy(r, &l);
     551        4179 :   for (j = iv = 1; j < l; j++)
     552             :   {
     553        2541 :     GEN rj = gel(r,j), a = gel(rj,1), ra = gel(rj,2);
     554        2541 :     if (!is_scalar_t(typ(a)) || typ(ra) != t_SER)
     555           0 :       pari_err_TYPE("lfunrootres [poles]",r);
     556        2541 :     if (valp(ra) >= 0) continue;
     557        2527 :     gel(v,iv++) = rj;
     558             :   }
     559        1638 :   setlg(v, iv); return v;
     560             : }
     561             : static int
     562        6300 : residues_known(GEN r)
     563             : {
     564        6300 :   long i, l = lg(r);
     565        6300 :   if (isintzero(r)) return 0;
     566        6174 :   if (!is_vec_t(typ(r))) return 1;
     567        8316 :   for (i = 1; i < l; i++)
     568             :   {
     569        5124 :     GEN ri = gel(r,i);
     570        5124 :     if (!is_vec_t(typ(ri)) || lg(ri)!=3)
     571           0 :       pari_err_TYPE("lfunrootres [poles]",r);
     572        5124 :     if (isintzero(gel(ri, 2))) return 0;
     573             :   }
     574        3192 :   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       13538 : lfunrtoR_i(GEN ldata, GEN r, GEN eno, long prec)
     581             : {
     582       13538 :   GEN Vga = ldata_get_gammavec(ldata), N = ldata_get_conductor(ldata);
     583             :   GEN R, vr, FVga;
     584       13538 :   pari_sp av = avma;
     585             :   long lr, j, jR;
     586       13538 :   GEN k = ldata_get_k(ldata);
     587             : 
     588       13538 :   if (!r || isintzero(eno) || !residues_known(r))
     589        9562 :     return gen_0;
     590        3976 :   r = normalizepoles(r, k);
     591        3976 :   if (typ(r) == t_COL) return gerepilecopy(av, r);
     592        3297 :   if (typ(ldata_get_dual(ldata)) != t_INT)
     593           0 :     pari_err(e_MISC,"please give the Taylor development of Lambda");
     594        3297 :   vr = lfunrtopoles(r); lr = lg(vr);
     595        3297 :   FVga = gammafactor(Vga);
     596        3297 :   R = cgetg(2*lr, t_COL);
     597        6811 :   for (j = jR = 1; j < lr; j++)
     598             :   {
     599        3514 :     GEN rj = gel(r,j), a = gel(rj,1), ra = gel(rj,2);
     600        3514 :     GEN Ra = rtoR(a, ra, FVga, N, prec);
     601        3514 :     GEN b = gsub(k, conj_i(a));
     602        3514 :     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        3514 :     gel(R,jR++) = mkvec2(a, Ra);
     606        3514 :     if (!tablesearch(vr, b, (int (*)(GEN,GEN))&cmp_universal))
     607             :     {
     608        3339 :       GEN mX = gneg(pol_x(varn(Ra)));
     609        3339 :       GEN Rb = gmul(eno, gsubst(conj_i(Ra), varn(Ra), mX));
     610        3339 :       gel(R,jR++) = mkvec2(b, Rb);
     611             :     }
     612             :   }
     613        3297 :   setlg(R, jR); return gerepilecopy(av, R);
     614             : }
     615             : static GEN
     616       13286 : lfunrtoR_eno(GEN ldata, GEN eno, long prec)
     617       13286 : { return lfunrtoR_i(ldata, ldata_get_residue(ldata), eno, prec); }
     618             : static GEN
     619       11718 : lfunrtoR(GEN ldata, long prec)
     620       11718 : { 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       11718 : lfunthetainit0(GEN ldata, GEN tdom, GEN an2, long m,
     625             :     long bitprec, long extrabit)
     626             : {
     627       11718 :   long prec = nbits2prec(bitprec);
     628       11718 :   GEN tech, N = ldata_get_conductor(ldata);
     629       11718 :   GEN K = gammamellininvinit(ldata, m, bitprec + extrabit);
     630       11718 :   GEN R = lfunrtoR(ldata, prec);
     631       11718 :   if (!tdom) tdom = gen_1;
     632       11718 :   if (typ(tdom) != t_VEC)
     633             :   {
     634             :     double r, a;
     635       11711 :     get_cone_fuzz(tdom, &r, &a);
     636       11711 :     tdom = mkvec2(dbltor(r), a? dbltor(a): gen_0);
     637             :   }
     638       11718 :   tech = mkvecn(7, an2,K,R, stoi(bitprec), stoi(m), tdom,
     639             :                    gsqrt(ginv(N), prec + EXTRAPRECWORD));
     640       11718 :   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        6279 : lfunthetainit_i(GEN data, GEN tdom, long m, long bitprec)
     647             : {
     648        6279 :   GEN ldata = lfunmisc_to_ldata_shallow(data);
     649        6279 :   long L = lfunthetacost(ldata, tdom, m, bitprec), prec = nbits2prec(bitprec);
     650        6272 :   GEN ldatan = ldata_newprec(ldata, prec);
     651        6272 :   GEN an = ldata_vecan(ldata_get_an(ldatan), L, prec);
     652        6272 :   GEN Vga = ldata_get_gammavec(ldatan);
     653        6272 :   if (m == 0 && Vgaeasytheta(Vga)) an = antwist(an, Vga, prec);
     654        6272 :   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         973 : lfunan(GEN ldata, long L, long prec)
     667             : {
     668         973 :   pari_sp av = avma;
     669             :   GEN an ;
     670         973 :   ldata = ldata_newprec(lfunmisc_to_ldata_shallow(ldata), prec);
     671         973 :   an = gerepilecopy(av, ldata_vecan(ldata_get_an(ldata), L, prec));
     672         966 :   if (typ(an) != t_VEC) an = vecsmall_to_vec_inplace(an);
     673         966 :   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       20576 : gmulvec(GEN x, GEN y)
     686             : {
     687       20576 :   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       20394 :     return gmul(x,y);
     691             : }
     692             : static GEN
     693        5460 : gdivvec(GEN x, GEN y)
     694             : {
     695        5460 :   if (is_vec_t(typ(x)) && is_vec_t(typ(y)))
     696         483 :     pari_APPLY_same(gdiv(gel(x,i),gel(y,i)))
     697             :   else
     698        5299 :     return gdiv(x,y);
     699             : }
     700             : 
     701             : static GEN
     702        2590 : gsubvec(GEN x, GEN y)
     703             : {
     704        2590 :   if (is_vec_t(typ(x)) && !is_vec_t(typ(y)))
     705           0 :     pari_APPLY_same(gsub(gel(x,i),y))
     706             :   else
     707        2590 :     return gsub(x,y);
     708             : }
     709             : 
     710             : /* [1^B,...,N^B] */
     711             : GEN
     712         252 : vecpowuu(long N, ulong B)
     713             : {
     714             :   GEN v;
     715             :   long p, i;
     716             :   forprime_t T;
     717             : 
     718         252 :   if (B <= 2)
     719             :   {
     720          70 :     if (!B) return const_vec(N,gen_1);
     721          63 :     v = cgetg(N+1, t_VEC); if (N == 0) return v;
     722          63 :     gel(v,1) = gen_1;
     723          63 :     if (B == 1)
     724        3787 :       for (i = 2; i <= N; i++) gel(v,i) = utoipos(i);
     725             :     else
     726         644 :       for (i = 2; i <= N; i++) gel(v,i) = sqru(i);
     727          63 :     return v;
     728             :   }
     729         182 :   v = const_vec(N, NULL);
     730         182 :   u_forprime_init(&T, 3, N);
     731        3934 :   while ((p = u_forprime_next(&T)))
     732             :   {
     733             :     long m, pk, oldpk;
     734        3752 :     gel(v,p) = powuu(p, B);
     735        8365 :     for (pk = p, oldpk = p; pk; oldpk = pk, pk = umuluu_le(pk,p,N))
     736             :     {
     737        4613 :       if (pk != p) gel(v,pk) = mulii(gel(v,oldpk), gel(v,p));
     738       20797 :       for (m = N/pk; m > 1; m--)
     739       16184 :         if (gel(v,m) && m%p) gel(v, m*pk) = mulii(gel(v,m), gel(v,pk));
     740             :     }
     741             :   }
     742         182 :   gel(v,1) = gen_1;
     743        7602 :   for (i = 2; i <= N; i+=2)
     744             :   {
     745        7420 :     long vi = vals(i);
     746        7420 :     gel(v,i) = shifti(gel(v,i >> vi), B * vi);
     747             :   }
     748         182 :   return v;
     749             : }
     750             : /* [1^B,...,N^B] */
     751             : GEN
     752       10530 : vecpowug(long N, GEN B, long prec)
     753             : {
     754       10530 :   GEN v = const_vec(N, NULL);
     755       10530 :   long p, eB = gexpo(B);
     756       10530 :   long prec0 = eB < 5? prec: prec + nbits2extraprec(eB);
     757             :   forprime_t T;
     758       10530 :   u_forprime_init(&T, 2, N);
     759       10530 :   gel(v,1) = gen_1;
     760      353937 :   while ((p = u_forprime_next(&T)))
     761             :   {
     762             :     long m, pk, oldpk;
     763      343407 :     gel(v,p) = gpow(utor(p,prec0), B, prec);
     764      343407 :     if (prec0 != prec) gel(v,p) = gprec_wtrunc(gel(v,p), prec);
     765      767353 :     for (pk = p, oldpk = p; pk; oldpk = pk, pk = umuluu_le(pk,p,N))
     766             :     {
     767      423946 :       if (pk != p) gel(v,pk) = gmul(gel(v,oldpk), gel(v,p));
     768     6077056 :       for (m = N/pk; m > 1; m--)
     769     5653110 :         if (gel(v,m) && m%p) gel(v, m*pk) = gmul(gel(v,m), gel(v,pk));
     770             :     }
     771             :   }
     772       10530 :   return v;
     773             : }
     774             : 
     775             : GEN
     776         203 : dirpowers(long n, GEN x, long prec)
     777             : {
     778         203 :   pari_sp av = avma;
     779             :   GEN v;
     780         203 :   if (n <= 0) return cgetg(1, t_VEC);
     781         189 :   if (typ(x) == t_INT && lgefint(x) <= 3 && signe(x) >= 0)
     782           7 :   {
     783          28 :     ulong B = itou(x);
     784          28 :     v = vecpowuu(n, B);
     785          28 :     if (B <= 2) return v;
     786             :   }
     787         161 :   else v = vecpowug(n, x, prec);
     788         168 :   return gerepilecopy(av, v);
     789             : }
     790             : 
     791             : /* return [1^(2/d), 2^(2/d),...,lim^(2/d)] */
     792             : static GEN
     793        5515 : mkvroots(long d, long lim, long prec)
     794             : {
     795        5515 :   if (d <= 4)
     796             :   {
     797        5361 :     GEN v = cgetg(lim+1,t_VEC);
     798             :     long n;
     799        5361 :     switch(d)
     800             :     {
     801        2051 :       case 1:
     802       36991 :         for (n=1; n <= lim; n++) gel(v,n) = sqru(n);
     803        2051 :         return v;
     804         945 :       case 2:
     805      170401 :         for (n=1; n <= lim; n++) gel(v,n) = utoipos(n);
     806         945 :         return v;
     807        1397 :       case 4:
     808     3630636 :         for (n=1; n <= lim; n++) gel(v,n) = sqrtr(utor(n, prec));
     809        1397 :         return v;
     810             :     }
     811             :   }
     812        1122 :   return vecpowug(lim, gdivgs(gen_2,d), prec);
     813             : }
     814             : 
     815             : GEN
     816       47750 : lfunthetacheckinit(GEN data, GEN t, long m, long bitprec)
     817             : {
     818       47750 :   if (is_linit(data) && linit_get_type(data)==t_LDESC_THETA)
     819             :   {
     820       43466 :     GEN tdom, thetainit = linit_get_tech(data);
     821       43466 :     long bitprecnew = theta_get_bitprec(thetainit);
     822       43466 :     long m0 = theta_get_m(thetainit);
     823             :     double r, al, rt, alt;
     824       43466 :     if (m0 != m)
     825           0 :       pari_err_DOMAIN("lfuntheta","derivative order","!=", stoi(m),stoi(m0));
     826       43466 :     if (bitprec > bitprecnew) goto INIT;
     827       43466 :     get_cone(t, &rt, &alt);
     828       43466 :     tdom = theta_get_tdom(thetainit);
     829       43466 :     r = gtodouble(gel(tdom,1));
     830       43466 :     al= gtodouble(gel(tdom,2)); if (rt >= r && alt <= al) return data;
     831             :   }
     832        4284 : INIT:
     833        5887 :   return lfunthetainit_i(data, t, m, bitprec);
     834             : }
     835             : 
     836             : static GEN
     837     4410437 : get_an(GEN an, long n)
     838             : {
     839     4410437 :   if (typ(an) == t_VECSMALL) { long a = an[n]; if (a) return stoi(a); }
     840     4410437 :   else { GEN a = gel(an,n); if (a && !gequal0(a)) return a; }
     841     3211907 :   return NULL;
     842             : }
     843             : /* x * an[n] */
     844             : static GEN
     845    16188937 : mul_an(GEN an, long n, GEN x)
     846             : {
     847    16188937 :   if (typ(an) == t_VECSMALL) { long a = an[n]; if (a) return gmulsg(a,x); }
     848    10792611 :   else { GEN a = gel(an,n); if (a && !gequal0(a)) return gmul(a,x); }
     849     7378824 :   return NULL;
     850             : }
     851             : /* 2*t^a * x **/
     852             : static GEN
     853      177760 : mulT(GEN t, GEN a, GEN x, long prec)
     854             : {
     855      177760 :   if (gequal0(a)) return gmul2n(x,1);
     856       17434 :   return gmul(x, gmul2n(gequal1(a)? t: gpow(t,a,prec), 1));
     857             : }
     858             : 
     859             : static GEN
     860    24995422 : vecan_cmul(void *E, GEN P, long a, GEN x)
     861             : {
     862             :   (void)E;
     863    24995422 :   if (typ(P) == t_VECSMALL)
     864    21097830 :     return (a==0 || !P[a])? NULL: gmulsg(P[a], x);
     865             :   else
     866     3897592 :     return (a==0 || !gel(P,a))? NULL: gmul(gel(P,a), x);
     867             : }
     868             : /* d=2, 2 sum_{n <= N} a(n) (n t)^al q^n, q = exp(-2pi t),
     869             :  * an2[n] = a(n) * n^al */
     870             : static GEN
     871      142987 : theta2_i(GEN an2, long N, GEN t, GEN al, long prec)
     872             : {
     873      142987 :   GEN S, q, pi2 = Pi2n(1,prec);
     874      142985 :   const struct bb_algebra *alg = get_Rg_algebra();
     875      142983 :   setsigne(pi2,-1); q = gexp(gmul(pi2, t), prec);
     876             :   /* Brent-Kung in case the a_n are small integers */
     877      142972 :   S = gen_bkeval(an2, N, q, 1, NULL, alg, vecan_cmul);
     878      142973 :   return mulT(t, al, S, prec);
     879             : }
     880             : static GEN
     881      138388 : theta2(GEN an2, long N, GEN t, GEN al, long prec)
     882             : {
     883      138388 :   pari_sp av = avma;
     884      138388 :   return gerepileupto(av, theta2_i(an2, N, t, al, prec));
     885             : }
     886             : 
     887             : /* d=1, 2 sum_{n <= N} a_n (n t)^al q^(n^2), q = exp(-pi t^2),
     888             :  * an2[n] is a_n n^al */
     889             : static GEN
     890       34786 : theta1(GEN an2, long N, GEN t, GEN al, long prec)
     891             : {
     892       34786 :   GEN q = gexp(gmul(negr(mppi(prec)), gsqr(t)), prec);
     893       34786 :   GEN vexp = gsqrpowers(q, N), S = gen_0;
     894       34786 :   pari_sp av = avma;
     895             :   long n;
     896     6735711 :   for (n = 1; n <= N; n++)
     897             :   {
     898     6700925 :     GEN c = mul_an(an2, n, gel(vexp,n));
     899     6700925 :     if (c)
     900             :     {
     901     5687490 :       S = gadd(S, c);
     902     5687490 :       if (gc_needed(av, 3)) S = gerepileupto(av, S);
     903             :     }
     904             :   }
     905       34786 :   return mulT(t, al, S, prec);
     906             : }
     907             : 
     908             : /* If m > 0, compute m-th derivative of theta(t) = theta0(t/sqrt(N))
     909             :  * with absolute error 2^-bitprec; theta(t)=\sum_{n\ge1}a(n)K(nt/N^(1/2)) */
     910             : GEN
     911       42143 : lfuntheta(GEN data, GEN t, long m, long bitprec)
     912             : {
     913       42143 :   pari_sp ltop = avma;
     914             :   long limt, d;
     915             :   GEN isqN, vecan, Vga, ldata, theta, thetainit, S;
     916       42143 :   long n, prec = nbits2prec(bitprec);
     917       42143 :   t = gprec_w(t, prec);
     918       42143 :   theta = lfunthetacheckinit(data, t, m, bitprec);
     919       42136 :   ldata = linit_get_ldata(theta);
     920       42136 :   thetainit = linit_get_tech(theta);
     921       42136 :   vecan = theta_get_an(thetainit);
     922       42136 :   isqN = theta_get_isqrtN(thetainit);
     923       42136 :   limt = lg(vecan)-1;
     924       42136 :   if (theta == data)
     925       40435 :     limt = minss(limt, lfunthetacost(ldata, t, m, bitprec));
     926       42136 :   if (!limt)
     927             :   {
     928          14 :     set_avma(ltop); S = real_0_bit(-bitprec);
     929          14 :     if (!is_real_t(typ(t)) || !ldata_isreal(ldata))
     930           7 :       S = gerepilecopy(ltop, mkcomplex(S,S));
     931          14 :     return S;
     932             :   }
     933       42122 :   t = gmul(t, isqN);
     934       42122 :   Vga = ldata_get_gammavec(ldata);
     935       42122 :   d = lg(Vga)-1;
     936       42122 :   if (m == 0 && Vgaeasytheta(Vga))
     937             :   {
     938       39386 :     if (theta_get_m(thetainit) > 0) vecan = antwist(vecan, Vga, prec);
     939       43986 :     if (d == 1) S = theta1(vecan, limt, t, gel(Vga,1), prec);
     940        4600 :     else        S = theta2_i(vecan, limt, t, vecmin(Vga), prec);
     941             :   }
     942             :   else
     943             :   {
     944        2736 :     GEN K = theta_get_K(thetainit);
     945        2736 :     GEN vroots = mkvroots(d, limt, prec);
     946             :     pari_sp av;
     947        2736 :     t = gpow(t, gdivgs(gen_2,d), prec);
     948        2736 :     S = gen_0; av = avma;
     949     4413173 :     for (n = 1; n <= limt; ++n)
     950             :     {
     951     4410437 :       GEN nt, an = get_an(vecan, n);
     952     4410437 :       if (!an) continue;
     953     1198530 :       nt = gmul(gel(vroots,n), t);
     954     1198530 :       if (m) an = gmul(an, powuu(n, m));
     955     1198530 :       S = gadd(S, gmul(an, gammamellininvrt(K, nt, bitprec)));
     956     1198530 :       if ((n & 0x1ff) == 0) S = gerepileupto(av, S);
     957             :     }
     958        2736 :     if (m) S = gmul(S, gpowgs(isqN, m));
     959             :   }
     960       42122 :   return gerepileupto(ltop, S);
     961             : }
     962             : 
     963             : /*******************************************************************/
     964             : /* Second part: Computation of L-Functions.                        */
     965             : /*******************************************************************/
     966             : 
     967             : struct lfunp {
     968             :   long precmax, Dmax, D, M, m0, nmax, d, vgaell;
     969             :   double k1, dc, dw, dh, MAXs, sub;
     970             :   GEN L, an, bn;
     971             : };
     972             : 
     973             : static void
     974       17152 : lfunparams(GEN ldata, long der, long bitprec, struct lfunp *S)
     975             : {
     976       17152 :   const long derprec = (der > 1)? dbllog2(mpfact(der)): 0; /* log2(der!) */
     977             :   GEN Vga, N, L, k;
     978             :   long k1, d, m, M, flag, nmax;
     979             :   double a, A, E, hd, Ep, d2, suma, maxs, mins, sub, B0,B1;
     980             :   double logN2, logC, Lestimate, Mestimate;
     981             : 
     982       17152 :   Vga = ldata_get_gammavec(ldata);
     983       17152 :   S->d = d = lg(Vga)-1; d2 = d/2.;
     984             : 
     985       17152 :   suma = gtodouble(sumVga(Vga));
     986       17152 :   k = ldata_get_k(ldata);
     987       17152 :   N = ldata_get_conductor(ldata);
     988       17152 :   logN2 = log(gtodouble(N)) / 2;
     989       17152 :   maxs = S->dc + S->dw;
     990       17152 :   mins = S->dc - S->dw;
     991       17152 :   S->MAXs = maxdd(maxs, gtodouble(k)-mins);
     992             : 
     993             :   /* we compute Lambda^(der)(s) / der!; need to compensate for L^(der)(s)
     994             :    * ln |gamma(s)| ~ -(pi/4) \sum_i |Im(s + a_i)|; max with 1: fudge factor */
     995       17152 :   a = (M_PI/(4*M_LN2))*(d*S->dh + sumVgaimpos(Vga));
     996       17152 :   S->D = (long)ceil(bitprec + derprec + maxdd(a, 1));
     997       17152 :   E = M_LN2*S->D; /* D:= required absolute bitprec */
     998             : 
     999       17152 :   Ep = E + maxdd(M_PI * S->dh * d2, (d*S->MAXs + suma - 1) * log(E));
    1000       17152 :   hd = d2*M_PI*M_PI / Ep;
    1001       17152 :   S->m0 = (long)ceil(M_LN2/hd);
    1002       17152 :   hd = M_LN2/S->m0;
    1003             : 
    1004       17152 :   logC = d2*M_LN2 - log(d2)/2;
    1005       17152 :   k1 = ldata_get_k1_dbl(ldata);
    1006       17152 :   S->k1 = k1; /* assume |a_n| << n^k1 with small implied constant */
    1007       17152 :   A = gammavec_expo(d, suma);
    1008             : 
    1009       17152 :   sub = 0.;
    1010       17152 :   if (mins > 1)
    1011             :   {
    1012        4487 :     GEN sig = dbltor(mins);
    1013        4487 :     sub += logN2*mins;
    1014        4487 :     if (gammaordinary(Vga, sig))
    1015             :     {
    1016        4396 :       GEN FVga = gammafactor(Vga);
    1017        4396 :       GEN gas = gammafactproduct(FVga, sig, LOWDEFAULTPREC);
    1018        4396 :       if (typ(gas) != t_SER)
    1019             :       {
    1020        4396 :         double dg = dbllog2(gas);
    1021        4396 :         if (dg > 0) sub += dg * M_LN2;
    1022             :       }
    1023             :     }
    1024             :   }
    1025       17152 :   S->sub = sub;
    1026       17152 :   M = 1000;
    1027       17152 :   L = cgetg(M+2, t_VECSMALL);
    1028       17152 :   a = S->k1 + A;
    1029             : 
    1030       17152 :   B0 = 5 + E - S->sub + logC + S->k1*logN2; /* 5 extra bits */
    1031       17152 :   B1 = hd * (S->MAXs - S->k1);
    1032       17152 :   Lestimate = dblcoro526(a + S->MAXs - 2./d, d/2.,
    1033       17152 :     E - S->sub + logC - log(2*M_PI*hd) + S->MAXs*logN2);
    1034       17152 :   Mestimate = ((Lestimate > 0? log(Lestimate): 0) + logN2) / hd;
    1035       17152 :   nmax = 0;
    1036       17152 :   flag = 0;
    1037       17152 :   for (m = 0;; m++)
    1038     1572205 :   {
    1039     1589357 :     double x, H = logN2 - m*hd, B = B0 + m*B1;
    1040             :     long n;
    1041     1589357 :     x = dblcoro526(a, d/2., B);
    1042     1589357 :     n = floor(x*exp(H));
    1043     1589357 :     if (n > nmax) nmax = n;
    1044     1589357 :     if (m > M) { M *= 2; L = vecsmall_lengthen(L,M+2); }
    1045     1589357 :     L[m+1] = n;
    1046     1589357 :     if (n == 0) { if (++flag > 2 && m > Mestimate) break; } else flag = 0;
    1047             :   }
    1048       17992 :   m -= 2; while (m > 0 && !L[m]) m--;
    1049       17152 :   if (m == 0) { nmax = 1; L[1] = 1; m = 1; } /* can happen for tiny bitprec */
    1050       17152 :   setlg(L, m+1); S->M = m-1;
    1051       17152 :   S->L = L;
    1052       17152 :   S->nmax = nmax;
    1053             : 
    1054       17152 :   S->Dmax = S->D + (long)ceil((S->M * hd * S->MAXs - S->sub) / M_LN2);
    1055       17152 :   if (S->Dmax < S->D) S->Dmax = S->D;
    1056       17152 :   S->precmax = nbits2prec(S->Dmax);
    1057       17152 :   if (DEBUGLEVEL > 1)
    1058           0 :     err_printf("Dmax=%ld, D=%ld, M = %ld, nmax = %ld, m0 = %ld\n",
    1059             :                S->Dmax,S->D,S->M,S->nmax, S->m0);
    1060       17152 : }
    1061             : 
    1062             : static GEN
    1063        5600 : lfuninit_pol(GEN v, GEN poqk, long prec)
    1064             : {
    1065        5600 :   long m, M = lg(v) - 2;
    1066        5600 :   GEN pol = cgetg(M+3, t_POL);
    1067        5600 :   pol[1] = evalsigne(1) | evalvarn(0);
    1068        5600 :   gel(pol, 2) = gprec_w(gmul2n(gel(v,1), -1), prec);
    1069        5600 :   if (poqk)
    1070      284677 :     for (m = 2; m <= M+1; m++)
    1071      279133 :       gel(pol, m+1) = gprec_w(gmul(gel(poqk,m), gel(v,m)), prec);
    1072             :   else
    1073        2324 :     for (m = 2; m <= M+1; m++)
    1074        2268 :       gel(pol, m+1) = gprec_w(gel(v,m), prec);
    1075        5600 :   return RgX_renormalize_lg(pol, M+3);
    1076             : }
    1077             : 
    1078             : static void
    1079       50044 : worker_init(long q, GEN *an, GEN *bn, GEN *AB, GEN *A, GEN *B)
    1080             : {
    1081       50044 :   if (typ(*bn) == t_INT) *bn = NULL;
    1082       50044 :   if (*bn)
    1083             :   {
    1084        1016 :     *AB = cgetg(3, t_VEC);
    1085        1016 :     gel(*AB,1) = *A = cgetg(q+1, t_VEC);
    1086        1016 :     gel(*AB,2) = *B = cgetg(q+1, t_VEC);
    1087        1016 :     if (typ(an) == t_VEC) *an = RgV_kill0(*an);
    1088        1016 :     if (typ(bn) == t_VEC) *bn = RgV_kill0(*bn);
    1089             :   }
    1090             :   else
    1091             :   {
    1092       49028 :     *B = NULL;
    1093       49028 :     *AB = *A = cgetg(q+1, t_VEC);
    1094       49027 :     if (typ(*an) == t_VEC) *an = RgV_kill0(*an);
    1095             :   }
    1096       50044 : }
    1097             : GEN
    1098       13725 : lfuninit_theta2_worker(long r, GEN L, GEN qk, GEN a, GEN di, GEN an, GEN bn)
    1099             : {
    1100       13725 :   long q, m, prec = di[1], M = di[2], m0 = di[3], L0 = lg(an)-1;
    1101             :   GEN AB, A, B;
    1102       13725 :   worker_init((M - r) / m0 + 1, &an, &bn, &AB, &A, &B);
    1103      145135 :   for (q = 0, m = r; m <= M; m += m0, q++)
    1104             :   {
    1105      131423 :     GEN t = gel(qk, m+1);
    1106      131423 :     long N = minss(L[m+1],L0);
    1107      131423 :     gel(A, q+1) = theta2(an, N, t, a, prec); /* theta(exp(mh)) */
    1108      131412 :     if (bn) gel(B, q+1) = theta2(bn, N, t, a, prec);
    1109             :   }
    1110       13712 :   return AB;
    1111             : }
    1112             : 
    1113             : /* theta(exp(mh)) ~ sum_{n <= N} a(n) k[m,n] */
    1114             : static GEN
    1115      148489 : an_msum(GEN an, long N, GEN vKm)
    1116             : {
    1117      148489 :   pari_sp av = avma;
    1118      148489 :   GEN s = gen_0;
    1119             :   long n;
    1120     9635029 :   for (n = 1; n <= N; n++)
    1121             :   {
    1122     9487731 :     GEN c = mul_an(an, n, gel(vKm,n));
    1123     9490846 :     if (c) s = gadd(s, c);
    1124             :   }
    1125      147298 :   return gerepileupto(av, s);
    1126             : }
    1127             : 
    1128             : GEN
    1129       36318 : lfuninit_worker(long r, GEN K, GEN L, GEN peh2d, GEN vroots, GEN dr, GEN di,
    1130             :                 GEN an, GEN bn)
    1131             : {
    1132       36318 :   pari_sp av0 = avma;
    1133       36318 :   long m, n, q, L0 = lg(an)-1;
    1134       36318 :   double sig0 = rtodbl(gel(dr,1)), sub2 = rtodbl(gel(dr,2));
    1135       36318 :   double k1 = rtodbl(gel(dr,3)), MAXs = rtodbl(gel(dr,4));
    1136       36318 :   long D = di[1], M = di[2], m0 = di[3];
    1137       36318 :   double M0 = sig0? sub2 / sig0: 1./0.;
    1138       36318 :   GEN AB, A, B, vK = cgetg(M/m0 + 2, t_VEC);
    1139             : 
    1140      183876 :   for (q = 0, m = r; m <= M; m += m0, q++)
    1141      147558 :     gel(vK, q+1) = const_vec(L[m+1], NULL);
    1142       36318 :   worker_init(q, &an, &bn, &AB, &A, &B);
    1143      182888 :   for (m -= m0, q--; m >= 0; m -= m0, q--)
    1144             :   {
    1145      147564 :     double c1 = D + ((m > M0)? m * sig0 - sub2 : 0);
    1146      147564 :     GEN vKm = gel(vK,q+1); /* conceptually K(m,n) */
    1147     9652920 :     for (n = 1; n <= L[m+1]; n++)
    1148             :     {
    1149             :       GEN t2d, kmn;
    1150     9506350 :       long nn, mm, qq, p = 0;
    1151             :       double c, c2;
    1152             :       pari_sp av;
    1153             : 
    1154     9506350 :       if (gel(vKm, n)) continue; /* done already */
    1155     7315997 :       c = c1 + k1 * log2(n);
    1156             :       /* n *= 2; m -= m0 => c += c2, provided m >= M0. Else c += k1 */
    1157     7315997 :       c2 = k1 - MAXs;
    1158             :       /* p = largest (absolute) accuracy to which we need K(m,n) */
    1159    11294154 :       for (mm=m,nn=n; mm >= M0;)
    1160             :       {
    1161     8031333 :         if (nn <= L[mm+1] && (gel(an, nn) || (bn && gel(bn, nn))))
    1162     1467590 :           p = maxuu(p, (ulong)c);
    1163     8031380 :         nn <<= 1;
    1164     8031380 :         mm -= m0; if (mm >= M0) c += c2; else { c += k1; break; }
    1165             :       }
    1166             :       /* mm < M0 || nn > L[mm+1] */
    1167    13871008 :       for (         ; mm >= 0; nn<<=1,mm-=m0,c+=k1)
    1168     6555100 :         if (nn <= L[mm+1] && (gel(an, nn) || (bn && gel(bn, nn))))
    1169     1667462 :           p = maxuu(p, (ulong)c);
    1170     7315908 :       if (!p) continue; /* a_{n 2^v} = 0 for all v in range */
    1171     2129438 :       av = avma;
    1172     2129438 :       t2d = mpmul(gel(vroots,n), gel(peh2d,m+1));/*(n exp(mh)/sqrt(N))^(2/d)*/
    1173     2130520 :       kmn = gerepileupto(av, gammamellininvrt(K, t2d, p));
    1174     6572044 :       for (qq=q,mm=m,nn=n; mm >= 0; nn<<=1,mm-=m0,qq--)
    1175     4443511 :         if (nn <= L[mm+1]) gmael(vK, qq+1, nn) = kmn;
    1176             :     }
    1177             :   }
    1178      182802 :   for (q = 0, m = r; m <= M; m += m0, q++)
    1179             :   {
    1180      147473 :     long N = minss(L0, L[m+1]);
    1181      147475 :     gel(A, q+1) = an_msum(an, N, gel(vK,q+1));
    1182      147480 :     if (bn) gel(B, q+1) = an_msum(bn, N, gel(vK,q+1));
    1183             :   }
    1184       35329 :   return gerepileupto(av0, AB);
    1185             : }
    1186             : /* return A = [\theta(exp(mh)), m=0..M], theta(t) = sum a(n) K(n/sqrt(N) t),
    1187             :  * h = log(2)/m0. If bn != NULL, return the pair [A, B] */
    1188             : static GEN
    1189        5446 : lfuninit_ab(GEN theta, GEN h, struct lfunp *S)
    1190             : {
    1191        5446 :   const long M = S->M, prec = S->precmax;
    1192        5446 :   GEN tech = linit_get_tech(theta), isqN = theta_get_isqrtN(tech);
    1193        5446 :   GEN an = S->an, bn = S->bn, va, vb;
    1194             :   struct pari_mt pt;
    1195             :   GEN worker;
    1196             :   long m0, r, pending;
    1197             : 
    1198        5446 :   if (S->vgaell)
    1199             :   { /* d=2 and Vga = [a,a+1] */
    1200        2681 :     GEN a = vecmin(ldata_get_gammavec(linit_get_ldata(theta)));
    1201        2681 :     GEN qk = gpowers0(mpexp(h), M, isqN);
    1202        2681 :     m0 = minss(M+1, mt_nbthreads());
    1203        2681 :     worker = snm_closure(is_entry("_lfuninit_theta2_worker"),
    1204             :                          mkvecn(6, S->L, qk, a, mkvecsmall3(prec, M, m0),
    1205             :                                 an, bn? bn: gen_0));
    1206             :   }
    1207             :   else
    1208             :   {
    1209             :     GEN vroots, peh2d, d2;
    1210        2765 :     double sig0 = S->MAXs / S->m0, sub2 = S->sub / M_LN2;
    1211             :     /* For all 0<= m <= M, and all n <= L[m+1] such that a_n!=0, we must compute
    1212             :      *   k[m,n] = K(n exp(mh)/sqrt(N))
    1213             :      * with ln(absolute error) <= E + max(mh sigma - sub, 0) + k1 * log(n).
    1214             :      * N.B. we use the 'rt' variant and pass argument (n exp(mh)/sqrt(N))^(2/d).
    1215             :      * Speedup: if n' = 2n and m' = m - m0 >= 0; then k[m,n] = k[m',n']. */
    1216             :     /* vroots[n] = n^(2/d) */
    1217        2765 :     vroots = mkvroots(S->d, S->nmax, prec);
    1218        2765 :     d2 = gdivgs(gen_2, S->d);
    1219             :     /* peh2d[m+1] = (exp(mh)/sqrt(N))^(2/d) */
    1220        2765 :     peh2d = gpowers0(gexp(gmul(d2,h), prec), M, gpow(isqN, d2, prec));
    1221        2765 :     m0 = S->m0;
    1222        2765 :     worker = snm_closure(is_entry("_lfuninit_worker"),
    1223             :                          mkvecn(8, theta_get_K(tech), S->L, peh2d, vroots,
    1224             :                                 mkvec4(dbltor(sig0), dbltor(sub2),
    1225             :                                        dbltor(S->k1), dbltor(S->MAXs)),
    1226             :                                 mkvecsmall3(S->D, M, m0),
    1227             :                                 an, bn? bn: gen_0));
    1228             :     /* For each 0 <= m <= M, we will sum for n<=L[m+1] a(n) K(m,n)
    1229             :      * bit accuracy for K(m,n): D + k1*log2(n) + 1_{m > M0} (m*sig0 - sub2)
    1230             :      * We restrict m to arithmetic progressions r mod m0 to save memory and
    1231             :      * allow parallelization */
    1232             :   }
    1233        5446 :   va = cgetg(M+2, t_VEC);
    1234        5446 :   vb = bn? cgetg(M+2, t_VEC): NULL;
    1235        5446 :   mt_queue_start_lim(&pt, worker, m0);
    1236        5446 :   pending = 0;
    1237       71208 :   for (r = 0; r < m0 || pending; r++)
    1238             :   { /* m = q m0 + r */
    1239             :     GEN done, A, B;
    1240             :     long q, m, workid;
    1241       65762 :     mt_queue_submit(&pt, r, r < m0 ? mkvec(utoi(r)): NULL);
    1242       65762 :     done = mt_queue_get(&pt, &workid, &pending);
    1243       65762 :     if (!done) continue;
    1244       50048 :     if (bn) { A = gel(done,1); B = gel(done,2); } else { A = done; B = NULL; }
    1245      329069 :     for (q = 0, m = workid; m <= M; m += m0, q++)
    1246             :     {
    1247      279021 :       gel(va, m+1) = gel(A, q+1);
    1248      279021 :       if (bn) gel(vb, m+1) = gel(B, q+1);
    1249             :     }
    1250             :   }
    1251        5446 :   mt_queue_end(&pt);
    1252        5446 :   return bn? mkvec2(va, vb): va;
    1253             : }
    1254             : 
    1255             : static void
    1256       90150 : parse_dom(double k, GEN dom, struct lfunp *S)
    1257             : {
    1258       90150 :   long l = lg(dom);
    1259       90150 :   if (typ(dom)!=t_VEC) pari_err_TYPE("lfuninit [domain]", dom);
    1260       90150 :   if (l == 2)
    1261             :   {
    1262       25222 :     S->dc = k/2.;
    1263       25222 :     S->dw = 0.;
    1264       25222 :     S->dh = gtodouble(gel(dom,1));
    1265             :   }
    1266       64928 :   else if (l == 3)
    1267             :   {
    1268         301 :     S->dc = k/2.;
    1269         301 :     S->dw = gtodouble(gel(dom,1));
    1270         301 :     S->dh = gtodouble(gel(dom,2));
    1271             :   }
    1272       64627 :   else if (l == 4)
    1273             :   {
    1274       64627 :     S->dc = gtodouble(gel(dom,1));
    1275       64627 :     S->dw = gtodouble(gel(dom,2));
    1276       64627 :     S->dh = gtodouble(gel(dom,3));
    1277             :   }
    1278             :   else
    1279             :   {
    1280           0 :     pari_err_TYPE("lfuninit [domain]", dom);
    1281           0 :     S->dc = S->dw = S->dh = 0; /*-Wall*/
    1282             :   }
    1283       90150 :   if (S->dw < 0 || S->dh < 0) pari_err_TYPE("lfuninit [domain]", dom);
    1284       90150 : }
    1285             : 
    1286             : /* do we have dom \subset dom0 ? dom = [center, width, height] */
    1287             : int
    1288       15468 : sdomain_isincl(double k, GEN dom, GEN dom0)
    1289             : {
    1290             :   struct lfunp S0, S;
    1291       15468 :   parse_dom(k, dom, &S);
    1292       15468 :   parse_dom(k, dom0, &S0);
    1293       15468 :   return S0.dc - S0.dw <= S.dc - S.dw
    1294       15468 :       && S0.dc + S0.dw >= S.dc + S.dw && S0.dh >= S.dh;
    1295             : }
    1296             : 
    1297             : static int
    1298       15468 : checklfuninit(GEN linit, GEN dom, long der, long bitprec)
    1299             : {
    1300       15468 :   GEN ldata = linit_get_ldata(linit);
    1301       15468 :   GEN domain = lfun_get_domain(linit_get_tech(linit));
    1302       15468 :   return domain_get_der(domain) >= der
    1303       15468 :     && domain_get_bitprec(domain) >= bitprec
    1304       30936 :     && sdomain_isincl(gtodouble(ldata_get_k(ldata)), dom, domain_get_dom(domain));
    1305             : }
    1306             : 
    1307             : static GEN
    1308         952 : ginvsqrtvec(GEN x, long prec)
    1309             : {
    1310         952 :   if (is_vec_t(typ(x)))
    1311         357 :     pari_APPLY_same(ginv(gsqrt(gel(x,i), prec)))
    1312         833 :   else return ginv(gsqrt(x, prec));
    1313             : }
    1314             : 
    1315             : GEN
    1316        6167 : lfuninit_make(long t, GEN ldata, GEN tech, GEN domain)
    1317             : {
    1318        6167 :   GEN Vga = ldata_get_gammavec(ldata);
    1319        6167 :   long d = lg(Vga)-1;
    1320        6167 :   GEN w2 = gen_1, k2 = gmul2n(ldata_get_k(ldata), -1);
    1321        6167 :   GEN expot = gdivgs(gadd(gmulsg(d, gsubgs(k2, 1)), sumVga(Vga)), 4);
    1322        6167 :   if (typ(ldata_get_dual(ldata))==t_INT)
    1323             :   {
    1324        6013 :     GEN eno = ldata_get_rootno(ldata);
    1325        6013 :     long prec = nbits2prec( domain_get_bitprec(domain) );
    1326        6013 :     if (!isint1(eno)) w2 = ginvsqrtvec(eno, prec);
    1327             :   }
    1328        6167 :   tech = mkvec3(domain, tech, mkvec4(k2, w2, expot, gammafactor(Vga)));
    1329        6167 :   return mkvec3(mkvecsmall(t), ldata, tech);
    1330             : }
    1331             : 
    1332             : static void
    1333        2765 : lfunparams2(struct lfunp *S)
    1334             : {
    1335        2765 :   GEN L = S->L, an = S->an, bn = S->bn;
    1336             :   double pmax;
    1337        2765 :   long m, nan, nmax, neval, M = S->M;
    1338             : 
    1339        2765 :   S->vgaell = 0;
    1340             :   /* try to reduce parameters now we know the a_n (some may be 0) */
    1341        2765 :   if (typ(an) == t_VEC) an = RgV_kill0(an);
    1342        2765 :   nan = S->nmax; /* lg(an)-1 may be large than this */
    1343        2765 :   nmax = neval = 0;
    1344        2765 :   if (!bn)
    1345      149311 :     for (m = 0; m <= M; m++)
    1346             :     {
    1347      146567 :       long n = minss(nan, L[m+1]);
    1348      209007 :       while (n > 0 && !gel(an,n)) n--;
    1349      146567 :       if (n > nmax) nmax = n;
    1350      146567 :       neval += n;
    1351      146567 :       L[m+1] = n; /* reduce S->L[m+1] */
    1352             :     }
    1353             :   else
    1354             :   {
    1355          21 :     if (typ(bn) == t_VEC) bn = RgV_kill0(bn);
    1356        1036 :     for (m = 0; m <= M; m++)
    1357             :     {
    1358        1015 :       long n = minss(nan, L[m+1]);
    1359        1057 :       while (n > 0 && !gel(an,n) && !gel(bn,n)) n--;
    1360        1015 :       if (n > nmax) nmax = n;
    1361        1015 :       neval += n;
    1362        1015 :       L[m+1] = n; /* reduce S->L[m+1] */
    1363             :     }
    1364             :   }
    1365        2765 :   if (DEBUGLEVEL >= 1) err_printf("expected evaluations: %ld\n", neval);
    1366        2765 :   for (; M > 0; M--)
    1367        2765 :     if (L[M+1]) break;
    1368        2765 :   setlg(L, M+2);
    1369        2765 :   S->M = M;
    1370        2765 :   S->nmax = nmax;
    1371             : 
    1372             :   /* need K(n*exp(mh)/sqrt(N)) to absolute accuracy
    1373             :    *   D + k1*log(n) + max(m * sig0 - sub2, 0) */
    1374        2765 :   pmax = S->D + S->k1 * log2(L[1]);
    1375        2765 :   if (S->MAXs)
    1376             :   {
    1377        2765 :     double sig0 = S->MAXs/S->m0, sub2 = S->sub / M_LN2;
    1378      125630 :     for (m = ceil(sub2 / sig0); m <= S->M; m++)
    1379             :     {
    1380      122865 :       double c = S->D + m*sig0 - sub2;
    1381      122865 :       if (S->k1 > 0) c += S->k1 * log2(L[m+1]);
    1382      122865 :       pmax = maxdd(pmax, c);
    1383             :     }
    1384             :   }
    1385        2765 :   S->Dmax = pmax;
    1386        2765 :   S->precmax = nbits2prec(pmax);
    1387        2765 : }
    1388             : 
    1389             : static GEN
    1390        5460 : lfun_init_theta(GEN ldata, GEN eno, struct lfunp *S)
    1391             : {
    1392        5460 :   GEN an2, dual, tdom = NULL, Vga = ldata_get_gammavec(ldata);
    1393        5460 :   long L, prec = S->precmax;
    1394        5460 :   if (eno)
    1395        4025 :     L = S->nmax;
    1396             :   else
    1397             :   {
    1398        1435 :     tdom = dbltor(sqrt(0.5));
    1399        1435 :     L = maxss(S->nmax, lfunthetacost(ldata, tdom, 0, S->D));
    1400             :   }
    1401        5460 :   dual = ldata_get_dual(ldata);
    1402        5460 :   S->an = ldata_vecan(ldata_get_an(ldata), L, prec);
    1403        5446 :   S->bn = typ(dual)==t_INT? NULL: ldata_vecan(dual, S->nmax, prec);
    1404        5446 :   if (!vgaell(Vga)) lfunparams2(S);
    1405             :   else
    1406             :   {
    1407        2681 :     S->an = antwist(S->an, Vga, prec);
    1408        2681 :     if (S->bn) S->bn = antwist(S->bn, Vga, prec);
    1409        2681 :     S->vgaell = 1;
    1410             :   }
    1411        5446 :   an2 = lg(Vga)-1 == 1? antwist(S->an, Vga, prec): S->an;
    1412        5446 :   return lfunthetainit0(ldata, tdom, an2, 0, S->Dmax, 0);
    1413             : }
    1414             : 
    1415             : GEN
    1416       11692 : lfuncost(GEN L, GEN dom, long der, long bitprec)
    1417             : {
    1418       11692 :   pari_sp av = avma;
    1419       11692 :   GEN ldata = lfunmisc_to_ldata_shallow(L);
    1420       11692 :   GEN k = ldata_get_k(ldata);
    1421             :   struct lfunp S;
    1422             : 
    1423       11692 :   parse_dom(gtodouble(k), dom, &S);
    1424       11692 :   lfunparams(ldata, der, bitprec, &S);
    1425       11692 :   set_avma(av); return mkvecsmall2(S.nmax, S.Dmax);
    1426             : }
    1427             : GEN
    1428          42 : lfuncost0(GEN L, GEN dom, long der, long bitprec)
    1429             : {
    1430          42 :   pari_sp av = avma;
    1431             :   GEN C;
    1432             : 
    1433          42 :   if (is_linit(L))
    1434             :   {
    1435          28 :     GEN tech = linit_get_tech(L);
    1436          28 :     GEN domain = lfun_get_domain(tech);
    1437          28 :     dom = domain_get_dom(domain);
    1438          28 :     der = domain_get_der(domain);
    1439          28 :     bitprec = domain_get_bitprec(domain);
    1440          28 :     if (linit_get_type(L) == t_LDESC_PRODUCT)
    1441             :     {
    1442          21 :       GEN v = lfunprod_get_fact(linit_get_tech(L)), F = gel(v,1);
    1443          21 :       long i, l = lg(F);
    1444          21 :       C = cgetg(l, t_VEC);
    1445          77 :       for (i = 1; i < l; ++i)
    1446          56 :         gel(C, i) = zv_to_ZV( lfuncost(gel(F,i), dom, der, bitprec) );
    1447          21 :       return gerepileupto(av, C);
    1448             :     }
    1449             :   }
    1450          21 :   if (!dom) pari_err_TYPE("lfuncost [missing s domain]", L);
    1451          21 :   C = lfuncost(L,dom,der,bitprec);
    1452          21 :   return gerepileupto(av, zv_to_ZV(C));
    1453             : }
    1454             : 
    1455             : GEN
    1456       21439 : lfuninit(GEN lmisc, GEN dom, long der, long bitprec)
    1457             : {
    1458       21439 :   pari_sp ltop = avma;
    1459             :   GEN poqk, AB, R, h, theta, ldata, eno, r, domain, tech, k;
    1460             :   struct lfunp S;
    1461             : 
    1462       21439 :   if (is_linit(lmisc))
    1463             :   {
    1464       15510 :     long t = linit_get_type(lmisc);
    1465       15510 :     if (t==t_LDESC_INIT || t==t_LDESC_PRODUCT)
    1466             :     {
    1467       15468 :       if (checklfuninit(lmisc, dom, der, bitprec)) return lmisc;
    1468          21 :       pari_warn(warner,"lfuninit: insufficient initialization");
    1469             :     }
    1470             :   }
    1471        5992 :   ldata = lfunmisc_to_ldata_shallow(lmisc);
    1472             : 
    1473        5992 :   if (ldata_get_type(ldata)==t_LFUN_NF)
    1474             :   {
    1475         532 :     GEN T = gel(ldata_get_an(ldata), 2);
    1476         532 :     return lfunzetakinit(T, dom, der, 0, bitprec);
    1477             :   }
    1478        5460 :   k = ldata_get_k(ldata);
    1479        5460 :   parse_dom(gtodouble(k), dom, &S);
    1480        5460 :   lfunparams(ldata, der, bitprec, &S);
    1481        5460 :   ldata = ldata_newprec(ldata, nbits2prec(S.Dmax));
    1482        5460 :   r = ldata_get_residue(ldata);
    1483             :   /* Note: all guesses should already have been performed (thetainit more
    1484             :    * expensive than needed: should be either tdom = 1 or bitprec = S.D).
    1485             :    * BUT if the root number / polar part do not have an algebraic
    1486             :    * expression, there is no way to do this until we know the
    1487             :    * precision, i.e. now. So we can't remove guessing code from here and
    1488             :    * lfun_init_theta */
    1489        5460 :   if (r && isintzero(r)) eno = NULL;
    1490             :   else
    1491             :   {
    1492        5460 :     eno = ldata_get_rootno(ldata);
    1493        5460 :     if (isintzero(eno)) eno = NULL;
    1494             :   }
    1495        5460 :   theta = lfun_init_theta(ldata, eno, &S);
    1496        5446 :   if (eno && !r)
    1497        2310 :     R = gen_0;
    1498             :   else
    1499             :   {
    1500        3136 :     GEN v = lfunrootres(theta, S.D);
    1501        3136 :     ldata = shallowcopy(ldata);
    1502        3136 :     gel(ldata, 6) = gel(v,3);
    1503        3136 :     r = gel(v,1);
    1504        3136 :     R = gel(v,2);
    1505        3136 :     if (isintzero(r)) setlg(ldata,7); else gel(ldata, 7) = r;
    1506             :   }
    1507        5446 :   h = divru(mplog2(S.precmax), S.m0);
    1508             :   /* exp(kh/2 . [0..M]) */
    1509        5446 :   poqk = gequal0(k) ? NULL
    1510        5446 :        : gpowers(gprec_w(mpexp(gmul2n(gmul(k,h), -1)), S.precmax), S.M);
    1511        5446 :   AB = lfuninit_ab(theta, h, &S);
    1512        5446 :   if (S.bn)
    1513             :   {
    1514         154 :     GEN A = gel(AB,1), B = gel(AB,2);
    1515         154 :     A = lfuninit_pol(A, poqk, S.precmax);
    1516         154 :     B = lfuninit_pol(B, poqk, S.precmax);
    1517         154 :     AB = mkvec2(A, B);
    1518             :   }
    1519             :   else
    1520        5292 :     AB = lfuninit_pol(AB, poqk, S.precmax);
    1521        5446 :   tech = mkvec3(h, AB, R);
    1522        5446 :   domain = mkvec2(dom, mkvecsmall2(der, bitprec));
    1523        5446 :   return gerepilecopy(ltop, lfuninit_make(t_LDESC_INIT, ldata, tech, domain));
    1524             : }
    1525             : 
    1526             : GEN
    1527         448 : lfuninit0(GEN lmisc, GEN dom, long der, long bitprec)
    1528             : {
    1529         448 :   GEN z = lfuninit(lmisc, dom, der, bitprec);
    1530         448 :   return z == lmisc? gcopy(z): z;
    1531             : }
    1532             : 
    1533             : /* If s is a pole of Lambda, return polar part at s; else return NULL */
    1534             : static GEN
    1535        5010 : lfunpoleresidue(GEN R, GEN s)
    1536             : {
    1537             :   long j;
    1538       14141 :   for (j = 1; j < lg(R); j++)
    1539             :   {
    1540        9733 :     GEN Rj = gel(R, j), be = gel(Rj, 1);
    1541        9733 :     if (gequal(s, be)) return gel(Rj, 2);
    1542             :   }
    1543        4408 :   return NULL;
    1544             : }
    1545             : 
    1546             : /* Compute contribution of polar part at s when not a pole. */
    1547             : static GEN
    1548        8067 : veccothderivn(GEN a, long n)
    1549             : {
    1550             :   long i;
    1551        8067 :   pari_sp av = avma;
    1552        8067 :   GEN c = pol_x(0), cp = mkpoln(3, gen_m1, gen_0, gen_1);
    1553        8067 :   GEN v = cgetg(n+2, t_VEC);
    1554        8067 :   gel(v, 1) = poleval(c, a);
    1555       24334 :   for(i = 2; i <= n+1; i++)
    1556             :   {
    1557       16267 :     c = ZX_mul(ZX_deriv(c), cp);
    1558       16267 :     gel(v, i) = gdiv(poleval(c, a), mpfact(i-1));
    1559             :   }
    1560        8067 :   return gerepilecopy(av, v);
    1561             : }
    1562             : 
    1563             : static GEN
    1564        8200 : polepart(long n, GEN h, GEN C)
    1565             : {
    1566        8200 :   GEN h2n = gpowgs(gdiv(h, gen_2), n-1);
    1567        8200 :   GEN res = gmul(h2n, gel(C,n));
    1568        8200 :   return odd(n)? res : gneg(res);
    1569             : }
    1570             : 
    1571             : static GEN
    1572        4002 : lfunsumcoth(GEN R, GEN s, GEN h, long prec)
    1573             : {
    1574             :   long i,j;
    1575        4002 :   GEN S = gen_0;
    1576       12069 :   for (j = 1; j < lg(R); ++j)
    1577             :   {
    1578        8067 :     GEN r = gel(R,j), be = gel(r,1), Rj = gel(r, 2);
    1579        8067 :     long e = valp(Rj);
    1580        8067 :     GEN z1 = gexpm1(gmul(h, gsub(s,be)), prec); /* exp(h(s-beta))-1 */
    1581        8067 :     GEN c1 = gaddgs(gdivsg(2, z1), 1); /* coth((h/2)(s-beta)) */
    1582        8067 :     GEN C1 = veccothderivn(c1, 1-e);
    1583       16267 :     for (i = e; i < 0; i++)
    1584             :     {
    1585        8200 :       GEN Rbe = mysercoeff(Rj, i);
    1586        8200 :       GEN p1 = polepart(-i, h, C1);
    1587        8200 :       S = gadd(S, gmul(Rbe, p1));
    1588             :     }
    1589             :   }
    1590        4002 :   return gmul2n(S, -1);
    1591             : }
    1592             : 
    1593             : static GEN lfunlambda_OK(GEN linit, GEN s, GEN sdom, long bitprec);
    1594             : /* L is a t_LDESC_PRODUCT Linit */
    1595             : static GEN
    1596        1272 : lfunlambda_product(GEN L, GEN s, GEN sdom, long bitprec)
    1597             : {
    1598        1272 :   GEN ldata = linit_get_ldata(L), v = lfunprod_get_fact(linit_get_tech(L));
    1599        1272 :   GEN r = gen_1, F = gel(v,1), E = gel(v,2), C = gel(v,3), cs = conj_i(s);
    1600        1272 :   long i, l = lg(F), isreal = gequal(imag_i(s), imag_i(cs));
    1601        4418 :   for (i = 1; i < l; ++i)
    1602             :   {
    1603        3146 :     GEN f = lfunlambda_OK(gel(F, i), s, sdom, bitprec);
    1604        3146 :     if (E[i])
    1605        3146 :       r = gmul(r, gpowgs(f, E[i]));
    1606        3146 :     if (C[i])
    1607             :     {
    1608         378 :       GEN fc = isreal? f: lfunlambda_OK(gel(F, i), cs, sdom, bitprec);
    1609         378 :       r = gmul(r, gpowgs(conj_i(fc), C[i]));
    1610             :     }
    1611             :   }
    1612        1272 :   return (ldata_isreal(ldata) && gequal0(imag_i(s)))? real_i(r): r;
    1613             : }
    1614             : 
    1615             : /* s a t_SER */
    1616             : static long
    1617        1457 : der_level(GEN s)
    1618        1457 : { return signe(s)? lg(s)-3: valp(s)-1; }
    1619             : 
    1620             : /* s a t_SER; return coeff(s, X^0) */
    1621             : static GEN
    1622         469 : ser_coeff0(GEN s) { return simplify_shallow(polcoef_i(s, 0, -1)); }
    1623             : 
    1624             : static GEN
    1625        9072 : get_domain(GEN s, GEN *dom, long *der)
    1626             : {
    1627        9072 :   GEN sa = s;
    1628        9072 :   *der = 0;
    1629        9072 :   switch(typ(s))
    1630             :   {
    1631           7 :     case t_POL:
    1632           7 :     case t_RFRAC: s = toser_i(s);
    1633         469 :     case t_SER:
    1634         469 :       *der = der_level(s);
    1635         469 :       sa = ser_coeff0(s);
    1636             :   }
    1637        9072 :   *dom = mkvec3(real_i(sa), gen_0, gabs(imag_i(sa),DEFAULTPREC));
    1638        9072 :   return s;
    1639             : }
    1640             : /* assume s went through get_domain and s/bitprec belong to domain */
    1641             : static GEN
    1642       22303 : lfunlambda_OK(GEN linit, GEN s, GEN sdom, long bitprec)
    1643             : {
    1644             :   GEN eno, ldata, tech, h, pol;
    1645       22303 :   GEN S, S0 = NULL, k2, cost;
    1646             :   long prec, prec0;
    1647             :   struct lfunp D, D0;
    1648             : 
    1649       22303 :   if (linit_get_type(linit) == t_LDESC_PRODUCT)
    1650        1272 :     return lfunlambda_product(linit, s, sdom, bitprec);
    1651       21031 :   ldata = linit_get_ldata(linit);
    1652       21031 :   eno = ldata_get_rootno(ldata);
    1653       21031 :   tech = linit_get_tech(linit);
    1654       21031 :   h = lfun_get_step(tech); prec = realprec(h);
    1655             :   /* try to reduce accuracy */
    1656       21031 :   parse_dom(0, sdom, &D0);
    1657       21031 :   parse_dom(0, domain_get_dom(lfun_get_domain(tech)), &D);
    1658       21031 :   if (0.8 * D.dh > D0.dh)
    1659             :   {
    1660       11615 :     cost = lfuncost(linit, sdom, typ(s)==t_SER? der_level(s): 0, bitprec);
    1661       11615 :     prec0 = nbits2prec(cost[2]);
    1662       11615 :     if (prec0 < prec) { prec = prec0; h = gprec_w(h, prec); }
    1663             :   }
    1664       21031 :   pol = lfun_get_pol(tech);
    1665       21031 :   s = gprec_w(s, prec);
    1666       21031 :   if (ldata_get_residue(ldata))
    1667             :   {
    1668        4457 :     GEN R = lfun_get_Residue(tech);
    1669        4457 :     GEN Ra = lfunpoleresidue(R, s);
    1670        4457 :     if (Ra) return gprec_w(Ra, nbits2prec(bitprec));
    1671        4002 :     S0 = lfunsumcoth(R, s, h, prec);
    1672             :   }
    1673       20576 :   k2 = lfun_get_k2(tech);
    1674       20576 :   if (typ(pol)==t_POL && typ(s) != t_SER && gequal(real_i(s), k2))
    1675       14570 :   { /* on critical line: shortcut */
    1676       14570 :     GEN polz, b = imag_i(s);
    1677       14570 :     polz = gequal0(b)? poleval(pol,gen_1): poleval(pol, expIr(gmul(h,b)));
    1678       14570 :     S = gadd(polz, gmulvec(eno, conj_i(polz)));
    1679             :   }
    1680             :   else
    1681             :   {
    1682        6006 :     GEN z = gexp(gmul(h, gsub(s, k2)), prec);
    1683        6006 :     GEN zi = ginv(z), zc = conj_i(zi);
    1684        6006 :     if (typ(pol)==t_POL)
    1685        5810 :       S = gadd(poleval(pol, z), gmulvec(eno, conj_i(poleval(pol, zc))));
    1686             :     else
    1687         196 :       S = gadd(poleval(gel(pol,1), z), gmulvec(eno, poleval(gel(pol,2), zi)));
    1688             :   }
    1689       20576 :   if (S0) S = gadd(S,S0);
    1690       20576 :   return gprec_w(gmul(S,h), nbits2prec(bitprec));
    1691             : }
    1692             : GEN
    1693         966 : lfunlambda(GEN lmisc, GEN s, long bitprec)
    1694             : {
    1695         966 :   pari_sp av = avma;
    1696             :   GEN linit, dom, z;
    1697             :   long der;
    1698         966 :   s = get_domain(s, &dom, &der);
    1699         966 :   linit = lfuninit(lmisc, dom, der, bitprec);
    1700         966 :   z = lfunlambda_OK(linit,s, dom, bitprec);
    1701         966 :   return gerepilecopy(av, z);
    1702             : }
    1703             : 
    1704             : static long
    1705        8449 : is_ser(GEN x)
    1706             : {
    1707        8449 :   long t = typ(x);
    1708        8449 :   if (t == t_SER) return 1;
    1709        6356 :   if (!is_vec_t(t) || lg(x)==1) return 0;
    1710         224 :   if (typ(gel(x,1))==t_SER) return 1;
    1711         140 :   return 0;
    1712             : }
    1713             : 
    1714             : static GEN
    1715         420 : lfunser(GEN res)
    1716             : {
    1717         420 :   long v = valp(res);
    1718         420 :   if (v > 0) return gen_0;
    1719         378 :   if (v == 0) res = gel(res, 2);
    1720             :   else
    1721         259 :     setlg(res, minss(lg(res), 2-v));
    1722         378 :   return res;
    1723             : }
    1724             : 
    1725             : static GEN
    1726         420 : lfunservec(GEN x)
    1727             : {
    1728         420 :   if (typ(x)==t_SER) return lfunser(x);
    1729           0 :   pari_APPLY_same(lfunser(gel(x,i)))
    1730             : }
    1731             : 
    1732             : /* assume lmisc is an linit, s went through get_domain and s/bitprec belong
    1733             :  * to domain */
    1734             : static GEN
    1735        4858 : lfun_OK(GEN linit, GEN s, GEN sdom, long bitprec)
    1736             : {
    1737        4858 :   GEN N, gas, S, FVga, res, ss = s;
    1738        4858 :   long prec = nbits2prec(bitprec);
    1739             : 
    1740        4858 :   FVga = lfun_get_factgammavec(linit_get_tech(linit));
    1741        4858 :   S = lfunlambda_OK(linit, s, sdom, bitprec);
    1742        4858 :   if (is_ser(S))
    1743             :   {
    1744        1701 :     GEN r = typ(S)==t_SER ? S : gel(S,1);
    1745        1701 :     long d = lg(r) - 2 + fracgammadegree(FVga);
    1746        1701 :     if (typ(s) == t_SER)
    1747        1323 :       ss = sertoser(s, d);
    1748             :     else
    1749         378 :       ss = deg1ser_shallow(gen_1, s, varn(r), d);
    1750             :   }
    1751        4858 :   gas = gammafactproduct(FVga, ss, prec);
    1752        4858 :   N = ldata_get_conductor(linit_get_ldata(linit));
    1753        4858 :   res = gdiv(S, gmul(gpow(N, gdivgs(ss, 2), prec), gas));
    1754        4858 :   if (typ(s)!=t_SER && is_ser(res))
    1755         420 :     res = lfunservec(res);
    1756        4858 :   return gprec_w(res, prec);
    1757             : }
    1758             : 
    1759             : GEN
    1760        6650 : lfun(GEN lmisc, GEN s, long bitprec)
    1761             : {
    1762        6650 :   pari_sp av = avma;
    1763             :   GEN linit, dom, z;
    1764             :   long der;
    1765        6650 :   s = get_domain(s, &dom, &der);
    1766        6650 :   if (!der && typ(s) == t_INT && !is_bigint(s))
    1767             :   { /* special value ? */
    1768             :     GEN ldata;
    1769        5215 :     long t, ss = itos(s);
    1770        5215 :     if (is_linit(lmisc))
    1771         581 :       ldata = linit_get_ldata(lmisc);
    1772             :     else
    1773        4634 :       lmisc = ldata = lfunmisc_to_ldata_shallow(lmisc);
    1774        5215 :     t = ldata_get_type(ldata);
    1775        5215 :     if (t == t_LFUN_KRONECKER || t == t_LFUN_ZETA)
    1776             :     {
    1777        2779 :       long D = itos_or_0(gel(ldata_get_an(ldata), 2));
    1778        2779 :       if (D)
    1779             :       {
    1780        2779 :         if (ss <= 0) return lfunquadneg(D, ss);
    1781             :         /* ss > 0 */
    1782         273 :         if ((!odd(ss) && D > 0) || (odd(ss) && D < 0))
    1783             :         {
    1784         168 :           long prec = nbits2prec(bitprec), q = labs(D);
    1785         168 :           ss = 1 - ss; /* <= 0 */
    1786         168 :           z = powrs(divrs(mppi(prec + EXTRAPRECWORD), q), 1-ss);
    1787         168 :           z = mulrr(shiftr(z, -ss), sqrtr_abs(utor(q, prec)));
    1788         168 :           z = gdiv(z, mpfactr(-ss, prec));
    1789         168 :           if (smodss(ss, 4) > 1) togglesign(z);
    1790         168 :           return gmul(z, lfunquadneg(D, ss));
    1791             :         }
    1792             :       }
    1793             :     }
    1794             :   }
    1795        3976 :   linit = lfuninit(lmisc, dom, der, bitprec);
    1796        3962 :   z = lfun_OK(linit, s, dom, bitprec);
    1797        3962 :   return gerepilecopy(av, z);
    1798             : }
    1799             : 
    1800             : /* given a t_SER a+x*s(x), return x*s(x), shallow */
    1801             : static GEN
    1802          42 : sersplit1(GEN s, GEN *head)
    1803             : {
    1804          42 :   long i, l = lg(s);
    1805             :   GEN y;
    1806          42 :   *head = simplify_shallow(mysercoeff(s, 0));
    1807          42 :   if (valp(s) > 0) return s;
    1808          28 :   y = cgetg(l-1, t_SER); y[1] = s[1];
    1809          28 :   setvalp(y, 1);
    1810         140 :   for (i=3; i < l; i++) gel(y,i-1) = gel(s,i);
    1811          28 :   return normalize(y);
    1812             : }
    1813             : 
    1814             : /* order of pole of Lambda at s (0 if regular point) */
    1815             : static long
    1816        2030 : lfunlambdaord(GEN linit, GEN s)
    1817             : {
    1818        2030 :   GEN tech = linit_get_tech(linit);
    1819        2030 :   if (linit_get_type(linit)==t_LDESC_PRODUCT)
    1820             :   {
    1821         224 :     GEN v = lfunprod_get_fact(linit_get_tech(linit));
    1822         224 :     GEN F = gel(v, 1), E = gel(v, 2), C = gel(v, 3);
    1823         224 :     long i, ex = 0, l = lg(F);
    1824         840 :     for (i = 1; i < l; i++)
    1825         616 :       ex += lfunlambdaord(gel(F,i), s) * (E[i]+C[i]);
    1826         224 :     return ex;
    1827             :   }
    1828        1806 :   if (ldata_get_residue(linit_get_ldata(linit)))
    1829             :   {
    1830         553 :     GEN r = lfunpoleresidue(lfun_get_Residue(tech), s);
    1831         553 :     if (r) return lg(r)-2;
    1832             :   }
    1833        1659 :   return 0;
    1834             : }
    1835             : 
    1836             : static GEN
    1837         112 : derser(GEN res, long m)
    1838             : {
    1839         112 :   long v = valp(res);
    1840         112 :   if (v > m) return gen_0;
    1841         112 :   if (v >= 0)
    1842         112 :     return gmul(mysercoeff(res, m), mpfact(m));
    1843             :   else
    1844           0 :     return derivn(res, m, -1);
    1845             : }
    1846             : 
    1847             : static GEN
    1848          56 : derservec(GEN x, long m)
    1849             : {
    1850          56 :   if (typ(x)==t_SER) return derser(x, m);
    1851         168 :   pari_APPLY_same(derser(gel(x,i),m))
    1852             : }
    1853             : 
    1854             : /* derivative of order m > 0 of L (flag = 0) or Lambda (flag = 1) */
    1855             : static GEN
    1856        1463 : lfunderiv(GEN lmisc, long m, GEN s, long flag, long bitprec)
    1857             : {
    1858        1463 :   pari_sp ltop = avma;
    1859        1463 :   GEN res, S = NULL, linit, dom;
    1860        1463 :   long der, prec = nbits2prec(bitprec);
    1861        1463 :   if (m <= 0) pari_err_DOMAIN("lfun", "D", "<=", gen_0, stoi(m));
    1862        1456 :   s = get_domain(s, &dom, &der);
    1863        1456 :   linit = lfuninit(lmisc, dom, der + m, bitprec);
    1864        1456 :   if (typ(s) == t_SER)
    1865             :   {
    1866          42 :     long v, l = lg(s)-1;
    1867             :     GEN sh;
    1868          42 :     if (valp(s) < 0) pari_err_DOMAIN("lfun","valuation", "<", gen_0, s);
    1869          42 :     S = sersplit1(s, &sh);
    1870          42 :     v = valp(S);
    1871          42 :     s = deg1ser_shallow(gen_1, sh, varn(S), m + (l+v-1)/v);
    1872             :   }
    1873             :   else
    1874             :   {
    1875        1414 :     long ex = lfunlambdaord(linit, s);
    1876             :     /* HACK: pretend lfuninit was done to right accuracy */
    1877        1414 :     s = deg1ser_shallow(gen_1, s, 0, m+1+ex);
    1878             :   }
    1879        1456 :   res = flag ? lfunlambda_OK(linit, s, dom, bitprec):
    1880         896 :                lfun_OK(linit, s, dom, bitprec);
    1881        1456 :   if (S)
    1882          42 :     res = gsubst(derivn(res, m, -1), varn(S), S);
    1883        1414 :   else if (typ(res)==t_SER)
    1884             :   {
    1885        1358 :     long v = valp(res);
    1886        1358 :     if (v > m) { set_avma(ltop); return gen_0; }
    1887        1351 :     if (v >= 0)
    1888        1225 :       res = gmul(mysercoeff(res, m), mpfact(m));
    1889             :     else
    1890         126 :       res = derivn(res, m, -1);
    1891             :   }
    1892          56 :   else if (is_ser(res))
    1893          56 :     res = derservec(res, m);
    1894        1449 :   return gerepilecopy(ltop, gprec_w(res, prec));
    1895             : }
    1896             : 
    1897             : GEN
    1898        1379 : lfunlambda0(GEN lmisc, GEN s, long der, long bitprec)
    1899             : {
    1900         560 :   return der? lfunderiv(lmisc, der, s, 1, bitprec)
    1901        1939 :             : lfunlambda(lmisc, s, bitprec);
    1902             : }
    1903             : 
    1904             : GEN
    1905        6188 : lfun0(GEN lmisc, GEN s, long der, long bitprec)
    1906             : {
    1907         903 :   return der? lfunderiv(lmisc, der, s, 0, bitprec)
    1908        7084 :             : lfun(lmisc, s, bitprec);
    1909             : }
    1910             : 
    1911             : GEN
    1912       12668 : lfunhardy(GEN lmisc, GEN t, long bitprec)
    1913             : {
    1914       12668 :   pari_sp ltop = avma;
    1915       12668 :   long prec = nbits2prec(bitprec), d;
    1916             :   GEN argz, z, linit, ldata, tech, dom, w2, k2, expot, h, a, k;
    1917             : 
    1918       12668 :   switch(typ(t))
    1919             :   {
    1920       12661 :     case t_INT: case t_FRAC: case t_REAL: break;
    1921           7 :     default: pari_err_TYPE("lfunhardy",t);
    1922             :   }
    1923             : 
    1924       12661 :   ldata = lfunmisc_to_ldata_shallow(lmisc);
    1925       12661 :   if (!is_linit(lmisc)) lmisc = ldata;
    1926       12661 :   k = ldata_get_k(ldata);
    1927       12661 :   d = ldata_get_degree(ldata);
    1928       12661 :   dom = mkvec3(gmul2n(k, -1), gen_0, gabs(t,LOWDEFAULTPREC));
    1929       12661 :   linit = lfuninit(lmisc, dom, 0, bitprec);
    1930       12661 :   tech = linit_get_tech(linit);
    1931       12661 :   w2 = lfun_get_w2(tech);
    1932       12661 :   k2 = lfun_get_k2(tech);
    1933       12661 :   expot = lfun_get_expot(tech);
    1934       12661 :   z = mkcomplex(k2, t);
    1935       12661 :   if (gequal0(k2))
    1936          14 :     argz = Pi2n(-1, prec);
    1937             :   else
    1938       12647 :     argz = gatan(gdiv(t, k2), prec);/* more accurate than garg since k/2 \in Q */
    1939             :   /* prec may have increased: don't lose accuracy if |z|^2 is exact */
    1940       12661 :   prec = precision(argz);
    1941       12661 :   a = gsub(gmulsg(d, gmul(t, gmul2n(argz,-1))),
    1942             :            gmul(expot,glog(gnorm(z),prec)));
    1943       12661 :   h = lfunlambda_OK(linit, z, dom, bitprec);
    1944       12661 :   if (!isint1(w2) && typ(ldata_get_dual(ldata))==t_INT)
    1945       10388 :     h = mulrealvec(h, w2);
    1946       12661 :   if (typ(h) == t_COMPLEX && gexpo(imag_i(h)) < -(bitprec >> 1))
    1947        2224 :     h = real_i(h);
    1948       12661 :   return gerepileupto(ltop, gmul(h, gexp(a, prec)));
    1949             : }
    1950             : 
    1951             : /* L = log(t); return  \sum_{i = 0}^{v-1}  R[-i-1] L^i/i! */
    1952             : static GEN
    1953        1120 : theta_pole_contrib(GEN R, long v, GEN L)
    1954             : {
    1955        1120 :   GEN s = mysercoeff(R,-v);
    1956             :   long i;
    1957        1183 :   for (i = v-1; i >= 1; i--)
    1958          63 :     s = gadd(mysercoeff(R,-i), gdivgs(gmul(s,L), i));
    1959        1120 :   return s;
    1960             : }
    1961             : /* subtract successively rather than adding everything then subtracting.
    1962             :  * The polar part is "large" and suffers from cancellation: a little stabler
    1963             :  * this way */
    1964             : static GEN
    1965        3353 : theta_add_polar_part(GEN S, GEN R, GEN t, long prec)
    1966             : {
    1967        3353 :   GEN logt = NULL;
    1968        3353 :   long j, l = lg(R);
    1969        4473 :   for (j = 1; j < l; j++)
    1970             :   {
    1971        1120 :     GEN Rj = gel(R,j), b = gel(Rj,1), Rb = gel(Rj,2);
    1972        1120 :     long v = -valp(Rb);
    1973        1120 :     if (v > 1 && !logt) logt = glog(t, prec);
    1974        1120 :     S = gsub(S, gmul(theta_pole_contrib(Rb,v,logt), gpow(t,b,prec)));
    1975             :   }
    1976        3353 :   return S;
    1977             : }
    1978             : 
    1979             : static long
    1980        2611 : lfuncheckfeq_i(GEN theta, GEN thetad, GEN t0, GEN t0i, long bitprec)
    1981             : {
    1982        2611 :   GEN ldata = linit_get_ldata(theta);
    1983             :   GEN S0, S0i, w, eno;
    1984        2611 :   long prec = nbits2prec(bitprec);
    1985        2611 :   if (thetad)
    1986          42 :     S0 = lfuntheta(thetad, t0, 0, bitprec);
    1987             :   else
    1988        2569 :     S0 = conj_i(lfuntheta(theta, conj_i(t0), 0, bitprec));
    1989        2611 :   S0i = lfuntheta(theta, t0i, 0, bitprec);
    1990             : 
    1991        2611 :   eno = ldata_get_rootno(ldata);
    1992        2611 :   if (ldata_get_residue(ldata))
    1993             :   {
    1994         546 :     GEN R = theta_get_R(linit_get_tech(theta));
    1995         546 :     if (gequal0(R))
    1996             :     {
    1997             :       GEN v, r;
    1998          63 :       if (ldata_get_type(ldata) == t_LFUN_NF)
    1999             :       { /* inefficient since theta not needed; no need to optimize for this
    2000             :            (artificial) query [e.g. lfuncheckfeq(t_POL)] */
    2001          21 :         GEN T = gel(ldata_get_an(ldata), 2);
    2002          21 :         GEN L = lfunzetakinit(T,zerovec(3),0,0,bitprec);
    2003          21 :         return lfuncheckfeq(L,t0,bitprec);
    2004             :       }
    2005          42 :       v = lfunrootres(theta, bitprec);
    2006          42 :       r = gel(v,1);
    2007          42 :       if (gequal0(eno)) eno = gel(v,3);
    2008          42 :       R = lfunrtoR_i(ldata, r, eno, nbits2prec(bitprec));
    2009             :     }
    2010         525 :     S0i = theta_add_polar_part(S0i, R, t0, prec);
    2011             :   }
    2012        2590 :   if (gequal0(S0i) || gequal0(S0)) pari_err_PREC("lfuncheckfeq");
    2013             : 
    2014        2590 :   w = gdivvec(S0i, gmul(S0, gpow(t0, ldata_get_k(ldata), prec)));
    2015             :   /* missing rootno: guess it */
    2016        2590 :   if (gequal0(eno)) eno = lfunrootno(theta, bitprec);
    2017        2590 :   w = gsubvec(w, eno);
    2018        2590 :   if (thetad) w = gdivvec(w, eno); /* |eno| may be large in non-dual case */
    2019        2590 :   return gexpo(w);
    2020             : }
    2021             : 
    2022             : /* Check whether the coefficients, conductor, weight, polar part and root
    2023             :  * number are compatible with the functional equation at t0 and 1/t0.
    2024             :  * Different from lfunrootres. */
    2025             : long
    2026        2681 : lfuncheckfeq(GEN lmisc, GEN t0, long bitprec)
    2027             : {
    2028             :   GEN ldata, theta, thetad, t0i;
    2029             :   pari_sp av;
    2030             : 
    2031        2681 :   if (is_linit(lmisc) && linit_get_type(lmisc)==t_LDESC_PRODUCT)
    2032             :   {
    2033         112 :     GEN v = lfunprod_get_fact(linit_get_tech(lmisc)), F = gel(v,1);
    2034         112 :     long i, b = -bitprec, l = lg(F);
    2035         413 :     for (i = 1; i < l; i++) b = maxss(b, lfuncheckfeq(gel(F,i), t0, bitprec));
    2036         112 :     return b;
    2037             :   }
    2038        2569 :   av = avma;
    2039        2569 :   if (!t0)
    2040             :   { /* ~Pi/3 + I/7, some random complex number */
    2041        2499 :     t0 = mkcomplex(sstoQ(355,339), sstoQ(1,7));
    2042        2499 :     t0i = ginv(t0);
    2043             :   }
    2044          70 :   else if (gcmpgs(gnorm(t0), 1) < 0) { t0i = t0; t0 = ginv(t0); }
    2045          63 :   else t0i = ginv(t0);
    2046             :   /* |t0| >= 1 */
    2047        2569 :   theta = lfunthetacheckinit(lmisc, t0i, 0, bitprec);
    2048        2569 :   ldata = linit_get_ldata(theta);
    2049        2569 :   thetad = theta_dual(theta, ldata_get_dual(ldata));
    2050        2569 :   return gc_long(av, lfuncheckfeq_i(theta, thetad, t0, t0i, bitprec));
    2051             : }
    2052             : 
    2053             : /*******************************************************************/
    2054             : /*       Compute root number and residues                          */
    2055             : /*******************************************************************/
    2056             : /* round root number to \pm 1 if close to integer. */
    2057             : static GEN
    2058        3038 : ropm1(GEN eno, long prec)
    2059             : {
    2060             :   long e;
    2061        3038 :   GEN r = grndtoi(eno, &e);
    2062        3038 :   return (e < -prec2nbits(prec)/2)? r: eno;
    2063             : }
    2064             : 
    2065             : /* theta for t=1/sqrt(2) and t2==2t simultaneously, saving 25% of the work.
    2066             :  * Assume correct initialization (no thetacheck) */
    2067             : static void
    2068         210 : lfunthetaspec(GEN linit, long bitprec, GEN *pv, GEN *pv2)
    2069             : {
    2070         210 :   pari_sp av = avma, av2;
    2071             :   GEN t, Vga, an, K, ldata, thetainit, v, v2, vroots;
    2072             :   long L, prec, n, d;
    2073             : 
    2074         210 :   ldata = linit_get_ldata(linit);
    2075         210 :   thetainit = linit_get_tech(linit);
    2076         210 :   prec = nbits2prec(bitprec);
    2077         210 :   Vga = ldata_get_gammavec(ldata); d = lg(Vga)-1;
    2078         210 :   if (Vgaeasytheta(Vga))
    2079             :   {
    2080         196 :     GEN v2 = sqrtr(real2n(1, nbits2prec(bitprec)));
    2081         196 :     GEN v = shiftr(v2,-1);
    2082         196 :     *pv = lfuntheta(linit, v,  0, bitprec);
    2083         196 :     *pv2= lfuntheta(linit, v2, 0, bitprec);
    2084         196 :     return;
    2085             :   }
    2086          14 :   an = RgV_kill0( theta_get_an(thetainit) );
    2087          14 :   L = lg(an)-1;
    2088             :   /* to compute theta(1/sqrt(2)) */
    2089          14 :   t = ginv(gsqrt(gmul2n(ldata_get_conductor(ldata), 1), prec));
    2090             :   /* t = 1/sqrt(2N) */
    2091             : 
    2092             :   /* From then on, the code is generic and could be used to compute
    2093             :    * theta(t) / theta(2t) without assuming t = 1/sqrt(2) */
    2094          14 :   K = theta_get_K(thetainit);
    2095          14 :   vroots = mkvroots(d, L, prec);
    2096          14 :   t = gpow(t, gdivgs(gen_2, d), prec); /* rt variant: t->t^(2/d) */
    2097             :   /* v = \sum_{n <= L, n odd} a_n K(nt) */
    2098        6790 :   for (v = gen_0, n = 1; n <= L; n+=2)
    2099             :   {
    2100        6776 :     GEN tn, Kn, a = gel(an, n);
    2101             : 
    2102        6776 :     if (!a) continue;
    2103        5677 :     av2 = avma;
    2104        5677 :     tn = gmul(t, gel(vroots,n));
    2105        5677 :     Kn = gammamellininvrt(K, tn, bitprec);
    2106        5677 :     v = gerepileupto(av2, gadd(v, gmul(a,Kn)));
    2107             :   }
    2108             :   /* v += \sum_{n <= L, n even} a_n K(nt), v2 = \sum_{n <= L/2} a_n K(2n t) */
    2109        6783 :   for (v2 = gen_0, n = 1; n <= L/2; n++)
    2110             :   {
    2111        6769 :     GEN t2n, K2n, a = gel(an, n), a2 = gel(an,2*n);
    2112             : 
    2113        6769 :     if (!a && !a2) continue;
    2114        5712 :     av2 = avma;
    2115        5712 :     t2n = gmul(t, gel(vroots,2*n));
    2116        5712 :     K2n = gerepileupto(av2, gammamellininvrt(K, t2n, bitprec));
    2117        5712 :     if (a) v2 = gadd(v2, gmul(a, K2n));
    2118        5712 :     if (a2) v = gadd(v,  gmul(a2,K2n));
    2119             :   }
    2120          14 :   *pv = v;
    2121          14 :   *pv2 = v2;
    2122          14 :   gerepileall(av, 2, pv,pv2);
    2123             : }
    2124             : 
    2125             : static GEN
    2126         210 : Rtor(GEN a, GEN R, GEN ldata, long prec)
    2127             : {
    2128         210 :   GEN FVga = gammafactor(ldata_get_gammavec(ldata));
    2129         210 :   GEN Na = gpow(ldata_get_conductor(ldata), gdivgs(a,2), prec);
    2130         210 :   return gdiv(R, gmul(Na, gammafactproduct(FVga, a, prec)));
    2131             : }
    2132             : 
    2133             : /* v = theta~(t), vi = theta(1/t) */
    2134             : static GEN
    2135        2828 : get_eno(GEN R, GEN k, GEN t, GEN v, GEN vi, long vx, long bitprec, long force)
    2136             : {
    2137        2828 :   long prec = nbits2prec(bitprec);
    2138        2828 :   GEN a0, a1, S = deg1pol(gmul(gpow(t,k,prec), gneg(v)), vi, vx);
    2139             : 
    2140        2828 :   S = theta_add_polar_part(S, R, t, prec);
    2141        2828 :   if (typ(S) != t_POL || degpol(S) != 1) return NULL;
    2142        2828 :   a1 = gel(S,3); if (!force && gexpo(a1) < -bitprec/4) return NULL;
    2143        2828 :   a0 = gel(S,2);
    2144        2828 :   return gdivvec(a0, gneg(a1));
    2145             : 
    2146             : }
    2147             : /* Return w using theta(1/t) - w t^k \bar{theta}(t) = polar_part(t,w).
    2148             :  * The full Taylor development of L must be known */
    2149             : GEN
    2150        2828 : lfunrootno(GEN linit, long bitprec)
    2151             : {
    2152             :   GEN ldata, t, eno, v, vi, R, thetad;
    2153        2828 :   long c = 0, prec = nbits2prec(bitprec), vx = fetch_var();
    2154             :   GEN k;
    2155             :   pari_sp av;
    2156             : 
    2157             :   /* initialize for t > 1/sqrt(2) */
    2158        2828 :   linit = lfunthetacheckinit(linit, dbltor(sqrt(0.5)), 0, bitprec);
    2159        2828 :   ldata = linit_get_ldata(linit);
    2160        2828 :   k = ldata_get_k(ldata);
    2161        2842 :   R = ldata_get_residue(ldata)? lfunrtoR_eno(ldata, pol_x(vx), prec)
    2162        2828 :                               : cgetg(1, t_VEC);
    2163        2828 :   t = gen_1;
    2164        2828 :   v = lfuntheta(linit, t, 0, bitprec);
    2165        2828 :   thetad = theta_dual(linit, ldata_get_dual(ldata));
    2166        2828 :   vi = !thetad ? conj_i(v): lfuntheta(thetad, t, 0, bitprec);
    2167        2828 :   eno = get_eno(R,k,t,vi,v, vx, bitprec, 0);
    2168        2828 :   if (!eno && !thetad)
    2169             :   { /* t = sqrt(2), vi = theta(1/t), v = theta(t) */
    2170           0 :     lfunthetaspec(linit, bitprec, &vi, &v);
    2171           0 :     t = sqrtr(utor(2, prec));
    2172           0 :     eno = get_eno(R,k,t,conj_i(v),vi, vx, bitprec, 0);
    2173             :   }
    2174        2828 :   av = avma;
    2175        2828 :   while (!eno)
    2176             :   {
    2177           0 :     t = addsr(1, shiftr(utor(pari_rand(), prec), -2-BITS_IN_LONG));
    2178             :     /* t in [1,1.25[ */
    2179           0 :     v = thetad? lfuntheta(thetad, t, 0, bitprec)
    2180           0 :               : conj_i(lfuntheta(linit, t, 0, bitprec));
    2181           0 :     vi = lfuntheta(linit, ginv(t), 0, bitprec);
    2182           0 :     eno = get_eno(R,k,t,v,vi, vx, bitprec, c++ == 5);
    2183           0 :     set_avma(av);
    2184             :   }
    2185        2828 :   delete_var(); return ropm1(eno,prec);
    2186             : }
    2187             : 
    2188             : /* Find root number and/or residues when L-function coefficients and
    2189             :    conductor are known. For the moment at most a single residue allowed. */
    2190             : GEN
    2191        3206 : lfunrootres(GEN data, long bitprec)
    2192             : {
    2193        3206 :   pari_sp ltop = avma;
    2194             :   GEN k, w, r, R, a, b, e, v, v2, be, ldata, linit;
    2195             :   long prec;
    2196             : 
    2197        3206 :   ldata = lfunmisc_to_ldata_shallow(data);
    2198        3206 :   r = ldata_get_residue(ldata);
    2199        3206 :   k = ldata_get_k(ldata);
    2200        3206 :   w = ldata_get_rootno(ldata);
    2201        3206 :   if (r) r = normalize_simple_pole(r, k);
    2202        3206 :   if (!r || residues_known(r))
    2203             :   {
    2204        2996 :     if (isintzero(w)) w = lfunrootno(data, bitprec);
    2205        2996 :     if (!r)
    2206        1442 :       r = R = gen_0;
    2207             :     else
    2208        1554 :       R = lfunrtoR_eno(ldata, w, nbits2prec(bitprec));
    2209        2996 :     return gerepilecopy(ltop, mkvec3(r, R, w));
    2210             :   }
    2211         210 :   linit = lfunthetacheckinit(data, dbltor(sqrt(0.5)), 0, bitprec);
    2212         210 :   prec = nbits2prec(bitprec);
    2213         210 :   if (lg(r) > 2) pari_err_IMPL("multiple poles in lfunrootres");
    2214             :   /* Now residue unknown, and r = [[be,0]]. */
    2215         210 :   be = gmael(r, 1, 1);
    2216         210 :   if (ldata_isreal(ldata) && gequalm1(w))
    2217           0 :     R = lfuntheta(linit, gen_1, 0, bitprec);
    2218             :   else
    2219             :   {
    2220         210 :     lfunthetaspec(linit, bitprec, &v2, &v);
    2221         210 :     if (gequal(gmulsg(2, be), k)) pari_err_IMPL("pole at k/2 in lfunrootres");
    2222         210 :     if (gequal(be, k))
    2223             :     {
    2224           7 :       GEN p2k = gpow(gen_2,k,prec);
    2225           7 :       a = conj_i(gsub(gmul(p2k, v), v2));
    2226           7 :       b = subiu(p2k, 1);
    2227           7 :       e = gmul(gsqrt(p2k, prec), gsub(v2, v));
    2228             :     }
    2229             :     else
    2230             :     {
    2231         203 :       GEN p2k = gpow(gen_2,k,prec);
    2232         203 :       GEN tk2 = gsqrt(p2k, prec);
    2233         203 :       GEN tbe = gpow(gen_2, be, prec);
    2234         203 :       GEN tkbe = gpow(gen_2, gdivgs(gsub(k, be), 2), prec);
    2235         203 :       a = conj_i(gsub(gmul(tbe, v), v2));
    2236         203 :       b = gsub(gdiv(tbe, tkbe), tkbe);
    2237         203 :       e = gsub(gmul(gdiv(tbe, tk2), v2), gmul(tk2, v));
    2238             :     }
    2239         210 :     if (!isintzero(w)) R = gdiv(gsub(e, gmul(a, w)), b);
    2240             :     else
    2241             :     { /* Now residue unknown, r = [[be,0]], and w unknown. */
    2242           0 :       GEN t0  = mkfrac(stoi(11),stoi(10));
    2243           0 :       GEN th1 = lfuntheta(linit, t0,  0, bitprec);
    2244           0 :       GEN th2 = lfuntheta(linit, ginv(t0), 0, bitprec);
    2245           0 :       GEN tbe = gpow(t0, gmulsg(2, be), prec);
    2246           0 :       GEN tkbe = gpow(t0, gsub(k, be), prec);
    2247           0 :       GEN tk2 = gpow(t0, k, prec);
    2248           0 :       GEN c = conj_i(gsub(gmul(tbe, th1), th2));
    2249           0 :       GEN d = gsub(gdiv(tbe, tkbe), tkbe);
    2250           0 :       GEN f = gsub(gmul(gdiv(tbe, tk2), th2), gmul(tk2, th1));
    2251           0 :       GEN D = gsub(gmul(a, d), gmul(b, c));
    2252           0 :       w = gdiv(gsub(gmul(d, e), gmul(b, f)), D);
    2253           0 :       R = gdiv(gsub(gmul(a, f), gmul(c, e)), D);
    2254             :     }
    2255             :   }
    2256         210 :   r = normalize_simple_pole(Rtor(be, R, ldata, prec), be);
    2257         210 :   R = lfunrtoR_i(ldata, r, w, prec);
    2258         210 :   return gerepilecopy(ltop, mkvec3(r, R, ropm1(w, prec)));
    2259             : }
    2260             : 
    2261             : /*******************************************************************/
    2262             : /*                           Zeros                                 */
    2263             : /*******************************************************************/
    2264             : struct lhardyz_t {
    2265             :   long bitprec, prec;
    2266             :   GEN linit;
    2267             : };
    2268             : 
    2269             : static GEN
    2270       12080 : lfunhardyzeros(void *E, GEN t)
    2271             : {
    2272       12080 :   struct lhardyz_t *S = (struct lhardyz_t*)E;
    2273       12080 :   return gprec_wensure(lfunhardy(S->linit, t, S->bitprec), S->prec);
    2274             : }
    2275             : 
    2276             : /* initialize for computation on critical line up to height h, zero
    2277             :  * of order <= m */
    2278             : static GEN
    2279         504 : lfuncenterinit(GEN lmisc, double h, long m, long bitprec)
    2280             : {
    2281         504 :   if (m < 0)
    2282             :   { /* choose a sensible default */
    2283         504 :     if (!is_linit(lmisc) || linit_get_type(lmisc) != t_LDESC_INIT) m = 4;
    2284             :     else
    2285             :     {
    2286         427 :       GEN domain = lfun_get_domain(linit_get_tech(lmisc));
    2287         427 :       m = domain_get_der(domain);
    2288             :     }
    2289             :   }
    2290         504 :   return lfuninit(lmisc, mkvec(dbltor(h)), m, bitprec);
    2291             : }
    2292             : 
    2293             : long
    2294         490 : lfunorderzero(GEN lmisc, long m, long bitprec)
    2295             : {
    2296         490 :   pari_sp ltop = avma;
    2297             :   GEN eno, ldata, linit, k2;
    2298             :   long G, c0, c, st;
    2299             : 
    2300         490 :   if (is_linit(lmisc) && linit_get_type(lmisc) == t_LDESC_PRODUCT)
    2301             :   {
    2302          63 :     GEN M = gmael(linit_get_tech(lmisc), 2,1);
    2303          63 :     long i, l = lg(M);
    2304         231 :     for (c=0, i=1; i < l; i++) c += lfunorderzero(gel(M,i), m, bitprec);
    2305          63 :     return c;
    2306             :   }
    2307         427 :   linit = lfuncenterinit(lmisc, 0, m, bitprec);
    2308         427 :   ldata = linit_get_ldata(linit);
    2309         427 :   eno = ldata_get_rootno(ldata);
    2310         427 :   if (typ(eno) == t_VEC) pari_err_TYPE("lfunorderzero [vector-valued]", lmisc);
    2311         406 :   k2 = gmul2n(ldata_get_k(ldata), -1);
    2312         406 :   G = -bitprec/2;
    2313         406 :   c0 = 0; st = 1;
    2314         406 :   if (ldata_isreal(ldata)) { st = 2; if (!gequal1(eno)) c0 = 1; }
    2315         406 :   for (c = c0;; c += st)
    2316         434 :     if (gexpo(lfun0(linit, k2, c, bitprec)) > G) return gc_long(ltop, c);
    2317             : }
    2318             : 
    2319             : /* assume T1 * T2 > 0, T1 <= T2 */
    2320             : static void
    2321          84 : lfunzeros_i(struct lhardyz_t *S, GEN *pw, long *ct, GEN T1, GEN T2, long d,
    2322             :             GEN cN, GEN pi2, GEN pi2div, long precinit, long prec)
    2323             : {
    2324          84 :   GEN T = T1, w = *pw;
    2325          84 :   long W = lg(w)-1, s = gsigne(lfunhardyzeros(S, T1));
    2326             :   for(;;)
    2327         392 :   {
    2328         476 :     pari_sp av = avma;
    2329             :     GEN D, T0, z;
    2330         476 :     D = gcmp(T, pi2) < 0? cN
    2331         476 :                         : gadd(cN, gmulsg(d, glog(gdiv(T, pi2), prec)));
    2332         476 :     D = gdiv(pi2div, D);
    2333             :     for(;;)
    2334        7721 :     {
    2335             :       long s0;
    2336        8197 :       T0 = T; T = gadd(T, D);
    2337        8197 :       if (gcmp(T, T2) >= 0) T = T2;
    2338        8197 :       s0 = gsigne(lfunhardyzeros(S, T));
    2339        8197 :       if (s0 != s) { s = s0; break; }
    2340        7805 :       if (T == T2) { setlg(w, *ct); *pw = w; return; }
    2341             :     }
    2342         392 :     z = zbrent(S, lfunhardyzeros, T0, T, prec); /* T <= T2 */
    2343         392 :     gerepileall(av, 2, &T, &z);
    2344         392 :     if (*ct > W) { W *= 2; w = vec_lengthen(w, W); }
    2345         392 :     if (typ(z) == t_REAL) z  = rtor(z, precinit);
    2346         392 :     gel(w, (*ct)++) = z;
    2347             :   }
    2348             :   setlg(w, *ct); *pw = w;
    2349             : }
    2350             : GEN
    2351          84 : lfunzeros(GEN ldata, GEN lim, long divz, long bitprec)
    2352             : {
    2353          84 :   pari_sp ltop = avma;
    2354             :   GEN linit, pi2, pi2div, cN, w, T, h1, h2;
    2355          84 :   long i, d, NEWD, c, ct, s1, s2, prec, prec0 = nbits2prec(bitprec);
    2356             :   double maxt;
    2357             :   struct lhardyz_t S;
    2358             : 
    2359          84 :   if (is_linit(ldata) && linit_get_type(ldata) == t_LDESC_PRODUCT)
    2360             :   {
    2361           0 :     GEN M = gmael(linit_get_tech(ldata), 2,1);
    2362           0 :     long l = lg(M);
    2363           0 :     w = cgetg(l, t_VEC);
    2364           0 :     for (i = 1; i < l; i++) gel(w,i) = lfunzeros(gel(M,i), lim, divz, bitprec);
    2365           0 :     return gerepileupto(ltop, vecsort0(shallowconcat1(w), NULL, 0));
    2366             :   }
    2367          84 :   if (typ(lim) == t_VEC)
    2368             :   {
    2369          49 :     if (lg(lim) != 3 || gcmp(gel(lim,1),gel(lim,2)) >= 0)
    2370           7 :       pari_err_TYPE("lfunzeros",lim);
    2371          42 :     h1 = gel(lim,1);
    2372          42 :     h2 = gel(lim,2);
    2373          42 :     maxt = maxdd(fabs(gtodouble(h1)), fabs(gtodouble(h2)));
    2374             :   }
    2375             :   else
    2376             :   {
    2377          35 :     if (gcmp(lim,gen_0) <= 0) pari_err_TYPE("lfunzeros",lim);
    2378          35 :     h1 = gen_0;
    2379          35 :     h2 = lim;
    2380          35 :     maxt = gtodouble(h2);
    2381             :   }
    2382          77 :   S.linit = linit = lfuncenterinit(ldata, maxt, -1, bitprec);
    2383          77 :   S.bitprec = bitprec;
    2384          77 :   S.prec = prec0;
    2385          77 :   ldata = linit_get_ldata(linit);
    2386          77 :   d = ldata_get_degree(ldata);
    2387             : 
    2388          77 :   NEWD = minss((long) ceil(bitprec + (M_PI/(4*M_LN2)) * d * maxt),
    2389             :                lfun_get_bitprec(linit_get_tech(linit)));
    2390          77 :   prec = nbits2prec(NEWD);
    2391          77 :   cN = gdiv(ldata_get_conductor(ldata), gpowgs(Pi2n(-1, prec), d));
    2392          77 :   cN = gexpo(cN) >= 0? gaddsg(d, gmulsg(2, glog(cN, prec))): utoi(d);
    2393          77 :   pi2 = Pi2n(1, prec);
    2394          77 :   pi2div = gdivgs(pi2, labs(divz));
    2395          77 :   s1 = gsigne(h1);
    2396          77 :   s2 = gsigne(h2);
    2397          77 :   w = cgetg(100+1, t_VEC); c = 1; ct = 0; T = NULL;
    2398          77 :   if (s1 <= 0 && s2 >= 0)
    2399             :   {
    2400          56 :     GEN r = ldata_get_residue(ldata);
    2401          56 :     if (!r || gequal0(r))
    2402             :     {
    2403          35 :       ct = lfunorderzero(linit, -1, bitprec);
    2404          35 :       if (ct) T = real2n(-prec2nbits(prec) / (2*ct), prec);
    2405             :     }
    2406             :   }
    2407          77 :   if (s1 <= 0)
    2408             :   {
    2409          63 :     if (s1 < 0)
    2410          21 :       lfunzeros_i(&S, &w, &c, h1, T? negr(T): h2,
    2411             :                   d, cN, pi2, pi2div, prec0, prec);
    2412          63 :     if (ct)
    2413             :     {
    2414          21 :       long n = lg(w)-1;
    2415          21 :       if (c + ct >= n) w = vec_lengthen(w, n + ct);
    2416          84 :       for (i = 1; i <= ct; i++) gel(w,c++) = gen_0;
    2417             :     }
    2418             :   }
    2419          77 :   if (s2 > 0 && (T || s1 >= 0))
    2420          63 :     lfunzeros_i(&S, &w, &c, T? T: h1, h2, d, cN, pi2, pi2div, prec0, prec);
    2421          77 :   return gerepilecopy(ltop, w);
    2422             : }
    2423             : 
    2424             : /*******************************************************************/
    2425             : /*       Guess conductor                                           */
    2426             : /*******************************************************************/
    2427             : struct huntcond_t {
    2428             :   GEN k;
    2429             :   GEN theta, thetad;
    2430             :   GEN *pM, *psqrtM, *pMd, *psqrtMd;
    2431             : };
    2432             : 
    2433             : static void
    2434       11162 : condset(struct huntcond_t *S, GEN M, long prec)
    2435             : {
    2436       11162 :   *(S->pM) = M;
    2437       11162 :   *(S->psqrtM) = gsqrt(ginv(M), prec);
    2438       11162 :   if (S->thetad != S->theta)
    2439             :   {
    2440           0 :     *(S->pMd) = *(S->pM);
    2441           0 :     *(S->psqrtMd) = *(S->psqrtM);
    2442             :   }
    2443       11162 : }
    2444             : 
    2445             : /* M should eventually converge to N, the conductor. L has no pole. */
    2446             : static GEN
    2447        6401 : wrap1(void *E, GEN M)
    2448             : {
    2449        6401 :   struct huntcond_t *S = (struct huntcond_t*)E;
    2450             :   GEN thetainit, tk, p1, p1inv;
    2451        6401 :   GEN t = mkfrac(stoi(11), stoi(10));
    2452             :   long prec, bitprec;
    2453             : 
    2454        6401 :   thetainit = linit_get_tech(S->theta);
    2455        6401 :   bitprec = theta_get_bitprec(thetainit);
    2456        6401 :   prec = nbits2prec(bitprec);
    2457        6401 :   condset(S, M, prec);
    2458        6401 :   tk = gpow(t, S->k, prec);
    2459        6401 :   p1 = lfuntheta(S->thetad, t, 0, bitprec);
    2460        6401 :   p1inv = lfuntheta(S->theta, ginv(t), 0, bitprec);
    2461        6401 :   return glog(gabs(gmul(tk, gdiv(p1, p1inv)), prec), prec);
    2462             : }
    2463             : 
    2464             : /* M should eventually converge to N, the conductor. L has a pole. */
    2465             : static GEN
    2466        4719 : wrap2(void *E, GEN M)
    2467             : {
    2468        4719 :   struct huntcond_t *S = (struct huntcond_t*)E;
    2469             :   GEN t1k, t2k, p1, p1inv, p2, p2inv, thetainit, R;
    2470        4719 :   GEN t1 = mkfrac(stoi(11), stoi(10)), t2 = mkfrac(stoi(13), stoi(11));
    2471             :   GEN t1be, t2be, t1bemk, t2bemk, t1kmbe, t2kmbe;
    2472             :   GEN F11, F12, F21, F22, P1, P2, res;
    2473             :   long prec, bitprec;
    2474        4719 :   GEN k = S->k;
    2475             : 
    2476        4719 :   thetainit = linit_get_tech(S->theta);
    2477        4719 :   bitprec = theta_get_bitprec(thetainit);
    2478        4719 :   prec = nbits2prec(bitprec);
    2479        4719 :   condset(S, M, prec);
    2480             : 
    2481        4719 :   p1 = lfuntheta(S->thetad, t1, 0, bitprec);
    2482        4719 :   p2 = lfuntheta(S->thetad, t2, 0, bitprec);
    2483        4719 :   p1inv = lfuntheta(S->theta, ginv(t1), 0, bitprec);
    2484        4719 :   p2inv = lfuntheta(S->theta, ginv(t2), 0, bitprec);
    2485        4719 :   t1k = gpow(t1, k, prec);
    2486        4719 :   t2k = gpow(t2, k, prec);
    2487        4719 :   R = theta_get_R(thetainit);
    2488        4719 :   if (typ(R) == t_VEC)
    2489             :   {
    2490           0 :     GEN be = gmael(R, 1, 1);
    2491           0 :     t1be = gpow(t1, be, prec); t1bemk = gdiv(gsqr(t1be), t1k);
    2492           0 :     t2be = gpow(t2, be, prec); t2bemk = gdiv(gsqr(t2be), t2k);
    2493           0 :     t1kmbe = gdiv(t1k, t1be);
    2494           0 :     t2kmbe = gdiv(t2k, t2be);
    2495             :   }
    2496             :   else
    2497             :   { /* be = k */
    2498        4719 :     t1be = t1k; t1bemk = t1k; t1kmbe = gen_1;
    2499        4719 :     t2be = t2k; t2bemk = t2k; t2kmbe = gen_1;
    2500             :   }
    2501        4719 :   F11 = conj_i(gsub(gmul(gsqr(t1be), p1), p1inv));
    2502        4719 :   F12 = conj_i(gsub(gmul(gsqr(t2be), p2), p2inv));
    2503        4719 :   F21 = gsub(gmul(t1k, p1), gmul(t1bemk, p1inv));
    2504        4719 :   F22 = gsub(gmul(t2k, p2), gmul(t2bemk, p2inv));
    2505        4719 :   P1 = gsub(gmul(t1bemk, t1be), t1kmbe);
    2506        4719 :   P2 = gsub(gmul(t2bemk, t2be), t2kmbe);
    2507        4719 :   res = gdiv(gsub(gmul(P2,F21), gmul(P1,F22)),
    2508             :              gsub(gmul(P2,F11), gmul(P1,F12)));
    2509        4719 :   return glog(gabs(res, prec), prec);
    2510             : }
    2511             : 
    2512             : /* If flag = 0 (default) return all conductors found as integers. If
    2513             : flag = 1, return the approximations, not the integers. If flag = 2,
    2514             : return all, even nonintegers. */
    2515             : 
    2516             : static GEN
    2517          84 : checkconductor(GEN v, long bit, long flag)
    2518             : {
    2519             :   GEN w;
    2520          84 :   long e, j, k, l = lg(v);
    2521          84 :   if (flag == 2) return v;
    2522          84 :   w = cgetg(l, t_VEC);
    2523         322 :   for (j = k = 1; j < l; j++)
    2524             :   {
    2525         238 :     GEN N = grndtoi(gel(v,j), &e);
    2526         238 :     if (e < -bit) gel(w,k++) = flag ? gel(v,j): N;
    2527             :   }
    2528          84 :   if (k == 2) return gel(w,1);
    2529           7 :   setlg(w,k); return w;
    2530             : }
    2531             : 
    2532             : static GEN
    2533          98 : parse_maxcond(GEN maxN)
    2534             : {
    2535             :   GEN M;
    2536          98 :   if (!maxN)
    2537          49 :     M = utoipos(10000);
    2538          49 :   else if (typ(maxN) == t_VEC)
    2539             :   {
    2540          14 :     if (!RgV_is_ZV(maxN)) pari_err_TYPE("lfunconductor",maxN);
    2541          14 :     return ZV_sort(maxN);
    2542             :   }
    2543             :   else
    2544          35 :     M = maxN;
    2545          84 :   return (typ(M) == t_INT)? addiu(M, 1): gceil(M);
    2546             : }
    2547             : 
    2548             : GEN
    2549          98 : lfunconductor(GEN data, GEN maxcond, long flag, long bitprec)
    2550             : {
    2551             :   struct huntcond_t S;
    2552          98 :   pari_sp av = avma;
    2553          98 :   GEN ldata = lfunmisc_to_ldata_shallow(data);
    2554          98 :   GEN ld, r, v, theta, thetad, M, tdom, t0 = NULL, t0i = NULL;
    2555             :   GEN (*eval)(void *, GEN);
    2556             :   long prec;
    2557          98 :   M = parse_maxcond(maxcond);
    2558          98 :   r = ldata_get_residue(ldata);
    2559          98 :   if (typ(M) == t_VEC) /* select in list */
    2560             :   {
    2561          14 :     if (lg(M) == 1) { set_avma(av); return cgetg(1,t_VEC); }
    2562           7 :     eval = NULL; tdom = dbltor(0.7);
    2563             :   }
    2564          84 :   else if (!r) { eval = wrap1; tdom = sstoQ(10,11); }
    2565             :   else
    2566             :   {
    2567          21 :     if (typ(r) == t_VEC && lg(r) > 2)
    2568           0 :       pari_err_IMPL("multiple poles in lfunconductor");
    2569          21 :     eval = wrap2; tdom = sstoQ(11,13);
    2570             :   }
    2571          91 :   if (eval) bitprec += bitprec/2;
    2572          91 :   prec = nbits2prec(bitprec);
    2573          91 :   ld = shallowcopy(ldata);
    2574          91 :   gel(ld, 5) = eval? M: gel(M,lg(M)-1);
    2575          91 :   theta = lfunthetainit_i(ld, tdom, 0, bitprec);
    2576          91 :   thetad = theta_dual(theta, ldata_get_dual(ldata));
    2577          91 :   gel(theta,3) = shallowcopy(linit_get_tech(theta));
    2578          91 :   S.k = ldata_get_k(ldata);
    2579          91 :   S.theta = theta;
    2580          91 :   S.thetad = thetad? thetad: theta;
    2581          91 :   S.pM = &gel(linit_get_ldata(theta),5);
    2582          91 :   S.psqrtM = &gel(linit_get_tech(theta),7);
    2583          91 :   if (thetad)
    2584             :   {
    2585           0 :     S.pMd = &gel(linit_get_ldata(thetad),5);
    2586           0 :     S.psqrtMd = &gel(linit_get_tech(thetad),7);
    2587             :   }
    2588          91 :   if (!eval)
    2589             :   {
    2590           7 :     long i, besti = 0, beste = -10, l = lg(M);
    2591           7 :     t0 = sstoQ(11,10); t0i = sstoQ(10,11);
    2592          49 :     for (i = 1; i < l; i++)
    2593             :     {
    2594          42 :       pari_sp av2 = avma;
    2595             :       long e;
    2596          42 :       condset(&S, gel(M,i), prec);
    2597          42 :       e = lfuncheckfeq_i(theta, thetad, t0, t0i, bitprec);
    2598          42 :       set_avma(av2);
    2599          42 :       if (e < beste) { beste = e; besti = i; }
    2600          35 :       else if (e == beste) beste = besti = 0; /* tie: forget */
    2601             :     }
    2602           7 :     if (!besti) { set_avma(av); return cgetg(1,t_VEC); }
    2603           7 :     return gerepilecopy(av, mkvec2(gel(M,besti), stoi(beste)));
    2604             :   }
    2605          84 :   v = solvestep((void*)&S, eval, ghalf, M, gen_2, 14, prec);
    2606          84 :   return gerepilecopy(av, checkconductor(v, bitprec/2, flag));
    2607             : }
    2608             : 
    2609             : /* assume chi primitive */
    2610             : static GEN
    2611         980 : znchargauss_i(GEN G, GEN chi, long bitprec)
    2612             : {
    2613         980 :   GEN z, q, F = znstar_get_N(G);
    2614             :   long prec;
    2615             : 
    2616         980 :   if (equali1(F)) return gen_1;
    2617         553 :   prec = nbits2prec(bitprec);
    2618         553 :   q = sqrtr_abs(itor(F, prec));
    2619         553 :   z = lfuntheta(mkvec2(G,chi), gen_1, 0, bitprec);
    2620         553 :   if (gexpo(z) < 10 - bitprec)
    2621             :   {
    2622          28 :     if (equaliu(F,300))
    2623             :     {
    2624          14 :       GEN z = rootsof1u_cx(25, prec);
    2625          14 :       GEN n = znconreyexp(G, chi);
    2626          14 :       if (equaliu(n, 131)) return gmul(q, gpowgs(z,14));
    2627           7 :       if (equaliu(n, 71)) return gmul(q, gpowgs(z,11));
    2628             :     }
    2629          14 :     if (equaliu(F,600))
    2630             :     {
    2631          14 :       GEN z = rootsof1u_cx(25, prec);
    2632          14 :       GEN n = znconreyexp(G, chi);
    2633          14 :       if (equaliu(n, 491)) return gmul(q, gpowgs(z,7));
    2634           7 :       if (equaliu(n, 11)) return gmul(q, gpowgs(z,18));
    2635             :     }
    2636           0 :     pari_err_BUG("znchargauss [ Theta(chi,1) = 0 ]");
    2637             :   }
    2638         525 :   z = gmul(gdiv(z, conj_i(z)), q);
    2639         525 :   if (zncharisodd(G,chi)) z = mulcxI(z);
    2640         525 :   return z;
    2641             : }
    2642             : static GEN
    2643         980 : Z_radical(GEN N, long *om)
    2644             : {
    2645         980 :   GEN P = gel(Z_factor(N), 1);
    2646         980 :   *om = lg(P)-1; return ZV_prod(P);
    2647             : }
    2648             : GEN
    2649        1197 : znchargauss(GEN G, GEN chi, GEN a, long bitprec)
    2650             : {
    2651             :   GEN v, T, N, F, b0, b1, b2, bF, a1, aF, A, r, GF, tau, B, faB, u, S;
    2652        1197 :   long omb0, prec = nbits2prec(bitprec);
    2653        1197 :   pari_sp av = avma;
    2654             : 
    2655        1197 :   if (typ(chi) != t_COL) chi = znconreylog(G,chi);
    2656        1197 :   T = znchartoprimitive(G, chi);
    2657        1197 :   GF  = gel(T,1);
    2658        1197 :   chi = gel(T,2); /* now primitive */
    2659        1197 :   N = znstar_get_N(G);
    2660        1197 :   F = znstar_get_N(GF);
    2661        1197 :   if (equalii(N,F)) b1 = bF = gen_1;
    2662             :   else
    2663             :   {
    2664         231 :     v = Z_ppio(diviiexact(N,F), F);
    2665         231 :     bF = gel(v,2); /* (N/F, F^oo) */
    2666         231 :     b1 = gel(v,3); /* cofactor */
    2667             :   }
    2668        1197 :   if (!a) a = a1 = aF = gen_1;
    2669             :   else
    2670             :   {
    2671        1148 :     if (typ(a) != t_INT) pari_err_TYPE("znchargauss",a);
    2672        1148 :     a = modii(a, N);
    2673        1148 :     v = Z_ppio(a, F);
    2674        1148 :     aF = gel(v,2);
    2675        1148 :     a1 = gel(v,3);
    2676             :   }
    2677        1197 :   if (!equalii(aF, bF)) { set_avma(av); return gen_0; }
    2678         980 :   b0 = Z_radical(b1, &omb0);
    2679         980 :   b2 = diviiexact(b1, b0);
    2680         980 :   A = dvmdii(a1, b2, &r);
    2681         980 :   if (r != gen_0) { set_avma(av); return gen_0; }
    2682         980 :   B = gcdii(A,b0); faB = Z_factor(B); /* squarefree */
    2683         980 :   S = eulerphi(mkvec2(B,faB));
    2684         980 :   if (odd(omb0 + lg(gel(faB,1))-1)) S = negi(S); /* moebius(b0/B) * phi(B) */
    2685         980 :   S = mulii(S, mulii(aF,b2));
    2686         980 :   tau = znchargauss_i(GF, chi, bitprec);
    2687         980 :   u = Fp_div(b0, A, F);
    2688         980 :   if (!equali1(u))
    2689             :   {
    2690         434 :     GEN ord = zncharorder(GF, chi), z = rootsof1_cx(ord, prec);
    2691         434 :     tau = gmul(tau, znchareval(GF, chi, u, mkvec2(z,ord)));
    2692             :   }
    2693         980 :   return gerepileupto(av, gmul(tau, S));
    2694             : }

Generated by: LCOV version 1.13