# Write a function using pattern test to test whether the input is polynomial and print error message if not

I am doing this which is supposed to be an easy problem and which I think my code should be correct but for some reasons it is not working. May anyone help correcting my mistakes? How should I write the correct code?

This is the question:

Write a function that takes a polynomial in the variable x as input and computes its degree. Using a pattern test your function should print an error message when the input is not a polynomial. (It is acceptable if your function regards a string object such as "Math" as a polynomial of degree zero).

If[Function[y, PolynomialQ[y, x]][x^2 + 3 x], Exponent[y, x], Print["error"]]


My code always returns zero and I have no idea why. Also, do we have to use Function[] to write a function?

Many thanks in advance for all the helps!

Edit:
Thanks @0x4A4D for suggestion.
Here is my edited code:

If[Function[x, PolynomialQ[x^2, x]], Exponent[y, x], Print["error"]]


which I think doesn't quite make any sense, the code does not even produce any output. I guess something is wrong with Function[], but not sure how. I would be grateful if anyone could lend some help. I have been doing this question for quite a long time. Thanks!

• PolynomialQ[] is intended to be used directly; that is, PolynomialQ[x^2 + 3 x, x]. Exponent[y, x] is necessarily $0$; I see no x whatsoever in the first argument... – J. M. will be back soon Jun 13 '13 at 13:20
• @0x4A4D. Thanks for the advice for PolynomialQ, but how then can we compute the degree and what do you mean by no x in first argument? Thanks. – user71346 Jun 13 '13 at 13:26
• There's no x in y, last I checked... if you'd assigned y to something with x in it, like y = x^2 + 3 x;, then you're cooking. – J. M. will be back soon Jun 13 '13 at 13:35
• To push you in the right direction: polynomialDegree[poly_, x_] /; PolynomialQ[poly, x] := (* stuff *). – J. M. will be back soon Jun 13 '13 at 13:38
• This is as far as I can get: polynomialdegree[poly_, x_] /; PolynomialQ[poly, x] := Exponent[poly, x]. But I cannot print an error message if it is not a polynomial? Should I use If instead, then how can I do it? – user71346 Jun 13 '13 at 13:53

There's a bit of a trick that lets you do this with a single definition:

fun::notpoly = "Not a polynomial!!!"
fun[poly_, x_] /; PolynomialQ[poly, x] || Message[fun::notpoly] :=
something[poly];


This works because the test clause has to be True for the rule to fire, and Message returns Null.

• emit a message and return unevaluated all in one function call, nice. +1 – rcollyer Jun 14 '13 at 19:16
• Although, strictly speaking, it doesn't use a PatternTest, but Conditional. – rcollyer Jun 14 '13 at 19:16

First, thanks to 0x4A4D and Markus Roellig for the helps.

This is what I have tried, using built-in functions PolynomialQ and Exponent and an If statement.

dop[f_] := If[PolynomialQ[f, x], d = Exponent[f, x]; Print[d], Print["ERROR"]]


Comments are welcomed, let me know if anything is wrong.

• 1. It would be best if you can make dop take the variable as another argument, in addition to the polynomial. 2. Replace d = Exponent[f, x]; Print[d] with just Exponent[f, x]; it should still work fine. – J. M. will be back soon Jun 13 '13 at 14:10
• As @0x4A4D mentioned already,in most cases, you don't need Print to output the result. Matheamtica will automatically output the result when finishing evaluation. – mmjang Jun 13 '13 at 14:16

This should get you started:

test::wronginp = "test accepts only polynomials as input.";
test[func_?(PolynomialQ[#, x] &)] := "Degree" (* replace with actual degree Determination*)
test[___] := (Message[test::wronginp]; $Failed )  Some tests: test[x^2 + 1]  "Degree" test[Sin[x]]  During evaluation of In:= test::wronginp: test accepts only polynomials as input.$Failed

• Abort[] here could be bad, especially if buried in a deeper calculation. Why not return \$Failed, instead? Although, the usual case is to return unevaluated. – rcollyer Jun 14 '13 at 19:19
• @rcollyer Good Point. Changed the answer accordingly. – Markus Roellig Jun 19 '13 at 9:33