MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Why does this pattern fail to match?

  • To: mathgroup at
  • Subject: [mg114166] Re: Why does this pattern fail to match?
  • From: Bob Hanlon <hanlonr at>
  • Date: Fri, 26 Nov 2010 05:26:14 -0500 (EST)

expr = Times[-1, Power[s, -1]];


{Flat, Listable, NumericFunction, OneIdentity, Orderless, Protected}

Note that Times has attribute Orderless and pattern for Power can only have one or two blanks not three. 

{MatchQ[expr, Times[___, Power[__]]],
 MatchQ[expr, Times[___, Power[_]]],
 MatchQ[expr, Times[__, Power[_]]],
 MatchQ[expr, Times[Power[__], ___]],
 MatchQ[expr, Times[Power[_], ___]],
 MatchQ[expr, Times[Power[_], __]],
 MatchQ[expr, Times[Power[__], _]]}

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

And, as you discovered in part

{MatchQ[expr, Times[___, _Power]],
 MatchQ[expr, Times[__, _Power]],
 MatchQ[expr, Times[_, _Power]],
 MatchQ[expr, Times[_Power, ___]],
 MatchQ[expr, Times[_Power, __]],
 MatchQ[expr, Times[_Power, _]]}

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

However, given that these all work, it is not clear why the following fail

{MatchQ[expr, Times[_, Power[_]]],
 MatchQ[expr, Times[__, Power[__]]],
 MatchQ[expr, Times[Power[__], __]]}

{False, False, False}

Bob Hanlon

---- kj < at> wrote: 


I'm tearing my hair out over this one.  Could someone please explain
to me why all the following MatchQ expressions 

MatchQ[Times[ -1, Power[s, -1]      ],
       Times[___, Power[___],    ___]]

MatchQ[Times[ -1, Power[s, -1]],
       Times[___, Power[___]  ]]

MatchQ[Times[ -1,  Power[s, -1]      ],
       Times[___, _Power        , ___]]

return False?  (In all cases, s is undefined).  And yet, this succeeds:

MatchQ[Times[ -1,  Power[s, -1]],
       Times[___, _Power       ]]

Is there a systematic way to debug/troubleshoot such pattern matching
problems? Trace is useless here.

Also, whatever the reason is for the failure of this pattern to
match, where is this reason documented?  I've gone blind poring
over the documentation trying to find an answer, without success.



  • Prev by Date: Re: Efficient search for bounding list elements
  • Next by Date: Re: Why does this pattern fail to match?
  • Previous by thread: Re: Why does this pattern fail to match?
  • Next by thread: Re: Why does this pattern fail to match?