MathGroup Archive 2010

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

Search the Archive

Re: Replacement Rule with Sqrt in denominator

Am 20.11.2010 12:11, schrieb kj:
> In<ic5igm$44p$1 at>  Themis Matsoukas<tmatsoukas at>  writes:
>> This replacement rule works,
>> Sqrt[-4 \[Zeta]^2 + (1 + \[Rho]^2)^2] /.
>> Sqrt[-4 \[Zeta]^2 + (1 + \[Rho]^2)^2] ->  G
>> G
>> but this doesn't:
>> 1/Sqrt[-4 \[Zeta]^2 + (1 + \[Rho]^2)^2] /.
>> Sqrt[-4 \[Zeta]^2 + (1 + \[Rho]^2)^2] ->  G
>> 1/Sqrt[-4 \[Zeta]^2 + (1 + \[Rho]^2)^2]
>> The only difference is that the quantity to be replaced is in the denominator. If I remove Sqrt from the replacement rule it will work but I don't understand why a square root in the denominator (but not in the numerator) causes the rule to fail.
> Yes, that's very annoying.
> The reason is that the pattern matching is done on the full forms
> of the expressions, which is not always immediately obvious.  To
> see this compare FullForm[Sqrt[x]], which is
> Power[x, Rational[1, 2]], with FullForm[1/Sqrt[x]], which is
> Power[x, Rational[-1, 2]].
> I don't know a way to write the replacement rule so that it works
> for both Sqrt[x] and 1/Sqrt[x], but maybe someone else does.

Use rules like

HoldPattern[x^Rational[a_,2]] :>  G^a

eg this works for contraction of products of fractional powers

((1 + I x)^(1/2)/(1 - I x)^(1/2)) /.
	{HoldPattern[a_^Rational[b_, c_] d_^Rational[e_, c_]]
		:> (a^b d^e)^(1/c)}

while this one does not work

(1 + I x)^(1/2)/(1 - I x)^(
   1/2) /. {a_^(b_/c_) d_^(e_/c_) :> (a^b d^e)^(1/c)}

The reason is that fractions of patterns are represented by Times with 
Power[#,-1]. Explicit integer fractions are represented by Rational. The 
same problem arises with Complex. It is impossible to transform 
explicitly complex expressions (expressions with I, Re, Im, Abs) without 
inspection of FullForm.


Roland Franzius

  • Prev by Date: NFourierTrigSeries and NFourierCoefficient
  • Next by Date: Re: Manipulate and ParametricPlot Control?
  • Previous by thread: Re: Replacement Rule with Sqrt in denominator
  • Next by thread: Re: Replacement Rule with Sqrt in denominator