RE: Pattern Matching

• To: mathgroup at smc.vnet.net
• Subject: [mg23856] RE: [mg23826] Pattern Matching
• From: "David Park" <djmp at earthlink.net>
• Date: Mon, 12 Jun 2000 01:17:50 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```> -----Original Message-----
> From: Johannes Ludsteck [mailto:ludsteck at zew.de]
To: mathgroup at smc.vnet.net
> Dear Group Members,
> I would like to "find" and replace expressions with the simple
> structure x[1] b[1]+x[2] b2]+...+x[n] b[n]
> I tried to use the following replacement rule
> In[27]:= x[1] b[1] + x[2] b[2] /. Plus[Times[x[_], b[_]] ..] -> z
>
> Out[27]= b[1] x[1] + b[2] x[2] + b[3] x[3]
> Which didn't work (Out[27] should be z).
> Why?
> The following FullForm seems to give exactly the structure I used
> in my replacement rule.
>
> In[17]:=
> FullForm[x[1] b[1] + x[2] b[2] + x[3] b[3]]
> Out[17]//FullForm=
> Plus[Times[b[1], x[1]], Times[b[2], x[2]], Times[b[3], x[3]]]
>
> Even if this worked, my pattern wouldn't account for equal indices,
> i.e. it would match x[1] b[500]+x[12] b[3], even if it shouldn't.
>
> Any suggestions?
> Thanks,
> 	Johannes Ludsteck
>
>
> Johannes Ludsteck
> Centre for European Economic Research (ZEW)
> Department of Labour Economics,

Johannes,

I hope you get many different answers to this problem. Here is one solution:

e1 = Sum[x[i]b[i], {i, 1, 5}] + x[1]b[500] + x[12]b[3];
e1 /. b[n_]x[n_] -> z /. n_Integer z -> z

z + b[500] x[1] + b[3] x[12]

David Park