Why does PolynomialQ[x^n, x] return 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)}
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.