/* Copyright (C) 2000  The PARI group.
This file is part of the PARI/GP package.

PARI/GP is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version. It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY WHATSOEVER.

Check the License for details. You should have received a copy of it, along
with the package; see the file 'COPYING'. If not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */

/********************************************************************/
/**                                                                **/
/**                         MULTIPLE ZETA VALUES                   **/
/**                                                                **/
/********************************************************************/
#include "pari.h"
#include "paripriv.h"

#define DEBUGLEVEL DEBUGLEVEL_zetamult

/********************************************************************/
/**                           CONVERSIONS                          **/
/********************************************************************/
/* vecsmall to binary */
static long
fd(GEN evec, long a, long b)
{
  long i, s = 0;
  for (i = a; i <= b; i++) s = evec[i] | (s << 1);
  return s;
}
/* 2^(b-a+1) + fd(evec) */
static long
fd1(GEN evec, long a, long b)
{
  long i, s = 1;
  for (i = a; i <= b; i++) s = evec[i] | (s << 1);
  return s;
}

/* m > 0 */
static GEN
mtoevec(GEN m)
{
  GEN e = vecsmall_append(binary_zv(m), 1);
  e[1] = 0; return e;
}
static GEN
etoindex(GEN evec) { return utoipos(fd1(evec, 2, lg(evec)-2)); }

/* dual of evec[1..l-1] */
static GEN
revslice(GEN evec, long l)
{
  GEN res = cgetg(l, t_VECSMALL);
  long i;
  for (i = 1; i < l; i++) res[i] = 1 - evec[l-i];
  return res;
}

/* N.B. evec[ne] = 1 */
static GEN
etoa(GEN evec)
{
  long c = 0, cold = 0, i = 1, l = lg(evec);
  GEN avec = cgetg(l, t_VECSMALL);
  while (++c < l)
    if (evec[c] == 1) { avec[i++] = c - cold; cold = c; }
  setlg(avec, i); return avec;
}

static GEN
atoe(GEN avec)
{
  long i, j, l = lg(avec), n = zv_sum(avec);
  GEN evec = zero_zv(n);
  for (i = 1, j = 0; i < l; i++) { long a = avec[i]; j += a; evec[j] = 1; }
  return evec;
}


/* Conversions: types are evec, avec, m (if evec=0y1, m=(1y)_2).
   fl is respectively 0, 1, 2. Type of a is autodetected. */
static GEN
zetamultconvert_i(GEN a, long fl)
{
  long i, l;
  if (fl < 0 || fl > 2) pari_err_FLAG("zetamultconvert");
  switch(typ(a))
  {
    case t_INT:
      if (signe(a) <= 0) pari_err_TYPE("zetamultconvert",a);
      switch (fl)
      {
        case 0: a = mtoevec(a); break;
        case 1: a = etoa(mtoevec(a)); break;
        case 2: a = icopy(a); break;
      }
      break;
    case t_VEC: case t_COL: case t_VECSMALL:
      a = gtovecsmall(a);
      l = lg(a);
      if (a[1] == 0)
      {
        if (!a[l-1]) pari_err_TYPE("zetamultconvert", a);
        for (i = 1; i < l; i++)
          if (a[i] & ~1UL) pari_err_TYPE("zetamultconvert", a);
        switch (fl)
        {
          case 1: a = etoa(a); break;
          case 2: a = etoindex(a);
        }
      }
      else
      {
        if (a[1] < 2) pari_err_TYPE("zetamultconvert", a);
        for (i = 2; i < l; i++)
          if (a[i] <= 0) pari_err_TYPE("zetamultconvert", a);
        switch (fl)
        {
          case 0: a = atoe(a); break;
          case 2: a = etoindex(atoe(a));
        }
      }
      break;
    default: pari_err_TYPE("zetamultconvert", a);
  }
  return a;
}
GEN
zetamultconvert(GEN a, long fl)
{
  pari_sp av = avma;
  return gc_leaf(av, zetamultconvert_i(a, fl));
}

GEN
zetamultdual(GEN s)
{
  pari_sp av = avma;
  GEN e = zetamultconvert_i(s, 0);
  return gc_leaf(av, etoa(revslice(e, lg(e))));
}

/********************************************************************/
/**                  AVEC -> LIST OF STAR AVECS                    **/
/********************************************************************/
/* all star avecs corresponding to given t_VECSMALL avec */
static GEN
allstar(GEN avec)
{
  long i, la = lg(avec), K = 1 << (la - 2);
  GEN w = cgetg(K + 1, t_VEC);

  gel(w, 1) = avec;
  for (i = 2; i < la; i++)
  {
    long L = 1 << (i - 2), j;
    for (j = 1; j <= L; j++)
    {
      GEN u = gel(w,j), v;
      long k, l = lg(u) - 1, ind = l - la + i;
      gel(w, L + j) = v = cgetg(l, t_VECSMALL);
      for (k = 1; k < ind; k++) v[k] = u[k];
      v[ind] = u[ind] + u[ind + 1];
      for (k = ind + 1; k < l; k++) v[k] = u[k+1];
    }
  }
  return w;
}
/* same for multipolylogs */
static GEN
allstar2(GEN avec, GEN zvec)
{
  long la = lg(avec), i, K = 1 << (la - 2);
  GEN W = cgetg(K + 1, t_VEC), Z = cgetg(K + 1, t_VEC);

  gel(W, 1) = avec;
  gel(Z, 1) = zvec = gtovec(zvec);
  for (i = 2; i < la; i++)
  {
    long L = 1 << (i - 2), j;
    for (j = 1; j <= L; j++)
    {
      GEN u = gel(W, j), w, y = gel(Z, j), z;
      long l = lg(u) - 1, ind = l - la + i, k;
      w = cgetg(l, t_VECSMALL);
      z = cgetg(l, t_VEC);
      for (k = 1; k < ind; k++) { w[k] = u[k]; gel(z, k) = gel(y, k); }
      w[ind] = u[ind] + u[ind + 1];
      gel(z, ind) = gmul(gel(y, ind), gel(y, ind + 1));
      for (k = ind + 1; k < l; k++) { w[k] = u[k+1]; gel(z, k) = gel(y, k+1); }
      gel(W, L + j) = w;
      gel(Z, L + j) = z;
    }
  }
  return mkvec2(W, Z);
}

/**************************************************************/
/*              ZAGIER & RADCHENKO'S ALGORITHM                */
/**************************************************************/
/* accuracy 2^(-b); s << (b/log b)^2, l << b/sqrt(log b) */
static void
zparams(long *s, long *l, long b)
{
  double D = b * LOG10_2, E = 3*D/2 / log(3*D);
  *s = (long)floor(E*E);
  *l = (long)floor(sqrt(*s * log((double)*s)/2.));
}

static GEN
zetamult_Zagier(GEN avec, long bit, long prec)
{
  pari_sp av;
  GEN ze, z = NULL, b;
  long h, r, n, s, l, lz = nbits2lg(prec), t = lg(avec) - 1;

  zparams(&s, &l, bit);
  ze= cgetg(s + 1, t_VEC);
  b = cgetg(l + 1, t_VEC);
  for (r = 1; r <= s; r++) gel(ze,r) = cgetg(lz, t_REAL);
  for (r = 1; r <= l; r++) gel(b,r) = utor_lg(0, lz);
  affur(1, gel(b,1)); av = avma;
  for (r = 1, h = -1; r <= t; r++)
  {
    long m, j = avec[r];
    GEN q;

    h += j - 1; z = gen_0;
    q = h? invr(itor(powuu(s,h), prec)): real_1(prec);
    for (m = 1; m <= l; m++)
    {
      pari_sp av2;
      GEN S = gel(b, m), C;
      q = divru(q, s); av2 = avma;
      C = binomialuu(h + m, m);
      for (n = 1; n < m; n++)
      { /* C = binom(h+m, m-n+1) */
        S = gsub(S, mulir(C, gel(b, n)));
        C = diviuexact(muliu(C, m-n+1), h+n);
      }
      affrr(divru(S, h+m), gel(b,m)); set_avma(av2);
      z = gadd(z, gmul(gel(b, m), q));
    }
    for (m = s; m >= 1; m--)
    {
      GEN z1 = r == 1? ginv(powuu(m,j)): gdiv(gel(ze, m), powuu(m,j));
      z1 = gadd(z, z1);
      affrr(z, gel(ze, m)); z = z1;
    }
    set_avma(av);
  }
  return z;
}

/* Compute t-mzvs; slower than Zagier's code for t=0 */
static GEN
zetamult_interpolate2_i(GEN avec, GEN t, long prec)
{
  pari_sp av;
  GEN a, b, ze, _1;
  long i, j, n, s, l;

  zparams(&s, &l, prec);
  n = lg(avec) - 1;
  a = zeromatcopy(n + 1, l);
  b = zerovec(l + 1);
  for (i = 1; i <= n; i++)
  {
    long h = avec[n + 1 - i];
    if (i == 1) gel(b, h) = gen_1;
    else
      for (j = l + 1; j >= 1; j--)
      {
        if (j <= h) gel(b, j) = gen_0;
        else gel(b, j) = gadd(gcoeff(a, i, j-h), gmul(t, gel(b, j-h)));
      }
    gcoeff(a, i+1, 1) = gel(b, 2); /* j = 1 */
    for (j = 2; j <= l; j++)
    { /* b[j+1] - sum_{0 <= u < j-1} binom(j,u) a[i+1,u+1]*/
      pari_sp av = avma;
      GEN S = gel(b, j + 1);
      S = gsub(S, gcoeff(a, i+1, 1)); /* u = 0 */
      if (j > 2) S = gsub(S, gmulgu(gcoeff(a, i+1, 2), j)); /* u = 1 */
      if (j >= 4)
      {
        GEN C = utoipos(j*(j-1) / 2);
        long u, U = (j-1)/2;
        for (u = 2; u <= U; u++)
        { /* C = binom(j, u) = binom(j, j-u) */
          GEN A = gadd(gcoeff(a, i+1, u+1), gcoeff(a, i+1, j-u+1));
          S = gsub(S, gmul(C, A));
          C = diviuexact(muliu(C, j-u), u+1);
        }
        if (!odd(j)) S = gsub(S, gmul(C, gcoeff(a,i+1, j/2+1)));
      }
      gcoeff(a, i+1, j) = gc_upto(av, gdivgu(S, j));
    }
  }
  _1 = real_1(prec + EXTRAPREC64); av = avma;
  ze = cgetg(n+1, t_VEC);
  for (i = 1; i <= n; i++)
  {
    GEN S = gdivgu(gcoeff(a, n+2-i, 1), s), sj = utoipos(s);
    for (j = 2; j <= l; j++)
    {
      sj = muliu(sj, s); /* = s^j */
      S = gadd(S, gdiv(gcoeff(a, n+2-i, j), sj));
    }
    gel(ze, i) = S;
  }
  for (i = s; i >= 1; i--)
  {
    GEN b0 = divri(_1, powuu(i, avec[n])), z;
    z = gel(ze,n); gel(ze,n) = gadd(z, b0);
    for (j = n-1; j >= 1; j--)
    {
      b0 = gdiv(gadd(gmul(t, b0), z), powuu(i, avec[j]));
      z = gel(ze,j); gel(ze,j) = gadd(gel(ze,j), b0);
    }
    if (gc_needed(av, 1))
    {
      if (DEBUGMEM>1) pari_warn(warnmem,"zetamult: i = %ld", i);
      ze = gc_GEN(av, ze);
    }
  }
  return gel(ze, 1);
}

/********************************************************************/
/**                      AKHILESH ALGORITHM                        **/
/********************************************************************/
/* a t_VECSMALL, upper bound for -log2(zeta(a)) */
static long
log2zeta_bound(GEN a)
{ return ceil(-dbllog2(zetamult_Zagier(a, 32, LOWDEFAULTPREC))); }
/* ibin[n+1] = 1 / binom(2n, n) as a t_REAL */
static void
get_ibin(GEN *pibin, GEN *pibin1, long N, long prec)
{
  GEN ibin, ibin1;
  long n;
  *pibin = ibin = cgetg(N + 2, t_VEC);
  *pibin1= ibin1= cgetg(N + 2, t_VEC);
  gel(ibin,1) = gel(ibin1,1) = gen_0; /* unused */
  gel(ibin,2) = gel(ibin1,2) = real2n(-1,prec);
  for (n = 2; n <= N; n++)
  {
    GEN z = divru(gel(ibin, n), 4*n-2);
    gel(ibin1, n+1) = z;
    gel(ibin, n+1) = mulru(z, n);
  }
}
/**************************************************************/
/*                         ALL MZV's                          */
/**************************************************************/

/* Generalization to Multiple Polylogarithms.
The basic function is polylogmult which takes two arguments
avec, as above, and zvec, the vector of z_j, and the result
is $\sum_{n_1>n_2>...>n_r>0}z_1^{n_1}...z_r^{n_r}/(n_1^a_1...n_r^{a_r})$. */

/* Given admissible evec = xe_2....e_{k-1}y, (k>=2), compute a,b,v such that
evec = x{1}_{a-1}v{0}_{b-1}y with v empty or admissible.
Input: vector w=evec
Output: v=wmid, wini, wfin */
static void
findabvgen(GEN evec, long _0, long _1, GEN *pwmid, GEN *pwini, GEN *pwfin,
           long *pa, long *pb)
{
  long s = lg(evec) - 1, m, a, b, j, x = evec[1], y = evec[s];
  GEN wmid, wini, wfin;
  if (s == 2)
  {
    *pwmid = cgetg(1, t_VECSMALL);
    *pwini = mkvecsmall(x);
    *pwfin = mkvecsmall(y);
    *pa = *pb = 1; return;
  }
  a = s - 1;
  for (j = 1; j <= s - 2; j++) if (evec[j + 1] != _1) { a = j; break; }
  *pa = a;
  b = s - 1;
  for (j = s - 2; j >= 1; j--) if (evec[j + 1] != _0) { b = s - 1 - j; break; }
  *pb = b;

  *pwmid = wmid = a+b < s? vecslice(evec, a+1, s-b): cgetg(1, t_VECSMALL);
  m = lg(wmid) - 1;
  *pwini = wini = cgetg(a + m + 1, t_VECSMALL);
  wini[1] = x; for (j = 2; j <= a; j++) wini[j] = _1;
  for (; j <= a + m; j++) wini[j] = wmid[j-a];
  *pwfin = wfin = cgetg(b + m + 1, t_VECSMALL);
  for (j = 1; j <= m; j++) wfin[j] = wmid[j];
  for (; j < b + m; j++) wfin[j] = _0;
  wfin[j] = y;
}

/* y != 0,1 */
static GEN
filllg1(GEN ibin1, GEN r1, GEN y, long N, long prec)
{
  GEN v, y1 = gsubgs(gmulsg(2, y), 1), y3 = gmul(y, gsubsg(1, y));
  long n;

  v = cgetg(N + 2, t_VEC); gel(v, N + 1) = gen_0;
  if (gcmpgs(gnorm(y3),1) > 0)
  {
    GEN y2 = gdiv(r1, y3);
    for (n = N; n >= 1; n--)
    {
      pari_sp av2 = avma;
      GEN z = gmul(y2, gsub(gel(v, n+1), gmul(y1, gel(ibin1, n+1))));
      gel(v, n) = gc_upto(av2, z);
    }
  }
  else
  {
    pari_sp av0 = avma;
    gel(v, 1) = gc_upto(av0, glog(gdiv(y, gsubgs(y, 1)), prec));
    for (n = 1; n < N; n++)
    {
      pari_sp av2 = avma;
      GEN z = gadd(gmul(y3, gel(v, n)), gmul(y1, gel(ibin1, n+1)));
      gel(v, n + 1) = gc_upto(av2, z);
    }
  }
  return v;
}
static GEN
fillrec(hashtable *H, GEN evec, long _0, long _1, GEN X, GEN pab, GEN r1,
        long N)
{
  long n, a, b, s, x0;
  GEN xy1, x, y, r, wmid, wini, wfin, mid, ini, fin;
  hashentry *ep = hash_search(H, evec);

  if (ep) return (GEN)ep->val;
  findabvgen(evec, _0, _1, &wmid, &wini, &wfin, &a, &b);
  x = gel(X, evec[1]); s = lg(evec)-1; /* > 1 */
  y = gel(X, evec[s]);
  mid = fillrec(H, wmid, _0, _1, X, pab, r1, N);
  ini = fillrec(H, wini, _0, _1, X, pab, r1, N);
  fin = fillrec(H, wfin, _0, _1, X, pab, r1, N);
  if (evec[1] == _0) { x0 = 1; xy1 = gdiv(r1, y); }
  else { x0 = 0; xy1 = gdiv(r1, gmul(gsubsg(1, x), y)); }
  r = cgetg(N+2, t_VEC); gel(r, N+1) = gen_0;
  for (n = N; n > 1; n--)
  {
    pari_sp av = avma;
    GEN t = gmul(gel(ini, n+1), gmael(pab, n, a));
    GEN u = gadd(gmul(gel(fin, n+1), gmael(pab, n, b)), gel(mid, n+1));
    GEN v = gdiv(x0? gadd(t, u): gsub(t, u), gmael(pab, n, a+b));
    gel(r, n) = gc_upto(av, gmul(xy1, gadd(gel(r, n+1), v)));
  }
  { /* n = 1 */
    pari_sp av = avma;
    GEN t = gel(ini, 2), u = gadd(gel(fin, 2), gel(mid, 2));
    GEN v = x0? gadd(t, u): gsub(t, u);
    gel(r,1) = gc_upto(av, gmul(xy1, gadd(gel(r,2), v)));
  }
  hash_insert(H, (void*)evec, (void*)r); return r;
}

static GEN
aztoe(GEN avec, GEN zvec, long prec)
{
  GEN y, E, u = subsr(1, real2n(10-prec, LOWDEFAULTPREC));
  long i, l = lg(avec);

  E = cgetg(l, t_VEC); if (l == 1) return E;
  y = gen_1;
  for (i = 1; i < l; i++)
  {
    long a = avec[i];
    GEN e, zi = gel(zvec, i);
    if (a <= 0 || (a == 1 && i == 1 && gequal1(zi)))
      pari_err_TYPE("polylogmult [divergent]", avec);
    if (gequal0(zi)) return NULL;
    gel(E, i) = e = zerovec(a);
    gel(e, a) = y = gdiv(y, zi);
    if (gcmp(gnorm(y), u) < 0) pari_err_TYPE("polylogmult [divergent]", zvec);
  }
  return shallowconcat1(E);
}

/***********************************************************/
/* Special case of zvec = [1,1,...], i.e., zetamult.       */
/***********************************************************/
static void
findabvgens(GEN evec, GEN *pwmid, GEN *pwini, GEN *pwfin, long *pa, long *pb)
{
  GEN wmid, wini, wfin;
  long s = lg(evec) - 1, a, b, j, m;
  if (s == 2)
  {
    *pwmid = cgetg(1, t_VECSMALL);
    *pwini = mkvecsmall(0);
    *pwfin = mkvecsmall(1);
    *pa = *pb = 1; return;
  }
  a = s - 1;
  for (j = 1; j <= s - 2; j++) if (!evec[j + 1]) { a = j; break; }
  *pa = a;
  b = s - 1;
  for (j = s - 2; j >= 1; j--) if (evec[j + 1]) { b = s - 1 - j; break; }
  *pb = b;

  *pwmid = wmid = a+b < s? vecslice(evec, a+1, s-b): cgetg(1, t_VECSMALL);
  m = lg(wmid) - 1;
  *pwini = wini = cgetg(a + m + 1, t_VECSMALL);
  wini[1] = 0; for (j = 2; j <= a; j++) wini[j] = 1;
  for (; j <= a + m; j++) wini[j] = wmid[j-a];
  *pwfin = wfin = cgetg(b + m + 1, t_VECSMALL);
  for (j = 1; j <= m; j++) wfin[j] = wmid[j];
  for (; j < b + m; j++) wfin[j] = 0;
  wfin[j] = 1;
}
/* pab a ZV */
static GEN
fillrecs(hashtable *H, GEN evec, GEN pab, long N, long prec)
{
  long n, a, b, lz;
  GEN r, wmid, wini, wfin, mid, ini, fin;
  hashentry *ep = hash_search(H, evec);

  if (ep) return (GEN)ep->val;
  findabvgens(evec, &wmid, &wini, &wfin, &a, &b);
  mid = fillrecs(H, wmid, pab, N, prec);
  ini = fillrecs(H, wini, pab, N, prec);
  fin = fillrecs(H, wfin, pab, N, prec); /* t_VECs of t_REAL */
  r = cgetg(N + 1, t_VEC); gel(r,N) = real_0(prec); lz = nbits2lg(prec);
  for (n = N-1; n > 1; n--)
  {
    GEN z = cgetg(lz, t_REAL);
    pari_sp av = avma;
    GEN t = mulri(gel(ini, n+1), gmael(pab, n, a));
    GEN u = addrr(mulri(gel(fin, n+1), gmael(pab, n, b)), gel(mid,n+1));
    GEN v = divri(addrr(t, u), gmael(pab, n, a+b));
    affrr(addrr(v, gel(r, n+1)), z); set_avma(av); gel(r,n) = z;
  }
  { /* n = 1 */
    GEN z = cgetg(lz, t_REAL);
    pari_sp av = avma;
    GEN t = gel(ini,2), u = addrr(gel(fin,2), gel(mid,2)), v = addrr(t, u);
    affrr(addrr(gel(r, 2), v), z); set_avma(av); gel(r,1) = z;
  }
  hash_insert(H, (void*)evec, (void*)r); return r;
}
/* [n, ..., n^(l-1)] */
static GEN
powersu(ulong n, long l)
{
  GEN v = cgetg(l, t_VEC);
  long i;
  gel(v,1) = utoipos(n);
  for (i = 2; i < l; i++) gel(v,i) = muliu(gel(v,i-1), n);
  return v;
}
/* [2n, ..., (2n)^k] given [n, ..., n^k] */
static GEN
powersu2(GEN w)
{
  long i, l;
  GEN v = cgetg_copy(w, &l);
  for (i = 1; i < l; i++) gel(v,i) = shifti(gel(w,i), i);
  return v;
}
/* [2, ..., 2^(l-1)] */
static GEN
powers2(long l)
{
  GEN v = cgetg(l, t_VEC);
  long i;
  for (i = 1; i < l; i++) gel(v,i) = int2n(i);
  return v;
}
/* n^a = pab[n][a], N > 1 */
static GEN
get_pab(long N, long k)
{
  GEN v = cgetg(N+1, t_VEC);
  long j, l = k + 1;
  gel(v, 1) = gen_0; /* not needed */
  gel(v, 2) = powers2(l);
  for (j = 3; j <= N; j++)
    gel(v, j) = odd(j)? powersu(j, l): powersu2(gel(v, j>>1));
  return v;
}
static hashtable *
zetamult_hash(long _0, long _1, GEN ibin, GEN ibin1)
{
  hashtable *H = hash_create(4096, (ulong(*)(void*))&hash_zv,
                                   (int(*)(void*,void*))&zv_equal, 1);
  hash_insert(H, (void*)cgetg(1, t_VECSMALL), (void*)ibin);
  hash_insert(H, (void*)mkvecsmall(_0), (void*)ibin1);
  hash_insert(H, (void*)mkvecsmall(_1), (void*)ibin1); return H;
}
/* Akhilesh recursive algorithm, #a > 1;
 * prec final precision, bit required bitprecision */
static GEN
zetamult_Akhilesh(GEN a, long bit, long prec)
{
  GEN r, pab, ibin, ibin1, e = atoe(a);
  long k = lg(e) - 1, N = 1 + bit/2, prec2 = nbits2prec(bit);
  hashtable *H;

  get_ibin(&ibin, &ibin1, N, prec2);
  pab = get_pab(N, k);
  H = zetamult_hash(0, 1, ibin, ibin1);
  r = fillrecs(H, e, pab, lg(pab)-1, prec2);
  if (DEBUGLEVEL) err_printf("polylogmult: k = %ld, %ld nodes\n", k, H->nb);
  return gprec_wtrunc(gel(r,1), prec);
}

/* lump together close entries + round to 1 entries that are ~ 1 */
static GEN
vec_round(GEN V, long b)
{
  GEN v = shallowcopy(V), w = shallowcopy(v);
  long i, j, l = lg(v);
  for (i = 1; i < l; i++)
  {
    long e;
    if (!gel(v,i)) continue;
    if (gexpo(gsubgs(gel(v,i), 1)) < b) gel(w,i) = gel(v,i) = gen_1;
    e = gexpo(gel(v,i));
    for (j = i+1; j < l; j++)
      if (gel(v,j) && gexpo(gsub(gel(v,i), gel(v,j))) - e < b)
      {
        gel(v,j) = NULL;
        gel(w,j) = gel(w,i);
      }
  }
  return w;
}

/* evec t_VEC */
static GEN
zetamultevec(GEN evec, long prec)
{
  pari_sp av = avma;
  double *x, *y, z = 0;
  long b, i, j, l, lH, bitprec, prec2, N, _0 = 0, _1 = 0, k = lg(evec) - 1;
  GEN r1, r, pab, ibin, ibin1, X, Evec, v;
  hashtable *H;

  if (k == 0) return gen_1;
  evec = vec_round(evec, 3 - prec);
  v = vec_equiv(evec); l = lg(v);
  Evec = cgetg(k+1, t_VECSMALL);
  X = cgetg(l + 2, t_VEC); /* our alphabet */
  for (i = lH = 1; i < l; i++)
  {
    GEN vi = gel(v,i), xi = gel(evec, vi[1]);
    long li = lg(vi);
    gel(X,i) = xi;
    if (!_0 && gequal0(xi)) _0 = i;
    else if (!_1 && gequal1(xi)) _1 = i;
    for (j = 1; j < li; j++) Evec[vi[j]] = i;
  }
  /* add 0,1 if needed */
  if (!_0) { gel(X, i) = gen_0; _0 = i++; }
  if (!_1) { gel(X, i) = gen_1; _1 = i++; }
  l = i; setlg(X, l);
  av = avma;
  x = (double*)stack_malloc_align(l * sizeof(double), sizeof(double));
  y = (double*)stack_malloc_align(l * sizeof(double), sizeof(double));
  for (j = 1; j < l; j++)
  {
    GEN t = gel(X,j);
    x[j] = (j == _1)? 0: -dbllog2(gsubsg(1, t));
    y[j] = (j == _0)? 0: -dbllog2(t);
  }
  for (i = 1; i < l; i++)
    for (j = i+1; j < l; j++) z = maxdd(z, x[i] + y[j]);
  b = 0;
  for (i = 1; i < l; i++)
  {
    GEN t = real_i(gel(X,i));
    long e = -gexpo(gsubgs(gmul2n(t,1), 1));
    b = maxss(b, e);
  }
  set_avma(av);
  if (z >= 2) pari_err_IMPL("polylogmult in this range");
  bitprec = prec + 64*(1 + (k >> 5));
  N = 1 + bitprec / (2 - z);
  bitprec += z * N;
  prec2 = nbits2prec(bitprec + b);
  X = gprec_wensure(X, prec2);
  get_ibin(&ibin, &ibin1, N, prec2);
  pab = get_pab(N, k);
  H = zetamult_hash(_0, _1, ibin, ibin1);
  r1 = real_1(prec2);
  for (i = 1; i < l; i++)
    if (i != _0 && i != _1)
      hash_insert(H, mkvecsmall(i), filllg1(ibin1, r1, gel(X,i), N, prec2));
  r = fillrec(H, Evec, _0, _1, X, pab, r1, N);
  if (DEBUGLEVEL) err_printf("polylogmult: k = %ld, %ld nodes\n", k, H->nb);
  return gprec_wtrunc(gel(r,1), prec);
}

static int
use_Zagier(GEN a, long r, long prec)
{
  long k;
  if (prec <= 128) return 1;
  k = zv_sum(a);
  return (((double)r) / (k*k) * prec / log((double)10*prec) < 0.5);
}
/* a t_VECSMALL */
static GEN
zetamult_i(GEN a, long prec)
{
  long r = lg(a)-1;
  if (r == 0) return gen_1;
  if (r == 1) return szeta(a[1], prec);
  if (use_Zagier(a, r, prec))
    return zetamult_Zagier(a, prec, prec + EXTRAPREC64);
  return zetamult_Akhilesh(a, prec + maxss(log2zeta_bound(a), 64), prec);
}
GEN
zetamult(GEN s, long prec)
{
  pari_sp av0 = avma, av;
  GEN z, avec, r = cgetr(prec);

  av = avma; avec = zetamultconvert_i(s,1);
  if (lg(avec) == 1) return gc_const(av0, gen_1);
  z = zetamult_i(avec, prec); affrr(z, r); return gc_const(av, r);
}

/* If star = NULL: MZV, otherwise Yamamoto interpolation (MZSV for t=1) */
GEN
zetamult_interpolate(GEN s, GEN t, long prec)
{
  pari_sp av = avma, av2;
  long i, k, l, la;
  GEN avec, v, V;

  if (lg(s) == 1) return gen_1;
  if (!t) return zetamult(s, prec);
  avec = zetamultconvert_i(s, 1); k = zv_sum(avec);
  if (prec <= 128 || k > 20 || (prec >> k) < 4)
    return zetamult_interpolate2_i(vecsmall_reverse(avec), t, prec);
  v = allstar(avec); l = lg(v); la = lg(avec);
  V = cgetg(la, t_VEC);
  for (i = 1; i < la; i++) gel(V,i) = utor(0, prec + EXTRAPREC64);
  av2 = avma;
  for (i = 1; i < l; i++, set_avma(av2))
  {
    GEN a = gel(v,i); /* avec */
    long n = lg(a)-1; /* > 0 */
    affrr(addrr(gel(V,n), zetamult_i(a, prec)), gel(V,n));
  }
  return gc_upto(av, poleval(vecreverse(V),t));
}

GEN
polylogmult_interpolate(GEN a, GEN z, GEN t, long prec)
{
  pari_sp av = avma;
  GEN V, avec, A, AZ, Z;
  long i, la, l;
  switch(typ(a))
  {
    case t_VEC:
    case t_COL: a = gtovecsmall(a); break;
    case t_VECSMALL: break;
    default: pari_err_TYPE("polylogmult", a);
             return NULL;/*LCOV_EXCL_LINE*/
  }
  if (!z) return zetamult_interpolate(a, t, prec);
  switch (typ(z))
  {
    case t_INT: case t_FRAC: case t_REAL: case t_COMPLEX:
      z = mkvec(z); break;
    case t_VEC: case t_COL: break;
    case t_VECSMALL: z = zv_to_ZV(z); break;
    default: pari_err_TYPE("polylogmult [z]", z);
  }
  if (lg(z) != lg(a))
    pari_err_TYPE("polylogmult [#s != #z]", mkvec2(a,z));
  if (!t) return gc_GEN(av, zetamultevec(aztoe(a,z,prec), prec));
  avec = zetamultconvert_i(a, 1); la = lg(avec);
  AZ = allstar2(avec, z);
  A = gel(AZ, 1); l = lg(A);
  Z = gel(AZ, 2); V = zerovec(la-1);
  for (i = 1; i < l; i++)
  {
    pari_sp av2 = avma;
    GEN ai = gel(A,i), e = aztoe(ai, gel(Z,i), prec);
    long n = lg(ai)-1; /* > 0 */
    gel(V,n) = gc_upto(av2, gadd(gel(V,n), zetamultevec(e, prec)));
  }
  return gc_upto(av, poleval(vecreverse(V),t));
}

GEN
polylogmult(GEN a, GEN z, long prec)
{
  return polylogmult_interpolate(a, z, NULL, prec);
}

/**************************************************************/
/*                           ALL MZV's                        */
/**************************************************************/

/* Given admissible evec w = 0e_2....e_{k-1}1, compute a,b,v such that
 * w=0{1}_{b-1}v{0}_{a-1}1 with v empty or admissible.
 * Input: binary vector evec */
static void
findabv(GEN w, long *pa, long *pb, long *pmini, long *pmmid, long *pmfin)
{
  long le = lg(w) - 2;
  if (le == 0)
  {
    *pa = 1;
    *pb = 1;
    *pmini = 2;
    *pmfin = 2;
    *pmmid = 1;
  }
  else
  {
    long a, b, j, lv;
    for (j = 1; j <= le; j++)
      if (!w[j+1]) break;
    *pb = b = j;
    for (j = le; j >= 1; j--)
      if (w[j+1]) break;
    *pa = a = le + 1 - j;
    lv = le + 2 - a - b;
    if (lv > 0)
    {
      long v = fd(w, b + 1, le - a + 2), u = v + (1 << (lv-1));
      *pmini = (((1 << b) - 1) << (lv - 1)) + (v/2) + 2;
      *pmfin = (u << (a - 1)) + 2;
      *pmmid = (u >> 1) + 2;
    }
    else
    {
      *pmini = (1 << (b - 1)) + 1;
      *pmfin = (a == 1) ? 2 : (1 << (a - 2)) + 2;
      *pmmid = 1;
    }
  }
}

static GEN
veccgetr(long l, long prec)
{
  GEN v = cgetg(l, t_VEC);
  long i, lz = nbits2lg(prec);
  for (i = 1; i < l; i++) gel(v, i) = cgetg(lz, t_REAL);
  return v;
}
static GEN
_addrr(GEN x, GEN y) { return x? addrr(x,y): y; }

/* Returns L: we use the code w=0y1 <--> m=1y
* L[1] = zeta(emptyset)_{n-1,n-1},
* L[2] = zeta({0})_{n-1,n-1} = zeta({1})_{n-1,n-1} for n >= 2,
* L[m+2][n] = zeta(w)_{n-1,n-1}, w <--> m,n; 1 <= m < 2^{k-2}, 1 <= n <= N+1
* L[m+2] = zeta(w), w <--> m; 2^{k-2} <= m < 2^{k-1}.
* All entries are t_REAL */
static GEN
fillL(long k, GEN *pdual, long bitprec)
{
  long N = 1 + bitprec/2, prec = nbits2prec(bitprec);
  long s, m, K = 1 << (k - 1), K2 = K/2;
  GEN dual = zero_zv(K), pab = get_pab(N, k), L = cgetg(K + 2, t_VEC);

  get_ibin(&gel(L,1), &gel(L,2), N, prec);
  for (s = 2; s <= k; s++)
  { /* Assume length evec < s filled */
    /* If evec = 0e_2...e_{s-1}1 then m = (1e_2...e_{s-1})_2 */
    GEN w = cgetg(s, t_VECSMALL);
    long M = 1 << (s - 2);
    for (m = M; m < 2*M; m++)
    {
      GEN p1, pini, pfin, pmid;
      long n, j, a, b, mbar, mini, mfin, mmid, mc;
      pari_sp av;
      for (j = s - 1, mc = m, mbar = 1; j >= 2; j--, mc >>= 1)
      {
        w[j] = mc & 1;
        mbar = (1 - w[j]) | (mbar << 1);
      }
      if (mbar < m) /* m, mbar are dual; handle smallest, copy the other */
      { gel(L, m + 2) = gel(L, mbar + 2); dual[m] = mbar; continue; }

      gel(L, m + 2) = p1 = m < K2 ? veccgetr(N, prec): utor(0, prec);
      av = avma;
      findabv(w, &a,&b,&mini,&mmid,&mfin);
      pini = gel(L, mini);
      pfin = gel(L, mfin);
      pmid = gel(L, mmid);
      { /* n = N-1 */
        GEN S = NULL;
        if (mini < 3) S = _addrr(S, mulri(gel(pini, N), gmael(pab, N-1, b)));
        if (mfin < 3) S = _addrr(S, mulri(gel(pfin, N), gmael(pab, N-1, a)));
        if (mmid < 3) S = _addrr(S, gel(pmid, N));
        if (S) S = divri(S, gmael(pab, N-1, a+b));
        if (s == k)
          affrr(_addrr(S, p1), p1);
        else
        {
          if (!S) S = gen_0;
          affgr(S, gel(p1, N-1));
        }
      }
      for (n = N-2; n > 1; n--, set_avma(av))
      {
        GEN t = mulri(gel(pini, n+1), gmael(pab, n, b));
        GEN u = mulri(gel(pfin, n+1), gmael(pab, n, a));
        GEN v = gel(pmid, n+1), S = s < k ? gel(p1, n+1): p1;
        S = addrr(S, divri(addrr(addrr(t, u), v), gmael(pab, n, a+b)));
        affrr(S, s < k ? gel(p1, n) : p1);
      }
      { /* n = 1: same formula simplifies */
        GEN t = gel(pini,2), u = gel(pfin,2), v = gel(pmid,2);
        GEN S = s < k ? gel(p1,2): p1;
        S = addrr(S, addrr(addrr(t, u), v));
        affrr(S, s < k ? gel(p1,1) : p1);
      }
      set_avma(av);
    }
  }
  *pdual = dual; return L;
}

/* bit 1 of flag unset: full, otherwise up to duality (~ half)
 * bit 2 of flag unset: all <= k, otherwise only k
 * half: 2^(k-3)+ delta_{k even} * 2^(k/2-2), sum = 2^(k-2)+2^(floor(k/2)-1)-1
 * full: 2^(k-2); sum = 2^(k-1)-1 */
static GEN
zetamultall_i(long k, long flag, long prec)
{
  GEN res, ind, dual, L = fillL(k, &dual, prec + 32);
  long c, m, K2 = 1 << (k-2), n = lg(L) - 1, m0 = (flag & 4L) ? K2 : 1;

  if (!(flag & 2L))
  {
    res = cgetg(n - m0, t_VEC);
    ind = cgetg(n - m0, t_VECSMALL);
    for (m = m0, c = 1; m < n - 1; m++)
    {
      GEN z = gel(L,m+2);
      gel(res, c) = m < K2 ? gel(z,1): z;
      ind[c] = m; c++;
    }
  }
  else
  { /* up to duality */
    long c, N = k == 2? 1: (1 << (k - 1));
    res = cgetg(N + 1, t_VEC);
    ind = cgetg(N + 1, t_VECSMALL);
    for (m = m0, c = 1; m < n - 1; m++)
      if (!dual[m])
      {
        GEN z = gel(L,m+2);
        gel(res, c) = m < K2 ? gel(z,1): z;
        ind[c] = m; c++;
      }
    setlg(res, c);
    setlg(ind, c);
  }
  return mkvec2(res, ind);
}

/* fd(e, 2, lg(e)-2), e = atoe(avec) */
static long
atom(GEN avec)
{
  long i, m, l = lg(avec);
  if (l < 3) return 0;
  m = 1; /* avec[1] != 0 */
  for (i = 2; i < l-1; i++) m = (m << avec[i]) + 1;
  return m << (avec[i]-1);
}
static long
atoind(GEN avec, long flag)
{ return atom(avec) + (flag? 1: (1 << (zv_sum(avec) - 2))); }
/* If flag is unset, L has all k1 <= k, otherwise only k */
static GEN
zetamultstar_i(GEN L, GEN avec, long flag)
{
  GEN s = allstar(avec), S = gen_0;
  long i, l = lg(s);
  for (i = 1; i < l; i++) S = gadd(S, gel(L, atoind(gel(s,i), flag)));
  return S;
}

/* bit 0: notstar/star
 * bit 1: full/half (ignored if star, always full)
 * bit 2: all <= k / only k
 * bit 3: without / with index */
GEN
zetamultall(long k, long flag, long prec)
{
  pari_sp av = avma;
  GEN Lind, L, res;
  long K, k1, ct, fl;

  if (flag < 0 || flag > 15) pari_err_FLAG("zetamultall");
  if (k < 1) pari_err_DOMAIN("zetamultall", "k", "<", gen_1, stoi(k));
  if (k == 1) return cgetg(1, t_VEC);
  if (k >= 64) pari_err_OVERFLOW("zetamultall");
  if (!(flag & 1L))
  { /* not star */
    Lind = zetamultall_i(k, flag, prec);
    res = (flag & 8L)? Lind : gel(Lind, 1);
    return gc_GEN(av, res);
  }
  /* star */
  fl = flag & 4L; /* 4 if k, else 0 (all <= k) */
  Lind = gc_GEN(av, zetamultall_i(k, fl, prec)); /* full */
  L = gel(Lind, 1);
  K = 1 << (k - 2);
  res = cgetg(fl? K+1: 2*K, t_VEC);
  for (ct = 1, k1 = fl? k: 2; k1 <= k; k1++)
  {
    GEN w = cgetg(k1 + 1, t_VECSMALL);
    long M = 1 << (k1 - 1), m;
    for (m = 1; m <= M; m += 2)
    {
      pari_sp av = avma;
      long j, mc = m;
      for (j = k1; j >= 1; j--) { w[j] = mc & 1; mc >>= 1; }
      gel(res, ct++) = gc_upto(av, zetamultstar_i(L, etoa(w), fl));
    }
  }
  if (flag & 8L) res = mkvec2(res, gel(Lind, 2));
  return gc_GEN(av, res);
}
