MathGroup Archive 2003

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

Search the Archive

Re: Pattern Matching Problem

  • To: mathgroup at
  • Subject: [mg43407] Re: Pattern Matching Problem
  • From: John Tanner <john at>
  • Date: Tue, 16 Sep 2003 04:36:02 -0400 (EDT)
  • References: <bihp1v$b3u$>
  • Sender: owner-wri-mathgroup at

In article <bihp1v$b3u$1 at>, "Ersek, Ted R" 
<ErsekTR at> writes
>Consider the following:
>  ClearAll[f,a,b,c,w,x,y,z];
>  expr=a+b+c+f[w,2]+f[w,3]+x+f[x,2]+f[x,3]+y+f[y,2]+f[z,2];
>Can somebody suggest a general way to seperate the terms above into like
>groups. By "like" I mean having the same second argument for (f).  So for
>this example I want to get
>{a+b+c+x+y,  f[w,2]+f[x,2]+f[y,2]+f[z,2],   f[w,3]+f[x,3]}
>The pattern matcher should be able to do this because Plus has attributes
>Flat and Orderless.  However I can't find a way to make it happen.
>   Ted Ersek
If you are sure you have a simple sum, then use Cases or equivalent on 
expr itself:

In[3]:=Plus @@ Cases[expr, _Symbol]

In[4]:= Plus @@ Cases[expr, _[_, 2]]

This procedure is "slightly unsafe", so it is probably better to do a 
bit of expanding first (hopefully this catches most problems, but such 
problem cases will need extra Cases[]).  Fortunately the List generation 
works even for negative valued terms, but to be general beware of sums 
in the denominator(s) of any term(s)!:

In[5]:= exprlist=ExpandAll[expr] /. Plus -> List

In[6]:= Cases[exprlist, _[_, 2]]
Out[6]:={f[w, 2], f[x, 2], f[y, 2], f[z, 2]}

so finally (if a little inelegant and not really "general": feel free to 

   (Plus @@ Cases[exprlist,_[_,#]])& /@ {2,3},
   Plus @@ Cases[exprlist,_Symbol]]
Out[7]:={a+b+c+x+y, f[w,2]+f[x,2]+f[y,2]+f[z,2], f[w,3]+f[x,3]}

There are various other ways of improving the use of the list: possibly
    Union[Part[#,-1]& /@ Cases[exprlist,_[_,_]]]
to generate the list of the values of the second argument, or
which goes some way towards being more general.  [use these at your own 

There is also a "really risky" but very direct alternative, good luck if 
you want to try to make it general:

In[8]:=DeleteCases[expr,#]& /@ {_[_,_],_Symbol | _[_,3],_Symbol | 
Out[8]:={a+b+c+x+y, f[w,2]+f[x,2]+f[y,2]+f[z,2], f[w,3]+f[x,3]}

   from -   John Tanner                 home -  john at
   mantra - curse Microsoft, curse...   work -  john.tanner at
I hate this 'orrible computer,  I really ought to sell it:
It never does what I want,      but only what I tell it.

  • Prev by Date: Elliptic pDE with two closed boundaries
  • Next by Date: Re: Creating Nested GridBoxes
  • Previous by thread: RE: Pattern Matching Problem
  • Next by thread: Re: Pattern Matching Problem