# How do I simplify expressions with square roots?

I want

$\left\{\frac{1}{1+\sqrt{2}-\sqrt{3}},\sqrt{5-2 \sqrt{6}},\sqrt{4+\sqrt{15}}\right\}$

to be simplified to

$\left\{\frac{1}{4} \left(2+\sqrt{2}+\sqrt{6}\right), \sqrt{3}-\sqrt{2}, \frac{1}{2} \left(\sqrt{6}+\sqrt{10}\right)\right\}$

But I haven't been able to get that result with Mathematica. How could I get it?

• You get an immediate answer with FullSimplify[1/(1 + Sqrt - Sqrt)] which doesn't coincide with yours. Is this acceptable ? Dec 7, 2012 at 13:38
• Er.I wouldn't like nested radical. Dec 7, 2012 at 13:51
• By what measure is the first expression in your output simpler than the first expression in your input? It does not include a nested radical as do the other two. Dec 7, 2012 at 15:14

There is a literature on denesting radicals. Not really my strength. In cases where you know (or suspect) the specific radicals that should appear in the result you can use a Groebner basis computation to recast your algebraic value via a minimal polynomial. Then factor over the extension defined by those radicals, solve, and pick the solution that is numerically correct (the others being algebraic conjugates). I illustrate with that first example.

rootpoly =
GroebnerBasis[{x*xrecip - 1, xrecip - (1 + y - z), y^2 - 2, z^2 - 3},
x, {xrecip, y, z}][]

(* -1 + 4*x + 4*x^2 - 16*x^3 + 8*x^4 *)

fax =
Select[FactorList[rootpoly, Extension -> {Sqrt, Sqrt}][[All,
1]], ! FreeQ[#, x] &]

(* {2 + Sqrt + Sqrt - 4*x, -2 - Sqrt + Sqrt + 4*x,
2 - Sqrt + Sqrt - 4*x, -2 + Sqrt + Sqrt + 4*x} *)

candidates = Flatten[Map[x /. Solve[# == 0, x] &, fax]];
First[Select[candidates, (N[#] == 1/(1 + Sqrt - Sqrt)) &]]

(* (1/4)*(2 + Sqrt + Sqrt) *)


If you are more familiar with manipulating algebraic numbers than you are with Groebner bases, here is a better way to get that defining polynomial.

RootReduce[1/(1 + Sqrt - Sqrt)][][x]

(* Out= -1 + 4 x + 4 x^2 - 16 x^3 + 8 x^4 *)


--- edit ---

I will show this in a way that is more automated, in terms of deciding what to use in the extension for factoring. The idea is to allow roots of all factors of all integers that appear in the nested radical.

val = Sqrt[4 + Sqrt];
rootpoly = RootReduce[val][][x]

(* 1 - 8 x^2 + x^4 *)

ints =
Flatten[Map[FactorInteger, Cases[val, _Integer, -1]][[All, All, 1]]]

(* {2, 3, 5} *)

fax =
Select[FactorList[rootpoly, Extension -> Sqrt[ints]][[All, 1]], !
FreeQ[#, x] &]

(* {Sqrt + Sqrt - 2 x, Sqrt - Sqrt + 2 x,
Sqrt - Sqrt - 2 x, Sqrt + Sqrt + 2 x} *)

candidates = Flatten[Map[x /. Solve[# == 0, x] &, fax]];
First[Select[candidates, (N[#] == val) &]]

(* 1/2 (Sqrt + Sqrt *)


--- end edit ---

• How would this be used for Sqrt[4 + Sqrt]? Dec 7, 2012 at 16:42
• @Mr.Wizard See edit. Dec 7, 2012 at 18:49
• Very excellently! Dec 8, 2012 at 13:04
• Any reason for doing RootReduce[root][][x] instead of MinimalPolynomial[root, x]? Jul 15, 2018 at 17:07
• @ChipHurst Probably not. This was so long ago I don't recall the details. Like whether I knew about MinimalPolynomial. Jul 16, 2018 at 15:54

Mathematica uses a ComplexityFunction to measure how 'simple' an expression is. According to the documentation:

ComplexityFunction counts the subexpressions and digits of integers

What we need to do is create a ComplexityFunction that considers the expressions you desire to be very cheap. Depth works for your middle expression

f[x_] := Depth[x];
FullSimplify[{1/(1 + Sqrt - Sqrt), Sqrt[5 - 2*Sqrt],
Sqrt[4 + Sqrt]}, ComplexityFunction -> f]


gives Sadly, the expression you want has the same depth as the expression you started with

Depth[Sqrt[4 + Sqrt]]

4

Depth[1/2 (Sqrt + Sqrt)]

4


I'm afraid that I couldn't find anything that would Simplify that final expression.

• The complexity function is not the problem as one can hack together one that scores the outputs lower than the inputs. However there doesn't seem to be an automatic transformation that produces this result so one will need to introduce a new TransformationFunctions function. Dec 7, 2012 at 15:23
• So I have realised since writing the above. Thanks for the extra info. Dec 7, 2012 at 15:25

Input#1:

a + b == (Sqrt[x] + Sqrt[y])^2 == x + y + 2 Sqrt[x*y];
Solve[{x + y == a, 4 x*y == b^2}, {x, y}]
(*
{{x -> 1/2 (a - Sqrt[a^2 - b^2]), y -> 1/2 (a + Sqrt[a^2 - b^2])},
{x -> 1/2 (a + Sqrt[a^2 - b^2]),
y -> 1/2 (a - Sqrt[a^2 - b^2])}}*)


Define function:

f[a_, b_] :=
Simplify[Sqrt[1/2 (a - Sqrt[a^2 - b^2])] +
Sqrt[1/2 (a + Sqrt[a^2 - b^2])]]


Input#2:

f[5, 2 Sqrt]
f[4, Sqrt]


Output#2:

Sqrt + Sqrt
Sqrt[3/2] + Sqrt[5/2]


The Simplify of 1/(1 + Sqrt - Sqrt)

Expand[(1 + Sqrt - Sqrt) (1 + Sqrt + Sqrt)]
(*2 Sqrt*)
FullSimplify[(Sqrt + 1 + Sqrt)/(2 Sqrt)]
(*1/4 (2 + Sqrt + Sqrt)*)


Picture: 