RE: Re: Rationalizing the denominator (better solution!)
- To: mathgroup at smc.vnet.net
- Subject: [mg18783] RE: [mg18697] Re: [mg18633] Rationalizing the denominator (better solution!)
- From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
- Date: Tue, 20 Jul 1999 01:33:36 -0400
- Sender: owner-wri-mathgroup at wolfram.com
Andrzej Kozlowski wrote: ---------------------------- A short time after sending my reply to this message I noticed that a far better way to rationalize the denominator of most expressions is already almost built in into Mathematica! What one has to do is to make use of the ComplexityFunction option in FullSimplify, which enables you to decide which of two altrnative forms of an expression Mathematica considers to be simpler. First we defince a complexity function which makes expressions with radicals in the denominator more complex than those without: In[1]:= rat[p_] := If[FreeQ[Denominator[p], Power[_, Rational[_, _]]], 0, 1] Now we can use FullSimplify with ComplexityFunction set to rat. We can now rationalize denominators in expressions which we could not deal with before: <snip> In[3]:= FullSimplify[1/(5 - 2*Sqrt[3]), ComplexityFunction -> rat] Out[3]= 1 -- (5 + 2 Sqrt[3]) 13 ---------------------------- The solution from Andrzej Kozlowski doesn't rationalize denominators deep down inside an expression. I wrote RationalizeDenominator below which does. In[1]:= RationalizeDenominator[expr_]:= FullSimplify[expr,ComplexityFunction-> ( Count[#,_? (MatchQ[Denominator[#],Power[_,_Rational] _.+_.]&), {0,Infinity} ]& ) ]/.x_Root? (LeafCount[ToRadicals[#]]<=LeafCount[#]&):>ToRadicals[x] --------------------- In[2]:= tst=2*(E^(Sqrt[2] +Sqrt[3])^(-1) + x)^2; In[3]:= RationalizeDenominator[tst] Out[3]= 2*(E^Sqrt[5 - 2*Sqrt[6]] + x)^2 In the example above a denominator deep inside the expression is rationalized. This version also converts Root objects to radicals when it makes sense to do so. ---------------------------------- Of course my function doesn't rationalize the denominator in the following example. To do that the expression returned would need the head (HoldForm). In[4]:= RationalizeDenominator[1/Sqrt[8]] Out[4]= 1/(2*Sqrt[2]) ---------------------------------- I think WRI could make a built-in function that would do this much faster, and I hope they give us one in a future release. Regards, Ted Ersek