Re: Re: Replace not spotting a replacement

*To*: mathgroup at smc.vnet.net*Subject*: [mg103643] Re: [mg103614] Re: [mg103399] Replace not spotting a replacement*From*: Daniel Lichtblau <danl at wolfram.com>*Date*: Thu, 1 Oct 2009 06:38:25 -0400 (EDT)*References*: <200909201021.GAA24204@smc.vnet.net> <52579.98.212.148.221.1253480392.squirrel@webmail.wolfram.com> <00bf01ca4079$2e230d30$8a692790$@clara.co.uk> <50884.140.177.99.93.1254191276.squirrel@webmail.wolfram.com> <200909300900.FAA09474@smc.vnet.net>

David Carter-Hitchin wrote: > Hi Daniel, > [Much of this was discussed and resolved off-line. I wanted to have the corrections on the record, since I go to MathGroup archives whenever I need to revisit/revamp this algwbraic replacement function. -DL] > Many thanks, but your post doesn't make sense to me. If we start with: > > Sqrt(2 pi) * tau^(3/2) > = Sqrt(2 pi) * tau^(1/2) * tau > = Sqrt(2 pi t) * tau > = mu tau > > This is not the result your function gives. I also do not understand the > way the second argument is constructed: > > {Sqrt[2 \[Pi] \[Tau]] - \[Mu], \[Mu]^2 - \[Tau]} > > Does that mean replace Sqrt(2 pi tau) with mu, then replace mu^2 with tau? > Which case the second replacement is wrong - it should be "2 pi tau". If I > do that then its fine: Yes, mine was incorrect, yours is what I should have had. > In[4]:= replacementFunction[ > Sqrt[2 \[Pi]] \[Tau]^(3/2), {Sqrt[2 \[Pi] \[Tau]] - \[Mu], \[Mu]^2 - > 2 \[Pi] \[Tau]}, {Sqrt[\[Tau]], \[Mu]}] > > > Out[4]= \[Mu] \[Tau] > > However, as you said, this is quite reliant on spotting the kind of > expression that turns up the first time and adapting it for a second go. > What would be nice would be to somehow specify "minimal" (i.e. simpler) > adjustments to the expression before replacement. I'll remark below on this business of spotting the subexpression flavors. > Nevertheless your function is very useful and have saved it for the next > time I'm working on something like this. > > Many thanks, > Kind regards, > David. Another mistake, pointed out off-line by Bobby Treat, was in dividing where I should be powering. Below is the corrected version (this is largely a note to myself, to be read when this topic reappears circa 2012). 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[hed] === Symbol && MemberQ[Attributes[hed], NumericFunction], Map[replacementFunction[#, rep, vars] &, expr], PolynomialReduce[expr, rep, vars][[2]]]]]] Now to the subject of figuring out how PolynomialReduce will create variables, what they are, etc. One can get a good idea of this using the special context function DistributedTermsList. It has been around, and stable, since version 4, and is used in several parts of Mathematica code, so I do not expect it to evaporate. It will give an internal form of its polynomialized input, as a list of coefficients and exponent vectors for each polynomial. It will then give a list of the "variables" used in these polynomials. One caveat is that it can only handle one polynomial list for input, so we must combine the first two arguments expr and rep above. For the example at hand, we would do this as below. In[9]:= InputForm[GroebnerBasis`DistributedTermsList[ {Sqrt[2*Pi]*tau^(3/2), Sqrt[2*Pi*tau]-mu, mu^2-2*Pi*tau}]] Out[9]//InputForm= {{{{{3, 0, 0}, Sqrt[2*Pi]}}, {{{1, 0, 0}, Sqrt[2*Pi]}, {{0, 0, 1}, -1}}, {{{2, 0, 0}, -2*Pi}, {{0, 0, 2}, 1}}}, {Sqrt[tau], tau, mu}} This tells us that Sqrt[tau] is in fact encapsulated as a variable. That is the hint needed to see how to specify and order the "variables" explicitly. We'll show the result of doing that, ordering Sqrt[tau]>mu and mu>tau. InputForm[GroebnerBasis`DistributedTermsList[ {Sqrt[2*Pi]*tau^(3/2), Sqrt[2*Pi*tau]-mu, mu^2-2*Pi*tau}, {Sqrt[tau],mu,tau}]] Out[13]//InputForm= {{{{{3, 0, 0}, Sqrt[2*Pi]}}, {{{1, 0, 0}, Sqrt[2*Pi]}, {{0, 1, 0}, -1}}, {{{0, 2, 0}, 1}, {{0, 0, 1}, -2*Pi}}}, {Sqrt[tau], mu, tau}} It is this different ordering that allows the substitutions to work as desired, where square roots of tau go to mu (with a constant factor), but squares of mu go back to tau (with a factor). Daniel Lichtblau Wolfram Research