/***************************************
 *
 * This file contains some examples for simplify_sum function
 *
 * Use:
 *   batch("rtest_simplify_sum.mac", test);
 *
 ***************************************/
(
  kill(all),
  load (simplify_sum),
  test_sum(sm, ass, use_gamma, smp, ans) := block(
    [product_use_gamma : use_gamma, cs, f],
    if length(ass)>0 then apply(assume, ass),
    cs : simplify_sum(sm),
    for f in smp do cs : apply(f, [cs]),
    if length(ass)>0 then apply(forget, ass),
    ratsimp(cs-ans)),
  0
);
0;

test_sum(
  sum(r/k*binomial(n,r)*binomial(m,k-r)/binomial(n+m,k),r,0,k),
  [n>k, m>k-1, m>k],
  true,
  [makefact, minfactorial],
  n/(n+m));
0;

test_sum(
  sum(binomial(n,k)/(k+1), k, 0, n),
  [],
  true,
  [],
  (2^(n+1)-1)/(n+1));
0;

test_sum(
  sum((-1)^k * binomial(a+b, a+k) * binomial(b+c, b+k) * binomial(c+a, c+k), k, -a, a),
  [a<b, a<c],
  true,
  [factcomb],
  (c+b+a)!/(a!*b!*c!));
0;

test_sum(
  sum((-1)^k * binomial(n,k)*x/(x+k), k, 0, n),
  [],
  true,
  [factcomb],
  (n!*x!)/(x+n)!);
0;

test_sum(
  sum((-1)^k * binomial(2*n, k)^2, k, 0, 2*n),
  [],
  false,
  [],
  ((product(2*%j1-1,%j1,1,n))*(-1)^n*2^n)/n!);
0;

test_sum(
  sum((binomial(3*k+1,k)*binomial(3*(n-k),n-k))/(3*k+1), k, 0, n),
  [],
  false,
  [],
  ((product(3*%j1-1,%j1,1,n))*(product(3*%j1+1,%j1,1,n))*3^n)/((product(2*%j1+1,%j1,1,n))*2^n*n!));
0;

test_sum(
  sum(binomial(n, k) * binomial(m, r - k), k, 0, r),
  [m>r, n>r],
  true,
  [factcomb],
  (n+m)!/((-r+n+m)!*r!));
0;

test_sum(
  sum(binomial(2*n,2*k), k, 0, n),
  [n>0],
  true,
  [],
  2^(2*n)/2);
0;

test_sum(
  sum((-1)^k*binomial(x-2*k,n-k)*binomial(x-k+1,k),k,0,n),
  [x>2*n, x>2*n-1, x>n-1],
  true,
  [],
  ((-1)^n+1)/2);
0;

test_sum(
  sum((-1)^s*binomial(2*m, s)^3, s, 0, 2*m),
  [],
  false,
  [],
  (product(3*%j1-2,%j1,1,m)*product(3*%j1-1,%j1,1,m)*(-1)^m*3^m)/m!^2);
0;

test_sum(
  sum((-1)^k*binomial(k,n-k), k, 0, n),
  [],
  true,
  [rectform, trigreduce],
  (3*cos((2*%pi*n)/3)-sqrt(3)*sin((2*%pi*n)/3))/3);
0;

test_sum(
  sum(binomial(n,k)^2*binomial(3*n+k,2*n), k, -inf, inf),
  [n>0],
  false,
  [],
  (product(3*%j1-2,%j1,1,n))^2*(product(3*%j1-1,%j1,1,n))^2*9^n/((product(2*%j1-1,%j1,1,n))^2*4^n*n!^2));
0;

test_sum(
  sum((-1)^k*binomial(n-k,k)*2^(n-2*k), k, 0, n),
  [],
  true,
  [],
  n+1);
0;

test_sum(
  sum(binomial(x,k)*binomial(y,k), k, 0, y),
  [x>y],
  true,
  [factcomb],
  (y+x)!/(x!*y!));
0;

test_sum(
  sum((-1)^(n-k)*binomial(n,k)*binomial(k+b,k), k, 0, n),
  [b>0],
  true,
  [factcomb],
  b!/((b-n)!*n!));
0;

test_sum(
  sum(binomial(n+k,k)/2^k,k,0,n),
  [],
  true,
  [],
  2^n);
0;

test_sum(
  sum((2^(4*k)*binomial(2*n-2*k,n-k))/(2*k*(2*k+1)*binomial(2*k,k)), k, 1, n),
  [],
  true,
  [],
  -(4*binomial(-2*(1-n),n-1)*(1-2*n))/(2*n+1));
0;

test_sum(
  sum(k^2 * binomial(n, k), k, 1, n),
  [],
  true,
  [],
  (n+n^2)*2^n/4);
0;

test_sum(
  sum(2^(n-k)*binomial(n+k,2*k),k,-inf,inf),
  [n>0],
  true,
  [],
  (2*4^n+1)/3);
0;

test_sum(
  sum(((-1)^k*binomial(2*k,k)*binomial(n,k))/4^k,k,0,n),
  [],
  true,
  [],
  ((2*n-1)/2)!/(sqrt(%pi)*n!));
0;

test_sum(
  sum((-1)^k*binomial(2*k,k)*binomial(2*n,k)*binomial(4*n-2*k,2*n-k),k,0,2*n),
  [],
  true,
  [],
  (4^n*2^(2*n)*((2*n-1)/2)!^2)/(%pi*n!^2));
0;

test_sum(
  sum(binomial(l+j,j)*(-1)^(l+2*j)*binomial(n+1,l+j+1),l,0,n-j),
  [j>1],
  true,
  [],
  1);
0;

test_sum(
  sum(binomial(n,4*k),k,0,inf),
  [],
  true,
  [rectform],
  (2*2^(n/2)*cos((%pi*n)/4)+2^n)/4
  ), sum_min=1;
0;

(declare(n, integer), load(orthopoly), 0);
0;

test_sum(
  niceindices(sum(binomial(n, k)/binomial(2*n, k)/k!*(2*x)^k, k, 0, inf)),
  [n>0],
  true,
  [lambda([u], niceindices(ev(u, gen_laguerre)))],
  niceindices((-1)^n*gen_laguerre(n,-2*n-1,2*x)/binomial(2*n,n))
  );
0;

(kill(n), 0);
0;

test_sum(
  sum(binomial(n,i)*p^i*(1-p)^(n-i),i,0,n),
  [],
  true,
  [],
  1);
0;

test_sum(
  sum(binomial(n,k)*binomial(k-1,j)*(-1)^(k-j-1), k, j+1, n),
  [j>1],
  true,
  [],
  1);
0;

test_sum(
  sum((n^2+n+1)/(n^4-2*n^2+1),n,2,inf),
  [],
  true,
  [ratsimp, multthru],
  %pi^2/6-5/16);
0;

test_sum(
  sum(1/n/(2*n+1),n,1,inf),
  [],
  true,
  [ratsimp, multthru],
  2-2*log(2));
0;

test_sum(
  sum(1/(5*n^2-1),n,1,inf),
  [],
  true,
  [ratsimp, multthru],
  (sqrt(5)*harmonic_number(1/sqrt(5))-sqrt(5)*harmonic_number(-1/sqrt(5)))/10);
0;

test_sum(
  sum(1/(5*n^2-3),n,1,inf),
  [],
  true,
  [radcan],
  (sqrt(3)*sqrt(5)*harmonic_number((sqrt(3)/sqrt(5)))-sqrt(3)*sqrt(5)*harmonic_number(-(sqrt(3)/sqrt(5))))/30);
0;

test_sum(
  sum(1/(n^2+1),n,1,inf),
  [],
  true,
  [ratsimp, multthru],
  -(%i*harmonic_number(%i)-%i*harmonic_number(-%i))/2);
0;

test_sum(
  sum(1/n/(n+a), n, 1, inf),
  [],
  true,
  [ratsimp, multthru],
  harmonic_number(a)/a);
0;

test_sum(
  sum(1/(5*k+1)^2,k,2,inf),
  [],
  false,
  [],
  (psi[1](1/5)-925/36)/25);
0;

test_sum(
  sum((-1)^(n+1)/n, n, 1, inf),
  [],
  true,
  [ratsimp, multthru],
  log(2));
0;

test_sum(
  sum((-1)^k*(k+5)/(2*k+1)/(2*k+3), k, 1, inf),
  [],
  true,
  [ratsimp, multthru],
  %pi-41/12);
0;

test_sum(
  sum((-1)^(k+1)/(2*k-1)^3, k, 1, inf),
  [],
  true,
  [ratsimp, multthru],
  %pi^3/32);
0;

test_sum(
  sum(x^n/n!, n, 0, inf),
  [],
  false,
  [],
  %e^x
)$
0;

test_sum(
  -sum(((-1)^i*x^(2*i))/i,i,1,inf),
  [],
  false,
  [],
  log(x^2+1)
)$
0;

test_sum(
  sum(n/((n+1)*2^n),n,1,inf),
  [],
  false,
  [ratsimp],
  2-2*log(2)
)$
0;

test_sum(
  sum(n/((2*n+1)*2^n),n,0,inf),
  [],
  false,
  [ratsimp],
  -(sqrt(2)*atanh(1/sqrt(2))-2)/2
)$
0;

test_sum(
  sum((4^(-i-1)*(-1)^i*x^(i+1))/(i+1),i,0,inf),
  [],
  false,
  [],
  log(x/4+1)
)$
0;

test_sum(
  'sum('sum(n!/(k!^2*(n - 2*k)!)*(x/y)^k*(x*y)^(n - k), n, 2*k, inf), k, 0, inf),
  [x*y<1],
  false,
  [lambda([u], scanmap(factor, u, 'bottomup))],
  1/sqrt((x*y-2*x-1)*(x*y+2*x-1))
), use_zeilberger=false$
0;

test_sum(
  sum(1/k, k, 1, n),
  [],
  false,
  [],
  harmonic_number(n))$
0;

test_sum(
  sum(k*harmonic_number(k),k,1,n),
  [],
  false,
  [ratsimp],
  ((n^2+n)*(harmonic_number(n)+1/(n+1)))/2-(n^2+n)/4);
0;

test_sum(
  sum(k^2*harmonic_number(k),k,1,n),
  [],
  false,
  [ratsimp],
  ((12*n^3+18*n^2+6*n)*harmonic_number(n)-4*n^3+3*n^2+n)/36);
0;

test_sum(
  sum(k^2*harmonic_number(n+k),k,1,n),
  [],
  false,
  [ratsimp],
  ((24*n^3+36*n^2+12*n)*harmonic_number(2*n)+(-12*n^3-18*n^2-6*n)*harmonic_number(n)-10*n^3-9*n^2+n)/36),
  harmonic_number_expand=true;
0;

declare(N,integer);
done;

test_sum(
  sum(sin(n*x), n, 1, N),
  [],
  false,
  [],
  -((cos(x)-1)*sin(x*N+x)-sin(x)*cos(x*N+x)+sin(x))/(2*cos(x)-2));
0;

remove(N,integer);
done;

test_sum(
  sum(((-4)^k*binomial(k,m)*binomial(n+k,2*k))*n/(n+k),k,m,n),
  [m>1, n>0],
  false,
  [factcomb, radcan],
  (2^(2*m)*n*(-1)^n*(n+m-1)!)/((2*m)!*(n-m)!)), sum_min=m;
0;

test_sum(
  sum(harmonic_number(n)/2^n, n, 1, inf),
  [],
  false,
  [],
  2*log(2));
0;

test_sum(
  sum(harmonic_number(n)/n/(n+1), n, 1, inf),
  [],
  false,
  [],
  %pi^2/6);
0;


test_sum(
  sum(n*harmonic_number(n)/2^n, n, 1, inf),
  [],
  false,
  [],
  2*log(2)+2
);
0;

test_sum(
  sum(harmonic_number(n)/n/2^n, n, 1, inf),
  [],
  false,
  [rectform],
  %pi^2/12
), sum_by_integral_transforms=[logcontract];
0;


test_sum(
  sum(n^2*harmonic_number(2*n)/2^n, n, 1, inf),
  [],
  false,
  [],
  -(17*sqrt(2)*log(3-2*sqrt(2))-24*log(2)-68)/8
);
0;

test_sum(
  sum(((x-1)^(2*n+1)*(x+1)^(-2*n-1))/(2*n+1),n,0,inf),
  [x<1, x>0],
  false,
  [rootscontract, lambda([u], scanmap(factor, u)), ratsimp],
  log(x)/2
);
0;

test_sum(
  sum(-1/(2*k)+1/(2*2*k+1)+1/(2*2*k+3), k, 1, inf),
  [],
  false,
  [ratsimp],
  (9*log(2)-8)/6
);
0;

test_sum(
  sum(log((2*k+1)/(2*k-1)), k, 1, n),
  [],
  false,
  [ratsimp],
  log(2*n+1)
);
0;

test_sum(
  sum(k*(k/2)!,k,1,n),
  [],
  false,
  [],
  2*(n/2+1)!+2*((n-1)/2+1)!-sqrt(%pi)-2
);
0;

test_sum(
  sum(i*fib(i-1)/2^i,i,1,inf),
  [],
  false,
  [],
  6
);
0;

test_sum(
  sum(1/(sqrt(k*(k + 1)) * (sqrt(k) + sqrt(k + 1))), k, 1, inf),
  [],
  false,
  [],
  1
);
0;

block([gamma_expand: true],
test_sum(
  sum(%e^-4*(k-4)^2*4^k/k!, k, 0, inf),
  [],
  false,
  [],
  4
));
0;


block([gamma_expand: true],
test_sum(
  sum(%e^-4*(k-5)^2*4^k/k!, k, 0, inf),
  [],
  false,
  [],
  5
));
0;

test_sum(
  sum(k^2*binomial(n,k), k, 0, n),
  [],
  false,
  [],
  2^(-2+n)*n*(1 + n)
  );
0;

test_sum(
  sum(k^2*binomial(n,k), k, 0, n),
  [],
  false,
  [],
  2^(-2+n)*n*(1 + n)
  );
0;

test_sum(
  sum(k^2*binomial(n,k), k, 1, n),
  [],
  false,
  [],
  2^(-2+n)*n*(1 + n)
  );
0;

test_sum(
  sum(k^2*binomial(n,k), k, 2, n),
  [],
  false,
  [],
  1/4*n*(-4+2^n+2^n*n)
  );
0;

test_sum(
  sum(k^2*binomial(n,k), k, 3, n),
  [],
  false,
  [],
  1/4*n*(4+2^n-8*n+2^n*n)
  );
0;


test_sum(
  sum(binomial(n,k)*binomial(k-1,j)*(-1)**(k-1-j),k,j+1,n),
  [j>1],
  false,
  [],
  1
  );
0;

/* SF bug #3236: "bug in simplifying infinite sum with 1st (0th) negative term" */

test_sum(
  sum(1/((n+1)*(2*n-1)), n, 1,inf),
  [],
  false,
  [],
  (2*log(2)+1)/3);
0;

test_sum(
  sum(1/((2*n-1)^2*(2*n+1)^2*(2*n+3)^2),n,0,inf),
  [],
  false,
  [],
  (3*%pi^2)/256);
0;

ratsimp (simplify_sum(sum(1/((n+1)*(2*n-1)), n, 1,inf)) -simplify_sum(sum(1/((n+1)*(2*n-1)), n, 0,inf)));
1;

ratsimp (simplify_sum(sum(1/((2*n+1)^2-4)^2, n, 0, inf)));
1/64*%pi^2;

/* SF bug #3521: "simplify_sum(sum(binomial(1,n),n,0,inf)) gives 3 instead of 2" */

simplify_sum(sum(binomial(1,n),n,0,inf));
2;

foo: makelist (simplify_sum(sum(binomial(1,n),n,n0,inf)), n0, 0, 10);
[2, 1, 'sum(binomial(1,n),n,2,inf), 'sum(binomial(1,n),n,3,inf), 'sum(binomial(1,n),n,4,inf),
 'sum(binomial(1,n),n,5,inf), 'sum(binomial(1,n),n,6,inf), 'sum(binomial(1,n),n,7,inf),
 'sum(binomial(1,n),n,8,inf), 'sum(binomial(1,n),n,9,inf), 'sum(binomial(1,n),n,10,inf)];

/* binomial(mm, nn) = 0 for nn - mm an integer and nn > mm.
 * This rule seems to interfere with extended_nusum (causing extended_nusum
 * to return 0 instead of 1 for 'sum(binomial(0, n), n, 0, inf)), so let's not worry about
 * making this a general rule.
 */
(matchdeclare (mm, all, nn, lambda ([e], featurep (e - mm, integer) and e > mm)),
 myrule: first (tellsimpafter (binomial (mm, nn), 0)),
 ev (foo, nouns)); /* expand(foo, 0, 0) doesn't have any effect. dunno what's going on. */
[2, 1, 0, 0, 0,  0, 0, 0, 0, 0,  0];

remrule (binomial, myrule);
binomial;

makelist (simplify_sum(sum(binomial(m, n), n, 0, inf)), m, 0, 10);
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024];

(reset(verbose,linsolvewarn),0);
0$
