1 /***************************************
3 * This file contains some examples for simplify_sum function
6 * batch("rtest_simplify_sum.mac", test);
8 ***************************************/
12 test_sum(sm, ass, use_gamma, smp, ans) := block(
13 [product_use_gamma : use_gamma, cs, f],
14 if length(ass)>0 then apply(assume, ass),
15 cs : simplify_sum(sm),
16 for f in smp do cs : apply(f, [cs]),
17 if length(ass)>0 then apply(forget, ass),
24 sum(r/k*binomial(n,r)*binomial(m,k-r)/binomial(n+m,k),r,0,k),
27 [makefact, minfactorial],
32 sum(binomial(n,k)/(k+1), k, 0, n),
40 sum((-1)^k * binomial(a+b, a+k) * binomial(b+c, b+k) * binomial(c+a, c+k), k, -a, a),
48 sum((-1)^k * binomial(n,k)*x/(x+k), k, 0, n),
56 sum((-1)^k * binomial(2*n, k)^2, k, 0, 2*n),
60 ((product(2*%j1-1,%j1,1,n))*(-1)^n*2^n)/n!);
64 sum((binomial(3*k+1,k)*binomial(3*(n-k),n-k))/(3*k+1), k, 0, n),
68 ((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!));
72 sum(binomial(n, k) * binomial(m, r - k), k, 0, r),
76 (n+m)!/((-r+n+m)!*r!));
80 sum(binomial(2*n,2*k), k, 0, n),
88 sum((-1)^k*binomial(x-2*k,n-k)*binomial(x-k+1,k),k,0,n),
89 [x>2*n, x>2*n-1, x>n-1],
96 sum((-1)^s*binomial(2*m, s)^3, s, 0, 2*m),
100 (product(3*%j1-2,%j1,1,m)*product(3*%j1-1,%j1,1,m)*(-1)^m*3^m)/m!^2);
104 sum((-1)^k*binomial(k,n-k), k, 0, n),
107 [rectform, trigreduce],
108 (3*cos((2*%pi*n)/3)-sqrt(3)*sin((2*%pi*n)/3))/3);
112 sum(binomial(n,k)^2*binomial(3*n+k,2*n), k, -inf, inf),
116 (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));
120 sum((-1)^k*binomial(n-k,k)*2^(n-2*k), k, 0, n),
128 sum(binomial(x,k)*binomial(y,k), k, 0, y),
136 sum((-1)^(n-k)*binomial(n,k)*binomial(k+b,k), k, 0, n),
144 sum(binomial(n+k,k)/2^k,k,0,n),
152 sum((2^(4*k)*binomial(2*n-2*k,n-k))/(2*k*(2*k+1)*binomial(2*k,k)), k, 1, n),
156 -(4*binomial(-2*(1-n),n-1)*(1-2*n))/(2*n+1));
160 sum(k^2 * binomial(n, k), k, 1, n),
168 sum(2^(n-k)*binomial(n+k,2*k),k,-inf,inf),
176 sum(((-1)^k*binomial(2*k,k)*binomial(n,k))/4^k,k,0,n),
180 ((2*n-1)/2)!/(sqrt(%pi)*n!));
184 sum((-1)^k*binomial(2*k,k)*binomial(2*n,k)*binomial(4*n-2*k,2*n-k),k,0,2*n),
188 (4^n*2^(2*n)*((2*n-1)/2)!^2)/(%pi*n!^2));
192 sum(binomial(l+j,j)*(-1)^(l+2*j)*binomial(n+1,l+j+1),l,0,n-j),
200 sum(binomial(n,4*k),k,0,inf),
204 (2*2^(n/2)*cos((%pi*n)/4)+2^n)/4
208 (declare(n, integer), load(orthopoly), 0);
212 niceindices(sum(binomial(n, k)/binomial(2*n, k)/k!*(2*x)^k, k, 0, inf)),
215 [lambda([u], niceindices(ev(u, gen_laguerre)))],
216 niceindices((-1)^n*gen_laguerre(n,-2*n-1,2*x)/binomial(2*n,n))
224 sum(binomial(n,i)*p^i*(1-p)^(n-i),i,0,n),
232 sum(binomial(n,k)*binomial(k-1,j)*(-1)^(k-j-1), k, j+1, n),
240 sum((n^2+n+1)/(n^4-2*n^2+1),n,2,inf),
248 sum(1/n/(2*n+1),n,1,inf),
256 sum(1/(5*n^2-1),n,1,inf),
260 (sqrt(5)*harmonic_number(1/sqrt(5))-sqrt(5)*harmonic_number(-1/sqrt(5)))/10);
264 sum(1/(5*n^2-3),n,1,inf),
268 (sqrt(3)*sqrt(5)*harmonic_number((sqrt(3)/sqrt(5)))-sqrt(3)*sqrt(5)*harmonic_number(-(sqrt(3)/sqrt(5))))/30);
272 sum(1/(n^2+1),n,1,inf),
276 -(%i*harmonic_number(%i)-%i*harmonic_number(-%i))/2);
280 sum(1/n/(n+a), n, 1, inf),
284 harmonic_number(a)/a);
288 sum(1/(5*k+1)^2,k,2,inf),
292 (psi[1](1/5)-925/36)/25);
296 sum((-1)^(n+1)/n, n, 1, inf),
304 sum((-1)^k*(k+5)/(2*k+1)/(2*k+3), k, 1, inf),
312 sum((-1)^(k+1)/(2*k-1)^3, k, 1, inf),
320 sum(x^n/n!, n, 0, inf),
329 -sum(((-1)^i*x^(2*i))/i,i,1,inf),
338 sum(n/((n+1)*2^n),n,1,inf),
347 sum(n/((2*n+1)*2^n),n,0,inf),
351 -(sqrt(2)*atanh(1/sqrt(2))-2)/2
356 sum((4^(-i-1)*(-1)^i*x^(i+1))/(i+1),i,0,inf),
365 'sum('sum(n!/(k!^2*(n - 2*k)!)*(x/y)^k*(x*y)^(n - k), n, 2*k, inf), k, 0, inf),
368 [lambda([u], scanmap(factor, u, 'bottomup))],
369 1/sqrt((x*y-2*x-1)*(x*y+2*x-1))
370 ), use_zeilberger=false$
382 sum(k*harmonic_number(k),k,1,n),
386 ((n^2+n)*(harmonic_number(n)+1/(n+1)))/2-(n^2+n)/4);
390 sum(k^2*harmonic_number(k),k,1,n),
394 ((12*n^3+18*n^2+6*n)*harmonic_number(n)-4*n^3+3*n^2+n)/36);
398 sum(k^2*harmonic_number(n+k),k,1,n),
402 ((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),
403 harmonic_number_expand=true;
410 sum(sin(n*x), n, 1, N),
414 -((cos(x)-1)*sin(x*N+x)-sin(x)*cos(x*N+x)+sin(x))/(2*cos(x)-2));
421 sum(((-4)^k*binomial(k,m)*binomial(n+k,2*k))*n/(n+k),k,m,n),
425 (2^(2*m)*n*(-1)^n*(n+m-1)!)/((2*m)!*(n-m)!)), sum_min=m;
429 sum(harmonic_number(n)/2^n, n, 1, inf),
437 sum(harmonic_number(n)/n/(n+1), n, 1, inf),
446 sum(n*harmonic_number(n)/2^n, n, 1, inf),
455 sum(harmonic_number(n)/n/2^n, n, 1, inf),
460 ), sum_by_integral_transforms=[logcontract];
465 sum(n^2*harmonic_number(2*n)/2^n, n, 1, inf),
469 -(17*sqrt(2)*log(3-2*sqrt(2))-24*log(2)-68)/8
474 sum(((x-1)^(2*n+1)*(x+1)^(-2*n-1))/(2*n+1),n,0,inf),
477 [rootscontract, lambda([u], scanmap(factor, u)), ratsimp],
483 sum(-1/(2*k)+1/(2*2*k+1)+1/(2*2*k+3), k, 1, inf),
492 sum(log((2*k+1)/(2*k-1)), k, 1, n),
505 2*(n/2+1)!+2*((n-1)/2+1)!-sqrt(%pi)-2
510 sum(i*fib(i-1)/2^i,i,1,inf),
519 sum(1/(sqrt(k*(k + 1)) * (sqrt(k) + sqrt(k + 1))), k, 1, inf),
527 block([gamma_expand: true],
529 sum(%e^-4*(k-4)^2*4^k/k!, k, 0, inf),
538 block([gamma_expand: true],
540 sum(%e^-4*(k-5)^2*4^k/k!, k, 0, inf),
549 sum(k^2*binomial(n,k), k, 0, n),
558 sum(k^2*binomial(n,k), k, 0, n),
567 sum(k^2*binomial(n,k), k, 1, n),
576 sum(k^2*binomial(n,k), k, 2, n),
585 sum(k^2*binomial(n,k), k, 3, n),
589 1/4*n*(4+2^n-8*n+2^n*n)
595 sum(binomial(n,k)*binomial(k-1,j)*(-1)**(k-1-j),k,j+1,n),
603 /* SF bug #3236: "bug in simplifying infinite sum with 1st (0th) negative term" */
606 sum(1/((n+1)*(2*n-1)), n, 1,inf),
614 sum(1/((2*n-1)^2*(2*n+1)^2*(2*n+3)^2),n,0,inf),
621 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)));
624 ratsimp (simplify_sum(sum(1/((2*n+1)^2-4)^2, n, 0, inf)));
627 /* SF bug #3521: "simplify_sum(sum(binomial(1,n),n,0,inf)) gives 3 instead of 2" */
629 simplify_sum(sum(binomial(1,n),n,0,inf));
632 foo: makelist (simplify_sum(sum(binomial(1,n),n,n0,inf)), n0, 0, 10);
633 [2, 1, 'sum(binomial(1,n),n,2,inf), 'sum(binomial(1,n),n,3,inf), 'sum(binomial(1,n),n,4,inf),
634 'sum(binomial(1,n),n,5,inf), 'sum(binomial(1,n),n,6,inf), 'sum(binomial(1,n),n,7,inf),
635 'sum(binomial(1,n),n,8,inf), 'sum(binomial(1,n),n,9,inf), 'sum(binomial(1,n),n,10,inf)];
637 /* binomial(mm, nn) = 0 for nn - mm an integer and nn > mm.
638 * This rule seems to interfere with extended_nusum (causing extended_nusum
639 * to return 0 instead of 1 for 'sum(binomial(0, n), n, 0, inf)), so let's not worry about
640 * making this a general rule.
642 (matchdeclare (mm, all, nn, lambda ([e], featurep (e - mm, integer) and e > mm)),
643 myrule: first (tellsimpafter (binomial (mm, nn), 0)),
644 ev (foo, nouns)); /* expand(foo, 0, 0) doesn't have any effect. dunno what's going on. */
645 [2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0];
647 remrule (binomial, myrule);
650 makelist (simplify_sum(sum(binomial(m, n), n, 0, inf)), m, 0, 10);
651 [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024];
653 (reset(verbose,linsolvewarn),0);
656 /* SF bug #3462: "simplify_sum() variable name clash" */
661 simplify_sum(sum((2*i+1)^-2, i, 0, inf));
664 (a[i] := i, simplify_sum(sum((2*i+1)^-2, i, 0, inf)));
667 (kill(a), simplify_sum(sum((2*i+1)^-2, i, 0, inf)));