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[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: In[2]:= FullSimplify[1/(5 - 2*Sqrt[3])] Out[2]= 1 ------------- 5 - 2 Sqrt[3] In[3]:= FullSimplify[1/(5 - 2*Sqrt[3]), ComplexityFunction -> rat] Out[3]= 1 -- (5 + 2 Sqrt[3]) 13 Moreover, we can now rationalize denominators in much more complex expressions: In[4]:= FullSimplify[1/(Sqrt[2] + Sqrt[3]), ComplexityFunction -> rat] Out[4]= 2 4 Root[1 - 10 #1 + #1 & , 3] In[5]:= ToRadicals[%] Out[5]= Sqrt[5 - 2 Sqrt[6]] 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[21]:= > 1/(2 + I) > Out[21]= > 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[1]:= > Sqrt[3] > ------- > 3 > Out[1]= > 1 > ------- > Sqrt[3] > > > In more complex expressions Mathematica does not do anything automatically, > but FullSimplify produces what at first appears to be contradictory > behaviour: > > In[2]:= > FullSimplify[1/(5 - Sqrt[3])] > Out[2]= > 1 > -- (5 + Sqrt[3]) > 22 > > In[3]:= > FullSimplify[1/(5 - 2*Sqrt[3])] > Out[3]= > 1 > ------------- > 5 - 2 Sqrt[3] > > > In[4]:= > FullSimplify[1/13*(5 + 2*Sqrt[3])] > Out[4]= > 1 > -- (5 + 2 Sqrt[3]) > 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[5]:= > Depth[1/(5 - Sqrt[3])] > Out[20]= > 5 > > In[5]:= > Depth[1/22*(5 + Sqrt[3])] > Out[21]= > 4 > > so the second one is chosen. > > On the other hand: > > In[6]:= > Depth[1/(5 - 2*Sqrt[3])] > Out[6]= > 5 > > In[7]:= > Depth[1/13*(5 + 2*Sqrt[3])] > Out[7]= > 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[8]:= > 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[9]:= > RationalizeDenominator[2/(5 - 2*Sqrt[3])^3] > Out[9]= > 610 + 348 Sqrt[3] > ----------------- > 2197 > > Of course this will not convert 1/Sqrt[2] to Sqrt[2]/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[10]:= > RationalizeDenominator[1/Sqrt[2]] > Out[10]= > Sqrt[2] > ------- > 2 > > but you must not forget that HoldForm is wrapped around the output. > > In[11]:= > ReleaseHold[%] > Out[11]= > 1 > ------- > Sqrt[2] > -- > 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]/2 >> >> instead of >> >> 1/Sqrt[2] >> >> 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]/2 > > instead of > > 1/Sqrt[2] > > 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]/2 > > instead of > > 1/Sqrt[2] > > 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 ? > >

**Cumulative distribution of Gauss**

**Rudimentary Spell Checker**

**Re: Re: Cumulative distribution of Gauss**

**RE: Re: Rationalizing the denominator (better solution!)**