15
$\begingroup$

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?

$\endgroup$
4
  • 1
    $\begingroup$ Sorry,I want to give a example,but I am a new user to this site,posting pictures is not allowed. $\endgroup$
    – bandaoti
    Aug 26, 2012 at 9:23
  • 1
    $\begingroup$ I think this is what the OP means $\endgroup$
    – P. Fonseca
    Aug 26, 2012 at 9:32
  • $\begingroup$ @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. $\endgroup$
    – stevenvh
    Aug 26, 2012 at 10:56
  • 1
    $\begingroup$ 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. $\endgroup$
    – murray
    Aug 26, 2012 at 15:55

3 Answers 3

16
$\begingroup$
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.
$\endgroup$
2
  • 1
    $\begingroup$ 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... $\endgroup$ Aug 26, 2012 at 19:28
  • $\begingroup$ 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. $\endgroup$
    – Kuba
    Jul 25, 2013 at 6:33
7
$\begingroup$

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

$\endgroup$
6
  • $\begingroup$ ratd fails to do the expected denominator rationalization in simple cases where RatDenom succeeds, e.g., with Sqrt[25/3]. $\endgroup$
    – murray
    Sep 19, 2012 at 3:32
  • $\begingroup$ @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? $\endgroup$ Sep 19, 2012 at 19:01
  • $\begingroup$ I'm getting 5/Sqrt[3], but that's with a different version of Mathematica. $\endgroup$
    – murray
    Sep 19, 2012 at 22:18
  • $\begingroup$ It doesn't work on V9 with test[[1]] and test[[4]]. Maybe this Q&A needs tag with version specification? $\endgroup$
    – Kuba
    Aug 21, 2013 at 7:35
  • $\begingroup$ @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. $\endgroup$ Aug 21, 2013 at 8:51
3
$\begingroup$
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

enter image description here

$\endgroup$
1
  • $\begingroup$ On V9 it fails with 1/Sqrt[2] // rationalize $\endgroup$
    – Kuba
    Aug 21, 2013 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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