sympy/sympy — pull request #792 by pernici (2011-11-30 19:19:39+00:00)
Title: sqrt matching in sqrtdenest

`sqrtdenest` does not denest this example (appearing with XFAIL in test_sqrtdenest.py)

```
>>> sqrtdenest(sqrt(16-2*sqrt(29)+2*sqrt(55-10*sqrt(29))))
sqrt(-2*sqrt(29) + 2*sqrt(-10*sqrt(29) + 55) + 16)
```

The reason is that `expr.match(sqrt(a + b * sqrt(r)))` does
not guarantee that `sqrt(r)` contains the deepest sqrt; on my computer

```
>>> a, b, r = Wild('a'), Wild('b'), Wild('r')
>>> expr = sqrt(-2*sqrt(29) + 2*sqrt(-10*sqrt(29) + 55) + 16)
>>> expr.match(sqrt(a + b * sqrt(r)))
{r_: 29, b_: -2, a_: 2*sqrt(-10*sqrt(29) + 55) + 16}
```

I fixed this problem using `sqrt_depth` to find the depth of `sqrt` in an expression.
Added a simplified radsimp to simplify the result; e.g. in master
the following example is denested

```
>>> z=sqrt(-2*sqrt(5) + 2*sqrt(2)*sqrt(-2*sqrt(5) + 11) + 13)
>>> a = sqrtdenest(z); a
sqrt(-16*sqrt(5)/(-8*sqrt(5) + 44) + 88/(-8*sqrt(5) + 44)) + sqrt(-2*sqrt(5) + 11)
```

but here one gets `sqrt(2) + sqrt(-2*sqrt(5) + 11)`.

I also changed `subsets` to use inversed lexicographic ordering, to
guarantee that the case of the fourth root is treated last.
In the examples I tried this gives sometimes a result more denested, e.g.
in this branch

```
>>> from sympy import *
>>> z=sqrt(-2*sqrt(10) + 2*sqrt(2)*sqrt(-2*sqrt(10) + 11) + 13)
>>> sqrtdenest(z)
-1 + sqrt(2) + sqrt(10)
```

Replacing `subsets` with the one used in master one gets

```
sqrt(-2*sqrt(10) - 2*sqrt(2) + 4*sqrt(5) + 13)
```