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.

If you write this expression in Mma notation, and then apply

`Simplify`

to it you get 0. Next time try that yourself. – Alexei Boulbitch – 2016-02-05T08:58:49.527@AlexeiBoulbitch that gives the final answer, I don't want the final answer. I want the intermediate step, collect the variables by factoring $c_1$ and $c_2$ so that I can copy steps from mathematica instead of writing the entire step. – Santosh Linkha – 2016-02-05T09:36:55.663

3

Requests for Mathematica to show steps come up often, and typically the answer is negative; see e.g. Get a "step-by-step" evaluation in Mathematica.

– MarcoB – 2016-02-05T11:55:18.667Try this stuff on http://www.wolframalpha.com/

– None – 2016-02-05T18:52:31.450If I understand OP correctly, I don't think you're asking (necessarily) to trace out the intermediate steps that Mathematica actually goes through during the calculation, but rather to find a way to break the calculation into steps. As previous commenters said, the former seems impossible, but the latter is definitely doable, if you're willing to put in a lot of work. (If you agree with what I said, I would suggest editing the question to reflect this.) After all, you don't

needto use any of Mathematica's built-in algebra functions if you'd prefer to write your own. – jjc385 – 2016-02-05T20:32:55.113