Maple double sums: summation order

Suppose we want to compute

$$\sum_{k=0}^1 \sum_{j=0}^{2k}{ 2 \choose 2k-j}{2 \choose j}=7$$

with Maple (I picked this trivial example only as an illustration).I always thought that the order of double summation in Maple is the natural one, where limit(s) of the inner sum can depend on the index of the outer sum. But when I enter this in Maple I get

sum(sum(binomial(2,2*k-j)*binomial(2,j),j=0..2*k),k=0..1); 0

sum(sum(binomial(2,2*k-j)*binomial(2,j),k=0..1),j=0..2*k); 7

Why the first computation returns $0$ and the second gives the correct answer?

Edit: On the other hand,

sum(sum(m,m=1..k),k=0..10); 100

as expected and

sum(sum(m,k=0..10),m=1..k); $11(k+1)^2/2-11k/2-11/2$

also as expected (it runs the k-summation from 1 to k 11 times).

• I think the sum inside should be able to read the variable of the sum outside but not the other way around. As in nested "for" loops. It also seems like better pratice to me. Oct 9, 2015 at 13:36
• I agree but that's not what they see in Maple: maplesoft.com/applications/view.aspx?SID=4094&view=html Oct 9, 2015 at 13:53

If your goal is to add up a finite number of terms then you could use add instead of sum.

restart;

7


Note that add has special evaluation rules in Maple, and its first argument is not evaluated until the index attains a numeric value. In this case that applies to both the outer and inner calls to add.

In contrast, sum is evaluated using Maple's regular evaluation rules, and its first argument is evaluated up front. In your first example, before index k attains a specific value (from the second argument, the range), the first argument of the outer call to sum is evaluated. That is to say, the inner call to sum is evaluated up front.

sum(binomial(2,2*k-j)*binomial(2,j), j=0..2*k);

0


and then the outer call to sum is computed,

sum(%, k=0..1);

0


For this particular example you could delay the evaluation of the inner call to sum, until k gets its particular numeric values.

sum('sum(binomial(2,2*k-j)*binomial(2,j),j=0..2*k)',k=0..1);

7

sum('sum'(binomial(2,2*k-j)*binomial(2,j),j=0..2*k),k=0..1);

7


Or you could delay the evaluation of the first argument of the inner call to sum, to the extent that the result of the outer sum call requires an eval to complete the computation.

sum(sum(''binomial(2,2*k-j)*binomial(2,j)'',j=0..2*k),k=0..1);

3 '1' + '4'

eval(%);

7


Note that some of these workarounds/kludges are quite problem specific.

Z := sum(binomial(2,2*k-j)*binomial(2,j), j=A..B):

ZZ := limit( limit(Z, B=2*k), A=0):
sum(ZZ, k=0..1);

7


These results are from version Maple 2015.1. Using version Maple 11.02 that last command returns an error about the summand being singular in the interval of summation. But in both versions the following obtains:

eval(ZZ,k=0);
Error, (in GAMMA) numeric exception: division by zero

eval(ZZ,k=1);
Error, (in GAMMA) numeric exception: division by zero

limit(ZZ,k=0) + limit(ZZ,k=1);

7

limit( limit(Z, A=0), B=2*k):
sum(%, k=0..1);

7


Or,

Q := sum(binomial(2,2*k-j)*binomial(2,j), j=0..M):

eval(Q, M=2*k);
Error, numeric exception: division by zero

limit(Q, M=2*k);

6 binomial(2, 2 k)
------------------
2
2 k  - 7 k + 6

sum(%, k=0..1);

7


Nesting the other way, you get a rather "fortuitous" result.

P := sum(binomial(2,2*k-j)*binomial(2,j), k=0..1);

P := binomial(2, -j) binomial(2, j) + binomial(2, 2 - j) binomial(2, j)

sum(P, j=0..2*k);

7


And now something interesting. In version Maple 11.02 the following obtains, and seems somewhat useful,

sum(P, j=0..M); # M is unassigned

7-binomial(2,M+1)*(binomial(2,-M-1)+binomial(2,1-M))
*hypergeom([1, M-1, M-1, 2-I*2^(1/2)+M, 2+I*2^(1/2)+M],
[2+M, 4+M, 1-I*2^(1/2)+M, 1+I*2^(1/2)+M],1)


while in Maple 12.02 through Maple 2015.1 one gets,

sum(P, j=0..M); # M is unassigned

7


which is interesting in light of,

sum(P, j=0..1);

6

6


Your other examples' results can also be seem to be a consequence of the usual evaluation rules of Maple which sum follows.

sum(m, m=1..k);

1        2   1     1
- (k + 1)  - - k - -
2            2     2

sum(%, k=0..10);

220

sum(m, k=0..10);

11 m

sum(%, m=1..k);

11        2   11     11
-- (k + 1)  - -- k - --
2             2      2

• This is very helpful. Thanks! Oct 14, 2015 at 20:18