Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Mathematica doesn't rationalize the denominator automatically, and I haven't found anything in the documentation about it. But I found an old post on MathGroup, which proposes a solution using ComplexityFunction.

In very simple cases, it works fine, but it fails in more complex cases. Is there a better way to do it?

share|improve this question
1  
It is always better to give a Mathematica example of what you mean. Give the input, and say what you expect the output to be. Similar to a picture is worth 1,000 words. – Nasser Aug 26 '12 at 9:10
Sorry,I want to give a example,but I am a new user to this site,posting pictures is not allowed. – bandaoti Aug 26 '12 at 9:23
1  
I think this is what the OP means – P. Fonseca Aug 26 '12 at 9:32
@bandaoti - You can always upload the image to an upload server and tell us the URI. We'll paste it in the question for you. Alternatively, if you use $\LaTeX$ code you may not need pictures. – stevenvh Aug 26 '12 at 10:56
You say but failed in more complex cases can you at least give an example where the solution shown at mathgroup failed? i.e. the complex case you mentioned. – Nasser Aug 26 '12 at 11:06
show 1 more comment

2 Answers

RatDenom[x_]:=
  Module[{y,nn,dd,f,g,c,k,blah},
    (y=Together[x];
     nn=Numerator[y];
     dd=Denominator[y];
     f=MinimalPolynomial[dd,t];
     c=f /. t -> 0;
     g=Factor[(c-f)/t];
     {k, blah}=FactorTermsList[Expand[nn*(g /. t -> dd)]];
     Sign[c] ((k/GCD[k,c])*blah)/HoldForm[Evaluate@Abs[c/GCD[k,c]]])]

Write $x=\nu/\delta$. The point is that $f(\delta)=0$, so $g(\delta)=c/\delta$. Expand[]ing $g(\delta)$ produces an expression without denominators. We call Together[] before doing anything else so that RatDenom[1+1/(Sqrt[2]+1)] will work.

There are two formatting hacks, both of which were suggested by J.M. in comments on my deleted answer: The point of the FactorTermsList[] is to get RatDenom[1/(Sqrt[2]+Sqrt[5])] to output (-Sqrt[2]+Sqrt[5])/3, rather than (-3 Sqrt[2]+3 Sqrt[5])/9. The HoldForm[] is to get RatDenom[1/Sqrt[2]] to be Sqrt[2]/2, not 1/Sqrt[2].

The following output shows the strengths and limitations of this method:

(* A straight forward example *)

In[58]:= RatDenom[1/(Sqrt[2]+Sqrt[3]+Sqrt[5])]

         3 Sqrt[2] + 2 Sqrt[3] - Sqrt[30]
Out[58]= --------------------------------
                        12

(* Evaluate[] knows how to multiply expressions with Sqrt[11] *)

In[59]:= RatDenom[(3+Sqrt[11])/(4+Sqrt[11])]

         1 + Sqrt[11]
Out[59]= ------------
              5
(* Nested radicals are fine *)

In[60]:= RatDenom[(2+Sqrt[3])/(1+Sqrt[5+Sqrt[11]])]

Out[60]= (-8 - 4 Sqrt[3] + 2 Sqrt[11] + Sqrt[33] + 8 Sqrt[5 + Sqrt[11]] + 4 Sqrt[3 (5 + Sqrt[11])] - 2 Sqrt[11 (5 + Sqrt[11])] -  Sqrt[33 (5 + Sqrt[11])]) / 5

(* The outermost operation after Together[] must be division. *)

In[65]:= RatDenom[Sqrt[(1+Sqrt[2])/(1+Sqrt[3])]]

              1 + Sqrt[2]
         Sqrt[-----------]
              1 + Sqrt[3]
Out[65]= -----------------
                 1

(* Expand doesn't realize that this numerator equals 1 .*)

In[67]:= RatDenom[Sqrt[3+2 Sqrt[2]]/(1+Sqrt[2])]

           Sqrt[3 + 2 Sqrt[2]] - Sqrt[2 (3 + 2 Sqrt[2])]
Out[67]= -(---------------------------------------------)
                                 1

(* As we can confirm by using N[]. *)

In[68]:= N[%]

         1.
Out[68]= --
         1.
share|improve this answer
FullSimplify[ToRadicals[RootReduce[stuff]]] would work on Sqrt[3 + 2 Sqrt[2]] - Sqrt[2 (3 + 2 Sqrt[2])], but it can sometimes be a crapshoot; I seem to remember some examples where it would rewrite a sum of different radicals as nested radicals instead, but I can't seem to find those examples right now... – J. M. Aug 26 '12 at 19:28

The following works on all the examples in David's answer. It uses the code provided by J.M. in the comments. The transformation is first tried on the whole expression, and if that fails it is applied separately to the numerator and denominator.

ratd[x_] := Module[{v},
  v = FullSimplify@ToRadicals@RootReduce@x;
  If[NumberQ[v], v, If[FreeQ[v, Root],
    With[{n = Numerator[v], d = Denominator[v]}, HoldForm[n/d]],
    With[{
      n = FullSimplify@ToRadicals@RootReduce@Numerator@x,
      d = FullSimplify@ToRadicals@RootReduce[1/Denominator@x]
      }, HoldForm[n d]]]]];

test = {1/(Sqrt[2] + Sqrt[3] + Sqrt[5]), (3 + Sqrt[11])/(4 + Sqrt[11]), 
  (2 + Sqrt[3])/(1 + Sqrt[5 + Sqrt[11]]), Sqrt[(1 + Sqrt[2])/(1 + Sqrt[3])], 
  Sqrt[3 + 2 Sqrt[2]]/(1 + Sqrt[2])};

ratd/@test

enter image description here

share|improve this answer
ratd fails to do the expected denominator rationalization in simple cases where RatDenom succeeds, e.g., with Sqrt[25/3]. – murray Sep 19 '12 at 3:32
@murray, I'm getting ratd@Sqrt[25/3] = 5 Sqrt[3]/3 as expected (the same result as RatDenom). That's with Mathematica version 8.0.4. What are you getting? – Simon Woods Sep 19 '12 at 19:01
I'm getting 5/Sqrt[3], but that's with a different version of Mathematica. – murray Sep 19 '12 at 22:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.