(expr)
return _mexpand(r)
Helper that denests the square root of three or more surds.

    It returns the denested expression; if it cannot be denested it
    throws SqrtdenestStopIteration

    Algorithm: expr.base is in the extension Q_m = Q(sqrt(r_1),..,sqrt(r_k));
    split expr.base = a + b*sqrt(r_k), where `a` and `b` are on
    Q_(m-1) = Q(sqrt(r_1),..,sqrt(r_(k-1))); then a**2 - b**2*r_k is
    on Q_(m-1); denest sqrt(a**2 - b**2*r_k) and so on.
    See [1], section 6.

    Examples
    ========

    >>> from sympy import sqrt
    >>> from sympy.simplify.sqrtdenest import _sqrtdenest_rec
    >>> _sqrtdenest_rec(sqrt(-72*sqrt(2) + 158*sqrt(5) + 498))
    -sqrt(10) + sqrt(2) + 9 + 9*sqrt(5)
    >>> w=-6*sqrt(55)-6*sqrt(35)-2*sqrt(22)-2*sqrt(14)+2*sqrt(77)+6*sqrt(10)+65
    >>> _sqrtdenest_rec(sqrt(w))
    -sqrt(11) - sqrt(7) + sqrt(2) + 3*sqrt(5)
Helper that denests the square root of three or more surds.
Helper that denests the square root of three or more surds .
def _sqrtdenest_rec(expr):
    """Helper that denests the square root of three or more surds.

    It returns the denested expression; if it cannot be denested it
    throws SqrtdenestStopIteration

    Algorithm: expr.base is in the extension Q_m = Q(sqrt(r_1),..,sqrt(r_k));
    split expr.base = a + b*sqrt(r_k), where `a` and `b` are on
    Q_(m-1) = Q(sqrt(r_1),..,sqrt(r_(k-1))); then a**2 - b**2*r_k is
    on Q_(m-1); denest sqrt(a**2 - b**2*r_k) and so on.
    See [1], section 6.

    Examples
    ========

    >>> from sympy import sqrt
    >>> from sympy.simplify.sqrtdenest import _sqrtdenest_rec
    >>> _sqrtdenest_rec(sqrt(-72*sqrt(2) + 158*sqrt(5) + 498))
    -sqrt(10) + sqrt(2) + 9 + 9*sqrt(5)
    >>> w=-6*sqrt(55)-6*sqrt(35)-2*sqrt(22)-2*sqrt(14)+2*sqrt(77)+6*sqrt(10)+65
    >>> _sqrtdenest_rec(sqrt(w))
    -sqrt(11) - sqrt(7) + sqrt(2) + 3*sqrt(5)
    """
    from sympy.simplify.simplify import radsimp, split_surds, rad_rationalize
    if not expr.is_Pow:
        return sqrtdenest(expr)
    if expr.base < 0:
        return sqrt(-1)*_sqrtdenest_rec(sqrt(-expr.base))
    g, a, b = split_surds(expr.base)
    a = a*sqrt(g)
    if a < b:
        a, b = b, a
    c2 = _mexpand(a**2 - b**2)
    if len(c2.args) > 2:
        g, a1, b1 = split_surds(c2)
        a1 = a1*sqrt(g)
        if a1 < b1:
            a1, b1 = b1, a1
        c2_1 = _mexpand(a1**2 - b1**2)
        c_1 = _sqrtdenest_rec(sqrt(c2_1))
        d_1 = _sqrtdenest_rec(sqrt(a1 + c_1))
        num, den = rad_rationalize(b1, d_1)
        c = _mexpand(d_1/sqrt(2) + num/(den*sqrt(2)))
    else:
        c = _sqrtdenest1(sqrt(c2))

    if sqrt_depth(c) > 1:
        raise SqrtdenestStopIteration
    ac = a + c
    if len(ac.args) >= len(expr.args):
        if count_ops(ac) >= count_ops(expr.base):
            raise SqrtdenestStopIteration
    d = sqrtdenest(sqrt(ac))
    if sqrt_depth(d) > 1:
        raise SqrtdenestStopIteration
    num, den = rad_rationalize(b, d)
    r = d/sqrt(2) + num/(den*sqrt(2))
    r = radsimp(r)
    return _mexpand(r)
def _sqrtdenest_rec ( expr ) : from sympy . simplify . simplify import radsimp , split_surds , rad_rationalize if not expr . is_Pow : return sqrtdenest ( expr ) if expr . base < 0 : return sqrt ( - 1 ) * _sqrtdenest_rec ( sqrt ( - expr . base ) ) g , a , b = split_surds ( expr . base ) a = a * sqrt ( g ) if a < b : a , b = b , a c2 = _mexpand ( a ** 2 - b ** 2 ) if len ( c2 . args ) > 2 : g , a1 , b1 = split_surds ( c2 ) a1 = a1 * sqrt ( g ) if a1 < b1 : a1 , b1 = b1 , a1 c2_1 = _mexpand ( a1 ** 2 - b1 ** 2 ) c_1 = _sqrtdenest_rec ( sqrt ( c2_1 ) ) d_1 = _sqrtdenest_rec ( sqrt ( a1 + c_1 ) ) num , den = rad_rationalize ( b1 , d_1 ) c = _mexpand ( d_1 / sqrt ( 2 ) + num / ( den * sqrt ( 2 ) ) ) else : c = _sqrtdenest1 ( sqrt ( c2 ) ) if sqrt_depth ( c ) > 1 : raise SqrtdenestStopIteration ac = a + c if len ( ac . args ) >= len ( expr . args ) : if count_ops ( ac ) >= count_ops ( expr . base ) : raise SqrtdenestStopIteration d = sqrtdenest ( sqrt ( ac ) ) if sqrt_depth ( d ) > 1 : raise SqrtdenestStopIteration num , den = rad_rationalize ( b , d ) r = d / sqrt ( 2 ) + num / ( den * sqrt ( 2 ) ) r = radsimp ( r ) return _mexpand ( r )