3
$\begingroup$

This rule works fine, of course:

a x + b /. a x + b -> z (* z *) 

However, this one does not:

a x + b /. -a x - b -> -z (* b + a x *) 

I understand that when one expands the expressions using FullForm the patterns do not match.

This works:

w /. Solve[w == a x + b && z == -a x - b, {w}, {a, x, b}][[1]] (* -z *) 

But it looks way too complicated. Is there a smarter way?

(Of course in this particular example there is an obvious solution: the first command shown above. Let us assume the perfectly matching pattern is not known)

Thanks for the help.

$\endgroup$
11
  • 4
    $\begingroup$ Use PolynomialReduce. In[9]:= PolynomialReduce[a x + b, {-a x - b + z}, {a, b}][[2]] Out[9]= z $\endgroup$ Commented Oct 5, 2016 at 15:17
  • 1
    $\begingroup$ Not sure if this helps but you can also use FullSimplify: FullSimplify[ax + b, Assumptions -> (-ax - b == -z)] $\endgroup$ Commented Oct 5, 2016 at 15:24
  • 1
    $\begingroup$ Or even simpler: Simplify[ax + b, (-ax - b == -z)] $\endgroup$ Commented Oct 5, 2016 at 15:32
  • $\begingroup$ @DanielLichtblau - Thanks, it works! I saw a "2" besides your comment. Can I accept a comment? $\endgroup$ Commented Oct 5, 2016 at 16:26
  • 1
    $\begingroup$ @FernandoSaldanha Much as I appreciate the gesture, the fact is that this is likely to be closed as a duplicate. See 1 or 2 or 3 (and there are some others as well). $\endgroup$ Commented Oct 5, 2016 at 16:32

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.