Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

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

share|improve this question

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

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
You should probably get a stronger grip on Mathematica syntax –  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
1  
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

4 Answers 4

up vote 1 down vote accepted

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]
share|improve this answer

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]

share|improve this answer

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]

share|improve this answer
    
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]
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.