§
    fŠtjîË  ã                   óü  — d dl Z d dlZd dlZd dlZd dlmZmZmZ d dlm	Z	m
Z
mZmZmZmZ d dlZd dlZd dlmZ d dlmZmZ d dlmZ ddlmZmZ d d	l mZ d d
lmZ d dlmZ g d¢Z  G d„ de!¦  «        Z"d„ Z#d„ Z$d„ Z%d„ Z& e' ed¦  «         (                    ¦   «          ed¦  «         (                    ¦   «         ¬¦  «        Z)d„ Z*	 	 	 	 dBd„Z+ e*e+¦  «         	 	 dCd„Z, G d„ d ¦  «        Z- G d!„ d"¦  «        Z. G d#„ d$¦  «        Z/d%„ Z0 G d&„ d'e.¦  «        Z1 G d(„ d)¦  «        Z2 ed*¦  «         (                    ¦   «         e)d+<    G d,„ d-e1¦  «        Z3 G d.„ d/e3¦  «        Z4 G d0„ d1e1¦  «        Z5 G d2„ d3e1¦  «        Z6 G d4„ d5e1¦  «        Z7 G d6„ d7e1¦  «        Z8 G d8„ d9e.¦  «        Z9d:„ Z: e:d;e3¦  «        Z; e:d<e4¦  «        Z< e:d=e5¦  «        Z= e:d>e7¦  «        Z> e:d?e6¦  «        Z? e:d@e8¦  «        Z@ e:dAe9¦  «        ZAdS )Dé    N)ÚasarrayÚdotÚvdot)ÚnormÚsolveÚinvÚqrÚsvdÚLinAlgError)Úget_blas_funcs)Úcopy_if_neededÚ_dedent_for_py313)Úgetfullargspec_no_selfé   )Úscalar_search_wolfe1Úscalar_search_armijo)Ú	signature)Úget_close_matches)ÚGenericAlias)Úbroyden1Úbroyden2ÚandersonÚlinearmixingÚdiagbroydenÚexcitingmixingÚnewton_krylovÚBroydenFirstÚKrylovJacobianÚInverseJacobianÚNoConvergencec                   ó   — e Zd ZdZdS )r    z\Exception raised when nonlinear solver fails to converge within the specified
    `maxiter`.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© ó    úT/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/scipy/optimize/_nonlin.pyr    r    !   s   € € € € € ðð à€Dr'   r    c                 óN   — t          j        | ¦  «                             ¦   «         S ©N)ÚnpÚabsoluteÚmax©Úxs    r(   Úmaxnormr0   '   s   € ÝŒ;�q‰>Œ>×ÒÑÔÐr'   c                 ó¢   — t          | ¦  «        } t          j        | j        t          j        ¦  «        st          | t          j        ¬¦  «        S | S )z:Return `x` as an array, of either floats or complex floats©Údtype)r   r+   Ú
issubdtyper3   ÚinexactÚfloat64r.   s    r(   Ú_as_inexactr7   +   s?   € å�‰
Œ
€AÝŒ=˜œ¥"¤*Ñ-Ô-ð ,Ý�q¥¤
Ð+Ñ+Ô+Ð+Ø€Hr'   c                 ó’   — t          j        | t          j        |¦  «        ¦  «        } t          |d| j        ¦  «        } || ¦  «        S )z;Return ndarray `x` as same array subclass and shape as `x0`Ú__array_wrap__)r+   ÚreshapeÚshapeÚgetattrr9   )r/   Úx0Úwraps      r(   Ú_array_liker?   3   s=   € å
Œ
�1•b”h˜r‘l”lÑ#Ô#€AÝ�2Ð'¨Ô)9Ñ:Ô:€DØˆ4�‰7Œ7€Nr'   c                 ó¨   — t          j        | ¦  «                             ¦   «         st          j        t           j        ¦  «        S t          | ¦  «        S r*   )r+   ÚisfiniteÚallÚarrayÚinfr   )Úvs    r(   Ú
_safe_normrF   :   s;   € ÝŒ;�q‰>Œ>×ÒÑÔð  ÝŒx�œÑÔÐÝ�‰7Œ7€Nr'   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_extrac                 ó@   — | j         r| j         t          z  | _         d S d S r*   )r%   Ú
_doc_parts)Úobjs    r(   Ú_set_docrL   x   s(   € Ø
„{ð /Ø”k¥JÑ.ˆŒˆˆð/ð /r'   ÚkrylovFÚarmijoTc           
      óÜ  ‡ ‡— |
€t           n|
}
t          ||||	||
¬¦  «        }t          ‰¦  «        Šˆ ˆfd„}‰                     ¦   «         }t	          j        |t          j        ¦  «        } ||¦  «        }t          |¦  «        }t          |¦  «        }| 	                    | 
                    ¦   «         ||¦  «         |€|�|dz   }nd|j        dz   z  }|du rd}n|du rd}|d	vrt          d
¦  «        ‚d}d}d}d}t          |¦  «        D �]‚}|                     |||¦  «        }|r �n†t          |||z  ¦  «        }|                     ||¬¦  «         }t          |¦  «        dk    rt          d¦  «        ‚|rt#          |||||¦  «        \  }}}}n!d}||z   } ||¦  «        }t          |¦  «        }|                     | 
                    ¦   «         |¦  «         |r |||¦  «         ||dz  z  |dz  z  }||dz  z  |k     rt          ||¦  «        }n$t          |t'          |||dz  z  ¦  «        ¦  «        }|}|rQt(          j                             |› d |
|¦  «        d›d|d›d�¦  «         t(          j                             ¦   «          �Œ„|rt1          t3          |‰¦  «        ¦  «        ‚d}|r,|j        |||dk    dddœ|         dœ}t3          |‰¦  «        |fS t3          |‰¦  «        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_tolÚf_rtolÚx_tolÚx_rtolÚiterr   c                 ót   •— t           ‰t          | ‰¦  «        ¦  «        ¦  «                             ¦   «         S r*   )r7   r?   Úflatten)ÚzÚFr=   s    €€r(   Úfuncznonlin_solve.<locals>.func°   s1   ø€ Ý˜1˜1�[¨¨BÑ/Ô/Ñ0Ô0Ñ1Ô1×9Ò9Ñ;Ô;Ð;r'   r   éd   TrN   F)NrN   ÚwolfezInvalid line searchgÍÌÌÌÌÌì?g§èH.ÿï?gš™™™™™¹?gü©ñÒMbP?)Útolr   z[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.)r   r^   )ÚnitÚfunÚstatusÚsuccessÚmessage)r0   ÚTerminationConditionr7   rV   r+   Ú	full_likerD   r   Ú
asjacobianÚsetupÚcopyÚsizeÚ
ValueErrorÚrangeÚcheckÚminr   Ú_nonlin_line_searchÚupdater-   ÚsysÚstdoutÚwriteÚflushr    r?   Ú	iteration) rX   r=   ÚjacobianrT   ÚverboseÚmaxiterrP   rQ   rR   rS   Útol_normÚline_searchÚcallbackÚfull_outputÚraise_exceptionÚ	conditionrY   r/   ÚdxÚFxÚFx_normÚgammaÚeta_maxÚeta_tresholdÚetaÚnrc   r\   ÚsÚFx_norm_newÚeta_AÚinfos    ``                              r(   Únonlin_solverŒ   }   s£  øø€ ðZ #Ð*�wˆw°€HÝ$¨5¸Ø+0¸Ø*.°Xð?ñ ?ô ?€Iõ 
�R‰Œ€Bð<ð <ð <ð <ð <ð <à
�
Š
‰Œ€Aå	Œ�a�œÑ	 Ô	 €BØ	ˆˆa‰Œ€BÝ�2‰hŒh€Gå˜(Ñ#Ô#€HØ‡N‚N�1—6’6‘8”8˜R Ñ&Ô&Ð&à€ØÐØ˜Q‘hˆGˆGà˜1œ6 !™8‘nˆGà�dÐÐØˆˆØ	˜Ð	Ð	ØˆàÐ3Ð3Ð3ÝÐ.Ñ/Ô/Ð/ð €EØ€GØ€LØ
€Cå�7‰^Œ^ð .ñ .ˆØ—’  Q¨Ñ+Ô+ˆØð 	Ø‰Eõ �#�s˜7‘{Ñ#Ô#ˆØ�nŠn˜R SˆnÑ)Ô)Ð)ˆå�‰8Œ8�qŠ=ˆ=Ýð .ñ /ô /ð /ð
 ð 	#Ý$7¸¸aÀÀRØ8Cñ%Eô %EÑ!ˆAˆq�"�k�kð ˆAØ�B‘ˆAØ��a‘”ˆBÝ˜r™(œ(ˆKà�Š˜Ÿš™œ "Ñ%Ô%Ð%àð 	ØˆH�Q˜‰OŒOˆOð ˜ Q™Ñ&¨°!©Ñ3ˆØ�3˜‘6‰>˜LÒ(Ð(Ý�g˜uÑ%Ô%ˆCˆCå�g�s 5¨%°°Q±©,Ñ7Ô7Ñ8Ô8ˆCàˆð ð 	ÝŒJ×Ò ÐMÐM¨x¨x¸©|¬|ÐMÐMÐMÀaÐMÐMÐMÐMÑNÔNÐNÝŒJ×ÒÑÔÐùàð 	Ý¥¨A¨rÑ 2Ô 2Ñ3Ô3Ð3àˆFàð "Ø Ô*ØØ Ø! Qš;ð ,ð 3ðð ð %ô	&ð		ð 	ˆõ ˜1˜bÑ!Ô! 4Ð'Ð'å˜1˜bÑ!Ô!Ð!r'   ç:Œ0âŽyE>ç{®Gáz„?c                 óÆ  ‡ ‡‡‡‡‡‡‡‡— dgŠ|gŠt          |¦  «        dz  gŠt          ‰¦  «        t          ‰¦  «        z  Šdˆˆ ˆˆˆˆfd„	Šˆˆˆfd„}|dk    rt          ‰|‰d         d|¬¦  «        \  }}	}
n)|d	k    r#t          ‰‰d         ‰d          |¬
¦  «        \  }}	|€d}‰|‰z  z   Š|‰d         k    r	‰d         }n ‰ ‰¦  «        }t          |¦  «        }|‰||fS )Nr   r^   Tc                 óœ   •— | ‰	d         k    r‰d         S ‰
| ‰z  z   } ‰|¦  «        }t          |¦  «        dz  }|r| ‰	d<   |‰d<   |‰d<   |S )Nr   r^   )rF   )rˆ   ÚstoreÚxtrE   Úpr€   rY   Útmp_FxÚtmp_phiÚtmp_sr/   s        €€€€€€r(   Úphiz _nonlin_line_search.<locals>.phi  sl   ø€ Ø��a”Š=ˆ=Ø˜1”:ÐØ��2‘‰XˆØˆD�‰HŒHˆÝ�q‰MŒM˜1ÑˆØð 	ØˆE�!‰HØˆG�A‰JØˆF�1‰IØˆr'   c                 ór   •— t          | ¦  «        ‰z   dz   ‰z  } ‰| |z   d¬¦  «         ‰| ¦  «        z
  |z  S )Nr   F)r‘   )Úabs)rˆ   Údsr—   ÚrdiffÚs_norms     €€€r(   Úderphiz#_nonlin_line_search.<locals>.derphi%  sG   ø€ Ý�!‰fŒf�v‰o Ñ! UÑ*ˆØ��A�b‘D Ð&Ñ&Ô&¨¨¨Q©¬Ñ/°2Ñ5Ð5r'   r[   rŽ   )ÚxtolÚaminrN   )rŸ   r]   )T)r   r   r   )rY   r/   r�   r€   Úsearch_typer›   Úsminr�   rˆ   Úphi1Úphi0r‚   r—   rœ   r”   r•   r–   s   `` ` `      @@@@@r(   rp   rp     se  øøøøøøøøø€ àˆC€EØˆT€FÝ�B‰xŒx˜‰{ˆm€GÝ�!‰WŒW•t˜B‘x”xÑ€Fð
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð6ð 6ð 6ð 6ð 6ð 6ð 6ð �gÒÐÝ,¨S°&¸'À!¼*Ø26¸TðCñ Cô C‰ˆˆ4��à	˜Ò	 Ð	 Ý& s¨G°A¬J¸À¼¸Ø,0ð2ñ 2ô 2‰ˆˆ4ð 	€yð ˆà	ˆAˆb‰D‰€AØˆE�!ŒH‚}€}Ø�AŒYˆˆàˆT�!‰WŒWˆÝ�2‰hŒh€Gàˆa��WÐÐr'   c                   ó,   — e Zd ZdZdddddefd„Zd„ ZdS )rf   z±
    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

    Nc                 ó  — |€&t          j        t           j        ¦  «        j        dz  }|€t           j        }|€t           j        }|€t           j        }|| _        || _        || _        || _        || _	        || _
        d | _        d| _        d S )NgUUUUUUÕ?r   )r+   Úfinfor6   ÚepsrD   rR   rS   rP   rQ   r   rT   Úf0_normrv   )ÚselfrP   rQ   rR   rS   rT   r   s          r(   Ú__init__zTerminationCondition.__init__L  s‚   € ð ˆ=Ý”H�RœZÑ(Ô(Ô,°Ñ6ˆEØˆ>Ý”VˆFØˆ=Ý”FˆEØˆ>Ý”VˆFàˆŒ
ØˆŒØˆŒ
ØˆŒàˆŒ	àˆŒ	àˆŒØˆŒˆˆr'   c                 óˆ  — | xj         dz  c_         |                      |¦  «        }|                      |¦  «        }|                      |¦  «        }| j        €|| _        |dk    rdS | j        �d| j         | j        k    z  S t	          || j        k    o+|| j        z  | j        k    o|| j        k    o|| j        z  |k    ¦  «        S )Nr   r   r^   )	rv   r   r¨   rT   ÚintrP   rQ   rR   rS   )r©   Úfr/   r€   Úf_normÚx_normÚdx_norms          r(   rn   zTerminationCondition.checkd  sÎ   € ØˆŒ˜!ÑˆŒØ—’˜1‘”ˆØ—’˜1‘”ˆØ—)’)˜B‘-”-ˆàŒ<ÐØ!ˆDŒLà�QŠ;ˆ;Ø�1àŒ9Ð à˜œ¨¬Ò2Ñ3Ð3õ �F˜dœjÒ(ð ;Ø˜tœ{Ñ*¨d¬lÒ:ð;à 4¤:Ò-ð :Ø# D¤KÑ/°6Ò9ñ<ô <ð 	<r'   )r"   r#   r$   r%   r0   rª   rn   r&   r'   r(   rf   rf   ?  sQ   € € € € € ðð ð "¨$°dÀ4Ø ðð ð ð ð0<ð <ð <ð <ð <r'   rf   c                   óH   — e Zd ZdZ ee¦  «        Zd„ Zd„ Zd	d„Z	d„ Z
d„ ZdS )
ÚJacobiana¦  
    Common interface for Jacobians or Jacobian approximations.

    The optional methods come useful when implementing trust region
    etc., algorithms that often require evaluating transposes of the
    Jacobian.

    Methods
    -------
    solve
        Returns J^-1 * v
    update
        Updates Jacobian to point `x` (where the function has residual `Fx`)

    matvec : optional
        Returns J * v
    rmatvec : optional
        Returns A^H * v
    rsolve : optional
        Returns A^-H * v
    matmat : optional
        Returns A * V, where V is a dense matrix with dimensions (N,K).
    todense : optional
        Form the dense Jacobian matrix. Necessary for dense trust region
        algorithms, and useful for testing.

    Attributes
    ----------
    shape
        Matrix dimensions (M, N)
    dtype
        Data type of the matrix.
    func : callable, optional
        Function the Jacobian corresponds to

    c                 óÌ   — g d¢}|                      ¦   «         D ]4\  }}||vrt          d|› �¦  «        ‚|�t          | |||         ¦  «         Œ5t          | d¦  «        rdd„}d S d S )N)	r   rq   ÚmatvecÚrmatvecÚrsolveÚmatmatÚtodenser;   r3   zUnknown keyword argument r¸   c                 óR   — |�t          d|› �¦  «        ‚|                      ¦   «         S )Nz`dtype` must be None, was )rl   r¸   )r©   r3   rj   s      r(   Ú	__array__z$Jacobian.__init__.<locals>.__array__³  s-   € ØÐ$Ý$Ð%IÀ%Ð%IÐ%IÑJÔJÐJØ—|’|‘~”~Ð%r'   ©NN)Úitemsrl   ÚsetattrÚhasattr)r©   ÚkwÚnamesÚnameÚvaluerº   s         r(   rª   zJacobian.__init__¨  s©   € ð8ð 8ð 8ˆàŸ8š8™:œ:ð 	.ð 	.‰KˆD�%Ø˜5Ð Ð Ý Ð!C¸TÐ!CÐ!CÑDÔDÐDØÐ Ý˜˜d B t¤HÑ-Ô-Ð-øõ �4˜Ñ#Ô#ð 	&ð&ð &ð &ð &ð &ð &ð	&ð 	&r'   c                 ó    — t          | ¦  «        S r*   )r   ©r©   s    r(   ÚaspreconditionerzJacobian.aspreconditioner¸  s   € Ý˜tÑ$Ô$Ð$r'   r   c                 ó   — t           ‚r*   ©ÚNotImplementedError©r©   rE   r\   s      r(   r   zJacobian.solve»  ó   € Ý!Ð!r'   c                 ó   — d S r*   r&   ©r©   r/   rX   s      r(   rq   zJacobian.update¾  ó   € Øˆr'   c                 ó²   — || _         |j        |j        f| _        |j        | _        | j        j        t          j        u r|                      ||¦  «         d S d S r*   )rY   rk   r;   r3   Ú	__class__ri   r²   rq   ©r©   r/   rX   rY   s       r(   ri   zJacobian.setupÁ  sV   € ØˆŒ	Ø”f˜aœfÐ%ˆŒ
Ø”WˆŒ
ØŒ>Ô¥8¤>Ð1Ð1à�KŠK˜˜1ÑÔÐÐÐð 2Ð1r'   N©r   )r"   r#   r$   r%   Úclassmethodr   Ú__class_getitem__rª   rÅ   r   rq   ri   r&   r'   r(   r²   r²     s�   € € € € € ð#ð #ðL $˜ LÑ1Ô1Ðð&ð &ð &ð %ð %ð %ð"ð "ð "ð "ðð ð ðð ð ð ð r'   r²   c                   óZ   — e Zd ZdZ ee¦  «        Zd„ Zed„ ¦   «         Z	ed„ ¦   «         Z
dS )r   a  
    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	          |d¦  «        r|j        | _        t	          |d¦  «        r|j        | _        d S d S )Nri   r¶   )rw   r   r´   rq   r¾   ri   r¶   rµ   )r©   rw   s     r(   rª   zInverseJacobian.__init__ã  s_   € Ø ˆŒØ”nˆŒØ”oˆŒÝ�8˜WÑ%Ô%ð 	(Ø!œˆDŒJÝ�8˜XÑ&Ô&ð 	+Ø#œ?ˆDŒLˆLˆLð	+ð 	+r'   c                 ó   — | j         j        S r*   )rw   r;   rÄ   s    r(   r;   zInverseJacobian.shapeì  ó   € àŒ}Ô"Ð"r'   c                 ó   — | j         j        S r*   )rw   r3   rÄ   s    r(   r3   zInverseJacobian.dtypeð  r×   r'   N)r"   r#   r$   r%   rÒ   r   rÓ   rª   Úpropertyr;   r3   r&   r'   r(   r   r   Ê  su   € € € € € ðð ð, $˜ LÑ1Ô1Ðð+ð +ð +ð ð#ð #ñ „Xð#ð ð#ð #ñ „Xð#ð #ð #r'   r   c                 óˆ  ‡ ‡— t           j        j        j        Št	          ‰ t
          ¦  «        r‰ S t          j        ‰ ¦  «        rt          ‰ t
          ¦  «        r
 ‰ ¦   «         S t	          ‰ t          j
        ¦  «        r˜‰ j        dk    rt          d¦  «        ‚t          j        t          j        ‰ ¦  «        ¦  «        Š ‰ j        d         ‰ j        d         k    rt          d¦  «        ‚t          ˆ fd„ˆ fd„dˆ fd„	dˆ fd	„	‰ j        ‰ j        ¬
¦  «        S t           j                             ‰ ¦  «        rZ‰ j        d         ‰ j        d         k    rt          d¦  «        ‚t          ˆ fd„ˆ fd„dˆ ˆfd„	dˆ ˆfd„	‰ j        ‰ j        ¬
¦  «        S t%          ‰ d¦  «        rŒt%          ‰ d¦  «        r|t%          ‰ d¦  «        rlt          t'          ‰ d¦  «        t'          ‰ d¦  «        ‰ j        t'          ‰ d¦  «        t'          ‰ d¦  «        t'          ‰ d¦  «        ‰ j        ‰ j        ¬¦  «        S t+          ‰ ¦  «        r  G ˆ ˆfd„dt
          ¦  «        } |¦   «         S t	          ‰ t,          ¦  «        rG t/          t0          t2          t4          t6          t8          t:          t<          ¬¦  «        ‰          ¦   «         S t?          d¦  «        ‚)zE
    Convert given object to one suitable for use as a Jacobian.
    r^   zarray must have rank <= 2r   r   zarray must be squarec                 ó$   •— t          ‰| ¦  «        S r*   )r   ©rE   ÚJs    €r(   ú<lambda>zasjacobian.<locals>.<lambda>  s   ø€ ­¨Q°©¬€ r'   c                 óR   •— t          ‰                     ¦   «         j        | ¦  «        S r*   )r   ÚconjÚTrÜ   s    €r(   rÞ   zasjacobian.<locals>.<lambda>  s   ø€ ­#¨a¯fªf©h¬h¬j¸!Ñ*<Ô*<€ r'   c                 ó$   •— t          ‰| ¦  «        S r*   )r   ©rE   r\   rÝ   s     €r(   rÞ   zasjacobian.<locals>.<lambda>  s   ø€ ­u°Q¸©{¬{€ r'   c                 óR   •— t          ‰                     ¦   «         j        | ¦  «        S r*   )r   rà   rá   rã   s     €r(   rÞ   zasjacobian.<locals>.<lambda>  s   ø€ µ°a·f²f±h´h´jÀ!Ñ0DÔ0D€ r'   )r´   rµ   r   r¶   r3   r;   zmatrix must be squarec                 ó   •— ‰| z  S r*   r&   rÜ   s    €r(   rÞ   zasjacobian.<locals>.<lambda>  s   ø€ ¨¨Q©€ r'   c                 ó<   •— ‰                      ¦   «         j        | z  S r*   ©rà   rá   rÜ   s    €r(   rÞ   zasjacobian.<locals>.<lambda>  s   ø€ ¨!¯&ª&©(¬(¬*°q©.€ r'   c                 ó   •—  ‰‰| ¦  «        S r*   r&   ©rE   r\   rÝ   Úspsolves     €€r(   rÞ   zasjacobian.<locals>.<lambda>  s   ø€ ¨w¨w°q¸!©}¬}€ r'   c                 óJ   •—  ‰‰                      ¦   «         j        | ¦  «        S r*   rç   ré   s     €€r(   rÞ   zasjacobian.<locals>.<lambda>  s   ø€ °°¸¿º¹¼¼
ÀAÑ0FÔ0F€ r'   r;   r3   r   r´   rµ   r¶   rq   ri   )r´   rµ   r   r¶   rq   ri   r3   r;   c                   óF   •— e Zd Zd„ Zdˆ ˆfd„	Zˆ fd„Zdˆ ˆfd„	Zˆ fd„ZdS )	úasjacobian.<locals>.Jacc                 ó   — || _         d S r*   r.   rÌ   s      r(   rq   zasjacobian.<locals>.Jac.update  s   € Ø�”��r'   r   c                 óì   •—  ‰| j         ¦  «        }t          |t          j        ¦  «        rt	          ||¦  «        S t
          j                             |¦  «        r ‰||¦  «        S t          d¦  «        ‚©NzUnknown matrix type)	r/   Ú
isinstancer+   Úndarrayr   ÚscipyÚsparseÚissparserl   ©r©   rE   r\   ÚmrÝ   rê   s       €€r(   r   zasjacobian.<locals>.Jac.solve!  sk   ø€ Ø�A�d”f‘I”I�Ý˜a¥¤Ñ,Ô,ð <Ý   A™;œ;Ð&Ý”\×*Ò*¨1Ñ-Ô-ð <Ø"˜7 1 a™=œ=Ð(å$Ð%:Ñ;Ô;Ð;r'   c                 óÞ   •—  ‰| j         ¦  «        }t          |t          j        ¦  «        rt	          ||¦  «        S t
          j                             |¦  «        r||z  S t          d¦  «        ‚rð   )	r/   rñ   r+   rò   r   ró   rô   rõ   rl   ©r©   rE   r÷   rÝ   s      €r(   r´   zasjacobian.<locals>.Jac.matvec*  sd   ø€ Ø�A�d”f‘I”I�Ý˜a¥¤Ñ,Ô,ð <Ý˜q !™9œ9Ð$Ý”\×*Ò*¨1Ñ-Ô-ð <Ø˜q™5�Lå$Ð%:Ñ;Ô;Ð;r'   c                 óH  •—  ‰| j         ¦  «        }t          |t          j        ¦  «        r't	          |                     ¦   «         j        |¦  «        S t          j         	                    |¦  «        r# ‰|                     ¦   «         j        |¦  «        S t          d¦  «        ‚rð   )r/   rñ   r+   rò   r   rà   rá   ró   rô   rõ   rl   rö   s       €€r(   r¶   zasjacobian.<locals>.Jac.rsolve3  sƒ   ø€ Ø�A�d”f‘I”I�Ý˜a¥¤Ñ,Ô,ð <Ý  §¢¡¤¤¨QÑ/Ô/Ð/Ý”\×*Ò*¨1Ñ-Ô-ð <Ø"˜7 1§6¢6¡8¤8¤:¨qÑ1Ô1Ð1å$Ð%:Ñ;Ô;Ð;r'   c                 ó:  •—  ‰| j         ¦  «        }t          |t          j        ¦  «        r't	          |                     ¦   «         j        |¦  «        S t          j         	                    |¦  «        r|                     ¦   «         j        |z  S t          d¦  «        ‚rð   )r/   rñ   r+   rò   r   rà   rá   ró   rô   rõ   rl   rù   s      €r(   rµ   zasjacobian.<locals>.Jac.rmatvec<  s{   ø€ Ø�A�d”f‘I”I�Ý˜a¥¤Ñ,Ô,ð <Ý˜qŸvšv™xœxœz¨1Ñ-Ô-Ð-Ý”\×*Ò*¨1Ñ-Ô-ð <ØŸ6š6™8œ8œ:¨™>Ð)å$Ð%:Ñ;Ô;Ð;r'   NrÑ   )r"   r#   r$   rq   r   r´   r¶   rµ   )rÝ   rê   s   €€r(   ÚJacrí     sœ   ø€ € € € € ðð ð ð<ð <ð <ð <ð <ð <ð <ð<ð <ð <ð <ð <ð<ð <ð <ð <ð <ð <ð <ð<ð <ð <ð <ð <ð <ð <r'   rü   )r   r   r   r   r   r   rM   z#Cannot convert object to a JacobianNrÑ   ) ró   rô   Úlinalgrê   rñ   r²   ÚinspectÚisclassÚ
issubclassr+   rò   Úndimrl   Ú
atleast_2dr   r;   r3   rõ   r¾   r<   r   ÚcallableÚstrÚdictr   ÚBroydenSecondÚAndersonÚDiagBroydenÚLinearMixingÚExcitingMixingr   Ú	TypeError)rÝ   rü   rê   s   ` @r(   rh   rh   õ  s  øø€ õ ŒlÔ!Ô)€GÝ�!•XÑÔð T?ØˆÝ	Œ˜Ñ	Ô	ð R?¥
¨1­hÑ 7Ô 7ð R?Øˆq‰sŒsˆ
Ý	�A•r”zÑ	"Ô	"ð P?ØŒ6�AŠ:ˆ:ÝÐ8Ñ9Ô9Ð9ÝŒM�"œ* Q™-œ-Ñ(Ô(ˆØŒ7�1Œ:˜œ œÒ#Ð#ÝÐ3Ñ4Ô4Ð4åÐ2Ð2Ð2Ð2Ø <Ð <Ð <Ð <Ø:Ð:Ð:Ð:Ð:ØDÐDÐDÐDÐDØœg¨Q¬Wð	6ñ 6ô 6ð 	6õ
 
Œ×	Ò	˜qÑ	!Ô	!ð D?ØŒ7�1Œ:˜œ œÒ#Ð#ÝÐ4Ñ5Ô5Ð5Ý˜˜˜˜Ø 8Ð 8Ð 8Ð 8Ø<Ð<Ð<Ð<Ð<Ð<ØFÐFÐFÐFÐFÐFØœg¨Q¬Wð	6ñ 6ô 6ð 	6õ
 
��GÑ	Ô	ð <?¥¨¨GÑ!4Ô!4ð <?½ÀÀGÑ9LÔ9Lð <?Ý�w q¨(Ñ3Ô3Ý '¨¨9Ñ 5Ô 5ØœgÝ& q¨(Ñ3Ô3Ý& q¨(Ñ3Ô3Ý% a¨Ñ1Ô1ØœgØœgð'ñ 'ô 'ð 	'õ 
�!‰Œð 3?ð&	<ð &	<ð &	<ð &	<ð &	<ð &	<ð &	<ð &	<•(ñ &	<ô &	<ð &	<ðN ˆs‰uŒuˆÝ	�A•sÑ	Ô	ð 	?ð.�t�\Ý*Ý%Ý +Ý!-Ý#1Ý)ð+ñ +ô +ð ,-ô.ñ 0ô 0ð 	0õ Ð=Ñ>Ô>Ð>r'   c                   ó6   — e Zd Z ee¦  «        Zd„ Zd„ Zd„ ZdS )ÚGenericBroydenc                 ó  — t                                | |||¦  «         || _        || _        t	          | d¦  «        rK| j        €Ft          |¦  «        }|r*dt          t          |¦  «        d¦  «        z  |z  | _        d S d| _        d S d S d S )NÚalphaç      à?r   r]   )r²   ri   Úlast_fÚlast_xr¾   r  r   r-   )r©   r=   Úf0rY   Únormf0s        r(   ri   zGenericBroyden.setupY  s–   € Ý�Š�t˜R  TÑ*Ô*Ð*ØˆŒØˆŒå�4˜Ñ!Ô!ð 	! d¤jÐ&8õ ˜"‘X”XˆFØð !Ø ¥¥T¨"¡X¤X¨qÑ!1Ô!1Ñ1°FÑ:�”
�
�
à �”
�
�
ð	!ð 	!Ð&8Ð&8r'   c                 ó   — t           ‚r*   rÇ   ©r©   r/   r­   r€   Údfr°   Údf_norms          r(   Ú_updatezGenericBroyden._updateg  rÊ   r'   c           
      ó²   — || j         z
  }|| j        z
  }|                      ||||t          |¦  «        t          |¦  «        ¦  «         || _         || _        d S r*   )r  r  r  r   )r©   r/   r­   r  r€   s        r(   rq   zGenericBroyden.updatej  sR   € Ø�”‰_ˆØ�”‰_ˆØ�Š�Q˜˜2˜r¥4¨¡8¤8­T°"©X¬XÑ6Ô6Ð6ØˆŒØˆŒˆˆr'   N)	r"   r#   r$   rÒ   r   rÓ   ri   r  rq   r&   r'   r(   r  r  U  sQ   € € € € € à#˜ LÑ1Ô1Ðð!ð !ð !ð"ð "ð "ðð ð ð ð r'   r  c                   óž   — e Zd ZdZ ee¦  «        Zd„ Zed„ ¦   «         Z	ed„ ¦   «         Z
d„ Zd„ Zdd„Zdd	„Zd
„ Zdd„Zd„ Zd„ Zd„ Zdd„ZdS )Ú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.

    c                 óZ   — || _         g | _        g | _        || _        || _        d | _        d S r*   )r  Úcsrš   r‡   r3   Ú	collapsed)r©   r  r‡   r3   s       r(   rª   zLowRankMatrix.__init__€  s0   € ØˆŒ
ØˆŒØˆŒØˆŒØˆŒ
ØˆŒˆˆr'   c                 ó¾   — t          g d¢|d d…         | gz   ¦  «        \  }}}|| z  }t          ||¦  «        D ]$\  }}	 ||	| ¦  «        }
 ||||j        |
¦  «        }Œ%|S )N)ÚaxpyÚscalÚdotcr   )r   Úziprk   )rE   r  r  rš   r!  r"  r#  ÚwÚcÚdÚas              r(   Ú_matveczLowRankMatrix._matvecˆ  sƒ   € å)Ð*BÐ*BÐ*BØ*,¨R¨a¨R¬&°A°3©,ñ8ô 8Ñˆˆd�Dà�A‰IˆÝ˜˜B‘K”Kð 	&ð 	&‰DˆAˆqØ��Q˜‘
”
ˆAØ��Q˜˜1œ6 1Ñ%Ô%ˆAˆAØˆr'   c           	      óŠ  — t          |¦  «        dk    r| |z  S t          ddg|dd…         | gz   ¦  «        \  }}|d         }|t          j        t          |¦  «        |j        ¬¦  «        z  }t          |¦  «        D ]6\  }}	t          |¦  «        D ]!\  }
}|||
fxx          ||	|¦  «        z  cc<   Œ"Œ7t          j        t          |¦  «        |j        ¬¦  «        }t          |¦  «        D ]\  }
}	 ||	| ¦  «        ||
<   Œ||z  }t          ||¦  «        }| |z  }t          ||¦  «        D ]\  }} ||||j	        | ¦  «        }Œ|S )úEvaluate w = M^-1 vr   r!  r#  Nr   r2   )
Úlenr   r+   Úidentityr3   Ú	enumerateÚzerosr   r$  rk   )rE   r  r  rš   r!  r#  Úc0ÚAÚir'  Újr&  Úqr%  Úqcs                  r(   Ú_solvezLowRankMatrix._solve’  sq  € õ ˆr‰7Œ7�aŠ<ˆ<Ø�U‘7ˆNõ $ V¨VÐ$4°b¸¸!¸´fÀ¸s±lÑCÔC‰
ˆˆdà�ŒUˆØ•B”K¥ B¡¤¨r¬xÐ8Ñ8Ô8Ñ8ˆÝ˜b‘M”Mð 	%ð 	%‰DˆAˆqÝ! "™œð %ð %‘��1Ø�!�A�#��”˜$˜$˜q !™*œ*Ñ$��‘�ð%õ ŒH•S˜‘W”W B¤HÐ-Ñ-Ô-ˆÝ˜b‘M”Mð 	ð 	‰DˆAˆqØ�4˜˜1‘:”:ˆAˆa‰DˆDØ	ˆU‰
ˆÝ�!�Q‰KŒKˆàˆe‰GˆÝ˜˜Q‘Z”Zð 	(ð 	(‰EˆAˆrØ��Q˜˜1œ6 B 3Ñ'Ô'ˆAˆAàˆr'   c                 óœ   — | j         �t          j        | j         |¦  «        S t                               || j        | j        | j        ¦  «        S )zEvaluate w = M v)r  r+   r   r  r)  r  r  rš   ©r©   rE   s     r(   r´   zLowRankMatrix.matvec®  s>   € àŒ>Ð%Ý”6˜$œ.¨!Ñ,Ô,Ð,Ý×$Ò$ Q¨¬
°D´G¸T¼WÑEÔEÐEr'   c                 óî   — | j         �1t          j        | j         j                             ¦   «         |¦  «        S t
                               |t          j        | j        ¦  «        | j        | j	        ¦  «        S )zEvaluate w = M^H v)
r  r+   r   rá   rà   r  r)  r  rš   r  r8  s     r(   rµ   zLowRankMatrix.rmatvec´  sW   € àŒ>Ð%Ý”6˜$œ.Ô*×/Ò/Ñ1Ô1°1Ñ5Ô5Ð5Ý×$Ò$ Q­¬°´
Ñ(;Ô(;¸T¼WÀdÄgÑNÔNÐNr'   r   c                 ó’   — | j         �t          | j         |¦  «        S t                               || j        | j        | j        ¦  «        S )r+  )r  r   r  r6  r  r  rš   rÉ   s      r(   r   zLowRankMatrix.solveº  s<   € àŒ>Ð%Ý˜œ¨Ñ+Ô+Ð+Ý×#Ò# A t¤z°4´7¸D¼GÑDÔDÐDr'   c                 óä   — | j         �,t          | j         j                             ¦   «         |¦  «        S t                               |t          j        | j        ¦  «        | j        | j	        ¦  «        S )zEvaluate w = M^-H v)
r  r   rá   rà   r  r6  r+   r  rš   r  rÉ   s      r(   r¶   zLowRankMatrix.rsolveÀ  sU   € àŒ>Ð%Ý˜œÔ)×.Ò.Ñ0Ô0°!Ñ4Ô4Ð4Ý×#Ò# A¥r¤w¨t¬zÑ':Ô':¸D¼GÀTÄWÑMÔMÐMr'   c                 óX  — | j         �;| xj         |d d …d f         |d d d …f                              ¦   «         z  z  c_         d S | j                             |¦  «         | j                             |¦  «         t          | j        ¦  «        |j        k    r|                      ¦   «          d S d S r*   )r  rà   r  Úappendrš   r,  rk   Úcollapse)r©   r&  r'  s      r(   r=  zLowRankMatrix.appendÆ  sœ   € ØŒ>Ð%ØˆNŒN˜a    $ œi¨!¨D°°°¨F¬)¯.ª.Ñ*:Ô*:Ñ:Ñ:ˆNŒNØˆFàŒ�Š�qÑÔÐØŒ�Š�qÑÔÐåˆtŒw‰<Œ<˜!œ&Ò Ð Ø�MŠM‰OŒOˆOˆOˆOð !Ð r'   Nc                 ó~  — |�t          j        d|› d�d¬¦  «         |�t          j        d|› d�d¬¦  «         | j        �| j        S | j        t	          j        | j        | j        ¬¦  «        z  }t          | j	        | j
        ¦  «        D ]3\  }}||d d …d f         |d d d …f                              ¦   «         z  z  }Œ4|S )NzJLowRankMatrix is scipy-internal code, `dtype` should only be None but was z (not handled)é   )Ú
stacklevelzILowRankMatrix is scipy-internal code, `copy` should only be None but was r2   )ÚwarningsÚwarnr  r  r+   r-  r‡   r3   r$  r  rš   rà   )r©   r3   rj   ÚGmr&  r'  s         r(   rº   zLowRankMatrix.__array__Ñ  s  € ØÐÝŒMð OØ9>ðOð Oð Oà%&ð(ñ (ô (ð (ð ÐÝŒMð NØ9=ðNð Nð Nà%&ð(ñ (ô (ð (ð Œ>Ð%Ø”>Ð!àŒZ�œ D¤F°$´*Ð=Ñ=Ô=Ñ=ˆÝ˜œ ¤Ñ)Ô)ð 	-ð 	-‰DˆAˆqØ�!�A�A�A�d�F”)˜A˜d 1 1 1˜fœIŸNšNÑ,Ô,Ñ,Ñ,ˆBˆBØˆ	r'   c                 óp   — t          j        | t          ¬¦  «        | _        d| _        d| _        d| _        dS )z0Collapse the low-rank matrix to a full-rank one.)rj   N)r+   rC   r   r  r  rš   r  rÄ   s    r(   r>  zLowRankMatrix.collapseâ  s1   € åœ $­^Ð<Ñ<Ô<ˆŒØˆŒØˆŒØˆŒ
ˆ
ˆ
r'   c                 ó„   — | j         �dS |dk    sJ ‚t          | j        ¦  «        |k    r| j        dd…= | j        dd…= dS dS )zH
        Reduce the rank of the matrix by dropping all vectors.
        Nr   ©r  r,  r  rš   ©r©   Úranks     r(   Úrestart_reducezLowRankMatrix.restart_reduceé  sW   € ð Œ>Ð%ØˆFØ�aŠxˆxˆxˆxÝˆtŒw‰<Œ<˜$ÒÐØ”˜˜˜�
Ø”˜˜˜�
�
�
ð Ðr'   c                 ó¬   — | j         �dS |dk    sJ ‚t          | j        ¦  «        |k    r*| j        d= | j        d= t          | j        ¦  «        |k    °(dS dS )zK
        Reduce the rank of the matrix by dropping oldest vectors.
        Nr   rG  rH  s     r(   Úsimple_reducezLowRankMatrix.simple_reduceô  sd   € ð Œ>Ð%ØˆFØ�aŠxˆxˆxˆxÝ�$”'‰lŒl˜TÒ!Ð!Ø”˜�
Ø”˜�
õ �$”'‰lŒl˜TÒ!Ð!Ð!Ð!Ð!Ð!r'   c                 óŒ  — | j         �dS |}|�|}n|dz
  }| j        r(t          |t          | j        d         ¦  «        ¦  «        }t	          dt          ||dz
  ¦  «        ¦  «        }t          | j        ¦  «        }||k     rdS t          j        | j        ¦  «        j        }t          j        | j        ¦  «        j        }t          |d¬¦  «        \  }}t          ||j                             ¦   «         ¦  «        }t          |d¬¦  «        \  }	}
}t          |t          |¦  «        ¦  «        }t          ||j                             ¦   «         ¦  «        }t          |¦  «        D ]N}|dd…|f                              ¦   «         | j        |<   |dd…|f                              ¦   «         | j        |<   ŒO| j        |d…= | j        |d…= dS )	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^   r   r   Úeconomic)ÚmodeF)Úfull_matrices)r  r  ro   r,  r-   r+   rC   rá   rš   r	   r   rà   r
   r   rm   rj   )r©   Úmax_rankÚ	to_retainr“   r4  r÷   ÚCÚDÚRÚUÚSÚWHÚks                r(   Ú
svd_reducezLowRankMatrix.svd_reduceÿ  s•  € ð: Œ>Ð%ØˆFàˆØÐ ØˆAˆAà�A‘ˆAàŒ7ð 	(Ý�A•s˜4œ7 1œ:‘”Ñ'Ô'ˆAÝ�•3�q˜!˜A™#‘;”;ÑÔˆå�”‰LŒLˆØˆqŠ5ˆ5àˆFåŒH�T”WÑÔÔˆÝŒH�T”WÑÔÔˆå�!˜*Ð%Ñ%Ô%‰ˆˆ1Ý��1”3—8’8‘:”:ÑÔˆå�q¨Ð.Ñ.Ô.‰ˆˆ1ˆbå�•3�r‘7”7‰OŒOˆÝ��2”4—9’9‘;”;ÑÔˆå�q‘”ð 	'ð 	'ˆAØ˜1˜1˜1˜Q˜3œŸš™œˆDŒG�A‰JØ˜1˜1˜1˜Q˜3œŸš™œˆDŒG�A‰JˆJàŒG�A�B�BˆKØŒG�A�B�BˆKˆKˆKr'   rÑ   r»   r*   )r"   r#   r$   r%   rÒ   r   rÓ   rª   Ústaticmethodr)  r6  r´   rµ   r   r¶   r=  rº   r>  rJ  rL  rZ  r&   r'   r(   r  r  r  s-  € € € € € ðð ð $˜ LÑ1Ô1Ððð ð ð ðð ñ „\ðð ðð ñ „\ðð6Fð Fð FðOð Oð OðEð Eð Eð EðNð Nð Nð Nð	ð 	ð 	ðð ð ð ð"ð ð ð	ð 	ð 	ð	ð 	ð 	ð?ð ?ð ?ð ?ð ?ð ?r'   r  aÔ  
    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_paramsc                   óH   — e Zd ZdZdd„Zd„ Zd„ Zdd„Zd	„ Zdd
„Z	d„ Z
d„ ZdS )r   al  
    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])

    NÚrestartc                 ó~  ‡ ‡— t                                ‰ ¦  «         |‰ _        d ‰ _        |€t          j        }|‰ _        t          |t          ¦  «        rdŠn|dd …         Š|d         }|dz
  f‰z   Š|dk    rˆˆ fd„‰ _	        d S |dk    rˆˆ fd„‰ _	        d S |dk    rˆˆ fd	„‰ _	        d S t          d
|› d�¦  «        ‚)Nr&   r   r   r
   c                  ó"   •—  ‰j         j        ‰ Ž S r*   )rD  rZ  ©Úreduce_paramsr©   s   €€r(   rÞ   z'BroydenFirst.__init__.<locals>.<lambda>Ÿ  s   ø€ Ð#5 4¤7Ô#5°}Ð#E€ r'   Úsimplec                  ó"   •—  ‰j         j        ‰ Ž S r*   )rD  rL  ra  s   €€r(   rÞ   z'BroydenFirst.__init__.<locals>.<lambda>¡  s   ø€ Ð#8 4¤7Ô#8¸-Ð#H€ r'   r^  c                  ó"   •—  ‰j         j        ‰ Ž S r*   )rD  rJ  ra  s   €€r(   rÞ   z'BroydenFirst.__init__.<locals>.<lambda>£  s   ø€ Ð#9 4¤7Ô#9¸=Ð#I€ r'   zUnknown rank reduction method 'ú')r  rª   r  rD  r+   rD   rQ  rñ   r  Ú_reducerl   )r©   r  Úreduction_methodrQ  rb  s   `   @r(   rª   zBroydenFirst.__init__Ž  sþ   øø€ Ý×Ò Ñ%Ô%Ð%ØˆŒ
ØˆŒàÐÝ”vˆHØ ˆŒåÐ&­Ñ,Ô,ð 	3ØˆMˆMà,¨Q¨R¨RÔ0ˆMØ/°Ô2ÐØ! A™˜¨-Ñ7ˆà˜uÒ$Ð$ØEÐEÐEÐEÐEˆDŒLˆLˆLØ Ò)Ð)ØHÐHÐHÐHÐHˆDŒLˆLˆLØ Ò*Ð*ØIÐIÐIÐIÐIˆDŒLˆLˆLåÐRÐ?OÐRÐRÐRÑSÔSÐSr'   c                 ó˜   — t                                | |||¦  «         t          | j         | j        d         | j        ¦  «        | _        d S )Nr   )r  ri   r  r  r;   r3   rD  rÐ   s       r(   ri   zBroydenFirst.setup§  s?   € Ý×Ò˜T 1 a¨Ñ.Ô.Ð.Ý ¤ ¨T¬Z¸¬]¸D¼JÑGÔGˆŒˆˆr'   c                 ó*   — t          | j        ¦  «        S r*   )r   rD  rÄ   s    r(   r¸   zBroydenFirst.todense«  s   € Ý�4”7‰|Œ|Ðr'   r   c                 ó  — | j                              |¦  «        }t          j        |¦  «                             ¦   «         s@|                      | j        | j        | j        ¦  «         | j                              |¦  «        S |S r*   )	rD  r´   r+   rA   rB   ri   r  r  rY   )r©   r­   r\   Úrs       r(   r   zBroydenFirst.solve®  sf   € ØŒG�NŠN˜1ÑÔˆÝŒ{˜1‰~Œ~×!Ò!Ñ#Ô#ð 	%à�JŠJ�t”{ D¤K°´Ñ;Ô;Ð;Ø”7—>’> !Ñ$Ô$Ð$Øˆr'   c                 ó6   — | j                              |¦  «        S r*   )rD  r   ©r©   r­   s     r(   r´   zBroydenFirst.matvec¶  s   € ØŒw�}Š}˜QÑÔÐr'   c                 ó6   — | j                              |¦  «        S r*   )rD  rµ   ©r©   r­   r\   s      r(   r¶   zBroydenFirst.rsolve¹  s   € ØŒw�Š˜qÑ!Ô!Ð!r'   c                 ó6   — | j                              |¦  «        S r*   )rD  r¶   rn  s     r(   rµ   zBroydenFirst.rmatvec¼  s   € ØŒw�~Š~˜aÑ Ô Ð r'   c                 óø   — |                       ¦   «          | j                             |¦  «        }|| j                             |¦  «        z
  }|t	          ||¦  «        z  }	| j                             ||	¦  «         d S r*   )rg  rD  rµ   r´   r   r=  ©
r©   r/   r­   r€   r  r°   r  rE   r&  r'  s
             r(   r  zBroydenFirst._update¿  sg   € Ø�Š‰ŒˆàŒG�OŠO˜BÑÔˆØ�”—’ Ñ#Ô#Ñ#ˆØ•�R˜‘”‰OˆàŒ�Š�q˜!ÑÔÐÐÐr'   )Nr^  NrÑ   )r"   r#   r$   r%   rª   ri   r¸   r   r´   r¶   rµ   r  r&   r'   r(   r   r   Y  s­   € € € € € ð2ð 2ðhTð Tð Tð Tð2Hð Hð Hðð ð ðð ð ð ð ð  ð  ð"ð "ð "ð "ð!ð !ð !ðð ð ð ð r'   r   c                   ó   — e Zd ZdZd„ ZdS )r  aK  
    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                             |¦  «        z
  }||dz  z  }	| j                             ||	¦  «         d S ©Nr^   )rg  rD  r´   r=  rs  s
             r(   r  zBroydenSecond._updateü  sU   € Ø�Š‰ŒˆàˆØ�”—’ Ñ#Ô#Ñ#ˆØ�˜‘
‰NˆØŒ�Š�q˜!ÑÔÐÐÐr'   N)r"   r#   r$   r%   r  r&   r'   r(   r  r  É  s.   € € € € € ð0ð 0ðdð ð ð ð r'   r  c                   ó.   — e Zd ZdZd
d„Zdd„Zd„ Zd	„ ZdS )r  a  
    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])

    NrŽ   é   c                 óŽ   — t                                | ¦  «         || _        || _        g | _        g | _        d | _        || _        d S r*   )r  rª   r  ÚMr€   r  rƒ   Úw0)r©   r  r{  rz  s       r(   rª   zAnderson.__init__P  sD   € Ý×Ò Ñ%Ô%Ð%ØˆŒ
ØˆŒØˆŒØˆŒØˆŒ
ØˆŒˆˆr'   r   c                 óæ  — | j          |z  }t          | j        ¦  «        }|dk    r|S t          j        ||j        ¬¦  «        }t          |¦  «        D ] }t          | j        |         |¦  «        ||<   Œ!	 t          | j
        |¦  «        }n&# t          $ r | j        d d …= | j        d d …= |cY S w xY wt          |¦  «        D ]1}|||         | j        |         | j         | j        |         z  z   z  z  }Œ2|S ©Nr   r2   )r  r,  r€   r+   Úemptyr3   rm   r   r  r   r(  r   )	r©   r­   r\   r€   r‡   Údf_frY  rƒ   r÷   s	            r(   r   zAnderson.solveY  s  € ØŒjˆ[˜‰]ˆå�”‰LŒLˆØ�Š6ˆ6ØˆIåŒx˜ ¤Ð)Ñ)Ô)ˆÝ�q‘”ð 	*ð 	*ˆAÝ˜4œ7 1œ: qÑ)Ô)ˆD�‰GˆGð	Ý˜$œ& $Ñ'Ô'ˆEˆEøÝð 	ð 	ð 	à”˜˜˜�
Ø”˜˜˜�
ØˆIˆIˆIð		øøøõ �q‘”ð 	@ð 	@ˆAØ�%˜”(˜DœG AœJ¨¬°D´G¸A´JÑ)>Ñ>Ñ?Ñ?ˆBˆBØˆ	s   Á4B
 Â
 B-Â,B-c           
      ó  — | | j         z  }t          | j        ¦  «        }|dk    r|S t          j        ||j        ¬¦  «        }t          |¦  «        D ] }t          | j        |         |¦  «        ||<   Œ!t          j        ||f|j        ¬¦  «        }t          |¦  «        D ]™}t          |¦  «        D ]‡}t          | j        |         | j        |         ¦  «        |||f<   ||k    rT| j	        dk    rI|||fxx         t          | j        |         | j        |         ¦  «        | j	        dz  z  | j         z  z  cc<   ŒˆŒšt          ||¦  «        }	t          |¦  «        D ]1}
||	|
         | j        |
         | j        |
         | j         z  z   z  z  }Œ2|S )Nr   r2   r^   )r  r,  r€   r+   r~  r3   rm   r   r  r{  r   )r©   r­   r€   r‡   r  rY  Úbr2  r3  rƒ   r÷   s              r(   r´   zAnderson.matvecp  s�  € ØˆR�”
‰]ˆå�”‰LŒLˆØ�Š6ˆ6ØˆIåŒx˜ ¤Ð)Ñ)Ô)ˆÝ�q‘”ð 	*ð 	*ˆAÝ˜4œ7 1œ: qÑ)Ô)ˆD�‰GˆGåŒH�a˜�V 1¤7Ð+Ñ+Ô+ˆÝ�q‘”ð 	Qð 	QˆAÝ˜1‘X”Xð Qð Q�Ý˜dœg aœj¨$¬'°!¬*Ñ5Ô5��!�A�#‘Ø˜’6�6˜dœg¨šl˜lØ�a˜�c�F�F”F�d 4¤7¨1¤:¨t¬w°q¬zÑ:Ô:¸4¼7ÀA¹:ÑEÀdÄjÑPÑP�F�F‘FøðQõ �a˜‘”ˆå�q‘”ð 	@ð 	@ˆAØ�%˜”(˜DœG AœJ¨¬°¬°D´JÑ)>Ñ>Ñ?Ñ?ˆBˆBØˆ	r'   c                 ó  — | j         dk    rd S | j                             |¦  «         | j                             |¦  «         t	          | j        ¦  «        | j         k    rQ| j                             d¦  «         | j                             d¦  «         t	          | j        ¦  «        | j         k    °Qt	          | j        ¦  «        }t          j        ||f|j        ¬¦  «        }t          |¦  «        D ]Y}	t          |	|¦  «        D ]F}
|	|
k    r| j
        dz  }nd}d|z   t          | j        |	         | j        |
         ¦  «        z  ||	|
f<   ŒGŒZ|t          j        |d¦  «        j                             ¦   «         z  }|| _        d S )Nr   r2   r^   r   )rz  r€   r=  r  r,  Úpopr+   r/  r3   rm   r{  r   Útriurá   rà   r(  )r©   r/   r­   r€   r  r°   r  r‡   r(  r2  r3  Úwds               r(   r  zAnderson._update‡  sZ  € ØŒ6�QŠ;ˆ;ØˆFàŒ�Š�rÑÔÐØŒ�Š�rÑÔÐå�$”'‰lŒl˜TœVÒ#Ð#ØŒG�KŠK˜‰NŒNˆNØŒG�KŠK˜‰NŒNˆNõ �$”'‰lŒl˜TœVÒ#Ð#õ �”‰LŒLˆÝŒH�a˜�V 1¤7Ð+Ñ+Ô+ˆå�q‘”ð 	=ð 	=ˆAÝ˜1˜a‘[”[ð =ð =�Ø˜’6�6Øœ !™�B�Bà�BØ˜B™$¥ T¤W¨Q¤Z°´¸´Ñ <Ô <Ñ<��!�A�#‘�ð=ð 	
�RŒW�Q˜‰]Œ]Œ_×!Ò!Ñ#Ô#Ñ#ˆØˆŒˆˆr'   )NrŽ   rx  rÑ   )r"   r#   r$   r%   rª   r   r´   r  r&   r'   r(   r  r  	  se   € € € € € ð+ð +ðLð ð ð ðð ð ð ð.ð ð ð.ð ð ð ð r'   r  c                   óH   — e Zd ZdZdd„Zd„ Zdd„Zd„ Zdd„Zd	„ Z	d
„ Z
d„ ZdS )r  a,  
    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                 óH   — t                                | ¦  «         || _        d S r*   ©r  rª   r  ©r©   r  s     r(   rª   zDiagBroyden.__init__Î  ó!   € Ý×Ò Ñ%Ô%Ð%ØˆŒ
ˆ
ˆ
r'   c                 óª   — t                                | |||¦  «         t          j        | j        d         fd| j        z  | j        ¬¦  «        | _        d S )Nr   r   r2   )r  ri   r+   Úfullr;   r  r3   r'  rÐ   s       r(   ri   zDiagBroyden.setupÒ  sI   € Ý×Ò˜T 1 a¨Ñ.Ô.Ð.Ý”˜$œ* Qœ-Ð)¨1¨t¬z©>ÀÄÐLÑLÔLˆŒˆˆr'   r   c                 ó   — | | j         z  S r*   ©r'  rp  s      r(   r   zDiagBroyden.solveÖ  ó   € Øˆr�D”F‰{Ðr'   c                 ó   — | | j         z  S r*   rŽ  rn  s     r(   r´   zDiagBroyden.matvecÙ  r�  r'   c                 ó<   — | | j                              ¦   «         z  S r*   ©r'  rà   rp  s      r(   r¶   zDiagBroyden.rsolveÜ  ó   € Øˆr�D”F—K’K‘M”MÑ!Ð!r'   c                 ó<   — | | j                              ¦   «         z  S r*   r’  rn  s     r(   rµ   zDiagBroyden.rmatvecß  r“  r'   c                 ó6   — t          j        | j         ¦  «        S r*   )r+   Údiagr'  rÄ   s    r(   r¸   zDiagBroyden.todenseâ  s   € ÝŒw˜œ�wÑÔÐr'   c                 óN   — | xj         || j         |z  z   |z  |dz  z  z  c_         d S rv  rŽ  r  s          r(   r  zDiagBroyden._updateå  s.   € ØˆŒ�2˜œ˜r™	‘> 2Ñ% g¨q¡jÑ0Ñ0ˆŒˆˆr'   r*   rÑ   ©r"   r#   r$   r%   rª   ri   r   r´   r¶   rµ   r¸   r  r&   r'   r(   r  r  ¥  s©   € € € € € ð&ð &ðPð ð ð ðMð Mð Mðð ð ð ðð ð ð"ð "ð "ð "ð"ð "ð "ð ð  ð  ð1ð 1ð 1ð 1ð 1r'   r  c                   óB   — e Zd ZdZdd„Zdd„Zd„ Zdd„Zd„ Zd	„ Z	d
„ Z
dS )r	  a  
    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                 óH   — t                                | ¦  «         || _        d S r*   rˆ  r‰  s     r(   rª   zLinearMixing.__init__   rŠ  r'   r   c                 ó   — | | j         z  S r*   ©r  rp  s      r(   r   zLinearMixing.solve  ó   € Øˆr�$”*‰}Ðr'   c                 ó   — | | j         z  S r*   rœ  rn  s     r(   r´   zLinearMixing.matvec  r�  r'   c                 ó<   — | t          j        | j        ¦  «        z  S r*   ©r+   rà   r  rp  s      r(   r¶   zLinearMixing.rsolve
  ó   € Øˆr•"”'˜$œ*Ñ%Ô%Ñ%Ð%r'   c                 ó<   — | t          j        | j        ¦  «        z  S r*   r   rn  s     r(   rµ   zLinearMixing.rmatvec  r¡  r'   c                 óv   — t          j        t          j        | j        d         d| j        z  ¦  «        ¦  «        S )Nr   éÿÿÿÿ)r+   r–  rŒ  r;   r  rÄ   s    r(   r¸   zLinearMixing.todense  s*   € ÝŒw•r”w˜tœz¨!œ}¨b°´©mÑ<Ô<Ñ=Ô=Ð=r'   c                 ó   — d S r*   r&   r  s          r(   r  zLinearMixing._update  rÍ   r'   r*   rÑ   )r"   r#   r$   r%   rª   r   r´   r¶   rµ   r¸   r  r&   r'   r(   r	  r	  é  s–   € € € € € ðð ð,ð ð ð ðð ð ð ðð ð ð&ð &ð &ð &ð&ð &ð &ð>ð >ð >ðð ð ð ð r'   r	  c                   óH   — e Zd ZdZdd„Zd„ Zdd„Zd„ Zdd	„Zd
„ Z	d„ Z
d„ ZdS )r
  aç  
    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
    Nr]   c                 ód   — t                                | ¦  «         || _        || _        d | _        d S r*   )r  rª   r  ÚalphamaxÚbeta)r©   r  r¨  s      r(   rª   zExcitingMixing.__init__2  s/   € Ý×Ò Ñ%Ô%Ð%ØˆŒ
Ø ˆŒØˆŒ	ˆ	ˆ	r'   c                 ó¤   — t                                | |||¦  «         t          j        | j        d         f| j        | j        ¬¦  «        | _        d S r}  )r  ri   r+   rŒ  r;   r  r3   r©  rÐ   s       r(   ri   zExcitingMixing.setup8  sE   € Ý×Ò˜T 1 a¨Ñ.Ô.Ð.Ý”G˜TœZ¨œ]Ð,¨d¬jÀÄ
ÐKÑKÔKˆŒ	ˆ	ˆ	r'   r   c                 ó   — | | j         z  S r*   ©r©  rp  s      r(   r   zExcitingMixing.solve<  ó   € Øˆr�$”)‰|Ðr'   c                 ó   — | | j         z  S r*   r¬  rn  s     r(   r´   zExcitingMixing.matvec?  r­  r'   c                 ó<   — | | j                              ¦   «         z  S r*   ©r©  rà   rp  s      r(   r¶   zExcitingMixing.rsolveB  ó   € Øˆr�$”)—.’.Ñ"Ô"Ñ"Ð"r'   c                 ó<   — | | j                              ¦   «         z  S r*   r°  rn  s     r(   rµ   zExcitingMixing.rmatvecE  r±  r'   c                 ó:   — t          j        d| j        z  ¦  «        S )Nr¤  )r+   r–  r©  rÄ   s    r(   r¸   zExcitingMixing.todenseH  s   € ÝŒw�r˜$œ)‘|Ñ$Ô$Ð$r'   c                 óÄ   — || j         z  dk    }| j        |xx         | j        z  cc<   | j        | j        | <   t          j        | j        d| j        | j        ¬¦  «         d S )Nr   )Úout)r  r©  r  r+   Úclipr¨  )r©   r/   r­   r€   r  r°   r  Úincrs           r(   r  zExcitingMixing._updateK  sa   € Ø�”‰}˜qÒ ˆØŒ	�$ˆˆŒ˜4œ:Ñ%ˆˆ‰Øœ:ˆŒ	�4�%ÑÝ
Œ�”	˜1˜dœm°´Ð;Ñ;Ô;Ð;Ð;Ð;r'   )Nr]   rÑ   r˜  r&   r'   r(   r
  r
    s¨   € € € € € ðð ð4ð ð ð ðLð Lð Lðð ð ð ðð ð ð#ð #ð #ð #ð#ð #ð #ð%ð %ð %ð<ð <ð <ð <ð <r'   r
  c                   ó>   — e Zd ZdZ	 	 dd„Zd„ Zd„ Zdd
„Zd„ Zd„ Z	dS )r   a¤  
    Find a root of a function, using Krylov approximation for inverse Jacobian.

    This method is suitable for solving large-scale problems.

    Parameters
    ----------
    %(params_basic)s
    rdiff : float, optional
        Relative step size to use in numerical differentiation.
    method : str or callable, optional
        Krylov method to use to approximate the Jacobian.  Can be a string,
        or a function implementing the same interface as the iterative
        solvers in `scipy.sparse.linalg`. If a string, needs to be one of:
        ``'lgmres'``, ``'gmres'``, ``'bicgstab'``, ``'cgs'``, ``'minres'``,
        ``'tfqmr'``.

        The default is `scipy.sparse.linalg.lgmres`.
    inner_maxiter : int, optional
        Parameter to pass to the "inner" Krylov solver: maximum number of
        iterations. Iteration will stop after maxiter steps even if the
        specified tolerance has not been achieved.
    inner_M : LinearOperator or InverseJacobian
        Preconditioner for the inner Krylov iteration.
        Note that you can use also inverse Jacobians as (adaptive)
        preconditioners. For example,

        >>> 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])

    NÚlgmresé   é
   c                 óê  — || _         || _        t          t          j        j        j        t          j        j        j        t          j        j        j        t          j        j        j	        t          j        j        j
        t          j        j        j        ¬¦  «                             ||¦  «        | _        t          || j         ¬¦  «        | _        | j        t          j        j        j        u r1|| j        d<   d| j        d<   | j                             dd¦  «         �n| j        t          j        j        j        t          j        j        j        t          j        j        j	        fv r| j                             dd¦  «         n�| j        t          j        j        j        u r€|| j        d<   d| j        d<   | j                             d	g ¦  «         | j                             d
d¦  «         | j                             dd¦  «         | j                             dd¦  «         d„ t#          | j        ¦  «        j        D ¦   «         }|                     ¦   «         D ]œ\  }}	|                     d¦  «        st+          d|› �¦  «        ‚|dd …         |vrRt-          |dd …         |d¬¦  «        }
|
rd|
d         › d�}nd}t/          j        d|› d|› d�|z   dt2          ¬¦  «         ŒŠ|	| j        |dd …         <   Œ�d S )N)ÚbicgstabÚgmresr¹  ÚcgsÚminresÚtfqmr)ry   rz  r^  r   ry   Úatolr   Úouter_kÚouter_vÚprepend_outer_vTÚstore_outer_AvFc                 ó   — g | ]}|d v¯|‘Œ	S ))r©   ÚargsÚkwargsr&   )Ú.0rY  s     r(   ú
<listcomp>z+KrylovJacobian.__init__.<locals>.<listcomp>ç  s.   € ð 
ð 
ð 
ØØÐ2Ð2Ð2ð Ø2Ð2Ð2r'   Úinner_zUnknown parameter é   )r‡   z Did you mean 'z'?Ú zOption 'z#' is invalid for the inner method: zO. It will be ignored.Please check inner method documentation for valid options.r@  )rA  Úcategory)Úpreconditionerr›   r  ró   rô   rý   r½  r¾  r¹  r¿  rÀ  rÁ  ÚgetÚmethodÚ	method_kwÚ
setdefaultÚgcrotmkr   Ú
parametersr¼   Ú
startswithrl   r   rB  rC  ÚUserWarning)r©   r›   rÒ  Úinner_maxiterÚinner_MrÃ  r¿   Úvalid_inner_paramsÚkeyrÂ   Úinner_param_suggestionsÚsuggestion_msgs               r(   rª   zKrylovJacobian.__init__¼  s"  € à%ˆÔØˆŒ
õ Ý”\Ô(Ô1Ý”,Ô%Ô+Ý”<Ô&Ô-Ý”Ô#Ô'Ý”<Ô&Ô-Ý”,Ô%Ô+ðñ ô ÷ Šc�&˜&Ñ!Ô!ð 	Œõ  m°tÔ7JÐKÑKÔKˆŒàŒ;�%œ,Ô-Ô3Ð3Ð3à(5ˆDŒN˜9Ñ%Ø()ˆDŒN˜9Ñ%ØŒN×%Ò% f¨aÑ0Ô0Ð0Ñ0ØŒ[�Uœ\Ô0Ô8Ý"œ\Ô0Ô9Ý"œ\Ô0Ô4ð6ð 6ð 6ð ŒN×%Ò% f¨aÑ0Ô0Ð0Ð0ØŒ[�EœLÔ/Ô6Ð6Ð6Ø(/ˆDŒN˜9Ñ%à()ˆDŒN˜9Ñ%àŒN×%Ò% i°Ñ4Ô4Ð4ØŒN×%Ò%Ð&7¸Ñ>Ô>Ð>ð ŒN×%Ò%Ð&6¸Ñ>Ô>Ð>ØŒN×%Ò% f¨aÑ0Ô0Ð0ð
ð 
Ý  ¤Ñ-Ô-Ô8ð
ñ 
ô 
Ðð
 Ÿ(š(™*œ*ð 	,ð 	,‰JˆC�Ø—>’> (Ñ+Ô+ð =Ý Ð!;°cÐ!;Ð!;Ñ<Ô<Ð<Ø�1�2�2ŒwÐ0Ð0Ð0å*;¸CÀÀÀ¼GØ<NØ>?ð+Añ +Aô +AÐ'ð +ð (ð'HØ)@ÀÔ)Cð'Hð 'Hð 'H�N�Nð &(�Nõ ”ðQ˜sð Qð QÀvð Qð Qð Qð %ñ%ð  !Ý(ð	ñ 	ô 	ð 	ð Ø&+ˆDŒN˜3˜q˜r˜rœ7Ñ#Ð#ð7	,ð 	,r'   c                 óú   — t          | j        ¦  «                             ¦   «         }t          | j        ¦  «                             ¦   «         }| j        t          d|¦  «        z  t          d|¦  «        z  | _        d S )Nr   )r™   r=   r-   r  r›   Úomega)r©   ÚmxÚmfs      r(   Ú_update_diff_stepz KrylovJacobian._update_diff_step	  s[   € Ý�”‰\Œ\×ÒÑÔˆÝ�”‰\Œ\×ÒÑÔˆØ”Z¥# a¨¡*¤*Ñ,­s°1°b©z¬zÑ9ˆŒ
ˆ
ˆ
r'   c                 óZ  — t          |¦  «        }|dk    rd|z  S | j        |z  }|                      | j        ||z  z   ¦  «        | j        z
  |z  }t          j        t          j        |¦  «        ¦  «        s5t          j        t          j        |¦  «        ¦  «        rt          d¦  «        ‚|S )Nr   z$Function returned non-finite results)	r   rà  rY   r=   r  r+   rB   rA   rl   )r©   rE   ÚnvÚscrl  s        r(   r´   zKrylovJacobian.matvec  sš   € Ý�!‰WŒWˆØ�Š7ˆ7Ø�Q‘3ˆJØŒZ˜"‰_ˆØ�YŠY�t”w  A¡‘~Ñ&Ô&¨¬Ñ0°BÑ6ˆÝŒv•b”k !‘n”nÑ%Ô%ð 	E­"¬&µ´¸Q±´Ñ*@Ô*@ð 	EÝÐCÑDÔDÐDØˆr'   r   c                 óŽ   — d| j         v r | j        | j        |fi | j         ¤Ž\  }}n | j        | j        |fd|i| j         ¤Ž\  }}|S )NÚrtol)rÓ  rÒ  Úop)r©   Úrhsr\   Úsolr‹   s        r(   r   zKrylovJacobian.solve  sa   € Ø�T”^Ð#Ð#Ø#˜œ D¤G¨SÐCÐC°D´NÐCÐC‰IˆC��à#˜œ D¤G¨SÐMÐM°sÐM¸d¼nÐMÐM‰IˆC�Øˆ
r'   c                 óÀ   — || _         || _        |                      ¦   «          | j        �2t	          | j        d¦  «        r| j                             ||¦  «         d S d S d S )Nrq   )r=   r  rã  rÐ  r¾   rq   )r©   r/   r­   s      r(   rq   zKrylovJacobian.update  ss   € ØˆŒØˆŒØ×ÒÑ Ô Ð ð ÔÐ*Ý�tÔ*¨HÑ5Ô5ð 1ØÔ#×*Ò*¨1¨aÑ0Ô0Ð0Ð0Ð0ð +Ð*ð1ð 1r'   c                 ó¨  — t                                | |||¦  «         || _        || _        t          j        j                             | ¦  «        | _        | j	        €&t          j        |j        ¦  «        j        dz  | _	        |                      ¦   «          | j        �3t!          | j        d¦  «        r | j                             |||¦  «         d S d S d S )Nr  ri   )r²   ri   r=   r  ró   rô   rý   Úaslinearoperatorré  r›   r+   r¦   r3   r§   rã  rÐ  r¾   )r©   r/   r­   rY   s       r(   ri   zKrylovJacobian.setup)  sÊ   € Ý�Š�t˜Q  4Ñ(Ô(Ð(ØˆŒØˆŒÝ”,Ô%×6Ò6°tÑ<Ô<ˆŒàŒ:ÐÝœ !¤'Ñ*Ô*Ô.°4Ñ8ˆDŒJà×ÒÑ Ô Ð ð ÔÐ*Ý�tÔ*¨GÑ4Ô4ð 6ØÔ#×)Ò)¨!¨Q°Ñ5Ô5Ð5Ð5Ð5ð +Ð*ð6ð 6r'   )Nr¹  rº  Nr»  rÑ   )
r"   r#   r$   r%   rª   rã  r´   r   rq   ri   r&   r'   r(   r   r   V  s”   € € € € € ðcð cðJ CEØ')ðK,ð K,ð K,ð K,ðZ:ð :ð :ð
ð ð ðð ð ð ð1ð 1ð 1ð6ð 6ð 6ð 6ð 6r'   r   c                 óN  — t          |j        ¦  «        }|\  }}}}}}}	t          t          |t	          |¦  «         d…         |¦  «        ¦  «        }
d                     d„ |
D ¦   «         ¦  «        }|rd|z   }d                     d„ |
D ¦   «         ¦  «        }|r|dz   }|rt          d|› �¦  «        ‚d}|t          | ||j        |¬¦  «        z  }i }| 	                    t          ¦   «         ¦  «         t          ||¦  «         ||          }|j        |_        t          |¦  «         |S )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, c                 ó"   — g | ]\  }}|› d |›�‘ŒS ©ú=r&   ©rÊ  rY  rE   s      r(   rË  z#_nonlin_wrapper.<locals>.<listcomp>J  s&   € Ð8Ð8Ð8©¨¨A˜1˜˜˜q˜˜Ð8Ð8Ð8r'   c                 ó"   — g | ]\  }}|› d |› �‘ŒS rñ  r&   ró  s      r(   rË  z#_nonlin_wrapper.<locals>.<listcomp>M  s&   € Ð8Ð8Ð8©¨¨A˜Q˜*˜* ˜*˜*Ð8Ð8Ð8r'   zUnexpected 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)
)rÁ   r¿   ÚjacÚkwkw)Ú_getfullargspecrª   Úlistr$  r,  Újoinrl   r  r"   rq   ÚglobalsÚexecr%   rL   )rÁ   rõ  r   rÈ  ÚvarargsÚvarkwÚdefaultsÚ
kwonlyargsÚ
kwdefaultsÚ_rÉ  Úkw_strÚkwkw_strÚwrapperÚnsrY   s                   r(   Ú_nonlin_wrapperr  >  sL  € õ   ¤Ñ-Ô-€IØ@IÑ=€Dˆ'�5˜( J°
¸AÝ•#�d�C ™MœM˜>˜?˜?Ô+¨XÑ6Ô6Ñ7Ô7€FØ�YŠYÐ8Ð8°Ð8Ñ8Ô8Ñ9Ô9€FØð Ø˜‘ˆØ�yŠyÐ8Ð8°Ð8Ñ8Ô8Ñ9Ô9€HØð #Ø˜d‘?ˆØð >ÝÐ<°Ð<Ð<Ñ=Ô=Ð=ð€Gð � $¨6°s´|Ø"*ð,ñ ,ô ,ñ ,€Gà	€BØ‡I‚I�g‰iŒiÑÔÐÝˆ�"ÑÔÐØˆdŒ8€DØ”;€D„LÝˆT�N„N€NØ€Kr'   r   r   r   r   r   r   r   )rM   NFNNNNNNrN   NFT)rN   r�   rŽ   )Brþ   rr   rB  Únumpyr+   r   r   r   Úscipy.linalgr   r   r   r	   r
   r   Úscipy.sparse.linalgró   Úscipy.sparser   Úscipy._lib._utilr   r   r   r÷  Ú_linesearchr   r   r   Údifflibr   Útypesr   Ú__all__Ú	Exceptionr    r0   r7   r?   rF   r  ÚstriprJ   rL   rŒ   rp   rf   r²   r   rh   r  r  r   r  r  r  r	  r
  r   r  r   r   r   r   r   r   r   r&   r'   r(   ú<module>r     sT  ðð €€€Ø 
€
€
€
Ø €€€à Ð Ð Ð Ø $Ð $Ð $Ð $Ð $Ð $Ð $Ð $Ð $Ð $à ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ø Ð Ð Ð Ø Ð Ð Ð Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'Ø >Ð >Ð >Ð >Ð >Ð >Ð >Ð >Ø FÐ FÐ FÐ FÐ FÐ FØ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CØ Ð Ð Ð Ð Ð Ø %Ð %Ð %Ð %Ð %Ð %Ø Ð Ð Ð Ð Ð ðJð Jð J€ð	ð 	ð 	ð 	ð 	�Iñ 	ô 	ð 	ð ð  ð  ðð ð ðð ð ðð ð ð ˆTØ"Ð"ð $ñ 	ô 	÷ 
Š‰ŒØ"Ð"ð ($ñ (	ô (	÷P 
Š‰Œða1ñ 1ô 1€
ðh/ð /ð /ð
 ?DØKOØ?CØ48ðO"ð O"ð O"ð O"ðd 	€ˆÑ Ô Ð ð FJØ!ð*ð *ð *ð *ðZ9<ð 9<ð 9<ð 9<ð 9<ñ 9<ô 9<ð 9<ð@Hð Hð Hð Hð Hñ Hô Hð HðV(#ð (#ð (#ð (#ð (#ñ (#ô (#ð (#ðVY?ð Y?ð Y?ð@ð ð ð ð �Xñ ô ð ð:Lð Lð Lð Lð Lñ Lô Lð Lð^  1Ð0ð 2ñ  	ô  	÷* 
Š‰Œð+ ÐÑ ð0mð mð mð mð m�>ñ mô mð mð`9ð 9ð 9ð 9ð 9�Lñ 9ô 9ð 9ð@Uð Uð Uð Uð Uˆ~ñ Uô Uð UðxA1ð A1ð A1ð A1ð A1�.ñ A1ô A1ð A1ðH+ð +ð +ð +ð +�>ñ +ô +ð +ð\8<ð 8<ð 8<ð 8<ð 8<�^ñ 8<ô 8<ð 8<ð~a6ð a6ð a6ð a6ð a6�Xñ a6ô a6ð a6ðP)ð )ð )ðX ˆ?˜: |Ñ4Ô4€Øˆ?˜: }Ñ5Ô5€Øˆ?˜: xÑ0Ô0€Øˆ˜~¨|Ñ<Ô<€Øˆo˜m¨[Ñ9Ô9€Ø �Ð!1°>ÑBÔB€Ø� °Ñ@Ô@€€€r'   