a
    H¸ˆcðÅ  ã                   @   s  d Z ddlZddlZddlmZmZmZmZm	Z	m
Z
 ddlmZmZmZ ddlZddlZddlmZ ddlZddlmZ ddlmZmZ g d	¢ZG d
d„ deƒZdd„ Zdd„ Zdd„ Zdd„ Ze d !¡ d !¡ d�Z"dd„ Z#dHdd„Z$e#e$ƒ dId!d"„Z%G d#d$„ d$ƒZ&G d%d&„ d&ƒZ'G d'd(„ d(ƒZ(d)d*„ Z)G d+d,„ d,e'ƒZ*G d-d.„ d.ƒZ+d/ !¡ e"d0< G d1d2„ d2e*ƒZ,G d3d4„ d4e,ƒZ-G d5d6„ d6e*ƒZ.G d7d8„ d8e*ƒZ/G d9d:„ d:e*ƒZ0G d;d<„ d<e*ƒZ1G d=d>„ d>e'ƒZ2d?d@„ Z3e3dAe,ƒZ4e3dBe-ƒZ5e3dCe.ƒZ6e3dDe0ƒZ7e3dEe/ƒZ8e3dFe1ƒZ9e3dGe2ƒZ:dS )Ja¶  

Nonlinear solvers
-----------------

.. currentmodule:: scipy.optimize

This is a collection of general-purpose nonlinear multidimensional
solvers. These solvers find *x* for which *F(x) = 0*. Both *x*
and *F* can be multidimensional.

Routines
~~~~~~~~

Large-scale nonlinear solvers:

.. autosummary::

   newton_krylov
   anderson

General nonlinear solvers:

.. autosummary::

   broyden1
   broyden2

Simple iterations:

.. autosummary::

   excitingmixing
   linearmixing
   diagbroyden


Examples
~~~~~~~~

**Small problem**

>>> def F(x):
...    return np.cos(x) + x[::-1] - [1, 2, 3, 4]
>>> import scipy.optimize
>>> x = scipy.optimize.broyden1(F, [1,1,1,1], f_tol=1e-14)
>>> x
array([ 4.04674914,  3.91158389,  2.71791677,  1.61756251])
>>> np.cos(x) + x[::-1]
array([ 1.,  2.,  3.,  4.])


**Large problem**

Suppose that we needed to solve the following integrodifferential
equation on the square :math:`[0,1]\times[0,1]`:

.. math::

   \nabla^2 P = 10 \left(\int_0^1\int_0^1\cosh(P)\,dx\,dy\right)^2

with :math:`P(x,1) = 1` and :math:`P=0` elsewhere on the boundary of
the square.

The solution can be found using the `newton_krylov` solver:

.. plot::

   import numpy as np
   from scipy.optimize import newton_krylov
   from numpy import cosh, zeros_like, mgrid, zeros

   # parameters
   nx, ny = 75, 75
   hx, hy = 1./(nx-1), 1./(ny-1)

   P_left, P_right = 0, 0
   P_top, P_bottom = 1, 0

   def residual(P):
       d2x = zeros_like(P)
       d2y = zeros_like(P)

       d2x[1:-1] = (P[2:]   - 2*P[1:-1] + P[:-2]) / hx/hx
       d2x[0]    = (P[1]    - 2*P[0]    + P_left)/hx/hx
       d2x[-1]   = (P_right - 2*P[-1]   + P[-2])/hx/hx

       d2y[:,1:-1] = (P[:,2:] - 2*P[:,1:-1] + P[:,:-2])/hy/hy
       d2y[:,0]    = (P[:,1]  - 2*P[:,0]    + P_bottom)/hy/hy
       d2y[:,-1]   = (P_top   - 2*P[:,-1]   + P[:,-2])/hy/hy

       return d2x + d2y - 10*cosh(P).mean()**2

   # solve
   guess = zeros((nx, ny), float)
   sol = newton_krylov(residual, guess, method='lgmres', verbose=1)
   print('Residual: %g' % abs(residual(sol)).max())

   # visualize
   import matplotlib.pyplot as plt
   x, y = mgrid[0:1:(nx*1j), 0:1:(ny*1j)]
   plt.pcolormesh(x, y, sol, shading='gouraud')
   plt.colorbar()
   plt.show()

é    N)ÚnormÚsolveÚinvÚqrÚsvdÚLinAlgError)ÚasarrayÚdotÚvdot)Úget_blas_funcs)Úgetfullargspec_no_selfé   )Úscalar_search_wolfe1Úscalar_search_armijo)Úbroyden1Úbroyden2ÚandersonÚlinearmixingÚdiagbroydenÚexcitingmixingÚnewton_krylovc                   @   s   e Zd ZdS )ÚNoConvergenceN)Ú__name__Ú
__module__Ú__qualname__© r   r   úe/var/www/html/gerincmet/prog_calc/prog_calc/venv/lib/python3.9/site-packages/scipy/optimize/nonlin.pyr   ƒ   s   r   c                 C   s   t  | ¡ ¡ S ©N)ÚnpÚabsoluteÚmax©Úxr   r   r   Úmaxnorm‡   s    r#   c                 C   s*   t | ƒ} t | jtj¡s&t | tjd�S | S )z:Return `x` as an array, of either floats or complex floats©Údtype)r   r   Z
issubdtyper%   ZinexactÚfloat_r!   r   r   r   Ú_as_inexact‹   s    r'   c                 C   s(   t  | t  |¡¡} t|d| jƒ}|| ƒS )z;Return ndarray `x` as same array subclass and shape as `x0`Ú__array_wrap__)r   ZreshapeÚshapeÚgetattrr(   )r"   Úx0Úwrapr   r   r   Ú_array_like“   s    r-   c                 C   s"   t  | ¡ ¡ st  t j¡S t| ƒS r   )r   ÚisfiniteÚallÚarrayÚinfr   ©Úvr   r   r   Ú
_safe_normš   s    r4   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.

    )Zparams_basicZparams_extrac                 C   s   | j r| j t | _ d S r   )Ú__doc__Ú
_doc_parts)Úobjr   r   r   Ú_set_docØ   s    r8   ÚkrylovFÚarmijoTc                     sh  |
du rt n|
}
t||||	||
d�}tˆƒ‰‡ ‡fdd„}ˆ ¡ }t |tj¡}||ƒ}t|ƒ}t|ƒ}| 	| 
¡ ||¡ |du r¢|dur”|d }nd|jd  }|du r°d}n|d	u r¼d}|d
vrÌtdƒ‚d}d}d}d}t|ƒD �]$}| |||¡}|�r �q&t||| ƒ}|j||d� }t|ƒdk�r8tdƒ‚|�rXt|||||ƒ\}}}}nd}|| }||ƒ}t|ƒ}| | 
¡ |¡ |�r”|||ƒ ||d  |d  }||d  |k �rÆt||ƒ}nt|t|||d  ƒƒ}|}|rätj d||
|ƒ|f ¡ tj ¡  qä|�r"tt|ˆƒƒ‚nd}|�rZ|j|||dkdddœ| dœ}t|ˆƒ|fS t|ˆƒS d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                    s   t ˆ t| ˆƒƒƒ ¡ S r   )r'   r-   Úflatten)Úz©ÚFr+   r   r   Ú<lambda>  ó    znonlin_solve.<locals>.<lambda>r   éd   Tr:   F)Nr:   Ú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   rJ   )ÚnitZfunÚstatusÚsuccessÚmessage)r#   ÚTerminationConditionr'   r@   r   Z	full_liker1   r   Ú
asjacobianÚsetupÚcopyÚsizeÚ
ValueErrorÚrangeÚcheckÚminr   Ú_nonlin_line_searchÚupdater    ÚsysÚstdoutÚwriteÚflushr   r-   Ú	iteration) rC   r+   Újacobianr?   ÚverboseÚmaxiterr;   r<   r=   r>   Ztol_normZline_searchÚcallbackZfull_outputZraise_exceptionÚ	conditionÚfuncr"   ÚdxÚFxÚFx_normÚgammaZeta_maxZeta_tresholdÚetaÚnrL   rH   ÚsZFx_norm_newZeta_AÚinfor   rB   r   Únonlin_solveÝ   s�    -þ

ÿ

ÿþüü
rm   ç:Œ0âŽyE>ç{®Gáz„?c                    sè   dg‰|g‰t |ƒd g‰t ˆƒt ˆ ƒ ‰d‡ ‡‡‡‡‡fdd„	‰‡‡‡fdd„}|dkrxtˆ|ˆd d	|d
�\}}	}
n&|dkržtˆˆd ˆd  |d�\}}	|d u rªd}ˆ|ˆ   ‰|ˆd krÌˆd }nˆˆƒ}t |ƒ}|ˆ||fS )Nr   rJ   Tc                    sT   | ˆd krˆd S ˆ| ˆ   }ˆ|ƒ}t |ƒd }|rP| ˆd< |ˆd< |ˆd< |S )Nr   rJ   )r4   )rk   ÚstoreZxtr3   Úp)re   rd   Útmp_FxÚtmp_phiÚtmp_sr"   r   r   Úphiy  s    z _nonlin_line_search.<locals>.phic                    s0   t | ƒˆ d ˆ }ˆ | | dd�ˆ | ƒ | S )Nr   F)rp   )Úabs)rk   Úds)ru   ÚrdiffÚs_normr   r   Úderphi…  s    z#_nonlin_line_search.<locals>.derphirG   ro   )ZxtolÚaminr:   )r{   rI   )T)r   r   r   )rd   r"   rf   re   Zsearch_typerx   Zsminrz   rk   Zphi1Zphi0rg   r   )	re   rd   ru   rx   ry   rr   rs   rt   r"   r   rX   r  s,    ÿÿ

rX   c                   @   s.   e Zd ZdZdddddefdd„Zdd„ ZdS )rO   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                 C   sx   |d u rt  t j¡jd }|d u r(t j}|d u r6t j}|d u rDt j}|| _|| _|| _|| _|| _	|| _
d | _d| _d S )NgUUUUUUÕ?r   )r   Úfinfor&   Úepsr1   r=   r>   r;   r<   r   r?   Úf0_normr^   )Úselfr;   r<   r=   r>   r?   r   r   r   r   Ú__init__¬  s     zTerminationCondition.__init__c                 C   s˜   |  j d7  _ |  |¡}|  |¡}|  |¡}| jd u r<|| _|dkrHdS | jd urbd| j | jk S t|| jko”|| j | jko”|| jko”|| j |kƒS )Nr   r   rJ   )	r^   r   r~   r?   Úintr;   r<   r=   r>   )r   Úfr"   re   Zf_normZx_normÚdx_normr   r   r   rV   Ä  s     




ÿ
ýzTerminationCondition.check)r   r   r   r5   r#   r€   rV   r   r   r   r   rO   Ÿ  s
   ÿ
rO   c                   @   s:   e Zd ZdZdd„ Zdd„ Zddd„Zd	d
„ Zd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                    sb   g d¢}|  ¡ D ]4\}}||vr,td| ƒ‚|d urtˆ ||| ƒ qtˆ dƒr^‡ fdd„ˆ _d S )N)	r   rY   ÚmatvecÚrmatvecÚrsolveZmatmatÚtodenser)   r%   zUnknown keyword argument %srˆ   c                      s   ˆ   ¡ S r   )rˆ   r   ©r   r   r   rD     rE   z#Jacobian.__init__.<locals>.<lambda>)ÚitemsrT   ÚsetattrÚhasattrÚ	__array__)r   ÚkwÚnamesÚnameÚvaluer   r‰   r   r€     s    
zJacobian.__init__c                 C   s   t | ƒS r   )ÚInverseJacobianr‰   r   r   r   Úaspreconditioner  s    zJacobian.aspreconditionerr   c                 C   s   t ‚d S r   ©ÚNotImplementedError©r   r3   rH   r   r   r   r     s    zJacobian.solvec                 C   s   d S r   r   ©r   r"   rC   r   r   r   rY     s    zJacobian.updatec                 C   s:   || _ |j|jf| _|j| _| jjtju r6|  ||¡ d S r   )rd   rS   r)   r%   Ú	__class__rQ   r„   rY   ©r   r"   rC   rd   r   r   r   rQ     s
    zJacobian.setupN)r   )	r   r   r   r5   r€   r“   r   rY   rQ   r   r   r   r   r„   ß  s   %
r„   c                   @   s,   e Zd Zdd„ Zedd„ ƒZedd„ ƒZdS )r’   c                 C   s>   || _ |j| _|j| _t|dƒr(|j| _t|dƒr:|j| _d S )NrQ   r‡   )r_   r   r…   rY   rŒ   rQ   r‡   r†   )r   r_   r   r   r   r€   $  s    

zInverseJacobian.__init__c                 C   s   | j jS r   )r_   r)   r‰   r   r   r   r)   -  s    zInverseJacobian.shapec                 C   s   | j jS r   )r_   r%   r‰   r   r   r   r%   1  s    zInverseJacobian.dtypeN)r   r   r   r€   Úpropertyr)   r%   r   r   r   r   r’   #  s
   	
r’   c              
      sÔ  t jjj‰tˆ tƒrˆ S t ˆ ¡r2tˆ tƒr2ˆ ƒ S tˆ t	j
ƒr´ˆ jdkrPtdƒ‚t	 t	 ˆ ¡¡‰ ˆ jd ˆ jd kr|tdƒ‚t‡ fdd„‡ fdd„‡ fd	d„‡ fd
d„ˆ jˆ jd�S t j ˆ ¡�rˆ jd ˆ jd krÞtdƒ‚t‡ fdd„‡ fdd„‡ ‡fdd„‡ ‡fdd„ˆ jˆ jd�S tˆ dƒ�rztˆ dƒ�rztˆ dƒ�rzttˆ dƒtˆ dƒˆ jtˆ dƒtˆ dƒtˆ dƒˆ jˆ jd�S tˆ ƒ�r G ‡ ‡fdd„dtƒ}|ƒ S tˆ tƒ�rÈttttttttd�ˆ  ƒ S tdƒ‚dS )zE
    Convert given object to one suitable for use as a Jacobian.
    rJ   zarray must have rank <= 2r   r   zarray must be squarec                    s
   t ˆ | ƒS r   )r	   r2   ©ÚJr   r   rD   F  rE   zasjacobian.<locals>.<lambda>c                    s   t ˆ  ¡ j| ƒS r   )r	   ÚconjÚTr2   r›   r   r   rD   G  rE   c                    s
   t ˆ | ƒS r   )r   r2   r›   r   r   rD   H  rE   c                    s   t ˆ  ¡ j| ƒS r   )r   r�   rž   r2   r›   r   r   rD   I  rE   )r…   r†   r   r‡   r%   r)   zmatrix must be squarec                    s   ˆ |  S r   r   r2   r›   r   r   rD   N  rE   c                    s   ˆ   ¡ j|  S r   ©r�   rž   r2   r›   r   r   rD   O  rE   c                    s
   ˆˆ | ƒS r   r   r2   ©rœ   Úspsolver   r   rD   P  rE   c                    s   ˆˆ   ¡ j| ƒS r   rŸ   r2   r    r   r   rD   Q  rE   r)   r%   r   r…   r†   r‡   rY   rQ   )r…   r†   r   r‡   rY   rQ   r%   r)   c                       sL   e Zd Zdd„ Zd‡ ‡fdd„	Z‡ fdd„Zd‡ ‡fdd	„	Z‡ fd
d„ZdS )zasjacobian.<locals>.Jacc                 S   s
   || _ d S r   r!   r—   r   r   r   rY   _  s    zasjacobian.<locals>.Jac.updater   c                    sB   ˆ | j ƒ}t|tjƒr t||ƒS tj |¡r6ˆ||ƒS tdƒ‚d S ©NzUnknown matrix type)	r"   Ú
isinstancer   Úndarrayr   ÚscipyÚsparseÚ
isspmatrixrT   ©r   r3   rH   Úmr    r   r   r   b  s    


zasjacobian.<locals>.Jac.solvec                    s@   ˆ | j ƒ}t|tjƒr t||ƒS tj |¡r4|| S tdƒ‚d S r¢   )	r"   r£   r   r¤   r	   r¥   r¦   r§   rT   ©r   r3   r©   r›   r   r   r…   k  s    

zasjacobian.<locals>.Jac.matvecc                    sN   ˆ | j ƒ}t|tjƒr&t| ¡ j|ƒS tj 	|¡rBˆ| ¡ j|ƒS t
dƒ‚d S r¢   )r"   r£   r   r¤   r   r�   rž   r¥   r¦   r§   rT   r¨   r    r   r   r‡   t  s    
zasjacobian.<locals>.Jac.rsolvec                    sL   ˆ | j ƒ}t|tjƒr&t| ¡ j|ƒS tj 	|¡r@| ¡ j| S t
dƒ‚d S r¢   )r"   r£   r   r¤   r	   r�   rž   r¥   r¦   r§   rT   rª   r›   r   r   r†   }  s    
zasjacobian.<locals>.Jac.rmatvecN)r   )r   )r   r   r   rY   r   r…   r‡   r†   r   r    r   r   ÚJac^  s
   			r«   )r   r   r   r   r   r   r9   z#Cannot convert object to a JacobianN) r¥   r¦   Úlinalgr¡   r£   r„   ÚinspectÚisclassÚ
issubclassr   r¤   ÚndimrT   Z
atleast_2dr   r)   r%   r§   rŒ   r*   r   ÚcallableÚstrÚdictÚBroydenFirstÚBroydenSecondÚAndersonÚDiagBroydenÚLinearMixingÚExcitingMixingÚKrylovJacobianÚ	TypeError)rœ   r«   r   r    r   rP   6  sf    





ü
ü$
ù
'úúrP   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚGenericBroydenc                 C   s`   t  | |||¡ || _|| _t| dƒr\| jd u r\t|ƒ}|rVdtt|ƒdƒ | | _nd| _d S )NÚalphaç      à?r   rI   )r„   rQ   Úlast_fÚlast_xrŒ   r½   r   r    )r   r+   Úf0rd   Znormf0r   r   r   rQ   —  s    zGenericBroyden.setupc                 C   s   t ‚d S r   r”   ©r   r"   r‚   re   Údfrƒ   Údf_normr   r   r   Ú_update¥  s    zGenericBroyden._updatec              	   C   s@   || j  }|| j }|  ||||t|ƒt|ƒ¡ || _ || _d S r   )r¿   rÀ   rÅ   r   )r   r"   r‚   rÃ   re   r   r   r   rY   ¨  s
    

zGenericBroyden.updateN)r   r   r   rQ   rÅ   rY   r   r   r   r   r¼   –  s   r¼   c                   @   s†   e Zd ZdZdd„ Zedd„ ƒZedd„ ƒZdd	„ Zd
d„ Z	ddd„Z
ddd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd 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                 C   s(   || _ g | _g | _|| _|| _d | _d S r   )r½   Úcsrw   rj   r%   Ú	collapsed)r   r½   rj   r%   r   r   r   r€   »  s    zLowRankMatrix.__init__c                 C   s\   t g d¢|d d… | g ƒ\}}}||  }t||ƒD ]"\}}	||	| ƒ}
||||j|
ƒ}q4|S )N)ÚaxpyÚscalÚdotcr   )r   ÚziprS   )r3   r½   rÇ   rw   rÉ   rÊ   rË   ÚwÚcÚdÚar   r   r   Ú_matvecÃ  s    ÿ

zLowRankMatrix._matvecc                 C   s
  t |ƒdkr| | S tddg|dd… | g ƒ\}}|d }|tjt |ƒ|jd� }t|ƒD ]4\}}	t|ƒD ]"\}
}|||
f  ||	|ƒ7  < qlq\tjt |ƒ|jd�}t|ƒD ]\}
}	||	| ƒ||
< q®|| }t||ƒ}| | }t||ƒD ]\}}||||j	| ƒ}qê|S )úEvaluate w = M^-1 vr   rÉ   rË   Nr   r$   )
Úlenr   r   Úidentityr%   Ú	enumerateÚzerosr   rÌ   rS   )r3   r½   rÇ   rw   rÉ   rË   Zc0ÚAÚirÏ   ÚjrÎ   ÚqrÍ   Zqcr   r   r   Ú_solveÍ  s"     
zLowRankMatrix._solvec                 C   s.   | j durt | j |¡S t || j| j| j¡S )zEvaluate w = M vN)rÈ   r   r	   rÆ   rÑ   r½   rÇ   rw   ©r   r3   r   r   r   r…   é  s    
zLowRankMatrix.matvecc                 C   s:   | j durt | j j ¡ |¡S t |t | j¡| j| j	¡S )zEvaluate w = M^H vN)
rÈ   r   r	   rž   r�   rÆ   rÑ   r½   rw   rÇ   rÜ   r   r   r   r†   ï  s    
zLowRankMatrix.rmatvecr   c                 C   s,   | j durt| j |ƒS t || j| j| j¡S )rÒ   N)rÈ   r   rÆ   rÛ   r½   rÇ   rw   r–   r   r   r   r   õ  s    
zLowRankMatrix.solvec                 C   s8   | j durt| j j ¡ |ƒS t |t | j¡| j| j	¡S )zEvaluate w = M^-H vN)
rÈ   r   rž   r�   rÆ   rÛ   r   r½   rw   rÇ   r–   r   r   r   r‡   û  s    
zLowRankMatrix.rsolvec                 C   sp   | j d ur<|  j |d d …d f |d d d …f  ¡  7  _ d S | j |¡ | j |¡ t| jƒ|jkrl|  ¡  d S r   )rÈ   r�   rÇ   Úappendrw   rÓ   rS   Úcollapse)r   rÎ   rÏ   r   r   r   rÝ     s    
.zLowRankMatrix.appendc                 C   sl   | j d ur| j S | jtj| j| jd� }t| j| jƒD ]0\}}||d d …d f |d d d …f  	¡  7 }q6|S )Nr$   )
rÈ   r½   r   rÔ   rj   r%   rÌ   rÇ   rw   r�   )r   ÚGmrÎ   rÏ   r   r   r   r�     s    
*zLowRankMatrix.__array__c                 C   s"   t  | ¡| _d| _d| _d| _dS )z0Collapse the low-rank matrix to a full-rank one.N)r   r0   rÈ   rÇ   rw   r½   r‰   r   r   r   rÞ     s    zLowRankMatrix.collapsec                 C   sD   | j durdS |dksJ ‚t| jƒ|kr@| jdd…= | jdd…= dS )zH
        Reduce the rank of the matrix by dropping all vectors.
        Nr   ©rÈ   rÓ   rÇ   rw   ©r   Zrankr   r   r   Úrestart_reduce  s    
zLowRankMatrix.restart_reducec                 C   s>   | j durdS |dksJ ‚t| jƒ|kr:| jd= | jd= qdS )zK
        Reduce the rank of the matrix by dropping oldest vectors.
        Nr   rà   rá   r   r   r   Úsimple_reduce'  s    
zLowRankMatrix.simple_reduceNc                 C   s8  | j durdS |}|dur |}n|d }| jrBt|t| jd ƒƒ}tdt||d ƒƒ}t| jƒ}||k rldS t | j¡j}t | j¡j}t	|dd�\}}t
||j ¡ ƒ}t|ddd	�\}	}
}t
|t|ƒƒ}t
||j ¡ ƒ}t|ƒD ]8}|dd…|f  ¡ | j|< |dd…|f  ¡ | j|< qâ| 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

        NrJ   r   r   Zeconomic)ÚmodeFT)Zfull_matricesZ
compute_uv)rÈ   rÇ   rW   rÓ   r    r   r0   rž   rw   r   r	   r�   r   r   rU   rR   )r   Úmax_rankZ	to_retainrq   rÚ   r©   ÚCÚDÚRÚUÚSZWHÚkr   r   r   Ú
svd_reduce2  s0    

zLowRankMatrix.svd_reduce)r   )r   )N)r   r   r   r5   r€   ÚstaticmethodrÑ   rÛ   r…   r†   r   r‡   rÝ   r�   rÞ   râ   rã   rì   r   r   r   r   rÆ   °  s    

	


	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).
    Zbroyden_paramsc                   @   sV   e Zd ZdZddd„Zdd„ Zdd	„ Zddd„Zdd„ Zddd„Z	dd„ Z
dd„ ZdS )r´   a   
    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://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.broyden1(fun, [0, 0])
    >>> sol
    array([0.84116396, 0.15883641])

    NÚrestartc                    sº   t  ˆ¡ |ˆ_d ˆ_|d u r$tj}|ˆ_t|tƒr:d‰ n|dd … ‰ |d }|d fˆ  ‰ |dkrv‡ ‡fdd„ˆ_	n@|dkr�‡ ‡fdd„ˆ_	n&|d	krª‡ ‡fd
d„ˆ_	nt
d| ƒ‚d S )Nr   r   r   r   c                      s   ˆj jˆ Ž S r   )rß   rì   r   ©Zreduce_paramsr   r   r   rD   Ó  rE   z'BroydenFirst.__init__.<locals>.<lambda>Úsimplec                      s   ˆj jˆ Ž S r   )rß   rã   r   rï   r   r   rD   Õ  rE   rî   c                      s   ˆj jˆ Ž S r   )rß   râ   r   rï   r   r   rD   ×  rE   z"Unknown rank reduction method '%s')r¼   r€   r½   rß   r   r1   rå   r£   r²   Ú_reducerT   )r   r½   Zreduction_methodrå   r   rï   r   r€   Â  s(    

ÿzBroydenFirst.__init__c                 C   s.   t  | |||¡ t| j | jd | jƒ| _d S )Nr   )r¼   rQ   rÆ   r½   r)   r%   rß   r™   r   r   r   rQ   Ü  s    zBroydenFirst.setupc                 C   s
   t | jƒS r   )r   rß   r‰   r   r   r   rˆ   à  s    zBroydenFirst.todenser   c                 C   s:   | j  |¡}t |¡ ¡ s.|  | j| j| j¡ | j  |¡S r   )	rß   r…   r   r.   r/   rQ   rÀ   r¿   rd   )r   r‚   rH   Úrr   r   r   r   ã  s    zBroydenFirst.solvec                 C   s   | j  |¡S r   )rß   r   ©r   r‚   r   r   r   r…   ê  s    zBroydenFirst.matvecc                 C   s   | j  |¡S r   )rß   r†   ©r   r‚   rH   r   r   r   r‡   í  s    zBroydenFirst.rsolvec                 C   s   | j  |¡S r   )rß   r‡   ró   r   r   r   r†   ð  s    zBroydenFirst.rmatvecc           
      C   sD   |   ¡  | j |¡}|| j |¡ }|t||ƒ }	| j ||	¡ d S r   )rñ   rß   r†   r…   r
   rÝ   ©
r   r"   r‚   re   rÃ   rƒ   rÄ   r3   rÎ   rÏ   r   r   r   rÅ   ó  s
    zBroydenFirst._update)Nrî   N)r   )r   )r   r   r   r5   r€   rQ   rˆ   r   r…   r‡   r†   rÅ   r   r   r   r   r´   Œ  s   5


r´   c                   @   s   e Zd ZdZdd„ ZdS )rµ   aL  
    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           
      C   s:   |   ¡  |}|| j |¡ }||d  }	| j ||	¡ d S ©NrJ   )rñ   rß   r…   rÝ   rõ   r   r   r   rÅ   0  s
    zBroydenSecond._updateN)r   r   r   r5   rÅ   r   r   r   r   rµ   ý  s   2rµ   c                   @   s4   e Zd ZdZddd„Zddd	„Zd
d„ Zd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])

    Nro   é   c                 C   s2   t  | ¡ || _|| _g | _g | _d | _|| _d S r   )r¼   r€   r½   ÚMre   rÃ   rh   Úw0)r   r½   rù   rø   r   r   r   r€   „  s    
zAnderson.__init__r   c           	      C   sÌ   | j  | }t| jƒ}|dkr"|S tj||jd�}t|ƒD ]}t| j| |ƒ||< q:zt	| j
|ƒ}W n. ty’   | jd d …= | jd d …= | Y S 0 t|ƒD ]*}||| | j| | j | j|    7 }qœ|S ©Nr   r$   )r½   rÓ   re   r   Úemptyr%   rU   r
   rÃ   r   rÐ   r   )	r   r‚   rH   re   rj   Údf_frë   rh   r©   r   r   r   r   �  s     

(zAnderson.solvec              	   C   s,  | | j  }t| jƒ}|dkr"|S tj||jd�}t|ƒD ]}t| j| |ƒ||< q:tj||f|jd�}t|ƒD ]x}t|ƒD ]j}t| j| | j| ƒ|||f< ||kr|| j	dkr||||f  t| j| | j| ƒ| j	d  | j  8  < q|qpt
||ƒ}	t|ƒD ]*}
||	|
 | j|
 | j|
 | j    7 }qü|S )Nr   r$   rJ   )r½   rÓ   re   r   rû   r%   rU   r
   rÃ   rù   r   )r   r‚   re   rj   rü   rë   ÚbrØ   rÙ   rh   r©   r   r   r   r…   ¤  s"    
:
(zAnderson.matvecc                 C   sê   | j dkrd S | j |¡ | j |¡ t| jƒ| j krP| j d¡ | j d¡ q&t| jƒ}tj||f|jd�}t	|ƒD ]R}	t	|	|ƒD ]B}
|	|
krœ| j
d }nd}d| t| j|	 | j|
 ƒ ||	|
f< q„qv|t |d¡j ¡ 7 }|| _d S )Nr   r$   rJ   r   )rø   re   rÝ   rÃ   rÓ   Úpopr   rÖ   r%   rU   rù   r
   Ztriurž   r�   rÐ   )r   r"   r‚   re   rÃ   rƒ   rÄ   rj   rÐ   rØ   rÙ   Úwdr   r   r   rÅ   »  s"    

*zAnderson._update)Nro   r÷   )r   )r   r   r   r5   r€   r   r…   rÅ   r   r   r   r   r¶   =  s
   F
	
r¶   c                   @   sV   e Zd ZdZddd„Zdd„ Zddd	„Zd
d„ Zddd„Zdd„ Z	dd„ Z
d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                 C   s   t  | ¡ || _d S r   ©r¼   r€   r½   ©r   r½   r   r   r   r€     s    
zDiagBroyden.__init__c                 C   s6   t  | |||¡ tj| jd fd| j | jd�| _d S )Nr   r   r$   )r¼   rQ   r   Úfullr)   r½   r%   rÏ   r™   r   r   r   rQ     s    zDiagBroyden.setupr   c                 C   s   | | j  S r   ©rÏ   rô   r   r   r   r   
  s    zDiagBroyden.solvec                 C   s   | | j  S r   r  ró   r   r   r   r…     s    zDiagBroyden.matvecc                 C   s   | | j  ¡  S r   ©rÏ   r�   rô   r   r   r   r‡     s    zDiagBroyden.rsolvec                 C   s   | | j  ¡  S r   r  ró   r   r   r   r†     s    zDiagBroyden.rmatvecc                 C   s   t  | j ¡S r   )r   ÚdiagrÏ   r‰   r   r   r   rˆ     s    zDiagBroyden.todensec                 C   s(   |  j || j |  | |d  8  _ d S rö   r  rÂ   r   r   r   rÅ     s    zDiagBroyden._update)N)r   )r   ©r   r   r   r5   r€   rQ   r   r…   r‡   r†   rˆ   rÅ   r   r   r   r   r·   Ù  s   (


r·   c                   @   sN   e Zd ZdZddd„Zddd„Zdd	„ Zdd
d„Zdd„ Zdd„ Z	d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                 C   s   t  | ¡ || _d S r   r   r  r   r   r   r€   4  s    
zLinearMixing.__init__r   c                 C   s   | | j  S r   ©r½   rô   r   r   r   r   8  s    zLinearMixing.solvec                 C   s   | | j  S r   r  ró   r   r   r   r…   ;  s    zLinearMixing.matvecc                 C   s   | t  | j¡ S r   ©r   r�   r½   rô   r   r   r   r‡   >  s    zLinearMixing.rsolvec                 C   s   | t  | j¡ S r   r  ró   r   r   r   r†   A  s    zLinearMixing.rmatvecc                 C   s   t  t  | jd d| j ¡¡S )Nr   éÿÿÿÿ)r   r  r  r)   r½   r‰   r   r   r   rˆ   D  s    zLinearMixing.todensec                 C   s   d S r   r   rÂ   r   r   r   rÅ   G  s    zLinearMixing._update)N)r   )r   )r   r   r   r5   r€   r   r…   r‡   r†   rˆ   rÅ   r   r   r   r   r¸     s   


r¸   c                   @   sV   e Zd ZdZddd„Zdd„ Zdd	d
„Zdd„ Zddd„Zdd„ Z	dd„ Z
d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
    NrI   c                 C   s    t  | ¡ || _|| _d | _d S r   )r¼   r€   r½   ÚalphamaxÚbeta)r   r½   r
  r   r   r   r€   f  s    
zExcitingMixing.__init__c                 C   s2   t  | |||¡ tj| jd f| j| jd�| _d S rú   )r¼   rQ   r   r  r)   r½   r%   r  r™   r   r   r   rQ   l  s    zExcitingMixing.setupr   c                 C   s   | | j  S r   ©r  rô   r   r   r   r   p  s    zExcitingMixing.solvec                 C   s   | | j  S r   r  ró   r   r   r   r…   s  s    zExcitingMixing.matvecc                 C   s   | | j  ¡  S r   ©r  r�   rô   r   r   r   r‡   v  s    zExcitingMixing.rsolvec                 C   s   | | j  ¡  S r   r  ró   r   r   r   r†   y  s    zExcitingMixing.rmatvecc                 C   s   t  d| j ¡S )Nr	  )r   r  r  r‰   r   r   r   rˆ   |  s    zExcitingMixing.todensec                 C   sL   || j  dk}| j|  | j7  < | j| j| < tj| jd| j| jd� d S )Nr   )Úout)r¿   r  r½   r   Zclipr
  )r   r"   r‚   re   rÃ   rƒ   rÄ   Úincrr   r   r   rÅ     s    zExcitingMixing._update)NrI   )r   )r   r  r   r   r   r   r¹   K  s   


r¹   c                   @   sD   e Zd ZdZddd„Zdd	„ Zd
d„ Zddd„Zdd„ Zd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 : {'lgmres', 'gmres', 'bicgstab', 'cgs', 'minres'} or function
        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`.

        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.nonlin import BroydenFirst, KrylovJacobian
        >>> from scipy.optimize.nonlin 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] D.A. Knoll and D.E. Keyes, J. Comp. Phys. 193, 357 (2004).
           :doi:`10.1016/j.jcp.2003.08.010`
    .. [2] 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           	      K   sJ  || _ || _ttjjjtjjjtjjjtjjj	tjjj
d� ||¡| _t|| j d�| _| jtjjju rŠ|| jd< d| jd< | j dd¡ n~| jtjjju 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 ]4\}}| d¡�s0td| ƒ‚|| j|dd … < �qd S )N)ÚbicgstabÚgmresr  ÚcgsÚminres)ra   rø   Zrestrtr   ra   Zatolr   Úouter_kZouter_vZprepend_outer_vTZstore_outer_AvFZinner_zUnknown parameter %sé   )Úpreconditionerrx   r³   r¥   r¦   r¬   r  r  r  r  r  ÚgetÚmethodÚ	method_kwÚ
setdefaultZgcrotmkrŠ   Ú
startswithrT   )	r   rx   r  Zinner_maxiterZinner_Mr  rŽ   Úkeyr‘   r   r   r   r€   ë  s:    ûú



zKrylovJacobian.__init__c                 C   s<   t | jƒ ¡ }t | jƒ ¡ }| jtd|ƒ td|ƒ | _d S )Nr   )rv   r+   r    rÁ   rx   Úomega)r   ZmxÚmfr   r   r   Ú_update_diff_step  s    z KrylovJacobian._update_diff_stepc                 C   sl   t |ƒ}|dkrd| S | j| }|  | j||  ¡| j | }t t |¡¡sht t |¡¡rhtdƒ‚|S )Nr   z$Function returned non-finite results)	r   r   rd   r+   rÁ   r   r/   r.   rT   )r   r3   ÚnvÚscrò   r   r   r   r…     s    
 zKrylovJacobian.matvecr   c                 C   sL   d| j v r(| j| j|fi | j ¤Ž\}}n | j| j|fd|i| j ¤Ž\}}|S )NrH   )r  r  Úop)r   ÚrhsrH   Zsolrl   r   r   r   r   $  s    
 zKrylovJacobian.solvec                 C   s<   || _ || _|  ¡  | jd ur8t| jdƒr8| j ||¡ d S )NrY   )r+   rÁ   r"  r  rŒ   rY   )r   r"   r‚   r   r   r   rY   +  s    
zKrylovJacobian.updatec                 C   s|   t  | |||¡ || _|| _tjj | ¡| _| j	d u rJt
 |j¡jd | _	|  ¡  | jd urxt| jdƒrx| j |||¡ d S )Nr¾   rQ   )r„   rQ   r+   rÁ   r¥   r¦   r¬   Zaslinearoperatorr%  rx   r   r|   r%   r}   r"  r  rŒ   )r   r"   r‚   rd   r   r   r   rQ   5  s    

zKrylovJacobian.setup)Nr  r  Nr  )r   )
r   r   r   r5   r€   r"  r…   r   rY   rQ   r   r   r   r   rº   Š  s   `  ÿ
*


rº   c                 C   sØ   t |jƒ}|\}}}}}}}	tt|t|ƒ d… |ƒƒ}
d dd„ |
D ƒ¡}|rXd| }d dd„ |
D ƒ¡}|rx|d }|rˆtd| ƒ‚d}|t| ||j|d� }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                 S   s   g | ]\}}d ||f ‘qS )z%s=%rr   ©Ú.0rë   r3   r   r   r   Ú
<listcomp>V  rE   z#_nonlin_wrapper.<locals>.<listcomp>c                 S   s   g | ]\}}d ||f ‘qS )z%s=%sr   r'  r   r   r   r)  Y  rE   zUnexpected signature %sa™  
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Ž   ÚjacZkwkw)Ú_getfullargspecr€   ÚlistrÌ   rÓ   ÚjoinrT   r³   r   rY   ÚglobalsÚexecr5   r8   )r�   r*  Ú	signatureÚargsÚvarargsÚvarkwÚdefaultsÚ
kwonlyargsÚ
kwdefaultsÚ_ÚkwargsZkw_strZkwkw_strÚwrapperÚnsrd   r   r   r   Ú_nonlin_wrapperJ  s,    	

ÿ
r;  r   r   r   r   r   r   r   )r9   NFNNNNNNr:   NFT)r:   rn   ro   );r5   rZ   Únumpyr   Zscipy.linalgr   r   r   r   r   r   r   r	   r
   Zscipy.sparse.linalgr¥   Zscipy.sparser   r­   Zscipy._lib._utilr   r+  Z
linesearchr   r   Ú__all__Ú	Exceptionr   r#   r'   r-   r4   r³   Ústripr6   r8   rm   rX   rO   r„   r’   rP   r¼   rÆ   r´   rµ   r¶   r·   r¸   r¹   rº   r;  r   r   r   r   r   r   r   r   r   r   r   Ú<module>   sp   m 	
ø4    ý
   ÿ
-@D` Eq@ D.? A,





