Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

MatchQ, silly question

2 views
Skip to first unread message

janos

unread,
Nov 20, 2009, 6:41:38 AM11/20/09
to
MatchQ[3 x^2, #] & /@ {3 x_^2, 3 x_^_, _ x_^_, _ _^_, _ (_^_), _ *
(_^_)}

dives False in the last cases. Why? The FullForm

Power[Blank[ ],Plus[1,Blank[ ]]]

contains Plus, why?

Thank you.

J=E1nos

Sjoerd C. de Vries

unread,
Nov 21, 2009, 3:35:33 AM11/21/09
to
Mathematica evaluates all arguments and hence _ _^_ gets evaluated as
well. Since the underscore is simply the function Blank[] this becomes
Blank[] Blank[]^Blank[]. This can be simplified to Blank[]^(Blank[]
+1). And now, the two structures are dissimilar and don't match. To
prevent this, the blanks indeed need to be labeled to prevent
simplification.

Cheers -- Sjoerd

Raffy

unread,
Nov 21, 2009, 3:36:53 AM11/21/09
to

x * x ^ x = x ^ (x + 1)

Andrzej Kozlowski

unread,
Nov 21, 2009, 3:37:52 AM11/21/09
to
Hi Janos. I assume you are back home ;-)

The problem is that your last three patterns evaluate to something that 3 x^2 does not match, namely

FullForm[_ (_^_)]

Power[Blank[],Plus[1,Blank[]]]

To understand this better look a this:

x*x^x

x^(x + 1)

exactly the same thing will happen if you replace x by Blank[] - you will just get Blank[] to the power Blank[]+1. One way to avoid this sort of problems is to use HoldPattern. So:

MatchQ[3 x^2, #] & /@ {3 x_^2, 3 x_^_, _ x_^_,

HoldPattern[_ _^_], HoldPattern[_ (_^_)], HoldPattern[_*(_^_)]}

{True,True,True,True,True,True}

Andrzej

Albert Retey

unread,
Nov 21, 2009, 3:41:34 AM11/21/09
to
janos schrieb:


just evaluate this:

(_)*(_)^(_)

to see that it gives

_^(_+1)

just as x*x^x would evaluate to x^(x+1), _ or Blank[] is not treated
special in this case. To prevent the evaluation of the patterns, use
HoldPattern:

MatchQ[3 x^2, #] & /@ {3 x_^2, 3 x_^_, _ x_^_, HoldPattern[_ _^_],


HoldPattern[_ (_^_)], HoldPattern[_*(_^_)]}

works as I think you expect...

hth,

albert

David Bailey

unread,
Nov 21, 2009, 3:43:00 AM11/21/09
to
Because expressions still evaluate, even if they contain Blank[] - this
can be easily seen by entering the expression _+_ .

Wrap the pattern in HoldPattern to prevent evaluation.

David Bailey
http://www.dbaileyconsultancy.co.uk

janos

unread,
Nov 22, 2009, 6:15:20 AM11/22/09
to
Thank you for all, I learned from the answers :)!
J=E1nos

janos

unread,
Nov 22, 2009, 6:15:30 AM11/22/09
to
An alternative solution from my student, Attila L=E1szl=F3 NAGY is:

MatchQ[3 x^2, #] & /@ {3 x_^2,

3 x_^_, _ x_^_, _ _Symbol^_, _ (_Symbol^_), _*(_Symbol^_)}

J=E1nos
**************************************************************************

David Bailey

unread,
Nov 24, 2009, 6:03:44 AM11/24/09
to
This works because _ and _Symbol don't look the same to Mathematica when
it does its arithmetic. The only problem with that approach, is that you
might re-use the code in a slightly more complicated context and hit
problems again.

David Bailey
http://www.dbaileyconsultancy.co.uk

0 new messages