7

Suppose I've got this:

In[13]:= Expand[(a + b) (b + c) (c + a)]

Out[13]= a^2 b + a b^2 + a^2 c + 2 a b c + b^2 c + a c^2 + b c^2

And I want to collect only terms involving a^2. In other words, I want the following output:

a^2(b + c) + a b^2 + 2 a b c + b^2 c + a c^2 + b c^2

How can I do this? If I use the following:

Collect[%, a^2]

Then it simply groups terms into the highest power of a, even if the highest term is less than 2. So it results in this:

In[14]:= Collect[%, a^2]

Out[14]= b^2 c + b c^2 + a^2 (b + c) + a (b^2 + 2 b c + c^2)

Ideally, I would like to extend this further to collect all a^2, b^2, and c^2 in one expression. So that running my command would transform the original fully expanded expression into the following:

a^2(b + c) + b^2(a+c) + c^2(a+b) + 2 a b c

In a single command. It this possible?

Zachary Turner
  • 213
  • 3
  • 6
  • 3
    FYI (its not the answer to your question but...) another symmetric form would be given by SymmetricReduction[Expand[(a + b) (b + c) (c + a)] , {a, b, c}][[1]] – chris Nov 15 '12 at 09:32
  • Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the FAQs! 3) When you see good Q&A, 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, remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign` – Vitaliy Kaurov Nov 16 '12 at 02:14

3 Answers3

4

Introducing dummy variables will do the job:

Collect[Expand[(a + b) (b + c) (c + a)]
  /. {a^2 -> x, b^2 -> y, c^2 -> z}, {x, y, z}]/. {x -> a^2, y -> b^2, z -> c^2}

2 a b c + (a + b) c^2 + b^2 (a + c) + a^2 (b + c)

If you will have more variables in the future, it probably makes sense to rewrite this:

P = {a, b, c}; Q = {x, y, z};
Collect[Expand[(a + b) (b + c) (c + a)] 
   /. MapThread[#1^2 -> #2 &, {P, Q}], Q] /. MapThread[#2 -> #1^2 &, {P, Q}]

2 a b c + (a + b) c^2 + b^2 (a + c) + a^2 (b + c)

Vitaliy Kaurov
  • 73,078
  • 9
  • 204
  • 355
3

How about Coefficient?

expr = Expand[(a + b) (b + c) (c + a)]
term = Coefficient[expr, a^2]*a^2 (* get the coefficient and multiply it with the variable *)
rest = (expr - term) // Expand (* expand the rest *)
term + rest
a^2 b + a b^2 + a^2 c + 2 a b c + b^2 c + a c^2 + b c^2

a^2 (b + c)

a b^2 + 2 a b c + b^2 c + a c^2 + b c^2

a b^2 + 2 a b c + b^2 c + a c^2 + b c^2 + a^2 (b + c)

The final line contains the a^2(b + c) term at the end.

István Zachar
  • 47,032
  • 20
  • 143
  • 291
0

Your first question is quite different from the second one. To address the first one you might do as follows:

pos = Position[Collect[expr, a], a^2*b_][[1, 1]]

this yields 3. Then

Collect[expr, a][[pos]]

yielding a^2 (b + c)

Alexei Boulbitch
  • 39,397
  • 2
  • 47
  • 96