Help with writing a polynomial series [duplicate]

This question already has an answer here:

Sum or Product with Exclusions

2 answers

I want to write and evaluate an expression something like

Sum[x[i] Product[y[j], {j(!=i), 1, n}], {i, 1, n}]

but with correct syntax, where n is a any number (say 4 as a concrete example).

=================

=================

4 Answers
4

=================

A simple, if not elegant way to do it, is

With[{n = 4}, Sum[x[i] Product[If[j == i, 1, y[j]], {j, 1, n}], {i, 1, n}]]

x[4] y[1] y[2] y[3] + x[3] y[1] y[2] y[4] + x[2] y[1] y[3] y[4] + x[1] y[2] y[3] y[4]

Also

Tr[Times @@@ SparseArray[{{i_, i_} -> x@i, {i_, j_} -> y@j}, {4, 4}]]

x[4] y[1] y[2] y[3] + x[3] y[1] y[2] y[4] + x[2] y[1] y[3] y[4] + x[1] y[2] y[3] y[4]

Here’s a fairly direct implementation of what you wanted:

n = 4;
Sum[x[i] Product[y[j], {j, Delete[Range[n], i]}], {i, 1, n}]

and here’s a way that uses Mathematica’s pattern matching facilities:

n = 4;
Plus@@ReplaceList[Product[y[j], {j, 1, n}], y[i_] rest_ :> x[i] rest]

Both return

x[4] y[1] y[2] y[3] + x[3] y[1] y[2] y[4] + x[2] y[1] y[3] y[4] + x[1] y[2] y[3] y[4]

  

 

The ReplaceList version is the fastest submitted so far: gist.github.com/simonjtyler/6759252
– Simon
Sep 30 ’13 at 4:04

Another approach defines f to be all the x’s and g to be all the y’s. The sum of the products can then be written concisely as

n = 4; f = Array[x, n]; g = Array[y, n];
Total[f[[#]] (Times @@ g)/g[[#]] & /@ Range[n]]

which gives the desired sum

x[4] y[1] y[2] y[3] + x[3] y[1] y[2] y[4] + x[2] y[1] y[3] y[4] + x[1] y[2] y[3] y[4]