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:



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



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!

  • $\begingroup$ Would HoldForm[Sum[ii, {ii,1,3}]] suffice? $\endgroup$ Jan 28 '15 at 0:13
  • $\begingroup$ No, since it gives back the unevaluated Sum[ii,{ii,1,3}], instead of the actual summands 1+2+3. $\endgroup$
    – Kagaratsch
    Jan 28 '15 at 0:23
  • $\begingroup$ {a + b + c} /. {a -> "1", b -> "2", c -> "3"} $\endgroup$
    – martin
    Jan 28 '15 at 0:27
  • $\begingroup$ 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. $\endgroup$
    – Mr.Wizard
    Jan 28 '15 at 0:32
  • $\begingroup$ 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. $\endgroup$
    – 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.

  • $\begingroup$ 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. $\endgroup$ Jan 28 '15 at 0:33
  • $\begingroup$ 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. $\endgroup$ Jan 28 '15 at 0:39
  • $\begingroup$ Yes, this solution is great! I like how \.List->Plus performs the summation on it too. $\endgroup$
    – Kagaratsch
    Jan 28 '15 at 0:41
  • 2
    $\begingroup$ 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. $\endgroup$ Jan 28 '15 at 0:47
  • 1
    $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Jan 28 '15 at 4:29

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