def _sqrtdenest0(expr):
 if is_sqrt(expr):
  (n, d) = expr.as_numer_denom()
  if (d is S.One):
   if n.base.is_Add:
    args = sorted(n.base.args, key=default_sort_key)
    if ((len(args) > 2) and all(((x ** 2).is_Integer for x in args))):
     try:
      return _sqrtdenest_rec(n)
     except SqrtdenestStopIteration:
      pass
    expr = sqrt(_mexpand(Add(*[_sqrtdenest0(x) for x in args])))
   return _sqrtdenest1(expr)
  else:
   (n, d) = [_sqrtdenest0(i) for i in (n, d)]
   return (n / d)
 if isinstance(expr, Expr):
  args = expr.args
  if args:
   return expr.func(*[_sqrtdenest0(a) for a in args])
 return expr

def _sqrtdenest0 ( expr ) : if is_sqrt ( expr ) : ( n , d ) = expr . as_numer_denom ( ) if ( d is S . One ) : if n . base . is_Add : args = sorted ( n . base . args , key = default_sort_key ) if ( ( len ( args ) > 2 ) and all ( ( ( x ** 2 ) . is_Integer for x in args ) ) ) : try : return _sqrtdenest_rec ( n ) except SqrtdenestStopIteration : pass expr = sqrt ( _mexpand ( Add ( * [ _sqrtdenest0 ( x ) for x in args ] ) ) ) return _sqrtdenest1 ( expr ) else : ( n , d ) = [ _sqrtdenest0 ( i ) for i in ( n , d ) ] return ( n / d ) if isinstance ( expr , Expr ) : args = expr . args if args : return expr . func ( * [ _sqrtdenest0 ( a ) for a in args ] ) return expr
returns expr after denesting its arguments .