I generalize Jason B's answer to group by like terms in any number of coefficients, as well as in cases where you have polynomials multiplying one another:
It sounds like you mostly want Plus to keep from evaluating. You can do this by replacing Plus with plus, where plus is a Flat function (since we might as well keep associativity of addition)
SetAttributes[plus, Flat];
expr = (a + b c) (a - b c);
expr /. Plus->plus
plus[a, -b c] plus[a, b c]
Now make plus distrbutive (including over itself -- i.e., when it appears in an integer power) :
sep = % //. { a_ b_plus :> (a*# &) /@ b, Power[a_plus, n_ /; n > 0 && IntegerQ@n] :> (Power[a, n - 1]*# &) /@ a }
plus[a^2, -a b c, a b c, -b^2 c^2]
Now say you want to sort the output based on powers of a, b, and c. To this end, I define a getPower function :
(* operator form *)
getPower[var_][expr_] := getPower[expr, var]
(* Thread over a list of variables *)
getPower[expr_, varList_List] := getPower[expr, #] & /@ varList
getPower[expr_, var_] /; FreeQ[expr, var] := 0
getPower[expr_Times, var_] := Plus @@ getPower[var] /@ List @@ expr
getPower[(expr_)^n_, var_] := n getPower[expr, var]
getPower[var_, var_] := 1
(Note that getPower requires expr to be a monomial in the supplied variables; otherwise the output will contain getPower. Furthermore, note that getPower[ 1/(a+x), x] returns 0, but getPower[ 1/x, x] returns -1. )
Then you can use GatherBy to sort the output by coefficient:
GatherBy[List @@ sep, getPower@{a, b, c}]
{{a^2}, {-a b c, a b c}, {-b^2 c^2}}
It might be desirable to convert the sublists back to plus :
plus@@@%
{plus[a^2], plus[-a b c, a b c], plus[-b^2 c^2]}
You can imagine generalizing the above to pull the coefficients out front.
Note that it may or may not be useful to have given plus attribute OneIdentity from the start, depending on how you'd like to generalize.
Simplifyto it you get 0. Next time try that yourself. – Alexei Boulbitch Feb 05 '16 at 08:58