# How to extract all the coefficients of a homogeneous polynomial [duplicate]

Suppose p is a homogeneous polynomial in four variables, say

p = x^10 y^10 z^5 w^5 + 3 a x^10 w^20;


p is a homogeneous polynomial of degree 30, where a is a free parameter. I want to extract the coefficients of monomials, which is just

{1, 3 a}


There is a function in Mathematica, CoefficentList; however, it is very very inefficient, if I do the following evaluation

Flatten[CoefficientList[p, {x, y, z, w}]]


It contains 15246 coefficients and 15244 coefficients out of 15246 are 0. So is there any more efficient and more quick way to extract the coefficients of a homogeneous polynomial of high degree in four variables?

• Maybe In[9]:= Apply[List, p] /. Thread[{x, y, z, w} -> 1] Out[9]= {3 a, 1} Jul 10, 2016 at 14:41
• Does this look familiar to you? Jul 11, 2016 at 4:29

Using an undocumented function:

GroebnerBasisDistributedTermsList[x^10 y^10 z^5 w^5 + 3 a x^10 w^20,
{x, y, z, w}][[1, All, -1]]
{1, 3 a}


Here's a documented solution:

CoefficientRules[x^10 y^10 z^5 w^5 + 3 a x^10 w^20, {x, y, z, w}]
{{10, 10, 5, 5} -> 1, {10, 0, 0, 20} -> 3 a}

• Thank you very much, do you know where I could find more details about this DistributedTermsList? Jul 10, 2016 at 12:41
• As I had emphasized in my answer, it's undocumented. This has been used before on this site and in MathGroup (you can search for those examples yourself), but there is no documentation for it. Jul 10, 2016 at 12:49
• @Wenzhe - You can at least see the available options using Options[GroebnerBasisDistributedTermsList]. Jul 10, 2016 at 13:49
• @BobHanlon Thank you! Jul 10, 2016 at 14:10