       RE: Pattern Matching Problem

• To: mathgroup at smc.vnet.net
• Subject: [mg43396] RE: Pattern Matching Problem
• From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
• Date: Tue, 16 Sep 2003 04:35:46 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```>-----Original Message-----
>From: Ersek, Ted R [mailto:ErsekTR at navair.navy.mil]
To: mathgroup at smc.vnet.net
>Sent: Wednesday, August 27, 2003 10:05 AM
>To: mathgroup at smc.vnet.net
>Subject: [mg43396]  Pattern Matching Problem
>
>
>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
>

Using some sort of Delete (with complementary criterion) appears to work,
e.g.

In:= seca = Union[Cases[expr, f[_, a_] :> a]]
Out= {2, 3}

In:=
patts = Prepend[e_ /; ! MatchQ[e, f[_, #]] & /@ seca, f[_, _]]
Out=
{f[_, _], e_ /; ! MatchQ[e, f[_, 2]], e_ /; ! MatchQ[e, f[_, 3]]}

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

... based on observations

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

In:= DeleteCases[expr, f[_, _]]
Out= a + b + c + x + y

so this could work, but ugly:

In:= Head[expr] @@ Cases[expr, f[_, 2]]
Out= f[w, 2] + f[x, 2] + f[y, 2] + f[z, 2]

perhaps more convenient, but ugly again at use of pattern matching:

In:= Select[expr, MatchQ[#, f[_, 2]] &]
Out= f[w, 2] + f[x, 2] + f[y, 2] + f[z, 2]

negating DeleteCases, ugly same way:

In:= DeleteCases[expr, e_ /; ! MatchQ[e, f[_, 2]]]
Out= f[w, 2] + f[x, 2] + f[y, 2] + f[z, 2]

Extracting gives same as Cases:

In:= Extract[expr, Position[expr, f[f_, 2]]]
Out= {f[w, 2], f[x, 2], f[y, 2], f[z, 2]}

As does Take:

In:= Take[expr, #] & /@ Position[expr, f[f_, 2]]
Out= {f[w, 2], f[x, 2], f[y, 2], f[z, 2]}

Delete is fine (as DeleteCases)

In:=
Delete[expr,
Complement[List /@ Range[Length[expr]], Position[expr, f[f_, 2]]]]
Out= f[w, 2] + f[x, 2] + f[y, 2] + f[z, 2]

--
Hartmut Wolf

```

• Prev by Date: Re: Pick up coordinates along different contours.
• Next by Date: Elliptic pDE with two closed boundaries
• Previous by thread: Re: Pattern Matching Problem
• Next by thread: Re: Pattern Matching Problem