# Summation and product term expressed in Mathematica [duplicate]

This question already has an answer here:

I have the following term which I would like to express correctly in Mathematica:

$$\sum_{i=1}^{m}\frac{1}{\prod_{j=1,j\neq i}^m(\rho_i-\rho_j)}$$

This means that for $$m=3$$ for example you should get the following

$$\frac{1}{(\rho_1-\rho_2)(\rho_1-\rho_3)}+\frac{1}{(\rho_2-\rho_1)(\rho_2-\rho_3)}+\frac{1}{(\rho_3-\rho_2)(\rho_3-\rho_1)}$$

## marked as duplicate by Roman, m_goldberg, corey979, user64494, MarcoBJun 5 at 14:45

• There is no duplication since the other question does not have a sum and thus the $i$ is not well defined. I chose to put a new question instead of creating some confusion. – Y.L Jun 5 at 14:44

f[m_Integer] := Sum[1/Product[(Subscript[\[Rho], i] - Subscript[\[Rho], j]),
{j, Complement[Range[1, m], {i}]}], {i, m}]

f • Thanks @Chris Degnen this is easy to implement into my calculations. – Y.L Jun 5 at 15:28
With[{m = 3},
Sum[1/Product[ρ[i] - ρ[j], {j, DeleteCases[Range[m], i]}], {i, m}]]


$$\frac{1}{(\rho (2)-\rho (1)) (\rho (2)-\rho (3))}+\frac{1}{(\rho (3)-\rho (1)) (\rho (3)-\rho (2))}+\frac{1}{(\rho (1)-\rho (2)) (\rho (1)-\rho (3))}$$

Here I used ρ[i] instead of Subscript[ρ,i] because subscripts are a pitfall for new users.

Update: It's easier to use Drop instead of DeleteCases, as in @kglr's solution:

With[{m = 3},
Sum[1/Product[ρ[i] - ρ[j], {j, Drop[Range[m], {i}]}], {i, m}]]

ClearAll[f1, f2]
f1[m_] := Module[{a = Array[ρ, m]}, m /
HarmonicMean @ MapIndexed[Apply[Times] @* Drop, Outer[Subtract, a, a]]

f2[m_] := Module[{a = Array[ρ, m]},
m Moment[ MapIndexed[Apply[Times] @* Drop, Outer[Subtract, a, a], -1]]

f1 f1 And @@ (f1[#] == f2[#] & /@ Range)


True