§
    µ³ÙgÄ  ã                   óž  — 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 d dlmZ ddlmZmZ g d	¢Z G d
„ de¦  «        Zd„ Zd„ Zd„ Zd„ Z  e!d "                    ¦   «         d "                    ¦   «         ¬¦  «        Z#d„ Z$	 	 	 	 d?d„Z% e$e%¦  «         	 	 d@d„Z& G d„ d¦  «        Z' G d„ d¦  «        Z( G d „ d!¦  «        Z)d"„ Z* G d#„ d$e(¦  «        Z+ G d%„ d&¦  «        Z,d' "                    ¦   «         e#d(<    G d)„ d*e+¦  «        Z- G d+„ d,e-¦  «        Z. G d-„ d.e+¦  «        Z/ G d/„ d0e+¦  «        Z0 G d1„ d2e+¦  «        Z1 G d3„ d4e+¦  «        Z2 G d5„ d6e(¦  «        Z3d7„ Z4 e4d8e-¦  «        Z5 e4d9e.¦  «        Z6 e4d:e/¦  «        Z7 e4d;e1¦  «        Z8 e4d<e0¦  «        Z9 e4d=e2¦  «        Z: e4d>e3¦  «        Z;dS )Aé    N)ÚasarrayÚdotÚvdot)ÚnormÚsolveÚinvÚqrÚsvdÚLinAlgError)Úget_blas_funcs)Úcopy_if_needed)Úgetfullargspec_no_selfé   )Úscalar_search_wolfe1Úscalar_search_armijo)Ú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__© ó    úL/granary/edev/quantum/lib/python3.11/site-packages/scipy/optimize/_nonlin.pyr   r      s   € € € € € ðð à€Dr#   r   c                 óN   — t          j        | ¦  «                             ¦   «         S ©N)ÚnpÚabsoluteÚmax©Úxs    r$   Úmaxnormr,   $   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'   Ú
issubdtyper/   ÚinexactÚfloat64r*   s    r$   Ú_as_inexactr3   (   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Úgetattrr5   )r+   Úx0Úwraps      r$   Ú_array_liker;   0   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_normrB   7   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_docrH   u   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  ¦  «        ¦  «        }|}|rLt(          j                             d| |
|¦  «        |fz  ¦  «         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&   )r3   r;   Úflatten)ÚzÚFr9   s    €€r$   Úfuncznonlin_solve.<locals>.func­   s1   ø€ Ý˜1˜1�[¨¨BÑ/Ô/Ñ0Ô0Ñ1Ô1×9Ò9Ñ;Ô;Ð;r#   r   éd   TrJ   F)NrJ   Ú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%d:  |F(x)| = %g; step %g
z0A solution was found at the specified tolerance.z:The maximum number of iterations allowed has been reached.)r   rZ   )ÚnitÚfunÚstatusÚsuccessÚmessage)r,   ÚTerminationConditionr3   rR   r'   Ú	full_liker@   r   Ú
asjacobianÚsetupÚcopyÚsizeÚ
ValueErrorÚrangeÚcheckÚminr   Ú_nonlin_line_searchÚupdater)   ÚsysÚstdoutÚwriteÚflushr   r;   Ú	iteration) rT   r9   ÚjacobianrP   ÚverboseÚmaxiterrL   rM   rN   rO   Útol_normÚline_searchÚcallbackÚfull_outputÚraise_exceptionÚ	conditionrU   r+   ÚdxÚFxÚFx_normÚgammaÚeta_maxÚeta_tresholdÚetaÚnr]   rX   ÚsÚFx_norm_newÚeta_AÚinfos    ``                              r$   Únonlin_solver†   z   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×ÒÐ:Ø�8�8˜B‘<”< ð>$ñ $ñ %ô %ð %åŒ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   rZ   Tc                 óœ   •— | ‰	d         k    r‰d         S ‰
| ‰z  z   } ‰|¦  «        }t          |¦  «        dz  }|r| ‰	d<   |‰d<   |‰d<   |S )Nr   rZ   )rB   )r‚   ÚstoreÚxtrA   Úprz   rU   Ú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#   rW   rˆ   )ÚxtolÚaminrJ   )r™   rY   )T)r   r   r   )rU   r+   r{   rz   Úsearch_typer•   Úsminr—   r‚   Úphi1Úphi0r|   r‘   r–   rŽ   r�   r�   s   `` ` `      @@@@@r$   rj   rj     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 )r`   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'   Úfinfor2   Úepsr@   rN   rO   rL   rM   r   rP   Úf0_normrp   )ÚselfrL   rM   rN   rO   rP   r   s          r$   Ú__init__zTerminationCondition.__init__J  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   rZ   )	rp   r   r¢   rP   ÚintrL   rM   rN   rO   )r£   Úfr+   rz   Úf_normÚx_normÚdx_norms          r$   rh   zTerminationCondition.checkb  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!   r,   r¤   rh   r"   r#   r$   r`   r`   =  sQ   € € € € € ðð ð "¨$°dÀ4Ø ðð ð ð ð0<ð <ð <ð <ð <r#   r`   c                   ó2   — e Zd Zd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   rk   ÚmatvecÚrmatvecÚrsolveÚmatmatÚtodenser7   r/   zUnknown keyword argument r²   c                 óR   — |�t          d|› �¦  «        ‚|                      ¦   «         S )Nz`dtype` must be None, was )rf   r²   )r£   r/   rd   s      r$   Ú	__array__z$Jacobian.__init__.<locals>.__array__®  s-   € ØÐ$Ý$Ð%IÀ%Ð%IÐ%IÑJÔJÐJØ—|’|‘~”~Ð%r#   ©NN)Úitemsrf   Ú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£   rA   rX   s      r$   r   zJacobian.solve¶  ó   € Ý!Ð!r#   c                 ó   — d S r&   r"   ©r£   r+   rT   s      r$   rk   zJacobian.update¹  ó   € Øˆr#   c                 ó²   — || _         |j        |j        f| _        |j        | _        | j        j        t          j        u r|                      ||¦  «         d S d S r&   )rU   re   r7   r/   Ú	__class__rc   r¬   rk   ©r£   r+   rT   rU   s       r$   rc   zJacobian.setup¼  sV   € ØˆŒ	Ø”f˜aœfÐ%ˆŒ
Ø”WˆŒ
ØŒ>Ô¥8¤>Ð1Ð1à�KŠK˜˜1ÑÔÐÐÐð 2Ð1r#   N©r   )	r   r   r    r!   r¤   r¿   r   rk   rc   r"   r#   r$   r¬   r¬   }  so   € € € € € ð#ð #ðJ&ð &ð &ð %ð %ð %ð"ð "ð "ð "ðð ð ðð ð ð ð r#   r¬   c                   óD   — e Zd Zd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 )Nrc   r°   )rq   r   r®   rk   r¸   rc   r°   r¯   )r£   rq   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&   )rq   r7   r¾   s    r$   r7   zInverseJacobian.shapeã  ó   € àŒ}Ô"Ð"r#   c                 ó   — | j         j        S r&   )rq   r/   r¾   s    r$   r/   zInverseJacobian.dtypeç  rÏ   r#   N)r   r   r    r!   r¤   Úpropertyr7   r/   r"   r#   r$   r   r   Å  sc   € € € € € ðð ð(+ð +ð +ð ð#ð #ñ „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.
    rZ   zarray must have rank <= 2r   r   zarray must be squarec                 ó$   •— t          ‰| ¦  «        S r&   )r   ©rA   Ú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   ©rA   rX   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°   r/   r7   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"   ©rA   rX   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#   r7   r/   r   r®   r¯   r°   rk   rc   )r®   r¯   r   r°   rk   rc   r/   r7   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$   rk   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Úissparserf   ©r£   rA   rX   Ú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í   rf   ©r£   rA   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í   rf   rî   s       €€r$   r°   zasjacobian.<locals>.Jac.rsolve*  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í   rf   rñ   s      €r$   r¯   zasjacobian.<locals>.Jac.rmatvec3  s{   ø€ Ø�A�d”f‘I”I�Ý˜a¥¤Ñ,Ô,ð <Ý˜qŸvšv™xœxœz¨1Ñ-Ô-Ð-Ý”\×*Ò*¨1Ñ-Ô-ð <ØŸ6š6™8œ8œ:¨™>Ð)å$Ð%:Ñ;Ô;Ð;r#   NrË   )r   r   r    rk   r   r®   r°   r¯   )rÕ   râ   s   €€r$   ÚJacrå     sœ   ø€ € € € € ðð ð ð<ð <ð <ð <ð <ð <ð <ð<ð <ð <ð <ð <ð<ð <ð <ð <ð <ð <ð <ð<ð <ð <ð <ð <ð <ð <r#   rô   )r   r   r   r   r   r   rI   z#Cannot convert object to a JacobianNrË   ) rë   rì   Úlinalgrâ   ré   r¬   ÚinspectÚisclassÚ
issubclassr'   rê   Úndimrf   Ú
atleast_2dr   r7   r/   rí   r¸   r8   r   ÚcallableÚstrÚdictr   ÚBroydenSecondÚAndersonÚDiagBroydenÚLinearMixingÚExcitingMixingr   Ú	TypeError)rÕ   rô   râ   s   ` @r$   rb   rb   ì  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                   ó    — e Zd 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   rY   )r¬   rc   Úlast_fÚlast_xr¸   r  r   r)   )r£   r9   Úf0rU   Únormf0s        r$   rc   zGenericBroyden.setupM  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§   rz   Údfrª   Údf_norms          r$   Ú_updatezGenericBroyden._update[  rÄ   r#   c           
      ó²   — || j         z
  }|| j        z
  }|                      ||||t          |¦  «        t          |¦  «        ¦  «         || _         || _        d S r&   )r	  r
  r  r   )r£   r+   r§   r  rz   s        r$   rk   zGenericBroyden.update^  sR   € Ø�”‰_ˆØ�”‰_ˆØ�Š�Q˜˜2˜r¥4¨¡8¤8­T°"©X¬XÑ6Ô6Ð6ØˆŒØˆŒˆˆr#   N)r   r   r    rc   r  rk   r"   r#   r$   r  r  L  sA   € € € € € ð!ð !ð !ð"ð "ð "ðð ð ð ð r#   r  c                   óˆ   — e Zd Zd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�   r/   Ú	collapsed)r£   r  r�   r/   s       r$   r¤   zLowRankMatrix.__init__q  s0   € ØˆŒ
ØˆŒØˆŒØˆŒØˆŒ
ØˆŒˆˆr#   c                 ó¾   — t          g d¢|d d…         | gz   ¦  «        \  }}}|| z  }t          ||¦  «        D ]$\  }}	 ||	| ¦  «        }
 ||||j        |
¦  «        }Œ%|S )N)ÚaxpyÚscalÚdotcr   )r   Úzipre   )rA   r  r  r”   r  r  r  ÚwÚcÚdÚas              r$   Ú_matveczLowRankMatrix._matvecy  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   r.   )
Úlenr   r'   Úidentityr/   Ú	enumerateÚzerosr   r  re   )rA   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£   rA   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  r0  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  r.  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  r.  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$  re   Úcollapse)r£   r  r  s      r$   r5  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 r.   )ÚwarningsÚwarnr  r  r'   r%  r�   r/   r  r  r”   rØ   )r£   r/   rd   Ú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.)rd   N)r'   r?   r   r  r  r”   r  r¾   s    r$   r6  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   r?  r@  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

        NrZ   r   r   Úeconomic)ÚmodeF)Úfull_matrices)r  r  ri   r$  r)   r'   r?   rÙ   r”   r	   r   rØ   r
   r   rg   rd   )r£   Úmax_rankÚ	to_retainr�   r,  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¤   Ústaticmethodr!  r.  r®   r¯   r   r°   r5  r´   r6  rB  rD  rR  r"   r#   r$   r  r  f  s  € € € € € ðð ðð ð ð ðð ñ „\ðð ðð ñ „\ðð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&   )r<  rR  ©Úreduce_paramsr£   s   €€r$   rÖ   z'BroydenFirst.__init__.<locals>.<lambda>�  s   ø€ Ð#5 4¤7Ô#5°}Ð#E€ r#   Úsimplec                  ó"   •—  ‰j         j        ‰ Ž S r&   )r<  rD  rY  s   €€r$   rÖ   z'BroydenFirst.__init__.<locals>.<lambda>’  s   ø€ Ð#8 4¤7Ô#8¸-Ð#H€ r#   rV  c                  ó"   •—  ‰j         j        ‰ Ž S r&   )r<  rB  rY  s   €€r$   rÖ   z'BroydenFirst.__init__.<locals>.<lambda>”  s   ø€ Ð#9 4¤7Ô#9¸=Ð#I€ r#   zUnknown rank reduction method 'ú')r  r¤   r  r<  r'   r@   rI  ré   rü   Ú_reducerf   )r£   r  Úreduction_methodrI  rZ  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  rc   r  r  r7   r/   r<  rÊ   s       r$   rc   zBroydenFirst.setup˜  s?   € Ý×Ò˜T 1 a¨Ñ.Ô.Ð.Ý ¤ ¨T¬Z¸¬]¸D¼JÑGÔGˆŒˆˆr#   c                 ó*   — t          | j        ¦  «        S r&   )r   r<  r¾   s    r$   r²   zBroydenFirst.todenseœ  s   € Ý�4”7‰|Œ|Ðr#   r   c                 ó  — | j                              |¦  «        }t          j        |¦  «                             ¦   «         s@|                      | j        | j        | j        ¦  «         | j                              |¦  «        S |S r&   )	r<  r®   r'   r=   r>   rc   r
  r	  rU   )r£   r§   rX   Úrs       r$   r   zBroydenFirst.solveŸ  sf   € ØŒG�NŠN˜1ÑÔˆÝŒ{˜1‰~Œ~×!Ò!Ñ#Ô#ð 	%à�JŠJ�t”{ D¤K°´Ñ;Ô;Ð;Ø”7—>’> !Ñ$Ô$Ð$Øˆr#   c                 ó6   — | j                              |¦  «        S r&   )r<  r   ©r£   r§   s     r$   r®   zBroydenFirst.matvec§  s   € ØŒw�}Š}˜QÑÔÐr#   c                 ó6   — | j                              |¦  «        S r&   )r<  r¯   ©r£   r§   rX   s      r$   r°   zBroydenFirst.rsolveª  s   € ØŒw�Š˜qÑ!Ô!Ð!r#   c                 ó6   — | j                              |¦  «        S r&   )r<  r°   rf  s     r$   r¯   zBroydenFirst.rmatvec­  s   € ØŒw�~Š~˜aÑ Ô Ð r#   c                 óø   — |                       ¦   «          | j                             |¦  «        }|| j                             |¦  «        z
  }|t	          ||¦  «        z  }	| j                             ||	¦  «         d S r&   )r_  r<  r¯   r®   r   r5  ©
r£   r+   r§   rz   r  rª   r  rA   r  r  s
             r$   r  zBroydenFirst._update°  sg   € Ø�Š‰ŒˆàŒG�OŠO˜BÑÔˆØ�”—’ Ñ#Ô#Ñ#ˆØ•�R˜‘”‰OˆàŒ�Š�q˜!ÑÔÐÐÐr#   )NrV  NrË   )r   r   r    r!   r¤   rc   r²   r   r®   r°   r¯   r  r"   r#   r$   r   r   J  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 ©NrZ   )r_  r<  r®   r5  rk  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  ÚMrz   r  r}   Úw0)r£   r  rs  rr  s       r$   r¤   zAnderson.__init__A  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   r.   )r  r$  rz   r'   Úemptyr/   rg   r   r  r   r   r   )	r£   r§   rX   rz   r�   Údf_frQ  r}   rï   s	            r$   r   zAnderson.solveJ  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   r.   rZ   )r  r$  rz   r'   rv  r/   rg   r   r  rs  r   )r£   r§   rz   r�   rw  rQ  Úbr*  r+  r}   rï   s              r$   r®   zAnderson.matveca  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   r.   rZ   r   )rr  rz   r5  r  r$  Úpopr'   r'  r/   rg   rs  r   ÚtriurÙ   rØ   r   )r£   r+   r§   rz   r  rª   r  r�   r   r*  r+  Úwds               r$   r  zAnderson._updatex  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ˆ   rp  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   r.   )r  rc   r'   Úfullr7   r  r/   r  rÊ   s       r$   rc   zDiagBroyden.setupÃ  sI   € Ý×Ò˜T 1 a¨Ñ.Ô.Ð.Ý”˜$œ* Qœ-Ð)¨1¨t¬z©>ÀÄÐLÑLÔLˆŒˆˆr#   r   c                 ó   — | | j         z  S r&   ©r  rh  s      r$   r   zDiagBroyden.solveÇ  ó   € Øˆr�D”F‰{Ðr#   c                 ó   — | | j         z  S r&   r†  rf  s     r$   r®   zDiagBroyden.matvecÊ  r‡  r#   c                 ó<   — | | j                              ¦   «         z  S r&   ©r  rØ   rh  s      r$   r°   zDiagBroyden.rsolveÍ  ó   € Øˆr�D”F—K’K‘M”MÑ!Ð!r#   c                 ó<   — | | j                              ¦   «         z  S r&   rŠ  rf  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 rn  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¤   rc   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  rh  s      r$   r   zLinearMixing.solveõ  ó   € Øˆr�$”*‰}Ðr#   c                 ó   — | | j         z  S r&   r”  rf  s     r$   r®   zLinearMixing.matvecø  r•  r#   c                 ó<   — | t          j        | j        ¦  «        z  S r&   ©r'   rØ   r  rh  s      r$   r°   zLinearMixing.rsolveû  ó   € Øˆr•"”'˜$œ*Ñ%Ô%Ñ%Ð%r#   c                 ó<   — | t          j        | j        ¦  «        z  S r&   r˜  rf  s     r$   r¯   zLinearMixing.rmatvecþ  r™  r#   c                 óv   — t          j        t          j        | j        d         d| j        z  ¦  «        ¦  «        S )Nr   éÿÿÿÿ)r'   rŽ  r„  r7   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
    NrY   c                 ód   — t                                | ¦  «         || _        || _        d | _        d S r&   )r  r¤   r  ÚalphamaxÚbeta)r£   r  r   s      r$   r¤   zExcitingMixing.__init__#  s/   € Ý×Ò Ñ%Ô%Ð%ØˆŒ
Ø ˆŒØˆŒ	ˆ	ˆ	r#   c                 ó¤   — t                                | |||¦  «         t          j        | j        d         f| j        | j        ¬¦  «        | _        d S ru  )r  rc   r'   r„  r7   r  r/   r¡  rÊ   s       r$   rc   zExcitingMixing.setup)  sE   € Ý×Ò˜T 1 a¨Ñ.Ô.Ð.Ý”G˜TœZ¨œ]Ð,¨d¬jÀÄ
ÐKÑKÔKˆŒ	ˆ	ˆ	r#   r   c                 ó   — | | j         z  S r&   ©r¡  rh  s      r$   r   zExcitingMixing.solve-  ó   € Øˆr�$”)‰|Ðr#   c                 ó   — | | j         z  S r&   r¤  rf  s     r$   r®   zExcitingMixing.matvec0  r¥  r#   c                 ó<   — | | j                              ¦   «         z  S r&   ©r¡  rØ   rh  s      r$   r°   zExcitingMixing.rsolve3  ó   € Øˆr�$”)—.’.Ñ"Ô"Ñ"Ð"r#   c                 ó<   — | | j                              ¦   «         z  S r&   r¨  rf  s     r$   r¯   zExcitingMixing.rmatvec6  r©  r#   c                 ó:   — t          j        d| j        z  ¦  «        S )Nrœ  )r'   rŽ  r¡  r¾   s    r$   r²   zExcitingMixing.todense9  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§   rz   r  rª   r  Úincrs           r$   r  zExcitingMixing._update<  sa   € Ø�”‰}˜qÒ ˆØŒ	�$ˆˆŒ˜4œ:Ñ%ˆˆ‰Øœ:ˆŒ	�4�%ÑÝ
Œ�”	˜1˜dœm°´Ð;Ñ;Ô;Ð;Ð;Ð;r#   )NrY   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 ]>\  }}|                     d¦  «        st'          d|› �¦  «        ‚|| j        |dd …         <   Œ?d S )N)ÚbicgstabÚgmresr±  ÚcgsÚminresÚtfqmr)rs   rr  rV  r   rs   Úatolr   Úouter_kÚouter_vÚprepend_outer_vTÚstore_outer_AvFÚinner_zUnknown parameter é   )Úpreconditionerr•   rý   rë   rì   rõ   rµ  r¶  r±  r·  r¸  r¹  ÚgetÚmethodÚ	method_kwÚ
setdefaultÚgcrotmkr¶   Ú
startswithrf   )	r£   r•   rÃ  Úinner_maxiterÚinner_Mr»  r¹   Úkeyr¼   s	            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àŸ(š(™*œ*ð 	,ð 	,‰JˆC�Ø—>’> (Ñ+Ô+ð =Ý Ð!;°cÐ!;Ð!;Ñ<Ô<Ð<Ø&+ˆDŒN˜3˜q˜r˜rœ7Ñ#Ð#ð	,ð 	,r#   c                 óú   — t          | j        ¦  «                             ¦   «         }t          | j        ¦  «                             ¦   «         }| j        t          d|¦  «        z  t          d|¦  «        z  | _        d S )Nr   )r“   r9   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Ì  rU   r9   r  r'   r>   r=   rf   )r£   rA   ÚnvÚscrd  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£   ÚrhsrX   Ú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 )Nrk   )r9   r  rÏ  rÁ  r¸   rk   )r£   r+   r§   s      r$   rk   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  rc   )r¬   rc   r9   r  rë   rì   rõ   ÚaslinearoperatorrÕ  r•   r'   r    r/   r¡   rÏ  rÁ  r¸   )r£   r+   r§   rU   s       r$   rc   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   rk   rc   r"   r#   r$   r   r   G  s�   € € € € € ðcð cðJ CEØ')ð-,ð -,ð -,ð -,ð^:ð :ð :ð
ð ð ðð ð ð ð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"   ©Ú.0rQ  rA   s      r$   ú
<listcomp>z#_nonlin_wrapper.<locals>.<listcomp>  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>   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$  Újoinrf   rý   r   rk   ÚglobalsÚexecr!   rH   )r»   rã  Ú	signatureÚargsÚvarargsÚvarkwÚdefaultsÚ
kwonlyargsÚ
kwdefaultsÚ_ÚkwargsÚkw_strÚkwkw_strÚwrapperÚnsrU   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   )rI   NFNNNNNNrJ   NFT)rJ   r‡   rˆ   )<rö   rl   r:  Únumpyr'   r   r   r   Úscipy.linalgr   r   r   r	   r
   r   Úscipy.sparse.linalgrë   Úscipy.sparser   Úscipy._lib._utilr   r   rå  Ú_linesearchr   r   Ú__all__Ú	Exceptionr   r,   r3   r;   rB   rý   ÚstriprF   rH   r†   rj   r`   r¬   r   rb   r  r  r   rþ   rÿ   r   r  r  r   r÷  r   r   r   r   r   r   r   r"   r#   r$   ú<module>r     sæ  ðð €€€Ø 
€
€
€
Ø €€€à Ð Ð Ð Ø $Ð $Ð $Ð $Ð $Ð $Ð $Ð $Ð $Ð $à ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ø Ð Ð Ð Ø Ð Ð Ð Ø 'Ð 'Ð 'Ð 'Ð 'Ð 'Ø +Ð +Ð +Ð +Ð +Ð +Ø 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ðP"ð P"ð P"ð P"ðf 	€ˆÑ Ô Ð ð FJØ!ð*ð *ð *ð *ðZ9<ð 9<ð 9<ð 9<ð 9<ñ 9<ô 9<ð 9<ð@Eð Eð Eð Eð Eñ Eô Eð EðP$#ð $#ð $#ð $#ð $#ñ $#ô $#ð $#ðNY?ð Y?ð Y?ð@ð ð ð ð �Xñ ô ð ð4Ið Ið Ið Ið Iñ Iô Ið IðX ÷* 	Š‰Œð+ ÐÑ ð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<ð~C6ð C6ð C6ð C6ð C6�Xñ C6ô C6ð C6ðT)ð )ð )ðX ˆ?˜: |Ñ4Ô4€Øˆ?˜: }Ñ5Ô5€Øˆ?˜: xÑ0Ô0€Øˆ˜~¨|Ñ<Ô<€Øˆo˜m¨[Ñ9Ô9€Ø �Ð!1°>ÑBÔB€Ø� °Ñ@Ô@€€€r#   