# Sum or Product with Exclusions

Is there a built-in feature for handling things like:

$$\sum_{\substack{i=0\\i\ne j}}^n\frac{a-a_i}{a_i-a_j}$$ and $$\prod_{\substack{i=0\\i\ne j}}^n\frac{a-a_i}{a_i-a_j}$$

or should I work out some sort of Do statement?

• Either of If[] or Boole[] is useful. – J. M.'s torpor Aug 10 '12 at 13:32
• @J.M. Yes, I ended up using Product[If[k != j, (x - l[[j]])/(l[[k]] - l[[j]]), 1], {j, 1, Length[l]}], just wondering if there was something built in to take care of things like this. Thanks for the suggestions! – process91 Aug 10 '12 at 13:39
• Have you seen this, BTW? – J. M.'s torpor Aug 10 '12 at 14:10
• @J.M. I had not - that's perfect, care to make it an answer? – process91 Aug 10 '12 at 14:16

## 2 Answers

The documentation for Product[] gives a nice example that you can adapt to your needs:

With[{j = 2, n = 6},
Sum[(a - Subscript[a, i])/(Subscript[a, i] - Subscript[a, j]),
{i, Complement[Range[0, n], {j}]}]]

(a - Subscript[a, 0])/(Subscript[a, 0] - Subscript[a, 2]) +
(a - Subscript[a, 1])/(Subscript[a, 1] - Subscript[a, 2]) +
(a - Subscript[a, 3])/(-Subscript[a, 2] + Subscript[a, 3]) +
(a - Subscript[a, 4])/(-Subscript[a, 2] + Subscript[a, 4]) +
(a - Subscript[a, 5])/(-Subscript[a, 2] + Subscript[a, 5]) +
(a - Subscript[a, 6])/(-Subscript[a, 2] + Subscript[a, 6])


and similarly for Product[]. Alternatively, you can do

With[{j = 2, n = 6},
Sum[(a - Subscript[a, i])/(Subscript[a, i] - Subscript[a, j]),
{i, DeleteCases[Range[0, n], j]}]]


J. M.'s method is appropriate for sums (and products) that will be computed iteratively.
It is however quite inappropriate for sums that can be computed symbolically.

You could use Piecewise in the latter case. Here is an illustration using a very simple function:

Sum[5 i, {i, Range[1*^7] ~Delete~ 777}] // Timing


{2.543, 250000024996115}

Sum[Piecewise[{{0, i == 777}}, 5 i], {i, x}] /. x -> 1*^7 // Timing


{0.047, 250000024996115}

The output of the symbolic sum is itself a Piecewise function:

Sum[Piecewise[{{0, i == 777}}, 5 i], {i, x}]

Piecewise[{{5, x == 1}, {(5*x*(1 + x))/2, 1 < x < 777] || x < 1}}, (5*(-1554 + x + x^2))/2]


Boole often performs poorly with the default Method:

Sum[Boole[i != 777] 5 i, {i, x}] /. x -> 1*^7 // Timing


{11.403, 250000024996115}

(Symbol x above should be \[FormalX] for safety, but I kept the simple form for brevity.)

One can also compute infinite sums in this fashion, e.g.:

Sum[
Piecewise[{{0, i == 3 || i == 5 || i == 7}}, 1/i^6],
{i, ∞}
]

(-1935422371 + 1418090625 π^6)/1340095640625


It may be simpler to compute two sums and subtract when possible:

Sum[1/i^6, {i, ∞}] - Sum[1/i^6, {i, {3, 5, 7}}]


Beware when using conditions that evaluate on non-numeric arguments (e.g. PrimeQ):

Sum[Piecewise[{{0, PrimeQ[i]}}, i], {i, x}]

1/2 x (1 + x)   (* clearly incorrect *)

• @MrWizard: Did you inform Wolfram about this error? It is still there in v10. – Jinxed Mar 5 '15 at 22:08
• @Jinxed No, I did not report the error. It would be nice if a warning was given but IMHO it belongs to a class of problems where user intelligence is necessary. I mean by that that there are many things Mathematica fails to "think through" for you. Such things are not really bugs but limitations, and while it would be nice to see those limitations pushed back (prioritized over social networking functions *cough*) the system still has ample value as it is if one is willing to work with it. (continued) – Mr.Wizard Mar 6 '15 at 2:33
• In this case it seems reasonable to conclude that Mathematica will not be able to give a closed form for the sum of prime numbers, or their complement, so it would be best not to set it up to fail. If we use f[i_Integer] := Piecewise[{{0, PrimeQ[i]}}, i]; Sum[f[i], {i, x}] it returns unevaluated. If we use use Sum[f[i], {i, 1*^6}] it evaluates numerically and returns 462450097977. However I encourage you to report this issue yourself as these reports may help Wolfram Research see that such things are important to its users and improve these core and mathematical functions. – Mr.Wizard Mar 6 '15 at 2:36
• @MrWizard: I will do that, since what I expect from Mathematica is not necessarily a result for everything, but if a result is produced, it shall be correct. I'd rather live with an unevaluated "results" than a wrong one. – Jinxed Mar 6 '15 at 8:50