Message Boards Message Boards

2
|
11511 Views
|
5 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Why does BlankSequence matching sometimes fail in Mathematica 10?

Posted 10 years ago

I only have one example actually, but a more accurate title didn't fit.

Why does this return False in Mathematica 10:

MatchQ[tMax/2,HoldPattern[Times][HoldPattern[Rational][__], b_]] 

It returns True in versions<=9.

FullForm[tMax/2] is the same in all versions:

 Times[Rational[1, 2], tMax]

I know I can get around this by using variants such as

MatchQ[tMax/2,HoldPattern[Times][HoldPattern[Rational][n1_,n2_], b_]]

but I have existing code with many of instances of "__", and all but this one seem to work on all my standard tests. I would like to understand if any others are likely to fail in Mathematica 10 in other circumstances. Thanks for any clarification.

POSTED BY: Elaine Kant
5 Replies

Thanks for the helpful explanation that tells me where to look for other possible problem code.

POSTED BY: Elaine Kant
Posted 10 years ago

As an atomic object, MatchQ[1/2, HoldPattern[Rational[__]] and MatchQ[1/2,Rational[a_,b_]] never really should have worked. Compare, for exampe, with these examples:

In[23]:= MatchQ["asdf", String[_]]
Out[23]= False

In[24]:= MatchQ[3., Real[_]]
Out[24]= False

The fact that they did was a quirk of implementation, which changed to a more efficient data structure in V10. Because of how common Rational[_,_] is, we continued to support by a special hook, but arbitrary patterns inside of Rational are unlikely to work. (BTW, the same thing happened with Complex in V9).

Fortunately, it should only be Rational is affected. MatchQ[tMax/2,HoldPattern[Times][Rational[n1_,n2_], b_]] and MatchQ[tMax/2,HoldPattern[Times][a_Rational, b_]] are both good replacedments that will continue to work. Notice also these don't require an inner HoldPattern as the Rational involved won't even try to evaluate.

POSTED BY: Itai Seggev

Moreover, to retain backward compatibility, this was treated as a bug and so in the next release the behavior will revert to what it was prior to version 10.

POSTED BY: Daniel Lichtblau

Thank you.

POSTED BY: Elaine Kant

This incorrect behavior has been reported to developers. My apologies for the inconvenience this has caused.

POSTED BY: Bruce Miller
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