7
$\begingroup$

I am crafting some functions on polynomials that must be in x. But I checked that it is always True whatever variable I use:

PolynomialQ[x^3-x+2,x]
True
PolynomialQ[x^3-x+2,y]
True
PolynomialQ[x^3-x+2,z]
True

Why?

$\endgroup$
1
  • 4
    $\begingroup$ Because you usually want something like PolynomialQ[a x^2 + b x + c, x] to be true, and the logical consequence of that is that your expression are polynomials in y/z of degree zero. PolynomialQ[_, y] basically checks that the expression doesn't involve weird functions of y such as Log[y] or y^(1/3). $\endgroup$ Commented Jun 11 at 11:48

4 Answers 4

11
$\begingroup$

We can use PolynomialExpressionQ, which has an optional 3rd argument that tests if all coefficients satisfy a constraint:

PolynomialExpressionQ[x^3 - x + 2, x, NumericQ]
True
PolynomialExpressionQ[x^3 - x + 2, y, NumericQ]
False
PolynomialExpressionQ[x^3 - x + 2, z, NumericQ]
False

Though this is not a fool proof answer:

PolynomialExpressionQ[x^3 - x + 2 + y^2 + 2y + 1 - (y + 1)^2, x, NumericQ]
False
Simplify[x^3 - x + 2 == x^3 - x + 2 + y^2 + 2y + 1 - (y + 1)^2]
True

Side note, why do both PolynomialQ and PolynomialExpressionQ exist? Only reason I found was a fringe example in the docs. Also, it would be nice if PolynomialQ also had this 3rd optional argument.

$\endgroup$
6
$\begingroup$

A slight variation of Julien Kluge's answer using Variables, which gives a list of all independent variables in a polynomial.

ExclusivePolynomialQ[func_, var_Symbol | var_List] := 
 With[{other = Complement[Variables[func], Flatten[{var}]]}, 
  PolynomialQ[func, var] && (other === {} || ! PolynomialQ[func, other])]

ExclusivePolynomialQ[x^3 - x + 2, x]
(* True *)
ExclusivePolynomialQ[x^3 - x + 2, y]
(* False *)
ExclusivePolynomialQ[x^3 - x + 2, z]
(* False *)
ExclusivePolynomialQ[x^3 - x + 2 + z, {x, y}]
(* False *)
ExclusivePolynomialQ[Sin[x], x]
(* False *)
$\endgroup$
4
$\begingroup$

The function $x^3-x+2$ is a polynomial in $x$ with coefficients {2,0,-1,1}. For $y$ and $z$ its a polynomial with coefficients {2-x+x^3}. (See CoefficentList) So the function is just the constant term for polynomials in $y$ or $z$.

If you need to exclude other variables, you can use a workaround:

ExclusivePolynomialQ[func_, var_Symbol | var_List] := 
 ContainsOnly[
   Select[DeleteDuplicates[Cases[func, _Symbol, {0, Infinity}]], 
    Context[#] != "System`" &], 
   If[Head[var] === Symbol, {var}, var]] && PolynomialQ[func, var]

So we can write:

ExclusivePolynomialQ[x^3 - x + 2, x]
ExclusivePolynomialQ[x^3 - x + 2, y]
ExclusivePolynomialQ[x^3 - x + 2, z]

Which yields

True
False
False
$\endgroup$
3
  • $\begingroup$ Why not something simple like NumericQ? As in ExclusivePolynomialQ[func_, var_Symbol | var_List] := AllTrue[Flatten[CoefficientList[func, var]], NumericQ]? $\endgroup$
    – Roman
    Commented Jun 11 at 10:19
  • $\begingroup$ That... would be way easier indeed. You should answer that . $\endgroup$ Commented Jun 11 at 10:24
  • $\begingroup$ Damn! I forgot the algebra basis. You're right. But this may be a simpler solution: f[p : _?(PolynomialQ[#, x] && Length@# == Length@DeleteCases[CoefficientList[#, x], 0] &)] $\endgroup$ Commented Jun 11 at 10:32
0
$\begingroup$

You can use FreeQ[] to test dependence. I hope that the following function will help you:

myPolynomialQ[poly_,var_] := (Not[FreeQ[poly, var]] && PolynomialQ[poly, var]);

{myPolynomialQ[x^3 - x + 2, x], myPolynomialQ[x^3 - x + 2, y], 
 myPolynomialQ[x^3 - x + 2, z]}

(* {True, False, False} *) 
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.