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?"
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$