collect a multivariate polynomial, and simplify all coefficients

I have a polynomial like this:

(a x + b c y + d z) (d x + e c y + g z)

I would like to rewrite it as something like this:

a d x^2 + b c^2 e y^2 + d g z^2 + (c (b d + a e)) x y + (d^2 + a g) x z + (c (d e + b g)) y z

i.e. keep all terms of different variables separate, collect all the coefficients for each term, and also simplify all the coefficients. How can we do this?

Collect gives a nested collection, so it is not what I want.

(i.e. it gives this:

a d x^2 + b c^2 e y^2 + (c d e + b c g) y z + d g z^2 + x ((b c d + a c e) y + (d^2 + a g) z)


where the last term is a nested one)

The closest I can find is

expr=(a x + b c y + d z) (d x + e c y + g z);

Plus @@ MonomialList[Expand[expr], {x, y, z}]


which collects all the coefficients but refuses to simplify them, i.e.

a d x^2 + (b c d + a c e) x y + b c^2 e y^2 + (d^2 + a g) x z + (c d e + b c g) y z + d g z^2.

• Commented Jul 26, 2017 at 22:01

Here is one possibility:

Activate @ Expand @ Collect[
(a x+b c y+d z) (d x+e c y+g z),
{x, y, z},
Inactive[Simplify]
]


a d x^2 + c (b d + a e) x y + b c^2 e y^2 + (d^2 + a g) x z + c (d e + b g) y z + d g z^2

expr = (a x + b c y + d z) (d x + e c y + g z);

Simplify /@ Total @ MonomialList[Expand[expr], {x, y, z}]
Total[Simplify /@ MonomialList[Expand[expr], {x, y, z}]]


or, a variation on Carl's approach (change the order of Collect and Expand)

Expand[Collect[(a x + b c y + d z) (d x + e c y + g z), {x, y, z}, foo]]/. foo -> Simplify


all give

a d x^2 + c (b d + a e) x y + b c^2 e y^2 + (d^2 + a g) x z + c (d e + b g) y z + d g z^2

It is unclear exactly what you seek, but this seems close:

Collect[Expand[(a x + b c y + d z) (d x + e c y + g z)], {x, y, z}]

• Collect gives a nested collection, which is not what I want. Commented Jul 26, 2017 at 22:06
• When your question says you're seeking "something like this" it is manifest you're not being clear and precise. I don't think we can help you without a detailed, precise specification of what you seek. Commented Jul 26, 2017 at 22:07
• What I want is stated after the second highlighted expression, after "i.e.". I used "something like this" because the exact order of the terms and variables, as well as the parentheses around the common coefficient c, does not matter. Commented Jul 26, 2017 at 22:13

Here are two approached using CoefficientRules and CoefficientList:

poly = (a x + b c y + d z) (d x + e c y + g z);

cr = Simplify@CoefficientRules[poly, {x, y, z}];
(x^#1 y^#2 z^#3 & @@@ #[[;; , 1]]).#[[;; , 2]] &@cr

cl = Simplify@CoefficientList[poly, {x, y, z}];
Total@Flatten[Array[x^#1 y^#2 z^#3 &, {3, 3, 3}, {0, 0, 0}] cl]


Both of which output

a d x^2 + c (b d + a e) x y + b c^2 e y^2 + (d^2 + a g) x z + c (d e + b g) y z + d g z^2