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?
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 *)
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. $\endgroup$
If[]orBoole[]is useful. $\endgroup$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! $\endgroup$