MathGroup Archive 2010

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

Search the Archive

Re: Why does this pattern fail to match?

  • To: mathgroup at
  • Subject: [mg114165] Re: Why does this pattern fail to match?
  • From: Albert Retey <awnl at>
  • Date: Fri, 26 Nov 2010 05:26:03 -0500 (EST)
  • References: <iclfej$lb7$>


> 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       ]]

Usually it is the FullForm that is what you need to look at, but this is
what you already do. Your examples are all showing another common
problem when doing pattern matching: the patterns are evaluated before
the pattern matching is done, so the pattern that is tried is not what
you actually expect. you can see this from evaluating the pattern:

Times[___, Power[___], ___]

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

actually I think by using Trace you should be able to spot that the
second argument of MatchQ is evaluated, but it is something that is easy
to overlook. This will prevent the evaluation of the pattern and work as
you expect:

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

you might want to look also at Verbatim, which is another symbol which
is very helpful in constructing nontrivial patterns.

> 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: Compile and arrays
  • Next by Date: Re: Documentation of Eigenvalues[], Eigensystem[]
  • Previous by thread: Why does this pattern fail to match?
  • Next by thread: Re: Why does this pattern fail to match?