I have the following simple rules:


I want to apply that rule to the following:


and get obviously


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


2 Answers 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]).


Here is solution:

   Expand[(x3 - 2*x2 + x1) /. {x2 -> s1 + x1, x3 -> s2 + x2}] /.x2 - x1 -> s1   
   (* -s1 + s2 *)
  • $\begingroup$ 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 - 2*x2 + x1) which breaks this method. I will update the question to reflect that. $\endgroup$
    – N D
    May 17, 2017 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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