1
$\begingroup$

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

$\endgroup$
4
  • 1
    $\begingroup$ You should probably get a stronger grip on Mathematica syntax $\endgroup$ Commented Sep 29, 2013 at 23:19
  • $\begingroup$ Sum[x[i]*Product[y[j], {j != i, n}], {i, 1, n}] $\endgroup$ Commented Sep 29, 2013 at 23:30
  • 1
    $\begingroup$ Perhaps Sum[x[i] Product[y[j], {j, 1, n}]/y[i], {i, 1, n}] /. n-> 4? $\endgroup$ Commented Sep 30, 2013 at 0:07
  • $\begingroup$ 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" $\endgroup$ Commented Sep 30, 2013 at 7:48

4 Answers 4

1
$\begingroup$

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] 
$\endgroup$
2
$\begingroup$

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]

$\endgroup$
2
$\begingroup$

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]

$\endgroup$
1
1
$\begingroup$

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] 
$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.