# Easiest way to extract the coefficient of a polynomial

For a term in a polynomial, say

387 a1^4 a2^3 x^3 y^7 z^100 w^364


what is the most efficient way to extract the coefficient of this term, i.e. 387?

• Put all variables equal to 1? Commented May 15, 2016 at 0:43

Another way:

poly = 387 a1^4 a2^3 x^3 y^7 z^100 w^364;

vars = Variables[poly];
exps = Exponent[poly, vars];

Coefficient[poly, Times @@ (vars^exps)]

387


or

Cancel[poly/(Times @@ (vars^exps))]

387


p.s. In general, you'd want to hit your polynomial with MonomialList if it's not a proper monomial.

And just for fun, here's an overly complicated solution

poly = 387 a1^4 a2^3 x^3 y^7 z^100 w^364;
vars = Variables[poly];

Times @@ ((# D[Log[poly], #]) + 1 & /@ vars) *
Fold[Integrate[#1, {#2, 0, 1}] &, poly, vars]

387

lookMaNoXYZ = 1 & @@@ # &;

lookMaNoXYZ[ x^2 y^6 ]


1

lookMaNoXYZ[10 x^2  Pi y^6 / 4]


(5 π)/2

lookMaNoXYZ[x^2 55. y^6]


55.

• ... i am also puzzled how/why this works.
– kglr
Commented May 12, 2016 at 21:47
• So it replaces all the powers with 1's... obviously Commented May 12, 2016 at 22:19
• Try lookMaNoXYZ[Sqrt[2/3] x^5 y^7]. Commented May 12, 2016 at 22:52
• To extend the comment by J. M.: lookMaNoXYZ fails whenever parts of the coefficient are not AtomQ. Commented May 13, 2016 at 14:31

Example:

(*Example 1*)
Select[387 a1^4 a2^3 x^3 y^7 z^100 w^364, IntegerQ]

(*Example 2*)
Select[x^2 y^6, IntegerQ]


Output:

(*Output 1*)
387

(*Output 2*)
1


Reference:

• This is so beautiful, thank you. Commented May 12, 2016 at 20:41
• I am sure there are other solutions but this should do it. Glad it helped Commented May 12, 2016 at 20:42
• It works only if the head is Times, for example Select[x^2, IntegerQ] returns 2. Commented May 12, 2017 at 23:04
• Multiplying by Unique[] adds very little overhead: Select[x^2 Unique[], IntegerQ] or in general Select[monomial * Unique[], NumericQ]. Select is the fastest way I've found. Commented May 13, 2017 at 15:40
ClearAll[cF]
cF = # /. Thread[Variables[#] -> 1] &;

cF[x^2 y^6 ]


1

cF [367 a1^4 a2^3 x^3   y^7 z^100 w^364 ]


367

poly = 387 a1^4 a2^3 x^3 y^7 z^100 w^364;

CoefficientRules[poly][[1, 2]]


387

• CoefficientRules[poly] is{{4, 3, 364, 3, 7, 100} -> 387}. Also, FromCoefficientRules[%, Variables[poly]] == poly is True Commented May 13, 2016 at 5:58

Using some undocumented functionality:

poly = 387 a1^4 a2^3 x^3 y^7 z^100 w^364;
GroebnerBasisDistributedTermsList[poly, Variables[poly]][[1, 1, 2]]
387

poly2 = Sqrt[2/3] x^5 y^7;
GroebnerBasisDistributedTermsList[poly2, Variables[poly2]][[1, 1, 2]]
Sqrt[2/3]

GroebnerBasisDistributedTermsList[x^3 y^2, {x, y}][[1, 1, 2]]
1


I would use FactorTermsList:

First @ FactorTermsList[387 a1^4 a2^3 x^3 y^7 z^100 w^364]


387

for your particular example, this is quite simple:

FirstCase[387 a1^4 a2^3 x^3 y^7 z^100 w^364, _Integer]
(* 387 *)


this case works even with integer being not in the first position:

FirstCase[a1^4 a2^3 x^3 387 y^7 z^100 w^364, _Integer]
(* 387 *)


Also, as @MichaelE2 suggested perhaps the shortest answer:

First[a1^4 a2^3 x^3 y^7 387 z^100 w^364]
(* 387 *)

• For this particular example, First[387 a1^4 a2^3 x^3 y^7 z^100 w^364]` is even simpler. :) Commented May 12, 2017 at 22:37
• Agreed @MichaelE2 Commented May 13, 2017 at 9:14