Sum
, like several other Mathematica functions, has two distinct purposes:
Compute numbers in a loop and add them up. This is a "programming use" of Sum
. It functions comparably to Table
or the for
loop of other languages.
Compute symbolic sums. This is a "mathematical use" of Sum
.
Personally, I find it a bit ugly that these two very different things are mixed together in the same function name ("Sum
").
These two uses require very different evaluation behaviour. When used as a programming construct, Sum
does not (must not) evaluate its argument before substituting in the value of the iterator. When used as a symbolic math function, there is no iteration, and therefore there is no true iterator. The argument is evaluated immediately, and analytical methods are used to arrive to a result.
With symbolic limits, Sum
will always do a symbolic calculation, and will not iterate. This is mentioned in the documentation under Details.
You need to decide which use you want. If you want it to iterate, n
must be replaced by an explicit number, to force Sum
to run a loop (and to not evaluate its argument immediately):
sum[n_Integer] := Sum[RandomChoice[{1,-1}] i, {i,n}]
A symbolic math use does not make sense here.
Personal note: I strongly dislike that these two very distinct functionalities are accessible through the same Mathematica function. I do use Sum
for symbolic calculations without worries, but I tend to avoid it for programming uses because I am never entirely sure when it will switch to using symbolic/analytic methods. The documentation does say:
If the range of a sum is finite, i
is typically assigned a sequence of values, with f
being evaluated for each one.
But what does "typically" mean? Is that a guarantee?
From the timings, it is pretty clear that Sum[i, {i, 1, 1000000000}]
is evaluated symbolically. If we change the body to something that can't be handled symbolically, like Sum[Boole@OddQ[i], {i, 1, 1000000000}]
, then it will loop. The problem is that the system decides automatically, and in a way that is not at all transparent (or controllable) to the user.
If I am going to use Sum
as a programming function (i.e. I want looping/iteration) then I must be assured that it will actually do that. Otherwise using programming (non-math) functions in the body of Sum
, like RandomChoice
, will mess things up.
Note: One specific case where I am comfortable using Sum
as a programming function is inside Compile
.
Update:
We can force Sum
to do an explicit iteration using Method -> "Procedural"
. If the limits are symbolic, it simply won't evaluate the sum, but it will still evaluate its argument prematurely.
Sum[RandomChoice[{1, -1}] i, {i, n}, Method -> "Procedural"]
(* Sum[-i, {i, n}, Method -> "Procedural"] *)
Sum
says "If a sum cannot be carried out explicitly by adding up a finite number of terms, Sum will attempt to find a symbolic result. In this case, f is first evaluated symbolically." I suspect that means in your examples that means your RandomChoice either becomes 1 or -1 and the evaluation proceeds from there. $\endgroup$Trace[Sum[RandomChoice[{-1, 1}], {i, 9}]]
doesn't seem to show "fixed"RandomChoice
for me. I'm also unsure what "natural" result you might be able to suggest should be the result ofSum[RandomChoice[{-1, 1}], {i, n}]
where n has not value assigned. Perhaps justSum[RandomChoice[{-1, 1}], {i, n}]
? $\endgroup$n = 6; sum=Total[Table[RandomChoice[{1, -1}] i, {i, n}]]
? If so, thensum
is a random variable. Do you want the distribution ofsum
for a generaln
? $\endgroup$n
is unassigned, the expression should be returned unevaluated. Ifn
is assigned an integer, it works as expected. $\endgroup$