Re: Pattern Matching Problem
- To: mathgroup at smc.vnet.net
- Subject: [mg43410] Re: Pattern Matching Problem
- From: "Mihajlo Vanevic" <mvane at EUnet.yu>
- Date: Tue, 16 Sep 2003 04:36:12 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Hi all, this is not actually the answer to Ted's question, but instead the way to extract "like" groups of elements from the list. pickPattern[list_,pat_] picks elements from the list that matches pattern pat, and splits them into "like" groups where "like" means that the elements have matched the same *named* subpatterns. The third argument, if supplied, explicitly lists pattern names that should match the same entity. Clear[pickPattern]; pickPattern[expr_, pat_, isti_:{}] := Module[{i, j, k, p, q, localpat, isti1}, localpat = pat; isti1 = isti; q = If[isti == {}, Cases[localpat, x_Pattern :> First[x], {0, \[Infinity]}, Heads -> True], isti1]; k[localpat] := Evaluate[Sequence @@ q]; i[f : localpat] := (p = k[f]; j[p] = {j[p], f}); j[n__] := Sequence[]; i /@ expr; Flatten /@ Union[j[k[#]] & /@ expr] ]; Tests: expr = {a, b, c, x, y, f[1, 1], f[w, 2], f[w, 3], f[x, 2], f[x, 3], f[y, 2], f[z, 2], g[w, 2], g[w, 3], h[1, 1], j[1, 1]}; In[]:= pickPattern[expr, _[_, a_]] Out[]= {{f[1, 1], h[1, 1], j[1, 1]}, {f[w, 3], f[x, 3], g[w, 3]}, {f[w, 2], f[x, 2], f[y, 2], f[z, 2], g[w, 2]}} In[]:= pickPattern[expr, a_[_, b_]] Out[]= {{f[1, 1]}, {g[w, 2]}, {g[w, 3]}, {h[1, 1]}, {j[1, 1]}, {f[w, 3], f[x, 3]}, {f[w, 2], f[x, 2], f[y, 2], f[z, 2]}} In[]:= pickPattern[expr, b_[_, a_], {b}] Out[]= {{h[1, 1]}, {j[1, 1]}, {g[w, 2], g[w, 3]}, {f[1, 1], f[w, 2], f[w, 3], f[x, 2], f[x, 3], f[y, 2], f[z, 2]}} In[]:= pickPattern[expr, _[a_, b_]] Out[]= {{f[x, 2]}, {f[x, 3]}, {f[y, 2]}, {f[z, 2]}, {f[w, 2], g[w, 2]}, {f[w, 3], g[w, 3]}, {f[1, 1], h[1, 1], j[1, 1]}} In[]:= pickPattern[expr, _[a_, b_] /; a =!= b] Out[]= {{f[x, 2]}, {f[x, 3]}, {f[y, 2]}, {f[z, 2]}, {f[w, 2], g[w, 2]}, {f[w, 3], g[w, 3]}} In[]:= pickPattern[2expr, _.*t_[a_, b_] /; (a =!= b && t =!= Times), {a}] Out[]= {{2 f[y, 2]}, {2 f[z, 2]}, {2 f[x, 2], 2 f[x, 3]}, {2 f[w, 2], 2 f[w, 3], 2 g[w, 2], 2 g[w, 3]}} In[]:= pickPattern[2expr, _.*(f | g | h)[a_, b_] /; a =!= b] Out[]= {{2 f[x, 2]}, {2 f[x, 3]}, {2 f[y, 2]}, {2 f[z, 2]}, {2 f[w, 2], 2 g[w, 2]}, {2 f[w, 3], 2 g[w, 3]}} In[]:= pickPattern[2expr, _.*(f | g | h)[a_, b_], {a}] Out[]= {{2 f[y, 2]}, {2 f[z, 2]}, {2 f[1, 1], 2 h[1, 1]}, {2 f[x, 2], 2 f[x, 3]}, {2 f[w, 2], 2 f[w, 3], 2 g[w, 2], 2 g[w, 3]}} Regards, Mihajlo Vanevic mvane at EUnet.yu 2003-08-28 ************************************************************** * At 2003-08-27, 04:05:00 * 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. > >------------------- >Thanks, > Ted Ersek **************************************************************