# Checking if a polynomial contains a certain monomial

I want to check if a given polynomial contains a certain monomial while ignoring ordering of the variables. For example: $$P(a,b,c)=abc + bca$$ And then I want to check if $$P$$ contains $$bac$$ (should be true). I tried FreeQ and MemberQ but both return false.

• Isn’t FreeQ[] —> False just the result you want? (I guess you realize that $P$ is just $2 abc$, though.) Apr 3, 2021 at 4:48
• Not exactly, for example FreeQ[abc, a*b]=False also Apr 3, 2021 at 4:59
• Have you seen MonomialList? Apr 3, 2021 at 5:02
• I guess I could do something like: MemberQ[MonomialList[abc], acb] but that wont work if there is a coefficient other than 1. Apr 3, 2021 at 5:06
• Perhaps MemberQ[MonomialList[2 a b c], Optional[_?NumericQ] a c b]? Apr 3, 2021 at 5:44

This seems to work:

MemberQ[MonomialList[2 a b c], Optional[_?NumericQ] a c b]
(*  True  *)


Also this, which is a more algebraic approach.

With[{vars = {a, b, c, d}},
KeyExistsQ[CoefficientRules[a b + 2 a b c + a b c d, vars],
First@Keys@CoefficientRules[a c b, vars]]
]
(*  True  *)


It's important that the variables be the same in the same order in both calls to CoefficientRules. (I tend to prefer algebraic approaches in dealing with polynomials.)

Another algebraic approach:

With[{vars = {a, b, c, d}},
With[{monomial = CoefficientArrays[a b c, vars]},
CoefficientArrays[a b + 2 a b c + a b c d,
vars][[Length@monomial]] * Last@monomial
]]@"NonzeroPositions" =!= {}
(*  True  *)

With[{vars = {a, b, c, d}},
With[{monomial = CoefficientArrays[a d c, vars]},
CoefficientArrays[a b + 2 a b c + a b c d,
vars][[Length@monomial]] * Last@monomial
]]@"NonzeroPositions" =!= {}
(*  False  *)

PolynomialQ[a b c + 2 b c a, {b, a, c}]


a, b, c do not need to be symbols: PolynomialQ

or use

Decompose

or use

FactorList

for more flexibility.

Best seems up to the exactness of the question:

SubsetQ[List @@ (a b c + b c a), List @@ (b a c)]


(* True *)

That is because You do not primarily intent to differentiate between permutations. The basic idea stems from PolynomialOrderings. The Apply of List remove the built-ins from the FullForm and that is for polynomials Plus and Times. It then uses lexicographics ordering.

The question is basically for multivariate polynomials in order without the meaning of the order. That is confusing. As seems there is used the set of variate names abc, bac and bca so the induced intent of multivariation is forgotten in the name strings.

MemberQ[List @@ (abc + bca), List @@ (bac)]


(* False *)

based on the different names. The built-in Characters is then the link between both approaches.

SubsetQ[Characters[ToString[(List @@ (abc + bca))]],
Characters[ToString[List @@ (bac)]]]


(* True *)

• These work on those examples but for example SubsetQ[List @@ (a b c + b c a), List @@ (b a)] returns true also when I want that to be false. I want the monomial to appear in the polynomial exactly up to permutation. Apr 3, 2021 at 15:35