2

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.

  • I don't know what you did wrong, $sx/.{x_1^2 \to 1}$ does substitutes $x_1^2$ with $1$. – jjagmath Oct 01 '20 at 18:13
  • @Moo: Please, let me know how to re-target this question to that Universe. I did not want to violate any rules, it is just be chance... – Paul Vetrov Oct 01 '20 at 19:46
  • You can use the "flag" link (on bottom of question) and ask a moderator to move it. – Moo Oct 01 '20 at 19:49
  • @jjagmath: of course I can attach an image of my Mathematica window, but it just happens. Maybe I am missing some important setting, or flag somewhere in settings? I have a Student Edition version, is it possible it is somehow limited in functionality? – Paul Vetrov Oct 01 '20 at 19:52
  • That functionality is in every version of Mathematica. And you'll need to mess very badly with the settings to break that. Can you copy and paste the lines you're writing? – jjagmath Oct 01 '20 at 22:12

2 Answers2

1

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

jjagmath
  • 18,214
  • 1
    Yes, it does provide an answer. If Indexed is changed to Subscript the code will do what the author is expecting. – jjagmath Oct 01 '20 at 23:59
  • Yes, it provides an asnwer. Don't worry @jjagmath, more of "high rep guys" has the work of review answers and they just copy paste that text from above just to get more "popularity" – L F Oct 02 '20 at 00:28
  • @jjagmath, after changing Indexed to Subscript it started to work, but only for term, which I specify explicitly. Is it possible to specify a formula for which I need substitution? For example, in case of square terms, I need to substitute all square terms according to logic I described in my OP. Is it possible to replace all square terms without mentioning them explicitly on the line with some command? – Paul Vetrov Oct 02 '20 at 14:25
0

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.