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

• I solved it for the three examples you gave. Other cases can be handled through additional replacement rules. May 8, 2012 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. May 8, 2012 at 3:50
• @murray: I'm studying circuit analysis by solving practice problems, and the reference answers are in this form.
– rmv
May 9, 2012 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. May 9, 2012 at 3:30
• I believe this has a solution (by David E Speyer) here Apr 10, 2018 at 9:32

## 7 Answers

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, 2012 at 22:20
• @rmv sorry I missed to copy this definition. I've added it to my answer. May 7, 2012 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 Jun 9, 2012 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. Jun 9, 2012 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 May 7, 2012 at 19:06
• @DavidCarraher Nice, but its a different concept - I think it deserves to be posted as an answer, not comment. May 7, 2012 at 23:28
• @VitaliyKaurov Ok, I posted it. May 7, 2012 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.