Skip to main content
added 31 characters in body
Source Link
kglr
  • 403.4k
  • 18
  • 501
  • 959

Don't quite understand why, but one needs both patterns (Jens' and OP'sywdr1987's) to get True for all the expressions in the example. This can be done using Alternatives or adding an additional definition to cover the pattern in Jens's answer:

ClearAll[intPolyQa]; intPolyQa[HoldPattern[Optional[_Integer] + Plus[Optional[_Integer] x_Symbol^Optional[_Integer] ...]], x_] := True; intPolyQa[Optional[_Integer] + Plus[Optional[_Integer] x_Symbol^Optional[_Integer] ...], x_] := True; intPolyQa[___] := False; intPolyQa[#, x] & /@ {x, 2 x, 2 x + 1, 2 x^2 + 3 x, 2 x^2 + 3 x + 1} (* {True, True, True, True, True} *) 

or

 ClearAll[intPolyQb]; intPolyQb[Alternatives[HoldPattern[Optional[_Integer] + Plus[Optional[_Integer] x_Symbol^Optional[_Integer] ...]], Optional[_Integer] + Plus[Optional[_Integer] x_Symbol^Optional[_Integer] ...]], x_] := True; intPolyQb[___] := False; intPolyQb[#, x] & /@ {x, 2 x, 2 x + 1, 2 x^2 + 3 x, 2 x^2 + 3 x + 1} (* {True, True, True, True, True} *) 

Don't quite understand why, but one needs both patterns (Jens' and OP's) to get True for all the expressions. This can be done using Alternatives or adding an additional definition to cover the pattern in Jens's answer:

ClearAll[intPolyQa]; intPolyQa[HoldPattern[Optional[_Integer] + Plus[Optional[_Integer] x_Symbol^Optional[_Integer] ...]], x_] := True; intPolyQa[Optional[_Integer] + Plus[Optional[_Integer] x_Symbol^Optional[_Integer] ...], x_] := True; intPolyQa[___] := False; intPolyQa[#, x] & /@ {x, 2 x, 2 x + 1, 2 x^2 + 3 x, 2 x^2 + 3 x + 1} (* *) 

or

 ClearAll[intPolyQb]; intPolyQb[Alternatives[HoldPattern[Optional[_Integer] + Plus[Optional[_Integer] x_Symbol^Optional[_Integer] ...]], Optional[_Integer] + Plus[Optional[_Integer] x_Symbol^Optional[_Integer] ...]], x_] := True; intPolyQb[___] := False; intPolyQb[#, x] & /@ {x, 2 x, 2 x + 1, 2 x^2 + 3 x, 2 x^2 + 3 x + 1} (* {True, True, True, True, True} *) 

Don't quite understand why, but one needs both patterns (Jens' and ywdr1987's) to get True for all the expressions in the example. This can be done using Alternatives or adding an additional definition to cover the pattern in Jens's answer:

ClearAll[intPolyQa]; intPolyQa[HoldPattern[Optional[_Integer] + Plus[Optional[_Integer] x_Symbol^Optional[_Integer] ...]], x_] := True; intPolyQa[Optional[_Integer] + Plus[Optional[_Integer] x_Symbol^Optional[_Integer] ...], x_] := True; intPolyQa[___] := False; intPolyQa[#, x] & /@ {x, 2 x, 2 x + 1, 2 x^2 + 3 x, 2 x^2 + 3 x + 1} (* {True, True, True, True, True} *) 

or

 ClearAll[intPolyQb]; intPolyQb[Alternatives[HoldPattern[Optional[_Integer] + Plus[Optional[_Integer] x_Symbol^Optional[_Integer] ...]], Optional[_Integer] + Plus[Optional[_Integer] x_Symbol^Optional[_Integer] ...]], x_] := True; intPolyQb[___] := False; intPolyQb[#, x] & /@ {x, 2 x, 2 x + 1, 2 x^2 + 3 x, 2 x^2 + 3 x + 1} (* {True, True, True, True, True} *) 
Source Link
kglr
  • 403.4k
  • 18
  • 501
  • 959

Don't quite understand why, but one needs both patterns (Jens' and OP's) to get True for all the expressions. This can be done using Alternatives or adding an additional definition to cover the pattern in Jens's answer:

ClearAll[intPolyQa]; intPolyQa[HoldPattern[Optional[_Integer] + Plus[Optional[_Integer] x_Symbol^Optional[_Integer] ...]], x_] := True; intPolyQa[Optional[_Integer] + Plus[Optional[_Integer] x_Symbol^Optional[_Integer] ...], x_] := True; intPolyQa[___] := False; intPolyQa[#, x] & /@ {x, 2 x, 2 x + 1, 2 x^2 + 3 x, 2 x^2 + 3 x + 1} (* *) 

or

 ClearAll[intPolyQb]; intPolyQb[Alternatives[HoldPattern[Optional[_Integer] + Plus[Optional[_Integer] x_Symbol^Optional[_Integer] ...]], Optional[_Integer] + Plus[Optional[_Integer] x_Symbol^Optional[_Integer] ...]], x_] := True; intPolyQb[___] := False; intPolyQb[#, x] & /@ {x, 2 x, 2 x + 1, 2 x^2 + 3 x, 2 x^2 + 3 x + 1} (* {True, True, True, True, True} *)