# Rationalize the Denominator by Default

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

-
I solved it for the three examples you gave. Other cases can be handled through additional replacement rules. – DavidC May 8 '12 at 1:27
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. – murray May 8 '12 at 3:50
@murray: I'm studying circuit analysis by solving practice problems, and the reference answers are in this form. – rmv May 9 '12 at 0:24
@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. – murray May 9 '12 at 3:30

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


-
Could you add the definition of EvaluiereAt to your answer? After finding it here, this solution worked best so far. – rmv May 7 '12 at 22:20
@rmv sorry I missed to copy this definition. I've added it to my answer. – Peter Breitfeld May 7 '12 at 22:36
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 – chris Jun 9 '12 at 11:58
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. – 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}$.

-
Perhaps this is clearer: rat = {Power[Rational[n_, d_], Rational[1, 2]] :> Sqrt[n d]/HoldForm[d]}; Sqrt[2/7] /. rat – DavidC May 7 '12 at 19:06
@DavidCarraher Nice, but its a different concept - I think it deserves to be posted as an answer, not comment. – Vitaliy Kaurov May 7 '12 at 23:28
@VitaliyKaurov Ok, I posted it. – 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 = ToExpression[ ToBoxes[#] /. x_SqrtBox :> ToBoxes[ Divide @@ HoldForm /@ {Numerator@#, Denominator@#} & @ ToExpression @ x ] ] &;  - 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  - 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}\}\$

-