3

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.

Soldalma
  • 1,289
  • 1
  • 9
  • 17
  • 4
    Use PolynomialReduce. `In[9]:= PolynomialReduce[a x + b, {-a x - b + z}, {a, b}][[2]]

    Out[9]= z`

    – Daniel Lichtblau Oct 05 '16 at 15:17
  • 1
    Not sure if this helps but you can also use FullSimplify: FullSimplify[ax + b, Assumptions -> (-ax - b == -z)] – Frank Martin Oct 05 '16 at 15:24
  • 1
    Or even simpler: Simplify[ax + b, (-ax - b == -z)] – Frank Martin Oct 05 '16 at 15:32
  • @DanielLichtblau - Thanks, it works! I saw a "2" besides your comment. Can I accept a comment? – Soldalma Oct 05 '16 at 16:26
  • @FrankMartin - Thanks for the suggestions, but neither one of them works if you change the sign of z. For example, – Soldalma Oct 05 '16 at 16:27
  • 1
    @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). – Daniel Lichtblau Oct 05 '16 at 16:32
  • @FrankMartin - Thanks for the suggestions, but neither one of them works. I had a*x +b, not ax+b, but even independently of that, if you change the sign in front of z you get back ax+b. For example, Simplify[ax + b, (-ax - b == z)] returns ax+b. – Soldalma Oct 05 '16 at 16:34
  • @FernandoSaldanha Simplify[a*x + b, -a*x - b == -z] returns 'z' for me and also Simplify[a*x + b, -a*x - b == z] returns '-z' for me. Could you please check that? (I'm working in Mathematica 11) – Frank Martin Oct 06 '16 at 08:24
  • @FrankMartin Indeed, with ''between the a and the x, Simplify[ax + b, -ax - b == -z] returns 'z' and also Simplify[ax + b, -ax - b == z] returns '-z'. However, (mysteriously, at least for me), without a between the z and the x, Simplify[ax + b, (-ax - b == z)] returns 'ax + b'. I am also using Mathematica 11. – Soldalma Oct 06 '16 at 14:31
  • @FrankMartin Sorry for the mess in my previous comment. There is a problem with the asterisk character. What I wanted to say is that with an asterisk between the a and the x, meaning Times[a,x], both commands work fine. However, without the asterisk, that is, with variable ax, one of them does not, that is, Simplify[ax + b, -ax - b == z] returns ax+b. I quit the kernel and restarted it before evaluating this. – Soldalma Oct 06 '16 at 14:44
  • @FernandoSaldanha, Thanks for your time to reply. I'm a bit confused why it doesn't work. Might get back to that later. – Frank Martin Oct 10 '16 at 08:06

0 Answers0