Skip to main content

Below is a function intended to test if thean expression is a polynomial with integerin which all the coefficients and exponents onlyare integers:

intPolyQ[Optional[_Integer] + Plus[Optional[_Integer] x_Symbol^Optional[_Integer] ...], x_] := True; intPolyQ[___] := False; 

but it turns outIt gives unexpected results in some cases:. For example,

intPolyQ[#, x] & /@ {x, 2 x, 2 x + 1, 2 x^2 + 3 x, 2 x^2 + 3 x + 1} 

gives

{True, True, True, False, True} 

which is wrong in the 4th case.

Why and how to correctly use Optional as an omissible parameterdoes this happen? How do I fix it?

Below is a function to test if the expression is a polynomial with integer coefficients and exponents only:

intPolyQ[Optional[_Integer] + Plus[Optional[_Integer] x_Symbol^Optional[_Integer] ...], x_] := True; intPolyQ[___] := False; 

but it turns out unexpected in some cases:

intPolyQ[#, x] & /@ {x, 2 x, 2 x + 1, 2 x^2 + 3 x, 2 x^2 + 3 x + 1} 

gives

{True, True, True, False, True} 

which is wrong in the 4th case.

Why and how to correctly use Optional as an omissible parameter?

Below is a function intended to test if an expression is a polynomial in which all the coefficients and exponents are integers:

intPolyQ[Optional[_Integer] + Plus[Optional[_Integer] x_Symbol^Optional[_Integer] ...], x_] := True; intPolyQ[___] := False; 

It gives unexpected results in some cases. For example,

intPolyQ[#, x] & /@ {x, 2 x, 2 x + 1, 2 x^2 + 3 x, 2 x^2 + 3 x + 1} 

gives

{True, True, True, False, True} 

which is wrong in the 4th case.

Why does this happen? How do I fix it?

Tweeted twitter.com/#!/StackMma/status/278087681471287296
Source Link

How to use Optional as an omissible parameter in pattern-match?

Below is a function to test if the expression is a polynomial with integer coefficients and exponents only:

intPolyQ[Optional[_Integer] + Plus[Optional[_Integer] x_Symbol^Optional[_Integer] ...], x_] := True; intPolyQ[___] := False; 

but it turns out unexpected in some cases:

intPolyQ[#, x] & /@ {x, 2 x, 2 x + 1, 2 x^2 + 3 x, 2 x^2 + 3 x + 1} 

gives

{True, True, True, False, True} 

which is wrong in the 4th case.

Why and how to correctly use Optional as an omissible parameter?