# Sums of products with $n$ variables

I want to be able to compute a function of $$n$$ indices which looks as follows

if $$n>2$$:

$$\sum_{(i=1,j=1,k=1,l=1,...), (i\neq j\neq k \neq l \neq ...)}^{n} x_{i}x_{j}(x_{k}^2+y_{k}^2)(x_{l}^2+y^{2}_{l})(...)$$

otherwise:

$$\sum_{(i=1,j=1), (i\neq j)}^{n} x_{i}x_{j}$$

i.e for $$n=4$$ for example it would give

$$x_{1}x_{2}(x^{2}_{3}+y^{2}_{3})(x^{2}_{4}+x^{2}_{4})+x_{4}x_{2}(x^{2}_{3}+y^{2}_{3})(x^{2}_{1}+x^{2}_{1})+ \text{the other such permutations of the indices}$$

The internal part of the sum isn't a problem, neither are the conditionals. In Mathematica for $$n$$ variables this object is written like the sum of a set of n products over the same variable. The problem is that the product and the sum are over the same variables and Mathematica seems to be having a problem with the scope of the set of $$n$$ variables.

So far I have tried the following:

The bit under the sum for n variables is given by

 Product[If[k < 3, Subscript[x, Subscript[i, k]], (Subscript[x,Subscript[i, k]]^2 + Subscript[y, Subscript[i, k]]^2)], {k, 1, n}]


and produces the correct output such as

$$x_{i_{1}}x_{i_{2}}(x^{2}_{i_{3}}+y^{2}_{i_{3}})(x^{2}_{i_{4}}+x^{2}_{i_{4}})$$ for n=4 and of course this works correctly for a given n.

The tricky part comes with the summation. It's simple to see that $$i\neq j \neq k \neq ...$$ can quickly be given by something like

 If[Sequence@@Table[Subscript[i, k] != Subscript[i, k + 1], {k, 1, n - 2}],insert the product from the line above].


Now adding in the summation over the n variables such as

testfunc[n_]:= Sum[If[Sequence @@
Table[Subscript[i, k] != Subscript[i, k + 1], {k, 1, n - 2}],
Product[If[k < 3,
Subscript[x,
Subscript[i, k]], (Subscript[x, Subscript[i, k]]^2 +
Subscript[y, Subscript[i, k]]^2)], {k, 1, 4}]],
Sequence @@ Table[{Subscript[i, k], 1, n - 1}, {k, 1, n - 2}]]


and testing the test function for say n=4 gives the following error:

"The variable Sequence@@Table[{Subscript[i, k],1,4-1},{k,1,4-2}] \


cannot be localized so that it can be assigned to numerical values. ".

I think this is because of the order of the operations but I'm not entirely convinced. I think the product is not computed before all of the elements of the sum are initialised, but afterwards, so the sum throws up an error because it can't find the variable?

Any help with understand this error and solving this problem would be much appreciated. I couldn't think of another way of computing this problem in Mathematica.

• Why don't you try to use KroneckerDelta like KroneckerDelta[i, j, k ...]? – Rolf Mertig Dec 12 '18 at 14:48
• There seems to be some typos in your example: where $x^{2}_{4}+x^{2}_{4}$ should be $x^{2}_{4}+y^{2}_{4}$ ... and again similarly in the next term – wolfies Dec 12 '18 at 16:17
• @RolfMertig How would a kronecker delta help? The "if" statement producing the conditionals isn't a problem, removing it and replacing it with a kronecker delta won't solve the problem. – SuperSock Dec 12 '18 at 16:32
• Take a look at SymmetricPolynomial, AugmentedSymmetricPolynomial, and PowerSymmetricPolynomial. – JimB Dec 12 '18 at 18:42
• @JimB thanks for introducing me to those functions, I've never seen/thought about something like that before. In this case it applies but to the more general problem (which I didn't mention already) the two given answers are more flexible. – SuperSock Dec 13 '18 at 11:30

Does this accomplish what you are looking for?

h[rest_]:=Times@@Map[Subscript[x,#]^2+Subscript[y,#]^2&,rest];
g[perm_]:=Subscript[x,perm[]]*Subscript[y,perm[]]*h[Drop[perm,2]];
f[n_]:=Total[Union[Map[g,Permutations[Range[n]]]]];
f

(*x4*(x1^2 + y1^2)*(x2^2 + y2^2)*y3 + x4*(x1^2 + y1^2)*y2*(x3^2 + y3^2) +
x4*y1*(x2^2 + y2^2)*(x3^2 + y3^2) + x3*(x1^2 + y1^2)*(x2^2 + y2^2)*y4 +
x2*(x1^2 + y1^2)*(x3^2 + y3^2)*y4 + x1*(x2^2 + y2^2)*(x3^2 + y3^2)*y4 +
x3*(x1^2 + y1^2)*y2*(x4^2 + y4^2) + x3*y1*(x2^2 + y2^2)*(x4^2 + y4^2) +
x2*(x1^2 + y1^2)*y3*(x4^2 + y4^2) + x1*(x2^2 + y2^2)*y3*(x4^2 + y4^2) +
x2*y1*(x3^2 + y3^2)*(x4^2 + y4^2) + x1*y2*(x3^2 + y3^2)*(x4^2 + y4^2) *)


I replaced the Subscript with plain ascii for compactness in displaying that result.

• It does and it's nice and elegant as well. Thanks! – SuperSock Dec 12 '18 at 18:41
• I did for a handful of different cases. It's sufficient for what I need. By elegant I mean it's both clear, compartmentalised and with the use of map it should also be memory efficient. – SuperSock Dec 12 '18 at 22:54
ClearAll[g, rules]
rules[n_] := {Power[a : Subscript[x, Alternatives @@ #], 2] :> a,
Power[Subscript[y, Alternatives @@ #], 2] -> 0} & /@ Subsets[Range[n], {2}]
g[n_] := Plus@@(Times@@(Array[Subscript[x, #] &, n]^2 + Array[Subscript[y, #] &, n]^2) /.
rules[n])


Examples:

g // TeXForm


$$x_1 x_2$$

g // TeXForm


$$x_2 x_3 \left(x_1^2+y_1^2\right)+x_1 x_3 \left(x_2^2+y_2^2\right)+x_1 x_2 \left(x_3^2+y_3^2\right)$$

g // TeXForm


$$x_3 x_4 \left(x_1^2+y_1^2\right) \left(x_2^2+y_2^2\right)+x_1 x_4 \left(x_3^2+y_3^2\right) \left(x_2^2+y_2^2\right)+x_1 x_3 \left(x_4^2+y_4^2\right) \left(x_2^2+y_2^2\right)+ \\ x_2 x_4 \left(x_1^2+y_1^2\right) \left(x_3^2+y_3^2\right)+x_2 x_3 \left(x_1^2+y_1^2\right) \left(x_4^2+y_4^2\right)+x_1 x_2 \left(x_3^2+y_3^2\right) \left(x_4^2+y_4^2\right)$$