RE: Re: Why doesn't this rule work?
- To: mathgroup at smc.vnet.net
- Subject: [mg32504] RE: [mg32451] Re: Why doesn't this rule work?
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.de>
- Date: Thu, 24 Jan 2002 05:20:57 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Dear Allen and James,
this seems to be a subtle problem of pattern matching when a Condition is
imposed on a Pattern with Head Function with attribute Flat. Observe
Remove[g]
SetAttributes[g, {Flat, Orderless}]
g[-a, -b, c] /. g[n_. a , m_. c] :> h[n, m] /; True
g[-b, h[-1, 1]]
g[-a, -b, c] /. g[n_. a , m_. c] /; True :> h[n, m]
g[-a, -b, c]
No match!
(which is understandable in a certain way, I think)
but
g[-a, -b, c] /. g[n_. a , m_. c, x___] /; True :> g[h[n, m], x]
g[-b, h[-1, 1]]
Applied to your example makes it working
c - a - b /. n_. a + m_. c + x___ /; n == -m :> m b + x
0
Imposing the condition on the pattern is quite a different thing
as to the rhs of the rule(delayed).
--
Hartmut Wolf
> -----Original Message-----
> From: Allan Hayes [mailto:hay at haystack.demon.co.uk]
To: mathgroup at smc.vnet.net
> Sent: Tuesday, January 22, 2002 9:19 AM
> To: mathgroup at smc.vnet.net
> Subject: [mg32504] [mg32451] Re: Why doesn't this rule work?
>
>
> James,
>
> I haven't sorted out what is happening but it seems to be due
> to the initial
> matching, before the condition is tested. If we move the
> condition to the
> right side of the rule (and make the necessary change ot RuleDelayed)
> everything works:
>
> c - a - b /. (n1_.)c + (n2_.)a :> n1b /; n1 == -n2
>
> 0
>
> Also we have,
>
> Simplify[c - a - b, c - a == b]
>
> 0
>
> --
> 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
>
>
> "James Jennings" <djeimz at no.spam.megaseattle.com> wrote in message
> news:a2ghu1$ki$1 at smc.vnet.net...
> > Suppose I want to simplify expressions linear in a, b, and
> c where a+b=c
> > -- something of this sort.
> >
> > In[]:= a+b /. a+b -> c
> >
> > Out[]= c
> >
> > In[]:= a+b+c /. a+b -> c
> >
> > Out[]= 2 c
> >
> > With more complicated coefficients I can use:
> >
> > In[]:= -2 a - 2 b /. n_. a+n_. b -> n c
> >
> > Out[]= -2 c
> >
> > In[]:= -a-b+c /. n_. a+n_. b -> n c
> >
> > Out[]= 0
> >
> > I also want to apply rules based on c-a=b
> >
> > In[]:= c-a /. n1_. c+n2_. a /; n1==-n2 -> n1 b
> >
> > Out[]= b
> >
> > The problem comes with I apply the above rule to longer expressions.
> >
> > In[]:= c-a-b /. n1_. c+n2_. a /; n1==-n2 -> n1 b
> >
> > Out[]= -a-b+c
> >
> > Why didn't that work? I had thought that since Plus[] is
> Orderless, my
> > rule ought to be applied to all pieces of my expression,
> but it doesn't
> > appear to be.
> >
> > ---
> >
> > Before someone suggests that I should just set c = a+b and
> be done with
> > it, my actual problem involved 12 objects like a, b, and c, where 16
> > distinct triplets add up like a+b=c. I'm looking for rules that will
> > keep my expressions as short as possible, even if they
> aren't unique.
> >
> > Thanks.
> > James
> >
>
>
>