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: [mg23854] Re: Pattern Matching
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Mon, 12 Jun 2000 01:17:46 -0400 (EDT)
  • References: <8hsu3g$dh4@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Johannes,

Re your first question.

We get

x[1] b[1] + x[2] b[2] /.
  Plus[Times[x[_],  b[_]] ..] -> z

    b[1] x[1] + b[2] x[2]

possibly because, before the matching and replacement, the left side of the
rule is evaluated thus:

Plus[Times[x[_],  b[_]] ..]

        b[_] x[_] ..

So we end up with

x[1] b[1] + x[2] b[2] /.              (*1*)
  (x[_]  b[_]) .. -> z

        b[1] x[1] + b[2] x[2]

We can avoid this by using HoldPattern:


x[1] b[1] + x[2] b[2] /.
  HoldPattern[Plus[Times[x[_],  b[_]] ..]] -> z

        z


However it does seem odd that, in spite of (*1*), we get

x * b[1] + x* b[2] /.
  (x* b[_]) .. -> z

        2 z

--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

"Johannes Ludsteck" <ludsteck at zew.de> wrote in message
news:8hsu3g$dh4 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,
> 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: Lorenz system
  • Next by Date: A Functional Expression Trick
  • Previous by thread: Re: Pattern Matching
  • Next by thread: Re: Pattern Matching