Re: Pattern Matching Problem

• To: mathgroup at smc.vnet.net
• Subject: [mg43420] Re: Pattern Matching Problem
• From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
• Date: Tue, 16 Sep 2003 04:36:43 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```On Wednesday, August 27, 2003, at 09:05 AM, Ersek, Ted R wrote:

> Consider the following:
>
> In[1]:=
>   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
>
>
>

I do not see any obvious way to do this using just "pure" pattern
matching, but one can always do something like this:

expr /. (x__) + (y__f) :> {{x}, Apply[Plus, Split[Sort[{y}, #1[[2]] =!=
#2[[2]] & ],
#1[[2]] == #2[[2]] & ], {1}]}

{{a, b, c, x, y}, {f[w, 2] + f[x, 2] + f[y, 2] + f[z, 2], f[w, 3] +
f[x, 3]}}

Andrzej Kozlowski
Yokohama, Japan
http://www.mimuw.edu.pl/~akoz/
http://platon.c.u-tokyo.ac.jp/andrzej/

```

• Prev by Date: Re: Pattern Matching Problem
• Next by Date: Re: Pattern Matching Problem
• Previous by thread: Re: Pattern Matching Problem
• Next by thread: Re: Pattern Matching Problem