MathGroup Archive 2010

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

Search the Archive

Re: Question on replacementFunction

  • To: mathgroup at smc.vnet.net
  • Subject: [mg109233] Re: Question on replacementFunction
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Sun, 18 Apr 2010 05:58:31 -0400 (EDT)

I can see a bug in replacementFunction. The following code fixes it:

replacementFunction[expr_, rep_, vars_] := 
 Module[{num = Numerator[expr], den = Denominator[expr], 
   hed = Head[expr], base, expon}, 
  If[PolynomialQ[num, vars] && 
    PolynomialQ[den, vars] && ! NumberQ[den], 
   replacementFunction[num, rep, vars]/
    replacementFunction[den, rep, vars], 
   If[hed === Power && Length[expr] == 2, 
    base = replacementFunction[expr[[1]], rep, vars];
    expon = replacementFunction[expr[[2]], rep, vars];
    PolynomialReduce[base^expon, rep, vars][[2]],
    If[PolynomialQ[expr, vars], 
     PolynomialReduce[expr, rep, vars][[2]], 
     If[Head[Evaluate[hed]] === Symbol && 
       MemberQ[Attributes[Evaluate[hed]], NumericFunction], 
      Map[replacementFunction[#, rep, vars] &, expr], 
      PolynomialReduce[expr, rep, vars][[2]]]]]]]


replacementFunction[x*y - w*z, x*y - w*z - 2*A, {x, y, z, w}]

2 A

Andrzej Kozlowski


On 17 Apr 2010, at 19:04, carlos at colorado.edu wrote:

> Could somebody explain why replacementFunction fails for
> the simpler x*y-w*z but works for (x*y-w*z)^2?   Of course the
> erratic behavior of ReplaceAll is well known.  Here are the tests
> (I took replacementFunction from an earlier thread):
> 
> replacementFunction[expr_, rep_, vars_] :=
> Module[{num = Numerator[expr], den = Denominator[expr],
>   hed = Head[expr], base, expon},
>  If[PolynomialQ[num, vars] &&
>    PolynomialQ[den, vars] && ! NumberQ[den],
>   replacementFunction[num, rep, vars]/
>    replacementFunction[den, rep, vars],
>   If[hed === Power && Length[expr] == 2,
>    base = replacementFunction[expr[[1]], rep, vars];
>    expon = replacementFunction[expr[[2]], rep, vars];
>    PolynomialReduce[base^expon, rep, vars][[2]],
>    If[Head[Evaluate[hed]] === Symbol &&
>      MemberQ[Attributes[Evaluate[hed]], NumericFunction],
>     Map[replacementFunction[#, rep, vars] &, expr],
>     PolynomialReduce[expr, rep, vars][[2]]]]]] ;
> 
> expr1 = x*y-w*z; res=x*y-w*z-2*A;
> Print[replacementFunction[expr1,res,{x,y,w,z}]//Simplify]; (* fails *)
> Print[ReplaceAll[expr1,x*y-w*z->(2*A)]];    (* OK *)
> Print[ReplaceAll[expr1,-x*y+w*z->-(2*A)]];  (* fails *)
> expr2 =(x*y-w*z)^2;
> Print[replacementFunction[expr2,res,{x,y,w,z}]//Simplify]; (* OK *)
> Print[ReplaceAll[expr2,x*y-w*z->(2*A)]];    (* OK *)
> Print[ReplaceAll[expr2,-x*y+w*z->-(2*A)]];  (* fails *)
> 
> Summary: it works for expr =(x*y-w*z)^n if n=2,3,4...
> also n=-2,-3,... but fails for n=1 or n=-1.  Any fix?  Thanks.
> 



  • Prev by Date: Re: Pade Approximation (further generalizations?---feature request)
  • Next by Date: Re: piecewise function
  • Previous by thread: Re: Question on replacementFunction
  • Next by thread: Re: Question on replacementFunction