In Mathematica 7, if I input Sqrt[2/7], Mathematica outputs $\sqrt{\frac{2}{7}}$, but I want it to output $\frac{\sqrt{14}}{7}$ instead. How do I make Mathematica output values without radicals in the denominator by default? I already tried this solution without success:

rat[p_] := If[FreeQ[Denominator[p], Power[_, Rational[_, _]]], 0, 1]
FullSimplify[Sqrt[2/7], ComplexityFunction -> rat]

I guess I need a better ComplexityFunction, and some way to have Mathematica output the rationalized form by default, instead of having to explicitly use FullSimplify every time.

edit: It would be great if the solution also worked for more complex expressions. For example: $\frac{1}{\sqrt{2}}\rightarrow\frac{\sqrt{2}}{2}$ and $\frac{1}{5-2\sqrt{3}}\rightarrow\frac{5}{13}+\frac{2\sqrt{3}}{13}$

  • $\begingroup$ I solved it for the three examples you gave. Other cases can be handled through additional replacement rules. $\endgroup$ – DavidC May 8 '12 at 1:27
  • $\begingroup$ An obvious question is why you want to rationalize the denominator? Yes, I know we were taught in school to do that, but it's not always the best thing. Even in calculus when, e.g, you calculate certain limits, the method involves rationalizing a numerator thereby unrationalizing ("irrationalizing"?) the numerator. $\endgroup$ – murray May 8 '12 at 3:50
  • $\begingroup$ @murray: I'm studying circuit analysis by solving practice problems, and the reference answers are in this form. $\endgroup$ – rmv May 9 '12 at 0:24
  • $\begingroup$ @mv: you can always apply Simplify to your answer (and, if necessary, to the reference answers) to see if they're the same -- without having to rationalize the denominators. $\endgroup$ – murray May 9 '12 at 3:30
  • $\begingroup$ I believe this has a solution (by David E Speyer) here $\endgroup$ – მამუკა ჯიბლაძე Apr 10 '18 at 9:32

In the old days, when "making the Numerator rational" was often wanted, I came up with the following set of rules:

EvaluiereAt[pos:{{__Integer}..},f_:Identity][expr_] :=
  Fold[ReplacePart[#1, #2 -> Extract[#1, #2, f]] &, expr, Reverse[Sort[pos]]];

$pinkHoldColor = ColorData["HTML"]["HotPink"];
pinkHold[x_] := Style[Tooltip[HoldForm[x], "held"], $pinkHoldColor];

Attributes[rootRational] = {Listable};
rootRational[expr_] := 
  Module[{zw, res, pos}, zw = expr /. Sqrt[a_] :> Sqrt[Together[a]];
   res = zw /. Sqrt[a_/b_] :> Sqrt[Expand[a b]]/b;
   res = res /. {a_./(b_ + d_. Sqrt[c_]) -> (a (b - d Sqrt[c]))/(b^2 -
           d^2 c), 
      a_./(b_ - d_. Sqrt[c_]) -> (a (b + d Sqrt[c]))/(b^2 - d^2 c)};
   res = res /. Sqrt[Rational[a_, b_]] :> pinkHold[Sqrt[a b]]/b;
   res = res /. (a_/Sqrt[b_]) :> a pinkHold[Sqrt[b]]/b;
   res = res /. 
     b_. Power[a_, Rational[-1, 2]] :> b pinkHold[Sqrt[a]]/a;
   pos = Position[res, _?NumberQ];
   If[Flatten[pos] =!= {}, res = EvaluiereAt[pos][res]];

Attributes[pinkUnhold] = {Listable};
pinkUnhold[expr_] := 
  ReleaseHold[expr /. Style[Tooltip[a_, __], __] -> a];

the function rootRational tries to achieve this. To show, that something is in HoldForm, I marked it with a pink color. To ReleaseHold and take away the color an tooltip there is the function pinkUnhold.


w = Sqrt[6]/9 
% // rootRational 
% // pinkUnhold Clear[a]; 
w = Sqrt[(1 + a)/(1 - a)] // rootRational 
% // FullSimplify 

Mathematica graphics

|improve this answer|||||
  • $\begingroup$ Could you add the definition of EvaluiereAt to your answer? After finding it here, this solution worked best so far. $\endgroup$ – rmv May 7 '12 at 22:20
  • $\begingroup$ @rmv sorry I missed to copy this definition. I've added it to my answer. $\endgroup$ – Peter Breitfeld May 7 '12 at 22:36
  • $\begingroup$ Nice feature. I would like to point out that w = Sqrt[(1 + a)^3/(1 - a)/(1 - b)] // rootRational fails in some sense. w = Sqrt[(1 + a)^3/(1 - a)/(1 - b)] // PowerExpand//rootRational gives the expected answer if a<1 and b<1 $\endgroup$ – chris Jun 9 '12 at 11:58
  • $\begingroup$ If you look into the code, you'll see, that the rules are made for squareroots. I made this for teaching purposes, not to be an ever working solution. $\endgroup$ – Peter Breitfeld Jun 9 '12 at 12:13

I guess the normal evaluation-process will always convert this back to $\sqrt{2/7}$ unless you hold the form explicitly. Converting your expression into the desired form can be done with Numerator and Denominator which luckily give the desired values of $\sqrt{14}$ and $7$.

Divide @@ (HoldForm /@ {Numerator[#], Denominator[#]} &[Sqrt[2/7]])

In the moment you release the HoldForm the expression gets evaluated back to $\sqrt{2/7}$.

|improve this answer|||||
  • $\begingroup$ Perhaps this is clearer: rat = {Power[Rational[n_, d_], Rational[1, 2]] :> Sqrt[n d]/HoldForm[d]}; Sqrt[2/7] /. rat $\endgroup$ – DavidC May 7 '12 at 19:06
  • $\begingroup$ @DavidCarraher Nice, but its a different concept - I think it deserves to be posted as an answer, not comment. $\endgroup$ – Vitaliy Kaurov May 7 '12 at 23:28
  • $\begingroup$ @VitaliyKaurov Ok, I posted it. $\endgroup$ – DavidC May 7 '12 at 23:56

If you want to specifically handle expressions that are visually displayed as radicals, the most robust method I know is to manipulate the Box structure itself. Here is one way to do that using the method proposed by halirutan.

$PrePrint = 
    ToBoxes[#] /. 
     x_SqrtBox :> 
        Divide @@ HoldForm /@ {Numerator@#, Denominator@#} & @ ToExpression @ x
  ] &;
|improve this answer|||||

Below are two ways to approach the problem. You could use a replacement rule with HoldForm:

rat1 = {Sqrt[Rational[n_, d_]] :>   Sqrt[n d]/HoldForm[d], 
        1/Sqrt[x_] :> Sqrt[x]/HoldForm[x], 
        1/(a_ + (b_ Sqrt[c_]) ) :> a/(a^2 - b^2 c) - (b Sqrt[c])/(a^2 - b^2 c)};

Or you could use FractionBox:

rat2 = {Sqrt[Rational[n_, d_]] :> DisplayForm@FractionBox[Sqrt[n d], d], 
        1/Sqrt[x_] :> DisplayForm@FractionBox[Sqrt[x], x], 
        1/(a_ + (b_ Sqrt[c_]) ) :> a/(a^2 - b^2 c) - (b Sqrt[c])/(a^2 - b^2 c)};

(The third rule for rat1 and rat2 is the same, and requires neither HoldForm nor FractionBox.)

Testing and showing output below:

{Sqrt[2/7], 1/Sqrt[2], 1/(5 - 2 Sqrt[3])} /. rat1
{Sqrt[2/7], 1/Sqrt[2], 1/(5 - 2 Sqrt[3])} /. rat2


|improve this answer|||||

I found this function by Andrzej Kozlowski in the MathGroup Archive:

f1[expr_] :=
  FullSimplify[expr, ComplexityFunction ->
      Count[#, _?
        (MatchQ[Denominator[#], Power[_, _Rational] _. + _.] &),
        {0, Infinity}
      ] + If[FreeQ[#, Root], 0, 1] &

A combination of this, Expand, and Peter Breitfeld's solution seems to work best. E.g.:

test $=\{\frac{2}{3\sqrt{5}},\frac{1}{2-\frac{3}{\sqrt5{}}},\frac{1}{2-\frac{3}{5+\sqrt{7}}}\}$

test // f1 // Expand // rootRational


|improve this answer|||||

Based on MinimalPolynomial for more complicated case:

rootRational[fraction_] := 
 Module[{numer = Numerator[fraction], denon = Denominator[fraction], 
   poly, x}, poly = MinimalPolynomial[denon, x]; 
  FullSimplify[Times[numer, Rest[poly]/-x/First[poly] /. x -> denon]]]


rootRational[1/(Sqrt[2] + Sqrt[3] + Sqrt[5])]

 6 - 6 Sqrt[2] + 5 Sqrt[3] + 3 Sqrt[5] - 2 Sqrt[6] - 3 Sqrt[10] + 
  2 Sqrt[15] - Sqrt[30])]

|improve this answer|||||

For displaying algebraics where the denominator is a simple radical, you can override the formatting of Times/Power:

MakeBoxes[a_. Power[b_, r_Rational?Negative], fmt_] := With[
    {p = Mod[r, 1], pow = Quotient[r, 1]},
    MakeBoxes[Times[a, b^pow, Defer[Power[b,p]]], fmt]


{1/Sqrt[2], 5/Power[3, (3)^-1], 1/(2+3/Sqrt[5])} //TeXForm

$\left\{\frac{\sqrt{2}}{2},\frac{5\ 3^{2/3}}{3},\frac{1}{2+\frac{3 \sqrt{5}}{5}}\right\}$

I didn't bother extending this to your more complicated examples, because having:

1/(5 - 2 Sqrt[3])

automatically display as:

5/13 + (2 Sqrt[3])/13

would be very confusing (what should First return?) Instead, I think it makes much more sense to have a function that converts the expression into a rationalized form, e.g., see question 9868.

|improve this answer|||||

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.