       Re: Pattern Matching Problem

• To: mathgroup at smc.vnet.net
• Subject: [mg43407] Re: Pattern Matching Problem
• From: John Tanner <john at janacek.demon.co.uk>
• Date: Tue, 16 Sep 2003 04:36:02 -0400 (EDT)
• References: <bihp1v\$b3u\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```In article <bihp1v\$b3u\$1 at smc.vnet.net>, "Ersek, Ted R"
<ErsekTR at navair.navy.mil> writes
>Consider the following:
>
>In:=
>  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.
>
>-------------------
>Thanks,
>   Ted Ersek
>
-----------------------------------------------------------------------
---------------------------------
If you are sure you have a simple sum, then use Cases or equivalent on
expr itself:

In:=Plus @@ Cases[expr, _Symbol]
Out:=a+b+c+x+y

In:= Plus @@ Cases[expr, _[_, 2]]
Out:=f[w,2]+f[x,2]+f[y,2]+f[z,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:= exprlist=ExpandAll[expr] /. Plus -> List
Out:={a,b,c,x,y,f[w,2],f[w,3],f[x,2],f[x,3],f[y,2],f[z,2]}

In:= Cases[exprlist, _[_, 2]]
Out:={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
improve!):

In:=Prepend[
(Plus @@ Cases[exprlist,_[_,#]])& /@ {2,3},
Plus @@ Cases[exprlist,_Symbol]]
Out:={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
Split[
Sort[Cases[exprlist,_[_,_]],OrderedQ[{Part[#1,-1],Part[#2,-1]}]&],
Part[#1,-1]==Part[#2,-1]&]
which goes some way towards being more general.  [use these at your own
risk..]

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

In:=DeleteCases[expr,#]& /@ {_[_,_],_Symbol | _[_,3],_Symbol |
_[_,2]}
Out:={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 janacek.demon.co.uk
mantra - curse Microsoft, curse...   work -  john.tanner at baesystems.com
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