Wolfram: doing substitution on indexed terms

81 Views Asked by At

I have

sx = Sum[Indexed[x, i], {i, 1, 10}]^2 // Expand

It expands in sum of 55 terms, from which

  1. $x_i^2$ - 10 terms, actual representation: $x_1^2+x_2^2+x_3^2+...$
  2. $2 \cdot x_i \cdot x_{i+1}$ - 9 terms, actual representation: $2 x_1 x_2 + 2 x_2 x_3 + ...$
  3. $2 \cdot x_i \cdot x_{i+l}, l>1$ - all the rest, actual representation: $2 x_1 x_3 + 2 x_2 x_4 + ...$

For my calculations I need to replace terms

  1. => $p \cdot (1-p)$
  2. => 0
  3. => $2 \cdot p^2 \cdot (1-p)^2$

I tried to perform straight-forward

sx1 = sx /. $\{x_1^2 -> 1\}$

but nothing happened at all.

What I would like to achieve: some small easy to maintain rule formula to perform such substitutions. I saw Wolfram Mathematica is very difficult with values with subscripts. Is it possible, or should I look for some another alternative?

Thanks.

2

There are 2 best solutions below

0
On BEST ANSWER

And finally I found a solution for my own problem.

  • Replacing all square terms with p*(1-p):
sx = sx /. Table[Subscript[x, i]^2 -> p*(1 - p), {i, 1, 10}]
  • Replacing $x_i \cdot x_{i+1}$ terms with $0$:
sx = sx /. Table[Subscript[x, i]*Subscript[x, i + 1] -> 0, {i, 1, 9}]
  • Replacing $x_i \cdot x_{i+l}, l>1$ terms with $p^2 \cdot (1-p)^2$
sx = sx /. Table[Subscript[x, i]*Subscript[x, i + 2] -> p^2*(1-p)^2, {i, 1, 8}]

Unfortunately, last statement must be edited and repeated many times to correct second subscript index. Let's say, provided example is replacing terms like $x_1 \cdot x_3, \text{... } x_8 \cdot x_{10}$ with $p^2 \cdot (1-p)^2$, while to replace other term patterns a bit different expressions are necessary. I tried to use double-indexing in Table[] function, but it creates 2D array, which is not doing what is intended.

I would like to express my appreciation to all auditory of this problem, without your help I would not get to the solution. I think this solution could be made more elegant, but I do not have yet necessary knowledge to achieve it.

3
On

Oh, sorry, I spotted the problem. You don't want Indexed[x,i], you should write Subscript[x,i]