Re: Question about Cases command.
- To: mathgroup at yoda.physics.unc.edu
- Subject: Re: Question about Cases command.
- From: withoff (David Withoff)
- Date: Tue, 28 Jun 1994 15:45:07 -0500
> In[2]:= myterms = {Derivative[1,2][a][x,y], Derivative[1,1][b][x,y]}; > > In[3]:= Map[Head, myterms] > > (1,2) (1,1) > Out[3]= {a , b } > > Now try to uses Cases: > > In[4]:= Cases[myterms,_Derivative[__][a]] > > Out[4]= {} > > Why didn't it select the "a" term out? Try: > > In[5]:= Cases[myterms, _Derivative[__][_]] > > Out[5]= {} > > In[6]:= Cases[myterms, _Derivative[__][__]] > > 3 2 > d a d b > Out[6]= {------, ------} > 1 2 1 1 > dx dy dx dy > > Ok, now why must I use two underscores in this form? ( In[6] ) > The "a" and the "b" are single elements. > > > Scott A. Herod > Program in Applied Mathematics > University of Colorado, Boulder > (sherod at newton.colorado.edu) Compare In[9]:= FullForm[myterms[[1]]] Out[9]//FullForm= Derivative[1, 2][a][x, y] with In[10]:= FullForm[_Derivative[__][_]] Out[10]//FullForm= Blank[Derivative][BlankSequence[]][Blank[]] The pattern will match anything with a single argument and with a head that matches Blank[Derivative][BlankSequence[]], which means that the head must be something with one or more elements and the head of the head must match Blank[Derivative]. The expression Derivative[1, 2][a][x, y] has two elements, so it fails the first test. The pattern Blank[h] does literal comparisons of the head. It does not use the Head function, and does not allow for the effects of special pattern expressions (things like __). For example: In[13]:= MatchQ[f[0][], Blank[f[0]]] Out[13]= True In[14]:= MatchQ[f[0][], Blank[f[_]]] Out[14]= False In[15]:= MatchQ[f[_][], Blank[f[_]]] Out[15]= True Also, the special input notation _h for Blank[h] works only if h is a symbol. The FullForm must be used if h is not a symbol. That is, _f[0] parses as Blank[f][0], and will match expressions, with element 0 and a head that matches _f, not expressions with a head of f[0]. The pattern that matches expressions with head f[0] must be entered as Blank[f[0]]. Dave Withoff Research and Development Wolfram Research