# How can I rationalize the denominator of an expression?

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?

-
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
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
If the OP does really mean the kind of denominator rationalization discussed in purplemath.com/modules/radicals5.htm, then I would say: (1) rationalizing a denominator is much over-rated -- one of those things that school math teachers try to pound into students' heads and yet is sometimes the very thing that you do not want to do (e.g., when finding certain limits of quotients where both numerator and denominator tend to 0); and (2) a denominator-rationalized form may become more complex than the original quotient. – murray Aug 26 '12 at 15:55

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.

-
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
I find this answer great. Notice that adding ReleaseHold helps with 1 in denominator and does not convert expression back to the form with root in denominator. – Kuba Jul 25 '13 at 6:33

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},
If[NumberQ[v], v, If[FreeQ[v, Root],
With[{n = Numerator[v], d = Denominator[v]}, HoldForm[n/d]],
With[{
}, 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


-
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
It doesn't work on V9 with test[[1]] and test[[4]]. Maybe this Q&A needs tag with version specification? – Kuba Aug 21 '13 at 7:35
@Kuba, the question isn't version specific though - just the answers! Hopefully someone will come up with a method that works for all the test cases, with all versions. – Simon Woods Aug 21 '13 at 8:51
ClearAll[rationalize];
SetAttributes[rationalize, Listable];
rationalize[x_] := Block[{den, t, len},
den = Denominator[x];
If[NumberQ@den || LeafCount@x <= 5, Return@x];
len = Length[List @@ Simplify[den]];
t = List @@ den.# & /@ Take[Tuples[{1, -1}, len], 2^(len - 1)];
(Times @@ DeleteCases[t, den]*Numerator[x])/Simplify[Times @@ t] // Simplify
];

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


-
On V9 it fails with 1/Sqrt[2] // rationalize – Kuba Aug 21 '13 at 7:33