Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2000
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2000

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

Search the Archive

Re: Pattern Matching

  • To: mathgroup at smc.vnet.net
  • Subject: [mg23850] Re: [mg23826] Pattern Matching
  • From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
  • Date: Mon, 12 Jun 2000 01:17:40 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

I just noticed that I failed to reply to the last part of your question. If
I understand you correctly you want to be able to convert all terms of the
form Sum[x[i] b[i],{i,1,n}] to z but leave alone ternms like x[1] b[2]. It
seems to me that the easiest way to accomplish this is to use a global
function rather than local rules. The following function will (I hope) do
what (according to my interpretation) you want to do:

In[1]:=
Clear[funct]

In[2]:=
funct[expr_Plus] := expr /. HoldPattern[Plus[Times[x[_], b[_]] ..] ] :> z;
funct[(x[i_]*b[j_] /; i != j) + c_] := x[i]*b[j] + funct[c];
funct[x_] := x

Now, you get:

In[5]:=
funct[b[1] x[1] + b[2] x[2] + b[3] x[4]]

Out[5]=
z + b[3] x[4]


Moreover, you can use MapAll to deal with more complex expresions, e.g.:

In[6]:=
expr = (b[1]*x[1] + b[2]*x[2] + b[3]*x[4])/(b[2]*x[2] + b[3]*x[3] +
b[4]*x[5])

Out[6]=
b[1] x[1] + b[2] x[2] + b[3] x[4]
---------------------------------
b[2] x[2] + b[3] x[3] + b[4] x[5]

In[7]:=
MapAll[funct, expr]

Out[7]=
z + b[3] x[4]
-------------
z + b[4] x[5]

-- 
Andrzej Kozlowski
Toyama International University, JAPAN

For Mathematica related links and resources try:
<http://www.sstreams.com/Mathematica/>



on 00.6.10 10:48 PM, Andrzej Kozlowski at andrzej at tuins.ac.jp wrote:

> The problem is that Plus[Times[x[_], b[_]] ..] evaluates before the pattern
> matching is performed and Plus disappears from the Pattern (Plus[a] gives a):
> 
> In[6]:=
> Plus[Times[x[_], b[_]] ..]
> Out[6]=
> b[_] x[_] ..
> 
> You have to prevent this from happening with HoldPattern:
> 
> In[7]:=
> Plus[x[1] b[1] , x[2] b[2] ] /. HoldPattern[Plus[Times[x[_], b[_]] ..] ] ->
> z
> 
> Out[7]=
> z
> 
> Andrzej
> -- 
> Andrzej Kozlowski
> Toyama International University, JAPAN
> 
> For Mathematica related links and resources try:
> <http://www.sstreams.com/Mathematica/>
> 
> 
> on 6/10/00 4:00 PM, Johannes Ludsteck at ludsteck at zew.de 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: Minimize number of mulitplications
  • Next by Date: Re: Derivative of Root objects
  • Previous by thread: Re: Pattern Matching
  • Next by thread: Re: Pattern Matching