25
$\begingroup$

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}$

$\endgroup$
5
  • $\begingroup$ I solved it for the three examples you gave. Other cases can be handled through additional replacement rules. $\endgroup$
    – DavidC
    May 8, 2012 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, 2012 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, 2012 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, 2012 at 3:30
  • $\begingroup$ I believe this has a solution (by David E Speyer) here $\endgroup$ Apr 10, 2018 at 9:32

7 Answers 7

16
$\begingroup$

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

EvaluiereAt[pos:(_Integer|{__Integer}),f_:Identity][expr_]:=
  ReplacePart[expr,pos->Extract[expr,pos,f]];
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]];
   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.

Examples:

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

Mathematica graphics

$\endgroup$
4
  • $\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, 2012 at 22:20
  • $\begingroup$ @rmv sorry I missed to copy this definition. I've added it to my answer. $\endgroup$ May 7, 2012 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, 2012 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$ Jun 9, 2012 at 12:13
11
$\begingroup$

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}$.

$\endgroup$
3
  • $\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, 2012 at 19:06
  • $\begingroup$ @DavidCarraher Nice, but its a different concept - I think it deserves to be posted as an answer, not comment. $\endgroup$ May 7, 2012 at 23:28
  • $\begingroup$ @VitaliyKaurov Ok, I posted it. $\endgroup$
    – DavidC
    May 7, 2012 at 23:56
9
$\begingroup$

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 = 
  ToExpression[
    ToBoxes[#] /. 
     x_SqrtBox :> 
      ToBoxes[
        Divide @@ HoldForm /@ {Numerator@#, Denominator@#} & @ ToExpression @ x
      ]
  ] &;
$\endgroup$
4
$\begingroup$

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

results

$\endgroup$
3
  • $\begingroup$ 1/(2*Sqrt[2]) /.rat1 doesn't work $\endgroup$ Nov 27, 2021 at 15:14
  • $\begingroup$ 1/(2*Sqrt[2]) /.rat2 puts the radical on top but doesn't give a simple fraction $\endgroup$ Nov 27, 2021 at 15:18
  • $\begingroup$ I'll examine this tomorrow and get back to you. $\endgroup$
    – DavidC
    Nov 28, 2021 at 23:13
3
$\begingroup$

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]]]

Usage

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

rootRational[7/(
 6 - 6 Sqrt[2] + 5 Sqrt[3] + 3 Sqrt[5] - 2 Sqrt[6] - 3 Sqrt[10] + 
  2 Sqrt[15] - Sqrt[30])]

$\endgroup$
3
$\begingroup$

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

Clear[MakeBoxes];
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]
]

Examples:

{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.

$\endgroup$
2
$\begingroup$

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

$\{\frac{2\sqrt{5}}{15},\frac{10}{11}+\frac{3\sqrt{5}}{11},1-\frac{\sqrt{7}}{7}\}$

$\endgroup$
1
  • $\begingroup$ Seems to fail with $\frac 1 {2\sqrt{2}}$ $\endgroup$ Nov 27, 2021 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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