1

I have the following simple rules:

{x2-x1->s1,
-(x2-x1)->-s1,
x3-x2->s2,
-(x3-x2)->-s2}

I want to apply that rule to the following:

c*(x3-2*x2+x1)

and get obviously

c*(s2-s1)

How can I do that without explicitly creating that rule as well? I have tried including the rule {-2*x2->-x2-x2} as well but that gets change at definition time back to {-2*x2->-2*x2}.

As a bonus question how can I get {x2-x1->s1,-(x2-x1)->-(s1)} to work without having to explicitly include the second rule. Or, said another way, how can I get {x2-x1->s1} to transform x1-x2 to -s1.

Updated: to better reflect real case

VividD
  • 3,660
  • 4
  • 26
  • 42
N D
  • 25
  • 4

2 Answers2

2

It's not a good habit to simplify expressions by constructing rules that will exactly match the output some other calculation. It's very non-robust and will break whenever Mathematica decides to output things in a slightly different way.

Here's a generic way to get the list of rules that you're looking for (I think, you didn't specify what's the generalization of your problem to 20 variables):

n = 20;
equations = Table[x[i + 1] - x[i] == s[i], {i, n}]
rules = Solve[equations, Array[x, n]]

And Simplify[c*(x[3] - 2*x[2] + x[1]) /. rules] gives c (-s[1] + s[2]).

yohbs
  • 7,046
  • 3
  • 29
  • 60
1

Here is solution:

   Expand[(x3 - 2*x2 + x1) /. {x2 -> s1 + x1, x3 -> s2 + x2}] /.x2 - x1 -> s1   
   (* -s1 + s2 *)
  • Okay, I should have said that in the real case there are 20 of those rules, so I think the rules transformation that you recommended becomes un- realistic because the number I would have to create. Is there a less heavy-handed approach that works? Also, I should have mentioned that the function is actually c(x3 - 2x2 + x1) which breaks this method. I will update the question to reflect that. – N D May 17 '17 at 13:50