Re: Replace not spotting a replacement
- To: mathgroup at smc.vnet.net
- Subject: [mg103422] Re: [mg103399] Replace not spotting a replacement
- From: danl at wolfram.com
- Date: Mon, 21 Sep 2009 05:50:56 -0400 (EDT)
- References: <200909201021.GAA24204@smc.vnet.net>
> Hi Folks,
>
> Seems the replace function is being a bit dim here. This fails:
>
> Sqrt[2 \[Pi]] \[Tau]^(3/2) /. Sqrt[ 2 \[Pi] \[Tau]] -> \[Mu]
>
> Apologies if this is an FAQ - I couldn't see anything about it. What do I
> need to do here to coerce Mathematica into making this replacement? I
> tried using the
> Collect function on tau first, but that didn't seem to work. I also tried
> re-writing the replacement rule as
>
> Sqrt[2 \[Pi]] \[Tau]^(3/2) /. Sqrt[ 2 \[Pi] ] t^(1/2) -> \[Mu]
>
> But still no joy.
>
> Anyone know?
>
> Many thanks,
> David.
It's more of an Occasionally Asked Question. Replacement is working fine;
the fact is, your expression does not contain \[Tau]^(1/2). Syntactic
replacement cares about niceties of that sort.
What you want can usually be attained by algebraic replacement. I say
"usually" because it can be a bit tricky to get the right algebraic
relation recognized where it is needed. Here is a slight alteration of
some code from a few years back, that works on your example.
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]]]]]]
In[10]:= replacementFunction[Sqrt[2 \[Pi]] \[Tau]^(3/2),
Sqrt[2 \[Pi] \[Tau]] - \[Mu], \[Tau]]
Out[10]= \[Mu]^3/(2 \[Pi])
Some day I'll have an epiphany, or at least a minor vision, and see a way
to code this so that it always just "does the right thing", whatever that
might be.
Daniel Lichtblau
Wolfram Research
- References:
- Replace not spotting a replacement
- From: "claranet news" <david@carter-hitchin.clara.co.uk>
- Replace not spotting a replacement