<s> from __future__ import division , print_function , absolute_import <EOL> import warnings <EOL> from . import _minpack <EOL> import numpy as np <EOL> from numpy import ( atleast_1d , dot , take , triu , shape , eye , <EOL> transpose , zeros , product , greater , array , <EOL> all , where , isscalar , asarray , inf , abs , <EOL> finfo , inexact , issubdtype , dtype ) <EOL> from scipy . linalg import svd <EOL> from scipy . _lib . _util import _asarray_validated , _lazywhere <EOL> from . optimize import OptimizeResult , _check_unknown_options , OptimizeWarning <EOL> from . _lsq import least_squares <EOL> from . _lsq . common import make_strictly_feasible <EOL> from . _lsq . least_squares import prepare_bounds <EOL> error = _minpack . error <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def _check_func ( checker , argname , thefunc , x0 , args , numinputs , <EOL> output_shape = None ) : <EOL> res = atleast_1d ( thefunc ( * ( ( x0 [ : numinputs ] , ) + args ) ) ) <EOL> if ( output_shape is not None ) and ( shape ( res ) != output_shape ) : <EOL> if ( output_shape [ <NUM_LIT:0> ] != <NUM_LIT:1> ) : <EOL> if len ( output_shape ) > <NUM_LIT:1> : <EOL> if output_shape [ <NUM_LIT:1> ] == <NUM_LIT:1> : <EOL> return shape ( res ) <EOL> msg = "<STR_LIT>" "<STR_LIT>" % ( checker , argname ) <EOL> func_name = getattr ( thefunc , '<STR_LIT>' , None ) <EOL> if func_name : <EOL> msg += "<STR_LIT>" % func_name <EOL> else : <EOL> msg += "<STR_LIT:.>" <EOL> msg += '<STR_LIT>' % ( output_shape , shape ( res ) ) <EOL> raise TypeError ( msg ) <EOL> if issubdtype ( res . dtype , inexact ) : <EOL> dt = res . dtype <EOL> else : <EOL> dt = dtype ( float ) <EOL> return shape ( res ) , dt <EOL> def fsolve ( func , x0 , args = ( ) , fprime = None , full_output = <NUM_LIT:0> , <EOL> col_deriv = <NUM_LIT:0> , xtol = <NUM_LIT> , maxfev = <NUM_LIT:0> , band = None , <EOL> epsfcn = None , factor = <NUM_LIT:100> , diag = None ) : <EOL> """<STR_LIT>""" <EOL> options = { '<STR_LIT>' : col_deriv , <EOL> '<STR_LIT>' : xtol , <EOL> '<STR_LIT>' : maxfev , <EOL> '<STR_LIT>' : band , <EOL> '<STR_LIT>' : epsfcn , <EOL> '<STR_LIT>' : factor , <EOL> '<STR_LIT>' : diag } <EOL> res = _root_hybr ( func , x0 , args , jac = fprime , ** options ) <EOL> if full_output : <EOL> x = res [ '<STR_LIT:x>' ] <EOL> info = dict ( ( k , res . get ( k ) ) <EOL> for k in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:r>' , '<STR_LIT>' ) if k in res ) <EOL> info [ '<STR_LIT>' ] = res [ '<STR_LIT>' ] <EOL> return x , info , res [ '<STR_LIT:status>' ] , res [ '<STR_LIT:message>' ] <EOL> else : <EOL> status = res [ '<STR_LIT:status>' ] <EOL> msg = res [ '<STR_LIT:message>' ] <EOL> if status == <NUM_LIT:0> : <EOL> raise TypeError ( msg ) <EOL> elif status == <NUM_LIT:1> : <EOL> pass <EOL> elif status in [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] : <EOL> warnings . warn ( msg , RuntimeWarning ) <EOL> else : <EOL> raise TypeError ( msg ) <EOL> return res [ '<STR_LIT:x>' ] <EOL> def _root_hybr ( func , x0 , args = ( ) , jac = None , <EOL> col_deriv = <NUM_LIT:0> , xtol = <NUM_LIT> , maxfev = <NUM_LIT:0> , band = None , eps = None , <EOL> factor = <NUM_LIT:100> , diag = None , ** unknown_options ) : <EOL> """<STR_LIT>""" <EOL> _check_unknown_options ( unknown_options ) <EOL> epsfcn = eps <EOL> x0 = asarray ( x0 ) . flatten ( ) <EOL> n = len ( x0 ) <EOL> if not isinstance ( args , tuple ) : <EOL> args = ( args , ) <EOL> shape , dtype = _check_func ( '<STR_LIT>' , '<STR_LIT>' , func , x0 , args , n , ( n , ) ) <EOL> if epsfcn is None : <EOL> epsfcn = finfo ( dtype ) . eps <EOL> Dfun = jac <EOL> if Dfun is None : <EOL> if band is None : <EOL> ml , mu = - <NUM_LIT:10> , - <NUM_LIT:10> <EOL> else : <EOL> ml , mu = band [ : <NUM_LIT:2> ] <EOL> if maxfev == <NUM_LIT:0> : <EOL> maxfev = <NUM_LIT:200> * ( n + <NUM_LIT:1> ) <EOL> retval = _minpack . _hybrd ( func , x0 , args , <NUM_LIT:1> , xtol , maxfev , <EOL> ml , mu , epsfcn , factor , diag ) <EOL> else : <EOL> _check_func ( '<STR_LIT>' , '<STR_LIT>' , Dfun , x0 , args , n , ( n , n ) ) <EOL> if ( maxfev == <NUM_LIT:0> ) : <EOL> maxfev = <NUM_LIT:100> * ( n + <NUM_LIT:1> ) <EOL> retval = _minpack . _hybrj ( func , Dfun , x0 , args , <NUM_LIT:1> , <EOL> col_deriv , xtol , maxfev , factor , diag ) <EOL> x , status = retval [ <NUM_LIT:0> ] , retval [ - <NUM_LIT:1> ] <EOL> errors = { <NUM_LIT:0> : "<STR_LIT>" , <EOL> <NUM_LIT:1> : "<STR_LIT>" , <EOL> <NUM_LIT:2> : "<STR_LIT>" <EOL> "<STR_LIT>" % maxfev , <EOL> <NUM_LIT:3> : "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % xtol , <EOL> <NUM_LIT:4> : "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> <NUM_LIT:5> : "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" } <EOL> info = retval [ <NUM_LIT:1> ] <EOL> info [ '<STR_LIT>' ] = info . pop ( '<STR_LIT>' ) <EOL> sol = OptimizeResult ( x = x , success = ( status == <NUM_LIT:1> ) , status = status ) <EOL> sol . update ( info ) <EOL> try : <EOL> sol [ '<STR_LIT:message>' ] = errors [ status ] <EOL> except KeyError : <EOL> info [ '<STR_LIT:message>' ] = errors [ '<STR_LIT>' ] <EOL> return sol <EOL> def leastsq ( func , x0 , args = ( ) , Dfun = None , full_output = <NUM_LIT:0> , <EOL> col_deriv = <NUM_LIT:0> , ftol = <NUM_LIT> , xtol = <NUM_LIT> , <EOL> gtol = <NUM_LIT:0.0> , maxfev = <NUM_LIT:0> , epsfcn = None , factor = <NUM_LIT:100> , diag = None ) : <EOL> """<STR_LIT>""" <EOL> x0 = asarray ( x0 ) . flatten ( ) <EOL> n = len ( x0 ) <EOL> if not isinstance ( args , tuple ) : <EOL> args = ( args , ) <EOL> shape , dtype = _check_func ( '<STR_LIT>' , '<STR_LIT>' , func , x0 , args , n ) <EOL> m = shape [ <NUM_LIT:0> ] <EOL> if n > m : <EOL> raise TypeError ( '<STR_LIT>' % ( n , m ) ) <EOL> if epsfcn is None : <EOL> epsfcn = finfo ( dtype ) . eps <EOL> if Dfun is None : <EOL> if maxfev == <NUM_LIT:0> : <EOL> maxfev = <NUM_LIT:200> * ( n + <NUM_LIT:1> ) <EOL> retval = _minpack . _lmdif ( func , x0 , args , full_output , ftol , xtol , <EOL> gtol , maxfev , epsfcn , factor , diag ) <EOL> else : <EOL> if col_deriv : <EOL> _check_func ( '<STR_LIT>' , '<STR_LIT>' , Dfun , x0 , args , n , ( n , m ) ) <EOL> else : <EOL> _check_func ( '<STR_LIT>' , '<STR_LIT>' , Dfun , x0 , args , n , ( m , n ) ) <EOL> if maxfev == <NUM_LIT:0> : <EOL> maxfev = <NUM_LIT:100> * ( n + <NUM_LIT:1> ) <EOL> retval = _minpack . _lmder ( func , Dfun , x0 , args , full_output , col_deriv , <EOL> ftol , xtol , gtol , maxfev , factor , diag ) <EOL> errors = { <NUM_LIT:0> : [ "<STR_LIT>" , TypeError ] , <EOL> <NUM_LIT:1> : [ "<STR_LIT>" <EOL> "<STR_LIT>" % ftol , None ] , <EOL> <NUM_LIT:2> : [ "<STR_LIT>" <EOL> "<STR_LIT>" % xtol , None ] , <EOL> <NUM_LIT:3> : [ "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( ftol , xtol ) , None ] , <EOL> <NUM_LIT:4> : [ "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % gtol , None ] , <EOL> <NUM_LIT:5> : [ "<STR_LIT>" <EOL> "<STR_LIT>" % maxfev , ValueError ] , <EOL> <NUM_LIT:6> : [ "<STR_LIT>" <EOL> "<STR_LIT>" "<STR_LIT>" % ftol , <EOL> ValueError ] , <EOL> <NUM_LIT:7> : [ "<STR_LIT>" <EOL> "<STR_LIT>" % xtol , <EOL> ValueError ] , <EOL> <NUM_LIT:8> : [ "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % gtol , ValueError ] , <EOL> '<STR_LIT>' : [ "<STR_LIT>" , TypeError ] } <EOL> info = retval [ - <NUM_LIT:1> ] <EOL> if info not in [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] and not full_output : <EOL> if info in [ <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ] : <EOL> warnings . warn ( errors [ info ] [ <NUM_LIT:0> ] , RuntimeWarning ) <EOL> else : <EOL> try : <EOL> raise errors [ info ] [ <NUM_LIT:1> ] ( errors [ info ] [ <NUM_LIT:0> ] ) <EOL> except KeyError : <EOL> raise errors [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ( errors [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) <EOL> mesg = errors [ info ] [ <NUM_LIT:0> ] <EOL> if full_output : <EOL> cov_x = None <EOL> if info in [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] : <EOL> from numpy . dual import inv <EOL> from numpy . linalg import LinAlgError <EOL> perm = take ( eye ( n ) , retval [ <NUM_LIT:1> ] [ '<STR_LIT>' ] - <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> r = triu ( transpose ( retval [ <NUM_LIT:1> ] [ '<STR_LIT>' ] ) [ : n , : ] ) <EOL> R = dot ( r , perm ) <EOL> try : <EOL> cov_x = inv ( dot ( transpose ( R ) , R ) ) <EOL> except ( LinAlgError , ValueError ) : <EOL> pass <EOL> return ( retval [ <NUM_LIT:0> ] , cov_x ) + retval [ <NUM_LIT:1> : - <NUM_LIT:1> ] + ( mesg , info ) <EOL> else : <EOL> return ( retval [ <NUM_LIT:0> ] , info ) <EOL> def _wrap_func ( func , xdata , ydata , weights ) : <EOL> if weights is None : <EOL> def func_wrapped ( params ) : <EOL> return func ( xdata , * params ) - ydata <EOL> else : <EOL> def func_wrapped ( params ) : <EOL> return weights * ( func ( xdata , * params ) - ydata ) <EOL> return func_wrapped <EOL> def _wrap_jac ( jac , xdata , weights ) : <EOL> if weights is None : <EOL> def jac_wrapped ( params ) : <EOL> return jac ( xdata , * params ) <EOL> else : <EOL> def jac_wrapped ( params ) : <EOL> return weights [ : , np . newaxis ] * np . asarray ( jac ( xdata , * params ) ) <EOL> return jac_wrapped <EOL> def _initialize_feasible ( lb , ub ) : <EOL> p0 = np . ones_like ( lb ) <EOL> lb_finite = np . isfinite ( lb ) <EOL> ub_finite = np . isfinite ( ub ) <EOL> mask = lb_finite & ub_finite <EOL> p0 [ mask ] = <NUM_LIT:0.5> * ( lb [ mask ] + ub [ mask ] ) <EOL> mask = lb_finite & ~ ub_finite <EOL> p0 [ mask ] = lb [ mask ] + <NUM_LIT:1> <EOL> mask = ~ lb_finite & ub_finite <EOL> p0 [ mask ] = ub [ mask ] - <NUM_LIT:1> <EOL> return p0 <EOL> def curve_fit ( f , xdata , ydata , p0 = None , sigma = None , absolute_sigma = False , <EOL> check_finite = True , bounds = ( - np . inf , np . inf ) , method = None , <EOL> jac = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if p0 is None : <EOL> from scipy . _lib . _util import getargspec_no_self as _getargspec <EOL> args , varargs , varkw , defaults = _getargspec ( f ) <EOL> if len ( args ) < <NUM_LIT:2> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> n = len ( args ) - <NUM_LIT:1> <EOL> else : <EOL> p0 = np . atleast_1d ( p0 ) <EOL> n = p0 . size <EOL> lb , ub = prepare_bounds ( bounds , n ) <EOL> if p0 is None : <EOL> p0 = _initialize_feasible ( lb , ub ) <EOL> bounded_problem = np . any ( ( lb > - np . inf ) | ( ub < np . inf ) ) <EOL> if method is None : <EOL> if bounded_problem : <EOL> method = '<STR_LIT>' <EOL> else : <EOL> method = '<STR_LIT>' <EOL> if method == '<STR_LIT>' and bounded_problem : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if check_finite : <EOL> ydata = np . asarray_chkfinite ( ydata ) <EOL> else : <EOL> ydata = np . asarray ( ydata ) <EOL> if isinstance ( xdata , ( list , tuple , np . ndarray ) ) : <EOL> if check_finite : <EOL> xdata = np . asarray_chkfinite ( xdata ) <EOL> else : <EOL> xdata = np . asarray ( xdata ) <EOL> weights = <NUM_LIT:1.0> / asarray ( sigma ) if sigma is not None else None <EOL> func = _wrap_func ( f , xdata , ydata , weights ) <EOL> if callable ( jac ) : <EOL> jac = _wrap_jac ( jac , xdata , weights ) <EOL> elif jac is None and method != '<STR_LIT>' : <EOL> jac = '<STR_LIT>' <EOL> if method == '<STR_LIT>' : <EOL> return_full = kwargs . pop ( '<STR_LIT>' , False ) <EOL> res = leastsq ( func , p0 , Dfun = jac , full_output = <NUM_LIT:1> , ** kwargs ) <EOL> popt , pcov , infodict , errmsg , ier = res <EOL> cost = np . sum ( infodict [ '<STR_LIT>' ] ** <NUM_LIT:2> ) <EOL> if ier not in [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] : <EOL> raise RuntimeError ( "<STR_LIT>" + errmsg ) <EOL> else : <EOL> res = least_squares ( func , p0 , jac = jac , bounds = bounds , method = method , <EOL> ** kwargs ) <EOL> if not res . success : <EOL> raise RuntimeError ( "<STR_LIT>" + res . message ) <EOL> cost = <NUM_LIT:2> * res . cost <EOL> popt = res . x <EOL> _ , s , VT = svd ( res . jac , full_matrices = False ) <EOL> threshold = np . finfo ( float ) . eps * max ( res . jac . shape ) * s [ <NUM_LIT:0> ] <EOL> s = s [ s > threshold ] <EOL> VT = VT [ : s . size ] <EOL> pcov = np . dot ( VT . T / s ** <NUM_LIT:2> , VT ) <EOL> return_full = False <EOL> warn_cov = False <EOL> if pcov is None : <EOL> pcov = zeros ( ( len ( popt ) , len ( popt ) ) , dtype = float ) <EOL> pcov . fill ( inf ) <EOL> warn_cov = True <EOL> elif not absolute_sigma : <EOL> if ydata . size > p0 . size : <EOL> s_sq = cost / ( ydata . size - p0 . size ) <EOL> pcov = pcov * s_sq <EOL> else : <EOL> pcov . fill ( inf ) <EOL> warn_cov = True <EOL> if warn_cov : <EOL> warnings . warn ( '<STR_LIT>' , <EOL> category = OptimizeWarning ) <EOL> if return_full : <EOL> return popt , pcov , infodict , errmsg , ier <EOL> else : <EOL> return popt , pcov <EOL> def check_gradient ( fcn , Dfcn , x0 , args = ( ) , col_deriv = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> x = atleast_1d ( x0 ) <EOL> n = len ( x ) <EOL> x = x . reshape ( ( n , ) ) <EOL> fvec = atleast_1d ( fcn ( x , * args ) ) <EOL> m = len ( fvec ) <EOL> fvec = fvec . reshape ( ( m , ) ) <EOL> ldfjac = m <EOL> fjac = atleast_1d ( Dfcn ( x , * args ) ) <EOL> fjac = fjac . reshape ( ( m , n ) ) <EOL> if col_deriv == <NUM_LIT:0> : <EOL> fjac = transpose ( fjac ) <EOL> xp = zeros ( ( n , ) , float ) <EOL> err = zeros ( ( m , ) , float ) <EOL> fvecp = None <EOL> _minpack . _chkder ( m , n , x , fvec , fjac , ldfjac , xp , fvecp , <NUM_LIT:1> , err ) <EOL> fvecp = atleast_1d ( fcn ( xp , * args ) ) <EOL> fvecp = fvecp . reshape ( ( m , ) ) <EOL> _minpack . _chkder ( m , n , x , fvec , fjac , ldfjac , xp , fvecp , <NUM_LIT:2> , err ) <EOL> good = ( product ( greater ( err , <NUM_LIT:0.5> ) , axis = <NUM_LIT:0> ) ) <EOL> return ( good , err ) <EOL> def _del2 ( p0 , p1 , d ) : <EOL> return p0 - np . square ( p1 - p0 ) / d <EOL> def _relerr ( actual , desired ) : <EOL> return ( actual - desired ) / desired <EOL> def _fixed_point_helper ( func , x0 , args , xtol , maxiter , use_accel ) : <EOL> p0 = x0 <EOL> for i in range ( maxiter ) : <EOL> p1 = func ( p0 , * args ) <EOL> if use_accel : <EOL> p2 = func ( p1 , * args ) <EOL> d = p2 - <NUM_LIT> * p1 + p0 <EOL> p = _lazywhere ( d != <NUM_LIT:0> , ( p0 , p1 , d ) , f = _del2 , fillvalue = p2 ) <EOL> else : <EOL> p = p1 <EOL> relerr = _lazywhere ( p0 != <NUM_LIT:0> , ( p , p0 ) , f = _relerr , fillvalue = p ) <EOL> if np . all ( np . abs ( relerr ) < xtol ) : <EOL> return p <EOL> p0 = p <EOL> msg = "<STR_LIT>" % ( maxiter , p ) <EOL> raise RuntimeError ( msg ) <EOL> def fixed_point ( func , x0 , args = ( ) , xtol = <NUM_LIT> , maxiter = <NUM_LIT> , method = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> use_accel = { '<STR_LIT>' : True , '<STR_LIT>' : False } [ method ] <EOL> x0 = _asarray_validated ( x0 , as_inexact = True ) <EOL> return _fixed_point_helper ( func , x0 , args , xtol , maxiter , use_accel ) </s>
