# Prevent a sum from simplifying? [duplicate]

Basically, I want to prevent a sum from cancelling or summing together the individual summands and just display them separately next to each other. Example:

Sum[ii,{ii,1,3}]


1+2+3

Mathematica either gives back the result 6, or does not perform the sum at all if I use diverse Hold functions:

Sum[ii,{ii,1,3}]//Hold


Hold[Sum[ii,{ii,1,3}]]

Is there any way to get the desired "halfway evaluated" output? I'd like to see every summand separately to make sure that the computation works correctly when it does a very complicated sum of variables but then reduces (mostly cancels) to a very simple answer. Thanks for any suggestion!

• Would HoldForm[Sum[ii, {ii,1,3}]] suffice? – David G. Stork Jan 28 '15 at 0:13
• No, since it gives back the unevaluated Sum[ii,{ii,1,3}], instead of the actual summands 1+2+3. – Kagaratsch Jan 28 '15 at 0:23
• {a + b + c} /. {a -> "1", b -> "2", c -> "3"} – martin Jan 28 '15 at 0:27
• I have marked this question as a duplicate. Please review the link inserted at the top of your post, then tell me if anything in your question remains unanswered. – Mr.Wizard Jan 28 '15 at 0:32
• The question tagged above is very similar, but much more specialized than mine - which also becomes apparent looking at the different answers the two questions received. – Kagaratsch Jan 28 '15 at 0:39

Simple: use Table

Sum[i^2, {i, 3}] (* 14 *)

Table[i, {i, 3}] (* {1, 4, 9} *)


If you want the output to be displayed like a sum, use Row:

Row[%, "+"] (* 1 + 4 + 9 *)


And when you want to evaluate, simply use Total:

Total[%%] (* 14 *)


There is another solution (see David's comment):

Sum[(HoldForm[#] &)[i^2], {i, 3}] (* 1 + 4 + 9 *)


To explain how this works, let's look at it piece by piece: First, we create a pure function using & and #. Here are a couple of (mostly) equivalent definitions to explain it:

f[x_] := x^2 - x + 1
f = Function[x, x^2 - x + 1];
f = Function[#^2 - # + 1];
f = (#^2 - # + 1)&;


You can see that in this particular case the function simply applies HoldForm to the argument. We apply this function (with the regular [] syntax) to each summand. This has the effect of wrapping each summand in HoldForm before adding them together, preventing further evaluation of the function while displaying them normally:

%//InputForm (* HoldForm[1] + HoldForm[4] + HoldForm[9] *)


The trick is that simply using HoldForm[i] won't work, since HoldForm will prevent the summand from being evaluated, and it will remain i^2: you'd get i^2 + i^2 + i^2. However, the argument to our anonymous function is evaluated before being passed in, so HoldForm is applied after the summand is evaluated.

To evaluate, simply call ReleaseHold on the expression:

ReleaseHold[%] (* 14 *)


This removes all the HoldForms from the expression, allowing it to be evaluated completely.

• There must be a superior way to the use of Table. For instance Sum[f[ii], {ii, 1,3}] gives almost what the poser seeks. Perhaps some use of an Identity function with Hold will suffice. – David G. Stork Jan 28 '15 at 0:33
• My logic was that if you want to "see every summand separately," then the best way was to simply generate them separately. There might be a trick, but this solution is easy to remember and foolproof, even in weird edge cases. – 2012rcampion Jan 28 '15 at 0:39
• Yes, this solution is great! I like how \.List->Plus performs the summation on it too. – Kagaratsch Jan 28 '15 at 0:41
• Here we go! Sum[(HoldForm[#] &)[ii], {ii, 1, 3}], and Product[(HoldForm[#] &)[ii], {ii, 1, 3}], etc. This approach permits a substitution that can "turn on" and "turn off" evaluation. – David G. Stork Jan 28 '15 at 0:47
• @Kagaratsch please see Pure Functions and (19035). Because Functions by default do not hold their arguments HoldForm[#] &[i^2] evaluates i^2 and then hold the result for further evaluation, in this case later addition to other summands. – Mr.Wizard Jan 28 '15 at 4:29