# 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+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+Sqrt)] to output (-Sqrt+Sqrt)/3, rather than (-3 Sqrt+3 Sqrt)/9. The HoldForm[] is to get RatDenom[1/Sqrt] to be Sqrt/2, not 1/Sqrt.

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

(* A straight forward example *)

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

3 Sqrt + 2 Sqrt - Sqrt
Out= --------------------------------
12

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

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

1 + Sqrt
Out= ------------
5
(* Nested radicals are fine *)

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

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

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

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

1 + Sqrt
Sqrt[-----------]
1 + Sqrt
Out= -----------------
1

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

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

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

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

In:= N[%]

1.
Out= --
1.

• FullSimplify[ToRadicals[RootReduce[stuff]]] would work on Sqrt[3 + 2 Sqrt] - Sqrt[2 (3 + 2 Sqrt)], 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. will be back soon 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 + Sqrt + Sqrt), (3 + Sqrt)/(4 + Sqrt),
(2 + Sqrt)/(1 + Sqrt[5 + Sqrt]), Sqrt[(1 + Sqrt)/(1 + Sqrt)],
Sqrt[3 + 2 Sqrt]/(1 + Sqrt)};

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 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, but that's with a different version of Mathematica. – murray Sep 19 '12 at 22:18
• It doesn't work on V9 with test[] and test[]. 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)/(4 + Sqrt), 1/(Sqrt + Sqrt + Sqrt),
(2 + Sqrt)/(1 + Sqrt[5 + Sqrt])} // rationalize // rationalize • On V9 it fails with 1/Sqrt // rationalize – Kuba Aug 21 '13 at 7:33