Collecting the terms of a multi-variable polynomial

Suppose I have a polynomial $$P(x,\,y)=a^5+b^2x+c\,y+d\,x^2+e\,x+f^3y+g\,y^2+h\,x^2$$ and I want to collect it in terms of main variables $x,\,y$ to get $$P(x,\,y)=a^5+(b^2+e)x+(c+f^3)y+(d+h)x^2+g\,y^2$$ How do I use Collect or similar commands to perform the task?

Input

P == a^5 + b^2*x + c*y + d*x^2 + e*x + f^3*y + g*y^2 + h*x^2

Output wanted

P(x, y) = a^5 + (b^2 + e)*x + (c +f^3)*y + (d + h)*x^2 + g*y^2

A substantial example

Collect[b^4 m^4 - 2 a^2 b^2 m^2 n^2 + a^4 n^4 - 4 b^3 m^4 R +
4 a^2 b m^2 n^2 R + 4 a b^2 m^2 n^2 R - 4 a^3 n^4 R +
4 b^2 m^4 R^2 - 8 a b m^2 n^2 R^2 + 4 a^2 n^4 R^2 + 2 b^2 m^4 S -
2 a^2 m^2 n^2 S - 2 b^2 m^2 n^2 S + 2 a^2 n^4 S - 4 b m^4 R S +
4 a m^2 n^2 R S + 4 b m^2 n^2 R S - 4 a n^4 R S + m^4 S^2 -
2 m^2 n^2 S^2 + n^4 S^2 - 2 b^2 c^2 m^2 T^2 - 2 a^2 c^2 n^2 T^2 +
4 b c^2 m^2 R T^2 + 4 a c^2 n^2 R T^2 - 2 c^2 m^2 S T^2 -
2 c^2 n^2 S T^2 + c^4 T^4, {R, S}]

results in

b^4 m^4 - 2 a^2 b^2 m^2 n^2 + a^4 n^4 +
(4 b^2 m^4 - 8 a b m^2 n^2 + 4 a^2 n^4) R^2 +
(m^4 -2 m^2 n^2 + n^4) S^2 -
2 b^2 c^2 m^2 T^2 - 2 a^2 c^2 n^2 T^2 + c^4 T^4 +
S (2 b^2 m^4 - 2 a^2 m^2 n^2 - 2 b^2 m^2 n^2 + 2 a^2 n^4 -
2 c^2 m^2 T^2 - 2 c^2 n^2 T^2) +
R ( -4 b^3 m^4 + 4 a^2 b m^2 n^2 + 4 a b^2 m^2 n^2 - 4 a^3 n^4 +
(-4 b m^4 + 4 a m^2 n^2 + 4 b m^2 n^2 - 4 a n^4) S +
4 b c^2 m^2 T^2 + 4 a c^2 n^2 T^2   )

Note several issues:

1) The last term appears as $R (A+BS+C)$ instead of $(A+C)R+BRS$

2) One would expect a standard form, for example $AR^2+BRS+CS^2+DR+ES+F$, for the outcome but the result does not seem to follow a discernible order.

3) The constant term , the one without $R$ or $S$, is split.

• What's wrong with Collect[P, {x, y}]? – J. M. will be back soon May 20 '17 at 14:29
• @J.M. How do you prevent it from doing partial factorization, as in writing $x(2x+3y)$ instead of $2x^2+3xy$. – Maesumi May 20 '17 at 20:49
• When I evaluate Collect[a^5 + b^2*x + c*y + d*x^2 + e*x + f^3*y + g*y^2 + h*x^2, {x, y}], I get the result you ask for and none of the additional factoring you allude to in your comment. – m_goldberg May 20 '17 at 22:40
• Judging by your comment, you have a weak example in that there are no "mixed" terms having factors of both $x$ and $y$. – Michael E2 May 20 '17 at 22:45
• @m_goldberg an example showing partial factorization is provided – Maesumi May 21 '17 at 16:41

You can use the third argument of Collect to bind the coefficients in something like Hold. Afterwards, release the hold or replace wrapper by Identity.

ReleaseHold@
Expand@Collect[poly =
b^4 m^4 - 2 a^2 b^2 m^2 n^2 + a^4 n^4 - 4 b^3 m^4 R +
4 a^2 b m^2 n^2 R + 4 a b^2 m^2 n^2 R - 4 a^3 n^4 R +
4 b^2 m^4 R^2 - 8 a b m^2 n^2 R^2 + 4 a^2 n^4 R^2 + 2 b^2 m^4 S -
2 a^2 m^2 n^2 S - 2 b^2 m^2 n^2 S + 2 a^2 n^4 S - 4 b m^4 R S +
4 a m^2 n^2 R S + 4 b m^2 n^2 R S - 4 a n^4 R S + m^4 S^2 -
2 m^2 n^2 S^2 + n^4 S^2 - 2 b^2 c^2 m^2 T^2 - 2 a^2 c^2 n^2 T^2 +
4 b c^2 m^2 R T^2 + 4 a c^2 n^2 R T^2 - 2 c^2 m^2 S T^2 -
2 c^2 n^2 S T^2 + c^4 T^4,
{R, S}, Hold]
(*
b^4 m^4 - 2 a^2 b^2 m^2 n^2 + a^4 n^4 +
(4 b^2 m^4 - 8 a b m^2 n^2 + 4 a^2 n^4) R^2 +
(-4 b m^4 + 4 a m^2 n^2 + 4 b m^2 n^2 - 4 a n^4) R S +
(m^4 - 2 m^2 n^2 + n^4) S^2 -
2 b^2 c^2 m^2 T^2 - 2 a^2 c^2 n^2 T^2 + c^4 T^4 +
S (2 b^2 m^4 - 2 a^2 m^2 n^2 - 2 b^2 m^2 n^2 + 2 a^2 n^4 -
2 c^2 m^2 T^2 - 2 c^2 n^2 T^2) +
R (-4 b^3 m^4 + 4 a^2 b m^2 n^2 + 4 a b^2 m^2 n^2 - 4 a^3 n^4 +
4 b c^2 m^2 T^2 + 4 a c^2 n^2 T^2)
*)

Other wrappers can be fun:

Expand@Collect[poly, {R, S}, Style[#, Red] &] To get a usable expression, execute this:

% /. Style -> (# &)

Defer makes for nice output that can be copied as input without further processing.

Expand@Collect[poly, {R, S}, Defer] • You seem to have n=2 somewhere. +1 – jjc385 May 21 '17 at 19:12

This is your polynomial:

p = b^4 m^4 - 2 a^2 b^2 m^2 n^2 + a^4 n^4 - 4 b^3 m^4 R +
4 a^2 b m^2 n^2 R + 4 a b^2 m^2 n^2 R - 4 a^3 n^4 R +
4 b^2 m^4 R^2 - 8 a b m^2 n^2 R^2 + 4 a^2 n^4 R^2 + 2 b^2 m^4 S -
2 a^2 m^2 n^2 S - 2 b^2 m^2 n^2 S + 2 a^2 n^4 S - 4 b m^4 R S +
4 a m^2 n^2 R S + 4 b m^2 n^2 R S - 4 a n^4 R S + m^4 S^2 -
2 m^2 n^2 S^2 + n^4 S^2 - 2 b^2 c^2 m^2 T^2 - 2 a^2 c^2 n^2 T^2 +
4 b c^2 m^2 R T^2 + 4 a c^2 n^2 R T^2 - 2 c^2 m^2 S T^2 -
2 c^2 n^2 S T^2 + c^4 T^4;

Try this:

lst = Drop[Flatten[Table[R^n*S^m, {n, 0, 2}, {m, 0, 2}], 1], 1]

(* {S, S^2, R, R S, R S^2, R^2, R^2 S, R^2 S^2}  *)

and then

Collect[p, lst]

(*  b^4 m^4 - 2 a^2 b^2 m^2 n^2 +
a^4 n^4 + (4 b^2 m^4 - 8 a b m^2 n^2 + 4 a^2 n^4) R^2 + (-4 b m^4 +
4 a m^2 n^2 + 4 b m^2 n^2 - 4 a n^4) R S + (m^4 - 2 m^2 n^2 +
n^4) S^2 - 2 b^2 c^2 m^2 T^2 - 2 a^2 c^2 n^2 T^2 + c^4 T^4 +
S (2 b^2 m^4 - 2 a^2 m^2 n^2 - 2 b^2 m^2 n^2 + 2 a^2 n^4 -
2 c^2 m^2 T^2 - 2 c^2 n^2 T^2) +
R (-4 b^3 m^4 + 4 a^2 b m^2 n^2 + 4 a b^2 m^2 n^2 - 4 a^3 n^4 +
4 b c^2 m^2 T^2 + 4 a c^2 n^2 T^2)  *)

To address your question in the comment

One may do as follows

p1 = p /. {R -> 0, S -> 0};
p2 = p - p1;
Collect[p2, lst] + Simplify[p1]

(* b^4 m^4 + (4 b^2 m^4 - 8 a b m^2 n^2 + 4 a^2 n^4) R^2 + (-4 b m^4 +
4 a m^2 n^2 + 4 b m^2 n^2 - 4 a n^4) R S + (m^4 - 2 m^2 n^2 +
n^4) S^2 + (a^2 n^2 - c^2 T^2)^2 - 2 b^2 m^2 (a^2 n^2 + c^2 T^2) +
S (2 b^2 m^4 - 2 a^2 m^2 n^2 - 2 b^2 m^2 n^2 + 2 a^2 n^4 -
2 c^2 m^2 T^2 - 2 c^2 n^2 T^2) +
R (-4 b^3 m^4 + 4 a^2 b m^2 n^2 + 4 a b^2 m^2 n^2 - 4 a^3 n^4 +
4 b c^2 m^2 T^2 + 4 a c^2 n^2 T^2) *)

Have fun!

• You can also use patterns {R, S, S^_, R^_, S*R, S^_*R^_} for the second argument, and it extends to arbitrary degree. (I tried this first, but left one or two out, I think, and it didn't work. Instead of finding my error, I thought it didn't. +1) – Michael E2 May 22 '17 at 10:49
• It looks like the coefficient of $R^0S^0$, i.e. the constant term, is split into two pieces. – Maesumi May 22 '17 at 13:43
• @Maesumi Yes, It looks so. – Alexei Boulbitch May 22 '17 at 13:44
• @Maesumi It's the automatic sorting by Plus and a famous difficulty with Mathematica and "constant terms." You can't really avoid it. In my answer, the terms are wrapped in another function. You have to get rid of the wrapper before you can use the result algebraically, but then the terms get sorted automatically, since Plus is Orderless. – Michael E2 May 22 '17 at 18:20
• There is a way around. Have a look at the edit. – Alexei Boulbitch May 22 '17 at 19:33