5
$\begingroup$

This question has already been asked three years ago (Is there a way to Collect[] for more than one symbol?) but I'm not allowed to comment since I'm new at Mathematica StackExchange.

The first suggested solution didn't work in Mathematica 10.0 and the other solutions gave expressions containing x as well as x-y. However, I search for an expression only containing x-y. So do you know another solution for this Mathematica version?

Here is the original question:

"Oftentimes you find yourself looking for polynomials in multiple variables. Consider the following expression:

a(x - y)^3 +  b(x - y) + c(x - y) + d

as you can see this is clearly a Polynomial in x-y. Is there an equivalent of Collect, that works on more complicated expressions than just a single variable? I would like to have something similar to

Collect[%,x-y] = a(x - y)^3 + (b+c)(x - y) + d

however. Collect can not work on x-y. Of course you could solve this first example by substituting x-y -> z then Collect the z and afterwards substitute backwards like so:

a(x - y)^3 +  b(x - y) + c(x - y) + d /. x-y->z

gives

a^3 + b z + c z + d

then

Collect[ a z^3 + b z + c z + d ]

gives

a z^3 + (b+c)z + d

now undo the substitution by running % /. z -> x - y. This gives the desired result:

d + (b + c) (x - y) + a (x - y)^3

So this is good. For obvious polynomials, we can solve this. But what about real world examples? Would you have guessed that

d + b x + c x + a x^3 - b y - c y - 3 a x^2 y + 3 a x y^2 - a y^3

is exactly the same polynomial? How would you Collect x-y here, as you cannot do the substitution?"

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. $\endgroup$ – bbgodfrey Apr 15 '15 at 10:56
  • 1
    $\begingroup$ PolynomialReduce is good for this sort of thing. poly = d + b x + c x + a x^3 - b y - c y - 3 a x^2 y + 3 a x y^2 - a y^3; PolynomialReduce[poly, (x - y) - t, {x, y}][[2]] /. t -> x - y $\endgroup$ – Daniel Lichtblau Apr 15 '15 at 15:00
3
$\begingroup$
 expr = d + b x + c x + a x^3 - b y - c y - 3 a x^2 y + 3 a x y^2 - 
   a y^3;

Collect[(expr /. x -> y + z), z] /. z -> x - y

(*   d + (b + c) (x - y) + a (x - y)^3  *)

Of course, in the real word case, when you do not know apriori, how to simplify the expression, you have no idea of what replacement to apply. But this is exactly the place, where science transforms itself into art.

Have fun still!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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