Hello,
MatchQ do not seem to treat the inbuilt functions Power and Times in the same way:
In[1]:= MatchQ[Power[a, b], Power[_, _]]
Out[1]= True
In[2]:= MatchQ[Times[a, b], Times[_, _]]
Out[2]= False
An explanation, or what am I doing wrong?
Hans
danl
(Daniel Lichtblau)
April 3, 2014, 6:05pm
2
You need HoldPattern to prevent that patter from evaluating to Power[_,2] .
MatchQ[Times[a, b], HoldPattern[Times[_, _]]]
Out[19]= True
Thanks,
But could you please elaborate on why HoldPattern is needed in case of Times, but not Power?
danl
(Daniel Lichtblau)
April 3, 2014, 6:19pm
4
As I stated, Times[_,_] evaluates to Power[_,2]. This happens before MatchQ ever sees its arguments. Power[_,_] on the other hand evalautes to itself. The different matching outcomes are explained by these facts.
Yes, as can be seen:
In[4]:= Times[_, _]
Out[4]= _^2
Thanks for very fast response!