def _nsort(roots, separated=False):
 if (not all((r.is_number for r in roots))):
  raise NotImplementedError
 key = [[i.n(2).as_real_imag()[0] for i in r.as_real_imag()] for r in roots]
 if any(((i._prec == 1) for k in key for i in k)):
  raise NotImplementedError('could not compute root with precision')
 key = [((1 if i else 0), r, i) for (r, i) in key]
 key = sorted(zip(key, roots))
 if separated:
  r = []
  i = []
  for ((im, _, _), v) in key:
   if im:
    i.append(v)
   else:
    r.append(v)
  return (r, i)
 (_, roots) = zip(*key)
 return list(roots)

def _nsort ( roots , separated = False ) : if ( not all ( ( r . is_number for r in roots ) ) ) : raise NotImplementedError key = [ [ i . n ( 2 ) . as_real_imag ( ) [ 0 ] for i in r . as_real_imag ( ) ] for r in roots ] if any ( ( ( i . _prec == 1 ) for k in key for i in k ) ) : raise NotImplementedError ( 'could not compute root with precision' ) key = [ ( ( 1 if i else 0 ) , r , i ) for ( r , i ) in key ] key = sorted ( zip ( key , roots ) ) if separated : r = [ ] i = [ ] for ( ( im , _ , _ ) , v ) in key : if im : i . append ( v ) else : r . append ( v ) return ( r , i ) ( _ , roots ) = zip ( * key ) return list ( roots )
sort the numerical roots putting the real roots first .