<s> """<STR_LIT>""" <EOL> from __future__ import division , print_function , absolute_import <EOL> __all__ = [ '<STR_LIT:root>' ] <EOL> import numpy as np <EOL> from scipy . _lib . six import callable <EOL> from warnings import warn <EOL> from . optimize import MemoizeJac , OptimizeResult , _check_unknown_options <EOL> from . minpack import _root_hybr , leastsq <EOL> from . _spectral import _root_df_sane <EOL> from . import nonlin <EOL> def root ( fun , x0 , args = ( ) , method = '<STR_LIT>' , jac = None , tol = None , callback = None , <EOL> options = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( args , tuple ) : <EOL> args = ( args , ) <EOL> meth = method . lower ( ) <EOL> if options is None : <EOL> options = { } <EOL> if callback is not None and meth in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> warn ( '<STR_LIT>' % method , <EOL> RuntimeWarning ) <EOL> if not callable ( jac ) and meth in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if bool ( jac ) : <EOL> fun = MemoizeJac ( fun ) <EOL> jac = fun . derivative <EOL> else : <EOL> jac = None <EOL> if tol is not None : <EOL> options = dict ( options ) <EOL> if meth in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> options . setdefault ( '<STR_LIT>' , tol ) <EOL> elif meth in ( '<STR_LIT>' , ) : <EOL> options . setdefault ( '<STR_LIT>' , tol ) <EOL> elif meth in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> options . setdefault ( '<STR_LIT>' , tol ) <EOL> options . setdefault ( '<STR_LIT>' , np . inf ) <EOL> options . setdefault ( '<STR_LIT>' , np . inf ) <EOL> options . setdefault ( '<STR_LIT>' , np . inf ) <EOL> if meth == '<STR_LIT>' : <EOL> sol = _root_hybr ( fun , x0 , args = args , jac = jac , ** options ) <EOL> elif meth == '<STR_LIT>' : <EOL> sol = _root_leastsq ( fun , x0 , args = args , jac = jac , ** options ) <EOL> elif meth == '<STR_LIT>' : <EOL> _warn_jac_unused ( jac , method ) <EOL> sol = _root_df_sane ( fun , x0 , args = args , callback = callback , <EOL> ** options ) <EOL> elif meth in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> _warn_jac_unused ( jac , method ) <EOL> sol = _root_nonlin_solve ( fun , x0 , args = args , jac = jac , <EOL> _method = meth , _callback = callback , <EOL> ** options ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % method ) <EOL> return sol <EOL> def _warn_jac_unused ( jac , method ) : <EOL> if jac is not None : <EOL> warn ( '<STR_LIT>' % ( method , ) , <EOL> RuntimeWarning ) <EOL> def _root_leastsq ( func , x0 , args = ( ) , jac = None , <EOL> col_deriv = <NUM_LIT:0> , xtol = <NUM_LIT> , ftol = <NUM_LIT> , <EOL> gtol = <NUM_LIT:0.0> , maxiter = <NUM_LIT:0> , eps = <NUM_LIT:0.0> , factor = <NUM_LIT:100> , diag = None , <EOL> ** unknown_options ) : <EOL> """<STR_LIT>""" <EOL> _check_unknown_options ( unknown_options ) <EOL> x , cov_x , info , msg , ier = leastsq ( func , x0 , args = args , Dfun = jac , <EOL> full_output = True , <EOL> col_deriv = col_deriv , xtol = xtol , <EOL> ftol = ftol , gtol = gtol , <EOL> maxfev = maxiter , epsfcn = eps , <EOL> factor = factor , diag = diag ) <EOL> sol = OptimizeResult ( x = x , message = msg , status = ier , <EOL> success = ier in ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) , cov_x = cov_x , <EOL> fun = info . pop ( '<STR_LIT>' ) ) <EOL> sol . update ( info ) <EOL> return sol <EOL> def _root_nonlin_solve ( func , x0 , args = ( ) , jac = None , <EOL> _callback = None , _method = None , <EOL> nit = None , disp = False , maxiter = None , <EOL> ftol = None , fatol = None , xtol = None , xatol = None , <EOL> tol_norm = None , line_search = '<STR_LIT>' , jac_options = None , <EOL> ** unknown_options ) : <EOL> _check_unknown_options ( unknown_options ) <EOL> f_tol = fatol <EOL> f_rtol = ftol <EOL> x_tol = xatol <EOL> x_rtol = xtol <EOL> verbose = disp <EOL> if jac_options is None : <EOL> jac_options = dict ( ) <EOL> jacobian = { '<STR_LIT>' : nonlin . BroydenFirst , <EOL> '<STR_LIT>' : nonlin . BroydenSecond , <EOL> '<STR_LIT>' : nonlin . Anderson , <EOL> '<STR_LIT>' : nonlin . LinearMixing , <EOL> '<STR_LIT>' : nonlin . DiagBroyden , <EOL> '<STR_LIT>' : nonlin . ExcitingMixing , <EOL> '<STR_LIT>' : nonlin . KrylovJacobian <EOL> } [ _method ] <EOL> if args : <EOL> if jac : <EOL> def f ( x ) : <EOL> return func ( x , * args ) [ <NUM_LIT:0> ] <EOL> else : <EOL> def f ( x ) : <EOL> return func ( x , * args ) <EOL> else : <EOL> f = func <EOL> x , info = nonlin . nonlin_solve ( f , x0 , jacobian = jacobian ( ** jac_options ) , <EOL> iter = nit , verbose = verbose , <EOL> maxiter = maxiter , f_tol = f_tol , <EOL> f_rtol = f_rtol , x_tol = x_tol , <EOL> x_rtol = x_rtol , tol_norm = tol_norm , <EOL> line_search = line_search , <EOL> callback = _callback , full_output = True , <EOL> raise_exception = False ) <EOL> sol = OptimizeResult ( x = x ) <EOL> sol . update ( info ) <EOL> return sol <EOL> def _root_broyden1_doc ( ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _root_broyden2_doc ( ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _root_anderson_doc ( ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _root_linearmixing_doc ( ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _root_diagbroyden_doc ( ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _root_excitingmixing_doc ( ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _root_krylov_doc ( ) : <EOL> """<STR_LIT>""" <EOL> pass </s>
