8

I defined a function in Mathematica. It is a function of n, so f[n_]:=, but in the definition, I used a sum, $\sum_{k=0}^n$. So, the $k$ is just an index variable for the sum and no $k$ shows up in the final answer. As I was using this function I tried evaluating f[k-1] and got a weird answer, 0. I finally figured out that Mathematica was trying to do the sum $\sum_{k=0}^{k-1}$, or so I guess. So, my question is, is there a way to make the $k$ local so that this error never occurs? My fix for now was to change $k$ to $index$ and I will probably not use f[index] at any point.

GeoffDS
  • 11,270
  • I tried to reproduce your result, but no luck. Could you post your function? What version are you using? – Calle Mar 24 '11 at 21:29
  • Just about anything will do... g[n_]:=$\sum_{k=0}^n \binom{n}{k}$ and then g[k-1] will yield 0 in Version 7. – GeoffDS Mar 24 '11 at 21:45
  • Huh, yeah. In version 8 too. But g[n_]:= $\sum_{k=0}^n k^2$ works fine. – Calle Mar 24 '11 at 21:52
  • So, maybe it's the Binomial coefficient being 0 that is causing the problem. – GeoffDS Mar 24 '11 at 22:03
  • @Numth @Calle I cannot reproduce this in 8. The help for "Sum" states "The iteration variable i is treated as local, effectively using Block." Your function g (square or Binomial version) works fine as-is or protected with 'Block' or 'Module'. I'm commenting because I do run into similar problems on occasion and haven't yet been able to characterize them, so any suggestions are welcome. – whuber Mar 24 '11 at 22:06
  • Even with the Binomial coefficient? – GeoffDS Mar 24 '11 at 22:18
  • 1
    @Numth @Calle @whuber: Mathematica make the summation index local using Block. In this way $k$ has a local value for the Sum function. However, $k$ is still $k$ so that g[k-1] translates into Sum[Binomial[k-1,k],{k,0,k-1}] which will evaluate to 0 (because Binomial[k-1,k]=0). What you want is that the summation index gets a different name and therefore you need Module. With Module the g[k-1] is effectively translated into Sum[Binomial[k-1,k$],{k$,0,k-1}]. – Fabian Mar 24 '11 at 22:32
  • @whuber, yes, they both work with Block or Module. @Fabian, what I find strange is that i don't get strange results when I use a $k^2$ instead of the binomail coefficient. – Calle Mar 25 '11 at 00:13
  • @Calle @Fabian My point is that I have no problems at all. For example, with g[n_] := Sum[Binomial[n, k], {k, 0, n}], the result of g[k-1] is 2^(-1+k), which is correct. – whuber Mar 25 '11 at 02:35
  • @whuber That is the result I got when I did g[n_]= without the :. But, when I do g[n_]:=, I do get 0 for g[k-1]. – GeoffDS Mar 25 '11 at 02:41
  • 1
    @Numth Thank you. I double-checked and was able to reproduce your results. (a) After quitting all kernels and re-evaluating the definition of g, I obtain zero for g[k-1]. (b) After issuing Remove[k], I get the correct result. (c) Protecting g with either Block or Module makes it work correctly. Clearly, then, the help statement is not entirely correct. – whuber Mar 25 '11 at 02:52

3 Answers3

7

The function you are looking for is called Module. You can define it as

f[n_] := Module[{k}, Sum[a[k], {k,0,n}]]

so that the evaluation f[k-1] is possible.

Fabian
  • 23,360
7

Block might be better than Module -- when you use recursion, a local variable defined in Module will be shared across stack frames, Block will have a separate version of the variable in each call

  • @Bulatov: but Block will not work for the OP's problem... – Fabian Mar 25 '11 at 05:50
  • @Fabian Why not? I tried your snippet in Mathematica 8 and replacing Module with Block makes no difference – Yaroslav Bulatov Mar 25 '11 at 17:31
  • defining g[n_]:=Block[{k},Sum[Binomial[n,k],{k,0,n}] and then executing g[k-1] gives a result different from 0? (then they must have changed something going to version 8) – Fabian Mar 25 '11 at 18:22
  • 1
    @Fabian I was too hasty, you are right, it doesn't work for that example, another approach might be to use Unique – Yaroslav Bulatov Mar 25 '11 at 19:49
3

An alternative to Module or Block is to use Formal Symbols. This allows for the cleanest code.

One may still run into trouble depending on how you choose to use Formal symbols elsewhere, but if you never use \[FormalK] in the argument to f you are safe.

Mr.Wizard
  • 802