       Re(2): Re: Rationalizing the denominator (better solution!)

• To: mathgroup at smc.vnet.net
• Subject: [mg18809] Re(2): [mg18697] Re: [mg18633] Rationalizing the denominator (better solution!)
• From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
• Date: Thu, 22 Jul 1999 08:19:27 -0400
• Sender: owner-wri-mathgroup at wolfram.com

```This is still somwhat imperfect. For example:

In:=
RationalizeDenominator[4/(3*(Sqrt - 5^(1/4)))]
Out=
2            4             6
Root[65536 - 294912 #1  + 290304 #1  - 1772928 #1  +

8
6561 #1  & , 1]

In:=
Out=
608   272 Sqrt   1      327680   1318912 Sqrt
-Sqrt[--- + ----------- + - Sqrt[------ + ---------------]]
9         9        2        9            81

which is not quite what one would like to get. One can do a little better
by modifying the complexity function to penalize the presence of Root:

In:=
RationalizeDenominator1[expr_] :=

FullSimplify[expr, ComplexityFunction ->
(
Count[#, _?
(MatchQ[Denominator[#], Power[_, _Rational] _. + _.] &),
{0, Infinity}
] + If[FreeQ[#, Root], 0, 1] &
)
]

In:=
RationalizeDenominator1[4/(3*(Sqrt - 5^(1/4)))]
Out=
4
-(-) Sqrt[38 + 17 Sqrt + 2 Sqrt[2 (360 + 161 Sqrt)]]
3

All these functions will fail however if roots of order higher than 4 are
involved:

In:=
RationalizeDenominator1[f = (2 + 5^(1/6))/(1 - 5^(1/6))]
Out=
1
-1 + --------
1/6
1   5
- - ----
3    3
In such cases one needs to use some mathematics. First we load in the package

In:=
<< Algebra`PolynomialExtendedGCD`

we set

In:=
p = Denominator[f] /. {5^(1/6) -> t}
Out=
1 - t
In:=
q = RootReduce[5^(1/6)][][t]
Out=
6
-5 + t

The answer then is given by:

In:=
Numerator[f]*PolynomialExtendedGCD[p, q][[2, 1]] /. t -> 5^(1/6) // Expand
Out=
1/6      1/3                  2/3      5/6
7    3 5      3 5      3 Sqrt   3 5      3 5
-(-) - ------ - ------ - --------- - ------ - ------
4      4        4          4         4        4

We can try to see if Mathematica can verify this by itself. Not
surprisingly it is forced to use numerical methods:

In:=
FullSimplify[
f == -(7/4) - (3*5^(1/6))/4 - (3*5^(1/3))/4 - (3*Sqrt)/4 - (3*5^(2/3))/
4 - (3*5^(5/6))/4]

\!\(\$MaxExtraPrecision::"meprec" \(\(:\)\(\ \)\)
"In increasing internal precision while attempting to evaluate
\!\(7\/4 + \
\(3\\ 5\^\(1/6\)\)\/4 + \(3\\ 5\^\(1/3\)\)\/4 + \(3 \(\(\[LeftSkeleton] 1 \
\[RightSkeleton]\)\) \(\(\[LeftSkeleton] 1 \[RightSkeleton]\)\)\)\/4 + \(3\\ \
5\^\(\[LeftSkeleton] 1 \[RightSkeleton]\)\)\/4 + \(3\\ 5\^\(5/6\)\)\/4 + \(2 \
+ 5\^\(1/6\)\)\/\(1 - 5\^\(1/6\)\)\), the limit \$MaxExtraPrecision = \!\(50.`\
\) was reached. Increasing the value of \$MaxExtraPrecision may help resolve \
the uncertainty."\)

Out=
True

On Mon, Jul 19, 1999, Ersek, Ted R <ErsekTR at navair.navy.mil> wrote:

>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:=
>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:=
>FullSimplify[1/(5 - 2*Sqrt), ComplexityFunction -> rat]
>Out=
>1
>-- (5 + 2 Sqrt)
>13
>
>----------------------------
>
>The solution from Andrzej Kozlowski doesn't rationalize denominators deep
>down inside an expression.  I wrote RationalizeDenominator below which does.
>
>
>In:=
>RationalizeDenominator[expr_]:=
>  FullSimplify[expr,ComplexityFunction->
>    (
>      Count[#,_?
>        (MatchQ[Denominator[#],Power[_,_Rational] _.+_.]&),
>        {0,Infinity}
>      ]&
>    )
>  ]/.x_Root?
>
>
>---------------------
>
>In:=
>tst=2*(E^(Sqrt +Sqrt)^(-1) + x)^2;
>
>
>In:=
>RationalizeDenominator[tst]
>Out=
>2*(E^Sqrt[5 - 2*Sqrt] + 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:=
>RationalizeDenominator[1/Sqrt]
>
>Out=
>1/(2*Sqrt)
>
>----------------------------------
>
>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
>

Andrzej Kozlowski
Toyama International University
JAPAN
http://sigma.tuins.ac.jp/
http://eri2.tuins.ac.jp/

```

• Prev by Date: Re: ValueBoxes
• Next by Date: Re: ARghh!!
• Previous by thread: lobster modeling
• Next by thread: Re: Is there a FAQ? (Different question -- why the pause