def minimize(fun, x0, args=(), method=None, jac=None, hess=None, hessp=None, bounds=None, constraints=(), tol=None, callback=None, options=None):
 x0 = np.asarray(x0)
 if (x0.dtype.kind in np.typecodes['AllInteger']):
  x0 = np.asarray(x0, dtype=float)
 if (not isinstance(args, tuple)):
  args = (args,)
 if (method is None):
  if constraints:
   method = 'SLSQP'
  elif (bounds is not None):
   method = 'L-BFGS-B'
  else:
   method = 'BFGS'
 if callable(method):
  meth = '_custom'
 else:
  meth = method.lower()
 if (options is None):
  options = {}
 if ((meth in ['nelder-mead', 'powell', 'cobyla']) and bool(jac)):
  warn(('Method %s does not use gradient information (jac).' % method), RuntimeWarning)
 if ((meth not in ('newton-cg', 'dogleg', 'trust-ncg', '_custom')) and (hess is not None)):
  warn(('Method %s does not use Hessian information (hess).' % method), RuntimeWarning)
 if ((meth not in ('newton-cg', 'dogleg', 'trust-ncg', '_custom')) and (hessp is not None)):
  warn(('Method %s does not use Hessian-vector product information (hessp).' % method), RuntimeWarning)
 if ((meth in ['nelder-mead', 'powell', 'cg', 'bfgs', 'newton-cg', 'dogleg', 'trust-ncg']) and ((bounds is not None) or np.any(constraints))):
  warn(('Method %s cannot handle constraints nor bounds.' % method), RuntimeWarning)
 if ((meth in ['l-bfgs-b', 'tnc']) and np.any(constraints)):
  warn(('Method %s cannot handle constraints.' % method), RuntimeWarning)
 if ((meth == 'cobyla') and (bounds is not None)):
  warn(('Method %s cannot handle bounds.' % method), RuntimeWarning)
 if ((meth in ['cobyla']) and (callback is not None)):
  warn(('Method %s does not support callback.' % method), RuntimeWarning)
 if ((meth in ['l-bfgs-b', 'tnc', 'cobyla', 'slsqp']) and options.get('return_all', False)):
  warn(('Method %s does not support the return_all option.' % method), RuntimeWarning)
 if (not callable(jac)):
  if bool(jac):
   fun = MemoizeJac(fun)
   jac = fun.derivative
  else:
   jac = None
 if (tol is not None):
  options = dict(options)
  if (meth == 'nelder-mead'):
   options.setdefault('xatol', tol)
   options.setdefault('fatol', tol)
  if (meth in ['newton-cg', 'powell', 'tnc']):
   options.setdefault('xtol', tol)
  if (meth in ['powell', 'l-bfgs-b', 'tnc', 'slsqp']):
   options.setdefault('ftol', tol)
  if (meth in ['bfgs', 'cg', 'l-bfgs-b', 'tnc', 'dogleg', 'trust-ncg']):
   options.setdefault('gtol', tol)
  if (meth in ['cobyla', '_custom']):
   options.setdefault('tol', tol)
 if (meth == '_custom'):
  return method(fun, x0, args=args, jac=jac, hess=hess, hessp=hessp, bounds=bounds, constraints=constraints, callback=callback, **options)
 elif (meth == 'nelder-mead'):
  return _minimize_neldermead(fun, x0, args, callback, **options)
 elif (meth == 'powell'):
  return _minimize_powell(fun, x0, args, callback, **options)
 elif (meth == 'cg'):
  return _minimize_cg(fun, x0, args, jac, callback, **options)
 elif (meth == 'bfgs'):
  return _minimize_bfgs(fun, x0, args, jac, callback, **options)
 elif (meth == 'newton-cg'):
  return _minimize_newtoncg(fun, x0, args, jac, hess, hessp, callback, **options)
 elif (meth == 'l-bfgs-b'):
  return _minimize_lbfgsb(fun, x0, args, jac, bounds, callback=callback, **options)
 elif (meth == 'tnc'):
  return _minimize_tnc(fun, x0, args, jac, bounds, callback=callback, **options)
 elif (meth == 'cobyla'):
  return _minimize_cobyla(fun, x0, args, constraints, **options)
 elif (meth == 'slsqp'):
  return _minimize_slsqp(fun, x0, args, jac, bounds, constraints, callback=callback, **options)
 elif (meth == 'dogleg'):
  return _minimize_dogleg(fun, x0, args, jac, hess, callback=callback, **options)
 elif (meth == 'trust-ncg'):
  return _minimize_trust_ncg(fun, x0, args, jac, hess, hessp, callback=callback, **options)
 else:
  raise ValueError(('Unknown solver %s' % method))

def minimize ( fun , x0 , args = ( ) , method = None , jac = None , hess = None , hessp = None , bounds = None , constraints = ( ) , tol = None , callback = None , options = None ) : x0 = np . asarray ( x0 ) if ( x0 . dtype . kind in np . typecodes [ 'AllInteger' ] ) : x0 = np . asarray ( x0 , dtype = float ) if ( not isinstance ( args , tuple ) ) : args = ( args , ) if ( method is None ) : if constraints : method = 'SLSQP' elif ( bounds is not None ) : method = 'L-BFGS-B' else : method = 'BFGS' if callable ( method ) : meth = '_custom' else : meth = method . lower ( ) if ( options is None ) : options = { } if ( ( meth in [ 'nelder-mead' , 'powell' , 'cobyla' ] ) and bool ( jac ) ) : warn ( ( 'Method %s does not use gradient information (jac).' % method ) , RuntimeWarning ) if ( ( meth not in ( 'newton-cg' , 'dogleg' , 'trust-ncg' , '_custom' ) ) and ( hess is not None ) ) : warn ( ( 'Method %s does not use Hessian information (hess).' % method ) , RuntimeWarning ) if ( ( meth not in ( 'newton-cg' , 'dogleg' , 'trust-ncg' , '_custom' ) ) and ( hessp is not None ) ) : warn ( ( 'Method %s does not use Hessian-vector product information (hessp).' % method ) , RuntimeWarning ) if ( ( meth in [ 'nelder-mead' , 'powell' , 'cg' , 'bfgs' , 'newton-cg' , 'dogleg' , 'trust-ncg' ] ) and ( ( bounds is not None ) or np . any ( constraints ) ) ) : warn ( ( 'Method %s cannot handle constraints nor bounds.' % method ) , RuntimeWarning ) if ( ( meth in [ 'l-bfgs-b' , 'tnc' ] ) and np . any ( constraints ) ) : warn ( ( 'Method %s cannot handle constraints.' % method ) , RuntimeWarning ) if ( ( meth == 'cobyla' ) and ( bounds is not None ) ) : warn ( ( 'Method %s cannot handle bounds.' % method ) , RuntimeWarning ) if ( ( meth in [ 'cobyla' ] ) and ( callback is not None ) ) : warn ( ( 'Method %s does not support callback.' % method ) , RuntimeWarning ) if ( ( meth in [ 'l-bfgs-b' , 'tnc' , 'cobyla' , 'slsqp' ] ) and options . get ( 'return_all' , False ) ) : warn ( ( 'Method %s does not support the return_all option.' % method ) , RuntimeWarning ) if ( not callable ( jac ) ) : if bool ( jac ) : fun = MemoizeJac ( fun ) jac = fun . derivative else : jac = None if ( tol is not None ) : options = dict ( options ) if ( meth == 'nelder-mead' ) : options . setdefault ( 'xatol' , tol ) options . setdefault ( 'fatol' , tol ) if ( meth in [ 'newton-cg' , 'powell' , 'tnc' ] ) : options . setdefault ( 'xtol' , tol ) if ( meth in [ 'powell' , 'l-bfgs-b' , 'tnc' , 'slsqp' ] ) : options . setdefault ( 'ftol' , tol ) if ( meth in [ 'bfgs' , 'cg' , 'l-bfgs-b' , 'tnc' , 'dogleg' , 'trust-ncg' ] ) : options . setdefault ( 'gtol' , tol ) if ( meth in [ 'cobyla' , '_custom' ] ) : options . setdefault ( 'tol' , tol ) if ( meth == '_custom' ) : return method ( fun , x0 , args = args , jac = jac , hess = hess , hessp = hessp , bounds = bounds , constraints = constraints , callback = callback , ** options ) elif ( meth == 'nelder-mead' ) : return _minimize_neldermead ( fun , x0 , args , callback , ** options ) elif ( meth == 'powell' ) : return _minimize_powell ( fun , x0 , args , callback , ** options ) elif ( meth == 'cg' ) : return _minimize_cg ( fun , x0 , args , jac , callback , ** options ) elif ( meth == 'bfgs' ) : return _minimize_bfgs ( fun , x0 , args , jac , callback , ** options ) elif ( meth == 'newton-cg' ) : return _minimize_newtoncg ( fun , x0 , args , jac , hess , hessp , callback , ** options ) elif ( meth == 'l-bfgs-b' ) : return _minimize_lbfgsb ( fun , x0 , args , jac , bounds , callback = callback , ** options ) elif ( meth == 'tnc' ) : return _minimize_tnc ( fun , x0 , args , jac , bounds , callback = callback , ** options ) elif ( meth == 'cobyla' ) : return _minimize_cobyla ( fun , x0 , args , constraints , ** options ) elif ( meth == 'slsqp' ) : return _minimize_slsqp ( fun , x0 , args , jac , bounds , constraints , callback = callback , ** options ) elif ( meth == 'dogleg' ) : return _minimize_dogleg ( fun , x0 , args , jac , hess , callback = callback , ** options ) elif ( meth == 'trust-ncg' ) : return _minimize_trust_ncg ( fun , x0 , args , jac , hess , hessp , callback = callback , ** options ) else : raise ValueError ( ( 'Unknown solver %s' % method ) )
select result of rules that minimizes objective .