Re: More robust pattern based replacement rules?
- To: mathgroup at smc.vnet.net
- Subject: [mg120998] Re: More robust pattern based replacement rules?
- From: Irretrev Unable <irdeva_ble at yahoo.com>
- Date: Sun, 21 Aug 2011 05:31:38 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <1313198560.22381.YahooMailClassic@web45110.mail.sp1.yahoo.com>
- Reply-to: Irretrev Unable <irdeva_ble at yahoo.com>
Daniel sent me an excellent suggestion which I thought I would pass on to the group. Daniel's email recommending PolynomialReduce[] is below.
Here's my variant of his approach:
simpMethod[expr_] := Module[{pExpr},
pExpr = PolynomialReduce[expr, a*b - c*d, {b, d}];
pExpr[[1]].{e} + pExpr[[2]]
]
simpMethod[a*b - c*d]
Out[54]= e
simpMethod[c*d - a*b]
Out[55]= -e
simpMethod[2*a*b - c*d]
Out[56]= c*d + 2*e
simpMethod[a*b - 2*c*d]
Out[57]= -c*d + e
simpMethod[q*a*b - r*c*d] // Simplify
Out[58]= e*q + c*d*(q - r)
--- On Tue, 8/9/11, Daniel Lichtblau <danl at wolfram.com> wrote:
> From: Daniel Lichtblau <danl at wolfram.com>
> Subject: Re: More robust pattern based replacement rules?
> To: "Irretrev Unable" <irdeva_ble at yahoo.com>
> Date: Tuesday, August 9, 2011, 1:04 PM
>
> ----- Original Message -----
> > From: "Irretrev Unable" <irdeva_ble at yahoo.com>
> > To: mathgroup at smc.vnet.net
> > Sent: Tuesday, August 9, 2011 6:20:42 AM
> > Subject: More robust pattern based
> replacement rules?
> > I'm trying to come up with a method for creating
> somewhat more robust
> > pattern based replacement rules. I've cooked up the
> following simple
> > example:
> >
> > simpRule = a b v___ - c d v___ -> e v
> >
> > a b - c d /. simpRule
> >
> > produces e
> >
> > - a b + c d /. simpRule
> >
> > produces - a b + c d
> >
> > Is there a way to write one simpRule that can handle
> both cases?
> >
> > (The actual rule I'm trying to create has to match
> more terms with a
> > leading coefficient that is more complicated than a
> simple minus sign,
> > but I figure that for an initial post I should stick
> with a simple
> > question.)
> >
> > Thanks in advance.
>
> PolynomialReduce might be a better function for your
> needs.
>
> In[28]:= e1 = a*b - c*d;
> e2 = c*d - a*b;
> rule = e - (a*b - c*d);
> PolynomialReduce[e1, rule, {a, b, c, d}][[2]]
> PolynomialReduce[e2, rule, {a, b, c, d}][[2]]
>
> Out[31]= e
>
> Out[32]= -e
>
> Daniel Lichtblau
> Wolfram Research
>
>