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.16.2 lcov report (development 29115-f22e516b23) Lines: 1591 1632 97.5 %
Date: 2024-02-22 08:05:55 Functions: 163 163 100.0 %
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; either version 2 of the License, or (at your option) any later
       8             : version. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : 
      15             : /********************************************************************/
      16             : /**                                                                **/
      17             : /**                       L-functions                              **/
      18             : /**                                                                **/
      19             : /********************************************************************/
      20             : 
      21             : #include "pari.h"
      22             : #include "paripriv.h"
      23             : 
      24             : #define DEBUGLEVEL DEBUGLEVEL_lfun
      25             : 
      26             : /*******************************************************************/
      27             : /*  Accessors                                                      */
      28             : /*******************************************************************/
      29             : 
      30             : static GEN
      31       12009 : mysercoeff(GEN x, long n)
      32             : {
      33       12009 :   long N = n - valser(x);
      34       12009 :   return (N < 0)? gen_0: gel(x, N+2);
      35             : }
      36             : 
      37             : long
      38       57287 : ldata_get_type(GEN ldata) { return mael3(ldata, 1, 1, 1); }
      39             : 
      40             : GEN
      41       46735 : ldata_get_an(GEN ldata) { return gel(ldata, 1); }
      42             : 
      43             : GEN
      44       42913 : ldata_get_dual(GEN ldata) { return gel(ldata, 2); }
      45             : 
      46             : long
      47        2789 : ldata_isreal(GEN ldata) { return isintzero(gel(ldata, 2)); }
      48             : 
      49             : GEN
      50      270967 : ldata_get_gammavec(GEN ldata) { return gel(ldata, 3); }
      51             : 
      52             : long
      53       23141 : ldata_get_degree(GEN ldata) { return lg(gel(ldata, 3))-1; }
      54             : 
      55             : GEN
      56      125253 : ldata_get_k(GEN ldata)
      57             : {
      58      125253 :   GEN w = gel(ldata,4);
      59      125253 :   if (typ(w) == t_VEC) w = gel(w,1);
      60      125253 :   return w;
      61             : }
      62             : 
      63             : /* a_n = O(n^{k1 + epsilon}) */
      64             : GEN
      65          98 : ldata_get_k1(GEN ldata)
      66             : {
      67          98 :   GEN w = gel(ldata,4);
      68          98 :   if (typ(w) == t_VEC) return gel(w,2);
      69             :   /* by default, assume that k1 = k-1 and even (k-1)/2 for entire functions */
      70          98 :   w = gaddgs(w,-1);
      71          98 :   return ldata_get_residue(ldata)? w: gmul2n(w, -1);
      72             : }
      73             : 
      74             : /* a_n = O(n^{k1 + epsilon}) */
      75             : static double
      76       79598 : ldata_get_k1_dbl(GEN ldata)
      77             : {
      78       79598 :   GEN w = gel(ldata,4);
      79             :   double k;
      80       79598 :   if (typ(w) == t_VEC) return gtodouble(gel(w,2));
      81             :   /* by default, assume that k1 = k-1 and even (k-1)/2 for entire functions */
      82       78177 :   k = gtodouble(w);
      83       78177 :   return ldata_get_residue(ldata)? k-1: (k-1)/2.;
      84             : }
      85             : 
      86             : GEN
      87      212315 : ldata_get_conductor(GEN ldata) { return gel(ldata, 5); }
      88             : 
      89             : GEN
      90       81285 : ldata_get_rootno(GEN ldata) { return gel(ldata, 6); }
      91             : 
      92             : GEN
      93      141706 : ldata_get_residue(GEN ldata) { return lg(ldata) == 7 ? NULL: gel(ldata, 7); }
      94             : 
      95             : long
      96      136520 : linit_get_type(GEN linit) { return mael(linit, 1, 1); }
      97             : 
      98             : GEN
      99      172827 : linit_get_ldata(GEN linit) { return gel(linit, 2); }
     100             : 
     101             : GEN
     102      224905 : linit_get_tech(GEN linit) { return gel(linit, 3); }
     103             : 
     104             : long
     105      244731 : is_linit(GEN data)
     106             : {
     107      174692 :   return lg(data) == 4 && typ(data) == t_VEC
     108      419423 :                        && typ(gel(data, 1)) == t_VECSMALL;
     109             : }
     110             : 
     111             : GEN
     112       27252 : lfun_get_step(GEN tech) { return gmael(tech, 2, 1);}
     113             : 
     114             : GEN
     115       27252 : lfun_get_pol(GEN tech) { return gmael(tech, 2, 2);}
     116             : 
     117             : GEN
     118        5140 : lfun_get_Residue(GEN tech) { return gmael(tech, 2, 3);}
     119             : 
     120             : GEN
     121       45332 : lfun_get_k2(GEN tech) { return gmael(tech, 3, 1);}
     122             : 
     123             : GEN
     124       19116 : lfun_get_w2(GEN tech) { return gmael(tech, 3, 2);}
     125             : 
     126             : GEN
     127       19116 : lfun_get_expot(GEN tech) { return gmael(tech, 3, 3);}
     128             : 
     129             : GEN
     130        5526 : lfun_get_factgammavec(GEN tech) { return gmael(tech, 3, 4); }
     131             : 
     132             : /* Handle complex Vga whose sum is real */
     133             : static GEN
     134       86766 : sumVga(GEN Vga) { return real_i(vecsum(Vga)); }
     135             : /* sum_i max (Im v[i],0) */
     136             : static double
     137       22412 : sumVgaimpos(GEN v)
     138             : {
     139       22412 :   double d = 0.;
     140       22412 :   long i, l = lg(v);
     141       60849 :   for (i = 1; i < l; i++)
     142             :   {
     143       38437 :     GEN c = imag_i(gel(v,i));
     144       38437 :     if (gsigne(c) > 0) d += gtodouble(c);
     145             :   }
     146       22412 :   return d;
     147             : }
     148             : 
     149             : static long
     150       30715 : vgaell(GEN Vga)
     151             : {
     152       30715 :   if (lg(Vga) == 3)
     153       19239 :   { GEN c = gsub(gel(Vga,1), gel(Vga,2)); return gequal1(c) || gequalm1(c); }
     154       11476 :   return 0;
     155             : }
     156             : int
     157       72960 : Vgaeasytheta(GEN Vga) { return lg(Vga)-1 == 1 || vgaell(Vga); }
     158             : /* return b(n) := a(n) * n^c, when Vgaeasytheta(Vga) is set */
     159             : static GEN
     160       11693 : antwist(GEN an, GEN Vga, long prec)
     161             : {
     162             :   long l, i;
     163       11693 :   GEN b, c = vecmin(Vga);
     164       11693 :   if (gequal0(c)) return an;
     165        2084 :   l = lg(an); b = cgetg(l, t_VEC);
     166        2084 :   if (gequal1(c))
     167             :   {
     168        1356 :     if (typ(an) == t_VECSMALL)
     169        8715 :       for (i = 1; i < l; i++) gel(b,i) = mulss(an[i], i);
     170             :     else
     171       24896 :       for (i = 1; i < l; i++) gel(b,i) = gmulgu(gel(an,i), i);
     172             :   }
     173             :   else
     174             :   {
     175         728 :     GEN v = vecpowug(l-1, c, prec);
     176         728 :     if (typ(an) == t_VECSMALL)
     177           0 :       for (i = 1; i < l; i++) gel(b,i) = gmulsg(an[i], gel(v,i));
     178             :     else
     179       20965 :       for (i = 1; i < l; i++) gel(b,i) = gmul(gel(an,i), gel(v,i));
     180             :   }
     181        2084 :   return b;
     182             : }
     183             : 
     184             : static GEN
     185        7070 : theta_dual(GEN theta, GEN bn)
     186             : {
     187        7070 :   if (typ(bn)==t_INT) return NULL;
     188             :   else
     189             :   {
     190          77 :     GEN thetad = shallowcopy(theta), ldata = linit_get_ldata(theta);
     191          77 :     GEN Vga = ldata_get_gammavec(ldata);
     192          77 :     GEN tech = shallowcopy(linit_get_tech(theta));
     193          77 :     GEN an = theta_get_an(tech);
     194          77 :     long prec = nbits2prec(theta_get_bitprec(tech));
     195          77 :     GEN vb = ldata_vecan(bn, lg(an)-1, prec);
     196          77 :     if (!theta_get_m(tech) && Vgaeasytheta(Vga)) vb = antwist(vb, Vga, prec);
     197          77 :     gel(tech,1) = vb;
     198          77 :     gel(thetad,3) = tech; return thetad;
     199             :   }
     200             : }
     201             : 
     202             : static GEN
     203       79009 : domain_get_dom(GEN domain)  { return gel(domain,1); }
     204             : static long
     205       25239 : domain_get_der(GEN domain)  { return mael2(domain, 2, 1); }
     206             : static long
     207       31882 : domain_get_bitprec(GEN domain)  { return mael2(domain, 2, 2); }
     208             : GEN
     209       79569 : lfun_get_domain(GEN tech) { return gel(tech,1); }
     210             : long
     211          91 : lfun_get_bitprec(GEN tech){ return domain_get_bitprec(lfun_get_domain(tech)); }
     212             : GEN
     213       54162 : lfun_get_dom(GEN tech) { return domain_get_dom(lfun_get_domain(tech)); }
     214             : 
     215             : GEN
     216        2540 : lfunprod_get_fact(GEN tech)  { return gel(tech, 2); }
     217             : 
     218             : GEN
     219       47533 : theta_get_an(GEN tdata)      { return gel(tdata, 1);}
     220             : GEN
     221        7633 : theta_get_K(GEN tdata)       { return gel(tdata, 2);}
     222             : GEN
     223        5991 : theta_get_R(GEN tdata)       { return gel(tdata, 3);}
     224             : long
     225       60909 : theta_get_bitprec(GEN tdata) { return itos(gel(tdata, 4));}
     226             : long
     227       92147 : theta_get_m(GEN tdata)       { return itos(gel(tdata, 5));}
     228             : GEN
     229       48912 : theta_get_tdom(GEN tdata)    { return gel(tdata, 6);}
     230             : GEN
     231       53395 : theta_get_isqrtN(GEN tdata)  { return gel(tdata, 7);}
     232             : 
     233             : /*******************************************************************/
     234             : /*  Helper functions related to Gamma products                     */
     235             : /*******************************************************************/
     236             : /* x != 0 */
     237             : static int
     238        6453 : serisscalar(GEN x)
     239             : {
     240             :   long i;
     241        6453 :   if (valser(x)) return 0;
     242        8154 :   for (i = lg(x)-1; i > 3; i--) if (!gequal0(gel(x,i))) return 0;
     243        6215 :   return 1;
     244             : }
     245             : 
     246             : /* return -itos(s) >= 0 if scalar s is (approximately) equal to a nonpositive
     247             :  * integer, and -1 otherwise */
     248             : static long
     249       16127 : isnegint(GEN s)
     250             : {
     251       16127 :   GEN r = ground(real_i(s));
     252       16127 :   if (signe(r) <= 0 && gequal(s, r)) return -itos(r);
     253       16001 :   return -1;
     254             : }
     255             : /* if s = a + O(x^n), a <= 0 integer, replace by a + b*x^n + O(x^(n+1)) */
     256             : static GEN
     257        6474 : serextendifnegint(GEN s, GEN b, long *ext)
     258             : {
     259        6474 :   if (!signe(s) || (serisscalar(s) && isnegint(gel(s,2)) >= 0))
     260             :   {
     261         112 :     long l = lg(s);
     262         112 :     GEN t = cgetg(l+1, t_SER);
     263         301 :     gel(t, l) = b; while (--l > 1) gel(t,l) = gel(s,l);
     264         112 :     if (gequal0(gel(t,2))) gel(t,2) = gen_0;
     265         112 :     t[1] = s[1]; s = normalizeser(t); *ext = 1;
     266             :   }
     267        6474 :   return s;
     268             : }
     269             : 
     270             : /* r/x + O(1), r != 0 */
     271             : static GEN
     272        4917 : serpole(GEN r)
     273             : {
     274        4917 :   GEN s = cgetg(3, t_SER);
     275        4917 :   s[1] = evalsigne(1)|evalvalser(-1)|evalvarn(0);
     276        4917 :   gel(s,2) = r; return s;
     277             : }
     278             : /* a0 +  a1 x + O(x^e), e >= 0 */
     279             : static GEN
     280        7409 : deg1ser_shallow(GEN a1, GEN a0, long v, long e)
     281        7409 : { return RgX_to_ser(deg1pol_shallow(a1, a0, v), e+2); }
     282             : 
     283             : /* pi^(-s/2) Gamma(s/2) */
     284             : static GEN
     285        9988 : gamma_R(GEN s, long *ext, long prec)
     286             : {
     287        9988 :   GEN s2 = gmul2n(s, -1);
     288             :   long ms;
     289             : 
     290        9988 :   if (typ(s) == t_SER)
     291        4710 :     s2 = serextendifnegint(s2, ghalf, ext);
     292        5278 :   else if ((ms = isnegint(s2)) >= 0)
     293             :   {
     294          35 :     GEN r = gmul(powPis(stoi(ms),prec), gdivsg(odd(ms)? -2: 2, mpfact(ms)));
     295          35 :     return serpole(r);
     296             :   }
     297        9953 :   return gdiv(ggamma(s2,prec), powPis(s2,prec));
     298             : }
     299             : /* gamma_R(s)gamma_R(s+1) = 2 (2pi)^(-s) Gamma(s) */
     300             : static GEN
     301        6398 : gamma_C(GEN s, long *ext, long prec)
     302             : {
     303             :   long ms;
     304        6398 :   if (typ(s) == t_SER)
     305        1764 :     s = serextendifnegint(s, gen_1, ext);
     306        4634 :   else if ((ms = isnegint(s)) >= 0)
     307             :   {
     308           0 :     GEN r = gmul(pow2Pis(stoi(ms),prec), gdivsg(odd(ms)? -2: 2, mpfact(ms)));
     309           0 :     return serpole(r);
     310             :   }
     311        6398 :   return gmul2n(gdiv(ggamma(s,prec), pow2Pis(s,prec)), 1);
     312             : }
     313             : 
     314             : static GEN
     315        1708 : gammafrac(GEN r, long d)
     316             : {
     317        1708 :   long i, l = labs(d) + 1, j = (d > 0)? 0: 2*d;
     318        1708 :   GEN T, v = cgetg(l, t_COL);
     319        4466 :   for (i = 1; i < l; i++, j += 2)
     320        2758 :     gel(v,i) = deg1pol_shallow(gen_1, gaddgs(r, j), 0);
     321        1708 :   T = RgV_prod(v); return d > 0? T: mkrfrac(gen_1, T);
     322             : }
     323             : 
     324             : /*
     325             : GR(s)=Pi^-(s/2)*gamma(s/2);
     326             : GC(s)=2*(2*Pi)^-s*gamma(s)
     327             : gdirect(F,s)=prod(i=1,#F,GR(s+F[i]))
     328             : gfact(F,s)=
     329             : { my([R,A,B]=gammafactor(F), [a,e]=A, [b,f]=B, p=poldegree(R));
     330             :   subst(R,x,s) * (2*Pi)^-p * prod(i=1,#a,GR(s+a[i])^e[i])
     331             :                            * prod(i=1,#b,GC(s+b[i])^f[i]); }
     332             : */
     333             : static GEN
     334       16817 : gammafactor(GEN Vga)
     335             : {
     336       16817 :   long i, r, c, l = lg(Vga);
     337       16817 :   GEN v, P, a, b, e, f, E, F = cgetg(l, t_VEC), R = gen_1;
     338       46241 :   for (i = 1; i < l; ++i)
     339             :   {
     340       29424 :     GEN a = gel(Vga,i), r = gmul2n(real_i(a), -1);
     341       29424 :     long q = itos(gfloor(r)); /* [Re a/2] */
     342       29424 :     r = gmul2n(gsubgs(r, q), 1);
     343       29424 :     gel(F,i) = gequal0(imag_i(a)) ? r : mkcomplex(r, imag_i(a)); /* 2{Re a/2} + I*(Im a) */
     344       29424 :     if (q) R = gmul(R, gammafrac(gel(F,i), q));
     345             :   }
     346       16817 :   F = vec_reduce(F, &E); l = lg(E);
     347       16817 :   v = cgetg(l, t_VEC);
     348       40998 :   for (i = 1; i < l; i++)
     349       24181 :       gel(v,i) = mkvec2(gsub(gel(F,i),gfloor(real_i(gel(F,i)))), stoi(E[i]));
     350       16817 :   gen_sort_inplace(v, (void*)cmp_universal, cmp_nodata, &P);
     351       16817 :   a = cgetg(l, t_VEC); e = cgetg(l, t_VECSMALL);
     352       16817 :   b = cgetg(l, t_VEC); f = cgetg(l, t_VECSMALL);
     353       34579 :   for (i = r = c = 1; i < l;)
     354       17762 :     if (i==l-1 || cmp_universal(gel(v,i), gel(v,i+1)))
     355       11343 :     { gel(a, r) = gel(F, P[i]); e[r++] = E[P[i]]; i++; }
     356             :     else
     357        6419 :     { gel(b, c) = gel(F, P[i]); f[c++] = E[P[i]]; i+=2; }
     358       16817 :   setlg(a, r); setlg(e, r);
     359       16817 :   setlg(b, c); setlg(f, c); return mkvec3(R, mkvec2(a,e), mkvec2(b,f));
     360             : }
     361             : 
     362             : static GEN
     363        3640 : polgammaeval(GEN F, GEN s)
     364             : {
     365        3640 :   GEN r = poleval(F, s);
     366        3640 :   if (typ(s) != t_SER && gequal0(r))
     367             :   { /* here typ(F) = t_POL */
     368             :     long e;
     369           7 :     for (e = 1;; e++)
     370             :     {
     371           7 :       F = RgX_deriv(F); r = poleval(F,s);
     372           7 :       if (!gequal0(r)) break;
     373             :     }
     374           7 :     if (e > 1) r = gdiv(r, mpfact(e));
     375           7 :     r = serpole(r); setvalser(r, e);
     376             :   }
     377        3640 :   return r;
     378             : }
     379             : static long
     380        1799 : rfrac_degree(GEN R)
     381             : {
     382        1799 :   GEN a = gel(R,1), b = gel(R,2);
     383        1799 :   return ((typ(a) == t_POL)? degpol(a): 0) - degpol(b);
     384             : }
     385             : static GEN
     386       15399 : fracgammaeval(GEN F, GEN s, long prec)
     387             : {
     388       15399 :   GEN R = gel(F,1);
     389             :   long d;
     390       15399 :   switch(typ(R))
     391             :   {
     392          42 :     case t_POL:
     393          42 :       d = degpol(R);
     394          42 :       R = polgammaeval(R, s); break;
     395        1799 :     case t_RFRAC:
     396        1799 :       d = rfrac_degree(R);
     397        1799 :       R = gdiv(polgammaeval(gel(R,1), s), polgammaeval(gel(R,2), s)); break;
     398       13558 :     default: return R;
     399             :   }
     400        1841 :   return gmul(R, powrs(Pi2n(1,prec), -d));
     401             : }
     402             : 
     403             : static GEN
     404       15399 : gammafactproduct(GEN F, GEN s, long *ext, long prec)
     405             : {
     406       15399 :   pari_sp av = avma;
     407       15399 :   GEN R = gel(F,2), Rw = gel(R,1), Re = gel(R,2);
     408       15399 :   GEN C = gel(F,3), Cw = gel(C,1), Ce = gel(C,2), z = fracgammaeval(F,s,prec);
     409       15399 :   long i, lR = lg(Rw), lC = lg(Cw);
     410       15399 :   *ext = 0;
     411       25387 :   for (i = 1; i < lR; i++)
     412        9988 :     z = gmul(z, gpowgs(gamma_R(gadd(s,gel(Rw, i)), ext, prec), Re[i]));
     413       21797 :   for (i = 1; i < lC; i++)
     414        6398 :     z = gmul(z, gpowgs(gamma_C(gadd(s,gel(Cw, i)), ext, prec), Ce[i]));
     415       15399 :   return gerepileupto(av, z);
     416             : }
     417             : 
     418             : static int
     419        4942 : gammaordinary(GEN Vga, GEN s)
     420             : {
     421        4942 :   long i, d = lg(Vga)-1;
     422       13391 :   for (i = 1; i <= d; i++)
     423             :   {
     424        8540 :     GEN z = gadd(s, gel(Vga,i));
     425             :     long e;
     426        8540 :     if (gexpo(imag_i(z)) < -10)
     427             :     {
     428        8540 :       z = real_i(z);
     429        8540 :       if (gsigne(z) <= 0) { (void)grndtoi(z, &e); if (e < -10) return 0; }
     430             :     }
     431             :   }
     432        4851 :   return 1;
     433             : }
     434             : 
     435             : /* Exponent A of t in asymptotic expansion; K(t) ~ C t^A exp(-pi d t^(2/d)).
     436             :  * suma = vecsum(Vga)*/
     437             : static double
     438       79591 : gammavec_expo(long d, double suma) { return (1 - d + suma) / d; }
     439             : 
     440             : /*******************************************************************/
     441             : /*       First part: computations only involving Theta(t)          */
     442             : /*******************************************************************/
     443             : 
     444             : static void
     445      120227 : get_cone(GEN t, double *r, double *a)
     446             : {
     447      120227 :   const long prec = LOWDEFAULTPREC;
     448      120227 :   if (typ(t) == t_COMPLEX)
     449             :   {
     450       21518 :     t  = gprec_w(t, prec);
     451       21518 :     *r = gtodouble(gabs(t, prec));
     452       21518 :     *a = fabs(gtodouble(garg(t, prec)));
     453             :   }
     454             :   else
     455             :   {
     456       98709 :     *r = fabs(gtodouble(t));
     457       98709 :     *a = 0.;
     458             :   }
     459      120227 :   if (!*r && !*a) pari_err_DOMAIN("lfunthetainit","t","=",gen_0,t);
     460      120220 : }
     461             : /* slightly larger cone than necessary, to avoid round-off problems */
     462             : static void
     463       71315 : get_cone_fuzz(GEN t, double *r, double *a)
     464       71315 : { get_cone(t, r, a); *r -= 1e-10; if (*a) *a += 1e-10; }
     465             : 
     466             : /* Initialization m-th Theta derivative. tdom is either
     467             :  * - [rho,alpha]: assume |t| >= rho and |arg(t)| <= alpha
     468             :  * - a positive real scalar: assume t real, t >= tdom;
     469             :  * - a complex number t: compute at t;
     470             :  * N is the conductor (either the true one from ldata or a guess from
     471             :  * lfunconductor) */
     472             : long
     473       57186 : lfunthetacost(GEN ldata, GEN tdom, long m, long bitprec)
     474             : {
     475       57186 :   pari_sp av = avma;
     476       57186 :   GEN Vga = ldata_get_gammavec(ldata);
     477       57186 :   long d = lg(Vga)-1;
     478       57186 :   double k1 = maxdd(ldata_get_k1_dbl(ldata), 0.);
     479       57186 :   double c = d/2., a, A, B, logC, al, rho, T;
     480       57186 :   double N = gtodouble(ldata_get_conductor(ldata));
     481             : 
     482       57186 :   if (!N) pari_err_TYPE("lfunthetaneed [missing conductor]", ldata);
     483       57186 :   if (typ(tdom) == t_VEC && lg(tdom) == 3)
     484             :   {
     485           7 :     rho= gtodouble(gel(tdom,1));
     486           7 :     al = gtodouble(gel(tdom,2));
     487             :   }
     488             :   else
     489       57179 :     get_cone_fuzz(tdom, &rho, &al);
     490       57179 :   A = gammavec_expo(d, gtodouble(sumVga(Vga))); set_avma(av);
     491       57179 :   a = (A+k1+1) + (m-1)/c;
     492       57179 :   if (fabs(a) < 1e-10) a = 0.;
     493       57179 :   logC = c*M_LN2 - log(c)/2;
     494             :   /* +1: fudge factor */
     495       57179 :   B = M_LN2*bitprec+logC+m*log(2*M_PI) + 1 + (k1+1)*log(N)/2 - (k1+m+1)*log(rho);
     496       57179 :   if (al)
     497             :   { /* t = rho e^(i*al), T^(1/c) = Re(t^(1/c)) > 0, T = rho cos^c(al/c) */
     498       10766 :     double z = cos(al/c);
     499       10766 :     if (z <= 0)
     500           7 :       pari_err_DOMAIN("lfunthetaneed", "arg t", ">", dbltor(c*M_PI/2), tdom);
     501       10759 :     T = (d == 2 && typ(tdom) != t_VEC)? gtodouble(real_i(tdom)): rho*pow(z,c);
     502       10759 :     B -= log(z) * (c * (k1+A+1) + m);
     503             :   }
     504             :   else
     505       46413 :     T = rho;
     506       57172 :   if (B <= 0) return 0;
     507       57172 :   A = floor(0.9 + dblcoro526(a,c,B) / T * sqrt(N));
     508       57172 :   if (dblexpo(A) >= BITS_IN_LONG-1) pari_err_OVERFLOW("lfunthetacost");
     509       57165 :   return (long)A;
     510             : }
     511             : long
     512          21 : lfunthetacost0(GEN L, GEN tdom, long m, long bitprec)
     513             : {
     514             :   long n;
     515          21 :   if (is_linit(L) && linit_get_type(L)==t_LDESC_THETA)
     516           7 :   {
     517           7 :     GEN tech = linit_get_tech(L);
     518           7 :     n = lg(theta_get_an(tech))-1;
     519             :   }
     520             :   else
     521             :   {
     522          14 :     pari_sp av = avma;
     523          14 :     GEN ldata = lfunmisc_to_ldata_shallow(L);
     524          14 :     n = lfunthetacost(ldata, tdom? tdom: gen_1, m, bitprec);
     525           7 :     set_avma(av);
     526             :   }
     527          14 :   return n;
     528             : }
     529             : 
     530             : static long
     531        6250 : fracgammadegree(GEN FVga)
     532        6250 : { GEN F = gel(FVga,1); return (typ(F)==t_RFRAC)? degpol(gel(F,2)): 0; }
     533             : 
     534             : /* Poles of a L-function can be represented in the following ways:
     535             :  * 1) Nothing (ldata has only 6 components, ldata_get_residue = NULL).
     536             :  * 2) a complex number (single pole at s = k with given residue, unknown if 0).
     537             :  * 3) A vector (possibly empty) of 2-component vectors [a, ra], where a is the
     538             :  * pole, ra a t_SER: its Taylor expansion at a. A t_VEC encodes the polar
     539             :  * part of L, a t_COL, the polar part of Lambda */
     540             : 
     541             : /* 'a' a complex number (pole), 'r' the polar part of L at 'a';
     542             :  * return 'R' the polar part of Lambda at 'a' */
     543             : static GEN
     544        4609 : rtoR(GEN a, GEN r, GEN FVga, GEN N, long prec)
     545             : {
     546        4609 :   long v = lg(r)-2, d = fracgammadegree(FVga), ext;
     547        4609 :   GEN Na, as = deg1ser_shallow(gen_1, a, varn(r), v);
     548        4609 :   Na = gpow(N, gdivgu(as, 2), prec);
     549             :   /* make up for a possible loss of accuracy */
     550        4609 :   if (d) as = deg1ser_shallow(gen_1, a, varn(r), v + d);
     551        4609 :   return gmul(gmul(r, Na), gammafactproduct(FVga, as, &ext, prec));
     552             : }
     553             : 
     554             : /* assume r in normalized form: t_VEC of pairs [be,re] */
     555             : GEN
     556        4378 : lfunrtopoles(GEN r)
     557             : {
     558        4378 :   long j, l = lg(r);
     559        4378 :   GEN v = cgetg(l, t_VEC);
     560        8987 :   for (j = 1; j < l; j++)
     561             :   {
     562        4609 :     GEN rj = gel(r,j), a = gel(rj,1);
     563        4609 :     gel(v,j) = a;
     564             :   }
     565        4378 :   gen_sort_inplace(v, (void*)&cmp_universal, cmp_nodata, NULL);
     566        4378 :   return v;
     567             : }
     568             : 
     569             : /* r / x + O(1) */
     570             : static GEN
     571        5022 : simple_pole(GEN r)
     572        5022 : { return isintzero(r)? gen_0: serpole(r); }
     573             : static GEN
     574        5876 : normalize_simple_pole(GEN r, GEN k)
     575             : {
     576        5876 :   long tx = typ(r);
     577        5876 :   if (is_vec_t(tx)) return r;
     578        5022 :   if (!is_scalar_t(tx)) pari_err_TYPE("lfunrootres [poles]", r);
     579        5022 :   return mkvec(mkvec2(k, simple_pole(r)));
     580             : }
     581             : /* normalize the description of a polar part */
     582             : static GEN
     583        5253 : normalizepoles(GEN r, GEN k)
     584             : {
     585             :   long iv, j, l;
     586             :   GEN v;
     587        5253 :   if (!is_vec_t(typ(r))) return normalize_simple_pole(r, k);
     588        2233 :   v = cgetg_copy(r, &l);
     589        5593 :   for (j = iv = 1; j < l; j++)
     590             :   {
     591        3360 :     GEN rj = gel(r,j), a = gel(rj,1), ra = gel(rj,2);
     592        3360 :     if (!is_scalar_t(typ(a)) || typ(ra) != t_SER)
     593           0 :       pari_err_TYPE("lfunrootres [poles]",r);
     594        3360 :     if (valser(ra) >= 0) continue;
     595        3346 :     gel(v,iv++) = rj;
     596             :   }
     597        2233 :   setlg(v, iv); return v;
     598             : }
     599             : static int
     600        8522 : residues_known(GEN r)
     601             : {
     602        8522 :   long i, l = lg(r);
     603        8522 :   if (isintzero(r)) return 0;
     604        8193 :   if (!is_vec_t(typ(r))) return 1;
     605       10934 :   for (i = 1; i < l; i++)
     606             :   {
     607        6671 :     GEN ri = gel(r,i);
     608        6671 :     if (!is_vec_t(typ(ri)) || lg(ri)!=3)
     609           0 :       pari_err_TYPE("lfunrootres [poles]",r);
     610        6671 :     if (isintzero(gel(ri, 2))) return 0;
     611             :   }
     612        4263 :   return 1;
     613             : }
     614             : 
     615             : /* Compute R's from r's (r = Taylor devts of L(s), R of Lambda(s)).
     616             :  * 'r/eno' passed to override the one from ldata  */
     617             : static GEN
     618       16663 : lfunrtoR_i(GEN ldata, GEN r, GEN eno, long prec)
     619             : {
     620       16663 :   GEN Vga = ldata_get_gammavec(ldata), N = ldata_get_conductor(ldata);
     621             :   GEN R, vr, FVga;
     622       16663 :   pari_sp av = avma;
     623             :   long lr, j, jR;
     624       16663 :   GEN k = ldata_get_k(ldata);
     625             : 
     626       16663 :   if (!r || isintzero(eno) || !residues_known(r))
     627       11410 :     return gen_0;
     628        5253 :   r = normalizepoles(r, k);
     629        5253 :   if (typ(r) == t_COL) return gerepilecopy(av, r);
     630        4378 :   if (typ(ldata_get_dual(ldata)) != t_INT)
     631           0 :     pari_err(e_MISC,"please give the Taylor development of Lambda");
     632        4378 :   vr = lfunrtopoles(r); lr = lg(vr);
     633        4378 :   FVga = gammafactor(Vga);
     634        4378 :   R = cgetg(2*lr, t_COL);
     635        8987 :   for (j = jR = 1; j < lr; j++)
     636             :   {
     637        4609 :     GEN rj = gel(r,j), a = gel(rj,1), ra = gel(rj,2);
     638        4609 :     GEN Ra = rtoR(a, ra, FVga, N, prec);
     639        4609 :     GEN b = gsub(k, conj_i(a));
     640        4609 :     if (lg(Ra)-2 < -valser(Ra))
     641           0 :       pari_err(e_MISC,
     642             :         "please give more terms in L function's Taylor development at %Ps", a);
     643        4609 :     gel(R,jR++) = mkvec2(a, Ra);
     644        4609 :     if (!tablesearch(vr, b, (int (*)(GEN,GEN))&cmp_universal))
     645             :     {
     646        4420 :       GEN mX = gneg(pol_x(varn(Ra)));
     647        4420 :       GEN Rb = gmul(eno, gsubst(conj_i(Ra), varn(Ra), mX));
     648        4420 :       gel(R,jR++) = mkvec2(b, Rb);
     649             :     }
     650             :   }
     651        4378 :   setlg(R, jR); return gerepilecopy(av, R);
     652             : }
     653             : static GEN
     654       16187 : lfunrtoR_eno(GEN ldata, GEN eno, long prec)
     655       16187 : { return lfunrtoR_i(ldata, ldata_get_residue(ldata), eno, prec); }
     656             : static GEN
     657       14143 : lfunrtoR(GEN ldata, long prec)
     658       14143 : { return lfunrtoR_eno(ldata, ldata_get_rootno(ldata), prec); }
     659             : 
     660             : static long
     661       14143 : prec_fix(long prec)
     662             : {
     663             : #ifndef LONG_IS_64BIT
     664             :   /* make sure that default accuracy is the same on 32/64bit */
     665        2023 :   if (odd(prec)) prec += EXTRAPREC64;
     666             : #endif
     667       14143 :   return prec;
     668             : }
     669             : 
     670             : /* thetainit using {an: n <= L}; if (m = 0 && easytheta), an2 is an * n^al */
     671             : static GEN
     672       14143 : lfunthetainit0(GEN ldata, GEN tdom, GEN an2, long m,
     673             :     long bitprec, long extrabit)
     674             : {
     675       14143 :   long prec = nbits2prec(bitprec);
     676       14143 :   GEN tech, N = ldata_get_conductor(ldata);
     677       14143 :   GEN K = gammamellininvinit(ldata, m, bitprec + extrabit);
     678       14143 :   GEN R = lfunrtoR(ldata, prec);
     679       14143 :   if (!tdom) tdom = gen_1;
     680       14143 :   if (typ(tdom) != t_VEC)
     681             :   {
     682             :     double r, a;
     683       14136 :     get_cone_fuzz(tdom, &r, &a);
     684       14136 :     tdom = mkvec2(dbltor(r), a? dbltor(a): gen_0);
     685             :   }
     686       14143 :   prec += maxss(EXTRAPREC64, nbits2extraprec(extrabit));
     687       14143 :   tech = mkvecn(7, an2,K,R, stoi(bitprec), stoi(m), tdom,
     688             :                    gsqrt(ginv(N), prec_fix(prec)));
     689       14143 :   return mkvec3(mkvecsmall(t_LDESC_THETA), ldata, tech);
     690             : }
     691             : 
     692             : /* tdom: 1) positive real number r, t real, t >= r; or
     693             :  *       2) [r,a], describing the cone |t| >= r, |arg(t)| <= a */
     694             : static GEN
     695        8001 : lfunthetainit_i(GEN data, GEN tdom, long m, long bit)
     696             : {
     697        8001 :   GEN ldata = lfunmisc_to_ldata_shallow(data);
     698        8001 :   long b = 32, L = lfunthetacost(ldata, tdom, m, bit), prec = nbits2prec(bit);
     699        7987 :   GEN ldatan = ldata_newprec(ldata, prec);
     700        7987 :   GEN an = ldata_vecan(ldata_get_an(ldatan), L, prec);
     701        7987 :   GEN Vga = ldata_get_gammavec(ldatan);
     702        7987 :   if (m == 0 && Vgaeasytheta(Vga)) an = antwist(an, Vga, prec);
     703        7987 :   if (typ(an) != t_VECSMALL) b = maxss(b, gexpo(an));
     704        7987 :   return lfunthetainit0(ldatan, tdom, an, m, bit, b);
     705             : }
     706             : 
     707             : GEN
     708         336 : lfunthetainit(GEN ldata, GEN tdom, long m, long bitprec)
     709             : {
     710         336 :   pari_sp av = avma;
     711         336 :   GEN S = lfunthetainit_i(ldata, tdom? tdom: gen_1, m, bitprec);
     712         336 :   return gerepilecopy(av, S);
     713             : }
     714             : 
     715             : GEN
     716        2162 : lfunan(GEN ldata, long L, long prec)
     717             : {
     718        2162 :   pari_sp av = avma;
     719             :   GEN an ;
     720        2162 :   ldata = ldata_newprec(lfunmisc_to_ldata_shallow(ldata), prec);
     721        2162 :   an = gerepilecopy(av, ldata_vecan(ldata_get_an(ldata), L, prec));
     722        2106 :   if (typ(an) != t_VEC) an = vecsmall_to_vec_inplace(an);
     723        2106 :   return an;
     724             : }
     725             : 
     726             : static GEN
     727       15897 : mulrealvec(GEN x, GEN y)
     728             : {
     729       15897 :   if (is_vec_t(typ(x)) && is_vec_t(typ(y)))
     730          84 :     pari_APPLY_same(mulreal(gel(x,i),gel(y,i)))
     731             :   else
     732       15869 :     return mulreal(x,y);
     733             : }
     734             : static GEN
     735       26853 : gmulvec(GEN x, GEN y)
     736             : {
     737       26853 :   if (is_vec_t(typ(x)) && is_vec_t(typ(y)))
     738        1974 :     pari_APPLY_same(gmul(gel(x,i),gel(y,i)))
     739             :   else
     740       26328 :     return gmul(x,y);
     741             : }
     742             : static GEN
     743        7049 : gdivvec(GEN x, GEN y)
     744             : {
     745        7049 :   if (is_vec_t(typ(x)) && is_vec_t(typ(y)))
     746        1519 :     pari_APPLY_same(gdiv(gel(x,i),gel(y,i)))
     747             :   else
     748        6615 :     return gdiv(x,y);
     749             : }
     750             : 
     751             : static GEN
     752        3556 : gsubvec(GEN x, GEN y)
     753             : {
     754        3556 :   if (is_vec_t(typ(x)) && !is_vec_t(typ(y)))
     755           0 :     pari_APPLY_same(gsub(gel(x,i),y))
     756             :   else
     757        3556 :     return gsub(x,y);
     758             : }
     759             : 
     760             : /* return [1^(2/d), 2^(2/d),...,lim^(2/d)] */
     761             : static GEN
     762        7633 : mkvroots(long d, long lim, long prec)
     763             : {
     764        7633 :   if (d <= 4)
     765             :   {
     766        7283 :     GEN v = cgetg(lim+1,t_VEC);
     767             :     long n;
     768        7283 :     switch(d)
     769             :     {
     770        2572 :       case 1:
     771       46913 :         for (n=1; n <= lim; n++) gel(v,n) = sqru(n);
     772        2572 :         return v;
     773        1267 :       case 2:
     774      227213 :         for (n=1; n <= lim; n++) gel(v,n) = utoipos(n);
     775        1267 :         return v;
     776        1983 :       case 4:
     777     6079017 :         for (n=1; n <= lim; n++) gel(v,n) = sqrtr(utor(n, prec));
     778        1983 :         return v;
     779             :     }
     780             :   }
     781        1811 :   return vecpowug(lim, gdivgu(gen_2,d), prec);
     782             : }
     783             : 
     784             : GEN
     785       54645 : lfunthetacheckinit(GEN data, GEN t, long m, long bitprec)
     786             : {
     787       54645 :   if (is_linit(data) && linit_get_type(data)==t_LDESC_THETA)
     788             :   {
     789       48912 :     GEN tdom, thetainit = linit_get_tech(data);
     790       48912 :     long bitprecnew = theta_get_bitprec(thetainit);
     791       48912 :     long m0 = theta_get_m(thetainit);
     792             :     double r, al, rt, alt;
     793       48912 :     if (m0 != m)
     794           0 :       pari_err_DOMAIN("lfuntheta","derivative order","!=", stoi(m),stoi(m0));
     795       48912 :     if (bitprec > bitprecnew) goto INIT;
     796       48912 :     get_cone(t, &rt, &alt);
     797       48912 :     tdom = theta_get_tdom(thetainit);
     798       48912 :     r = gtodouble(gel(tdom,1));
     799       48912 :     al= gtodouble(gel(tdom,2)); if (rt >= r && alt <= al) return data;
     800             :   }
     801        5733 : INIT:
     802        7574 :   return lfunthetainit_i(data, t, m, bitprec);
     803             : }
     804             : 
     805             : static GEN
     806    14711879 : get_an(GEN an, long n)
     807             : {
     808    14711879 :   if (typ(an) == t_VECSMALL) { long a = an[n]; if (a) return stoi(a); }
     809    14711879 :   else { GEN a = gel(an,n); if (a && !gequal0(a)) return a; }
     810    12686150 :   return NULL;
     811             : }
     812             : /* x * an[n] */
     813             : static GEN
     814    12634316 : mul_an(GEN an, long n, GEN x)
     815             : {
     816    12634316 :   if (typ(an) == t_VECSMALL) { long a = an[n]; if (a) return gmulsg(a,x); }
     817     7210792 :   else { GEN a = gel(an,n); if (a && !gequal0(a)) return gmul(a,x); }
     818     2347069 :   return NULL;
     819             : }
     820             : /* 2*t^a * x **/
     821             : static GEN
     822      191497 : mulT(GEN t, GEN a, GEN x, long prec)
     823             : {
     824      191497 :   if (gequal0(a)) return gmul2n(x,1);
     825       17981 :   return gmul(x, gmul2n(gequal1(a)? t: gpow(t,a,prec), 1));
     826             : }
     827             : 
     828             : static GEN
     829    28310745 : vecan_cmul(void *E, GEN P, long a, GEN x)
     830             : {
     831             :   (void)E;
     832    28310745 :   if (typ(P) == t_VECSMALL)
     833    23776234 :     return (a==0 || !P[a])? NULL: gmulsg(P[a], x);
     834             :   else
     835     4534511 :     return (a==0 || !gel(P,a))? NULL: gmul(gel(P,a), x);
     836             : }
     837             : /* d=2, 2 sum_{n <= N} a(n) (n t)^al q^n, q = exp(-2pi t),
     838             :  * an2[n] = a(n) * n^al */
     839             : static GEN
     840      154090 : theta2_i(GEN an2, long N, GEN t, GEN al, long prec)
     841             : {
     842      154090 :   GEN S, q, pi2 = Pi2n(1,prec);
     843      154098 :   const struct bb_algebra *alg = get_Rg_algebra();
     844      154096 :   setsigne(pi2,-1); q = gexp(gmul(pi2, t), prec);
     845             :   /* Brent-Kung in case the a_n are small integers */
     846      154100 :   S = gen_bkeval(an2, N, q, 1, NULL, alg, vecan_cmul);
     847      154103 :   return mulT(t, al, S, prec);
     848             : }
     849             : static GEN
     850      148325 : theta2(GEN an2, long N, GEN t, GEN al, long prec)
     851             : {
     852      148325 :   pari_sp av = avma;
     853      148325 :   return gerepileupto(av, theta2_i(an2, N, t, al, prec));
     854             : }
     855             : 
     856             : /* d=1, 2 sum_{n <= N} a_n (n t)^al q^(n^2), q = exp(-pi t^2),
     857             :  * an2[n] is a_n n^al */
     858             : static GEN
     859       37394 : theta1(GEN an2, long N, GEN t, GEN al, long prec)
     860             : {
     861       37394 :   GEN q = gexp(gmul(negr(mppi(prec)), gsqr(t)), prec);
     862       37394 :   GEN vexp = gsqrpowers(q, N), S = gen_0;
     863       37394 :   pari_sp av = avma;
     864             :   long n;
     865     6754731 :   for (n = 1; n <= N; n++)
     866             :   {
     867     6717337 :     GEN c = mul_an(an2, n, gel(vexp,n));
     868     6717337 :     if (c)
     869             :     {
     870     5648635 :       S = gadd(S, c);
     871     5648635 :       if (gc_needed(av, 3)) S = gerepileupto(av, S);
     872             :     }
     873             :   }
     874       37394 :   return mulT(t, al, S, prec);
     875             : }
     876             : 
     877             : /* If m > 0, compute m-th derivative of theta(t) = theta0(t/sqrt(N))
     878             :  * with absolute error 2^-bitprec; theta(t)=\sum_{n\ge1}a(n)K(nt/N^(1/2)) */
     879             : GEN
     880       47246 : lfuntheta(GEN data, GEN t, long m, long bitprec)
     881             : {
     882       47246 :   pari_sp ltop = avma;
     883             :   long limt, d;
     884             :   GEN isqN, vecan, Vga, ldata, theta, thetainit, S;
     885             :   long n, prec;
     886             : 
     887       47246 :   theta = lfunthetacheckinit(data, t, m, bitprec);
     888       47239 :   ldata = linit_get_ldata(theta);
     889       47239 :   thetainit = linit_get_tech(theta);
     890       47239 :   vecan = theta_get_an(thetainit);
     891       47239 :   isqN = theta_get_isqrtN(thetainit);
     892       47239 :   prec = maxss(realprec(isqN), nbits2prec(bitprec));
     893       47239 :   t = gprec_w(t, prec);
     894       47239 :   limt = lg(vecan)-1;
     895       47239 :   if (theta == data)
     896       45391 :     limt = minss(limt, lfunthetacost(ldata, t, m, bitprec));
     897       47239 :   if (!limt)
     898             :   {
     899          14 :     set_avma(ltop); S = real_0_bit(-bitprec);
     900          14 :     if (!is_real_t(typ(t)) || !ldata_isreal(ldata))
     901           7 :       S = gerepilecopy(ltop, mkcomplex(S,S));
     902          14 :     return S;
     903             :   }
     904       47225 :   t = gmul(t, isqN);
     905       47225 :   Vga = ldata_get_gammavec(ldata);
     906       47225 :   d = lg(Vga)-1;
     907       47225 :   if (m == 0 && Vgaeasytheta(Vga))
     908             :   {
     909       43158 :     if (theta_get_m(thetainit) > 0) vecan = antwist(vecan, Vga, prec);
     910       43158 :     if (d == 1) S = theta1(vecan, limt, t, gel(Vga,1), prec);
     911        5764 :     else        S = theta2_i(vecan, limt, t, vecmin(Vga), prec);
     912             :   }
     913             :   else
     914             :   {
     915        4067 :     GEN K = theta_get_K(thetainit);
     916        4067 :     GEN vroots = mkvroots(d, limt, prec);
     917             :     pari_sp av;
     918        4067 :     t = gpow(t, gdivgu(gen_2,d), prec);
     919        4067 :     S = gen_0; av = avma;
     920    14715946 :     for (n = 1; n <= limt; ++n)
     921             :     {
     922    14711879 :       GEN nt, an = get_an(vecan, n);
     923    14711879 :       if (!an) continue;
     924     2025729 :       nt = gmul(gel(vroots,n), t);
     925     2025729 :       if (m) an = gmul(an, powuu(n, m));
     926     2025729 :       S = gadd(S, gmul(an, gammamellininvrt(K, nt, bitprec)));
     927     2025729 :       if ((n & 0x1ff) == 0) S = gerepileupto(av, S);
     928             :     }
     929        4067 :     if (m) S = gmul(S, gpowgs(isqN, m));
     930             :   }
     931       47225 :   return gerepileupto(ltop, S);
     932             : }
     933             : 
     934             : /*******************************************************************/
     935             : /* Second part: Computation of L-Functions.                        */
     936             : /*******************************************************************/
     937             : 
     938             : struct lfunp {
     939             :   long precmax, Dmax, D, M, m0, nmax, d, vgaell;
     940             :   double k1, dc, dw, dh, MAXs, sub;
     941             :   GEN L, an, bn;
     942             : };
     943             : 
     944             : static void
     945       22412 : lfunp_set(GEN ldata, long der, long bitprec, struct lfunp *S)
     946             : {
     947       22412 :   const long derprec = (der > 1)? dbllog2(mpfact(der)): 0; /* log2(der!) */
     948             :   GEN Vga, N, L, k;
     949             :   long k1, d, m, M, flag, nmax;
     950             :   double a, A, E, hd, Ep, d2, suma, maxs, mins, sub, B0,B1;
     951             :   double logN2, logC, Lestimate, Mestimate;
     952             : 
     953       22412 :   Vga = ldata_get_gammavec(ldata);
     954       22412 :   S->d = d = lg(Vga)-1; d2 = d/2.;
     955             : 
     956       22412 :   suma = gtodouble(sumVga(Vga));
     957       22412 :   k = ldata_get_k(ldata);
     958       22412 :   N = ldata_get_conductor(ldata);
     959       22412 :   logN2 = log(gtodouble(N)) / 2;
     960       22412 :   maxs = S->dc + S->dw;
     961       22412 :   mins = S->dc - S->dw;
     962       22412 :   S->MAXs = maxdd(maxs, gtodouble(k)-mins);
     963             : 
     964             :   /* we compute Lambda^(der)(s) / der!; need to compensate for L^(der)(s)
     965             :    * ln |gamma(s)| ~ -(pi/4) \sum_i |Im(s + a_i)|; max with 1: fudge factor */
     966       22412 :   a = (M_PI/(4*M_LN2))*(d*S->dh + sumVgaimpos(Vga));
     967       22412 :   S->D = (long)ceil(bitprec + derprec + maxdd(a, 1));
     968       22412 :   E = M_LN2*S->D; /* D:= required absolute bitprec */
     969             : 
     970       22412 :   Ep = E + maxdd(M_PI * S->dh * d2, (d*S->MAXs + suma - 1) * log(E));
     971       22412 :   hd = d2*M_PI*M_PI / Ep;
     972       22412 :   S->m0 = (long)ceil(M_LN2/hd);
     973       22412 :   hd = M_LN2/S->m0;
     974             : 
     975       22412 :   logC = d2*M_LN2 - log(d2)/2;
     976       22412 :   k1 = maxdd(ldata_get_k1_dbl(ldata), 0.);
     977       22412 :   S->k1 = k1; /* assume |a_n| << n^k1 with small implied constant */
     978       22412 :   A = gammavec_expo(d, suma);
     979             : 
     980       22412 :   sub = 0.;
     981       22412 :   if (mins > 1)
     982             :   {
     983        4942 :     GEN sig = dbltor(mins);
     984        4942 :     sub += logN2*mins;
     985        4942 :     if (gammaordinary(Vga, sig))
     986             :     {
     987             :       long ext;
     988        4851 :       GEN gas = gammafactproduct(gammafactor(Vga), sig, &ext, LOWDEFAULTPREC);
     989        4851 :       if (typ(gas) != t_SER)
     990             :       {
     991        4851 :         double dg = dbllog2(gas);
     992        4851 :         if (dg > 0) sub += dg * M_LN2;
     993             :       }
     994             :     }
     995             :   }
     996       22412 :   S->sub = sub;
     997       22412 :   M = 1000;
     998       22412 :   L = cgetg(M+2, t_VECSMALL);
     999       22412 :   a = S->k1 + A;
    1000             : 
    1001       22412 :   B0 = 5 + E - S->sub + logC + S->k1*logN2; /* 5 extra bits */
    1002       22412 :   B1 = hd * (S->MAXs - S->k1);
    1003       22412 :   Lestimate = dblcoro526(a + S->MAXs - 2./d, d/2.,
    1004       22412 :     E - S->sub + logC - log(2*M_PI*hd) + S->MAXs*logN2);
    1005       22412 :   Mestimate = ((Lestimate > 0? log(Lestimate): 0) + logN2) / hd;
    1006       22412 :   nmax = 0;
    1007       22412 :   flag = 0;
    1008       22412 :   for (m = 0;; m++)
    1009     2252100 :   {
    1010     2274512 :     double x, H = logN2 - m*hd, B = B0 + m*B1;
    1011             :     long n;
    1012     2274512 :     x = dblcoro526(a, d/2., B);
    1013     2274512 :     n = floor(x*exp(H));
    1014     2274512 :     if (n > nmax) nmax = n;
    1015     2274512 :     if (m > M) { M *= 2; L = vecsmall_lengthen(L,M+2); }
    1016     2274512 :     L[m+1] = n;
    1017     2274512 :     if (n == 0) { if (++flag > 2 && m > Mestimate) break; } else flag = 0;
    1018             :   }
    1019       23252 :   m -= 2; while (m > 0 && !L[m]) m--;
    1020       22412 :   if (m == 0) { nmax = 1; L[1] = 1; m = 1; } /* can happen for tiny bitprec */
    1021       22412 :   setlg(L, m+1); S->M = m-1;
    1022       22412 :   S->L = L;
    1023       22412 :   S->nmax = nmax;
    1024             : 
    1025       22412 :   S->Dmax = S->D + (long)ceil((S->M * hd * S->MAXs - S->sub) / M_LN2);
    1026       22412 :   if (S->Dmax < S->D) S->Dmax = S->D;
    1027       22412 :   S->precmax = nbits2prec(S->Dmax);
    1028       22412 :   if (DEBUGLEVEL > 1)
    1029           0 :     err_printf("Dmax=%ld, D=%ld, M = %ld, nmax = %ld, m0 = %ld\n",
    1030             :                S->Dmax,S->D,S->M,S->nmax, S->m0);
    1031       22412 : }
    1032             : 
    1033             : static GEN
    1034        6310 : lfuninit_pol(GEN v, GEN poqk, long prec)
    1035             : {
    1036        6310 :   long m, M = lg(v) - 2;
    1037        6310 :   GEN pol = cgetg(M+3, t_POL);
    1038        6310 :   pol[1] = evalsigne(1) | evalvarn(0);
    1039        6310 :   gel(pol, 2) = gprec_w(gmul2n(gel(v,1), -1), prec);
    1040        6310 :   if (poqk)
    1041      370889 :     for (m = 2; m <= M+1; m++)
    1042      364635 :       gel(pol, m+1) = gprec_w(gmul(gel(poqk,m), gel(v,m)), prec);
    1043             :   else
    1044        2324 :     for (m = 2; m <= M+1; m++)
    1045        2268 :       gel(pol, m+1) = gprec_w(gel(v,m), prec);
    1046        6310 :   return RgX_renormalize_lg(pol, M+3);
    1047             : }
    1048             : 
    1049             : static void
    1050       67795 : worker_init(long q, GEN *an, GEN *bn, GEN *AB, GEN *A, GEN *B)
    1051             : {
    1052       67795 :   if (typ(*bn) == t_INT) *bn = NULL;
    1053       67795 :   if (*bn)
    1054             :   {
    1055        1016 :     *AB = cgetg(3, t_VEC);
    1056        1016 :     gel(*AB,1) = *A = cgetg(q+1, t_VEC);
    1057        1016 :     gel(*AB,2) = *B = cgetg(q+1, t_VEC);
    1058        1016 :     if (typ(an) == t_VEC) *an = RgV_kill0(*an);
    1059        1016 :     if (typ(bn) == t_VEC) *bn = RgV_kill0(*bn);
    1060             :   }
    1061             :   else
    1062             :   {
    1063       66779 :     *B = NULL;
    1064       66779 :     *AB = *A = cgetg(q+1, t_VEC);
    1065       66779 :     if (typ(*an) == t_VEC) *an = RgV_kill0(*an);
    1066             :   }
    1067       67799 : }
    1068             : GEN
    1069       14490 : lfuninit_theta2_worker(long r, GEN L, GEN qk, GEN a, GEN di, GEN an, GEN bn)
    1070             : {
    1071       14490 :   long q, m, prec = di[1], M = di[2], m0 = di[3], L0 = lg(an)-1;
    1072             :   GEN AB, A, B;
    1073       14490 :   worker_init((M - r) / m0 + 1, &an, &bn, &AB, &A, &B);
    1074      155858 :   for (q = 0, m = r; m <= M; m += m0, q++)
    1075             :   {
    1076      141360 :     GEN t = gel(qk, m+1);
    1077      141360 :     long N = minss(L[m+1],L0);
    1078      141361 :     gel(A, q+1) = theta2(an, N, t, a, prec); /* theta(exp(mh)) */
    1079      141371 :     if (bn) gel(B, q+1) = theta2(bn, N, t, a, prec);
    1080             :   }
    1081       14498 :   return AB;
    1082             : }
    1083             : 
    1084             : /* theta(exp(mh)) ~ sum_{n <= N} a(n) k[m,n] */
    1085             : static GEN
    1086      224787 : an_msum(GEN an, long N, GEN vKm)
    1087             : {
    1088      224787 :   pari_sp av = avma;
    1089      224787 :   GEN s = gen_0;
    1090             :   long n;
    1091    12109167 :   for (n = 1; n <= N; n++)
    1092    11884980 :     if (gel(vKm,n))
    1093             :     {
    1094     5916806 :       GEN c = mul_an(an, n, gel(vKm,n));
    1095     5916058 :       if (c) s = gadd(s, c);
    1096             :     }
    1097      224187 :   return gerepileupto(av, s);
    1098             : }
    1099             : 
    1100             : GEN
    1101       53310 : lfuninit_worker(long r, GEN K, GEN L, GEN peh2d, GEN vroots, GEN dr, GEN di,
    1102             :                 GEN an, GEN bn)
    1103             : {
    1104       53310 :   pari_sp av0 = avma;
    1105       53310 :   long m, n, q, L0 = lg(an)-1;
    1106       53310 :   double sig0 = rtodbl(gel(dr,1)), sub2 = rtodbl(gel(dr,2));
    1107       53301 :   double k1 = rtodbl(gel(dr,3)), MAXs = rtodbl(gel(dr,4));
    1108       53304 :   long D = di[1], M = di[2], m0 = di[3];
    1109       53304 :   double M0 = sig0? sub2 / sig0: 1./0.;
    1110       53304 :   GEN AB, A, B, vK = cgetg(M/m0 + 2, t_VEC);
    1111             : 
    1112      277094 :   for (q = 0, m = r; m <= M; m += m0, q++)
    1113      223792 :     gel(vK, q+1) = const_vec(L[m+1], NULL);
    1114       53302 :   worker_init(q, &an, &bn, &AB, &A, &B);
    1115      276303 :   for (m -= m0, q--; m >= 0; m -= m0, q--)
    1116             :   {
    1117      223835 :     double c1 = D + ((m > M0)? m * sig0 - sub2 : 0);
    1118      223835 :     GEN vKm = gel(vK,q+1); /* conceptually K(m,n) */
    1119    12112807 :     for (n = 1; n <= L[m+1]; n++)
    1120             :     {
    1121             :       GEN t2d, kmn;
    1122    11889813 :       long nn, mm, qq, p = 0;
    1123             :       double c, c2;
    1124             :       pari_sp av;
    1125             : 
    1126    11889813 :       if (gel(vKm, n)) continue; /* done already */
    1127     8916374 :       c = c1 + k1 * log2(n);
    1128             :       /* n *= 2; m -= m0 => c += c2, provided m >= M0. Else c += k1 */
    1129     8916374 :       c2 = k1 - MAXs;
    1130             :       /* p = largest (absolute) accuracy to which we need K(m,n) */
    1131    14073864 :       for (mm=m,nn=n; mm >= M0;)
    1132             :       {
    1133    10500796 :         if (nn <= L[mm+1] && (gel(an, nn) || (bn && gel(bn, nn))))
    1134     2912474 :           if (c > 0) p = maxuu(p, (ulong)c);
    1135    10500983 :         nn <<= 1;
    1136    10500983 :         mm -= m0; if (mm >= M0) c += c2; else { c += k1; break; }
    1137             :       }
    1138             :       /* mm < M0 || nn > L[mm+1] */
    1139    16065622 :       for (         ; mm >= 0; nn<<=1,mm-=m0,c+=k1)
    1140     7149123 :         if (nn <= L[mm+1] && (gel(an, nn) || (bn && gel(bn, nn))))
    1141     1727477 :           if (c > 0) p = maxuu(p, (ulong)c);
    1142     8916499 :       if (!p) continue; /* a_{n 2^v} = 0 for all v in range */
    1143     2946338 :       av = avma;
    1144     2946338 :       t2d = mpmul(gel(vroots,n), gel(peh2d,m+1));/*(n exp(mh)/sqrt(N))^(2/d)*/
    1145     2947227 :       kmn = gerepileupto(av, gammamellininvrt(K, t2d, p));
    1146     9006591 :       for (qq=q,mm=m,nn=n; mm >= 0; nn<<=1,mm-=m0,qq--)
    1147     6061219 :         if (nn <= L[mm+1]) gmael(vK, qq+1, nn) = kmn;
    1148             :     }
    1149             :   }
    1150      276233 :   for (q = 0, m = r; m <= M; m += m0, q++)
    1151             :   {
    1152      223775 :     long N = minss(L0, L[m+1]);
    1153      223774 :     gel(A, q+1) = an_msum(an, N, gel(vK,q+1));
    1154      223764 :     if (bn) gel(B, q+1) = an_msum(bn, N, gel(vK,q+1));
    1155             :   }
    1156       52458 :   return gerepileupto(av0, AB);
    1157             : }
    1158             : /* return A = [\theta(exp(mh)), m=0..M], theta(t) = sum a(n) K(n/sqrt(N) t),
    1159             :  * h = log(2)/m0. If bn != NULL, return the pair [A, B] */
    1160             : static GEN
    1161        6156 : lfuninit_ab(GEN theta, GEN h, struct lfunp *S)
    1162             : {
    1163        6156 :   const long M = S->M, prec = S->precmax;
    1164        6156 :   GEN tech = linit_get_tech(theta), isqN = theta_get_isqrtN(tech);
    1165        6156 :   GEN an = S->an, bn = S->bn, va, vb;
    1166             :   struct pari_mt pt;
    1167             :   GEN worker;
    1168             :   long m0, r, pending;
    1169             : 
    1170        6156 :   if (S->vgaell)
    1171             :   { /* d=2 and Vga = [a,a+1] */
    1172        2800 :     GEN a = vecmin(ldata_get_gammavec(linit_get_ldata(theta)));
    1173        2800 :     GEN qk = gpowers0(mpexp(h), M, isqN);
    1174        2800 :     m0 = minss(M+1, mt_nbthreads());
    1175        2800 :     worker = snm_closure(is_entry("_lfuninit_theta2_worker"),
    1176             :                          mkvecn(6, S->L, qk, a, mkvecsmall3(prec, M, m0),
    1177             :                                 an, bn? bn: gen_0));
    1178             :   }
    1179             :   else
    1180             :   {
    1181             :     GEN vroots, peh2d, d2;
    1182        3356 :     double sig0 = S->MAXs / S->m0, sub2 = S->sub / M_LN2;
    1183             :     /* For all 0<= m <= M, and all n <= L[m+1] such that a_n!=0, we compute
    1184             :      *   k[m,n] = K(n exp(mh)/sqrt(N))
    1185             :      * with ln(absolute error) <= E + max(mh sigma - sub, 0) + k1 * log(n).
    1186             :      * N.B. we use the 'rt' variant and pass (n exp(mh)/sqrt(N))^(2/d).
    1187             :      * Speedup: if n' = 2n and m' = m - m0 >= 0; then k[m,n] = k[m',n']. */
    1188        3356 :     vroots = mkvroots(S->d, S->nmax, prec); /* vroots[n] = n^(2/d) */
    1189        3356 :     d2 = gdivgu(gen_2, S->d);
    1190        3356 :     peh2d = gpowers0(gexp(gmul(d2,h), prec), M, gpow(isqN, d2, prec));
    1191        3356 :     m0 = S->m0; /* peh2d[m+1] = (exp(mh)/sqrt(N))^(2/d) */
    1192        3356 :     worker = snm_closure(is_entry("_lfuninit_worker"),
    1193             :                          mkvecn(8, theta_get_K(tech), S->L, peh2d, vroots,
    1194             :                                 mkvec4(dbltor(sig0), dbltor(sub2),
    1195             :                                        dbltor(S->k1), dbltor(S->MAXs)),
    1196             :                                 mkvecsmall3(S->D, M, m0),
    1197             :                                 an, bn? bn: gen_0));
    1198             :     /* For each 0 <= m <= M, we will sum for n<=L[m+1] a(n) K(m,n)
    1199             :      * bit accuracy for K(m,n): D + k1*log2(n) + 1_{m > M0} (m*sig0 - sub2)
    1200             :      * We restrict m to arithmetic progressions r mod m0 to save memory and
    1201             :      * allow parallelization */
    1202             :   }
    1203        6156 :   va = cgetg(M+2, t_VEC);
    1204        6156 :   vb = bn? cgetg(M+2, t_VEC): NULL;
    1205        6156 :   mt_queue_start_lim(&pt, worker, m0);
    1206        6156 :   pending = 0;
    1207       92364 :   for (r = 0; r < m0 || pending; r++)
    1208             :   { /* m = q m0 + r */
    1209             :     GEN done, A, B;
    1210             :     long q, m, workid;
    1211       86208 :     mt_queue_submit(&pt, r, r < m0 ? mkvec(utoi(r)): NULL);
    1212       86208 :     done = mt_queue_get(&pt, &workid, &pending);
    1213       86208 :     if (!done) continue;
    1214       67825 :     if (bn) { A = gel(done,1); B = gel(done,2); } else { A = done; B = NULL; }
    1215      433058 :     for (q = 0, m = workid; m <= M; m += m0, q++)
    1216             :     {
    1217      365233 :       gel(va, m+1) = gel(A, q+1);
    1218      365233 :       if (bn) gel(vb, m+1) = gel(B, q+1);
    1219             :     }
    1220             :   }
    1221        6156 :   mt_queue_end(&pt);
    1222        6156 :   return bn? mkvec2(va, vb): va;
    1223             : }
    1224             : 
    1225             : static void
    1226      126484 : parse_dom(double k, GEN dom, struct lfunp *S)
    1227             : {
    1228      126484 :   long l = lg(dom);
    1229      126484 :   if (typ(dom)!=t_VEC) pari_err_TYPE("lfuninit [domain]", dom);
    1230      126484 :   if (l == 1)
    1231             :   {
    1232          84 :     S->dc = 0;
    1233          84 :     S->dw = -1;
    1234          84 :     S->dh = -1; return;
    1235             :   }
    1236      126400 :   if (l == 2)
    1237             :   {
    1238       38124 :     S->dc = k/2.;
    1239       38124 :     S->dw = 0.;
    1240       38124 :     S->dh = gtodouble(gel(dom,1));
    1241             :   }
    1242       88276 :   else if (l == 3)
    1243             :   {
    1244         301 :     S->dc = k/2.;
    1245         301 :     S->dw = gtodouble(gel(dom,1));
    1246         301 :     S->dh = gtodouble(gel(dom,2));
    1247             :   }
    1248       87975 :   else if (l == 4)
    1249             :   {
    1250       87975 :     S->dc = gtodouble(gel(dom,1));
    1251       87975 :     S->dw = gtodouble(gel(dom,2));
    1252       87975 :     S->dh = gtodouble(gel(dom,3));
    1253             :   }
    1254             :   else
    1255             :   {
    1256           0 :     pari_err_TYPE("lfuninit [domain]", dom);
    1257           0 :     S->dc = S->dw = S->dh = 0; /*-Wall*/
    1258             :   }
    1259      126400 :   if (S->dw < 0 || S->dh < 0) pari_err_TYPE("lfuninit [domain]", dom);
    1260             : }
    1261             : 
    1262             : /* do we have dom \subset dom0 ? dom = [center, width, height] */
    1263             : int
    1264       24742 : sdomain_isincl(double k, GEN dom, GEN dom0)
    1265             : {
    1266             :   struct lfunp S0, S;
    1267       24742 :   parse_dom(k, dom, &S); if (S.dw < 0) return 1;
    1268       24742 :   parse_dom(k, dom0, &S0); if (S0.dw < 0) return 0;
    1269       24742 :   return S0.dc - S0.dw <= S.dc - S.dw
    1270       24742 :       && S0.dc + S0.dw >= S.dc + S.dw && S0.dh >= S.dh;
    1271             : }
    1272             : 
    1273             : static int
    1274       24819 : checklfuninit(GEN linit, GEN DOM, long der, long bitprec)
    1275             : {
    1276       24819 :   GEN ldata = linit_get_ldata(linit);
    1277       24819 :   GEN domain = lfun_get_domain(linit_get_tech(linit));
    1278       24819 :   GEN dom = domain_get_dom(domain);
    1279       24819 :   if (lg(dom) == 1) return 1;
    1280       24742 :   return domain_get_der(domain) >= der
    1281       24742 :     && domain_get_bitprec(domain) >= bitprec
    1282       49484 :     && sdomain_isincl(gtodouble(ldata_get_k(ldata)), DOM, dom);
    1283             : }
    1284             : 
    1285             : static GEN
    1286        1078 : ginvsqrtvec(GEN x, long prec)
    1287             : {
    1288        1078 :   if (is_vec_t(typ(x)))
    1289        1085 :     pari_APPLY_same(ginv(gsqrt(gel(x,i), prec)))
    1290         770 :   else return ginv(gsqrt(x, prec));
    1291             : }
    1292             : 
    1293             : GEN
    1294        7175 : lfuninit_make(long t, GEN ldata, GEN tech, GEN domain)
    1295             : {
    1296        7175 :   GEN Vga = ldata_get_gammavec(ldata);
    1297        7175 :   long d = lg(Vga)-1;
    1298        7175 :   GEN w2 = gen_1, k2 = gmul2n(ldata_get_k(ldata), -1);
    1299        7175 :   GEN expot = gdivgu(gadd(gmulsg(d, gsubgs(k2, 1)), sumVga(Vga)), 4);
    1300        7175 :   if (typ(ldata_get_dual(ldata))==t_INT)
    1301             :   {
    1302        7021 :     GEN eno = ldata_get_rootno(ldata);
    1303        7021 :     long prec = nbits2prec( domain_get_bitprec(domain) );
    1304        7021 :     if (!isint1(eno)) w2 = ginvsqrtvec(eno, prec);
    1305             :   }
    1306        7175 :   tech = mkvec3(domain, tech, mkvec4(k2, w2, expot, gammafactor(Vga)));
    1307        7175 :   return mkvec3(mkvecsmall(t), ldata, tech);
    1308             : }
    1309             : static GEN
    1310         165 : lfunnoinit(GEN ldata, long bitprec)
    1311             : {
    1312         165 :   GEN tech, domain = mkvec2(cgetg(1,t_VEC), mkvecsmall2(0, bitprec));
    1313         165 :   GEN R = gen_0, r = ldata_get_residue(ldata), v = lfunrootres(ldata, bitprec);
    1314         165 :   ldata = shallowcopy(ldata);
    1315         165 :   gel(ldata,6) = gel(v,3);
    1316         165 :   if (r)
    1317             :   {
    1318         137 :     if (isintzero(r)) setlg(ldata,7); else gel(ldata,7) = r;
    1319         137 :     R = gel(v,2);
    1320             :   }
    1321         165 :   tech = mkvec3(domain, gen_0, R);
    1322         165 :   return lfuninit_make(t_LDESC_INIT, ldata, tech, domain);
    1323             : }
    1324             : 
    1325             : static void
    1326        3356 : lfunparams2(struct lfunp *S)
    1327             : {
    1328        3356 :   GEN L = S->L, an = S->an, bn = S->bn;
    1329             :   double pmax;
    1330        3356 :   long m, nan, nmax, neval, M = S->M;
    1331             : 
    1332        3356 :   S->vgaell = 0;
    1333             :   /* try to reduce parameters now we know the a_n (some may be 0) */
    1334        3356 :   if (typ(an) == t_VEC) an = RgV_kill0(an);
    1335        3356 :   nan = S->nmax; /* lg(an)-1 may be large than this */
    1336        3356 :   nmax = neval = 0;
    1337        3356 :   if (!bn)
    1338      226167 :     for (m = 0; m <= M; m++)
    1339             :     {
    1340      222832 :       long n = minss(nan, L[m+1]);
    1341      318438 :       while (n > 0 && !gel(an,n)) n--;
    1342      222832 :       if (n > nmax) nmax = n;
    1343      222832 :       neval += n;
    1344      222832 :       L[m+1] = n; /* reduce S->L[m+1] */
    1345             :     }
    1346             :   else
    1347             :   {
    1348          21 :     if (typ(bn) == t_VEC) bn = RgV_kill0(bn);
    1349        1036 :     for (m = 0; m <= M; m++)
    1350             :     {
    1351        1015 :       long n = minss(nan, L[m+1]);
    1352        1057 :       while (n > 0 && !gel(an,n) && !gel(bn,n)) n--;
    1353        1015 :       if (n > nmax) nmax = n;
    1354        1015 :       neval += n;
    1355        1015 :       L[m+1] = n; /* reduce S->L[m+1] */
    1356             :     }
    1357             :   }
    1358        3356 :   if (DEBUGLEVEL >= 1) err_printf("expected evaluations: %ld\n", neval);
    1359        3356 :   for (; M > 0; M--)
    1360        3356 :     if (L[M+1]) break;
    1361        3356 :   setlg(L, M+2);
    1362        3356 :   S->M = M;
    1363        3356 :   S->nmax = nmax;
    1364             : 
    1365             :   /* need K(n*exp(mh)/sqrt(N)) to absolute accuracy
    1366             :    *   D + k1*log(n) + max(m * sig0 - sub2, 0) */
    1367        3356 :   pmax = S->D + S->k1 * log2(L[1]);
    1368        3356 :   if (S->MAXs)
    1369             :   {
    1370        3356 :     double sig0 = S->MAXs/S->m0, sub2 = S->sub / M_LN2;
    1371      194239 :     for (m = ceil(sub2 / sig0); m <= S->M; m++)
    1372             :     {
    1373      190883 :       double c = S->D + m*sig0 - sub2;
    1374      190883 :       if (S->k1 > 0) c += S->k1 * log2(L[m+1]);
    1375      190883 :       pmax = maxdd(pmax, c);
    1376             :     }
    1377             :   }
    1378        3356 :   S->Dmax = pmax;
    1379        3356 :   S->precmax = nbits2prec(pmax);
    1380        3356 : }
    1381             : 
    1382             : static GEN
    1383        6170 : lfun_init_theta(GEN ldata, GEN eno, struct lfunp *S)
    1384             : {
    1385        6170 :   GEN an2, dual, tdom = NULL, Vga = ldata_get_gammavec(ldata);
    1386        6170 :   long L, prec = S->precmax;
    1387        6170 :   if (eno)
    1388        4483 :     L = S->nmax;
    1389             :   else
    1390             :   {
    1391        1687 :     tdom = dbltor(sqrt(0.5));
    1392        1687 :     L = maxss(S->nmax, lfunthetacost(ldata, tdom, 0, S->D));
    1393             :   }
    1394        6170 :   dual = ldata_get_dual(ldata);
    1395        6170 :   S->an = ldata_vecan(ldata_get_an(ldata), L, prec);
    1396        6156 :   S->bn = typ(dual)==t_INT? NULL: ldata_vecan(dual, S->nmax, prec);
    1397        6156 :   if (!vgaell(Vga)) lfunparams2(S);
    1398             :   else
    1399             :   {
    1400        2800 :     S->an = antwist(S->an, Vga, prec);
    1401        2800 :     if (S->bn) S->bn = antwist(S->bn, Vga, prec);
    1402        2800 :     S->vgaell = 1;
    1403             :   }
    1404        6156 :   an2 = lg(Vga)-1 == 1? antwist(S->an, Vga, prec): S->an;
    1405        6156 :   return lfunthetainit0(ldata, tdom, an2, 0, S->Dmax, 0);
    1406             : }
    1407             : 
    1408             : GEN
    1409       16242 : lfuncost(GEN L, GEN dom, long der, long bit)
    1410             : {
    1411       16242 :   pari_sp av = avma;
    1412       16242 :   GEN ldata = lfunmisc_to_ldata_shallow(L);
    1413       16242 :   GEN w, k = ldata_get_k(ldata);
    1414             :   struct lfunp S;
    1415             : 
    1416       16242 :   parse_dom(gtodouble(k), dom, &S); if (S.dw < 0) return mkvecsmall2(0, 0);
    1417       16242 :   lfunp_set(ldata, der, bit, &S);
    1418       16242 :   w = ldata_get_rootno(ldata);
    1419       16242 :   if (isintzero(w)) /* for lfunrootres */
    1420           7 :     S.nmax = maxss(S.nmax, lfunthetacost(ldata, dbltor(sqrt(0.5)), 0, bit+1));
    1421       16242 :   set_avma(av); return mkvecsmall2(S.nmax, S.Dmax);
    1422             : }
    1423             : GEN
    1424          49 : lfuncost0(GEN L, GEN dom, long der, long bitprec)
    1425             : {
    1426          49 :   pari_sp av = avma;
    1427             :   GEN C;
    1428             : 
    1429          49 :   if (is_linit(L))
    1430             :   {
    1431          28 :     GEN tech = linit_get_tech(L);
    1432          28 :     GEN domain = lfun_get_domain(tech);
    1433          28 :     dom = domain_get_dom(domain);
    1434          28 :     der = domain_get_der(domain);
    1435          28 :     bitprec = domain_get_bitprec(domain);
    1436          28 :     if (linit_get_type(L) == t_LDESC_PRODUCT)
    1437             :     {
    1438          21 :       GEN v = lfunprod_get_fact(linit_get_tech(L)), F = gel(v,1);
    1439          21 :       long i, l = lg(F);
    1440          21 :       C = cgetg(l, t_VEC);
    1441          70 :       for (i = 1; i < l; ++i)
    1442          49 :         gel(C, i) = zv_to_ZV( lfuncost(gel(F,i), dom, der, bitprec) );
    1443          21 :       return gerepileupto(av, C);
    1444             :     }
    1445             :   }
    1446          28 :   if (!dom) pari_err_TYPE("lfuncost [missing s domain]", L);
    1447          28 :   C = lfuncost(L,dom,der,bitprec);
    1448          28 :   return gerepileupto(av, zv_to_ZV(C));
    1449             : }
    1450             : 
    1451             : static int
    1452        5152 : is_dirichlet(GEN ldata)
    1453             : {
    1454        5152 :   switch(ldata_get_type(ldata))
    1455             :   {
    1456        1302 :     case t_LFUN_ZETA:
    1457             :     case t_LFUN_KRONECKER:
    1458        1302 :     case t_LFUN_CHIZ: return 1;
    1459         770 :     case t_LFUN_CHIGEN: return ldata_get_degree(ldata)==1;
    1460        3080 :     default: return 0;
    1461             :   }
    1462             : }
    1463             : 
    1464             : static ulong
    1465       10294 : lfuninit_cutoff(GEN ldata)
    1466             : {
    1467       10294 :   GEN gN = ldata_get_conductor(ldata);
    1468             :   ulong L, N;
    1469       10294 :   if (ldata_get_type(ldata) == t_LFUN_NF) /* N ~ f^(d-1), exact for d prime */
    1470         721 :     gN = sqrtnint(gN, ldata_get_degree(ldata) - 1);
    1471       10294 :   N = itou_or_0(gN);
    1472       10294 :   if (N > 1000) L = 7000;
    1473       10280 :   else if (N > 100) L = 5000;
    1474        7319 :   else if (N > 15) L = 3000;
    1475        7130 :   else L = 2500;
    1476       10294 :   return L;
    1477             : }
    1478             : 
    1479             : GEN
    1480       31752 : lfuninit(GEN lmisc, GEN dom, long der, long bitprec)
    1481             : {
    1482       31752 :   pari_sp av = avma;
    1483             :   GEN poqk, AB, R, h, theta, ldata, eno, r, domain, tech, k;
    1484             :   struct lfunp S;
    1485             : 
    1486       31752 :   if (is_linit(lmisc))
    1487             :   {
    1488       24868 :     long t = linit_get_type(lmisc);
    1489       24868 :     if (t == t_LDESC_INIT || t == t_LDESC_PRODUCT)
    1490             :     {
    1491       24819 :       if (checklfuninit(lmisc, dom, der, bitprec)) return lmisc;
    1492          21 :       pari_warn(warner,"lfuninit: insufficient initialization");
    1493             :     }
    1494             :   }
    1495        6954 :   ldata = lfunmisc_to_ldata_shallow(lmisc);
    1496             : 
    1497        6954 :   switch (ldata_get_type(ldata))
    1498             :   {
    1499         609 :   case t_LFUN_NF:
    1500             :     {
    1501         609 :       GEN T = gel(ldata_get_an(ldata), 2);
    1502         609 :       return gerepilecopy(av, lfunzetakinit(T, dom, der, bitprec));
    1503             :     }
    1504          91 :   case t_LFUN_ABELREL:
    1505             :     {
    1506          91 :       GEN T = gel(ldata_get_an(ldata), 2);
    1507          91 :       return gerepilecopy(av, lfunabelianrelinit(gel(T,1), gel(T,2), dom, der, bitprec));
    1508             :     }
    1509             :   }
    1510        6254 :   k = ldata_get_k(ldata);
    1511        6254 :   parse_dom(gtodouble(k), dom, &S);
    1512             :   /* Reduce domain for Dirichlet characters. NOT for Abelian t_LFUN_NF,
    1513             :    * handled above. */
    1514        6254 :   if (S.dw >= 0 && (!der && is_dirichlet(ldata)))
    1515        1526 :     S.dh = mindd(S.dh, lfuninit_cutoff(ldata));
    1516        6254 :   if (S.dw < 0)
    1517             :   {
    1518          84 :     if (der)
    1519           0 :       pari_err_IMPL("domain = [] for derivatives in lfuninit");
    1520          84 :     if (!is_dirichlet(ldata))
    1521           0 :       pari_err_IMPL("domain = [] for L functions of degree > 1");
    1522          84 :     return gerepilecopy(av, lfunnoinit(ldata, bitprec));
    1523             :   }
    1524             : 
    1525        6170 :   lfunp_set(ldata, der, bitprec, &S);
    1526        6170 :   ldata = ldata_newprec(ldata, nbits2prec(S.Dmax));
    1527        6170 :   r = ldata_get_residue(ldata);
    1528             :   /* Note: all guesses should already have been performed (thetainit more
    1529             :    * expensive than needed: should be either tdom = 1 or bitprec = S.D).
    1530             :    * BUT if the root number / polar part do not have an algebraic
    1531             :    * expression, there is no way to do this until we know the
    1532             :    * precision, i.e. now. So we can't remove guessing code from here and
    1533             :    * lfun_init_theta */
    1534        6170 :   if (r && isintzero(r)) eno = NULL;
    1535             :   else
    1536             :   {
    1537        6170 :     eno = ldata_get_rootno(ldata);
    1538        6170 :     if (isintzero(eno)) eno = NULL;
    1539             :   }
    1540        6170 :   theta = lfun_init_theta(ldata, eno, &S);
    1541        6156 :   if (eno && !r)
    1542        2576 :     R = gen_0;
    1543             :   else
    1544             :   {
    1545        3580 :     GEN v = lfunrootres(theta, S.D);
    1546        3580 :     ldata = shallowcopy(ldata);
    1547        3580 :     gel(ldata, 6) = gel(v,3);
    1548        3580 :     r = gel(v,1);
    1549        3580 :     R = gel(v,2);
    1550        3580 :     if (isintzero(r)) setlg(ldata,7); else gel(ldata, 7) = r;
    1551             :   }
    1552        6156 :   h = divru(mplog2(S.precmax), S.m0);
    1553             :   /* exp(kh/2 . [0..M]) */
    1554        6156 :   poqk = gequal0(k) ? NULL
    1555        6156 :        : gpowers(gprec_w(mpexp(gmul2n(gmul(k,h), -1)), S.precmax), S.M);
    1556        6156 :   AB = lfuninit_ab(theta, h, &S);
    1557        6156 :   if (S.bn)
    1558             :   {
    1559         154 :     GEN A = gel(AB,1), B = gel(AB,2);
    1560         154 :     A = lfuninit_pol(A, poqk, S.precmax);
    1561         154 :     B = lfuninit_pol(B, poqk, S.precmax);
    1562         154 :     AB = mkvec2(A, B);
    1563             :   }
    1564             :   else
    1565        6002 :     AB = lfuninit_pol(AB, poqk, S.precmax);
    1566        6156 :   tech = mkvec3(h, AB, R);
    1567        6156 :   domain = mkvec2(dom, mkvecsmall2(der, bitprec));
    1568        6156 :   return gerepilecopy(av, lfuninit_make(t_LDESC_INIT, ldata, tech, domain));
    1569             : }
    1570             : 
    1571             : GEN
    1572         532 : lfuninit0(GEN lmisc, GEN dom, long der, long bitprec)
    1573             : {
    1574         532 :   GEN z = lfuninit(lmisc, dom, der, bitprec);
    1575         532 :   return z == lmisc? gcopy(z): z;
    1576             : }
    1577             : 
    1578             : /* If s is a pole of Lambda, return polar part at s; else return NULL */
    1579             : static GEN
    1580        5140 : lfunpoleresidue(GEN R, GEN s)
    1581             : {
    1582             :   long j;
    1583       14650 :   for (j = 1; j < lg(R); j++)
    1584             :   {
    1585       10063 :     GEN Rj = gel(R, j), be = gel(Rj, 1);
    1586       10063 :     if (gequal(s, be)) return gel(Rj, 2);
    1587             :   }
    1588        4587 :   return NULL;
    1589             : }
    1590             : 
    1591             : /* Compute contribution of polar part at s when not a pole. */
    1592             : static GEN
    1593        8369 : veccothderivn(GEN a, long n)
    1594             : {
    1595             :   long i;
    1596        8369 :   pari_sp av = avma;
    1597        8369 :   GEN c = pol_x(0), cp = mkpoln(3, gen_m1, gen_0, gen_1);
    1598        8369 :   GEN v = cgetg(n+2, t_VEC);
    1599        8369 :   gel(v, 1) = poleval(c, a);
    1600       25226 :   for(i = 2; i <= n+1; i++)
    1601             :   {
    1602       16857 :     c = ZX_mul(ZX_deriv(c), cp);
    1603       16857 :     gel(v, i) = gdiv(poleval(c, a), mpfact(i-1));
    1604             :   }
    1605        8369 :   return gerepilecopy(av, v);
    1606             : }
    1607             : 
    1608             : static GEN
    1609        8488 : polepart(long n, GEN h, GEN C)
    1610             : {
    1611        8488 :   GEN h2n = gpowgs(gdiv(h, gen_2), n-1);
    1612        8488 :   GEN res = gmul(h2n, gel(C,n));
    1613        8488 :   return odd(n)? res : gneg(res);
    1614             : }
    1615             : 
    1616             : static GEN
    1617        4153 : lfunsumcoth(GEN R, GEN s, GEN h, long prec)
    1618             : {
    1619             :   long i,j;
    1620        4153 :   GEN S = gen_0;
    1621       12522 :   for (j = 1; j < lg(R); ++j)
    1622             :   {
    1623        8369 :     GEN r = gel(R,j), be = gel(r,1), Rj = gel(r, 2);
    1624        8369 :     long e = valser(Rj);
    1625        8369 :     GEN z1 = gexpm1(gmul(h, gsub(s,be)), prec); /* exp(h(s-beta))-1 */
    1626        8369 :     GEN c1 = gaddgs(gdivsg(2, z1), 1); /* coth((h/2)(s-beta)) */
    1627        8369 :     GEN C1 = veccothderivn(c1, 1-e);
    1628       16857 :     for (i = e; i < 0; i++)
    1629             :     {
    1630        8488 :       GEN Rbe = mysercoeff(Rj, i);
    1631        8488 :       GEN p1 = polepart(-i, h, C1);
    1632        8488 :       S = gadd(S, gmul(Rbe, p1));
    1633             :     }
    1634             :   }
    1635        4153 :   return gmul2n(S, -1);
    1636             : }
    1637             : 
    1638             : static GEN lfunlambda_OK(GEN linit, GEN s, GEN sdom, long bitprec);
    1639             : /* L is a t_LDESC_PRODUCT or t_LDESC_INIT Linit */
    1640             : static GEN
    1641        2418 : _product(GEN (*fun)(GEN,GEN,long), GEN L, GEN s, long bitprec)
    1642             : {
    1643        2418 :   GEN ldata = linit_get_ldata(L), v, r, F, E, C, cs;
    1644             :   long i, l;
    1645             :   int isreal;
    1646        2418 :   if (linit_get_type(L) == t_LDESC_INIT) return fun(ldata, s, bitprec);
    1647        1903 :   v = lfunprod_get_fact(linit_get_tech(L));
    1648        1903 :   F = gel(v,1); E = gel(v,2); C = gel(v,3); l = lg(F);
    1649        1903 :   cs = conj_i(s); isreal = gequal(imag_i(s), imag_i(cs));
    1650        6102 :   for (i = 1, r = gen_1; i < l; ++i)
    1651             :   {
    1652        4202 :     GEN f = fun(gel(F, i), s, bitprec);
    1653        4199 :     if (typ(f)==t_VEC) f = RgV_prod(f);
    1654        4199 :     if (E[i]) r = gmul(r, gpowgs(f, E[i]));
    1655        4199 :     if (C[i])
    1656             :     {
    1657           0 :       GEN fc = isreal? f: conj_i(fun(gel(F, i), cs, bitprec));
    1658           0 :       r = gmul(r, gpowgs(fc, C[i]));
    1659             :     }
    1660             :   }
    1661        1900 :   return (ldata_isreal(ldata) && gequal0(imag_i(s)))? real_i(r): r;
    1662             : }
    1663             : 
    1664             : /* s a t_SER; # terms - 1 */
    1665             : static long
    1666        2230 : der_level(GEN s)
    1667        2230 : { return signe(s)? lg(s)-3: valser(s)-1; }
    1668             : 
    1669             : /* s a t_SER; return coeff(s, X^0) */
    1670             : static GEN
    1671        1214 : ser_coeff0(GEN s) { return simplify_shallow(polcoef_i(s, 0, -1)); }
    1672             : 
    1673             : static GEN
    1674       15140 : get_domain(GEN s, GEN *dom, long *der)
    1675             : {
    1676       15140 :   GEN sa = s;
    1677       15140 :   *der = 0;
    1678       15140 :   switch(typ(s))
    1679             :   {
    1680          10 :     case t_POL:
    1681          10 :     case t_RFRAC: s = toser_i(s);
    1682        1214 :     case t_SER:
    1683        1214 :       *der = der_level(s);
    1684        1214 :       sa = ser_coeff0(s);
    1685             :   }
    1686       15140 :   *dom = mkvec3(real_i(sa), gen_0, gabs(imag_i(sa),DEFAULTPREC));
    1687       15140 :   return s;
    1688             : }
    1689             : /* assume s went through get_domain and s/bitprec belong to domain */
    1690             : static GEN
    1691       28910 : lfunlambda_OK(GEN linit, GEN s, GEN sdom, long bitprec)
    1692             : {
    1693       28910 :   GEN dom, eno, ldata, tech, h, pol, k2, cost, S, S0 = NULL;
    1694             :   long prec, prec0;
    1695             :   struct lfunp D, D0;
    1696             : 
    1697       28910 :   if (linit_get_type(linit) == t_LDESC_PRODUCT)
    1698        1658 :     return _product(&lfunlambda, linit, s, bitprec);
    1699       27252 :   ldata = linit_get_ldata(linit);
    1700       27252 :   eno = ldata_get_rootno(ldata);
    1701       27252 :   tech = linit_get_tech(linit);
    1702       27252 :   dom = lfun_get_dom(tech);
    1703       27252 :   if (lg(dom) == 1) return lfunlambda(linit, s, bitprec); /* FIXME:not OK! */
    1704       27252 :   h = lfun_get_step(tech); prec = realprec(h);
    1705             :   /* try to reduce accuracy */
    1706       27252 :   parse_dom(0, sdom, &D0);
    1707       27252 :   parse_dom(0, dom, &D);
    1708       27252 :   if (0.8 * D.dh > D0.dh)
    1709             :   {
    1710       16165 :     cost = lfuncost(linit, sdom, typ(s)==t_SER? der_level(s): 0, bitprec);
    1711       16165 :     prec0 = nbits2prec(cost[2]);
    1712       16165 :     if (prec0 < prec) { prec = prec0; h = gprec_w(h, prec); }
    1713             :   }
    1714       27252 :   pol = lfun_get_pol(tech);
    1715       27252 :   s = gprec_w(s, prec);
    1716       27252 :   if (ldata_get_residue(ldata))
    1717             :   {
    1718        4552 :     GEN R = lfun_get_Residue(tech);
    1719        4552 :     GEN Ra = lfunpoleresidue(R, s);
    1720        4552 :     if (Ra) return gprec_w(Ra, nbits2prec(bitprec));
    1721        4153 :     S0 = lfunsumcoth(R, s, h, prec);
    1722             :   }
    1723       26853 :   k2 = lfun_get_k2(tech);
    1724       26853 :   if (typ(pol)==t_POL && typ(s) != t_SER && gequal(real_i(s), k2))
    1725       20704 :   { /* on critical line: shortcut */
    1726       20704 :     GEN polz, b = imag_i(s);
    1727       20704 :     polz = gequal0(b)? poleval(pol,gen_1): poleval(pol, expIr(gmul(h,b)));
    1728       20704 :     S = gadd(polz, gmulvec(eno, conj_i(polz)));
    1729             :   }
    1730             :   else
    1731             :   {
    1732        6149 :     GEN z = gexp(gmul(h, gsub(s, k2)), prec);
    1733        6149 :     GEN zi = ginv(z), zc = conj_i(zi);
    1734        6149 :     if (typ(pol)==t_POL)
    1735        5953 :       S = gadd(poleval(pol, z), gmulvec(eno, conj_i(poleval(pol, zc))));
    1736             :     else
    1737         196 :       S = gadd(poleval(gel(pol,1), z), gmulvec(eno, poleval(gel(pol,2), zi)));
    1738             :   }
    1739       26853 :   if (S0) S = gadd(S,S0);
    1740       26853 :   return gprec_w(gmul(S,h), nbits2prec(bitprec));
    1741             : }
    1742             : 
    1743             : static long
    1744       33400 : lfunspec_OK(GEN lmisc, GEN s, GEN *pldata)
    1745             : {
    1746       33400 :   long t, large = 0;
    1747             :   GEN ldata;
    1748       33400 :   *pldata = ldata = lfunmisc_to_ldata_shallow(lmisc);
    1749       33393 :   if (!is_linit(lmisc)) lmisc = ldata;
    1750       25370 :   else switch(linit_get_type(lmisc))
    1751             :   {
    1752       25328 :     case t_LDESC_INIT: case t_LDESC_PRODUCT:
    1753       25328 :       if (lg(lfun_get_dom(linit_get_tech(lmisc))) == 1) large = 1;
    1754       25328 :       break;
    1755          42 :     default: return 0;
    1756             :   }
    1757       33351 :   switch(typ(s))
    1758             :   {
    1759       33005 :     case t_INT: case t_REAL: case t_FRAC: case t_COMPLEX: break;
    1760         346 :     default: return 0;
    1761             :   }
    1762       33005 :   t = ldata_get_type(ldata);
    1763       33005 :   switch(t)
    1764             :   {
    1765        7624 :     case t_LFUN_KRONECKER: case t_LFUN_ZETA:
    1766        7624 :       if (typ(s) == t_INT && !is_bigint(s)) return 1;
    1767             :       /* fall through */
    1768             :     case t_LFUN_NF: case t_LFUN_CHIZ:
    1769        7757 :       if (!large)
    1770        7477 :         large = (fabs(gtodouble(imag_i(s))) >= lfuninit_cutoff(ldata));
    1771        7757 :       break;
    1772        1841 :     case t_LFUN_CHIGEN:
    1773        1841 :       if (ldata_get_degree(ldata) != 1) return 0;
    1774        1519 :       if (!large)
    1775        1270 :         large = (fabs(gtodouble(imag_i(s))) >= lfuninit_cutoff(ldata));
    1776        1519 :       break;
    1777             :   }
    1778       28630 :   if (large)
    1779             :   {
    1780        1620 :     if (t == t_LFUN_NF)
    1781             :     {
    1782          14 :       GEN an = ldata_get_an(ldata), nf = gel(an,2), G = galoisinit(nf, NULL);
    1783          14 :       if (isintzero(G) || !group_isabelian(galois_group(G))) return 0;
    1784             :     }
    1785        1620 :     return 2;
    1786             :   }
    1787       27010 :   return 0;
    1788             : }
    1789             : 
    1790             : GEN
    1791        4793 : lfunlambda(GEN lmisc, GEN s, long bitprec)
    1792             : {
    1793        4793 :   pari_sp av = avma;
    1794        4793 :   GEN linit = NULL, dom, z;
    1795             :   long der;
    1796        4793 :   s = get_domain(s, &dom, &der);
    1797        4793 :   if (!der)
    1798             :   {
    1799             :     GEN ldata;
    1800        3967 :     long t = lfunspec_OK(lmisc, s, &ldata);
    1801        3967 :     if (t == 1)
    1802             :     { /* special value ? */
    1803         539 :       GEN z = lfun(ldata, s, bitprec), gv = ldata_get_gammavec(ldata);
    1804         539 :       long e = itou(gel(gv, 1));
    1805         539 :       if (!isintzero(z) && (e || gsigne(s) > 0)) /* TODO */
    1806             :       {
    1807         462 :         GEN q = ldata_get_conductor(ldata);
    1808         462 :         long prec = nbits2prec(bitprec);
    1809         462 :         GEN se, r, Q = divir(q, mppi(prec));
    1810         462 :         se = gmul2n(gaddgs(s, e), -1);
    1811         462 :         r = gmul(gpow(Q, se, prec), ggamma(se, prec));
    1812         462 :         if (e && !equali1(q)) r = gdiv(r, gsqrt(q, prec));
    1813         490 :         return gerepileupto(av, gmul(r, z));
    1814             :       }
    1815             :     }
    1816        3505 :     if (is_linit(lmisc)) linit = lmisc; else lmisc = ldata;
    1817        3505 :     if (t == 2)
    1818          28 :       return gerepilecopy(av, linit? _product(&lfunlambda, linit, s, bitprec)
    1819           0 :                                    : lfunlambdalarge(ldata, s, bitprec));
    1820             :   }
    1821        4303 :   linit = lfuninit(lmisc, dom, der, bitprec);
    1822        4303 :   z = lfunlambda_OK(linit,s, dom, bitprec);
    1823        4303 :   return gerepilecopy(av, z);
    1824             : }
    1825             : 
    1826             : static long
    1827        9803 : is_ser(GEN x)
    1828             : {
    1829        9803 :   long t = typ(x);
    1830        9803 :   if (t == t_SER) return 1;
    1831        7826 :   if (!is_vec_t(t) || lg(x)==1) return 0;
    1832         350 :   if (typ(gel(x,1))==t_SER) return 1;
    1833         252 :   return 0;
    1834             : }
    1835             : 
    1836             : static GEN
    1837         371 : lfunser(GEN L)
    1838             : {
    1839         371 :   long v = valser(L);
    1840         371 :   if (v > 0) return gen_0;
    1841         329 :   if (v == 0) L = gel(L, 2);
    1842             :   else
    1843         203 :     setlg(L, minss(lg(L), 2-v));
    1844         329 :   return L;
    1845             : }
    1846             : 
    1847             : static GEN
    1848         371 : lfunservec(GEN x)
    1849             : {
    1850         371 :   if (typ(x)==t_SER) return lfunser(x);
    1851           0 :   pari_APPLY_same(lfunser(gel(x,i)))
    1852             : }
    1853             : static GEN
    1854         105 : lfununext(GEN L)
    1855             : {
    1856         105 :   setlg(L, maxss(lg(L)-1, valser(L)? 2: 3));
    1857         105 :   return normalizeser(L);
    1858             : }
    1859             : static GEN
    1860         105 : lfununextvec(GEN x)
    1861             : {
    1862         105 :   if (typ(x)==t_SER) return lfununext(x);
    1863           0 :   pari_APPLY_same(lfununext(gel(x,i)));
    1864             : }
    1865             : 
    1866             : /* assume lmisc is an linit, s went through get_domain and s/bitprec belong
    1867             :  * to domain */
    1868             : static GEN
    1869        5526 : lfun_OK(GEN linit, GEN s, GEN sdom, long bitprec)
    1870             : {
    1871        5526 :   GEN N, gas, S, FVga, res, ss = s;
    1872        5526 :   long prec = nbits2prec(bitprec), ext;
    1873             : 
    1874        5526 :   FVga = lfun_get_factgammavec(linit_get_tech(linit));
    1875        5526 :   S = lfunlambda_OK(linit, s, sdom, bitprec);
    1876        5526 :   if (is_ser(S))
    1877             :   {
    1878        1641 :     GEN r = typ(S)==t_SER ? S : gel(S,1);
    1879        1641 :     long d = lg(r) - 2 + fracgammadegree(FVga);
    1880        1641 :     if (typ(s) == t_SER)
    1881        1312 :       ss = sertoser(s, d);
    1882             :     else
    1883         329 :       ss = deg1ser_shallow(gen_1, s, varn(r), d);
    1884             :   }
    1885        5526 :   gas = gammafactproduct(FVga, ss, &ext, prec);
    1886        5526 :   N = ldata_get_conductor(linit_get_ldata(linit));
    1887        5526 :   res = gdiv(S, gmul(gpow(N, gdivgu(ss, 2), prec), gas));
    1888        5526 :   if (typ(s) != t_SER && is_ser(res)) res = lfunservec(res);
    1889        5155 :   else if (ext) res = lfununextvec(res);
    1890        5526 :   return gprec_w(res, prec);
    1891             : }
    1892             : 
    1893             : GEN
    1894        8737 : lfun(GEN lmisc, GEN s, long bitprec)
    1895             : {
    1896        8737 :   pari_sp av = avma;
    1897        8737 :   GEN linit = NULL, ldata, dom, z;
    1898             :   long der;
    1899        8737 :   s = get_domain(s, &dom, &der);
    1900        8737 :   if (der && typ(s) != t_SER)
    1901             :   {
    1902           0 :     if (lfunspec_OK(lmisc, s, &ldata))
    1903             :     {
    1904           0 :       linit = lfuninit(lmisc, cgetg(1,t_VEC), 0, bitprec);
    1905           0 :       return derivnumk((void*)linit, (GEN(*)(void*,GEN,long))&lfun,
    1906             :                        s, stoi(der), nbits2prec(bitprec));
    1907             :     }
    1908             :   }
    1909             :   else
    1910             :   {
    1911        8737 :     long t = lfunspec_OK(lmisc, s, &ldata);
    1912        8730 :     if (t == 1)
    1913             :     { /* special value ? */
    1914        3339 :       long D = itos_or_0(gel(ldata_get_an(ldata), 2)), ss = itos(s);
    1915        3339 :       if (D)
    1916             :       {
    1917        3339 :         if (ss <= 0) return lfunquadneg(D, ss);
    1918             :         /* ss > 0 */
    1919         756 :         if ((!odd(ss) && D > 0) || (odd(ss) && D < 0))
    1920             :         {
    1921         658 :           long prec = nbits2prec(bitprec), q = labs(D);
    1922         658 :           ss = 1 - ss; /* <= 0 */
    1923         658 :           z = powrs(divrs(mppi(prec + EXTRAPREC64), q), 1-ss);
    1924         658 :           z = mulrr(shiftr(z, -ss), sqrtr_abs(utor(q, prec)));
    1925         658 :           z = gdiv(z, mpfactr(-ss, prec));
    1926         658 :           if (smodss(ss, 4) > 1) togglesign(z);
    1927         658 :           return gmul(z, lfunquadneg(D, ss));
    1928             :         }
    1929             :       }
    1930             :     }
    1931        5489 :     if (is_linit(lmisc)) linit = lmisc; else lmisc = ldata;
    1932        5489 :     if (t == 2)
    1933        1074 :       return gerepilecopy(av, linit? _product(&lfun, linit, s, bitprec)
    1934         183 :                                    : lfunlarge(ldata, s, bitprec));
    1935             :   }
    1936        4574 :   linit = lfuninit(lmisc, dom, der, bitprec);
    1937        4560 :   z = lfun_OK(linit, s, dom, bitprec);
    1938        4560 :   return gerepilecopy(av, z);
    1939             : }
    1940             : 
    1941             : /* given a t_SER a+x*s(x), return x*s(x), shallow */
    1942             : static GEN
    1943          42 : sersplit1(GEN s, GEN *head)
    1944             : {
    1945          42 :   long i, l = lg(s);
    1946             :   GEN y;
    1947          42 :   *head = simplify_shallow(mysercoeff(s, 0));
    1948          42 :   if (valser(s) > 0) return s;
    1949          28 :   y = cgetg(l-1, t_SER); y[1] = s[1];
    1950          28 :   setvalser(y, 1);
    1951         140 :   for (i=3; i < l; i++) gel(y,i-1) = gel(s,i);
    1952          28 :   return normalizeser(y);
    1953             : }
    1954             : 
    1955             : /* order of pole of Lambda at s (0 if regular point) */
    1956             : static long
    1957        2198 : lfunlambdaord(GEN linit, GEN s)
    1958             : {
    1959        2198 :   GEN tech = linit_get_tech(linit);
    1960        2198 :   if (linit_get_type(linit)==t_LDESC_PRODUCT)
    1961             :   {
    1962         280 :     GEN v = lfunprod_get_fact(linit_get_tech(linit));
    1963         280 :     GEN F = gel(v, 1), E = gel(v, 2), C = gel(v, 3);
    1964         280 :     long i, ex = 0, l = lg(F);
    1965         952 :     for (i = 1; i < l; i++)
    1966         672 :       ex += lfunlambdaord(gel(F,i), s) * (E[i]+C[i]);
    1967         280 :     return ex;
    1968             :   }
    1969        1918 :   if (ldata_get_residue(linit_get_ldata(linit)))
    1970             :   {
    1971         588 :     GEN r = lfunpoleresidue(lfun_get_Residue(tech), s);
    1972         588 :     if (r) return lg(r)-2;
    1973             :   }
    1974        1764 :   return 0;
    1975             : }
    1976             : 
    1977             : static GEN
    1978         126 : derser(GEN res, long m)
    1979             : {
    1980         126 :   long v = valser(res);
    1981         126 :   if (v > m) return gen_0;
    1982         126 :   if (v >= 0)
    1983         126 :     return gmul(mysercoeff(res, m), mpfact(m));
    1984             :   else
    1985           0 :     return derivn(res, m, -1);
    1986             : }
    1987             : 
    1988             : static GEN
    1989         189 : derservec(GEN x, long m) { pari_APPLY_same(derser(gel(x,i),m)) }
    1990             : 
    1991             : /* derivative of order m > 0 of L (flag = 0) or Lambda (flag = 1) */
    1992             : static GEN
    1993        1617 : lfunderiv(GEN lmisc, long m, GEN s, long flag, long bitprec)
    1994             : {
    1995        1617 :   pari_sp ltop = avma;
    1996        1617 :   GEN res, S = NULL, linit, ldata, dom;
    1997        1617 :   long der, prec = nbits2prec(bitprec);
    1998        1617 :   if (m <= 0) pari_err_DOMAIN("lfun", "D", "<=", gen_0, stoi(m));
    1999        1610 :   s = get_domain(s, &dom, &der);
    2000        1610 :   if (typ(s) != t_SER && lfunspec_OK(lmisc, s, &ldata) == 2)
    2001             :   {
    2002          28 :     linit = lfuninit(lmisc, cgetg(1,t_VEC), 0, bitprec);
    2003          28 :     return derivnumk((void*)linit, (GEN(*)(void*,GEN,long))&lfun,
    2004             :                      s, stoi(der + m), prec);
    2005             :   }
    2006        1582 :   linit = lfuninit(lmisc, dom, der + m, bitprec);
    2007        1582 :   if (lg(lfun_get_dom(linit_get_tech(linit))) == 1)
    2008          14 :     pari_err_IMPL("domain = [] for derivatives in lfuninit");
    2009        1568 :   if (typ(s) == t_SER)
    2010             :   {
    2011             :     GEN a;
    2012          42 :     if (valser(s) < 0) pari_err_DOMAIN("lfun","valuation", "<", gen_0, s);
    2013          42 :     S = sersplit1(s, &a);
    2014          42 :     s = deg1ser_shallow(gen_1, a, varn(S), m + ceildivuu(lg(s)-2, valser(S)));
    2015             :   }
    2016             :   else
    2017             :   {
    2018        1526 :     long ex = lfunlambdaord(linit, s);
    2019             :     /* HACK: pretend lfuninit was done to right accuracy */
    2020        1526 :     if (gequal0(s)) s = gen_0;
    2021        1526 :     s = deg1ser_shallow(gen_1, s, 0, m+1+ex);
    2022             :   }
    2023        1568 :   res = flag ? lfunlambda_OK(linit, s, dom, bitprec):
    2024         966 :                lfun_OK(linit, s, dom, bitprec);
    2025        1568 :   if (S)
    2026          42 :     res = gsubst(derivn(res, m, -1), varn(S), S);
    2027        1526 :   else if (typ(res)==t_SER)
    2028             :   {
    2029        1463 :     long v = valser(res);
    2030        1463 :     if (v > m) { set_avma(ltop); return gen_0; }
    2031        1456 :     if (v >= 0)
    2032        1330 :       res = gmul(mysercoeff(res, m), mpfact(m));
    2033             :     else
    2034         126 :       res = derivn(res, m, -1);
    2035             :   }
    2036          63 :   else if (is_ser(res))
    2037          63 :     res = derservec(res, m);
    2038        1561 :   return gerepilecopy(ltop, gprec_w(res, prec));
    2039             : }
    2040             : 
    2041             : GEN
    2042        1512 : lfunlambda0(GEN lmisc, GEN s, long der, long bitprec)
    2043             : {
    2044         623 :   return der? lfunderiv(lmisc, der, s, 1, bitprec)
    2045        2128 :             : lfunlambda(lmisc, s, bitprec);
    2046             : }
    2047             : 
    2048             : GEN
    2049        6811 : lfun0(GEN lmisc, GEN s, long der, long bitprec)
    2050             : {
    2051         994 :   return der? lfunderiv(lmisc, der, s, 0, bitprec)
    2052        7791 :             : lfun(lmisc, s, bitprec);
    2053             : }
    2054             : 
    2055             : GEN
    2056       19135 : lfunhardy(GEN lmisc, GEN t, long bitprec)
    2057             : {
    2058       19135 :   pari_sp ltop = avma;
    2059       19135 :   long prec = nbits2prec(bitprec), d, isbig = 0;
    2060             :   GEN linit, h, ldata, tech, w2, k2, E, a, argz, z;
    2061             : 
    2062       19135 :   switch(typ(t))
    2063             :   {
    2064       19128 :     case t_INT: case t_FRAC: case t_REAL: break;
    2065           7 :     default: pari_err_TYPE("lfunhardy",t);
    2066             :   }
    2067       19128 :   if (lfunspec_OK(lmisc, mkcomplex(gen_0, t), &ldata) == 2)
    2068             :   {
    2069         649 :     long B = bitprec + maxss(gexpo(t), 0);
    2070         649 :     GEN L = NULL;
    2071         649 :     isbig = 1;
    2072         649 :     k2 = ghalf;
    2073         649 :     z = mkcomplex(k2, t);
    2074         649 :     if (is_linit(lmisc))
    2075             :     {
    2076         568 :       linit = lmisc;
    2077         568 :       if (linit_get_type(linit) == t_LDESC_PRODUCT)
    2078          14 :         L = mkvec(linit);/*HACK*/
    2079             :     }
    2080             :     else
    2081             :     {
    2082          81 :       linit = lfunnoinit(ldata, B);
    2083          81 :       ldata = linit_get_ldata(linit); /* make sure eno is included */
    2084             :     }
    2085         649 :     h = lfunloglambdalarge(L? L: ldata, gprec_w(z, nbits2prec(B)), B);
    2086         637 :     tech = linit_get_tech(linit);
    2087             :   }
    2088             :   else
    2089             :   {
    2090       18479 :     GEN k = ldata_get_k(ldata);
    2091       18479 :     GEN dom = mkvec3(gmul2n(k, -1), gen_0, gabs(t,LOWDEFAULTPREC));
    2092       18479 :     if (!is_linit(lmisc)) lmisc = ldata;
    2093       18479 :     linit = lfuninit(lmisc, dom, 0, bitprec);
    2094       18479 :     tech = linit_get_tech(linit);
    2095       18479 :     k2 = lfun_get_k2(tech);
    2096       18479 :     z = mkcomplex(k2, t);
    2097       18479 :     h = lfunlambda_OK(linit, z, dom, bitprec);
    2098             :   }
    2099       19116 :   w2 = lfun_get_w2(tech);
    2100       19116 :   E = lfun_get_expot(tech); /* 4E = d(k2 - 1) + real(vecsum(Vga)) */
    2101       19116 :   d = ldata_get_degree(ldata);
    2102             :   /* more accurate than garg: k/2 in Q */
    2103       19116 :   argz = gequal0(k2)? Pi2n(-1, prec): gatan(gdiv(t, k2), prec);
    2104       19116 :   prec = precision(argz);
    2105             :   /* prec may have increased: don't lose accuracy if |z|^2 is exact */
    2106       19116 :   a = gsub(gmulsg(d, gmul(t, gmul2n(argz,-1))),
    2107             :            gmul(E, glog(gnorm(z),prec)));
    2108       19116 :   if (!isint1(w2) && typ(ldata_get_dual(ldata))==t_INT)
    2109       16202 :     h = isbig ? gadd(h, glog(w2, prec)) : mulrealvec(h, w2);
    2110       19116 :   if (typ(h) == t_COMPLEX && gexpo(imag_i(h)) < -(bitprec >> 1))
    2111        2533 :     h = real_i(h);
    2112       19116 :   if (isbig) h = greal(gexp(gadd(h, a), prec));
    2113       18479 :   else h = gmul(h, gexp(a, prec));
    2114       19116 :   return gerepileupto(ltop, h);
    2115             : }
    2116             : 
    2117             : /* L = log(t); return  \sum_{i = 0}^{v-1}  R[-i-1] L^i/i! */
    2118             : static GEN
    2119        1918 : theta_pole_contrib(GEN R, long v, GEN L)
    2120             : {
    2121        1918 :   GEN s = mysercoeff(R,-v);
    2122             :   long i;
    2123        2023 :   for (i = v-1; i >= 1; i--)
    2124         105 :     s = gadd(mysercoeff(R,-i), gdivgu(gmul(s,L), i));
    2125        1918 :   return s;
    2126             : }
    2127             : /* subtract successively rather than adding everything then subtracting.
    2128             :  * The polar part is "large" and suffers from cancellation: a little stabler
    2129             :  * this way */
    2130             : static GEN
    2131        4389 : theta_add_polar_part(GEN S, GEN R, GEN t, long prec)
    2132             : {
    2133        4389 :   GEN logt = NULL;
    2134        4389 :   long j, l = lg(R);
    2135        6307 :   for (j = 1; j < l; j++)
    2136             :   {
    2137        1918 :     GEN Rj = gel(R,j), b = gel(Rj,1), Rb = gel(Rj,2);
    2138        1918 :     long v = -valser(Rb);
    2139        1918 :     if (v > 1 && !logt) logt = glog(t, prec);
    2140        1918 :     S = gsub(S, gmul(theta_pole_contrib(Rb,v,logt), gpow(t,b,prec)));
    2141             :   }
    2142        4389 :   return S;
    2143             : }
    2144             : 
    2145             : static long
    2146        3598 : lfuncheckfeq_i(GEN theta, GEN thetad, GEN t0, GEN t0i, long bitprec)
    2147             : {
    2148        3598 :   GEN ldata = linit_get_ldata(theta);
    2149             :   GEN S0, S0i, w, eno;
    2150        3598 :   long prec = nbits2prec(bitprec);
    2151        3598 :   if (thetad)
    2152          70 :     S0 = lfuntheta(thetad, t0, 0, bitprec);
    2153             :   else
    2154        3528 :     S0 = conj_i(lfuntheta(theta, conj_i(t0), 0, bitprec));
    2155        3598 :   S0i = lfuntheta(theta, t0i, 0, bitprec);
    2156             : 
    2157        3598 :   eno = ldata_get_rootno(ldata);
    2158        3598 :   if (ldata_get_residue(ldata))
    2159             :   {
    2160         959 :     GEN R = theta_get_R(linit_get_tech(theta));
    2161         959 :     if (gequal0(R))
    2162             :     {
    2163             :       GEN v, r;
    2164         105 :       long t = ldata_get_type(ldata);
    2165         105 :       if (t == t_LFUN_NF || t == t_LFUN_ABELREL)
    2166             :       { /* inefficient since theta not needed; no need to optimize for this
    2167             :            (artificial) query [e.g. lfuncheckfeq(t_POL)] */
    2168          42 :         GEN L = lfuninit(ldata,zerovec(3),0,bitprec);
    2169          42 :         return lfuncheckfeq(L,t0,bitprec);
    2170             :       }
    2171          63 :       v = lfunrootres(theta, bitprec);
    2172          63 :       r = gel(v,1);
    2173          63 :       if (gequal0(eno)) eno = gel(v,3);
    2174          63 :       R = lfunrtoR_i(ldata, r, eno, nbits2prec(bitprec));
    2175             :     }
    2176         917 :     S0i = theta_add_polar_part(S0i, R, t0, prec);
    2177             :   }
    2178        3556 :   if (gequal0(S0i) || gequal0(S0)) pari_err_PREC("lfuncheckfeq");
    2179             : 
    2180        3556 :   w = gdivvec(S0i, gmul(S0, gpow(t0, ldata_get_k(ldata), prec)));
    2181             :   /* missing rootno: guess it */
    2182        3556 :   if (gequal0(eno)) eno = lfunrootno(theta, bitprec);
    2183        3556 :   w = gsubvec(w, eno);
    2184        3556 :   if (thetad) w = gdivvec(w, eno); /* |eno| may be large in non-dual case */
    2185        3556 :   return gexpo(w);
    2186             : }
    2187             : 
    2188             : /* Check whether the coefficients, conductor, weight, polar part and root
    2189             :  * number are compatible with the functional equation at t0 and 1/t0.
    2190             :  * Different from lfunrootres. */
    2191             : long
    2192        3731 : lfuncheckfeq(GEN lmisc, GEN t0, long bitprec)
    2193             : {
    2194             :   GEN ldata, theta, thetad, t0i;
    2195             :   pari_sp av;
    2196             : 
    2197        3731 :   if (is_linit(lmisc) && linit_get_type(lmisc)==t_LDESC_PRODUCT)
    2198             :   {
    2199         168 :     GEN v = lfunprod_get_fact(linit_get_tech(lmisc)), F = gel(v,1);
    2200         168 :     long i, b = -bitprec, l = lg(F);
    2201         560 :     for (i = 1; i < l; i++) b = maxss(b, lfuncheckfeq(gel(F,i), t0, bitprec));
    2202         168 :     return b;
    2203             :   }
    2204        3563 :   av = avma;
    2205        3563 :   if (!t0)
    2206             :   { /* ~Pi/3 + I/7, some random complex number */
    2207        3388 :     t0 = mkcomplex(uutoQ(355,339), uutoQ(1,7));
    2208        3388 :     t0i = ginv(t0);
    2209             :   }
    2210         175 :   else if (gcmpgs(gnorm(t0), 1) < 0) { t0i = t0; t0 = ginv(t0); }
    2211         119 :   else t0i = ginv(t0);
    2212             :   /* |t0| >= 1 */
    2213        3563 :   theta = lfunthetacheckinit(lmisc, t0i, 0, bitprec);
    2214        3556 :   ldata = linit_get_ldata(theta);
    2215        3556 :   thetad = theta_dual(theta, ldata_get_dual(ldata));
    2216        3556 :   return gc_long(av, lfuncheckfeq_i(theta, thetad, t0, t0i, bitprec));
    2217             : }
    2218             : 
    2219             : /*******************************************************************/
    2220             : /*       Compute root number and residues                          */
    2221             : /*******************************************************************/
    2222             : /* round root number to \pm 1 if close to integer. */
    2223             : static GEN
    2224        3836 : ropm1(GEN w, long prec)
    2225             : {
    2226             :   long e;
    2227             :   GEN r;
    2228        3836 :   if (typ(w) == t_INT) return w;
    2229        3430 :   r = grndtoi(w, &e);
    2230        3430 :   return (e < -prec/2)? r: w;
    2231             : }
    2232             : 
    2233             : /* theta for t=1/sqrt(2) and t2==2t simultaneously, saving 25% of the work.
    2234             :  * Assume correct initialization (no thetacheck) */
    2235             : static void
    2236         420 : lfunthetaspec(GEN linit, long bitprec, GEN *pv, GEN *pv2)
    2237             : {
    2238         420 :   pari_sp av = avma, av2;
    2239             :   GEN t, Vga, an, K, ldata, thetainit, v, v2, vroots;
    2240             :   long L, prec, n, d;
    2241             : 
    2242         420 :   ldata = linit_get_ldata(linit);
    2243         420 :   thetainit = linit_get_tech(linit);
    2244         420 :   prec = nbits2prec(bitprec);
    2245         420 :   Vga = ldata_get_gammavec(ldata); d = lg(Vga)-1;
    2246         420 :   if (Vgaeasytheta(Vga))
    2247             :   {
    2248         210 :     GEN v2 = sqrtr(real2n(1, nbits2prec(bitprec)));
    2249         210 :     GEN v = shiftr(v2,-1);
    2250         210 :     *pv = lfuntheta(linit, v,  0, bitprec);
    2251         210 :     *pv2= lfuntheta(linit, v2, 0, bitprec);
    2252         210 :     return;
    2253             :   }
    2254         210 :   an = RgV_kill0( theta_get_an(thetainit) );
    2255         210 :   L = lg(an)-1;
    2256             :   /* to compute theta(1/sqrt(2)) */
    2257         210 :   t = ginv(gsqrt(gmul2n(ldata_get_conductor(ldata), 1), prec));
    2258             :   /* t = 1/sqrt(2N) */
    2259             : 
    2260             :   /* From then on, the code is generic and could be used to compute
    2261             :    * theta(t) / theta(2t) without assuming t = 1/sqrt(2) */
    2262         210 :   K = theta_get_K(thetainit);
    2263         210 :   vroots = mkvroots(d, L, prec);
    2264         210 :   t = gpow(t, gdivgu(gen_2, d), prec); /* rt variant: t->t^(2/d) */
    2265             :   /* v = \sum_{n <= L, n odd} a_n K(nt) */
    2266     1815212 :   for (v = gen_0, n = 1; n <= L; n+=2)
    2267             :   {
    2268     1815002 :     GEN tn, Kn, a = gel(an, n);
    2269             : 
    2270     1815002 :     if (!a) continue;
    2271      113729 :     av2 = avma;
    2272      113729 :     tn = gmul(t, gel(vroots,n));
    2273      113729 :     Kn = gammamellininvrt(K, tn, bitprec);
    2274      113729 :     v = gerepileupto(av2, gadd(v, gmul(a,Kn)));
    2275             :   }
    2276             :   /* v += \sum_{n <= L, n even} a_n K(nt), v2 = \sum_{n <= L/2} a_n K(2n t) */
    2277     1815114 :   for (v2 = gen_0, n = 1; n <= L/2; n++)
    2278             :   {
    2279     1814904 :     GEN t2n, K2n, a = gel(an, n), a2 = gel(an,2*n);
    2280             : 
    2281     1814904 :     if (!a && !a2) continue;
    2282      120484 :     av2 = avma;
    2283      120484 :     t2n = gmul(t, gel(vroots,2*n));
    2284      120484 :     K2n = gerepileupto(av2, gammamellininvrt(K, t2n, bitprec));
    2285      120484 :     if (a) v2 = gadd(v2, gmul(a, K2n));
    2286      120484 :     if (a2) v = gadd(v,  gmul(a2,K2n));
    2287             :   }
    2288         210 :   *pv = v;
    2289         210 :   *pv2 = v2;
    2290         210 :   gerepileall(av, 2, pv,pv2);
    2291             : }
    2292             : 
    2293             : static GEN
    2294         413 : Rtor(GEN a, GEN R, GEN ldata, long prec)
    2295             : {
    2296         413 :   GEN FVga = gammafactor(ldata_get_gammavec(ldata));
    2297         413 :   GEN Na = gpow(ldata_get_conductor(ldata), gdivgu(a,2), prec);
    2298             :   long ext;
    2299         413 :   return gdiv(R, gmul(Na, gammafactproduct(FVga, a, &ext, prec)));
    2300             : }
    2301             : 
    2302             : /* v = theta~(t), vi = theta(1/t) */
    2303             : static GEN
    2304        3472 : get_eno(GEN R, GEN k, GEN t, GEN v, GEN vi, long vx, long bitprec, long force)
    2305             : {
    2306        3472 :   long prec = nbits2prec(bitprec);
    2307        3472 :   GEN a0, a1, S = deg1pol(gmul(gpow(t,k,prec), gneg(v)), vi, vx);
    2308             : 
    2309        3472 :   S = theta_add_polar_part(S, R, t, prec);
    2310        3472 :   if (typ(S) != t_POL || degpol(S) != 1) return NULL;
    2311        3472 :   a1 = gel(S,3); if (!force && gexpo(a1) < -bitprec/4) return NULL;
    2312        3423 :   a0 = gel(S,2);
    2313        3423 :   return gdivvec(a0, gneg(a1));
    2314             : 
    2315             : }
    2316             : /* Return w using theta(1/t) - w t^k \bar{theta}(t) = polar_part(t,w).
    2317             :  * The full Taylor development of L must be known */
    2318             : GEN
    2319        3423 : lfunrootno(GEN linit, long bitprec)
    2320             : {
    2321             :   GEN ldata, t, eno, v, vi, R, thetad;
    2322        3423 :   long c = 0, prec = nbits2prec(bitprec), vx = fetch_var();
    2323             :   GEN k;
    2324             :   pari_sp av;
    2325             : 
    2326             :   /* initialize for t > 1/sqrt(2) */
    2327        3423 :   linit = lfunthetacheckinit(linit, dbltor(sqrt(0.5)), 0, bitprec);
    2328        3423 :   ldata = linit_get_ldata(linit);
    2329        3423 :   k = ldata_get_k(ldata);
    2330        3437 :   R = ldata_get_residue(ldata)? lfunrtoR_eno(ldata, pol_x(vx), prec)
    2331        3423 :                               : cgetg(1, t_VEC);
    2332        3423 :   t = gen_1;
    2333        3423 :   v = lfuntheta(linit, t, 0, bitprec);
    2334        3423 :   thetad = theta_dual(linit, ldata_get_dual(ldata));
    2335        3423 :   vi = !thetad ? conj_i(v): lfuntheta(thetad, t, 0, bitprec);
    2336        3423 :   eno = get_eno(R,k,t,vi,v, vx, bitprec, 0);
    2337        3423 :   if (!eno && !thetad)
    2338             :   { /* t = sqrt(2), vi = theta(1/t), v = theta(t) */
    2339           7 :     lfunthetaspec(linit, bitprec, &vi, &v);
    2340           7 :     t = sqrtr(utor(2, prec));
    2341           7 :     eno = get_eno(R,k,t,conj_i(v),vi, vx, bitprec, 0);
    2342             :   }
    2343        3423 :   av = avma;
    2344        3465 :   while (!eno)
    2345             :   {
    2346          42 :     t = addsr(1, shiftr(utor(pari_rand(), prec), -2-BITS_IN_LONG));
    2347             :     /* t in [1,1.25[ */
    2348           0 :     v = thetad? lfuntheta(thetad, t, 0, bitprec)
    2349          42 :               : conj_i(lfuntheta(linit, t, 0, bitprec));
    2350          42 :     vi = lfuntheta(linit, ginv(t), 0, bitprec);
    2351          42 :     eno = get_eno(R,k,t,v,vi, vx, bitprec, c++ == 5);
    2352          42 :     set_avma(av);
    2353             :   }
    2354        3423 :   delete_var(); return ropm1(eno,prec);
    2355             : }
    2356             : 
    2357             : /* Find root number and/or residues when L-function coefficients and
    2358             :    conductor are known. For the moment at most a single residue allowed. */
    2359             : GEN
    2360        4200 : lfunrootres(GEN data, long bitprec)
    2361             : {
    2362        4200 :   pari_sp ltop = avma;
    2363             :   GEN k, w, r, R, a, b, e, v, v2, be, ldata, linit;
    2364             :   long prec;
    2365             : 
    2366        4200 :   ldata = lfunmisc_to_ldata_shallow(data);
    2367        4200 :   r = ldata_get_residue(ldata);
    2368        4200 :   k = ldata_get_k(ldata);
    2369        4200 :   w = ldata_get_rootno(ldata);
    2370        4200 :   if (r) r = normalize_simple_pole(r, k);
    2371        4200 :   if (!r || residues_known(r))
    2372             :   {
    2373        3787 :     if (isintzero(w)) w = lfunrootno(data, bitprec);
    2374        3787 :     if (!r)
    2375        1757 :       r = R = gen_0;
    2376             :     else
    2377        2030 :       R = lfunrtoR_eno(ldata, w, nbits2prec(bitprec));
    2378        3787 :     return gerepilecopy(ltop, mkvec3(r, R, w));
    2379             :   }
    2380         413 :   linit = lfunthetacheckinit(data, dbltor(sqrt(0.5)), 0, bitprec);
    2381         413 :   prec = nbits2prec(bitprec);
    2382         413 :   if (lg(r) > 2) pari_err_IMPL("multiple poles in lfunrootres");
    2383             :   /* Now residue unknown, and r = [[be,0]]. */
    2384         413 :   be = gmael(r, 1, 1);
    2385         413 :   if (ldata_isreal(ldata) && gequalm1(w))
    2386           0 :     R = lfuntheta(linit, gen_1, 0, bitprec);
    2387             :   else
    2388             :   {
    2389         413 :     GEN p2k = gpow(gen_2,k,prec);
    2390         413 :     lfunthetaspec(linit, bitprec, &v2, &v);
    2391         413 :     if (gequal(gmulsg(2, be), k)) pari_err_IMPL("pole at k/2 in lfunrootres");
    2392         413 :     if (gequal(be, k))
    2393             :     {
    2394         147 :       a = conj_i(gsub(gmul(p2k, v), v2));
    2395         147 :       b = subiu(p2k, 1);
    2396         147 :       e = gmul(gsqrt(p2k, prec), gsub(v2, v));
    2397             :     }
    2398             :     else
    2399             :     {
    2400         266 :       GEN tk2 = gsqrt(p2k, prec);
    2401         266 :       GEN tbe = gpow(gen_2, be, prec);
    2402         266 :       GEN tkbe = gpow(gen_2, gdivgu(gsub(k, be), 2), prec);
    2403         266 :       a = conj_i(gsub(gmul(tbe, v), v2));
    2404         266 :       b = gsub(gdiv(tbe, tkbe), tkbe);
    2405         266 :       e = gsub(gmul(gdiv(tbe, tk2), v2), gmul(tk2, v));
    2406             :     }
    2407         413 :     if (isintzero(w))
    2408             :     { /* Now residue unknown, r = [[be,0]], and w unknown. */
    2409           7 :       GEN t0  = mkfrac(utoi(11),utoi(10));
    2410           7 :       GEN th1 = lfuntheta(linit, t0,  0, bitprec);
    2411           7 :       GEN th2 = lfuntheta(linit, ginv(t0), 0, bitprec);
    2412           7 :       GEN tbe = gpow(t0, gmulsg(2, be), prec);
    2413           7 :       GEN tkbe = gpow(t0, gsub(k, be), prec);
    2414           7 :       GEN tk2 = gpow(t0, k, prec);
    2415           7 :       GEN c = conj_i(gsub(gmul(tbe, th1), th2));
    2416           7 :       GEN d = gsub(gdiv(tbe, tkbe), tkbe);
    2417           7 :       GEN f = gsub(gmul(gdiv(tbe, tk2), th2), gmul(tk2, th1));
    2418           7 :       GEN D = gsub(gmul(a, d), gmul(b, c));
    2419           7 :       w = gdiv(gsub(gmul(d, e), gmul(b, f)), D);
    2420             :     }
    2421         413 :     w = ropm1(w, prec);
    2422         413 :     R = gdiv(gsub(e, gmul(a, w)), b);
    2423             :   }
    2424         413 :   r = normalize_simple_pole(Rtor(be, R, ldata, prec), be);
    2425         413 :   R = lfunrtoR_i(ldata, r, w, prec);
    2426         413 :   return gerepilecopy(ltop, mkvec3(r, R, w));
    2427             : }
    2428             : 
    2429             : /*******************************************************************/
    2430             : /*                           Zeros                                 */
    2431             : /*******************************************************************/
    2432             : struct lhardyz_t {
    2433             :   long bitprec, prec;
    2434             :   GEN linit;
    2435             : };
    2436             : 
    2437             : static GEN
    2438       18396 : lfunhardyzeros(void *E, GEN t)
    2439             : {
    2440       18396 :   struct lhardyz_t *S = (struct lhardyz_t*)E;
    2441       18396 :   GEN z = gprec_wensure(lfunhardy(S->linit, t, S->bitprec), S->prec);
    2442       18393 :   return typ(z) == t_VEC ? RgV_prod(z): z;
    2443             : }
    2444             : 
    2445             : /* initialize for computation on critical line up to height h, zero
    2446             :  * of order <= m */
    2447             : static GEN
    2448         560 : lfuncenterinit(GEN lmisc, double h, long m, long bitprec)
    2449             : {
    2450         560 :   GEN ldata = lfunmisc_to_ldata_shallow(lmisc);
    2451         560 :   if (m < 0)
    2452             :   { /* choose a sensible default */
    2453         560 :     m = 4;
    2454         560 :     if (is_linit(lmisc) && linit_get_type(lmisc) == t_LDESC_INIT)
    2455             :     {
    2456         469 :       GEN domain = lfun_get_domain(linit_get_tech(lmisc));
    2457         469 :       m = domain_get_der(domain);
    2458             :     }
    2459             :   }
    2460         560 :   if (is_dirichlet(ldata)) m = 0;
    2461         560 :   return lfuninit(lmisc, mkvec(dbltor(h)), m, bitprec);
    2462             : }
    2463             : 
    2464             : long
    2465         553 : lfunorderzero(GEN lmisc, long m, long bitprec)
    2466             : {
    2467         553 :   pari_sp ltop = avma;
    2468             :   GEN eno, ldata, linit, k2;
    2469             :   long G, c0, c, st;
    2470             : 
    2471         553 :   if (is_linit(lmisc) && linit_get_type(lmisc) == t_LDESC_PRODUCT)
    2472             :   {
    2473          84 :     GEN M = gmael(linit_get_tech(lmisc), 2,1);
    2474          84 :     long i, l = lg(M);
    2475         280 :     for (c=0, i=1; i < l; i++) c += lfunorderzero(gel(M,i), m, bitprec);
    2476          84 :     return c;
    2477             :   }
    2478         469 :   linit = lfuncenterinit(lmisc, 0, m, bitprec);
    2479         469 :   ldata = linit_get_ldata(linit);
    2480         469 :   eno = ldata_get_rootno(ldata);
    2481         469 :   k2 = gmul2n(ldata_get_k(ldata), -1);
    2482         469 :   G = -bitprec/2;
    2483         469 :   c0 = 0; st = 1;
    2484         469 :   if (typ(eno) == t_VEC)
    2485             :   {
    2486          42 :     long i, l = lg(eno), cnt = l-1, s = 0;
    2487          42 :     GEN v = zero_zv(l-1);
    2488          42 :     if (ldata_isreal(ldata)) st = 2;
    2489          84 :     for (c = c0; cnt; c += st)
    2490             :     {
    2491          42 :       GEN L = lfun0(linit, k2, c, bitprec);
    2492         154 :       for (i = 1; i < l; i++)
    2493             :       {
    2494         112 :         if (v[i]==0 && gexpo(gel(L,i)) > G)
    2495             :         {
    2496         112 :           v[i] = c; cnt--; s += c;
    2497             :         }
    2498             :       }
    2499             :     }
    2500          42 :     return gc_long(ltop,s);
    2501             :   }
    2502             :   else
    2503             :   {
    2504         427 :     if (ldata_isreal(ldata)) { st = 2; if (!gequal1(eno)) c0 = 1; }
    2505         455 :     for (c = c0;; c += st)
    2506         455 :       if (gexpo(lfun0(linit, k2, c, bitprec)) > G) return gc_long(ltop, c);
    2507             :   }
    2508             : }
    2509             : 
    2510             : /* assume T1 * T2 > 0, T1 <= T2 */
    2511             : static void
    2512          98 : lfunzeros_i(struct lhardyz_t *S, GEN *pw, long *ct, GEN T1, GEN T2, long d,
    2513             :             GEN cN, GEN pi2, GEN pi2div, long precinit, long prec)
    2514             : {
    2515          98 :   GEN T = T1, w = *pw;
    2516          98 :   long W = lg(w)-1, s = gsigne(lfunhardyzeros(S, T1));
    2517             :   for(;;)
    2518         418 :   {
    2519         513 :     pari_sp av = avma;
    2520             :     GEN D, T0, z;
    2521         513 :     D = gcmp(T, pi2) < 0? cN
    2522         513 :                         : gadd(cN, gmulsg(d, glog(gdiv(T, pi2), prec)));
    2523         513 :     D = gdiv(pi2div, gmulsg(d, D));
    2524             :     for(;;)
    2525       13407 :     {
    2526             :       long s0;
    2527       13920 :       T0 = T; T = gadd(T, D);
    2528       13920 :       if (gcmp(T, T2) >= 0) T = T2;
    2529       13920 :       s0 = gsigne(lfunhardyzeros(S, T));
    2530       13920 :       if (s0 != s) { s = s0; break; }
    2531       13502 :       if (T == T2) { setlg(w, *ct); *pw = w; return; }
    2532             :     }
    2533         418 :     z = zbrent(S, lfunhardyzeros, T0, T, prec); /* T <= T2 */
    2534         418 :     gerepileall(av, 2, &T, &z);
    2535         418 :     if (*ct > W) { W *= 2; w = vec_lengthen(w, W); }
    2536         418 :     if (typ(z) == t_REAL) z  = rtor(z, precinit);
    2537         418 :     gel(w, (*ct)++) = z;
    2538             :   }
    2539             :   setlg(w, *ct); *pw = w;
    2540             : }
    2541             : GEN
    2542          98 : lfunzeros(GEN ldata, GEN lim, long divz, long bitprec)
    2543             : {
    2544          98 :   pari_sp ltop = avma;
    2545             :   GEN linit, pi2, pi2div, cN, w, T, h1, h2;
    2546          98 :   long i, d, NEWD, c, ct, s1, s2, prec, prec0 = nbits2prec(bitprec);
    2547             :   double maxt;
    2548             :   struct lhardyz_t S;
    2549             : 
    2550          98 :   if (is_linit(ldata) && linit_get_type(ldata) == t_LDESC_PRODUCT)
    2551             :   {
    2552           0 :     GEN M = gmael(linit_get_tech(ldata), 2,1);
    2553           0 :     long l = lg(M);
    2554           0 :     w = cgetg(l, t_VEC);
    2555           0 :     for (i = 1; i < l; i++) gel(w,i) = lfunzeros(gel(M,i), lim, divz, bitprec);
    2556           0 :     return gerepileupto(ltop, vecsort0(shallowconcat1(w), NULL, 0));
    2557             :   }
    2558          98 :   if (typ(lim) == t_VEC)
    2559             :   {
    2560             :     double H1, H2;
    2561          63 :     if (lg(lim) != 3 || gcmp(gel(lim, 1), gel(lim, 2)) >= 0)
    2562           7 :       pari_err_TYPE("lfunzeros",lim);
    2563          56 :     h1 = gel(lim, 1); H1 = gtodouble(h1);
    2564          56 :     h2 = gel(lim, 2); H2 = gtodouble(h2);
    2565          56 :     maxt = maxdd(fabs(H1), fabs(H2));
    2566          56 :     if (H1 * H2 > 0)
    2567             :     {
    2568          35 :       GEN LDATA = lfunmisc_to_ldata_shallow(ldata);
    2569          35 :       double m = mindd(fabs(H1), fabs(H2));
    2570          35 :       if (is_dirichlet(LDATA) && m > lfuninit_cutoff(LDATA)) maxt = 0;
    2571             :     }
    2572             :   }
    2573             :   else
    2574             :   {
    2575          35 :     if (gcmp(lim, gen_0) <= 0) pari_err_TYPE("lfunzeros",lim);
    2576          35 :     h1 = gen_0;
    2577          35 :     h2 = lim;
    2578          35 :     maxt = gtodouble(h2);
    2579             :   }
    2580          91 :   S.linit = linit = lfuncenterinit(ldata, maxt, -1, bitprec);
    2581          91 :   S.bitprec = bitprec;
    2582          91 :   S.prec = prec0;
    2583          91 :   ldata = linit_get_ldata(linit);
    2584          91 :   d = ldata_get_degree(ldata);
    2585             : 
    2586          91 :   NEWD = minss((long) ceil(bitprec + (M_PI/(4*M_LN2)) * d * maxt),
    2587             :                lfun_get_bitprec(linit_get_tech(linit)));
    2588          91 :   prec = nbits2prec(NEWD);
    2589          91 :   cN = gdiv(ldata_get_conductor(ldata), gpowgs(Pi2n(-1, prec), d));
    2590          91 :   cN = gexpo(cN) >= 0? gaddsg(d, gmulsg(2, glog(cN, prec))): utoi(d);
    2591          91 :   pi2 = Pi2n(1, prec);
    2592          91 :   pi2div = gdivgu(pi2, labs(divz));
    2593          91 :   s1 = gsigne(h1);
    2594          91 :   s2 = gsigne(h2);
    2595          91 :   w = cgetg(100+1, t_VEC); c = 1; ct = 0; T = NULL;
    2596          91 :   if (s1 <= 0 && s2 >= 0)
    2597             :   {
    2598          56 :     GEN r = ldata_get_residue(ldata);
    2599          56 :     if (!r || gequal0(r))
    2600             :     {
    2601          35 :       ct = lfunorderzero(linit, -1, bitprec);
    2602          35 :       if (ct) T = real2n(-prec / (2*ct), prec);
    2603             :     }
    2604             :   }
    2605          91 :   if (s1 <= 0)
    2606             :   {
    2607          63 :     if (s1 < 0)
    2608          21 :       lfunzeros_i(&S, &w, &c, h1, T? negr(T): h2,
    2609             :                   d, cN, pi2, pi2div, prec0, prec);
    2610          63 :     if (ct)
    2611             :     {
    2612          21 :       long n = lg(w)-1;
    2613          21 :       if (c + ct >= n) w = vec_lengthen(w, n + ct);
    2614          84 :       for (i = 1; i <= ct; i++) gel(w,c++) = gen_0;
    2615             :     }
    2616             :   }
    2617          91 :   if (s2 > 0 && (T || s1 >= 0))
    2618          77 :     lfunzeros_i(&S, &w, &c, T? T: h1, h2, d, cN, pi2, pi2div, prec0, prec);
    2619          88 :   return gerepilecopy(ltop, w);
    2620             : }
    2621             : 
    2622             : /*******************************************************************/
    2623             : /*       Guess conductor                                           */
    2624             : /*******************************************************************/
    2625             : struct huntcond_t {
    2626             :   GEN k;
    2627             :   GEN theta, thetad;
    2628             :   GEN *pM, *psqrtM, *pMd, *psqrtMd;
    2629             : };
    2630             : 
    2631             : static void
    2632       11962 : condset(struct huntcond_t *S, GEN M, long prec)
    2633             : {
    2634       11962 :   *(S->pM) = M;
    2635       11962 :   *(S->psqrtM) = gsqrt(ginv(M), prec);
    2636       11962 :   if (S->thetad != S->theta)
    2637             :   {
    2638           0 :     *(S->pMd) = *(S->pM);
    2639           0 :     *(S->psqrtMd) = *(S->psqrtM);
    2640             :   }
    2641       11962 : }
    2642             : 
    2643             : /* M should eventually converge to N, the conductor. L has no pole. */
    2644             : static GEN
    2645        6888 : wrap1(void *E, GEN M)
    2646             : {
    2647        6888 :   struct huntcond_t *S = (struct huntcond_t*)E;
    2648             :   GEN thetainit, tk, p1, p1inv;
    2649        6888 :   GEN t = mkfrac(stoi(11), stoi(10));
    2650             :   long prec, bitprec;
    2651             : 
    2652        6888 :   thetainit = linit_get_tech(S->theta);
    2653        6888 :   bitprec = theta_get_bitprec(thetainit);
    2654        6888 :   prec = nbits2prec(bitprec);
    2655        6888 :   condset(S, M, prec);
    2656        6888 :   tk = gpow(t, S->k, prec);
    2657        6888 :   p1 = lfuntheta(S->thetad, t, 0, bitprec);
    2658        6888 :   p1inv = lfuntheta(S->theta, ginv(t), 0, bitprec);
    2659        6888 :   return glog(gabs(gmul(tk, gdiv(p1, p1inv)), prec), prec);
    2660             : }
    2661             : 
    2662             : /* M should eventually converge to N, the conductor. L has a pole. */
    2663             : static GEN
    2664        5032 : wrap2(void *E, GEN M)
    2665             : {
    2666        5032 :   struct huntcond_t *S = (struct huntcond_t*)E;
    2667             :   GEN t1k, t2k, p1, p1inv, p2, p2inv, thetainit, R;
    2668        5032 :   GEN t1 = mkfrac(stoi(11), stoi(10)), t2 = mkfrac(stoi(13), stoi(11));
    2669             :   GEN t1be, t2be, t1bemk, t2bemk, t1kmbe, t2kmbe;
    2670             :   GEN F11, F12, F21, F22, P1, P2, res;
    2671             :   long prec, bitprec;
    2672        5032 :   GEN k = S->k;
    2673             : 
    2674        5032 :   thetainit = linit_get_tech(S->theta);
    2675        5032 :   bitprec = theta_get_bitprec(thetainit);
    2676        5032 :   prec = nbits2prec(bitprec);
    2677        5032 :   condset(S, M, prec);
    2678             : 
    2679        5032 :   p1 = lfuntheta(S->thetad, t1, 0, bitprec);
    2680        5032 :   p2 = lfuntheta(S->thetad, t2, 0, bitprec);
    2681        5032 :   p1inv = lfuntheta(S->theta, ginv(t1), 0, bitprec);
    2682        5032 :   p2inv = lfuntheta(S->theta, ginv(t2), 0, bitprec);
    2683        5032 :   t1k = gpow(t1, k, prec);
    2684        5032 :   t2k = gpow(t2, k, prec);
    2685        5032 :   R = theta_get_R(thetainit);
    2686        5032 :   if (typ(R) == t_VEC)
    2687             :   {
    2688           0 :     GEN be = gmael(R, 1, 1);
    2689           0 :     t1be = gpow(t1, be, prec); t1bemk = gdiv(gsqr(t1be), t1k);
    2690           0 :     t2be = gpow(t2, be, prec); t2bemk = gdiv(gsqr(t2be), t2k);
    2691           0 :     t1kmbe = gdiv(t1k, t1be);
    2692           0 :     t2kmbe = gdiv(t2k, t2be);
    2693             :   }
    2694             :   else
    2695             :   { /* be = k */
    2696        5032 :     t1be = t1k; t1bemk = t1k; t1kmbe = gen_1;
    2697        5032 :     t2be = t2k; t2bemk = t2k; t2kmbe = gen_1;
    2698             :   }
    2699        5032 :   F11 = conj_i(gsub(gmul(gsqr(t1be), p1), p1inv));
    2700        5032 :   F12 = conj_i(gsub(gmul(gsqr(t2be), p2), p2inv));
    2701        5032 :   F21 = gsub(gmul(t1k, p1), gmul(t1bemk, p1inv));
    2702        5032 :   F22 = gsub(gmul(t2k, p2), gmul(t2bemk, p2inv));
    2703        5032 :   P1 = gsub(gmul(t1bemk, t1be), t1kmbe);
    2704        5032 :   P2 = gsub(gmul(t2bemk, t2be), t2kmbe);
    2705        5032 :   res = gdiv(gsub(gmul(P2,F21), gmul(P1,F22)),
    2706             :              gsub(gmul(P2,F11), gmul(P1,F12)));
    2707        5032 :   return glog(gabs(res, prec), prec);
    2708             : }
    2709             : 
    2710             : /* If flag = 0 (default) return all conductors found as integers. If
    2711             : flag = 1, return the approximations, not the integers. If flag = 2,
    2712             : return all, even nonintegers. */
    2713             : 
    2714             : static GEN
    2715          84 : checkconductor(GEN v, long bit, long flag)
    2716             : {
    2717             :   GEN w;
    2718          84 :   long e, j, k, l = lg(v);
    2719          84 :   if (flag == 2) return v;
    2720          84 :   w = cgetg(l, t_VEC);
    2721         322 :   for (j = k = 1; j < l; j++)
    2722             :   {
    2723         238 :     GEN N = grndtoi(gel(v,j), &e);
    2724         238 :     if (e < -bit) gel(w,k++) = flag ? gel(v,j): N;
    2725             :   }
    2726          84 :   if (k == 2) return gel(w,1);
    2727           7 :   setlg(w,k); return w;
    2728             : }
    2729             : 
    2730             : static GEN
    2731          98 : parse_maxcond(GEN maxN)
    2732             : {
    2733             :   GEN M;
    2734          98 :   if (!maxN)
    2735          49 :     M = utoipos(10000);
    2736          49 :   else if (typ(maxN) == t_VEC)
    2737             :   {
    2738          14 :     if (!RgV_is_ZV(maxN)) pari_err_TYPE("lfunconductor",maxN);
    2739          14 :     return ZV_sort_shallow(maxN);
    2740             :   }
    2741             :   else
    2742          35 :     M = maxN;
    2743          84 :   return (typ(M) == t_INT)? addiu(M, 1): gceil(M);
    2744             : }
    2745             : 
    2746             : GEN
    2747          98 : lfunconductor(GEN data, GEN maxcond, long flag, long bitprec)
    2748             : {
    2749             :   struct huntcond_t S;
    2750          98 :   pari_sp av = avma;
    2751          98 :   GEN ldata = lfunmisc_to_ldata_shallow(data);
    2752          98 :   GEN ld, r, v, theta, thetad, M, tdom, t0 = NULL, t0i = NULL;
    2753             :   GEN (*eval)(void *, GEN);
    2754             :   long prec;
    2755          98 :   M = parse_maxcond(maxcond);
    2756          98 :   r = ldata_get_residue(ldata);
    2757          98 :   if (typ(M) == t_VEC) /* select in list */
    2758             :   {
    2759          14 :     if (lg(M) == 1) { set_avma(av); return cgetg(1,t_VEC); }
    2760           7 :     eval = NULL; tdom = dbltor(0.7);
    2761             :   }
    2762          84 :   else if (!r) { eval = wrap1; tdom = uutoQ(10,11); }
    2763             :   else
    2764             :   {
    2765          21 :     if (typ(r) == t_VEC && lg(r) > 2)
    2766           0 :       pari_err_IMPL("multiple poles in lfunconductor");
    2767          21 :     eval = wrap2; tdom = uutoQ(11,13);
    2768             :   }
    2769          91 :   if (eval) bitprec += bitprec/2;
    2770          91 :   prec = nbits2prec(bitprec);
    2771          91 :   ld = shallowcopy(ldata);
    2772          91 :   gel(ld, 5) = eval? M: veclast(M);
    2773          91 :   theta = lfunthetainit_i(ld, tdom, 0, bitprec);
    2774          91 :   thetad = theta_dual(theta, ldata_get_dual(ldata));
    2775          91 :   gel(theta,3) = shallowcopy(linit_get_tech(theta));
    2776          91 :   S.k = ldata_get_k(ldata);
    2777          91 :   S.theta = theta;
    2778          91 :   S.thetad = thetad? thetad: theta;
    2779          91 :   S.pM = &gel(linit_get_ldata(theta),5);
    2780          91 :   S.psqrtM = &gel(linit_get_tech(theta),7);
    2781          91 :   if (thetad)
    2782             :   {
    2783           0 :     S.pMd = &gel(linit_get_ldata(thetad),5);
    2784           0 :     S.psqrtMd = &gel(linit_get_tech(thetad),7);
    2785             :   }
    2786          91 :   if (!eval)
    2787             :   {
    2788           7 :     long i, besti = 0, beste = -10, l = lg(M);
    2789           7 :     t0 = uutoQ(11,10); t0i = uutoQ(10,11);
    2790          49 :     for (i = 1; i < l; i++)
    2791             :     {
    2792          42 :       pari_sp av2 = avma;
    2793             :       long e;
    2794          42 :       condset(&S, gel(M,i), prec);
    2795          42 :       e = lfuncheckfeq_i(theta, thetad, t0, t0i, bitprec);
    2796          42 :       set_avma(av2);
    2797          42 :       if (e < beste) { beste = e; besti = i; }
    2798          35 :       else if (e == beste) beste = besti = 0; /* tie: forget */
    2799             :     }
    2800           7 :     if (!besti) { set_avma(av); return cgetg(1,t_VEC); }
    2801           7 :     return gerepilecopy(av, mkvec2(gel(M,besti), stoi(beste)));
    2802             :   }
    2803          84 :   v = solvestep((void*)&S, eval, ghalf, M, gen_2, 14, prec);
    2804          84 :   return gerepilecopy(av, checkconductor(v, bitprec/2, flag));
    2805             : }
    2806             : 
    2807             : /* assume chi primitive */
    2808             : static GEN
    2809         560 : znchargauss_i(GEN G, GEN chi, long bitprec)
    2810             : {
    2811         560 :   GEN z, q, F = znstar_get_N(G);
    2812             :   long prec;
    2813             : 
    2814         560 :   if (equali1(F)) return gen_1;
    2815         560 :   prec = nbits2prec(bitprec);
    2816         560 :   q = sqrtr_abs(itor(F, prec));
    2817         560 :   z = lfuntheta(mkvec2(G,chi), gen_1, 0, bitprec);
    2818         560 :   if (gexpo(z) < 10 - bitprec)
    2819             :   {
    2820          28 :     if (equaliu(F,300))
    2821             :     {
    2822          14 :       GEN z = rootsof1u_cx(25, prec);
    2823          14 :       GEN n = znconreyexp(G, chi);
    2824          14 :       if (equaliu(n, 131)) return gmul(q, gpowgs(z,14));
    2825           7 :       if (equaliu(n, 71)) return gmul(q, gpowgs(z,11));
    2826             :     }
    2827          14 :     if (equaliu(F,600))
    2828             :     {
    2829          14 :       GEN z = rootsof1u_cx(25, prec);
    2830          14 :       GEN n = znconreyexp(G, chi);
    2831          14 :       if (equaliu(n, 491)) return gmul(q, gpowgs(z,7));
    2832           7 :       if (equaliu(n, 11)) return gmul(q, gpowgs(z,18));
    2833             :     }
    2834           0 :     pari_err_BUG("znchargauss [ Theta(chi,1) = 0 ]");
    2835             :   }
    2836         532 :   z = gmul(gdiv(z, conj_i(z)), q);
    2837         532 :   if (zncharisodd(G,chi)) z = mulcxI(z);
    2838         532 :   return z;
    2839             : }
    2840             : static GEN
    2841         560 : Z_radical(GEN N, long *om)
    2842             : {
    2843         560 :   GEN P = gel(Z_factor(N), 1);
    2844         560 :   *om = lg(P)-1; return ZV_prod(P);
    2845             : }
    2846             : GEN
    2847        1218 : znchargauss(GEN G, GEN chi, GEN a, long bitprec)
    2848             : {
    2849             :   GEN v, T, N, F, b0, b1, b2, bF, a1, aF, A, r, GF, tau, B, faB, u, S;
    2850        1218 :   long omb0, prec = nbits2prec(bitprec);
    2851        1218 :   pari_sp av = avma;
    2852             : 
    2853        1218 :   if (typ(chi) != t_COL) chi = znconreylog(G,chi);
    2854        1218 :   T = znchartoprimitive(G, chi);
    2855        1218 :   GF  = gel(T,1);
    2856        1218 :   chi = gel(T,2); /* now primitive */
    2857        1218 :   N = znstar_get_N(G);
    2858        1218 :   F = znstar_get_N(GF);
    2859        1218 :   if (equalii(N,F)) b1 = bF = gen_1;
    2860             :   else
    2861             :   {
    2862         245 :     v = Z_ppio(diviiexact(N,F), F);
    2863         245 :     bF = gel(v,2); /* (N/F, F^oo) */
    2864         245 :     b1 = gel(v,3); /* cofactor */
    2865             :   }
    2866        1218 :   if (!a) a = a1 = aF = gen_1;
    2867             :   else
    2868             :   {
    2869        1169 :     if (typ(a) != t_INT) pari_err_TYPE("znchargauss",a);
    2870        1169 :     a = modii(a, N);
    2871        1169 :     if (!signe(a)) { set_avma(av); return is_pm1(F)? eulerphi(N): gen_0; }
    2872         728 :     v = Z_ppio(a, F);
    2873         728 :     aF = gel(v,2);
    2874         728 :     a1 = gel(v,3);
    2875             :   }
    2876         777 :   if (!equalii(aF, bF)) { set_avma(av); return gen_0; }
    2877         560 :   b0 = Z_radical(b1, &omb0);
    2878         560 :   b2 = diviiexact(b1, b0);
    2879         560 :   A = dvmdii(a1, b2, &r);
    2880         560 :   if (r != gen_0) { set_avma(av); return gen_0; }
    2881         560 :   B = gcdii(A,b0); faB = Z_factor(B); /* squarefree */
    2882         560 :   S = eulerphi(mkvec2(B,faB));
    2883         560 :   if (odd(omb0 + lg(gel(faB,1))-1)) S = negi(S); /* moebius(b0/B) * phi(B) */
    2884         560 :   S = mulii(S, mulii(aF,b2));
    2885         560 :   tau = znchargauss_i(GF, chi, bitprec);
    2886         560 :   u = Fp_div(b0, A, F);
    2887         560 :   if (!equali1(u))
    2888             :   {
    2889           7 :     GEN ord = zncharorder(GF, chi), z = rootsof1_cx(ord, prec);
    2890           7 :     tau = gmul(tau, znchareval(GF, chi, u, mkvec2(z,ord)));
    2891             :   }
    2892         560 :   return gerepileupto(av, gmul(tau, S));
    2893             : }

Generated by: LCOV version 1.14