0

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!

Kagaratsch
  • 11,955
  • 4
  • 25
  • 72
  • Would HoldForm[Sum[ii, {ii,1,3}]] suffice? – David G. Stork Jan 28 '15 at 00: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 00:23
  • {a + b + c} /. {a -> "1", b -> "2", c -> "3"} – martin Jan 28 '15 at 00: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 00: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 00:39
  • Make sure to start comments with @Mr.Wizard if you want me to see them in a timely fashion. You needs do seem simpler but I think that the existing answers are also applicable. Are you unhappy with the closure? – Mr.Wizard Jan 28 '15 at 04:25
  • @Mr.Wizard The closure prevented a user from posting his solution as an answer, so he had to post it in the comments. I personally still received great help from the community, so that I am happy. – Kagaratsch Jan 28 '15 at 15:02
  • I am glad you received help and I am glad you are happy. Note that the method that David proposed is exactly the same as in this answer. The only difference is that the Function has one argument instead of two. – Mr.Wizard Jan 28 '15 at 22:28

1 Answers1

2

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.

2012rcampion
  • 7,851
  • 25
  • 44
  • 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 00: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 00:39
  • Yes, this solution is great! I like how \.List->Plus performs the summation on it too. – Kagaratsch Jan 28 '15 at 00:41
  • One can easily imagine general cases involving Times, Sum, Total, etc., with non-trivial functions f[ii], where the OP's desired answer would be more valuable than making a Table of terms. Surely we don't want to hand code substitutions, as given by @Martin. Moreover, if we could solve the original problem, we could more easily switch back and forth between evaluated and unevaluated outputs, especially for complex equations. – David G. Stork Jan 28 '15 at 00:43
  • 2
    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 00:47
  • @DavidG.Stork You should post that as an answer, it's better than mine, although you should probably explain how it works. (Note also that you can just call ReleaseHold[%] and the expression evaluates: that's worth a +1.) – 2012rcampion Jan 28 '15 at 01:02
  • @2012rcampion: I would post my solution except the problem has been closed. Oh well. At least you know how to solve it in the future. (You might as well +1 my comment, though!) – David G. Stork Jan 28 '15 at 01:03
  • Could you please explain the (HoldForm[#] &)[ii] notation? I am most interested in knowing some details on the # operator in general. – Kagaratsch Jan 28 '15 at 01:38
  • 1
    @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 04:29