# Help with writing a polynomial series [duplicate]

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).

-

## marked as duplicate by Mr.Wizard♦Sep 30 '13 at 7:47

You should probably get a stronger grip on Mathematica syntax – Dr. belisarius Sep 29 '13 at 23:19
Sum[x[i]*Product[y[j], {j != i, n}], {i, 1, n}] – santosh Sep 29 '13 at 23:30
Perhaps Sum[x[i] Product[y[j], {j, 1, n}]/y[i], {i, 1, n}] /. n-> 4? – Michael E2 Sep 30 '13 at 0:07
I have marked this as a duplicate, because based on j(!=i) (sic) I believe it is. If the answers there do not address your question please edit yours to make clear the difference reply here starting your comment with "@Mr.Wizard" – Mr.Wizard Sep 30 '13 at 7:48

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]

-

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]

-

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