4 (assume_pos_save: assume_pos, assume_pos: true);
7 /* Verify that we at least get errors when passing the wrong number
8 * of arguments to sum and product.
10 * The ev tests are significant. In the 2 and 3 arg cases, we used
11 * to get bogus results. In the case of more than 4 args, the extra
12 * args used to be effectively ignored. (In the 0 and 1 arg cases,
13 * we got obscure error messages about assigning to false. The tests
14 * below don't do any checking about *why* we got an error.)
20 errcatch (ev (sum ()));
23 errcatch (sum (f (x)));
26 errcatch (ev (sum (f (x))));
29 errcatch (sum (f (x), x));
32 errcatch (ev (sum (f (x), x)));
35 errcatch (sum (f (x), x, 0));
38 errcatch (ev (sum (f (x), x, 0)));
41 errcatch (sum (f (x), x, 0, 1, 2));
44 errcatch (ev (sum (f (x), x, 0, 1, 2)));
47 errcatch (product ());
50 errcatch (ev (product ()));
53 errcatch (product (f (x)));
56 errcatch (ev (product (f (x))));
59 errcatch (product (f (x), x));
62 errcatch (ev (product (f (x), x)));
65 errcatch (product (f (x), x, 1));
68 errcatch (ev (product (f (x), x, 1)));
71 errcatch (product (f (x), x, 1, 2, 3));
74 errcatch (ev (product (f (x), x, 1, 2, 3)));
77 /* Known failure: concat (q, k) => gensym
79 sum (concat (q, k), k, a, b);
82 /* Known failure: concat (q, k) => gensym
84 product (concat (q, k), k, a, b);
87 /* Known failure: causes asksign to ask an unanswerable question about index variable;
88 * this is a bug in integrate and/or asksign.
89 (assume (b > 0, a > 0, b > a), sum (integrate (1/x^k, x, a, b), k, 1, n));
90 integrate (1/x, x, a, b) + sum (integrate (1/x^k, x, a, b), k, 2, n);
93 /* Known failure: causes asksign to ask an unanswerable question about index variable;
94 * this is a bug in integrate and/or asksign.
95 product (integrate (1/x^k, x, a, b), k, 1, n);
96 integrate (1/x, x, a, b) * product (integrate (1/x^k, x, a, b), k, 2, n);
99 block ([prederror: false], sum(if k < 1 then a else b,k,0,n));
100 '( 'sum(if k < 1 then a else b,k,0,n));
102 /* see SF bug report # 625278 */
104 'sum(binomial(2,2-k)-binomial(2,1-k),k,1,2),simpsum;
107 'sum(binomial(2,2-k)-binomial(2,1-k),k,1,2),sum;
110 'sum(binomial(x,2-k)-binomial(x,1-k),k,1,2),simpsum;
113 'sum(binomial(x,2-k)-binomial(x,1-k),k,1,2),sum;
116 sum (f(k) + 1, k, 1, n), simpsum;
117 n + 'sum (f(k), k, 1, n);
119 /* further examples in the same vein: part of summand depends on index, part doesn't. */
121 sum (sin(g(j)) - %pi^j + cos(h(k)) - %e^l, j, 1, m), simpsum;
122 m*cos(h(k)) - m*%e^l + sum (sin(g(j)), j, 1, m) - ((%pi^(m + 1) - %pi)/(%pi - 1));
124 sum (sum (f(j) + g(k), j, 1, m), k, 1, n), simpsum;
125 m * 'sum (g(k), k, 1, n) + n * 'sum (f(j), j, 1, m);
127 sum (sum (f(j) + g(k) - %pi^l + 2, j, 1, m), k, 1, n), simpsum;
128 m * 'sum (g(k), k, 1, n) + n * 'sum (f(j), j, 1, m) - m*n*%pi^l + 2*m*n;
130 sum (sum (sum (%pi + %phi - 1, i, 1, n_i), j, 1, n_j), k, 1, n_k);
131 n_i * n_j * n_k * (%pi + %phi - 1);
133 sum (sum (sum (f(%pi, i) + g(%phi, j) - h(k), i, 1, n_i), j, 1, n_j), k, 1, n_k);
134 'sum ('sum ('sum (f(%pi, i) + g(%phi, j) - h(k), i, 1, n_i), j, 1, n_j), k, 1, n_k);
136 /* see SF bug report # 649428 */
138 (factsum3 (mt, ej) := sum ((-1)^(k + 1)/(k*mt^k)*sum((1 - l)^k - l^k, l, 1, ej),k,1,inf), t% : taylor (factsum3 (mt, ej), [mt, 0, 3, asymp]));
139 'sum(1-2*l,l,1,ej)/mt-('sum(1-2*l,l,1,ej)/(2*mt^2))+'sum(-2*l^3+3*l^2-3*l+1,l,1,ej)/(3*mt^3);
142 -((6*ej^2*mt^2-3*ej^2*mt+ej^4+ej^2)/(6*mt^3));
144 /* Known failure: interaction of taylor and simpsum
146 taylor (factsum3 (mt, ej), [mt, 0, 3, asymp]), simpsum;
147 -(6*ej^2*mt^2-3*ej^2*mt+ej^4+ej^2)/(6*mt^3);
149 /* see SF bug report # 740134 */
151 (foo: i^2, [sum ('foo, i, 0, 2), sum (foo, i, 0, 2), sum (foo, i, 0, n)]);
152 [3*foo, 5, 'sum (i^2, i, 0, n)];
154 /* summand is a function which has a side effect --
155 * outcome here differs from recommendation by Stavros in 740134
157 block ([L:[]], f(i) := (L: append (L, [i]), i), S: sum (f (i), i, 1, 3), [S, L]);
160 sum (integrate (x^i ,x), i, 0, 2);
163 sum (integrate (1/(x^i + 1), x), i, 0, 1);
166 (f[i](x) := x^i, g[i](x) := x^i, h[i](x) := x^i, 0);
172 sum (f[i](x), i, 0, n);
175 sum (g[i](x), i, 0, n);
178 sum (h[i](x), i, 0, n);
181 /* see SF bug report # 817521 */
183 (kill (foo), foo (n):= block([aaa,m], modedeclare([m,n], fixnum), m:n, [ aaa[1], aaa[n], aaa[m], aaa[1]+aaa[2], sum(aaa[i],i,1,m) ] ), foo (2));
184 [aaa[1],aaa[2],aaa[2],aaa[2]+aaa[1], aaa[2]+aaa[1]];
190 [aaa[1],aaa[2],aaa[2],aaa[2]+aaa[1], aaa[2]+aaa[1]];
192 /* see SF bug report # 851765 */
194 (apply (forget, facts ()), assume (i >= a), sum (i, i, a, b), facts ());
200 (assume (i < 0), sum (abs (i), i, 1, a));
203 (apply (forget, facts ()), 0);
206 /* see SF bug report # 1007094 */
208 (u: 'sum((-1/(8*i+6)-1/(8*i+5)-2/(8*i+4)+4/ (8*i+1))/16^i,i,1,a), 0);
215 0.0082540570132481894$
217 u, a=2, simpsum, numer;
218 0.0082540570132481911$
220 u, a=2, simpsum, bfloat;
223 /* see SF bug report # 1192935 */
225 (f(x) := x^2, b: 1/10, Ak: b*f(k*b), sum (Ak, k, 0, 9));
228 /* When A(k) is defined as A(k) := b*f(k*b) instead of define(A(k), b*f(k*b)),
229 * A(k) evaluates to k^2/n^3 even if n is bound to something;
230 * this is a consequence of Maxima's evaluation policies:
231 * rhs of := is not evaluated, and variables (b in this case)
232 * are evaluated just once.
234 (b:1/n, define (A(k), b*f(k*b)), A(k));
237 sum (A(k), k, 0, n-1);
238 ('sum (k^2, k, 0, n-1))/n^3;
240 sum (A(k), k, 0, n-1), simpsum;
241 (n+2*(n-1)^3+3*(n-1)^2-1)/(6*n^3);
243 [''%, sum (A(k), k, 0, n-1)], n=10;
246 (B(k) := k*f(k*b), B(k));
249 sum (B(k), k, 0, n-1), simpsum;
250 ((n-1)^4 + 2*(n-1)^3 + (n-1)^2) / (4*n^2);
252 /* see SF bug report # 1363411 */
254 (kill (f), 'sum(1+f(k),k,1,2)), simpsum;
257 is(equal(block([gcd:subres], ratsimp((-x^3+1)/(2*x^5+2) + (-sqrt(5)+5)/(20*x^2+(-10*sqrt(5)-10)*x+20))),
258 block([gcd:spmod], ratsimp((-x^3+1)/(2*x^5+2) + (-sqrt(5)+5)/(20*x^2+(-10*sqrt(5)-10)*x+20)))));
261 /* General tests from here on */
263 sum (x, x, p, p + 2);
264 p + (p + 1) + (p + 2);
266 product (x, x, p, p + 2);
267 p * (p + 1) * (p + 2);
269 sum (x, x, %i, %i + 2);
270 %i + (%i + 1) + (%i + 2);
272 product (x, x, %i, %i + 2);
273 %i * (%i + 1) * (%i + 2);
275 sum (imagpart (1 + %i*k), k, 1, n), simpsum;
278 product (imagpart (1 + %i*k), k, 1, n), simpproduct;
281 (g(x) := q(x^2), sum (g(k), k, 1, n));
282 'sum (q (k^2), k, 1, n);
284 (g(x) := q(x^2), product (g(k), k, 1, n));
285 'product (q (k^2), k, 1, n);
287 sum (integrate (x^k, x, 0, 1), k, 1, n);
288 'sum (1/(k + 1), k, 1, n);
290 product (integrate (x^k, x, 0, 1), k, 1, n);
291 'product (1/(k + 1), k, 1, n);
293 (kill(b), sum ('concat (q, k), k, a, b));
294 'sum ('concat (q, k), k, a, b);
296 sum ('concat (q, k), k, a, b), nouns, a=1, b=5;
297 q5 + q4 + q3 + q2 + q1;
299 product ('concat (q, k), k, a, b);
300 'product ('concat (q, k), k, a, b);
302 product ('concat (q, k), k, a, b), nouns, a=1, b=5;
303 q5 * q4 * q3 * q2 * q1;
305 (assume (n>= 1), sum (if k <= n then a else b, k, 1, n));
308 product (if k <= n then a else b, k, 1, n);
311 sum (sin (%pi * k), k, 0, nn);
314 product (cos (2 * %pi * k), k, 0, nn);
317 sum (lambda ([x], x^i), i, 1, 3);
318 lambda ([x], x) + lambda ([x], x^2) + lambda ([x], x^3);
320 sum (k: k^2, k, 1, 3);
323 sum (a, k, inf, minf);
326 (assume (i < 0, j < 0, n > 0), sum(sum(abs(i) + abs(j),i,1,j),j,1,n));
327 'sum ('sum (i + j, i, 1, j), j, 1, n);
329 sum (sum (sqrt (i + j), i, 1, j), j, 1, n), simpsum;
330 'sum ('sum (sqrt (i + j), i, 1, j), j, 1, n);
332 sum (1/k, k, rat(1), rat(n));
335 sum (taylor (x^i, x, 0, 5), i, 0, 5);
336 ''(taylor (x^5 + x^4 + x^3 + x^2 + x + 1,x,0,5));
338 (i: 888, sum (lambda ([i], i^2), i, 1, 3));
339 3 * lambda ([i], i^2);
341 sum (lambda ([i], i^2), i, 1, n);
342 n * lambda ([i], i^2);
344 (f(x) := sum (x, i, 1, 3), f(-x));
347 (ak : k^2, g(a,n) := sum(a,k,1,n), g(ak, 5));
350 (translate (g), g(ak,5));
353 sum (integrate (x^i, x), i, 0, n);
354 'sum (x^(i+1) / (i+1), i, 0, n);
356 (assume (k < 0), assume (0 < n, m > n), sum (sqrt (k^2), k, n, m));
359 (assume (i >= n_1, i <= n_2), sum (z(i), i, n_1, n_2));
360 'sum (z(i), i, n_1, n_2);
362 block ([prederror: false], [is (i >= n_1), is (i <= n_2)]);
365 /* some examples adapted from describe("sum") */
367 (old_facts: facts (), foo: 42, bar: 12 + exp(7), baz: 1/quux, 0);
370 sum (foo^2, foo, 1, 7);
373 sum (aa[bar], bar, 1, 7);
374 aa[7] + aa[6] + aa[5] + aa[4] + aa[3] + aa[2] + aa[1];
376 sum (aa(baz), baz, 1, 7);
377 aa(7) + aa(6) + aa(5) + aa(4) + aa(3) + aa(2) + aa(1);
379 sum (aa(foo), foo, 1, n);
380 'sum (aa(foo), foo, 1, n);
382 sum (2^bar + bar^2, bar, 0, n), simpsum;
383 2^(n + 1) + (2*n^3 + 3*n^2 + n)/6 - 1;
385 sum (1/3^baz, baz, 1, inf), simpsum;
388 sum (foo^2, foo, 1, 4) * sum (1/foo^2, foo, 1, inf), simpsum;
391 (gg(bar) := 1/bar^2, sum (gg(bar)^2, bar, 1, inf));
392 sum (1/bar^4, bar, 1, inf);
394 sum (gg(baz)^2, baz, 1, inf), simpsum;
397 [is (foo = 42), is (bar = 12 + exp(7)), is (baz = 1/quux)];
400 (new_facts: facts(), is (sort (old_facts) = sort (new_facts)));
403 /* redo all of the tests, this time with product */
405 product (lambda ([x], x^i), i, 1, 3);
406 lambda ([x], x) * lambda ([x], x^2) * lambda ([x], x^3);
408 product (k: k^2, k, 1, 3);
411 product (a, k, inf, minf);
414 (assume (i < 0, j < 0, n > 0), product(product(abs(i) + abs(j),i,1,j),j,1,n));
415 'product ('product (i + j, i, 1, j), j, 1, n);
417 product (1/k, k, rat(1), rat(n));
418 'product (1/k, k, 1, n);
420 product (taylor (x^i, x, 0, 5), i, 0, 5);
421 ''(taylor (x^15,x,0,15));
423 (i: 888, product (lambda ([i], i^2), i, 1, 3));
424 (lambda ([i], i^2))^3;
426 product (lambda ([i], i^2), i, 1, n);
429 (f(x) := product (x, i, 1, 3), f(-x));
432 (f(x) := x^2, b: 1/10, Ak: b*f(k*b), product (Ak, k, 1, 9));
435 (A(k) := k*f(k*b), b:1/n, A(k));
438 product (A(k), k, 0, n-1);
439 ('product (k^3/n^2, k, 0, n-1));
441 (assume (n > 0, m > n), ev (product (k, k, n, m), simpproduct));
444 (ak : k^2, g(a,n) := product(a,k,1,n), g(ak, 5));
447 (translate (g), g(ak,5));
450 /* multiplicand is a function which has a side effect */
452 block ([L:[]], f(i) := (L: append (L, [i]), i), S: product (f (i), i, 1, 3), [S, L]);
455 product (integrate (x^i ,x), i, 0, 2);
458 product (integrate (1/(x^i + 1), x), i, 0, 1);
461 (f[i](x) := x^i, g[i](x) := x^i, h[i](x) := x^i, 0);
464 /* reference f[i] and g[i] -- see 740134 for the effect this has on previous defn of sum */
469 product (f[i](x), i, 0, n);
470 'product (x^i, i, 0, n);
472 product (g[i](x), i, 0, n);
473 'product (x^i, i, 0, n);
475 product (h[i](x), i, 0, n);
476 'product (x^i, i, 0, n);
478 product (integrate (x^i, x), i, 0, n);
479 'product (x^(i+1) / (i+1), i, 0, n);
481 (assume (k < 0), assume (0 < n, m > n), ev (product (sqrt (k^2), k, n, m), simpproduct));
484 (assume (i >= n_1, i <= n_2), product (z(i), i, n_1, n_2));
485 'product (z(i), i, n_1, n_2);
487 block ([prederror: false], [is (i >= n_1), is (i <= n_2)]);
490 /* some examples adapted from describe("sum") */
492 (old_facts: facts (), foo: 42, bar: 12 + exp(7), baz: 1/quux, 0);
495 product (foo^2, foo, 1, 7);
498 product (aa[bar], bar, 1, 7);
499 aa[7] * aa[6] * aa[5] * aa[4] * aa[3] * aa[2] * aa[1];
501 product (aa(baz), baz, 1, 7);
502 aa(7) * aa(6) * aa(5) * aa(4) * aa(3) * aa(2) * aa(1);
504 product (aa(foo), foo, 1, n);
505 product (aa(foo), foo, 1, n);
507 product (2^bar + bar^2, bar, 0, n), simpsum;
508 product (2^bar + bar^2, bar, 0, n);
510 product (1/3^baz, baz, 1, inf), simpsum;
511 product (1/3^baz, baz, 1, inf);
513 product (foo^2, foo, 1, 4) * product (1/foo^2, foo, 1, inf), simpsum;
514 (4!)^2 * 'product (1/'foo^2, 'foo, 1, inf);
516 (gg(bar) := 1/bar^2, product (gg(bar)^2, bar, 1, inf));
517 'product (1/'bar^4, 'bar, 1, inf);
519 product (gg(baz)^2, baz, 1, inf), simpsum;
520 'product (1/'baz^4, 'baz, 1, inf);
522 [is (foo = 42), is (bar = 12 + exp(7)), is (baz = 1/quux)];
525 (new_facts: facts(), is (sort (old_facts) = sort (new_facts)));
528 /*---- new tests from bw ------*/
539 sum(sqrt(3),i,0,inf);
542 sum(sqrt(3),i,-1932,inf);
548 sum(-sqrt(3),i,0,inf);
557 sum(p(i + 1) - p(i),i,1,5);
563 product(1,i,minf,inf);
566 product(1,i,-42,inf);
575 product(%pi,i,0,inf);
578 product(%pi - 3,i,0,inf);
581 product(%pi - 3,i,minf,inf);
584 product(-%pi,i,0,inf);
590 product(-%pi,i,minf,inf);
593 product((1 + %i)/sqrt(2),k,1,inf);
602 product(p(i + 1) / p(i),i,1,5);
605 sum(lambda([x],x^i),i,1,3);
606 lambda([x],x) + lambda([x],x^2) + lambda([x],x^3);
608 product(lambda([x],x^i),i,1,3);
609 lambda([x],x) * lambda([x],x^2) * lambda([x],x^3);
614 sum([1,k],k,1,n),simpsum;
617 (old_facts : facts(),0);
620 errcatch(sum(1/k,k,-1,1));
623 (new_facts: facts(), is (sort (old_facts) = sort (new_facts)));
626 (old_facts : facts(),0);
629 errcatch(product(1/k,k,-1,1));
632 (new_facts: facts(), is (sort (old_facts) = sort (new_facts)));
635 sum(diff(log(x),x,k),k,1,2);
638 product(diff(log(x),x,k),k,1,2);
641 sum(x^k * at(diff(sin(x),x,k)/k!,x=0),k,0,5) - taylor(sin(x),x,0,5);
644 product(%i-x,k,1,inf);
645 'product(%i-x,k,1,inf);
659 product(%i,k,inf,minf);
662 product(%i,k, 0, minf);
680 product(k,k,%i,%i+1);
689 sum(k,k,1,entier(4/3));
692 sum(k,k,entier(4/3),1);
695 sum(a,k,entier(4/3),entier(5/2));
698 product(k,k,1,entier(4/3));
701 product(k,k,entier(4/3),1);
704 product(a,k,entier(4/3),entier(5/2));
707 sum(q(k),k,n^2 + 1,-n^2 - 1); /* It's an empty sum */
710 product(q(k),k,n^2 + 1,-n^2 - 1);
713 sum(concat(q,k),k,1,3);
716 (s : sum('concat(q,k),k,1,n),0);
719 (s : subst(n=3,s), ev(s,nouns));
722 sum(product(concat(q,i),i,1,j),j,1,3);
723 q1 + q1 * q2 + q1 * q2 * q3$
725 product(concat(q,k),k,1,3);
728 (s : product('concat(q,k),k,1,n),0);
731 (s : subst(n=3,s), ev(s,nouns));
737 sum(realpart(a + %i * k),k,1,3);
743 sum(sum(abs(k),k,1,n),n,1,m);
744 sum(sum(k,k,1,n),n,1,m)$
746 sum(sum(k,k,1,i),i,1,n),simpsum,ratsimp;
747 ''(ratsimp(n^3/6+n^2/2+n/3))$
749 sum(sum(abs(k),k,1,n) + sum(abs(k),k,1,n),n,1,m);
750 2 * sum(sum(k,k,1,n),n,1,m)$
758 sum(if k < 1 then a else b,k,0,2);
761 block ([prederror : true], sum(if k < 1 then a else b,k,0,2));
764 sum(1/3^k,k,1,inf),simpsum;
767 sum(3^-i,i,1,inf),simpsum;
770 (kill(x), assume(abs(x)-1 < 0));
773 sum(x^k,k,0,inf),simpsum;
776 (forget(abs(x) - 1 < 0),0);
779 /*--- A few challenging sums ----*/
781 (assume (n >= 1), sum (if k <= n then a else b, k, 1, n));
784 (declare(nn,integer), sum(sin(%pi * k),k,0,nn));
787 sum(sin(x),x,1/2,1/2 + nn);
788 'sum(sin(x),x,1/2,1/2 + nn);
790 remove (nn, integer);
793 (assume_pos: assume_pos_save, apply (forget, facts ()), 0);
796 nusum ((n - 1)*(n - 1)!, n, 1, n);
802 nusum ((2*m)!/(m!*(m + 1)!), m, 0, n);
803 'sum ((2*m)!/(m!*(m + 1)!), m, 0, n);
806 /*--- A test for sumcontract ---*/
808 sumcontract(sum(k,k,1,n) + sum(k^2, k, 1, n));
811 /* Bug ID: 2770575 - rtestsum test 226
812 * This test simplifies correctly after revision 1.12 of sumcon.lisp.
814 sumcontract(sum(k, k, 1, n) + sum(k, k, 1, n+1));
817 /* Tests for bug report [ 1497706 ] sum(1/k^2,k,2,inf), simpsum; */
819 /* sum (1/k^2, ...) not simplified unless specifically requested */
820 (sum (1/k^2, k, 1, inf), [op (%%), args (%%)]);
821 [''(nounify (sum)), [1/k^2, k, 1, inf]];
823 sum (1/k^2, k, 1, inf), simpsum;
826 /* this is the case cited in the bug report */
827 sum (1/k^2, k, 2, inf), simpsum;
830 sum (1/k^2, k, 1, 9);
833 sum (1/k^2, k, 10, inf), simpsum;
834 %pi^2/6 - 9778141/6350400;
836 sum (1/k^4, k, 1, 5);
839 sum (1/k^4, k, 6, inf), simpsum;
840 %pi^4/90 - 14001361/12960000;
842 sum (1/k^7, k, 1, inf), simpsum;
845 sum (1/k^7, k, 1, 3);
848 sum (1/k^7, k, 4, inf), simpsum;
849 zeta (7) - 282251/279936;
851 /* Tests for bug report [ 1550985 ] niceindices
852 * Test the cases mentioned in the bug report & some others for good measure
855 (reset (niceindicespref), 0);
858 niceindices (sum (1/kk, kk, 1, n) - sum (1/ii, ii, 1, n));
861 niceindices (sum (1/k, k, 1, n) - sum(1/i, i, 1, n));
864 niceindices (sum (F(foo), foo, 1, inf));
865 'sum (F(i), i, 1, inf);
867 (niceindicespref : '[foo, bar, baz, quux], 0);
870 niceindices (sum (1/kk, kk, 1, n) - sum (1/ii, ii, 1, n));
873 niceindices (sum (1/k, k, 1, n) - sum(1/i, i, 1, n));
876 niceindices (sum (F(foo), foo, 1, inf));
877 'sum (F(foo), foo, 1, inf);
879 niceindices (product (sum (product (sum (product (m*F(i) + cos(G(j))^k/sin(l), m, 1, n5), l, 1, n4), k, 1, n3), j, 1, n2), i, 1, n1));
880 'product ('sum ('product ('sum ('product (foo*F(foo0) + cos(G(quux))^baz/sin(bar), foo, 1, n5), bar, 1, n4), baz, 1, n3), quux, 1, n2), foo0, 1, n1);
882 niceindices (sum (sum (foo/foo0, foo, 1, n), foo0, 1, m));
883 ('sum (1/foo, foo, 1, m)) * 'sum (foo, foo, 1, n);
885 niceindices (sum (sum (1/(foo + foo0), foo, 1, n), foo0, 1, m));
886 'sum ('sum (1/(foo + bar), foo, 1, n), bar, 1, m);
888 (reset (niceindicespref), 0);
891 /* Test assignments to gensumnum and genindex */
892 errcatch (gensumnum:-5);
894 errcatch(genindex:4);
901 (reset(gensumnum), gensumnum);
903 (reset(genindex), genindex);
906 /* Tests for bashindices, which used to not transform all indices in nested
907 * sums and products (only the outermost)
910 (reset (gensumnum), 0);
913 bashindices (sum (product (sum (F (l, m, n), l, 0, m), m, 0, n), n, 0, N));
914 'sum ('product ('sum (F (j1, j2, j3), j1, 0, j2), j2, 0, j3), j3, 0, N);
916 (reset (gensumnum), 0);
919 /* Test for bug report [ 1552710 ] product(sum(f(i),i,1,inf),j,1,inf) => inf (wrong) */
921 /* Since sum(f(i), i, 1, inf) is free of j, another result
922 * such as limit(sum(f(i), i, 1, inf)^n, n, inf) would be OK, too.
923 * In any event, inf (observed in the bug report) is not OK.
925 (kill(f), block ([x : product (sum (f(i), i, 1, inf), j, 1, inf)], block ([simp : false], is (x = 'product ('sum (f(i), i, 1, inf), j, 1, inf)))));
929 /* Test for bug reported by Amit Aronovitch to mailing list 2006/01/06
932 (kill(f), f(x) := sum (x^n/n!, n, 0, inf), f(2*x));
933 'sum (2^n*x^n/n!, n, 0, inf);
936 /* Test for intosum */
938 intosum(2*sum(k,k,1,inf));
941 /* correct a bug in SUBST-IF-NOT-FREEOF (src/asum.lisp):
942 * array flag in CAR was lost before, but it's OK now
944 (kill (a, i), x : a[i], sum (x, i, 1, 3));
947 /* Restore treatment of definite summations as it was before summation
948 * revisions of December 2005: simplify definite summation to explicit
949 * sum only if simpsum is true.
952 (kill (f), sum (f(i), i, 1, 3));
955 'sum (f(i), i, 1, 3);
956 'sum (f(i), i, 1, 3);
958 'sum (f(i), i, 1, 3), simpsum;
961 /* (analogous tests for product) */
963 product (f(i), i, 1, 3);
966 'product (f(i), i, 1, 3);
967 'product (f(i), i, 1, 3);
969 'product (f(i), i, 1, 3), simpproduct;
972 /* Differentiate wrt a subscripted symbol in a summation.
973 * Should work the same as for an ordinary symbol.
975 (kill (x, a, a1), 0);
978 diff ('sum (sin (a1 * x[i]), i, 1, n), a1);
979 'sum (x[i] * cos (a1 * x[i]), i, 1, n);
981 diff ('sum (x[i]^a1, i, 1, n), a1);
982 'sum (x[i]^a1 * log(x[i]), i, 1, n);
984 diff ('sum (sin (a[1] * x[i]), i, 1, n), a[1]);
985 'sum (x[i] * cos (a[1] * x[i]), i, 1, n);
987 diff ('sum (x[i]^a[1], i, 1, n), a[1]);
988 'sum (x[i]^a[1] * log(x[i]), i, 1, n);
990 /* Feature request 1848704: no way to convert log(product(...)) to sum(log(...)...)
993 (kill (u, a, x, T), 0);
996 log (product (u[k], k, 1, n));
997 log (product (u[k], k, 1, n));
999 log (product (u[k], k, 1, n)), logexpand=true;
1000 log (product (u[k], k, 1, n));
1002 log (product (u[k], k, 1, n)), logexpand=all;sum (log (u[k]), k, 1, n);
1004 log (product (u[k], k, 1, n)), logexpand=super;
1005 sum (log (u[k]), k, 1, n);
1007 log (product (a*%e^x(i), i, 1, T));
1008 log (product (a*%e^x(i), i, 1, T));
1010 log (product (a*%e^x(i), i, 1, T)), logexpand=all;
1011 /* Yields sum(const + ...) instead of const*T + sum(...)
1012 * since sum is not known to be linear by default.
1013 * Change this result if default ever changes.
1015 sum (log(a) + x(i), i, 1, T);
1017 /* bug reported to mailing list 2008-04-17:
1018 * simpsum causes two evaluations of summand, should be only one
1020 (bar (x, y) := sum (F (i, x, y), i, 1, n),
1021 ev (bar (y, x), simpsum=false));
1022 'sum (F (i, y, x), i, 1, n);
1024 ev (bar (y, x), simpsum=true);
1025 'sum (F (i, y, x), i, 1, n);
1027 /* original problem */
1030 geom(x,m,n):= (x^m-x^(n+1))/(1-x),
1031 B(s,sp):=-phi[1](s)*phi[1](s+sp)*phi[0](s+sp)/(1-phi[1](s+sp)*phi[0](s +sp))
1032 +(1-phi[1](s))*sum((phi[1](s)*phi[0](s))^n*(
1033 geom( (phi[0](s+sp)*phi[1](s+sp))/(phi[0](s)*phi[1](s)), 1, n-1 )
1034 -phi[1](s+sp)/phi[1](s)
1035 *geom((phi[0](s+sp)*phi[1](s+sp))/(phi[0](s)*phi[1](s)), 0, n-1))
1037 A(s):=phi[1](s)*(1+phi[0](s))/(1-phi[1](s)*phi[0](s)),
1038 C(s,sp):=A(s+sp)+B(s,sp)+B(sp,s),
1042 (expr_1 : ev (C (s, sp), simpsum=false),
1043 expr_2 : ev (C (s, sp), simpsum=true),
1044 is (expr_1 = expr_2));
1047 (expr_1 : ev (C (sp, s), simpsum=false),
1048 expr_2 : ev (C (sp, s), simpsum=true),
1049 is (expr_1 = expr_2));
1052 is (sublis ([s=sp, sp=s], C (s, sp)) = C (s, sp)), simpsum;
1055 is (sublis ([s=sp, sp=s], C (sp, s)) = C (sp, s)), simpsum;
1058 is (C (sp, s) = C (s, sp)), simpsum;
1061 /* Tests for fbino in combin.lisp */
1062 sum(binomial(2*n-k,k), k, 0, n), simpsum;
1065 sum(binomial(n-k,k), k, 1, n), simpsum;
1071 sum(binomial(n-k,k+1), k, 1, n-1), simpsum;
1074 sum(binomial(2*n,2*k), k, 0, n), simpsum;
1077 sum(binomial(n,2*k+1), k, 0, n), simpsum;
1080 sum(binomial(n,2*k), k, 0, n), simpsum;
1083 sum(binomial(n,2*k), k, 1, n), simpsum;
1086 sum(binomial(n+1,k), k, 1, n), simpsum;
1089 sum(binomial(n,k), k, -1, n+1), simpsum;
1092 sum(binomial(t+i,t),i,0,k), simpsum;
1093 binomial(t+k+1,t+1);
1095 /* Because of revision 1.43 of csimp2.lisp binomial(x,x) no longer simplifies
1096 * automatically to 1. The sign must be known not be negative.
1097 * Therefore, we add a fact about the sign of t.
1101 sum(binomial(t+i,t),i,1,k), simpsum;
1102 binomial(t+k+1,t+1)-1;
1106 sum(binomial(t+i,i),i,0,k), simpsum;
1107 binomial(t+k+1,t+1);
1112 /* bug in assume database causes trouble for conditional in summation
1113 * see mailing list 2009-07-29 "Fourier Series using fourie.mac"
1116 /* If ever the assume stuff is revised, this next test can go away.
1117 * It's here to ensure the underlying problem is fixed.
1122 (kill (all), assume (xx >= 1), 0);
1128 if xx > 1 then aa else bb;
1129 if xx > 1 then aa else bb;
1131 sum (if xx > 1 then aa else bb, xx, 1, 3);
1134 foo : 'sum (if xx > 1 then aa else bb, xx, 1, 3);
1135 'sum (if xx > 1 then aa else bb, xx, 1, 3);
1140 foo : sum (if equal (xx, 1) then aa else bb, xx, 1, nn);
1141 'sum (if equal (xx, 1) then aa else bb, xx, 1, nn);
1143 ev (foo, nouns, nn=3);
1146 (reset(assume_pos),1);
1149 /* SF bug #3605: "Variable confusion in function handling Taylor series" */
1154 /* function definition copied verbatim from bug report */
1156 (test(f) := block([i:taylorinfo(f)[1]], sum(coeff(f, i[1], x) * i[1]^x, x, 0, i[3])), 0);
1159 ratdisrep (test(taylor(cos(y), y, 0, 4)));
1162 ratdisrep (test(taylor(cos(x), x, 0, 4)));
1165 /* declare summation index with different name in block to distinguish from global */
1166 (test1(f) := block([i:taylorinfo(f)[1], x%], sum(coeff(f, i[1], x%) * i[1]^x%, x%, 0, i[3])), 0);
1169 ratdisrep (test1(taylor(cos(y), y, 0, 4)));
1172 ratdisrep (test1(taylor(cos(x), x, 0, 4)));
1175 /* Bug #2267: simpsum error */
1177 sum((2^(k+i))/(2^(k+i)+2^(i)),i,0,k), simpsum;
1178 (2^k*(1 + k))/(1 + 2^k);