def monotone_fn_inverter(fn, x, vectorized=True, **keywords):
 x = np.asarray(x)
 if vectorized:
  y = fn(x, **keywords)
 else:
  y = []
  for _x in x:
   y.append(fn(_x, **keywords))
  y = np.array(y)
 a = np.argsort(y)
 return interp1d(y[a], x[a])

def monotone_fn_inverter ( fn , x , vectorized = True , ** keywords ) : x = np . asarray ( x ) if vectorized : y = fn ( x , ** keywords ) else : y = [ ] for _x in x : y . append ( fn ( _x , ** keywords ) ) y = np . array ( y ) a = np . argsort ( y ) return interp1d ( y [ a ] , x [ a ] )
given a monotone function fn and a set of x values .