###Edit###
Michael E2, in an extension to his answer, reminds us of the value of simplifying multiple sums. His generalization further clarifies the nature of these operations. Because the variable name "$x$" does not explicitly appear in the $s_i$ notation, the variable name is immaterial. What matters are the index names, only insofar as they are used to connect an $x$ with its enclosing summation: as I remarked at the outset, as bound variables they must disappear at the end.
Thus, for example, we could write sum[i^3, i]
for $\sum_{i=1}^n x_i^3$: the first argument to sum
is a polynomial (or rational function, even) and the second one is a symbol to indicate what is varying over the summation.
A comparable extension is trivial to make: just include the second argument explicitly in the definition of sum
. So that we can reproduce the previous solution (where x
was the only variable), we can have the second argument default to x
if it's missing. Here is the entire (generalized) solution:
ClearAll[sum, s, n];
sum[a_] := sum[a, x];
sum[x_, x_] := Subscript[s, 1]; sum[x_^p_, x_] := Subscript[s, p];
sum[Times[a_, y__], x_] /; FreeQ[a, x] := a sum[Times[y], x];
sum[a_ + b_, x_] := sum[a, x] + sum[b, x];
sum[a_, x_] /; FreeQ[a, x] := n a;
expand[a_] /; ! FreeQ[a, sum] := Map[Expand, a, Infinity]; expand[a_] := a;
For example, compute $\sum_{k=1}^n\sum_{j=1}^n\sum_{i=1}^n (x_i+x_j+x_k - 3\bar{x})^2$, where $\bar{x} = \sum_{i=1}^n x_i / n$ is the average:
sum[sum[sum[(i + j + k - 3 sum[i, i]/n)^3, i], j], k]
$6 s_1^3-9 n s_1 s_2+3 n^2 s_3$
(Notice that it was no problem for i
to appear in two summations.) You do have to be a little careful not to go overboard and stretch the use of sum
too far:
sum[x^2, x^2] // expand
$s_1$
This surprising result is correct: the symbol x^2
in the second argument is being used to represent the variable summed over; in terms of this pattern, the first argument x^2
is the first power, whence the result is indeed $s_1$.
One can rather easily go further with generalizations, depending on the need. The next big step would be to implement multivariate sums, such as $\sum_{i=1}^n\sum_{j=1}^n x_i y_j^2$, perhaps to be written $s_1(x)s_2(y)$ or--assuming a specific ordering $(x,y)$ of the variables--even as $s_{1,2}$. As we appear to have gone beyond the scope of the present question, I leave these generalizations to interested readers. I hope that the examples given here have shown how stripping the notation down to its essentials can lead to simple and clear solutions.