# Constructing a Summation with a Variable Number of Inner Sums

I have a question regarding multiple sums. And my second index depends on the first index. Here it is:

$$\sum_{d=1}^{P}e^{-d}\sum_{\substack{1\leq k_{1}\leq Q \\ 1\leq k_{2}\leq Q \\... \\...\\1\leq k_{d}\leq Q}}e^{-(k_{1}+...+k_{d})}$$

I could not write this sum in mathematica. The problem is that my second sum is a multiple sum and I do not know how to sum $d$ times. How can I do it?

Let's concentrate on the inner multiple sum. First note that we know d because it is given from the outer sum. If we want to write the expression $$-\exp\left[k_1+k_2+\ldots+k_d\right]$$ in Mathematica we could do this very easy. First we create the list {k[1],k[2],...,k[d]} and remember, that this is internally nothing more than List[k[1],k[2],...,k[d]]. If we would now replace the List head with Plus, then it is exactly what we want. For this we can use Apply which is written @@ in infix notation. That leaves how to create the {k[1],k[2],...,k[d]} list. Here we can use Table or Array or we think of it as _mapping the function k over the list {1,2,...,d}. This can be written as

k /@ Range[d]


for a known d. All together this gives

Exp[-Plus @@ k /@ Range[d]]


Now we need to build a multiple Sum, summing over d different k. Again, this can be done in several ways. One way is to create a function which gets as arguments the indices for a Sum

f = Function[Sum[1, ##]]


I only use 1 in the sum for the sake of simplicity. You may ask now, what this ## is: it's the sequence of all arguments given to f. So lets try it:

In[63]:= {Sum[1, {3}], f[{3}]}

Out[63]= {3, 3}

In[62]:= {Sum[1, {3}, {5}], f[{3}, {5}]}

Out[62]= {15, 15}


Seems to work. The only think which is left now, is to create the ranges {k[i],1,q} where i is always a concrete number. Here we can use again the trick with Range and for a known d this gives a list of ranges:

{k[#], 1, q} & /@ Range[d]

(* For d = 3 for instance
Out[64]= {{k[1], 1, q}, {k[2], 1, q}, {k[3], 1, q}}
*)


The last thing is to think about, that our Sum function needs a Sequence of arguments and the above is a List. But we already solved the problem of replacing the List with something different by using @@. This gives all together:

Sum[Exp[-Plus @@ k /@ Range[d]], ##] & @@ ({k[#], 1, q} & /@ Range[d])


Don't be afraid of the expression, because now you know every single piece of it and you know what every piece does. You could try it by for instance

With[{d = 4},
Sum[Exp[-Plus @@ k /@ Range[d]], ##] & @@ ({k[#], 1, q} & /@ Range[d])
]


and get $$\frac{e^{-4 q} \left(e^q-1\right)^4}{(e-1)^4}$$ or you use this to plant it directly into your outer sum

mysum[p_Integer] :=
Sum[Exp[-d]*
Sum[Exp[-Plus @@ k /@ Range[d]], ##] & @@ ({k[#], 1, q} & /@
Range[d]), {d, 1, p}]


I put it into a function mysum, because p needs to be a number to make all the Ranges work. Now you should check mysum[2]

$$\frac{e^{-2 q-2} \left(e^q-1\right)^2}{(e-1)^2}+\frac{e^{-q-1} \left(e^q-1\right)}{e-1}$$

• How do you get $2(q^5 + q^4)$? I don't get that result with your code. May 3, 2012 at 15:19
• Me neither. Wherever I copied this from.. it was the wrong place. Thanks for paying attention. May 3, 2012 at 15:29
• I do not understand why mathematica doesnt give the closed expression for mysum. because it seems that mysum[n] gives $x+x^2+x^3+...+x^n$ where $x$ is mysum[1] May 3, 2012 at 16:15

I'm not sure there's a way to do this symbolically, but you can do the following:

multipleSum[P_] :=
Sum[Exp[-d] Sum[
Exp[-Plus @@ Table[Symbol["k" <> ToString[i]], {i, 1, d}]],
Evaluate[
Sequence @@
Table[{Symbol["k" <> ToString[i]], 1, Q}, {i, 1, d}]]], {d, 1,
P}]


For example:

In[87]:= multipleSum[3]

Out[87]= (E^(-1 - Q) (-1 + E^Q))/(-1 + E) + (
E^(-2 - 2 Q) (-1 + E^Q)^2)/(-1 + E)^2 + (
E^(-3 - 3 Q) (-1 + E^Q)^3)/(-1 + E)^3


I think this is correct.

• I think you missed a minus-sign in the inside exponential. May 3, 2012 at 14:25
• @halirutan erk, thanks: fixed May 3, 2012 at 14:26