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 - lerch.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.16.2 lcov report (development 29395-ef22f77854) Lines: 340 353 96.3 %
Date: 2024-06-14 09:03:06 Functions: 21 21 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2022  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             : #include "pari.h"
      16             : #include "paripriv.h"
      17             : 
      18             : #define DEBUGLEVEL DEBUGLEVEL_trans
      19             : 
      20             : /********************************************************/
      21             : /*                   Hurwitz zeta function              */
      22             : /********************************************************/
      23             : struct hurwitzp_t { GEN B, _1, s1; };
      24             : static void
      25         126 : hurwitzp_init(struct hurwitzp_t *S, long prec, GEN s)
      26             : {
      27         126 :   GEN B, C = gen_1, s1 = gsubgs(s, 1), p = gel(s, 2);
      28         126 :   long j, J = ((equaliu(p,2)? (prec >> 1): prec) + 2) >> 1;
      29         126 :   if (gequal0(s1)) s1 = NULL;
      30         126 :   B = bernvec(J);
      31        1519 :   for (j = 1; j <= J; j++)
      32             :   {
      33        1393 :     GEN t = (j == 1 && !s1)? s: gmul(gaddgs(s, 2*j-3), gaddgs(s, 2*j-2));
      34        1393 :     C = gdivgunextu(gmul(C, t), 2*j-1);
      35        1393 :     gel(B, j+1) = gmul(gel(B, j+1), C); /* B_{2j} * binomial(1-s, 2j) */
      36             :   }
      37         126 :   S->_1 = cvtop(gen_1, p, prec);
      38         126 :   S->s1 = s1;
      39         126 :   S->B = B;
      40         126 : }
      41             : 
      42             : /* s1 = s-1 or NULL (if s=1) */
      43             : static GEN
      44         336 : hurwitzp_i(struct hurwitzp_t *S, GEN x)
      45             : {
      46         336 :   GEN z, x2, x2j, s1 = S->s1;
      47         336 :   long j, J = lg(S->B) - 2;
      48             : 
      49         336 :   x = cvtop2(ginv(x), S->_1); z = gmul2n(x, -1);
      50         336 :   z = s1? gmul(s1, z): gadd(Qp_log(x), z);
      51         336 :   x2j = x2 = gsqr(x); z = gaddgs(z, 1);
      52         336 :   for (j = 1;; j++)
      53             :   {
      54        3899 :     z = gadd(z, gmul(gel(S->B, j + 1), x2j));
      55        3899 :     if (j == J) break;
      56        3563 :     x2j = gmul(x2, x2j);
      57             :   }
      58         336 :   if (s1) z = gmul(gdiv(z, s1), Qp_exp(gmul(s1, Qp_log(x))));
      59         336 :   return z;
      60             : }
      61             : /* private (absolute) padicprec */
      62             : static long
      63         210 : pprec(GEN x) { return maxss(valp(x) + precp(x), 1); }
      64             : 
      65             : /* L_p(s, (D, .)); assume s != 1 if D = 1 */
      66             : static GEN
      67          14 : zetap_i(GEN s, long D)
      68             : {
      69          14 :   pari_sp av = avma;
      70          14 :   GEN z, va, gp = gel(s,2);
      71          14 :   ulong a, p = itou(gp), m;
      72          14 :   long prec = pprec(s);
      73             :   struct hurwitzp_t S;
      74             : 
      75          14 :   if (D <= 0) pari_err_DOMAIN("p-adic L-function", "D", "<=", gen_0, stoi(D));
      76          14 :   if (!uposisfundamental(D))
      77           0 :     pari_err_TYPE("p-adic L-function [D not fundamental]", stoi(D));
      78          14 :   hurwitzp_init(&S, prec, s);
      79          14 :   m = ulcm(D, p == 2? 4: p); va = coprimes_zv(m);
      80          42 :   for (a = 1, z = gen_0; a <= (m >> 1); a++)
      81          28 :     if (va[a])
      82             :     {
      83          21 :       GEN h = hurwitzp_i(&S, uutoQ(a, m));
      84          21 :       if (D != 1 && kross(D, a) < 0) h = gneg(h);
      85          21 :       z = gadd(z, h);
      86             :     }
      87          14 :   z = gdivgs(gmul2n(z, 1), m);
      88          14 :   if (D != 1) z = gmul(z, Qp_exp(gmul(gsubsg(1, s), Qp_log(cvstop2(m, s)))));
      89          14 :   return gerepileupto(av, z);
      90             : }
      91             : GEN
      92          14 : Qp_zeta(GEN s) { return zetap_i(s, 1); }
      93             : 
      94             : /* s a t_PADIC; gerepileupto-safe */
      95             : static GEN
      96         112 : hurwitzp(GEN s, GEN x)
      97             : {
      98         112 :   GEN gp = gel(s,2);
      99         112 :   long p = itou(gp), prec = pprec(s);
     100             :   struct hurwitzp_t S;
     101         112 :   hurwitzp_init(&S, prec, s);
     102         112 :   if (typ(x) != t_PADIC) x = gadd(x, zeropadic_shallow(gp, prec));
     103         112 :   if (valp(x) >= ((p==2)? -1: 0))
     104             :   {
     105         105 :     GEN z = gen_0;
     106         105 :     long j, M = (p==2)? 4: p;
     107         560 :     for (j = 0; j < M; j++)
     108             :     {
     109         455 :       GEN y = gaddsg(j, x);
     110         455 :       if (valp(y) <= 0) z = gadd(z, hurwitzp_i(&S, gdivgu(y, M)));
     111             :     }
     112         105 :     return gdivgu(z, M);
     113             :   }
     114           7 :   if (valp(s) < 0) pari_err_DOMAIN("hurwitzp", "v(s)", "<", gen_0, s);
     115           7 :   return hurwitzp_i(&S, x);
     116             : }
     117             : 
     118             : static void
     119        8596 : binsplit(GEN *pP, GEN *pR, GEN aN2, GEN isqaN, GEN s, long j, long k, long prec)
     120             : {
     121        8596 :   if (j + 1 == k)
     122             :   {
     123        4347 :     long j2 = j << 1;
     124             :     GEN P;
     125        4347 :     if (!j) P = gdiv(s, aN2);
     126             :     else
     127             :     {
     128        4249 :       P = gmul(gaddgs(s, j2-1), gaddgs(s, j2));
     129        4249 :       P = gdivgunextu(gmul(P, isqaN), j2+1);
     130             :     }
     131        4347 :     if (pP) *pP = P;
     132        4347 :     if (pR) *pR = gmul(bernreal(j2+2, prec), P);
     133             :   }
     134             :   else
     135             :   {
     136             :     GEN P1, R1, P2, R2;
     137        4249 :     binsplit(&P1,pR? &R1: NULL, aN2, isqaN, s, j, (j+k) >> 1, prec);
     138        4249 :     binsplit(pP? &P2: NULL, pR? &R2: NULL, aN2, isqaN, s, (j+k) >> 1, k, prec);
     139        4249 :     if (pP) *pP = gmul(P1,P2);
     140        4249 :     if (pR) *pR = gadd(R1, gmul(P1, R2));
     141             :   }
     142        8596 : }
     143             : 
     144             : /* a0 +  a1 x + O(x^e), e >= 0 */
     145             : static GEN
     146          77 : deg1ser_shallow(GEN a1, GEN a0, long v, long e)
     147          77 : { return RgX_to_ser(deg1pol_shallow(a1, a0, v), e+2); }
     148             : 
     149             : static long
     150         175 : hurwitz_cutoff(GEN s, long bit)
     151             : {
     152         224 :   return typ(s) == t_COMPLEX &&
     153          49 :          fabs(gtodouble(gel(s,2))) > 5.37 * pow(bit, 1.4) / mt_nbthreads();
     154             : }
     155             : 
     156             : /* New zetahurwitz, from Fredrik Johansson. */
     157             : GEN
     158         371 : zetahurwitz(GEN s, GEN x, long der, long bitprec)
     159             : {
     160         371 :   pari_sp av = avma, av2;
     161         371 :   GEN a, ra, ra0, Nx, S1, S2, S3, N2, rx, sch = NULL, s0 = s, x0 = x, y;
     162         371 :   long j, k, m, N, prec0 = nbits2prec(bitprec), prec = prec0, fli = 0;
     163             :   pari_timer T;
     164             : 
     165         371 :   if (der < 0) pari_err_DOMAIN("zetahurwitz", "der", "<", gen_0, stoi(der));
     166         371 :   if (der)
     167             :   {
     168             :     GEN z;
     169          21 :     if (!is_scalar_t(typ(s)))
     170             :     {
     171           7 :       z = deriv(zetahurwitz(s, x, der - 1, bitprec), -1);
     172           7 :       z = gdiv(z, deriv(s, -1));
     173             :     }
     174             :     else
     175             :     {
     176          14 :       if (gequal1(s)) pari_err_DOMAIN("zetahurwitz", "s", "=", gen_1, s0);
     177          14 :       s = deg1ser_shallow(gen_1, s, 0, der+2);
     178          14 :       z = zetahurwitz(s, x, 0, bitprec + der * log2(der));
     179          14 :       z = gmul(mpfact(der), polcoef_i(z, der, -1));
     180             :     }
     181          21 :     return gerepileupto(av,z);
     182             :   }
     183         350 :   if (typ(s) == t_PADIC) return gerepileupto(av, hurwitzp(s, x));
     184         322 :   if (typ(x) == t_PADIC)
     185             :   {
     186          84 :     GEN p = gel(x,2);
     187          84 :     long e = pprec(x);
     188          84 :     e += sdivsi(e, gsubgs(p, 1));
     189          84 :     s = gadd(s, zeropadic_shallow(p, e));
     190          84 :     return gerepileupto(av, hurwitzp(s, x));
     191             :   }
     192         238 :   switch(typ(x))
     193             :   {
     194         224 :     case t_INT: case t_REAL: case t_FRAC: case t_COMPLEX: break;
     195          14 :     default:
     196          14 :       if (!(y = toser_i(x))) pari_err_TYPE("zetahurwitz", x);
     197           7 :       x = y; x0 = polcoef_i(x, 0, -1); break;
     198             :   }
     199         231 :   rx = grndtoi(real_i(x0), NULL);
     200         231 :   if (typ(rx) != t_INT) pari_err_TYPE("zetahurwitz", x);
     201         231 :   if (x0 == x && signe(rx) <= 0 && gexpo(gsub(x, rx)) < 17 - bitprec)
     202           0 :     pari_err_DOMAIN("zetahurwitz", "x", "<=", gen_0, x);
     203         231 :   switch (typ(s))
     204             :   {
     205             :     long v, pr;
     206         161 :     case t_INT: case t_REAL: case t_FRAC: case t_COMPLEX:
     207         161 :       if (!der && hurwitz_cutoff(s, bitprec))
     208           7 :         return zetahurwitzlarge(s, x, prec);
     209         154 :       break;
     210          70 :     default:
     211          70 :       if (!(y = toser_i(s))) pari_err_TYPE("zetahurwitz", s);
     212          70 :       if (valser(y) < 0) pari_err_DOMAIN("zetahurwitz", "val(s)", "<", gen_0, s);
     213          70 :       s0 = polcoef_i(y, 0, -1);
     214          70 :       switch(typ(s0))
     215             :       {
     216          63 :         case t_INT: case t_REAL: case t_FRAC: case t_COMPLEX: break;
     217           0 :         case t_PADIC: pari_err_IMPL("zetahurwitz(t_SER of t_PADIC)");
     218           7 :         default: pari_err_TYPE("zetahurwitz", s0);
     219             :       }
     220          63 :       sch = gequal0(s0)? y: serchop0(y);
     221          63 :       v = valser(sch);
     222          63 :       pr = (lg(y) + v + 1) / v;
     223          63 :       if (gequal1(s0)) pr += v;
     224          63 :       s = deg1ser_shallow(gen_1, s0, 0, pr);
     225             :     }
     226         217 :   a = gneg(s0); ra = real_i(a); ra0 = ground(ra);
     227         217 :   if (gequal1(s0) && (!sch || gequal0(sch)))
     228          14 :     pari_err_DOMAIN("zetahurwitz", "s", "=", gen_1, s0);
     229         203 :   fli = (gsigne(ra0) >= 0 && gexpo(gsub(a, ra0)) < 17 - bitprec);
     230         203 :   if (!sch && fli)
     231             :   { /* a ~ non negative integer */
     232          14 :     k = itos(gceil(ra)) + 1;
     233          14 :     if (odd(k)) k++;
     234          14 :     N = 1;
     235             :   }
     236             :   else
     237             :   {
     238         189 :     GEN C, ix = imag_i(x0);
     239         189 :     double c = (typ(s) == t_INT)? 1: 20 * log((double)bitprec);
     240         189 :     double rs = gtodouble(ra) + 1;
     241             :     long k0;
     242         189 :     if (fli) a = gadd(a, ghalf); /* hack */
     243         189 :     if (rs > 0)
     244             :     {
     245          49 :       bitprec += (long)ceil(rs * expu(bitprec));
     246          49 :       prec = nbits2prec(bitprec);
     247          49 :       x = gprec_w(x, prec);
     248          49 :       s = gprec_w(s, prec);
     249          49 :       if (sch) sch = gprec_w(sch, prec);
     250             :     }
     251         189 :     k = bitprec * M_LN2 / (1 + dbllambertW0(M_PI / c));
     252         189 :     k0 = itos(gceil(gadd(ra, ghalf))) + 1;
     253         189 :     k = maxss(k0, k);
     254         189 :     if (odd(k)) k++;
     255             :     /* R_k < 2 |binom(a,k+1) B_{k+2}/(k+2)| */
     256         189 :     C = binomial(a, k+1); C = polcoef_i(C, 0, -1);
     257         189 :     C = gmul(C, gdivgu(bernfrac(k+2), k+2));
     258         189 :     C = gmul2n(gabs(C,LOWDEFAULTPREC), bitprec + 1);
     259         189 :     C = gpow(C, ginv(gsubsg(k+1, ra)), LOWDEFAULTPREC);
     260             :     /* need |N + x - 1|^2 > C^2 */
     261         189 :     if (!gequal0(ix)) C = gsqrt(gsub(gsqr(C), gsqr(ix)), LOWDEFAULTPREC);
     262             :     /* need |N + re(x) - 1| > C */
     263         189 :     C = gceil(gadd(C, gsubsg(1, rx)));
     264         189 :     if (typ(C) != t_INT) pari_err_TYPE("zetahurwitz",s);
     265         189 :     N = signe(C) > 0? itos(C) : 1;
     266         189 :     if (N == 1 && signe(a) > 0)
     267             :     { /* May reduce k if 2Pix > a */
     268             :       /* Need 2 |x^(-K) (B_K/K) binom(a, K-1)| < 2^-bit |x|^-rs |zeta(s,x)|
     269             :        * with K = k+2; N = 1; |zeta(s,x)| ~ |x|^(rs-1);
     270             :        * if a > 0, (B_K/K) binom(a, K-1) < 2 |a / 2Pi|^K */
     271           0 :       double dx = dbllog2(x0), d = 1 + dx + log2(M_PI) - dbllog2(s0);
     272           0 :       if (d > 0)
     273             :       { /* d ~ log2 |2Pi x / a| */
     274           0 :         long K = (long)ceil((bitprec + 1 + dx) / d);
     275           0 :         K = maxss(k0, K);
     276           0 :         if (odd(K)) K++;
     277           0 :         if (K < k) k = K;
     278             :       }
     279             :     }
     280             :   }
     281         203 :   if (gsigne(rx) < 0) N = maxss(N, 1 - itos(rx));
     282         203 :   a = gneg(s);
     283         203 :   if (DEBUGLEVEL>2) timer_start(&T);
     284         203 :   incrprec(prec);
     285         203 :   Nx = gaddsg(N - 1, x);
     286         203 :   Nx = typ(Nx) == t_SER? RgX_gtofp(Nx, prec): gtofp(Nx, prec);
     287         203 :   S1 = S3 = gpow(Nx, a, prec);
     288         203 :   av2 = avma;
     289         203 :   if (gequal1(x)) S1 = dirpowerssum(N, a, 0, prec);
     290             :   else
     291        1638 :     for (m = N - 2; m >= 0; m--)
     292             :     {
     293        1491 :       S1 = gadd(S1, gpow(gaddsg(m,x), a, prec));
     294        1491 :       if ((m & 0xff) == 0) S1 = gerepileupto(av2, S1);
     295             :     }
     296         203 :   if (DEBUGLEVEL>2) timer_printf(&T,"sum from 0 to N - 1");
     297         203 :   constbern(k >> 1);
     298         203 :   N2 = ginv(gsqr(Nx));
     299         203 :   if (typ(s0) == t_INT)
     300             :   {
     301         105 :     S2 = divru(bernreal(k, prec), k);
     302        3276 :     for (j = k - 2; j >= 2; j -= 2)
     303             :     {
     304        3171 :       GEN t = gsubgs(a, j), u = gmul(t, gaddgs(t, 1));
     305        3171 :       u = gmul(gdivgunextu(u, j), gmul(S2, N2));
     306        3171 :       S2 = gadd(divru(bernreal(j, prec), j), u);
     307             :     }
     308         105 :     S2 = gmul(S2, gdiv(a, Nx));
     309             :   }
     310             :   else
     311             :   {
     312          98 :     binsplit(NULL,&S2, gmul2n(Nx,1), N2, s, 0, k >> 1, prec);
     313          98 :     S2 = gneg(S2);
     314             :   }
     315         203 :   S2 = gadd(ghalf, S2);
     316         203 :   if (DEBUGLEVEL>2) timer_printf(&T,"Bernoulli sum");
     317         203 :   S2 = gmul(S3, gadd(gdiv(Nx, gaddsg(1, a)), S2));
     318         203 :   S1 = gprec_wtrunc(gsub(S1, S2), prec0);
     319         203 :   if (sch) return gerepileupto(av, gsubst(S1, 0, sch));
     320         147 :   return gerepilecopy(av, S1);
     321             : }
     322             : 
     323             : /* New Lerch, inspired by Fredrik Johansson. */
     324             : 
     325             : GEN
     326      153696 : lerch_worker(GEN t, GEN E)
     327             : {
     328      153696 :   GEN n, d, T, s = gel(E,1), a = gmul(gel(E,2), t), z = gel(E,3);
     329      153256 :   long p = itos(gel(E,4)), prec = labs(p);
     330      153258 :   d = gadd(gexp(t, prec), z);
     331      153492 :   T = p > 0? t: gneg(t);
     332      153538 :   if (typ(s) == t_INT)
     333       58390 :     n = gmul(gpow(T, s, prec), gexp(a, prec));
     334             :   else /* save one exp */
     335       95148 :     n = gexp(gadd(gmul(s, glog(T, prec)), a), prec);
     336      153754 :   return gdiv(n, d);
     337             : }
     338             : 
     339             : /* tab already computed with N = #tab[1] even */
     340             : static GEN
     341         441 : parintnumgauss(GEN f, GEN a, GEN b, GEN tab, long prec)
     342             : {
     343         441 :   GEN R = gel(tab, 1), W = gel(tab, 2), bma, bpa, S = gen_0, VP, VM, V;
     344         441 :   long n = lg(R) - 1, i, prec2 = prec + EXTRAPREC64;
     345         441 :   a = gprec_wensure(a, prec2);
     346         441 :   b = gprec_wensure(b, prec2);
     347         441 :   VP = cgetg(n + 1, t_VEC); bma = gmul2n(gsub(b, a), -1);
     348         441 :   VM = cgetg(n + 1, t_VEC); bpa = gadd(bma, a);
     349       28203 :   for (i = 1; i <= n; i++)
     350             :   {
     351       27762 :     GEN h = gmul(bma, gel(R, i));
     352       27762 :     gel(VP, i) = gadd(bpa, h);
     353       27762 :     gel(VM, i) = gsub(bpa, h);
     354             :   }
     355         441 :   V = gadd(parapply(f, VP), parapply(f, VM));
     356       28203 :   for (i = 1; i <= n; i++)
     357             :   {
     358       27762 :     S = gadd(S, gmul(gel(W, i), gel(V, i)));
     359       27762 :     S = gprec_wensure(S, prec2);
     360             :   }
     361         441 :   return gprec_wtrunc(gmul(bma, S), prec);
     362             : }
     363             : 
     364             : /* Assume tab computed and a >= 0 */
     365             : static GEN
     366         119 : parintnum(GEN f, GEN a, GEN tab)
     367             : {
     368             :   pari_sp av;
     369         119 :   GEN tabx0 = gel(tab, 2), tabw0 = gel(tab, 3), tabxm = gel(tab, 6);
     370         119 :   GEN tabxp = gel(tab, 4), tabwp = gel(tab, 5), tabwm = gel(tab, 7);
     371         119 :   GEN VP = tabxp, VM = tabxm, x0 = tabx0, S;
     372         119 :   long prec = gprecision(tabw0), L = lg(tabxp), i, fla = 0;
     373         119 :   if (!gequal0(a))
     374             :   {
     375          91 :     if (gexpo(a) <= 0)
     376             :     {
     377          63 :       x0 = gadd(a, x0);
     378       30357 :       for (i = 1; i < L; i++)
     379             :       {
     380       30294 :         gel(VP, i) = gadd(a, gel(VP, i));
     381       30294 :         gel(VM, i) = gadd(a, gel(VM, i));
     382             :       }
     383             :     }
     384             :     else
     385             :     {
     386          28 :       x0 = gmul(a, gaddsg(1, x0)); fla = 1;
     387        5404 :       for (i = 1; i < L; i++)
     388             :       {
     389        5376 :         gel(VP, i) = gmul(a, gaddsg(1, gel(VP, i)));
     390        5376 :         gel(VM, i) = gmul(a, gaddsg(1, gel(VM, i)));
     391             :       }
     392             :     }
     393             :   }
     394         119 :   VP = parapply(f, VP);
     395         119 :   VM = parapply(f, VM); av = avma;
     396         119 :   S = gmul(tabw0, closure_callgen1(f, x0));
     397       49257 :   for (i = 1; i < L; i++)
     398             :   {
     399       49138 :     S = gadd(S, gadd(gmul(gel(tabwp, i), gel(VP, i)),
     400       49138 :                      gmul(gel(tabwm, i), gel(VM, i))));
     401       49138 :     if ((i & 0x7f) == 1) S = gerepileupto(av, S);
     402       49138 :     S = gprec_wensure(S, prec);
     403             :   }
     404         119 :   if (fla) S = gmul(S, a);
     405         119 :   return gmul(S, gel(tab, 1));
     406             : }
     407             : 
     408             : static GEN
     409          84 : refine(GEN A)
     410             : {
     411          84 :   long n = lg(A) - 1, i;
     412          84 :   GEN B = cgetg(2 * n, t_VEC);
     413         231 :   for (i = 1; i < n; i++)
     414             :   {
     415         147 :     gel(B, 2 * i - 1) = gel(A, i);
     416         147 :     gel(B, 2 * i) = gmul2n(gadd(gel(A, i), gel(A, i + 1)), -1);
     417             :   }
     418          84 :   gel(B, 2 * n - 1) = gel(A, n); return B;
     419             : }
     420             : 
     421             : /* Here L = [a1, a2, a3,...] integration vertices. Refine by splitting
     422             :  * intervals. */
     423             : static GEN
     424          84 : parintnumgaussadapt(GEN f, GEN L, GEN tab, long bit)
     425             : {
     426          84 :   GEN Rold = gen_0, Rnew;
     427          84 :   long i, ct = 0, prec = nbits2prec(bit);
     428         168 :   while (ct <= 5)
     429             :   {
     430         168 :     Rnew = gen_0;
     431         609 :     for (i = 1; i < lg(L) - 1; i++)
     432         441 :       Rnew = gadd(Rnew, parintnumgauss(f, gel(L, i), gel(L, i + 1), tab, prec));
     433         168 :     if (ct && gexpo(gsub(Rnew, Rold)) - gexpo(Rnew) < 10 - bit) return Rnew;
     434          84 :     ct++; Rold = Rnew; L = refine(L);
     435             :   }
     436           0 :   if (DEBUGLEVEL) err_printf("intnumgaussadapt: possible accuracy loss");
     437           0 :   return Rnew; /* Possible accuracy loss */
     438             : }
     439             : 
     440             : /* Here b = [oo, r], so refine by increasing integration step m */
     441             : static GEN
     442          42 : parintnumadapt(GEN f, GEN a, GEN b, GEN tab, long bit)
     443             : {
     444          42 :   GEN Rold = gen_0, Rnew;
     445          42 :   long m = 0, prec = nbits2prec(bit);
     446          42 :   if (!tab) tab = intnuminit(gen_0, b, 0, prec);
     447         119 :   while (m <= 5)
     448             :   {
     449         119 :     Rnew = parintnum(f, a, tab);
     450         119 :     if (m && gexpo(gsub(Rnew, Rold)) - gexpo(Rnew) < 10 - bit) return Rnew;
     451          77 :     m++; Rold = Rnew; tab = intnuminit(gen_0, b, m, prec);
     452             :   }
     453           0 :   if (DEBUGLEVEL) err_printf("intnumadapt: possible accuracy loss");
     454           0 :   return Rnew; /* Possible accuracy loss */
     455             : }
     456             : 
     457             : static int
     458         210 : iscplx(GEN z) { long t = typ(z); return is_real_t(t) || t == t_COMPLEX; }
     459             : 
     460             : static GEN
     461          14 : lerch_easy(GEN z, GEN s, GEN a, long B)
     462             : {
     463          14 :   long n, prec = nbits2prec(B + 32);
     464          14 :   GEN zn, ms = gneg(s), S = gpow(a, ms, prec);
     465          14 :   zn = z = gtofp(z, prec);
     466        3808 :   for (n = 1;; n++, zn = gmul(zn, z))
     467             :   {
     468        3808 :     S = gadd(S, gmul(zn, gpow(gaddgs(a, n), ms, prec)));
     469        3808 :     if (gexpo(zn) <= - B - 5) return S;
     470             :   }
     471             : }
     472             : 
     473             : static GEN
     474         112 : _lerchphi(GEN z, GEN s, GEN a, long prec)
     475             : {
     476         112 :   GEN res = NULL, L, LT, J, rs, mleft, left, right, top, w, Linf, tabg;
     477             :   GEN E, f, fm;
     478         112 :   long B = prec2nbits(prec), MB = 3 - B, NB, prec2;
     479             :   entree *ep;
     480             : 
     481         112 :   if (gexpo(z) < MB) return gpow(a, gneg(s), prec);
     482         112 :   if (gexpo(gsubgs(z, 1)) < MB) return zetahurwitz(s, a, 0, B); /* z ~ 1 */
     483         112 :   if (gexpo(gaddgs(z, 1)) < MB) /* z ~ -1 */
     484             :   {
     485           7 :     GEN tmp = gsub(zetahurwitz(s, gmul2n(a, -1), 0, B),
     486             :                    zetahurwitz(s, gmul2n(gaddgs(a, 1), -1), 0, B));
     487           7 :     return gmul(gpow(gen_2, gneg(s), prec), tmp);
     488             :   }
     489         105 :   if (gcmpgs(gmulsg(10, gabs(z, prec)), 9) <= 0) /* |z| <= 9/10 */
     490          14 :     return lerch_easy(z, s, a, B);
     491          91 :   if (gcmpgs(real_i(a), 2) < 0)
     492          49 :     return gadd(gpow(a, gneg(s), prec),
     493             :                 gmul(z, _lerchphi(z, s, gaddgs(a, 1), prec)));
     494          42 :   NB = (long)ceil(B + M_PI * fabs(gtodouble(imag_i(s))));
     495          42 :   prec2 = nbits2prec(NB);
     496          42 :   z = gprec_w(z, prec2); /* |z| > 9/10 */
     497          42 :   s = gprec_w(s, prec2);
     498          42 :   a = gprec_w(a, prec2); /* Re(a) >= 2 */
     499          42 :   rs = ground(real_i(s)); L = glog(z, prec2); /* Re(L) > -0.11 */
     500          42 :   ep = is_entry("_lerch_worker");
     501          42 :   E = mkvec4(gsubgs(s, 1), gsubsg(1, a), gneg(z), stoi(prec2));
     502          42 :   f = snm_closure(ep, mkvec(E));
     503          42 :   E = shallowcopy(E); gel(E,4) = stoi(-prec2);
     504          42 :   fm = snm_closure(ep, mkvec(E));
     505          42 :   Linf = mkvec2(mkoo(), real_i(a));
     506          42 :   if (gexpo(gsub(s, rs)) < MB && gcmpgs(rs, 1) >= 0)
     507             :   { /* s ~ positive integer */
     508          14 :     if (gcmp(gabs(imag_i(L), prec2), sstoQ(1, 4)) < 0 && gsigne(real_i(L)) >= 0)
     509           7 :     { /* Re(L) >= 0, |Im(L)| < 1/4 */
     510           7 :       GEN t = gsigne(imag_i(z)) > 0 ? gen_m1: gen_1;
     511           7 :       GEN LT1 = gaddgs(gabs(L, prec2), 1);
     512           7 :       LT = mkvec4(gen_0, mkcomplex(gen_0, t), mkcomplex(LT1, t), LT1);
     513           7 :       tabg = intnumgaussinit(2*(NB >> 2) + 60, prec2);
     514           7 :       J = parintnumgaussadapt(f, LT, tabg, NB);
     515           7 :       J = gadd(J, parintnumadapt(f, LT1, Linf, NULL, NB));
     516             :     }
     517           7 :     else J = parintnumadapt(f, gen_0, Linf, NULL, NB);
     518          14 :     return gdiv(J, ggamma(s, prec2));
     519             :   }
     520          28 :   tabg = intnumgaussinit(2*(NB >> 2) + 60, prec2);
     521          28 :   if (gcmp(gabs(imag_i(L), prec2), ghalf) > 0) /* |Im(L)| > 1/2 */
     522          14 :     left = right = top = gmin(gmul2n(gabs(imag_i(L), prec2), -1), gen_1);
     523             :   else
     524             :   {
     525          14 :     res = gdiv(gpow(gneg(L), s, prec2), gmul(L, gpow(z, a, prec2)));
     526          14 :     left = gaddgs(gmax(gen_0, gneg(real_i(L))), 1);
     527          14 :     top = gaddgs(gabs(imag_i(L), prec2), 1);
     528          14 :     right = gaddgs(gabs(L, prec2), 1);
     529             :   }
     530          28 :   w = expIPiC(gsubgs(s, 1), prec2);
     531          28 :   mleft = gneg(left);
     532          28 :   if (gexpo(imag_i(z)) < MB && gexpo(imag_i(a)) < MB && gexpo(imag_i(s)) < MB
     533           7 :       && gcmpgs(real_i(z), 1) < 0)
     534             :   { /* (z, s, a) real, z < 1 */
     535           7 :     LT = mkvec3(right, mkcomplex(right, top), mkcomplex(mleft, top));
     536           7 :     J = imag_i(gdiv(parintnumgaussadapt(f, LT, tabg, NB), w));
     537           7 :     LT = mkvec2(mkcomplex(mleft, top), mleft);
     538           7 :     J = gmul2n(gadd(J, imag_i(parintnumgaussadapt(fm, LT, tabg, NB))), 1);
     539           7 :     J = mulcxI(J);
     540             :   }
     541             :   else
     542             :   {
     543          21 :     GEN mtop = gneg(top);
     544          21 :     LT = mkvec3(right, mkcomplex(right, top), mkcomplex(mleft, top));
     545          21 :     J = gdiv(parintnumgaussadapt(f, LT, tabg, NB), w);
     546          21 :     LT = mkvec2(mkcomplex(mleft, top), mkcomplex(mleft, mtop));
     547          21 :     J = gadd(J, parintnumgaussadapt(fm, LT, tabg, NB));
     548          21 :     LT = mkvec3(mkcomplex(mleft, mtop), mkcomplex(right, mtop), right);
     549          21 :     J = gadd(J, gmul(parintnumgaussadapt(f, LT, tabg, NB), w));
     550             :   }
     551          28 :   J = gadd(J, gmul(gsub(w, ginv(w)), parintnumadapt(f, right, Linf, NULL, NB)));
     552          28 :   J = gdiv(J, PiI2(prec2)); if (res) J = gadd(J, res);
     553          28 :   return gneg(gmul(ggamma(gsubsg(1, s), prec2), J));
     554             : }
     555             : /* lerchphi(z,-k,a)=
     556             :  *  -1/(z-1)*sum(q=0,k,(z/(z-1))^q*sum(j=0,q,(-1)^j*(j+a)^k*binomial(q,j)))
     557             :  * zetahurwitz(-k,a)=-B(k+1,a)/(k+1) */
     558             : GEN
     559          56 : lerchphi(GEN z, GEN s, GEN a, long prec)
     560             : {
     561          56 :   pari_sp av = avma;
     562          56 :   if (!iscplx(z)) pari_err_TYPE("lerchphi", z);
     563          56 :   if (!iscplx(s)) pari_err_TYPE("lerchphi", s);
     564          56 :   if (!iscplx(a)) pari_err_TYPE("lerchphi", a);
     565          56 :   return gerepileupto(av, _lerchphi(z, s, a, prec));
     566             : }
     567             : 
     568             : GEN
     569          14 : lerchzeta(GEN s, GEN a, GEN lam, long prec)
     570             : {
     571          14 :   pari_sp av = avma;
     572          14 :   GEN z = gexp(gmul(PiI2(prec), lam), prec);
     573          14 :   if (!iscplx(z)) pari_err_TYPE("lerchzeta", z);
     574          14 :   if (!iscplx(s)) pari_err_TYPE("lerchzeta", s);
     575          14 :   if (!iscplx(a)) pari_err_TYPE("lerchzeta", a);
     576          14 :   if (hurwitz_cutoff(s, prec)) return lerchzetalarge(s, a, lam, prec);
     577           7 :   return gerepileupto(av, _lerchphi(z, s, a, prec));
     578             : }

Generated by: LCOV version 1.16