Re: Pattern Matching Problem
- To: mathgroup at smc.vnet.net
- Subject: [mg43394] Re: Pattern Matching Problem
- From: bobhanlon at aol.com (Bob Hanlon)
- Date: Fri, 29 Aug 2003 07:15:54 -0400 (EDT)
- References: <bihp1v$b3u$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
I would take a different approach: ClearAll[f,a,b,c,w,x,y,z,sep]; sep[expr_] := Module[ {tm = List@@expr, at}, at = Select[tm,AtomQ]; Tr/@Prepend[ Split[ Sort[Complement[tm,at], #1[[2]]<#2[[2]]&], #1[[2]]==#2[[2]]&], at]]; expr=a+b+c+f[w,2]+f[w,3]+x+f[x,2]+f[x,3]+y+f[y,2]+f[z,2]; sep[expr] {a + b + c + x + y, f[w, 2] + f[x, 2] + f[y, 2] + f[z, 2], f[w, 3] + f[x, 3]} Bob Hanlon In article <bihp1v$b3u$1 at smc.vnet.net>, "Ersek, Ted R" <ErsekTR at navair.navy.mil> 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.