L iddlZddlZddlZddlZddlmZmZmZddlm Z m Z m Z m Z m Z mZddlZddlZddlmZddlmZddlmZddlmZmZdd lmZdd lmZgd ZGd d eZdZ dZ!dZ"dZ#e$djKdjKZ&dZ' d;dZ(e'e( dy)=N)asarraydotvdot)normsolveinvqrsvd LinAlgError)get_blas_funcs)copy_if_needed)getfullargspec_no_self)scalar_search_wolfe1scalar_search_armijo) signature)get_close_matches) broyden1broyden2anderson linearmixing diagbroydenexcitingmixing newton_krylov BroydenFirstKrylovJacobianInverseJacobian NoConvergenceceZdZdZy)rz\Exception raised when nonlinear solver fails to converge within the specified `maxiter`.N)__name__ __module__ __qualname____doc__\/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/optimize/_nonlin.pyrr s r%rcHtj|jSN)npabsolutemaxxs r&maxnormr.&s ;;q>   r%ct|}tj|jtjst|tj S|S)z:Return `x` as an array, of either floats or complex floatsdtype)rr) issubdtyper1inexactfloat64r,s r& _as_inexactr5*s7 A =="** -q ++ Hr%ctj|tj|}t|d|j}||S)z;Return ndarray `x` as same array subclass and shape as `x0`__array_wrap__)r)reshapeshapegetattrr7)r-x0wraps r& _array_liker=2s8 1bhhrl#A 2')9)9 :D 7Nr%ctj|js#tjtjSt |Sr()r)isfiniteallarrayinfr)vs r& _safe_normrD9s2 ;;q>   xx 7Nr%z F : function(x) -> f Function whose root to find; should take and return an array-like object. xin : array_like Initial guess for the solution a iter : int, optional Number of iterations to make. If omitted (default), make as many as required to meet tolerances. verbose : bool, optional Print status to stdout on every iteration. maxiter : int, optional Maximum number of iterations to make. If more are needed to meet convergence, `NoConvergence` is raised. f_tol : float, optional Absolute tolerance (in max-norm) for the residual. If omitted, default is 6e-6. f_rtol : float, optional Relative tolerance for the residual. If omitted, not used. x_tol : float, optional Absolute minimum step size, as determined from the Jacobian approximation. If the step size is smaller than this, optimization is terminated as successful. If omitted, not used. x_rtol : float, optional Relative minimum step size. If omitted, not used. tol_norm : function(vector) -> scalar, optional Norm to use in convergence check. Default is the maximum norm. line_search : {None, 'armijo' (default), 'wolfe'}, optional Which type of a line search to use to determine the step size in the direction given by the Jacobian approximation. Defaults to 'armijo'. callback : function, optional Optional callback function. It is called on every iteration as ``callback(x, f)`` where `x` is the current solution and `f` the corresponding residual. Returns ------- sol : ndarray An array (of similar array type as `x0`) containing the final solution. Raises ------ NoConvergence When a solution was not found. ) params_basic params_extracN|jr|jtz|_yyr()r# _doc_parts)objs r&_set_docrJws {{kkJ. r%c | tn| } t|||| || }tfd}j}t j |tj }||}t|}t|}|j|j|||||dz}nd|jdzz}| durd} n| durd} | d vr td d }d }d }d}t|D]H}|j|||}|rnIt|||z}|j!|| }t|dk(r td| rt#||||| \}}}}nd}||z}||}t|}|j%|j|| r | ||||dzz|dzz }||dzz|kr t||}nt|t'|||dzz}|}|st(j*j-|d| |dd|ddt(j*j/K|rt1t3|d}| r)|j4|||dk(ddd|d}t3||fSt3|S)a Find a root of a function, in a way suitable for large-scale problems. Parameters ---------- %(params_basic)s jacobian : Jacobian A Jacobian approximation: `Jacobian` object or something that `asjacobian` can transform to one. Alternatively, a string specifying which of the builtin Jacobian approximations to use: krylov, broyden1, broyden2, anderson diagbroyden, linearmixing, excitingmixing %(params_extra)s full_output : bool If true, returns a dictionary `info` containing convergence information. raise_exception : bool If True, a `NoConvergence` exception is raise if no solution is found. See Also -------- asjacobian, Jacobian Notes ----- This algorithm implements the inexact Newton method, with backtracking or full line searches. Several Jacobian approximations are available, including Krylov and Quasi-Newton methods. References ---------- .. [KIM] C. T. Kelley, "Iterative Methods for Linear and Nonlinear Equations". Society for Industrial and Applied Mathematics. (1995) https://archive.siam.org/books/kelley/fr16/ N)f_tolf_rtolx_tolx_rtoliterrcVtt|jSr()r5r=flatten)zFr;s r&funcznonlin_solve..funcs#1[B/0199;;r%rdTarmijoF)NrWwolfezInvalid line searchg?gH.?g?gMbP?)tolrz[Jacobian inversion yielded zero vector. This indicates a bug in the Jacobian approximation.?z : |F(x)| = gz; step  z0A solution was found at the specified tolerance.z:The maximum number of iterations allowed has been reached.)rr[)nitfunstatussuccessmessage)r.TerminationConditionr5rRr) full_likerBr asjacobiansetupcopysize ValueErrorrangecheckminr_nonlin_line_searchupdater+sysstdoutwriteflushrr= iteration) rTr;jacobianrPverbosemaxiterrLrMrNrOtol_norm line_searchcallback full_outputraise_exception conditionrUr-dxFxFx_normgammaeta_max eta_tresholdetanr`rYs Fx_norm_neweta_Ainfos `` r& nonlin_solver|sZ#*wH$5+0*.X?I RB< A a B aB2hG(#H NN1668R&  QhG166!8nGd   33.// EGL C 7^.Q+  #s7{#nnRSn) ) 8q=./ / $7aR8C%E !Aq"kABAaBr(K"%  QO Q&!3 36>L (gu%Cgs5%Q,78C  JJ  s,x|A.>gaU"M N JJ   S.V  Ar 23 3F ** !Q; , 3% & 1b!4''1b!!r%cl dg|gt|dzgttz d fd  fd}|dk(rt |dd|\}} } n|dk(rt dd | \}} d }|zz|dk(rd}n}t|} ||| fS) Nrr[c| dk(rdS |zz}|}t|dz}|r| d<|d<|d<|S)Nrr[)rD) rstorextrCpr}rUtmp_Fxtmp_phitmp_sr-s r&phiz _nonlin_line_search..phis_ a=1:  2X H qM1  E!HGAJF1Ir%c^t|zdzz}||zd|z |z S)NrF)r)abs)rdsrrdiffs_norms r&derphiz#_nonlin_line_search..derphi$s9!fvo!U *AbD&Q/255r%rX{Gz?)xtolaminrW)rrZ)T)rrr)rUr-r~r} search_typersminrrphi1phi0rrrrrrs`` ` ` @@@@@r&rmrms CETFBx{mG !WtBx F  6g,S&'!*26TC 4  &sGAJ ,024 y  AbDAE!H} AY !W2hG aW r%c*eZdZdZdddddefdZdZy)rcz Termination condition for an iteration. It is terminated if - |F| < f_rtol*|F_0|, AND - |F| < f_tol AND - |dx| < x_rtol*|x|, AND - |dx| < x_tol NcD|0tjtjjdz}|tj}|tj}|tj}||_||_||_||_||_ ||_ d|_ d|_ y)NgUUUUUU?r) r)finfor4epsrBrNrOrLrMrrPf0_normrs)selfrLrMrNrOrPrs r&__init__zTerminationCondition.__init__Ks =HHRZZ(,,6E >VVF =FFE >VVF       r%c|xjdz c_|j|}|j|}|j|}|j||_|dk(ry|jd|j|jkDzSt ||j kxr||j z |jkxr#||jkxr||jz |kS)Nrrr[) rsrrrPintrLrMrNrO)rfr-r}f_normx_normdx_norms r&rkzTerminationCondition.checkcs !11))B- << !DL Q; 99 23 3Fdjj(;t{{*dll:;4::-:#DKK/69< s! "$d40.__array__s'$$'A%%IJJ||~%r%NN)itemsrisetattrhasattr)rkwnamesnamevaluers r&rzJacobian.__init__si888: .KD%5  #>  8>> 1 KK1  2r%Nr) r r!r"r#rrrrnrfr$r%r&rr~s!#J& %" r%rc6eZdZdZdZedZedZy)ra A simple wrapper that inverts the Jacobian using the `solve` method. .. legacy:: class See the newer, more consistent interfaces in :mod:`scipy.optimize`. Parameters ---------- jacobian : Jacobian The Jacobian to invert. Attributes ---------- shape Matrix dimensions (M, N) dtype Data type of the matrix. c||_|j|_|j|_t |dr|j |_t |dr|j |_yy)Nrfr)rtrrrnrrfrr)rrts r&rzInverseJacobian.__init__sN  nn oo 8W %!DJ 8X &#??DL 'r%c.|jjSr()rtr9rs r&r9zInverseJacobian.shape}}"""r%c.|jjSr()rtr1rs r&r1zInverseJacobian.dtyperr%N)r r!r"r#rpropertyr9r1r$r%r&rrs4(+####r%rc 0tjjjt t rSt jrtt rSt tjrjdkDr tdtjtjjdjdk7r tdt fdfddfd dfd j j Stjj#r_jdjdk7r td t fd fd dfd dfd j j St%dr{t%drot%drct t'dt'dj(t'dt'dt'dj jSt+rGfddt }|St t,r6t/t0t2t4t6t8t:t<St?d)zE Convert given object to one suitable for use as a Jacobian. r[zarray must have rank <= 2rrzarray must be squarect|Sr()rrCJs r&zasjacobian..s Qr%cLtjj|Sr()rconjTrs r&rzasjacobian..s#affhjj!*<r%ct|Sr()rrCrYrs r&rzasjacobian..s uQ{r%cLtjj|Sr()rrrrs r&rzasjacobian..saffhjj!0Dr%)rrrrr1r9zmatrix must be squarec|zSr(r$rs r&rzasjacobian..s Qr%c>jj|zSr(rrrs r&rzasjacobian..s!&&(**q.r%c|Sr(r$rCrYrspsolves r&rzasjacobian..s wq!}r%cFjj|Sr(rrs r&rzasjacobian..s A0Fr%r9r1rrrrrnrf)rrrrrnrfr1r9cDeZdZdZdfd ZfdZdfd ZfdZy)asjacobian..Jacc||_yr(r,rs r&rnzasjacobian..Jac.updates r%c|j}t|tjr t ||St j j|r ||StdNzUnknown matrix type) r- isinstancer)ndarrayrscipysparseissparserirrCrYmrrs r&rzasjacobian..Jac.solvesTdffIa, A;&\\**1-"1a=($%:;;r%c|j}t|tjr t ||St j j|r||zStdr) r-rr)rrrrrrirrCrrs r&rzasjacobian..Jac.matvec"sQdffIa,q!9$\\**1-q5L$%:;;r%c:|j}t|tjr$t |j j |Stjj|r!|j j |Stdr) r-rr)rrrrrrrrirs r&rzasjacobian..Jac.rsolve+sjdffIa, Q//\\**1-"1668::q11$%:;;r%c2|j}t|tjr$t |j j |Stjj|r|j j |zStdr) r-rr)rrrrrrrrirs r&rzasjacobian..Jac.rmatvec4sgdffIa,qvvxzz1--\\**1-668::>)$%:;;r%Nr)r r!r"rnrrrr)rrsr&Jacrs  < < < >r%ceZdZdZdZdZy)GenericBroydenctj||||||_||_t |drC|j 6t |}|r!dtt |dz|z |_yd|_yyy)Nalpha?rrZ)rrflast_flast_xrr rr+)rr;f0rUnormf0s r&rfzGenericBroyden.setupNsntRT*  4 !djj&8"XF T"Xq!11F:   '9 !r%ctr(rrr-rr}dfrdf_norms r&_updatezGenericBroyden._update\rr%c ||jz }||jz }|j||||t|t|||_||_yr()r r rr)rr-rrr}s r&rnzGenericBroyden.update_sH _ _ Q2r48T"X6  r%N)r r!r"rfrrnr$r%r&rrMs !"r%rczeZdZdZdZedZedZdZdZ ddZ ddZ d Z dd Z d Zd ZdZddZy ) LowRankMatrixz A matrix represented as .. math:: \alpha I + \sum_{n=0}^{n=M} c_n d_n^\dagger However, if the rank of the matrix reaches the dimension of the vectors, full matrix representation will be used thereon. cX||_g|_g|_||_||_d|_yr()r csrrr1 collapsed)rr rr1s r&rzLowRankMatrix.__init__rs,  r%ctgd|dd|gz\}}}||z}t||D]#\}} || |} ||||j| }%|S)N)axpyscaldotcr)r ziprh) rCr rrrrrwcdas r&_matveczLowRankMatrix._matveczsm)*B*,Ra&A3,8dD AIBK &DAqQ AQ1661%A &r%c Jt|dk(r||z Stddg|dd|gz\}}|d}|tjt||jz}t |D].\}} t |D]\} } ||| fxx|| | z cc<0tj t||j} t |D]\} } || || | <| |z} t|| } ||z } t|| D]\} }|| | | j| } | S)Evaluate w = M^-1 vrrrNrr0) lenr r)identityr1 enumeratezerosrrrh)rCr rrrrc0Air"jr!qr qcs r&_solvezLowRankMatrix._solves: r7a<U7N$VV$4b!fslC d U BKKBrxx8 8bM %DAq!"  %1!A#$q!*$ % % HHSWBHH -bM DAq1:AaD  U  !QK eGQZ (EArQ166B3'A (r%c|j tj|j|Stj ||j |j |jS)zEvaluate w = M v)rr)rrr$r rrrrCs r&rzLowRankMatrix.matvecsD >> %66$..!, ,$$Q DGGTWWEEr%c|j8tj|jjj |St j |tj|j|j|jS)zEvaluate w = M^H v) rr)rrrrr$r rrr3s r&rzLowRankMatrix.rmatvecs\ >> %66$..**//115 5$$Q (;TWWdggNNr%c|jt|j|Stj||j|j |j S)r&)rrrr1r rrrs r&rzLowRankMatrix.solves@ >> %+ +##Atzz477DGGDDr%c|j.t|jjj|Stj |t j|j|j|jS)zEvaluate w = M^-H v) rrrrrr1r)r rrrs r&rzLowRankMatrix.rsolvesX >> %))..0!4 4##Arwwtzz':DGGTWWMMr%cX|j5|xj|dddf|dddfjzz c_y|jj||jj|t |j|j kDr|jyyr()rrrappendrr'rhcollapse)rr!r"s r&r8zLowRankMatrix.appends{ >> % NNa$i!DF)..*:: :N  q q tww~O%& (   MM99=nN%& ( >> %>> ! ZZ DFF$**= =) -DAq !AdF)Ad1fINN,, ,B - r%cntj|t|_d|_d|_d|_y)z0Collapse the low-rank matrix to a full-rank one.)rgN)r)rAr rrrr rs r&r9zLowRankMatrix.collapses)$^< r%c|jy|dkDsJt|j|kDr|jdd=|jdd=yy)zH Reduce the rank of the matrix by dropping all vectors. Nrrr'rrrranks r&restart_reducezLowRankMatrix.restart_reducesG >> % axx tww<$    r%c|jy|dkDsJt|j|kDr4|jd=|jd=t|j|kDr3yy)zK Reduce the rank of the matrix by dropping oldest vectors. NrrBrCs r& simple_reducezLowRankMatrix.simple_reducesT >> % axx$''lT!  $''lT!r%c|jy|}||}n|dz }|jr"t|t|jd}t dt||dz }t|j}||kryt j |jj}t j |jj}t|d\}}t||jj}t|d\} } } t|t| }t|| jj}t|D]J} |dd| fj|j| <|dd| fj|j| <L|j|d=|j|d=y) a Reduce the rank of the matrix by retaining some SVD components. This corresponds to the "Broyden Rank Reduction Inverse" algorithm described in [1]_. Note that the SVD decomposition can be done by solving only a problem whose size is the effective rank of this matrix, which is viable even for large problems. Parameters ---------- max_rank : int Maximum rank of this matrix after reduction. to_retain : int, optional Number of SVD components to retain when reduction is done (ie. rank > max_rank). Default is ``max_rank - 2``. References ---------- .. [1] B.A. van der Rotten, PhD thesis, "A limited memory Broyden method to solve high-dimensional systems of nonlinear equations". Mathematisch Instituut, Universiteit Leiden, The Netherlands (2003). https://web.archive.org/web/20161022015821/http://www.math.leidenuniv.nl/scripties/Rotten.pdf Nr[rreconomic)modeF) full_matrices)rrrlr'r+r)rArrr rrr rrjrg) rmax_rank to_retainrr/rCDRUSWHks r& svd_reducezLowRankMatrix.svd_reducesh: >> %    AAA 77As4771:'A 3q!A#;  L q5  HHTWW    HHTWW   !*%1 13388: q.1b 3r7O 24499; q 'A1Q3DGGAJ1Q3DGGAJ ' GGABK GGABKr%rrr()r r!r"r#r staticmethodr$r1rrrrr8rr9rErGrUr$r%r&rrgsj6F O E N "  ?r%ra alpha : float, optional Initial guess for the Jacobian is ``(-1/alpha)``. reduction_method : str or tuple, optional Method used in ensuring that the rank of the Broyden matrix stays low. Can either be a string giving the name of the method, or a tuple of the form ``(method, param1, param2, ...)`` that gives the name of the method and values for additional parameters. Methods available: - ``restart``: drop all matrix columns. Has no extra parameters. - ``simple``: drop oldest matrix column. Has no extra parameters. - ``svd``: keep only the most significant SVD components. Takes an extra parameter, ``to_retain``, which determines the number of SVD components to retain when rank reduction is done. Default is ``max_rank - 2``. max_rank : int, optional Maximum rank for the Broyden matrix. Default is infinity (i.e., no rank reduction). broyden_paramscFeZdZdZd dZdZdZd dZdZd dZ d Z d Z y) ral Find a root of a function, using Broyden's first Jacobian approximation. This method is also known as "Broyden's good method". Parameters ---------- %(params_basic)s %(broyden_params)s %(params_extra)s See Also -------- root : Interface to root finding algorithms for multivariate functions. See ``method='broyden1'`` in particular. Notes ----- This algorithm implements the inverse Jacobian Quasi-Newton update .. math:: H_+ = H + (dx - H df) dx^\dagger H / ( dx^\dagger H df) which corresponds to Broyden's first Jacobian update .. math:: J_+ = J + (df - J dx) dx^\dagger / dx^\dagger dx References ---------- .. [1] B.A. van der Rotten, PhD thesis, "A limited memory Broyden method to solve high-dimensional systems of nonlinear equations". Mathematisch Instituut, Universiteit Leiden, The Netherlands (2003). https://math.leidenuniv.nl/scripties/Rotten.pdf Examples -------- The following functions define a system of nonlinear equations >>> def fun(x): ... return [x[0] + 0.5 * (x[0] - x[1])**3 - 1.0, ... 0.5 * (x[1] - x[0])**3 + x[1]] A solution can be obtained as follows. >>> from scipy import optimize >>> sol = optimize.broyden1(fun, [0, 0]) >>> sol array([0.84116396, 0.15883641]) NcNtj|_d_|tj }|_t|trdn |dd|d}|dz fz|dk(r fd_ y|dk(r fd_ y|dk(r fd _ ytd |d ) Nr$rrr c6jjSr()r?rU reduce_paramsrsr&rz'BroydenFirst.__init__..s#5477#5#5}#Er%simplec6jjSr()r?rGr[sr&rz'BroydenFirst.__init__..s#8477#8#8-#Hr%restartc6jjSr()r?rEr[sr&rz'BroydenFirst.__init__..s#9477#9#9=#Ir%zUnknown rank reduction method '') rrr r?r)rBrLrr_reduceri)rr reduction_methodrLr\s` @r&rzBroydenFirst.__init__s%   vvH  & ,M,QR0M/2 !A-7 u $EDL  )HDL  *IDL>?O>PPQRS Sr%ctj||||t|j |jd|j |_y)Nr)rrfrr r9r1r?rs r&rfzBroydenFirst.setups8T1a. TZZ]DJJGr%c,t|jSr()rr?rs r&rzBroydenFirst.todenses477|r%c|jj|}tj|j sL|j |j |j|j|jj|S|Sr() r?rr)r?r@rfr r rU)rrrYrs r&rzBroydenFirst.solves\ GGNN1 {{1~!!# JJt{{DKK ;77>>!$ $r%c8|jj|Sr()r?rrrs r&rzBroydenFirst.matvecsww}}Qr%c8|jj|Sr()r?rrrrYs r&rzBroydenFirst.rsolveswwq!!r%c8|jj|Sr()r?rris r&rzBroydenFirst.rmatvecsww~~a  r%c|j|jj|}||jj|z }|t ||z } |jj || yr()rbr?rrrr8 rr-rr}rrrrCr!r"s r&rzBroydenFirst._updatesU  GGOOB  # # R O q!r%)Nr_Nr) r r!r"r#rrfrrrrrrr$r%r&rrKs22hT2H "!r%rceZdZdZdZy)raK Find a root of a function, using Broyden's second Jacobian approximation. This method is also known as "Broyden's bad method". Parameters ---------- %(params_basic)s %(broyden_params)s %(params_extra)s See Also -------- root : Interface to root finding algorithms for multivariate functions. See ``method='broyden2'`` in particular. Notes ----- This algorithm implements the inverse Jacobian Quasi-Newton update .. math:: H_+ = H + (dx - H df) df^\dagger / ( df^\dagger df) corresponding to Broyden's second method. References ---------- .. [1] B.A. van der Rotten, PhD thesis, "A limited memory Broyden method to solve high-dimensional systems of nonlinear equations". Mathematisch Instituut, Universiteit Leiden, The Netherlands (2003). https://web.archive.org/web/20161022015821/http://www.math.leidenuniv.nl/scripties/Rotten.pdf Examples -------- The following functions define a system of nonlinear equations >>> def fun(x): ... return [x[0] + 0.5 * (x[0] - x[1])**3 - 1.0, ... 0.5 * (x[1] - x[0])**3 + x[1]] A solution can be obtained as follows. >>> from scipy import optimize >>> sol = optimize.broyden2(fun, [0, 0]) >>> sol array([0.84116365, 0.15883529]) c|j|}||jj|z }||dzz } |jj|| yNr[)rbr?rr8rns r&rzBroydenSecond._updatesF   # #  N q!r%N)r r!r"r#rr$r%r&rrs 0dr%rc,eZdZdZddZddZdZdZy) ra Find a root of a function, using (extended) Anderson mixing. The Jacobian is formed by for a 'best' solution in the space spanned by last `M` vectors. As a result, only a MxM matrix inversions and MxN multiplications are required. [Ey]_ Parameters ---------- %(params_basic)s alpha : float, optional Initial guess for the Jacobian is (-1/alpha). M : float, optional Number of previous vectors to retain. Defaults to 5. w0 : float, optional Regularization parameter for numerical stability. Compared to unity, good values of the order of 0.01. %(params_extra)s See Also -------- root : Interface to root finding algorithms for multivariate functions. See ``method='anderson'`` in particular. References ---------- .. [Ey] V. Eyert, J. Comp. Phys., 124, 271 (1996). Examples -------- The following functions define a system of nonlinear equations >>> def fun(x): ... return [x[0] + 0.5 * (x[0] - x[1])**3 - 1.0, ... 0.5 * (x[1] - x[0])**3 + x[1]] A solution can be obtained as follows. >>> from scipy import optimize >>> sol = optimize.anderson(fun, [0, 0]) >>> sol array([0.84116588, 0.15883789]) Nctj|||_||_g|_g|_d|_||_yr()rrr Mr}rrw0)rr rurts r&rzAnderson.__init__Bs:%  r%c|j |z}t|j}|dk(r|Stj||j }t |D]}t|j||||<  t|j|}t |D]7}||||j||j|j|zzzz }9|S#t$r#|jdd=|jdd=|cYSwxYwNrr0) r r'r}r)emptyr1rjrrrr#r ) rrrYr}rdf_frTrrs r&rzAnderson.solveKsjj[] L 6Ixx)q *A4771:q)DG * $&&$'Eq @A %(DGGAJDGGAJ)>>? ?B @    I  s;C)DDc B| |jz }t|j}|dk(r|Stj||j }t |D]}t|j||||< tj||f|j }t |D]}t |D]}t|j||j||||f<||k(s4|jdk7sD|||fxxt|j||j||jdzz|jzzcc<t||} t |D]7} || | |j| |j| |jz zzz }9|S)Nrr0r[) r r'r}r)rxr1rjrrrur) rrr}rryrTbr-r.rrs r&rzAnderson.matvecbsyR ] L 6Ixx)q *A4771:q)DG * HHaV177 +q QA1X Qdggaj$''!*5!A#6dgglacFd4771:twwqz:477A:EdjjPPF Q Q aq @A %(DGGAJDJJ)>>? ?B @ r%c8|jdk(ry|jj||jj|t |j|jkDrY|jj d|jj dt |j|jkDrYt |j}t j||f|j}t|D][} t| |D]J} | | k(r|jdz} nd} d| zt|j| |j| z|| | f<L]|t j|djjz }||_y)Nrr0r[r)rtr}r8rr'popr)r*r1rjrurtriurrr#) rr-rr}rrrrr#r-r.wds r&rzAnderson._updateys7 66Q;  r r$''lTVV# GGKKN GGKKN$''lTVV# L HHaV177 +q =A1a[ =6!BBB$TWWQZ <<!A#  = = RWWQ]__ ! ! ##r%)Nrr)r r!r"r#rrrrr$r%r&rrs+L..r%rcFeZdZdZd dZdZd dZdZd dZdZ d Z d Z y) ra, Find a root of a function, using diagonal Broyden Jacobian approximation. The Jacobian approximation is derived from previous iterations, by retaining only the diagonal of Broyden matrices. .. warning:: This algorithm may be useful for specific problems, but whether it will work may depend strongly on the problem. Parameters ---------- %(params_basic)s alpha : float, optional Initial guess for the Jacobian is (-1/alpha). %(params_extra)s See Also -------- root : Interface to root finding algorithms for multivariate functions. See ``method='diagbroyden'`` in particular. Examples -------- The following functions define a system of nonlinear equations >>> def fun(x): ... return [x[0] + 0.5 * (x[0] - x[1])**3 - 1.0, ... 0.5 * (x[1] - x[0])**3 + x[1]] A solution can be obtained as follows. >>> from scipy import optimize >>> sol = optimize.diagbroyden(fun, [0, 0]) >>> sol array([0.84116403, 0.15883384]) Nc<tj|||_yr(rrr rr s r&rzDiagBroyden.__init__% r%ctj||||tj|jdfd|j z |j |_y)Nrrr0)rrfr)fullr9r r1r"rs r&rfzDiagBroyden.setupsAT1a.$**Q-)1tzz>Lr%c"| |jz Sr(r"rks r&rzDiagBroyden.solverDFF{r%c"| |jzSr(rris r&rzDiagBroyden.matvecrr%c>| |jjz Sr(r"rrks r&rzDiagBroyden.rsolverDFFKKM!!r%c>| |jjzSr(rris r&rzDiagBroyden.rmatvecrr%cBtj|j Sr()r)diagr"rs r&rzDiagBroyden.todenseswwwr%c`|xj||j|zz|z|dzz zc_yrqrrs r&rzDiagBroyden._updates* 2r >2%gqj00r%r(r r r!r"r#rrfrrrrrrr$r%r&rrs1&PM"" 1r%rc@eZdZdZd dZd dZdZd dZdZdZ d Z y) ra Find a root of a function, using a scalar Jacobian approximation. .. warning:: This algorithm may be useful for specific problems, but whether it will work may depend strongly on the problem. Parameters ---------- %(params_basic)s alpha : float, optional The Jacobian approximation is (-1/alpha). %(params_extra)s See Also -------- root : Interface to root finding algorithms for multivariate functions. See ``method='linearmixing'`` in particular. Nc<tj|||_yr(rrs r&rzLinearMixing.__init__rr%c"| |jzSr(r rks r&rzLinearMixing.solver$**}r%c"| |jz Sr(rris r&rzLinearMixing.matvecrr%cH| tj|jzSr(r)rr rks r&rzLinearMixing.rsolver"''$**%%%r%cH| tj|jz Sr(rris r&rzLinearMixing.rmatvecrr%ctjtj|jdd|jz S)Nr)r)rrr9r rs r&rzLinearMixing.todenses,wwrwwtzz!}bm<==r%cyr(r$rs r&rzLinearMixing._updaterr%r(r) r r!r"r#rrrrrrrr$r%r&rrs*,&&> r%rcFeZdZdZd dZdZd dZdZd dZdZ d Z d Z y) ra Find a root of a function, using a tuned diagonal Jacobian approximation. The Jacobian matrix is diagonal and is tuned on each iteration. .. warning:: This algorithm may be useful for specific problems, but whether it will work may depend strongly on the problem. See Also -------- root : Interface to root finding algorithms for multivariate functions. See ``method='excitingmixing'`` in particular. Parameters ---------- %(params_basic)s alpha : float, optional Initial Jacobian approximation is (-1/alpha). alphamax : float, optional The entries of the diagonal Jacobian are kept in the range ``[alpha, alphamax]``. %(params_extra)s NcXtj|||_||_d|_yr()rrr alphamaxbeta)rr rs r&rzExcitingMixing.__init__$s%%    r%ctj||||tj|jdf|j |j |_yrw)rrfr)rr9r r1rrs r&rfzExcitingMixing.setup*s=T1a.GGTZZ],djj K r%c"| |jzSr(rrks r&rzExcitingMixing.solve.r$))|r%c"| |jz Sr(rris r&rzExcitingMixing.matvec1rr%c>| |jjzSr(rrrks r&rzExcitingMixing.rsolve4r$)).."""r%c>| |jjz Sr(rris r&rzExcitingMixing.rmatvec7rr%cFtjd|jz S)Nr)r)rrrs r&rzExcitingMixing.todense:swwr$))|$$r%c ||jzdkD}|j|xx|jz cc<|j|j|<tj|jd|j |jy)Nr)out)r rr r)clipr)rr-rr}rrrincrs r&rzExcitingMixing._update=s\}q  $4::%:: 4%  1dmm;r%)NrZrrr$r%r&rr s04 L##%>> from scipy.optimize import BroydenFirst, KrylovJacobian >>> from scipy.optimize import InverseJacobian >>> jac = BroydenFirst() >>> kjac = KrylovJacobian(inner_M=InverseJacobian(jac)) If the preconditioner has a method named 'update', it will be called as ``update(x, f)`` after each nonlinear step, with ``x`` giving the current point, and ``f`` the current function value. outer_k : int, optional Size of the subspace kept across LGMRES nonlinear iterations. See `scipy.sparse.linalg.lgmres` for details. inner_kwargs : kwargs Keyword parameters for the "inner" Krylov solver (defined with `method`). Parameter names must start with the `inner_` prefix which will be stripped before passing on the inner method. See, e.g., `scipy.sparse.linalg.gmres` for details. %(params_extra)s See Also -------- root : Interface to root finding algorithms for multivariate functions. See ``method='krylov'`` in particular. scipy.sparse.linalg.gmres scipy.sparse.linalg.lgmres Notes ----- This function implements a Newton-Krylov solver. The basic idea is to compute the inverse of the Jacobian with an iterative Krylov method. These methods require only evaluating the Jacobian-vector products, which are conveniently approximated by a finite difference: .. math:: J v \approx (f(x + \omega*v/|v|) - f(x)) / \omega Due to the use of iterative matrix inverses, these methods can deal with large nonlinear problems. SciPy's `scipy.sparse.linalg` module offers a selection of Krylov solvers to choose from. The default here is `lgmres`, which is a variant of restarted GMRES iteration that reuses some of the information obtained in the previous Newton steps to invert Jacobians in subsequent steps. For a review on Newton-Krylov methods, see for example [1]_, and for the LGMRES sparse inverse method, see [2]_. References ---------- .. [1] C. T. Kelley, Solving Nonlinear Equations with Newton's Method, SIAM, pp.57-83, 2003. :doi:`10.1137/1.9780898718898.ch3` .. [2] D.A. Knoll and D.E. Keyes, J. Comp. Phys. 193, 357 (2004). :doi:`10.1016/j.jcp.2003.08.010` .. [3] A.H. Baker and E.R. Jessup and T. Manteuffel, SIAM J. Matrix Anal. Appl. 26, 962 (2005). :doi:`10.1137/S0895479803422014` Examples -------- The following functions define a system of nonlinear equations >>> def fun(x): ... return [x[0] + 0.5 * x[1] - 1.0, ... 0.5 * (x[1] - x[0]) ** 2] A solution can be obtained as follows. >>> from scipy import optimize >>> sol = optimize.newton_krylov(fun, [0, 0]) >>> sol array([0.66731771, 0.66536458]) Nc N||_||_ttjj j tjj jtjj jtjj jtjj jtjj jj|||_ t||j|_|jtjj jur<||jd<d|jd<|jjddnR|jtjj j tjj j tjj jfvr|jjddn|jtjj jur||jd<d|jd<|jjd g|jjd d |jjd d |jjddt#|jj$Dcgc]}|dvr| }}|j'D]\} } | j)dst+d| | dd|vrFt-| dd|d} | r d| dd} nd} t/j0d| d|d| zdt2r| |j| dd<ycc}w)N)bicgstabgmreslgmrescgsminrestfqmr)rvrtr_rrvatolrouter_kouter_vprepend_outer_vTstore_outer_AvF)rargskwargsinner_zUnknown parameter )rz Did you mean 'z'?zOption 'z#' is invalid for the inner method: zO. It will be ignored.Please check inner method documentation for valid options.r;)r<category)preconditionerrrrrrrrrrrrgetmethod method_kw setdefaultgcrotmkr parametersr startswithrirr=r> UserWarning) rrr inner_maxiterinner_MrrrTvalid_inner_paramskeyrinner_param_suggestionssuggestion_msgs r&rzKrylovJacobian.__init__s% \\((11,,%%++<<&&-- ##''<<&&--,,%%++ c&&! mt7J7JK ;;%,,--33 3(5DNN9 %()DNN9 % NN % %fa 0 [[U\\0088"\\0099"\\004466 NN % %fa 0 [[ELL//66 6(/DNN9 %()DNN9 % NN % %i 4 NN % %&7 > NN % %&6 > NN % %fa 0!-88 22   ((* ,JC>>(+ #5cU!;<<12w00*;CG?+A'+(7)@)C(DB'HN&(N se#FvhOQQ%% !( &+DNN3qr7 #7 ,  s< N"ct|jj}t|jj}|jtd|ztd|z |_y)Nr)rr;r+rromega)rmxmfs r&_update_diff_stepz KrylovJacobian._update_diff_stepsO \    \   ZZ#a*,s1bz9 r%cft|}|dk(rd|zS|j|z }|j|j||zz|jz |z }t j t j|s3t j t j|r td|S)Nrz$Function returned non-finite results) rrrUr;rr)r@r?ri)rrCnvscrgs r&rzKrylovJacobian.matvecs !W 7Q3J ZZ"_ YYtwwA~ & 0B 6vvbkk!n%"&&Q*@CD Dr%cd|jvr-|j|j|fi|j\}}|S|j|j|fd|i|j\}}|S)Nrtol)rrop)rrhsrYsolrs r&rzKrylovJacobian.solve sg T^^ ## DGGSCDNNCIC $ DGGSMsMdnnMIC r%c||_||_|j|j4t |jdr|jj ||yyy)Nrn)r;rrrrrn)rr-rs r&rnzKrylovJacobian.updatesZ      *t**H5##**1a06 +r%ctj||||||_||_tj j j||_|j1tj|jjdz|_ |j|j5t!|jdr|jj|||yyy)Nr rf)rrfr;rrrraslinearoperatorrrr)rr1rrrr)rr-rrUs r&rfzKrylovJacobian.setupstQ4(,,%%66t< :: !''*..48DJ      *t**G4##))!Q55 +r%)NrN r) r r!r"r#rrrrrnrfr$r%r&rrHs3cJCE')K,Z: 16r%rc Lt|j}|\}}}}}}} tt|t | d|} dj | D cgc] \} } | d| c} } } | rd| z} dj | D cgc] \} } | d| c} } }|r|dz}|rt d|d}|t|| |j|z}i}|jtt||||}|j|_ t||Scc} } wcc} } w)a Construct a solver wrapper with given name and Jacobian approx. It inspects the keyword arguments of ``jac.__init__``, and allows to use the same arguments in the wrapper function, in addition to the keyword arguments of `nonlin_solve` Nz, =zUnexpected signature a def %(name)s(F, xin, iter=None %(kw)s, verbose=False, maxiter=None, f_tol=None, f_rtol=None, x_tol=None, x_rtol=None, tol_norm=None, line_search='armijo', callback=None, **kw): jac = %(jac)s(%(kwkw)s **kw) return nonlin_solve(F, xin, jac, iter, verbose, maxiter, f_tol, f_rtol, x_tol, x_rtol, tol_norm, line_search, callback) )rrjackwkw)_getfullargspecrlistrr'joinrirr rnglobalsexecr#rJ)rrrrvarargsvarkwdefaults kwonlyargs kwdefaults_rrTrCkw_strkwkw_strwrappernsrUs r&_nonlin_wrapperr0s.  -I@I=D'5(J A #dCM>?+X6 7F YY8A1#Qqe 8 9F yy8AQCq*89Hd?0 <==G$6s||"*,,G BIIgi" d8D;;DL TN K;99s D D rrrrrrr) rNFNNNNNNrWNFT)rWg:0yE>r)?rror=numpyr)rrr scipy.linalgrrrr r r scipy.sparse.linalgr scipy.sparser scipy._lib._utilr rr _linesearchrrrdifflibr__all__ Exceptionrr.r5r=rDrstriprHrJrrmrcrrrerrrrrrrrrrrrrrrrrr$r%r&rs $$??'+FC% J I    (P a1 h/ ?DKO?C48O"d FJ!*Z9<9<@EEP$#$#NY?@X4IIX * + 0m>m`9L9@U~UxA1.A1H+ >+ \8<^8<~a6Xa6P)X :| 4 :} 5 :x 0~|< m[9  !1>B@ r%