[Date Index]
[Thread Index]
[Author Index]
Re: Pattern Matching
*To*: mathgroup at smc.vnet.net
*Subject*: [mg23839] Re: [mg23826] Pattern Matching
*From*: Wagner Truppel <wtruppel at uci.edu>
*Date*: Mon, 12 Jun 2000 01:17:31 -0400 (EDT)
*References*: <200006100700.DAA13117@smc.vnet.net>
*Sender*: owner-wri-mathgroup at wolfram.com
I'm also uncertain why x[1] b[1] + x[2] b[2] /. Plus[Times[x[_],
b[_]] ..] -> z does not work as intended. However, I've managed to
cook up a solution, admittedly not very elegant:
In[17]:= x[1] b[1] + x[2] b[2] + x[3] b[3] /. Times[ x[n_], b[n_] ]
-> z /. _ z -> z
Out[17] = z
In[18]:= x[1] b[1] + x[2] b[2] + x[3] b[30] /. Times[ x[n_], b[n_] ]
-> z /. _ z -> z
Out[18] = z + b[30] x[3]
In[19]:= x[1] b[10] + x[2] b[20] + x[3] b[30] /. Times[ x[n_], b[n_]
] -> z /. _ z -> z
Out[19] = x[1] b[10] + x[2] b[20] + x[3] b[30]
Note the space between the blank and the z after the second /.
Hope this helps.
Wagner
At 3:00 AM -0400 on 6/10/00, Johannes Ludsteck wrote:
>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,
>Human Resources and Social Policy
>Phone (+49)(0)621/1235-157
>Fax (+49)(0)621/1235-225
>
>P.O.Box 103443
>D-68034 Mannheim
>GERMANY
>
>Email: ludsteck at zew.de
Prev by Date:
**Re: Derivative of Root objects**
Next by Date:
**Re: Lorenz system**
Previous by thread:
**Pattern Matching**
Next by thread:
**RE: Pattern Matching**
| |