7
$\begingroup$

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?

$\endgroup$
2
  • 3
    $\begingroup$ 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]] $\endgroup$
    – chris
    Nov 15, 2012 at 9:32
  • $\begingroup$ 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` $\endgroup$ Nov 16, 2012 at 2:14

3 Answers 3

4
$\begingroup$

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)

$\endgroup$
3
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

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)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.