Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - lfun.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23712-7b25a218b) Lines: 1352 1408 96.0 %
Date: 2019-03-24 05:44:59 Functions: 139 140 99.3 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13