<s> from __future__ import division , print_function , absolute_import <EOL> import warnings <EOL> from . import _zeros <EOL> from numpy import finfo , sign , sqrt <EOL> _iter = <NUM_LIT:100> <EOL> _xtol = <NUM_LIT> <EOL> _rtol = <NUM_LIT:4> * finfo ( float ) . eps <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> CONVERGED = '<STR_LIT>' <EOL> SIGNERR = '<STR_LIT>' <EOL> CONVERR = '<STR_LIT>' <EOL> flag_map = { <NUM_LIT:0> : CONVERGED , - <NUM_LIT:1> : SIGNERR , - <NUM_LIT:2> : CONVERR } <EOL> class RootResults ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , root , iterations , function_calls , flag ) : <EOL> self . root = root <EOL> self . iterations = iterations <EOL> self . function_calls = function_calls <EOL> self . converged = flag == <NUM_LIT:0> <EOL> try : <EOL> self . flag = flag_map [ flag ] <EOL> except KeyError : <EOL> self . flag = '<STR_LIT>' % ( flag , ) <EOL> def __repr__ ( self ) : <EOL> attrs = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:root>' ] <EOL> m = max ( map ( len , attrs ) ) + <NUM_LIT:1> <EOL> return '<STR_LIT:\n>' . join ( [ a . rjust ( m ) + '<STR_LIT>' + repr ( getattr ( self , a ) ) <EOL> for a in attrs ] ) <EOL> def results_c ( full_output , r ) : <EOL> if full_output : <EOL> x , funcalls , iterations , flag = r <EOL> results = RootResults ( root = x , <EOL> iterations = iterations , <EOL> function_calls = funcalls , <EOL> flag = flag ) <EOL> return x , results <EOL> else : <EOL> return r <EOL> def newton ( func , x0 , fprime = None , args = ( ) , tol = <NUM_LIT> , maxiter = <NUM_LIT:50> , <EOL> fprime2 = None ) : <EOL> """<STR_LIT>""" <EOL> if tol <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" % tol ) <EOL> if maxiter < <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if fprime is not None : <EOL> p0 = <NUM_LIT:1.0> * x0 <EOL> fder2 = <NUM_LIT:0> <EOL> for iter in range ( maxiter ) : <EOL> myargs = ( p0 , ) + args <EOL> fder = fprime ( * myargs ) <EOL> if fder == <NUM_LIT:0> : <EOL> msg = "<STR_LIT>" <EOL> warnings . warn ( msg , RuntimeWarning ) <EOL> return p0 <EOL> fval = func ( * myargs ) <EOL> if fprime2 is not None : <EOL> fder2 = fprime2 ( * myargs ) <EOL> if fder2 == <NUM_LIT:0> : <EOL> p = p0 - fval / fder <EOL> else : <EOL> discr = fder ** <NUM_LIT:2> - <NUM_LIT:2> * fval * fder2 <EOL> if discr < <NUM_LIT:0> : <EOL> p = p0 - fder / fder2 <EOL> else : <EOL> p = p0 - <NUM_LIT:2> * fval / ( fder + sign ( fder ) * sqrt ( discr ) ) <EOL> if abs ( p - p0 ) < tol : <EOL> return p <EOL> p0 = p <EOL> else : <EOL> p0 = x0 <EOL> if x0 >= <NUM_LIT:0> : <EOL> p1 = x0 * ( <NUM_LIT:1> + <NUM_LIT> ) + <NUM_LIT> <EOL> else : <EOL> p1 = x0 * ( <NUM_LIT:1> + <NUM_LIT> ) - <NUM_LIT> <EOL> q0 = func ( * ( ( p0 , ) + args ) ) <EOL> q1 = func ( * ( ( p1 , ) + args ) ) <EOL> for iter in range ( maxiter ) : <EOL> if q1 == q0 : <EOL> if p1 != p0 : <EOL> msg = "<STR_LIT>" % ( p1 - p0 ) <EOL> warnings . warn ( msg , RuntimeWarning ) <EOL> return ( p1 + p0 ) / <NUM_LIT> <EOL> else : <EOL> p = p1 - q1 * ( p1 - p0 ) / ( q1 - q0 ) <EOL> if abs ( p - p1 ) < tol : <EOL> return p <EOL> p0 = p1 <EOL> q0 = q1 <EOL> p1 = p <EOL> q1 = func ( * ( ( p1 , ) + args ) ) <EOL> msg = "<STR_LIT>" % ( maxiter , p ) <EOL> raise RuntimeError ( msg ) <EOL> def bisect ( f , a , b , args = ( ) , <EOL> xtol = _xtol , rtol = _rtol , maxiter = _iter , <EOL> full_output = False , disp = True ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( args , tuple ) : <EOL> args = ( args , ) <EOL> if xtol <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" % xtol ) <EOL> if rtol < _rtol : <EOL> raise ValueError ( "<STR_LIT>" % ( rtol , _rtol ) ) <EOL> r = _zeros . _bisect ( f , a , b , xtol , rtol , maxiter , args , full_output , disp ) <EOL> return results_c ( full_output , r ) <EOL> def ridder ( f , a , b , args = ( ) , <EOL> xtol = _xtol , rtol = _rtol , maxiter = _iter , <EOL> full_output = False , disp = True ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( args , tuple ) : <EOL> args = ( args , ) <EOL> if xtol <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" % xtol ) <EOL> if rtol < _rtol : <EOL> raise ValueError ( "<STR_LIT>" % ( rtol , _rtol ) ) <EOL> r = _zeros . _ridder ( f , a , b , xtol , rtol , maxiter , args , full_output , disp ) <EOL> return results_c ( full_output , r ) <EOL> def brentq ( f , a , b , args = ( ) , <EOL> xtol = _xtol , rtol = _rtol , maxiter = _iter , <EOL> full_output = False , disp = True ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( args , tuple ) : <EOL> args = ( args , ) <EOL> if xtol <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" % xtol ) <EOL> if rtol < _rtol : <EOL> raise ValueError ( "<STR_LIT>" % ( rtol , _rtol ) ) <EOL> r = _zeros . _brentq ( f , a , b , xtol , rtol , maxiter , args , full_output , disp ) <EOL> return results_c ( full_output , r ) <EOL> def brenth ( f , a , b , args = ( ) , <EOL> xtol = _xtol , rtol = _rtol , maxiter = _iter , <EOL> full_output = False , disp = True ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( args , tuple ) : <EOL> args = ( args , ) <EOL> if xtol <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" % xtol ) <EOL> if rtol < _rtol : <EOL> raise ValueError ( "<STR_LIT>" % ( rtol , _rtol ) ) <EOL> r = _zeros . _brenth ( f , a , b , xtol , rtol , maxiter , args , full_output , disp ) <EOL> return results_c ( full_output , r ) </s>
