MathGroup Archive 2010

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

Search the Archive

Question on replacementFunction

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: Formatting GraphicsRow/column
  • Next by Date: Re: integrate log*sinc
  • Previous by thread: Piecewise once more
  • Next by thread: Re: Question on replacementFunction