# Collect a multivariate polynomial with 26 variables

I have a polynomial like this:

$$(wz+h+j-q)^2+((gk+2g+k+1)\cdot(h+j)+h-z)^2+(2n+p+q+z-e)^2+(16(k+1)^3\cdot(k+2)\cdot(n+1)^2+1-f^2)^2+(e^3\cdot(e+2)(a+1)^2+1-o^2)^2+((a^2-1)y^2+1-x^2)^2+(16r^2y^4(a^2-1)+1-u^2)^2+(((a+u^2(u^2-a))^2+1)\cdot(n+4dy)^2+1+(x+cu)^2)^2+(n+l+v-y)^2+((a^2-1)l^2+1-m^2)^2+(ai+k+1-l-i)^2+(p+l(a-n-1)+b(2an+2a-n^2-2n-2)-m)^2+(q+y(a-p-1)+s(2ap+2a-p^2-2p-2)-x)^2+(z+pl(a-p)+t(2ap-p^2-1)-pm)^2)$$

(w z + h + j - q)^2 + ((g k + 2 g + k + 1) (h + j) + h - z)^2 + (2 n +
p + q + z - e)^2 + (16 (k + 1)^3 (k + 2) (n + 1)^2 + 1 -
f^2)^2 + (e^3 (e + 2) (a + 1)^2 + 1 - o^2)^2 + ((a^2 - 1) y^2 +
1 - x^2)^2 + (16 r^2 y^4 (a^2 - 1) + 1 -
u^2)^2 + (((a + u^2 (u^2 - a))^2 + 1) (n + 4 d y)^2 +
1 + (x + c u)^2)^2 + (n + l + v - y)^2 + ((a^2 - 1) l^2 + 1 -
m^2)^2 + (a i + k + 1 - l - i)^2 + (p + l (a - n - 1) +
b (2 a n + 2 a - n^2 - 2 n - 2) - m)^2 + (q + y (a - p - 1) +
s (2 a p + 2 a - p^2 - 2 p - 2) - x)^2 + (z + p l (a - p) +
t (2 a p - p^2 - 1) - p m)^2


I would like to 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.

• @MarcoB: In fact, this is my first question and I have no information about MMA code. Jan 5 at 16:08
• I mean, please post the polynomial itself as a Mathematica expression (and any code you have tried so far, if any). For instance, (w z + h +j - q)^2 + ..... Nobody will take the time to re-type your very long polynomial by hand into MMA. You must have typed it in MMA at some point, right? Jan 5 at 16:10
• @MarcoB: I never tried any thing. But I will try to do this. Jan 5 at 16:11
• Thanks. On second thought, CoefficientRules[poly, Variables[poly]] might be more appropriate than CoefficientArrays. Jan 5 at 17:16
• If you only want the polynomial completely expanded, you may use: Expand[ yourPolynomial] Jan 5 at 21:09

Using CoefficientRules is the way to go. However, the left hand sides of the rules returned are just numerical lists of the powers of each variable, which may not be what you want. Here's a way to get an Association of associations where the keys are the monomials you're interested in:

With[{vars = Symbol /@ CharacterRange["a", "z"]},
coeffs =
KeyMap[vars^# & /* Apply[Times],
Association@CoefficientRules[poly, vars]]];


You can reconstruct it like so:

Total@KeyValueMap[Times, coeffs] == poly // Simplify
(* True *)