7
$\begingroup$

How can I evaluate such a sum:

$$ \sum_{j=0, j\neq10}^{J} f(j) $$

Since I am trying to do some symbolic calculations, solutions such as the one below are undesirable:

$$ -f(10) + \sum_{j=0}^{J} f(j) $$

Thanks.

$\endgroup$
9
$\begingroup$

You can start by creating a list of all the values you want to iterate through:

indices = DeleteCases[Range[0,J],10];

And then do either

Sum[f[i], {i, indices}]

or

Total[f /@ indices]
$\endgroup$
3
$\begingroup$

You can use an If statement:

Sum[If[i == 10, 0, f[i]], {i, J}]
$\endgroup$
  • $\begingroup$ It's probably equivalent but I find this syntax more elegant: Sum[f[j] Boole[j != 10], {j, 0, J}] $\endgroup$ – Rahul Nov 4 '14 at 13:04
1
$\begingroup$

There are two ways basically. First one:

Sum[f(j)*Boole[j != 10], {j, 0, 10}]

Another way is:

Sum[If[j != 10, Sin[1] Cos[j], 0], {j, 0, 10}]

See this link too: How do you put conditions on indices in a sum?

$\endgroup$
0
$\begingroup$

The solutions given above are correct, and probably the best way to go.

But if you can be absolutely certain that f(10) is not an expensive call, it might even be better to do something like what you are saying, since it would avoid having to making a comparison for each index i.

So you could do the following:

g[x_] := f[x];
g[10] := 0;
Total[g /@ Range[100000]]
$\endgroup$

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