MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Pattern Matching Problem

  • To: mathgroup at smc.vnet.net
  • Subject: [mg43434] Re: Pattern Matching Problem
  • From: Dr Reinhard Simonovits <Reinhard.Simonovits at uni-graz.at>
  • Date: Wed, 17 Sep 2003 07:58:50 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Dear Ted,

One method is the "1:1 Translation" trick, from words directly into the 
Mathematica syntax
In words,
Take at first the terms without f,    Cases[expr,a_/;FreeQ[a,_f] ]
then those f 2 terms,    Cases[expr,f[_,2]]
then those f 3 terms,   Cases[expr,f[_,3]]

Put it all together:
In[4]:=
Clear[a,b,c,x,f,g,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];

Plus@@@{ Cases[expr, a_/;FreeQ[a,_f]], Cases[expr,f[_,2]], Cases[expr,f[_,3]] }

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


or the (slightly improved) "marker method" from Paul Abbot:

In[7]:=
CoefficientList[Collect[expr /. c:f[_, n_] :> c g^n , g], g] /.( 
0->Sequence[] )

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


Actually you are collecting the coefficients of the powers of g:
In[9]:=
Collect[expr /. c:f[_, n_] :> g^n  c, g ]//InputForm

Out[9]//InputForm=
a + b + c + x + y +  g^3*( f[w, 3] + f[x, 3] ) +  g^2*( f[w, 2] + f[x, 2] 
+   f[y, 2] + f[z, 2] )



>"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]}

********************************************
              Dr. Reinhard Simonovits
Handelsakademie  |  Karl Franzens University
Math Department  |  Inst. of Th. Physics
Grazbachgasse 71 |  Universitaetsplatz 5
               A-8010 Graz, Austria

Email: Reinhard.Simonovits at uni-graz.at
*********************************************



  • Prev by Date: Nonzero Min[]
  • Next by Date: Re: Discrete convolution problem
  • Previous by thread: Re: Pattern Matching Problem
  • Next by thread: Defining a function