# Simplifying expressions with nested square roots

When I run

FullSimplify[Sqrt[(1 - Sqrt[1 - 2 x^2 y^2])^2], {1 > 2 x^2 y^2 > 0}]


I obtain 1 - Sqrt[1 - 2 x^2 y^2]. However, when I run

FullSimplify[ExpandAll[Sqrt[(1 - Sqrt[1 - 2 x^2 y^2])^2]], {1 > 2 x^2 y^2 > 0}]


I am never able to recover the simpler expression and I always obtain Sqrt[2 - 2 x^2 y^2 - 2 Sqrt[1 - 2 x^2 y^2]]. (I did try various PowerExpand and ComplexityFunction variants with no avail.)

How to deal with this? Is there a way to ask FullSimplify to look into square roots and search for squares of simpler expressions that have unambiguous square roots given the assumptions?

Ultimately, I believe that the issue is with FullSimplify not realizing that some forms of the expressions would simplify even further in the next step given the assumptions.

Here is a more advanced example of what I am dealing with and that I would like to simplify more algorithmically. First:

FullSimplify[
-Sqrt[2] x^2 y
+ Sqrt[1 - x^2 y^2 - Sqrt[1 - 2 x^2 y^2]]/y
+ x Sqrt[
((-1 + 2 x^2 y^2) (-1 + Sqrt[1 - 2 x^2 y^2]))/(1 + Sqrt[1- 2 x^2 y^2])
],
{1 > 2 x^2 y^2 > 0, x > 0, y > 0}]


returns the same expression. However, one can see by "manual" operations that

Simplify[
Simplify[
Simplify[
-Sqrt[2] x^2 y
+ Sqrt[1 - x^2 y^2 - Sqrt[1 - 2 x^2 y^2]]/y
+ x Sqrt[
((-1 + 2 x^2 y^2) (-1 + Sqrt[1 - 2 x^2 y^2]))/(1 + Sqrt[1 - 2 x^2 y^2])
],
{1 > 2 x^2 y^2 > 0, x > 0, y > 0}]
/. {1 - x^2 y^2 - Sqrt[1 - 2 x^2 y^2] -> (1 - Sqrt[1 - 2 x^2 y^2])^2/2},
{1 > 2 x^2 y^2 > 0, x > 0, y > 0}]
/. {((-1 + 2 x^2 y^2) (-1 + Sqrt[1 - 2 x^2 y^2]))/(1 + Sqrt[1 - 2 x^2 y^2]) -> ((1 - 2 x^2 y^2) 2 x^2 y^2)/(1 + Sqrt[1 - 2 x^2 y^2])^2},
{1 > 2 x^2 y^2 > 0, x > 0, y > 0}]


the expression is actually identically zero given the assumptions. Note that no further assumptions were used in these "manual" operations, only identities that apply under the assumptions {1 > 2 x^2 y^2 > 0, x > 0, y > 0} were used. Ideally the solution should work also in this example.

As kindly pointed out by JimB in the comments, it turns out that the function I needed was given by Carl Woll in this answer:

denestSqrt[e_, domain_, X] :=
Replace[
z /. Solve[
Simplify[Reduce[Reduce[
z == e && domain, X
], z, Reals], domain], z],
{{r_} :> r, _ -> e}];


This function is used as follows

nasty /. Sqrt[s_] :> denestSqrt[Sqrt[s], mydomain, myvar]]


It takes the nasty expression, finds every square root, and looks whether it can be reduced given that the variable myvar fulfills the conditions specified in mydomain.

Let us see how it fares with the advanced example. I have more than one variable, x,y. In principle one can use more than one variable by myvar = {x,y} and specify the conditions in mydomain = x>0 && y>0 && 1>2 x^2 y^2>0. This did not yield satisfactory results. However, I was able to choose a different variable $$X = \sqrt{2}xy$$ so that all the square roots end up only referring to $$X$$ and not $$y$$:

nasty = FullSimplify[
-Sqrt[2] x^2 y
+ Sqrt[1 - x^2 y^2 - Sqrt[1 - 2 x^2 y^2]]/y
+ x Sqrt[((-1 + 2 x^2 y^2) (-1 + Sqrt[1 - 2 x^2 y^2]))/(1 + Sqrt[1 - 2 x^2 y^2])]
/. {x -> X/(y Sqrt[2])},
{y > 0, 1 > X > 0}
]


Then I was able to finally obtain

Simplify[nasty /. Sqrt[s_] :> denestSqrt[Sqrt[s], 0 < X < 1, X]]
>> 0


So far, this has also worked for any other expression that I get in my expansions.

Eliminate the term you want to ged rid of with Solve

f = Sqrt[(1 - Sqrt[1 - 2 x^2 y^2])^2];

fe = ExpandAll[f];

fsol /. First@
Solve[{fe == fsol, g == Sqrt[1 - 2 x^2 y^2], g >= 0}, fsol, {g},
Reals] // FullSimplify[#, 1 > 2 x^2 y^2 > 0] &

(*   1 - Sqrt[1 - 2 x^2 y^2]   *)