# Why does PolynomialQ[x^n, x] return False?

From what I can see PolynomialQ will return False whenever some exponent is another variable such as here:

    PolynomialQ[x^n, x]


Is there an alternative test that will return True for such a polynomial?

Motivation:

In another thread I need to use

Product[x^k (1 - x^k), {k, 1, n}]


for which Mathematica returns:

x^(1/2 n (1 + n)) QPochhammer[x, x, n]


However those two forms do not expand in the same way, for instance with $n=3$:

With[{n = 3},
Expand[{x^(1/2 n (1 + n)) QPochhammer[x, x, n], Product[x^k (1 - x^k), {k, 1, n}]}]]
(* {x^6 QPochhammer[x, x, 3], x^6 - x^7 - x^8 + x^10 + x^11 - x^12} *)


Indeed, Mathematica treats only the second expression as a polynomial:

With[{n = 3}, PolynomialQ[#, x] & /@
{x^(1/2 n (1 + n)) QPochhammer[x, x, n], Product[x^k (1 - x^k), {k, 1, n}]}]
(* {False, True} *)


even if I specify that n is integer as follows

f[x_, n_Integer] := x^(1/2 n (1 + n)) QPochhammer[x, x, n];
PolynomialQ[f[n], x]
(* False *)

-

Assuming[n ∈ Integers && n > 0, PolynomialQ[x^n, x]] won't work because Assuming only works on functions with Assumptions option, such as Simplify, Refine, etc. Unfortunately PolynomialQ doesn't have this option. Still, something like

Simplify[PolynomialQ[x^n, x], n ∈ Integers && n > 0]


won't work because Mathematica will calculate PolynomialQ[x^n, x] first.

Currently the only solution in my mind is to define a new function:

pQ[poly_, var_, assum_] := PolynomialQ[poly /. Thread[assum -> 1], var]


The first argument of pQ is the possible polynomial, the second argument is the variables of the possible polynomial, the third argument is the variables which are assumed to be integers and positive. This function is in fact a realization of the method suggested by @Mark Adler. It can be used it like this:

pQ[x^n, x, n]
pQ[x^(n + m), x, {n, m}]
pQ[x^(n + 1/m), x, {n, 1/m}]
pQ[x^(n + 1/m), x, n + 1/m]

True
True
True
True


Well, I admit this solution isn't robust enough. Changing the definition of the function into something like

pQ[poly_, var_, assum_] := PolynomialQ[poly /. Thread[assum ->RandomInteger[{1, 100}]], var]


can somewhat help, but it still has a certain probability to fail…

However, for your added Motivation part, FunctionExpand will give the desired result:

With[{n = 3},
FunctionExpand[{x^(1/2 n (1 + n)) QPochhammer[x, x, n],
Product[x^k (1 - x^k), {k, 1, n}]}]]

{(1 - x) x^6 (1 - x^2) (1 - x^3), (1 - x) x^6 (1 - x^2) (1 - x^3)}

-
From what I understand of your code, it tests poly after replacing integer variables by 1. I would like to avoid being tricked if I test pQ[x^(1/n), x, n] (returns True). – A.G. Dec 23 '13 at 4:55
@A.G. I'm sorry that I can't find a robust enough way to solve this, but for your Motivation part, you can use FunctionExpand to expand the two expressions in the same way. – xzczd Dec 23 '13 at 6:17

Apparently you know somehow that n is a non-negative integer, but Mathematica has no idea. n could be anything. You need to let Mathematica know.

Since PolynomialQ doesn't honor Assuming, you would need to make your own that does. Using Simplify could incorporate the assumptions.

Here is my attempt at it (though my pattern-fu is not very strong -- e.g. I couldn't get Repeated to work right, so I just used Map):

pq[p_, x_] :=
And @@ (MatchQ[#, (c_ /; FreeQ[c, x]) | (
c_. x^n_. /;
FreeQ[c, x] && FreeQ[n, x] &&
Simplify[n ∈ Integers && n >= 0])] & /@
If[Head[p] === Plus, List @@ p, {p}])


Then:

pq[x^n, x]

False

Assuming[{n ∈ Integers, n >= 0}, pq[x^n, x]]

True

Assuming[{n ∈ Integers, n >= 0}, pq[x^(n - 2), x]]

False

Assuming[{n ∈ Integers, n >= 2}, pq[x^(n - 2), x]]

True


This could probably be extended to take a list of variables, as PolynomialQ permits, instead of just one but I will leave that as an exercise for the reader.

-
Of course I would like to get the correct answer without replacing $n$. I tried to tell Mathematica about $n$ like this: Assuming[n \[Element] Integers && n > 0, PolynomialQ[x^n, x]] but I still get False. – A.G. Dec 23 '13 at 3:22