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.