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 - mellininv.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.14.0 lcov report (development 27775-aca467eab2) Lines: 334 338 98.8 %
Date: 2022-07-03 07:33:15 Functions: 36 36 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             : #include "pari.h"
      16             : #include "paripriv.h"
      17             : 
      18             : #define DEBUGLEVEL DEBUGLEVEL_gammamellininv
      19             : 
      20             : /*******************************************************************/
      21             : /*               Computation of inverse Mellin                     */
      22             : /*               transforms of gamma products.                     */
      23             : /*******************************************************************/
      24             : /* Handle complex Vga whose sum is real */
      25             : static GEN
      26       16737 : sumVga(GEN Vga) { return real_i(vecsum(Vga)); }
      27             : 
      28             : /* ac != 0 */
      29             : static double
      30      524561 : lemma526_i(double ac, double c, double t, double B)
      31             : {
      32      524561 :   double D = -B/ac; /* sgn(t) = sgn(a) = - sgn(D) */
      33      524561 :   if (D <= 0)
      34             :   {
      35      458160 :     if (D > -100)
      36             :     {
      37       67263 :       D = -exp(D) / t;
      38       67263 :       if (D < - 1/M_E) return 0;
      39       66934 :       D = dbllambertW_1(D);
      40             :     }
      41             :     else
      42             :     { /* avoid underflow, use asymptotic expansion */
      43      390897 :       double U = D - log(t);
      44      390897 :       D = U - log(-U);
      45             :     }
      46      457831 :     return pow(maxdd(t, -t * D), c);
      47             :   }
      48             :   else
      49             :   {
      50       66401 :     if (D < 100)
      51        9310 :       D = dbllambertW0(-exp(D) / t);
      52             :     else
      53             :     { /* avoid overflow, use asymptotic expansion */
      54       57091 :       double U = D - log(-t);
      55       57091 :       D = U - log(U);
      56             :     }
      57       66401 :     return pow(-t * D, c);
      58             :   }
      59             : }
      60             : /* b > 0, c > 0; solve x^a exp(-b x^(1/c)) < e^(-B) for x >= 0 */
      61             : double
      62          14 : dbllemma526(double a, double b, double c, double B)
      63             : {
      64             :   double ac;
      65          14 :   if (!a) return B <= 0? 0: pow(B/b, c);
      66          14 :   ac = a*c; if (B < 0) B = 1e-9;
      67          14 :   return lemma526_i(ac, c, ac/b, B);
      68             : }
      69             : /* Same, special case b/c = 2Pi, the only one needed: for c = d/2 */
      70             : double
      71     1741229 : dblcoro526(double a, double c, double B)
      72             : {
      73     1741229 :   if (!a) return B <= 0? 0: pow(B/(2*M_PI*c), c);
      74      524547 :   if (B < 0) B = 1e-9;
      75      524547 :   return lemma526_i(a*c, c, a/(2*M_PI), B);
      76             : }
      77             : 
      78             : static const double MELLININV_CUTOFF = 121.; /* C*C */
      79             : 
      80             : /* x real */
      81             : static GEN
      82        8771 : RMOD2(GEN x) { return gsub(x, gmul2n(gdiventgs(x,2), 1)); }
      83             : /* x real or complex, return canonical representative for x mod 2Z */
      84             : static GEN
      85        8771 : MOD2(GEN x)
      86        8771 : { return typ(x) == t_COMPLEX? mkcomplex(RMOD2(gel(x,1)), gel(x,2)): RMOD2(x); }
      87             : static GEN
      88        2723 : RgV_MOD2(GEN x)
      89       11494 : { pari_APPLY_same(MOD2(gel(x,i))); }
      90             : 
      91             : /* classes of poles of the gamma factor mod 2Z, sorted by increasing
      92             :  * Re(s) mod 2 (in [0,2[).*/
      93             : static GEN
      94        2723 : gammapoles(GEN Vga, long *pdV, long bit)
      95             : {
      96        2723 :   long i, m, emax, l = lg(Vga);
      97        2723 :   GEN P, B = RgV_MOD2(Vga), V = cgetg(l, t_VEC);
      98        2723 :   P = gen_indexsort(B, (void*)lexcmp, cmp_nodata);
      99        7630 :   for (i = m = 1; i < l;)
     100             :   {
     101        4907 :     GEN u = gel(B, P[i]);
     102             :     long k;
     103        8771 :     for(k = i+1; k < l; k++)
     104             :     {
     105        6048 :       GEN v = gsub(u, gel(B, P[k]));
     106        6048 :       if (!gequal0(v) && (!isinexactreal(v) || gexpo(v) > -bit)) break;
     107             :     }
     108        4907 :     gel(V, m++) = vecslice(P,i,k-1);
     109        4907 :     i = k;
     110             :   }
     111        2723 :   setlg(V, m); emax = 0;
     112        7630 :   for (i = 1; i < m; i++)
     113             :   {
     114        4907 :     long j, e = 0, li = lg(gel(V,i))-1;
     115        4907 :     GEN b = gel(B, gel(V,i)[1]);
     116       15120 :     for (j = 1; j < m; j++)
     117       10213 :       if (j != i) e -= gexpo(gsub(gel(B, gel(V,j)[1]), b));
     118        4907 :     emax = maxss(emax, e * li);
     119             :   }
     120        7630 :   for (i = 1; i < m; i++) gel(V,i) = vecpermute(Vga, gel(V,i));
     121        2723 :   *pdV = emax; return V;
     122             : }
     123             : 
     124             : static GEN
     125      282869 : sercoeff(GEN x, long n, long prec)
     126             : {
     127      282869 :   long N = n - valp(x);
     128      282869 :   return (N < 0)? gen_0: gprec_wtrunc(gel(x, N+2), prec);
     129             : }
     130             : 
     131             : /* prod_i Gamma(s/2 + (m+LA[i])/2), set t *= prod_i (s/2 + (m+LA[i])/2) */
     132             : static GEN
     133       10213 : get_gamma(GEN *pt, GEN LA, GEN m, int round, long precdl, long prec)
     134             : {
     135       10213 :   long i, l = lg(LA);
     136       10213 :   GEN pr = NULL, t = *pt;
     137       26803 :   for (i = 1; i < l; i++)
     138             :   {
     139       16590 :     GEN u, g, a = gmul2n(gadd(m, gel(LA,i)), -1);
     140       16590 :     if (round) a = ground(a);
     141       16590 :     u = deg1pol_shallow(ghalf, a, 0);
     142       16590 :     g = ggamma(RgX_to_ser(u, precdl), prec);
     143       16590 :     pr = pr? gmul(pr, g): g;
     144       16590 :     t = t? gmul(t, u): u;
     145             :   }
     146       10213 :   *pt = t; return pr;
     147             : }
     148             : /* generalized power series expansion of inverse Mellin around x = 0;
     149             :  * m-th derivative */
     150             : static GEN
     151        2723 : Kderivsmallinit(GEN ldata, GEN Vga, long m, long bit)
     152             : {
     153        2723 :   const double C2 = MELLININV_CUTOFF;
     154        2723 :   long prec2, N, j, l, dLA, limn, d = lg(Vga)-1;
     155             :   GEN piA, LA, L, M, mat;
     156             : 
     157        2723 :   LA = gammapoles(Vga, &dLA, bit); N = lg(LA)-1;
     158        2723 :   prec2 = nbits2prec(dLA + bit * (1 + M_PI*d/C2));
     159             : #if BITS_IN_LONG == 32
     160         389 :   prec2 += prec2 & 1L;
     161             : #endif
     162        2723 :   if (ldata) Vga = ldata_get_gammavec(ldata_newprec(ldata, prec2));
     163        2723 :   L = cgetg(N+1, t_VECSMALL);
     164        2723 :   M = cgetg(N+1, t_VEC);
     165        2723 :   mat = cgetg(N+1, t_VEC);
     166        2723 :   limn = ceil(2*M_LN2*bit / (d * dbllambertW0(C2/(M_PI*M_E))));
     167        2723 :   l = limn + 2;
     168        7630 :   for (j = 1; j <= N; j++)
     169             :   {
     170        4907 :     GEN S = gel(LA,j);
     171        4907 :     GEN C, c, mj, G = NULL, t = NULL, tj = NULL;
     172        4907 :     long i, k, n, jj, lj = L[j] = lg(S)-1, precdl = lj+3;
     173             : 
     174        4907 :     gel(M,j) = mj = gsubsg(2, gel(S, vecindexmin(real_i(S))));
     175       15120 :     for (jj = 1; jj <= N; jj++)
     176             :     {
     177             :       GEN g;
     178       10213 :       if (jj == j) /* poles come from this class only */
     179        4907 :         g = get_gamma(&tj, gel(LA,jj), mj, 1, precdl, prec2);
     180             :       else
     181        5306 :         g = get_gamma(&t, gel(LA,jj), mj, 0, precdl, prec2);
     182       10213 :       G = G? gmul(G, g): g;
     183             :     }
     184        4907 :     c = cgetg(limn+2,t_COL); gel(c,1) = G;
     185      170953 :     for (n=1; n <= limn; n++)
     186             :     {
     187      166046 :       GEN A = utoineg(2*n), T = RgX_translate(tj, A);
     188             :       /* T = exact polynomial, may vanish at 0 (=> pole in c[n+1]) */
     189      166046 :       if (t) T = RgX_mul(T, RgX_translate(t, A)); /* no pole here */
     190      166046 :       gel(c,n+1) = gdiv(gel(c,n), T);
     191             :     }
     192        4907 :     gel(mat, j) = C = cgetg(lj+1, t_COL);
     193       13678 :     for (k = 1; k <= lj; k++)
     194             :     {
     195        8771 :       GEN L = cgetg(l, t_POL);
     196      291640 :       for (n = 2; n < l; n++) gel(L,n) = sercoeff(gel(c,n), -k, prec2);
     197        8771 :       L[1] = evalsigne(1)|evalvarn(0); gel(C,k) = L;
     198             :     }
     199             :     /* C[k] = \sum_n c_{j,k} t^n =: C_k(t) in Dokchitser's Algo 3.3
     200             :      * m-th derivative of t^(-M+2) sum_k (-ln t)^k/k! C_k(t^2) */
     201        4907 :     if (m)
     202             :     {
     203         119 :       mj = gsubgs(mj, 2);
     204         238 :       for (i = 1; i <= m; i++, mj = gaddgs(mj,1))
     205         322 :         for (k = 1; k <= lj; k++)
     206             :         {
     207         203 :           GEN c = gel(C,k), d = RgX_shift_shallow(gmul2n(RgX_deriv(c),1), 1);
     208         203 :           c = RgX_Rg_mul(c, mj);
     209         203 :           if (k < lj) c = RgX_add(c, gel(C,k+1));
     210         203 :           gel(C,k) = RgX_sub(d, c);
     211             :         }
     212         119 :       gel(M,j) = gaddgs(mj,2);
     213             :     }
     214       13678 :     for (k = 1; k <= lj; k++)
     215             :     {
     216        8771 :       GEN c = gel(C,k);
     217        8771 :       if (k > 2) c = RgX_Rg_div(c, mpfact(k-1));
     218        8771 :       gel(C,k) = RgX_to_RgC(c, lgpol(c));
     219             :     }
     220             :   }
     221             :   /* Algo 3.3: * \phi^(m)(t) = sum_j t^m_j sum_k (-ln t)^k mat[j,k](t^2) */
     222        2723 :   piA = gsubsg(m*d, sumVga(Vga));
     223        2723 :   if (!gequal0(piA)) piA = powPis(gmul2n(piA,-1), prec2);
     224        2723 :   return mkvec5(L, RgV_neg(M), mat, mkvecsmall(prec2), piA);
     225             : }
     226             : 
     227             : /* Evaluate a vector considered as a polynomial using Horner. */
     228             : static GEN
     229      239830 : evalvec(GEN vec, long N, GEN u)
     230             : {
     231      239830 :   GEN S = gen_0;
     232             :   long n;
     233      239830 :   N = minss(N, lg(vec)-1);
     234     7095485 :   for (n = N; n >= 1; n--) S = gmul(u, gadd(gel(vec,n), S));
     235      239847 :   return S;
     236             : }
     237             : 
     238             : /* gammamellininvinit accessors */
     239             : static double
     240     4145229 : get_tmax(long bitprec)
     241     4145229 : { return (M_LN2 / MELLININV_CUTOFF) * bitprec ; }
     242             : static GEN
     243        5726 : GMi_get_Vga(GEN K) { return gel(K,2); }
     244             : static long
     245     9030094 : GMi_get_degree(GEN K) { return lg(gel(K,2))-1; }
     246             : static long
     247     4478031 : GMi_get_m(GEN K) { return itos( gel(K,3) ); }
     248             : static GEN /* [lj,mj,mat,prec2], Kderivsmall only */
     249     4616110 : GMi_get_VS(GEN K) { return gel(K,4); }
     250             : static GEN /* [Ms,cd,A2], Kderivlarge only */
     251     8955333 : GMi_get_VL(GEN K) { return gel(K,5); }
     252             : static double
     253     4546971 : GMi_get_tmax(GEN K, long bitprec)
     254     4546971 : { return (typ(GMi_get_VS(K)) == t_INT)? -1.0 : get_tmax(bitprec); }
     255             : 
     256             : /* Compute m-th derivative of inverse Mellin at x by generalized power series
     257             :  * around x = 0; x2d = x^(2/d), x is possibly NULL (don't bother about
     258             :  * complex branches). Assume |x|^(2/d) <= tmax = M_LN2*bitprec/MELLININV_CUTOFF*/
     259             : static GEN
     260       69068 : Kderivsmall(GEN K, GEN x, GEN x2d, long bitprec)
     261             : {
     262       69068 :   GEN VS = GMi_get_VS(K), L = gel(VS,1), M = gel(VS,2), mat = gel(VS,3);
     263       69068 :   GEN d2, Lx, x2, S, pi, piA = gel(VS,5);
     264       69068 :   long prec = gel(VS,4)[1], d = GMi_get_degree(K);
     265       69069 :   long j, k, limn, N = lg(L)-1;
     266       69069 :   double xd, Wd, Ed = M_LN2*bitprec / d;
     267             : 
     268       69069 :   xd = maxdd(M_PI*dblmodulus(x2d), 1E-13); /* pi |x|^2/d unless x tiny */
     269       69069 :   if (xd > Ed) pari_err_BUG("Kderivsmall (x2d too large)");
     270             :   /* Lemma 5.2.6 (2), a = 1 + log(Pi x^(2/d)) = log(e / xd),
     271             :    * B = log(2)*bitprec / d = Ed */
     272       69069 :   Wd = dbllambertW0( Ed / (M_E*xd) ); /* solution of w exp(w) = B exp(-a)*/
     273       69069 :   limn = (long) ceil(2*Ed/Wd);
     274       69069 :   pi = mppi(prec);
     275       69067 :   d2 = gdivsg(d,gen_2);
     276       69067 :   if (x)
     277        1122 :     x = gmul(gtofp(x,prec), gpow(pi,d2,prec));
     278             :   else
     279       67945 :     x = gpow(gmul(gtofp(x2d,prec),pi), d2, prec);
     280             :   /* at this stage, x has been replaced by pi^(d/2) x */
     281       69068 :   x2 = gsqr(x);
     282       69071 :   Lx = gpowers(gneg(glog(x,prec)), vecsmall_max(L));
     283       69069 :   S = gen_0;
     284      191589 :   for (j = 1; j <= N; ++j)
     285             :   {
     286      122521 :     long lj = L[j];
     287      122521 :     GEN s = gen_0;
     288      362355 :     for (k = 1; k <= lj; k++)
     289      239832 :       s = gadd(s, gmul(gel(Lx,k), evalvec(gmael(mat,j,k), limn, x2)));
     290      122523 :     S = gadd(S, gmul(gpow(x, gel(M,j), prec), s));
     291             :   }
     292       69068 :   if (!gequal0(piA)) S = gmul(S, piA);
     293       69069 :   return S;
     294             : }
     295             : 
     296             : /* In Klarge, we conpute K(t) as (asymptotic) * F(z), where F ~ 1 is given by
     297             :  * a continued fraction and z = Pi t^(2/d). If we take 2n terms in F (n terms
     298             :  * in Euler form), F_n(z) - F(z) is experimentally in exp(- C sqrt(n*z))
     299             :  * where C ~ 8 for d > 2 [HEURISTIC] and C = 4 (theorem) for d = 1 or d = 2
     300             :  * and vga = [0,1]. For e^(-E) absolute error, we want
     301             :  *   exp(-C sqrt(nz)) < e^-(E+a), where a ~ ln(asymptotic)
     302             :  * i.e.  2n > (E+a)^2 / t^(2/d) * 2/(C^2 Pi); C^2*Pi/2 ~ 100.5 ~ 101
     303             :  *
     304             :  * In fact, this model becomes wrong for z large: we use instead
     305             :  *
     306             :  *   exp(- sqrt(D * nz/log(z+1))) < e^-(E+a),
     307             :  * i.e.  2n > (E+a)^2 * log(1 + Pi t^(2/d))/ t^(2/d) * 2/(D Pi); */
     308             : static double
     309     4491713 : get_D(long d) { return d <= 2 ? 157. : 180.; }
     310             : /* if (abs), absolute error rather than relative */
     311             : static void
     312     4478152 : Kderivlarge_optim(GEN K, long abs, GEN t2d,GEN gcd, long *pbitprec, long *pnlim)
     313             : {
     314     4478152 :   GEN VL = GMi_get_VL(K), A2 = gel(VL,3);
     315     4477949 :   long bitprec = *pbitprec, d = GMi_get_degree(K);
     316     4477990 :   const double D = get_D(d), td = dblmodulus(t2d), cd = gtodouble(gcd);
     317     4477550 :   double a, rtd, E = M_LN2*bitprec;
     318             : 
     319     4477550 :   rtd = (typ(t2d) == t_COMPLEX)? gtodouble(gel(t2d,1)): td;
     320             :   /* A2/2 = A, log(td) = (2/d)*log t */
     321     4477550 :   a = d*gtodouble(A2)*log2(td)/2 - (M_PI/M_LN2)*d*rtd + log2(cd);/*log2 K(t)~a*/
     322             :   /* if bitprec <= 0, caller should return K(t) ~ 0 */
     323     4477233 :   bitprec += 64;
     324     4477233 :   if (abs)
     325             :   {
     326     4472635 :     bitprec += ceil(a);
     327     4472635 :     if (a <= -65) E = M_LN2*bitprec; /* guarantees E <= initial E */
     328             :   }
     329     4477233 :   *pbitprec = bitprec;
     330     4477233 :   *pnlim = ceil(E*E * log2(1+M_PI*td) / (D*td));
     331     4477233 : }
     332             : 
     333             : /* Compute m-th derivative of inverse Mellin at t by continued fraction of
     334             :  * asymptotic expansion; t2d = t^(2/d). If t is NULL, "lfun" mode: don't
     335             :  * bother about complex branches + use absolute (rather than relative)
     336             :  * accuracy */
     337             : static GEN
     338     4478220 : Kderivlarge(GEN K, GEN t, GEN t2d, long bitprec0)
     339             : {
     340             :   GEN tdA, P, S, pi, z;
     341     4478220 :   const long d = GMi_get_degree(K);
     342     4477975 :   GEN M, VL = GMi_get_VL(K), Ms = gel(VL,1), cd = gel(VL,2), A2 = gel(VL,3);
     343     4477951 :   long status, prec, nlim, m = GMi_get_m(K), bitprec = bitprec0;
     344             : 
     345     4477977 :   Kderivlarge_optim(K, !t, t2d, cd, &bitprec, &nlim);
     346     4477725 :   if (bitprec <= 0) return gen_0;
     347     4392591 :   prec = nbits2prec(bitprec);
     348     4392526 :   t2d = gtofp(t2d, prec);
     349     4392600 :   if (t)
     350        4695 :     tdA = gpow(t, gdivgu(A2,d), prec);
     351             :   else
     352     4387905 :     tdA = gpow(t2d, gdivgu(A2,2), prec);
     353     4392551 :   tdA = gmul(cd, tdA);
     354             : 
     355     4392304 :   pi = mppi(prec);
     356     4392357 :   z = gmul(pi, t2d);
     357     4392947 :   P = gmul(tdA, gexp(gmulsg(-d, z), prec));
     358     4392886 :   if (m) P = gmul(P, gpowgs(mulsr(-2, pi), m));
     359     4392886 :   M = gel(Ms,1); status = itos(gel(Ms,2));
     360     4392777 :   if (status == 2)
     361      413205 :     S = (lg(M) == 2)? gel(M,1) /* constant continued fraction */
     362      413207 :                     : poleval(RgV_to_RgX(M, 0), ginv(z));
     363             :   else
     364             :   {
     365     3979570 :     S = contfraceval_inv(M, z, nlim/2);
     366     3979796 :     if (DEBUGLEVEL>3)
     367             :     {
     368           0 :       GEN S0 = contfraceval_inv(M, z, nlim/2 + 1);
     369           0 :       long e = gexpo(gmul(P, gsub(S,S0)));
     370           0 :       if (-e < bitprec0)
     371           0 :         err_printf("Kderivlarge: e = %ld, bit = %ld\n",e,bitprec0);
     372             :     }
     373     3979796 :     if (status == 1) S = gmul(S, gsubsg(1, ginv(gmul(z, pi))));
     374             :   }
     375     4393001 :   return gmul(P, S);
     376             : }
     377             : 
     378             : /* Dokchitser's coefficients used for asymptotic expansion of inverse Mellin
     379             :  * 2 <= p <= min(n+1, d), c = 2n-p+1; sh = (sh(x)/x)^(d-p) */
     380             : static GEN
     381      803178 : vp(long p, long c, GEN SMd, GEN sh)
     382             : {
     383      803178 :   GEN s, ve = cgetg(p+2, t_VEC);
     384             :   long m, j, k;
     385             : 
     386      803178 :   gel(ve,1) = gen_1; gel(ve,2) = utoipos(c);
     387     2268633 :   for (j = 2; j <= p; j++) gel(ve,j+1) = gdivgs(gmulgu(gel(ve,j), c), j);
     388      803178 :   s = gel(SMd, 1);
     389     3071811 :   for (m = 1; m <= p; m++)
     390             :   {
     391     2268633 :     GEN t, c = gel(SMd, m+1);
     392     2268633 :     if (gequal0(c)) continue;
     393     1225040 :     t = gel(ve, m+1);
     394     2379835 :     for (k = 1; k <= m/2; k++)
     395     1154795 :       t = gadd(t, gmul(gel(ve, m-2*k+1), RgX_coeff(sh, k)));
     396     1225040 :     s = gadd(s, gmul(c, t));
     397             :   }
     398      803178 :   return s;
     399             : }
     400             : 
     401             : static GEN
     402        2758 : get_SM(GEN Vga)
     403             : {
     404        2758 :   long k, m, d = lg(Vga)-1;
     405        2758 :   GEN pol, nS1, SM, C, t = vecsum(Vga);
     406             : 
     407        2758 :   pol = roots_to_pol(gmulgs(Vga, -d), 0); /* deg(pol) = d */
     408        2758 :   SM = cgetg(d+2, t_VEC); gel(SM,1) = gen_1;
     409        2758 :   if (gequal0(t))
     410             :   { /* shortcut */
     411        4606 :     for (m = 1; m <= d; m++) gel(SM,m+1) = gel(pol,d+2-m);
     412        1148 :     return SM;
     413             :   }
     414        1610 :   nS1 = gpowers(gneg(t), d); C = matpascal(d);
     415        7007 :   for (m = 1; m <= d; m++)
     416             :   {
     417        5397 :     pari_sp av = avma;
     418        5397 :     GEN s = gmul(gel(nS1, m+1), gcoeff(C, d+1, m+1));
     419       17920 :     for (k = 1; k <= m; k++)
     420             :     {
     421       12523 :       GEN e = gmul(gel(nS1, m-k+1), gcoeff(C, d-k+1, m-k+1));
     422       12523 :       s = gadd(s, gmul(e, RgX_coeff(pol, d-k)));
     423             :     }
     424        5397 :     gel(SM, m+1) = gerepileupto(av, s);
     425             :   }
     426        1610 :   return SM;
     427             : }
     428             : 
     429             : static GEN
     430        2758 : get_SMd(GEN Vga)
     431             : {
     432        2758 :   GEN M, SM = get_SM(Vga);
     433        2758 :   long p, m, d = lg(Vga)-1;
     434             : 
     435        2758 :   M = cgetg(d, t_VEC);
     436        8855 :   for (p = 2; p <= d; p++)
     437             :   {
     438        6097 :     GEN a = gen_1, c;
     439        6097 :     long D = d - p;
     440        6097 :     gel(M, p-1) = c = cgetg(p+2, t_COL);
     441        6097 :     gel(c, 1) = gel(SM, p+1);
     442       23471 :     for (m = 1; m <= p; m++)
     443             :     {
     444       17374 :       a = muliu(a, D + m);
     445       17374 :       gel(c, m+1) = gmul(gel(SM, p-m+1), a);
     446             :     }
     447             :   }
     448        2758 :   return M;
     449             : }
     450             : 
     451             : /* Asymptotic expansion of inverse Mellin, to length nlimmax. Set status = 0
     452             :  * (regular), 1 (one Hankel determinant vanishes => contfracinit will fail)
     453             :  * or 2 (same as 1, but asymptotic expansion is finite!)
     454             :  *
     455             :  * If status = 2, the asymptotic expansion is finite so return only
     456             :  * the necessary number of terms nlim <= nlimmax + d. */
     457             : static GEN
     458       13783 : Klargeinit(GEN Vga, long nlimmax, long *status, long prec)
     459             : {
     460       13783 :   long d = lg(Vga) - 1, p, n, cnt;
     461             :   GEN M, SMd, se, vsinh, vd;
     462             : 
     463       13783 :   if (Vgaeasytheta(Vga)) { *status = 2; return mkvec(gen_1); }
     464             :   /* d >= 2 */
     465        2758 :   *status = 0; prec += prec >> 1;
     466        2758 :   SMd = get_SMd(Vga);
     467        2758 :   se = gsinh(RgX_to_ser(pol_x(0), d+2), 0); setvalp(se,0);
     468        2758 :   se = gdeflate(se, 0, 2); /* se(x^2) = sinh(x)/x */
     469        2758 :   vsinh = gpowers(se, d);
     470        2758 :   vd = gpowers(utoipos(2*d), d);
     471        2758 :   M = cgetg(nlimmax + d + 1, t_VEC); gel(M,1) = gen_1;
     472      367148 :   for (n = 2, cnt = 0; n <= nlimmax || cnt; n++)
     473             :   {
     474      367148 :     pari_sp av = avma;
     475      367148 :     long ld = minss(d, n);
     476      367148 :     GEN s = gen_0;
     477     1170326 :     for (p = 2; p <= ld; p++)
     478             :     {
     479      803178 :       GEN z = vp(p, 2*n-1-p, gel(SMd, p-1), gel(vsinh, d-p+1));
     480      803178 :       s = gadd(s, gmul(gdiv(z, gel(vd, p+1)), gel(M, n+1-p)));
     481             :     }
     482      367148 :     if (prec && !isinexact(s)) s = gtofp(s, prec);
     483      367148 :     gel(M,n) = s = gerepileupto(av, gdivgs(s, 1-n));
     484      367148 :     if (gequal0(s))
     485             :     {
     486          48 :       cnt++; *status = 1;
     487          48 :       if (cnt >= d-1) { *status = 2; n -= d-2; break; }
     488             :     }
     489             :     else
     490             :     {
     491      367100 :       if (n >= nlimmax) { n++; break; }
     492      364370 :       cnt = 0;
     493             :     }
     494             :   }
     495        2758 :   setlg(M, n); return M;
     496             : }
     497             : 
     498             : /* remove trailing zeros from vector. */
     499             : static void
     500         252 : stripzeros(GEN M)
     501             : {
     502             :   long i;
     503         441 :   for(i = lg(M)-1; i >= 1; --i)
     504         441 :     if (!gequal0(gel(M, i))) break;
     505         252 :   setlg(M, i+1);
     506         252 : }
     507             : 
     508             : /* Asymptotic expansion of the m-th derivative of inverse Mellin, to length
     509             :  * nlimmax. If status = 2, the asymptotic expansion is finite so return only
     510             :  * the necessary number of terms nlim <= nlimmax + d. */
     511             : static GEN
     512       13783 : gammamellininvasymp_i(GEN Vga, long nlimmax, long m, long *status, long prec)
     513             : {
     514             :   GEN M, A, Aadd;
     515             :   long d, i, nlim, n;
     516             : 
     517       13783 :   M = Klargeinit(Vga, nlimmax, status, prec);
     518       13783 :   if (!m) return M;
     519         252 :   d = lg(Vga)-1;
     520             :   /* half the exponent of t in asymptotic expansion. */
     521         252 :   A = gdivgu(gaddsg(1-d, sumVga(Vga)), 2*d);
     522         252 :   if (*status == 2) M = shallowconcat(M, zerovec(m));
     523         252 :   nlim = lg(M)-1;
     524         252 :   Aadd = sstoQ(2-d, 2*d); /* (1/d) - (1/2) */
     525         532 :   for (i = 1; i <= m; i++, A = gadd(A,Aadd))
     526        8022 :     for (n = nlim-1; n >= 1; --n)
     527       15484 :       gel(M, n+1) = gsub(gel(M, n+1),
     528       15484 :                          gmul(gel(M, n), gsub(A, uutoQ(n-1, d))));
     529         252 :   stripzeros(M); return M;
     530             : }
     531             : static GEN
     532       13797 : get_Vga(GEN x, GEN *ldata)
     533             : {
     534       13797 :   *ldata = lfunmisc_to_ldata_shallow_i(x);
     535       13797 :   if (*ldata) x = ldata_get_gammavec(*ldata);
     536       13797 :   return x;
     537             : }
     538             : GEN
     539          28 : gammamellininvasymp(GEN Vga, long nlim, long m)
     540             : {
     541          28 :   pari_sp av = avma;
     542             :   long status;
     543             :   GEN ldata;
     544          28 :   Vga = get_Vga(Vga, &ldata);
     545          28 :   if (!is_vec_t(typ(Vga)) || lg(Vga) == 1)
     546           7 :     pari_err_TYPE("gammamellininvasymp",Vga);
     547          21 :   return gerepilecopy(av, gammamellininvasymp_i(Vga, nlim, m, &status, 0));
     548             : }
     549             : 
     550             : /* Does the continued fraction of the asymptotic expansion M at oo of inverse
     551             :  * Mellin transform attached to Vga have zero Hankel determinants ? */
     552             : static long
     553        2717 : ishankelspec(GEN Vga, GEN M)
     554             : {
     555        2717 :   long status, i, d = lg(Vga)-1;
     556             : 
     557        2717 :   if (d == 5 || d == 7)
     558             :   { /* known bad cases: a x 5 and a x 7 */
     559         127 :     GEN v1 = gel(Vga, 1);
     560         576 :     for (i = 2; i <= d; ++i)
     561         464 :       if (!gequal(gel(Vga,i), v1)) break;
     562         127 :     if (i > d) return 1;
     563             :   }
     564        2605 :   status = 0;
     565             :   /* Heuristic: if 6 first terms in contfracinit don't fail, assume it's OK */
     566        2605 :   pari_CATCH(e_INV) { status = 1; }
     567        2605 :   pari_TRY { contfracinit(M, minss(lg(M)-2,6)); }
     568        2605 :   pari_ENDCATCH; return status;
     569             : }
     570             : 
     571             : /* Initialize data for computing m-th derivative of inverse Mellin */
     572             : GEN
     573       13769 : gammamellininvinit(GEN Vga, long m, long bitprec)
     574             : {
     575       13769 :   const double C2 = MELLININV_CUTOFF;
     576       13769 :   pari_sp ltop = avma;
     577             :   GEN A2, M, VS, VL, cd, ldata;
     578       13769 :   long nlimmax, status, d, prec = nbits2prec((4*bitprec)/3);
     579       13769 :   double E = M_LN2*bitprec, tmax = get_tmax(bitprec); /* = E/C2 */
     580             : 
     581       13769 :   Vga = get_Vga(Vga, &ldata); d = lg(Vga)-1;
     582       13769 :   if (!is_vec_t(typ(Vga)) || !d) pari_err_TYPE("gammamellininvinit",Vga);
     583       13762 :   nlimmax = ceil(E * log2(1+M_PI*tmax) * C2 / get_D(d));
     584       13762 :   A2 = gaddsg(m*(2-d) + 1-d, sumVga(Vga));
     585       13762 :   cd = (d <= 2)? gen_2: gsqrt(gdivgu(int2n(d+1), d), nbits2prec(bitprec));
     586             :   /* if in Klarge, we have |t| > tmax = E/C2, thus nlim < E*C2/D. */
     587       13762 :   M = gammamellininvasymp_i(Vga, nlimmax, m, &status, prec);
     588       13762 :   if (status == 2)
     589             :   {
     590       11039 :     tmax = -1.; /* only use Klarge */
     591       11039 :     VS = gen_0;
     592             :   }
     593             :   else
     594             :   {
     595        2723 :     VS = Kderivsmallinit(ldata, Vga, m, bitprec);
     596        2723 :     if (status == 0 && ishankelspec(Vga, M)) status = 1;
     597        2723 :     if (status == 1)
     598             :     { /* a Hankel determinant vanishes => contfracinit is undefined.
     599             :          So compute K(t) / (1 - 1/(pi^2*t)) instead of K(t)*/
     600         119 :       GEN t = ginv(mppi(prec));
     601             :       long i;
     602       18018 :       for (i = 2; i < lg(M); ++i)
     603       17899 :         gel(M, i) = gadd(gel(M, i), gmul(gel(M, i-1), t));
     604             :     }
     605             :     else
     606        2604 :       M = RgC_gtofp(M, prec); /* convert from rationals to t_REAL: faster */
     607        2723 :     M = contfracinit(M, lg(M)-2);
     608             :   }
     609       13762 :   VL = mkvec3(mkvec2(M, stoi(status)), cd, A2);
     610       13762 :   return gerepilecopy(ltop, mkvec5(dbltor(tmax), Vga, stoi(m), VS, VL));
     611             : }
     612             : 
     613             : /* Compute m-th derivative of inverse Mellin at s2d = s^(d/2) using
     614             :  * initialization data. Use Taylor expansion at 0 for |s2d| < tmax, and
     615             :  * asymptotic expansion at oo otherwise. WARNING: assume that accuracy
     616             :  * has been increased according to tmax by the CALLING program. */
     617             : static GEN
     618     4547328 : gammamellininvrt_i(GEN K, GEN s, GEN s2d, long bit)
     619             : {
     620     4547328 :   if (dblmodulus(s2d) < GMi_get_tmax(K, bit))
     621       69068 :     return Kderivsmall(K, s, s2d, bit);
     622             :   else
     623     4478211 :     return Kderivlarge(K, s, s2d, bit);
     624             : }
     625             : GEN
     626     4541665 : gammamellininvrt(GEN K, GEN s2d, long bit)
     627     4541665 : { return gammamellininvrt_i(K, NULL, s2d, bit); }
     628             : 
     629             : /* Compute inverse Mellin at s. K from gammamellininv OR a Vga, in which
     630             :  * case the initialization data is computed. */
     631             : GEN
     632        5817 : gammamellininv(GEN K, GEN s, long m, long bitprec)
     633             : {
     634        5817 :   pari_sp av = avma;
     635             :   GEN s2d;
     636             :   long d;
     637             : 
     638        5817 :   if (!is_vec_t(typ(K)) || lg(K) != 6 || !is_vec_t(typ(GMi_get_Vga(K))))
     639          91 :     K = gammamellininvinit(K, m, bitprec);
     640        5817 :   d = GMi_get_degree(K);
     641        5817 :   s2d = gpow(s, gdivgu(gen_2, d), nbits2prec(bitprec));
     642        5817 :   return gerepileupto(av, gammamellininvrt_i(K, s, s2d, bitprec));
     643             : }

Generated by: LCOV version 1.13