       Re: Rationalizing the denominator (better solution!)

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

```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:

In:=
FullSimplify[1/(5 - 2*Sqrt)]
Out=
1
-------------
5 - 2 Sqrt

In:=
FullSimplify[1/(5 - 2*Sqrt), ComplexityFunction -> rat]
Out=
1
-- (5 + 2 Sqrt)
13

Moreover, we can now rationalize denominators in much more complex
expressions:

In:=
FullSimplify[1/(Sqrt + Sqrt), ComplexityFunction -> rat]
Out=
2     4
Root[1 - 10 #1  + #1  & , 3]

In:=
Out=
Sqrt[5 - 2 Sqrt]

After all this time Mathematica's power still keeps surprising me!

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

> The comparison between complex numbers and irationals isn't really valid.
> Complex numbers really do have a canonical representation in the form  a+b*I
> so it is natural for Mathematica to automatically reduce them to this form
> as in:
>
> In:=
> 1/(2 + I)
> Out=
> 2   I
> - - -
> 5   5
>
> The fact that we can always do this follows from the very basic fact that
> the commplex numbers are a field.
>
> On the other hand rationalizing the denominator is less basic and the fact
> that you can always do this depends on the Euclidean property of polynomial
> rings: a much less obvious fact. Moreover, although rationalizing the
> denominator is often a useful thing to do it is not necessarily what you
> would always like to happen. So it is reasonable that Mathematia does not do
> this automatically.
>
> Actually Mathematica's behaviour in this respect may seem strange and
> erratic. For a start, Mathematica will always de-rationalize denominators in
> expresions like :
>
> In:=
> Sqrt
> -------
>    3
> Out=
>    1
> -------
> Sqrt
>
>
> In more complex expressions Mathematica does not do anything automatically,
> but FullSimplify produces what at first appears to be contradictory
> behaviour:
>
> In:=
> FullSimplify[1/(5 - Sqrt)]
> Out=
> 1
> -- (5 + Sqrt)
> 22
>
> In:=
> FullSimplify[1/(5 - 2*Sqrt)]
> Out=
>       1
> -------------
> 5 - 2 Sqrt
>
>
> In:=
> FullSimplify[1/13*(5 + 2*Sqrt)]
> Out=
> 1
> -- (5 + 2 Sqrt)
> 13
>
>
> This seemingly erratic behaviour is explained by the fact that Mathematica's
> notion of simplicity is based on its internal representation of expressions.
> It seems to me (I am not sure of this but this agrees with all the examples)
> that mathematica choses as the simplest the representation which has the
> least Depth. For example:
>
> In:=
> Depth[1/(5 - Sqrt)]
> Out=
> 5
>
> In:=
> Depth[1/22*(5 + Sqrt)]
> Out=
> 4
>
> so the second one is chosen.
>
> On the other hand:
>
> In:=
> Depth[1/(5 - 2*Sqrt)]
> Out=
> 5
>
> In:=
> Depth[1/13*(5 + 2*Sqrt)]
> Out=
> 5
>
> So neither is considered simpler and FullSimplify makes no change.
>
> Finally, I do think it would be useful to have a RationalizeDenominator
> function that would perform this for any expression involving radicals.
> Sometime ago, when I needed this to perform some computation,  I wrote a
> simple special case, which only deals with  simple expressions involving
> square roots:
>
> In:=
> RationalizeDenominator[(p_:1)*Power[a_ + (b_:1)*Sqrt[v_], m_?Negative]] :=
>  Expand[p*(a - b*Sqrt[v])^(-m)]/Expand[(a - b*Sqrt[v])*(a + b*Sqrt[v])]^(-m)
>
>
> It will manage simple cases like:
>
> In:=
> RationalizeDenominator[2/(5 - 2*Sqrt)^3]
> Out=
> 610 + 348 Sqrt
> -----------------
>       2197
>
> Of course this will not convert 1/Sqrt to Sqrt/2. To do that you must
> wrap the final output in HoldForm to prevent mathematica immediately putting
> it back into what it considers the simpler form:
>
> RationalizeDenominator[(a_:1)*Power[v_, Rational[-1, 2]]] :=
>  a/v*HoldForm[Sqrt[v]]
>
> Now you indeed get
>
> In:=
> RationalizeDenominator[1/Sqrt]
> Out=
> Sqrt
> -------
>    2
>
> but you must not forget that HoldForm is wrapped around the output.
>
> In:=
> ReleaseHold[%]
> Out=
>    1
> -------
> Sqrt
> --
> Andrzej Kozlowski
> Toyama International University
> JAPAN
> http://sigma.tuins.ac.jp
> http://eri2.tuins.ac.jp
>
>
> ----------
>>From: "Drago Ganic" <drago.ganic at in2.hr>
To: mathgroup at smc.vnet.net
>>To: mathgroup at smc.vnet.net
>>Subject: [mg18697] [mg18633] Rationalizing the denominator
>>Date: Tue, Jul 13, 1999, 2:01 PM
>>
>
>> How can I get
>>
>>     Sqrt/2
>>
>>
>>     1/Sqrt
>>
>> as a result for Sin[Pi/4].
>>
>> When it comes to complex numbers Mathematica never returns 1/I - she always
>> returns -I.
>> Why is the behaviour for irrationals different ?
>>
>>

----------
>From: "Drago Ganic" <drago.ganic at in2.hr>
To: mathgroup at smc.vnet.net
>To: mathgroup at smc.vnet.net
>Subject: [mg18697] [mg18633] Rationalizing the denominator
>Date: Tue, Jul 13, 1999, 2:01 PM
>

> How can I get
>
>     Sqrt/2
>
>
>     1/Sqrt
>
> as a result for Sin[Pi/4].
>
> When it comes to complex numbers Mathematica never returns 1/I - she always
> returns -I.
> Why is the behaviour for irrationals different ?
>
>

----------
>From: "Drago Ganic" <drago.ganic at in2.hr>
To: mathgroup at smc.vnet.net
>To: mathgroup at smc.vnet.net
>Subject: [mg18697] [mg18633] Rationalizing the denominator
>Date: Tue, Jul 13, 1999, 2:01 PM
>

> How can I get
>
>     Sqrt/2
>
>
>     1/Sqrt
>
> as a result for Sin[Pi/4].
>
> When it comes to complex numbers Mathematica never returns 1/I - she always
> returns -I.
> Why is the behaviour for irrationals different ?
>
>

```

• Prev by Date: Cumulative distribution of Gauss
• Next by Date: Rudimentary Spell Checker
• Previous by thread: Re: Re: Cumulative distribution of Gauss
• Next by thread: RE: Re: Rationalizing the denominator (better solution!)