I have a function of three discrete variables $f=f(n,m,p)$. It is known that the sum of $f$ over all three variables

$$F=F(N)=\sum_{n,m,p = 1}^N f(n,m,p)$$

can be expressed in terms of a sum $Q$ over a (known) function of only one variable

$$Q(N) = \sum_{n=1}^N q(n).$$

The functional $F=F[Q]$ is to be found in closed form, which is known to be possible for the function at hand.

Can this be done in Mathematica? I tried using the Collect[expr, x] function, but I don't think that will be able to do the job.

EDIT - To make the question more clear: I have a sum over multiple variables where it is possible to completely 'disentangle' the sum and write it in terms of simpler sums (over one variable). I want to find the original sum in terms of these simpler sums. E.g. when I have a sum F=Sum[q(n)*q(m)*q(p),{n, 1, N}, {m, 1, N}, {p, 1, N}] I want to tell Mathematica: "Look, F can be written in terms of Q=Sum[q(n),{n,1,N}]" and I want Mathematica to give me Out: Q^3. This is a simple algebraic manipulation of resubstituting a sum into an expression. Can Mathematica do that?

Simple example:

$f(n,m,p) = \sin(n)\sin(m)\sin(p)$

(or anything else that factorizes into terms containing only one variable for that matter). Then we can write

$$F(N)=Q(N)^3 = \left(\sum_{n=1}^N \sin(n) \right)^3$$

and therefore the expression required is $F[Q]=Q^3$.

EDIT in response to a comment: An example where $F \neq Q^3$ is

$$f(n,m,p) = npq + np$$

with $Q(n)=n$. The answer here is $$F[Q] = Q^3 + Q^2.$$ However this comment made me realise that all the examples here will be super simple and have polynomial answers. That doesn't really matter however, the exercise is still to get Mathematica to find given sums over one variable in expressions with sums over multiple variables. A more complicated example that is not quite of the form I gave above could be something like

$$F=\left( \frac{\sum_n^N n}{1+\sin(\sum_m^N m)} \right)^{\sum_q^N q}$$

where the answer given that $Q=\sum_n^N n$ would be $F=\left( \frac{Q}{1+\sin(Q)} \right)^{Q}$. However since this is clearly not of the form $F=F(N)=\sum_{n,m,p = 1}^N f(n,m,p)$ and I also realise that this question is way to long for this simple problem already (my bad, sorry) let's forget about that.

Mathematica example from above:

q = Sin[l]
f = (q /. l -> n)*(q /. l -> m)*(q /. l -> p)
F = Sum[f, {n, 1, N}, {m, 1, N}, {p, 1, N}]

Also an example where the function F can't actually be evaluated explicitly for arbitrary $N$, but the functional F[Q] could still be found:

q = (Sqrt[l]*Sin[l])/(x - (l - 1)^2);
f = (q /. l -> n)*(q /. l -> m)*(q /. l -> p)
F = Sum[f, {n, 1, N}, {m, 1, N}, {p, 1, N}]
  • $\begingroup$ Is the sum symbolic, or could N have a value (by the way, you should avoid using N as a variable since it already has a meaning in Mathematica)? $\endgroup$
    – Carl Woll
    Jun 3, 2017 at 0:49
  • $\begingroup$ @CarlWoll oops, I forgot about that. $N$ is always symbolic. $\endgroup$ Jun 3, 2017 at 7:51
  • 1
    $\begingroup$ Can you give any example where the result is not Q^3? $\endgroup$ Jun 4, 2017 at 2:21
  • $\begingroup$ @BrunoLeFloch thanks for pointing that out, see edit. $\endgroup$ Jun 4, 2017 at 8:33
  • 1
    $\begingroup$ To avoid the symbolic N you may want to replace Nnmp -> nijk $\endgroup$
    – A.G.
    Jun 4, 2017 at 11:28

2 Answers 2


Here is my take on the problem. So you have a discrete function $$ F(n)=\sum_{1\leq i,j,k \leq n}f(i,j,k) $$ for some function $f(\cdot , \cdot,\cdot)$.

Simple cases

In some case, Mathematica will find a closed form for $F$, for example

Sum[Sin[i ] Sin[j] Sin[k], {i, 1, n}, {j, 1, n}, {k, 1, n}]

outputs $$ \frac{1}{8} \left(\sin (n)-\cot \left(\frac{1}{2}\right) \cos (n)+\cot \left(\frac{1}{2}\right)\right)^3 $$ which you may call $Q(n)$ if need be. It is also the case for

Sum[i j k + j k, {i, 1, n}, {j, 1, n}, {k, 1, n}]

$$ \frac{1}{8} n^2 (n+1)^2 \left(n^2+3 n\right) $$

Looking at differences

In all cases you can still define $$ q(n)=F(n)-F(n-1)\quad\text{with}\quad q(0)=0. $$ so that $$ F(n)=\sum_1^n q(n). $$ Mathematica can compute that for you and you can decide if $q$ is simple enough for your purposes.

If Mathematica has a hard time simplifying $q$ you can give it a nudge, noting that $q$ will contain terms where 3, 2 or 1 indices will be equal to $n$; i.e. $$ q(n)=F(n)-F(n-1) =q_3(n)+q_2(n)+q_1(n) $$ where $q_3(n)=f(n,n,n)$, $$ q_2(n)=\sum_{1\leq i\leq n-1} [f(i,n,n)+f(n,i,n)+f(n,n,i)]. $$ and $$ q_1(n)=\sum_{1\leq i,j\leq n-1} [f(i,j,n)+f(i,n,j)+f(n,i,j)]. $$ Again you might be lucky if Mathematica finds a closed form for these expressions but there is no guarantee.

ps. I changed the notation in order to avoid the $N$ symbol issue.

Edit. I had an error in the sum for $q(n)$, it should now be fixed with the sums $q_1,q_2,q_3$. That makes my answer much less interesting, so feel free to cancel upvotes...

  • $\begingroup$ +1 and thanks for your answer! This does provide a solution for the problem posed in the question. Unfortunately (for me) you elegantly circumvented what I really wanted: to get Mathematica to substitute sums into expressions with multiple symbolic sums. I may ask a separate question on that problem. Also unless someone comes along and provides a substitution solution I will also award the bounty to this answer. $\endgroup$ Jun 6, 2017 at 14:43
  • $\begingroup$ @Wolpertinger. I found a mistake in my nudge, it should be correct now. $\endgroup$
    – A.G.
    Jun 6, 2017 at 18:48

Is this what you want?


Clear[q, Q, F]
F[q_][n_] := Array[Times @@ Map[q, {#1, #2, #3}] &, {n, n, n}] // Flatten // Total
Q[q_][n_] := n // Range // Map[q] // Total
F[q][4] // Factor
F[q][4] == (Q[q][4])^3 // Simplify


enter image description here

  • $\begingroup$ thanks for your answer! I don't really get what your code does, but the output does not look like what I want. Where is F[Q]? $\endgroup$ May 30, 2017 at 19:36
  • $\begingroup$ @Wolpertinger F[Q] is Q[q].Maybe I used the wrong name, I should use F instead Q. So the result would be F[q]. If the output is not what you expected, maybe I misunderstood the question. $\endgroup$
    – webcpu
    May 30, 2017 at 19:45
  • $\begingroup$ hm I think you might have. F[Q] should just be equal to Q^3 $\endgroup$ May 30, 2017 at 21:15
  • $\begingroup$ I have updated the answer. I think it's better now. $\endgroup$
    – webcpu
    May 31, 2017 at 7:01
  • $\begingroup$ Yes. If we redefine F using Q, Q[q_][n_] := n // Range // Map[q] // Total F[q_][n_] := (Q[q][n])^3 $\endgroup$
    – webcpu
    May 31, 2017 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.