Message Boards Message Boards

0
|
3586 Views
|
3 Replies
|
1 Total Likes
View groups...
Share
Share this post:
GROUPS:

Unexpected result from a pattern constraint with the construct ?test

Posted 9 years ago

Hello

Below is a line which works as expected:

  Cases[\!\(\*
  TagBox[
  StyleBox[
  RowBox[{"Times", "[", 
  RowBox[{
  RowBox[{"Power", "[", 
  RowBox[{"_", ",", "n_"}], "]"}], ",", "x_"}], "]"}],
  ShowSpecialCharacters->False,
  ShowStringCharacters->True,
  NumberMarks->True],
  FullForm]\) /; EvenQ[n]] @@ {1 + y x^2 + y x^3 }   

I expect the line below to have an identical result (only change: use of the n?EvenQ construct instead of EvenQ[n]).

 Cases[{ 1 + y x^2 + y x^3}, \!\(\*
 TagBox[
 StyleBox[
 RowBox[{"Times", "[", 
 RowBox[{
 RowBox[{"Power", "[", 
 RowBox[{"_", ",", "n_"}], "]"}], ",", "x_"}], "]"}],
 ShowSpecialCharacters->False,
 ShowStringCharacters->True,
 NumberMarks->True],
 FullForm]\) /;  n?EvenQ, Infinity]

It is accepted by MMA but since it returns nothing, It must be interpreted differently. But how?

True I always saw in examples that the construct form ' _?test' is being used but I never read that the _ cannot be replaced here by a pattern variable

POSTED BY: wojtek potocki
3 Replies

Is this what you're trying to do?

In[6]:= Cases[{1 + y x^2 + y x^3}, 
 Times[Power[_, n_?EvenQ], x_], Infinity]

Out[6]= {x^2 y}
POSTED BY: Frank Kampas
Posted 9 years ago

Yes it is. But do you know how my previous answer was interpreted by MMA.?

POSTED BY: wojtek potocki

Your previous version had incorrect syntax. As described in the documentation for Condition:

https://reference.wolfram.com/language/ref/Condition.html

"patt /; test is a pattern which matches only if the evaluation of test yields True".

POSTED BY: Frank Kampas
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract