3
$\begingroup$

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).

My answer:

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!

$\endgroup$
7
  • 1
    $\begingroup$ 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... $\endgroup$ Jun 13, 2013 at 13:20
  • $\begingroup$ @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. $\endgroup$
    – user71346
    Jun 13, 2013 at 13:26
  • $\begingroup$ 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. $\endgroup$ Jun 13, 2013 at 13:35
  • $\begingroup$ To push you in the right direction: polynomialDegree[poly_, x_] /; PolynomialQ[poly, x] := (* stuff *). $\endgroup$ Jun 13, 2013 at 13:38
  • $\begingroup$ 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? $\endgroup$
    – user71346
    Jun 13, 2013 at 13:53

3 Answers 3

4
$\begingroup$

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.

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

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.

$\endgroup$
2
  • 1
    $\begingroup$ 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. $\endgroup$ Jun 13, 2013 at 14:10
  • $\begingroup$ 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. $\endgroup$
    – mmjang
    Jun 13, 2013 at 14:16
0
$\begingroup$

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[535]:= test::wronginp: test accepts only polynomials as input.

$Failed

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – rcollyer
    Jun 14, 2013 at 19:19
  • $\begingroup$ @rcollyer Good Point. Changed the answer accordingly. $\endgroup$ Jun 19, 2013 at 9:33

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.