
ʽXc           @` s\  d  Z  d d l m Z m Z m 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 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  d d d d d d d g Z! d e" f d     YZ# d   Z$ d   Z% d   Z& d   Z' e( d d j)   d d j)    Z* d   Z+ d d e- d d d d d d d d e- e. d  Z/ e+ e/  d d  d! d"  Z0 d# e1 f d$     YZ2 d% e1 f d&     YZ3 d' e1 f d(     YZ4 d)   Z5 d* e3 f d+     YZ6 d, e1 f d-     YZ7 d. j)   e* d/ <d0 e6 f d1     YZ8 d2 e8 f d3     YZ9 d4 e6 f d5     YZ: d6 e6 f d7     YZ; d8 e6 f d9     YZ< d: e6 f d;     YZ= d< e3 f d=     YZ> d>   Z? e? d e8  Z@ e? d e9  ZA e? d e:  ZB e? d e<  ZC e? d e;  ZD e? d e=  ZE e? d e>  ZF d S(?   s  

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.pcolor(x, y, sol)
   plt.colorbar()
   plt.show()

i    (   t   divisiont   print_functiont   absolute_importN(   t   callablet   exec_t   xrange(   t   normt   solvet   invt   qrt   svdt   LinAlgError(   t   asarrayt   dott   vdot(   t   get_blas_funcs(   t   getargspec_no_selfi   (   t   scalar_search_wolfe1t   scalar_search_armijot   broyden1t   broyden2t   andersont   linearmixingt   diagbroydent   excitingmixingt   newton_krylovt   NoConvergencec           B` s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR      s   c         C` s   t  j |   j   S(   N(   t   npt   absolutet   max(   t   x(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyt   maxnorm   s    c         C` s;   t  |   }  t j |  j t j  s7 t  |  d t j S|  S(   s:   Return `x` as an array, of either floats or complex floatst   dtype(   R   R   t
   issubdtypeR"   t   inexactt   float_(   R    (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyt   _as_inexact   s    c         C` s:   t  j |  t  j |   }  t | d |  j  } | |   S(   s;   Return ndarray `x` as same array subclass and shape as `x0`t   __array_wrap__(   R   t   reshapet   shapet   getattrR'   (   R    t   x0t   wrap(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyt   _array_like   s    c         C` s/   t  j |   j   s% t  j t  j  St |   S(   N(   R   t   isfinitet   allt   arrayt   infR   (   t   v(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyt
   _safe_norm   s    t   params_basics   
    F : function(x) -> f
        Function whose root to find; should take and return an array-like
        object.
    x0 : array_like
        Initial guess for the solution
    t   params_extras  
    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.

    c         C` s    |  j  r |  j  t |  _  n  d  S(   N(   t   __doc__t
   _doc_parts(   t   obj(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyt   _set_doc   s    	t   krylovt   armijoc          ` sd  t  d | d | d | d |	 d | d |
  } t       f d   }  j   } t j } | |  } t |  } t |  } | j | j   | |  | d k r | d k	 r | d } q d	 | j
 d } n  | t k r d
 } n | t k r d } n  | d k rt d   n  d } d } d } d } xt |  D]} | j | | |  } | rePn  t | | |  } | j | d | } t |  d k rt d   n  | rt | | | | |  \ } } } } n( d } | | } | |  } t |  } | j | j   |  | r0| | |  n  | | d | d } | | d | k  rlt | |  } n  t | t | | | d   } | } | r@t j j d | t |  | | f  t j j   q@q@W| rt t |     n d } | rSi | j d 6| d 6| d 6| d k d 6i d d 6d d 6| d 6} t |   | f St |   Sd S(    s  
    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)
       http://www.siam.org/books/kelley/

    t   f_tolt   f_rtolt   x_tolt   x_rtolt   iterR   c         ` s   t    t |      j   S(   N(   R&   R-   t   flatten(   t   z(   t   FR+   (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyt   <lambda>  s    i   id   R;   t   wolfes   Invalid line searchg?gH.?g?gMbP?t   toli    s[   Jacobian inversion yielded zero vector. This indicates a bug in the Jacobian approximation.g      ?i   s"   %d:  |F(x)| = %g; step %g; tol %g
t   nitt   funt   statust   successs0   A solution was found at the specified tolerance.s:   The maximum number of iterations allowed has been reached.t   messageN(   NR;   RE   (   t   TerminationConditionR&   RA   R   R1   R   t
   asjacobiant   setupt   copyt   Nonet   sizet   Truet   Falset
   ValueErrorR   t   checkt   minR   t   _nonlin_line_searcht   updateR   t   syst   stdoutt   writet   flushR   R-   t	   iteration(    RC   R+   t   jacobianR@   t   verboset   maxiterR<   R=   R>   R?   t   tol_normt   line_searcht   callbackt   full_outputt   raise_exceptiont	   conditiont   funcR    t   dxt   Fxt   Fx_normt   gammat   eta_maxt   eta_tresholdt   etat   nRI   RF   t   st   Fx_norm_newt   eta_At   info(    (   RC   R+   s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyt   nonlin_solve   s    ,			
 
g:0yE>g{Gz?c         ` sI  d g  | g  t  |  d g  t    t      t        f d      f d   } | d k r t  |  d d d d | \ } }	 }
 n6 | d	 k r t   d  d d | \ } }	 n  | d  k r d
 } n   |    |  d k r! d } n    } t  |  } |  | | f S(   Ni    i   c         ` sm   |   d k r  d S |    }  |  } t  |  d } | ri |   d <|  d <|  d <n  | S(   Ni    i   (   R3   (   Rp   t   storet   xtR2   t   p(   Rh   Rg   t   tmp_Fxt   tmp_phit   tmp_sR    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyt   phix  s    

c         ` s:   t  |    d  }   |  | d t   |   | S(   Ni   Ru   (   t   absRS   (   Rp   t   ds(   R{   t   rdifft   s_norm(    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyt   derphi  s    RE   t   xtolg{Gz?t   aminR;   g      ?(   R   RR   R   R   RP   (   Rg   R    Ri   Rh   t   search_typeR~   t   sminR   Rp   t   phi1t   phi0Rj   (    (	   Rh   Rg   R{   R~   R   Rx   Ry   Rz   R    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRW   q  s(    		!	RL   c           B` s2   e  Z d  Z d d d d d e d  Z d   Z RS(   s   
    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

    c         C` s   | d  k r( t j t j  j d } n  | d  k r@ t j } n  | d  k rX t j } n  | d  k rp t j } n  | |  _ | |  _ | |  _ | |  _	 | d  k r t
 |  _ n	 | |  _ | |  _ d  |  _ d |  _ d  S(   Ng      ?i   i    gUUUUUU?(   RP   R   t   finfoR%   t   epsR1   R>   R?   R<   R=   R!   R   R@   t   f0_normR]   (   t   selfR<   R=   R>   R?   R@   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyt   __init__  s$    							c         C` s   |  j  d 7_  |  j |  } |  j |  } |  j |  } |  j d  k rW | |  _ n  | d k rg d S|  j d  k	 r d |  j  |  j k St | |  j k o | |  j |  j k o | |  j k o | |  j	 | k  S(   Ni   i    i   (
   R]   R   R   RP   R@   t   intR<   R=   R>   R?   (   R   t   fR    Rh   t   f_normt   x_normt   dx_norm(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRU     s    N(   R   R   R6   RP   R!   R   RU   (    (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRL     s   t   Jacobianc           B` s>   e  Z d  Z d   Z d   Z d d  Z d   Z d   Z RS(   s  
    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      	   ` s   d d d d d d d d d	 g	 } x\ | j    D]N \ } } | | k rY t d
 |   n  | d  k	 r. t   | | |  q. q. Wt   d  r   f d     _ n  d  S(   NR   RX   t   matvect   rmatvect   rsolvet   matmatt   todenseR)   R"   s   Unknown keyword argument %sc           ` s
     j    S(   N(   R   (    (   R   (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRD     s    (   t   itemsRT   RP   t   setattrt   hasattrt	   __array__(   R   t   kwt   namest   namet   value(    (   R   s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c         C` s
   t  |   S(   N(   t   InverseJacobian(   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyt   aspreconditioner  s    i    c         C` s
   t   d  S(   N(   t   NotImplementedError(   R   R2   RF   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c         C` s   d  S(   N(    (   R   R    RC   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRX     s    c         C` sY   | |  _  | j | j f |  _ | j |  _ |  j j t j k rU |  j |  | |  n  d  S(   N(   Rg   RQ   R)   R"   t	   __class__RN   R   RX   (   R   R    RC   Rg   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRN     s
    	(   R   R   R6   R   R   R   RX   RN   (    (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s   $			R   c           B` s/   e  Z d    Z e d    Z e d    Z RS(   c         C` sa   | |  _  | j |  _ | j |  _ t | d  r? | j |  _ n  t | d  r] | j |  _ n  d  S(   NRN   R   (   R^   R   R   RX   R   RN   R   R   (   R   R^   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR   &  s    	c         C` s
   |  j  j S(   N(   R^   R)   (   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR)   /  s    c         C` s
   |  j  j S(   N(   R^   R"   (   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR"   3  s    (   R   R   R   t   propertyR)   R"   (    (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR   %  s   		c         ` s  t  j j j  t   t  r"   St j    rG t   t  rG     St   t	 j
  r  j d k rw t d   n  t	 j t	 j         j d   j d k r t d   n  t d   f d   d   f d	   d
   f d   d   f d   d   j d   j  St  j j    r  j d   j d k rHt d   n  t d   f d   d   f d   d
    f d   d    f d   d   j d   j  St   d  r=t   d  r=t   d
  r=t d t   d  d t   d  d
   j d t   d  d t   d  d t   d  d   j d   j  St    rod t f    f d     Y} |   St   t  rt d t d t d t d t d t d t d t      St d    d! S("   sE   
    Convert given object to one suitable for use as a Jacobian.
    i   s   array must have rank <= 2i    i   s   array must be squareR   c         ` s   t    |   S(   N(   R   (   R2   (   t   J(    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRD   H  s    R   c         ` s   t    j   j |   S(   N(   R   t   conjt   T(   R2   (   R   (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRD   I  s    R   c         ` s   t    |   S(   N(   R   (   R2   (   R   (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRD   J  s    R   c         ` s   t    j   j |   S(   N(   R   R   R   (   R2   (   R   (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRD   K  s    R"   R)   s   matrix must be squarec         ` s     |  S(   N(    (   R2   (   R   (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRD   P  s    c         ` s     j    j |  S(   N(   R   R   (   R2   (   R   (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRD   Q  s    c         ` s      |   S(   N(    (   R2   (   R   t   spsolve(    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRD   R  s    c         ` s      j    j |   S(   N(   R   R   (   R2   (   R   R   (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRD   S  s    RX   RN   t   Jacc           ` sY   e  Z d    Z d    f d  Z   f d   Z d    f d  Z   f d   Z RS(   c         S` s   | |  _  d  S(   N(   R    (   R   R    RC   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRX   a  s    i    c         ` s]     |  j   } t | t j  r. t | |  St j j |  rM  | |  St d   d  S(   Ns   Unknown matrix type(	   R    t
   isinstanceR   t   ndarrayR   t   scipyt   sparset
   isspmatrixRT   (   R   R2   RF   t   m(   R   R   (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR   d  s    c         ` sX     |  j   } t | t j  r. t | |  St j j |  rH | | St d   d  S(   Ns   Unknown matrix type(	   R    R   R   R   R   R   R   R   RT   (   R   R2   R   (   R   (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR   m  s    c         ` so     |  j   } t | t j  r7 t | j   j |  St j j	 |  r_  | j   j |  St
 d   d  S(   Ns   Unknown matrix type(   R    R   R   R   R   R   R   R   R   R   RT   (   R   R2   RF   R   (   R   R   (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR   v  s    c         ` sj     |  j   } t | t j  r7 t | j   j |  St j j	 |  rZ | j   j | St
 d   d  S(   Ns   Unknown matrix type(   R    R   R   R   R   R   R   R   R   R   RT   (   R   R2   R   (   R   (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    (   R   R   RX   R   R   R   R   (    (   R   R   (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR   `  s
   				R   R   R   R   R   R   R:   s#   Cannot convert object to a JacobianN(    R   R   t   linalgR   R   R   t   inspectt   isclasst
   issubclassR   R   t   ndimRT   t
   atleast_2dR   R)   R"   R   R   R*   R   R   t   strt   dictt   BroydenFirstt   BroydenSecondt   Andersont   DiagBroydent   LinearMixingt   ExcitingMixingt   KrylovJacobiant	   TypeError(   R   R   (    (   R   R   s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRM   8  sZ    -		
't   GenericBroydenc           B` s#   e  Z d    Z d   Z d   Z RS(   c         C` s   t  j |  | | |  | |  _ | |  _ t |  d  r |  j d  k r t |  } | r{ d t t |  d  | |  _ q d |  _ n  d  S(   Nt   alphag      ?i   g      ?(	   R   RN   t   last_ft   last_xR   R   RP   R   R   (   R   R+   t   f0Rg   t   normf0(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRN     s    		#c         C` s
   t   d  S(   N(   R   (   R   R    R   Rh   t   dfR   t   df_norm(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyt   _update  s    c         C` sX   | |  j  } | |  j } |  j | | | | t |  t |   | |  _  | |  _ d  S(   N(   R   R   R   R   (   R   R    R   R   Rh   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRX     s
    (	(   R   R   RN   R   RX   (    (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s   		t   LowRankMatrixc           B` s   e  Z d  Z d   Z e d    Z e d    Z d   Z d   Z d d  Z	 d d  Z
 d	   Z d
   Z d   Z d   Z d   Z d d  Z RS(   s   
    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(   N(   R   t   csR}   Ro   R"   RP   t	   collapsed(   R   R   Ro   R"   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    					c         C` s   t  d d d g | d  |  g  \ } } } | |  } xD t | |  D]3 \ } }	 | |	 |   }
 | | | | j |
  } qF W| S(   Nt   axpyt   scalt   dotci   (   R   t   zipRQ   (   R2   R   R   R}   R   R   R   t   wt   ct   dt   a(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyt   _matvec  s    
c         C` ss  t  |  d k r |  | St d d g | d  |  g  \ } } | d } | t j t  |  d | j } xV t |  D]H \ } }	 x9 t |  D]+ \ }
 } | | |
 f c | |	 |  7<q Wqy Wt j t  |  d | j } x- t |  D] \ }
 }	 | |	 |   | |
 <q W| | } t | |  } |  | } x6 t | |  D]% \ } } | | | | j	 |  } qFW| S(   s   Evaluate w = M^-1 vi    R   R   i   R"   (
   t   lenR   R   t   identityR"   t	   enumeratet   zerosR   R   RQ   (   R2   R   R   R}   R   R   t   c0t   At   iR   t   jR   t   qR   t   qc(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyt   _solve  s"    &
"'

c         C` sA   |  j  d k	 r" t j |  j  |  St j | |  j |  j |  j  S(   s   Evaluate w = M vN(	   R   RP   R   R   R   R   R   R   R}   (   R   R2   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c         C` sS   |  j  d k	 r+ t j |  j  j j   |  St j | t j |  j  |  j	 |  j
  S(   s   Evaluate w = M^H vN(   R   RP   R   R   R   R   R   R   R   R}   R   (   R   R2   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    i    c         C` s>   |  j  d k	 r t |  j  |  St j | |  j |  j |  j  S(   s   Evaluate w = M^-1 vN(   R   RP   R   R   R   R   R   R}   (   R   R2   RF   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c         C` sP   |  j  d k	 r( t |  j  j j   |  St j | t j |  j  |  j	 |  j
  S(   s   Evaluate w = M^-H vN(   R   RP   R   R   R   R   R   R   R   R}   R   (   R   R2   RF   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c         C` s   |  j  d  k	 rL |  j  | d  d   d  f | d  d  d   f j   7_  d  S|  j j |  |  j j |  t |  j  | j k r |  j   n  d  S(   N(	   R   RP   R   R   t   appendR}   R   RQ   t   collapse(   R   R   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    9c         C` s   |  j  d  k	 r |  j  S|  j t j |  j d |  j } xW t |  j |  j	  D]@ \ } } | | d  d   d  f | d  d  d   f j
   7} qN W| S(   NR"   (   R   RP   R   R   R   Ro   R"   R   R   R}   R   (   R   t   GmR   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    ""8c         C` s1   t  j |   |  _ d |  _ d |  _ d |  _ d S(   s0   Collapse the low-rank matrix to a full-rank one.N(   R   R0   R   RP   R   R}   R   (   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    		c         C` sO   |  j  d k	 r d S| d k s% t  t |  j  | k rK |  j 2|  j 2n  d S(   sH   
        Reduce the rank of the matrix by dropping all vectors.
        Ni    (   R   RP   t   AssertionErrorR   R   R}   (   R   t   rank(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyt   restart_reduce  s    c         C` sY   |  j  d k	 r d S| d k s% t  x- t |  j  | k rT |  j d =|  j d =q( Wd S(   sK   
        Reduce the rank of the matrix by dropping oldest vectors.
        Ni    (   R   RP   R   R   R   R}   (   R   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyt   simple_reduce)  s    
c         C` s  |  j  d k	 r d S| } | d k	 r. | } n
 | d } |  j r` t | t |  j d   } n  t d t | | d   } t |  j  } | | k  r d St j |  j  j } t j |  j	  j } t
 | d d \ } } t | | j j    } t | d t d t \ }	 }
 } t | t |   } t | | j j    } xZ t |  D]L } | d d  | f j   |  j | <| d d  | f j   |  j	 | <qPW|  j | 3|  j	 | 3d S(	   s  
        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).

           http://www.math.leidenuniv.nl/scripties/Rotten.pdf

        Ni   i    i   t   modet   economict   full_matricest
   compute_uv(   R   RP   R   RV   R   R   R   R0   R   R}   R	   R   R   R
   RS   RR   R   R   RO   (   R   t   max_rankt	   to_retainRw   R   R   t   Ct   Dt   Rt   Ut   St   WHt   k(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyt
   svd_reduce4  s0    	
	!#'
N(   R   R   R6   R   t   staticmethodR   R   R   R   R   R   R   R   R   R   R   RP   R   (    (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s   		
								s  
    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 (ie., no rank reduction).
    t   broyden_paramsR   c           B` se   e  Z d  Z d d d d  Z d   Z d   Z d d  Z d   Z d d  Z	 d	   Z
 d
   Z RS(   sL  
    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

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

       http://www.math.leidenuniv.nl/scripties/Rotten.pdf

    t   restartc         ` s   t  j   |  _ d   _ | d  k r7 t j } n  |  _ t | t	  rX d
   n | d   | d } | d f     | d k r    f d    _
 nX | d k r    f d    _
 n4 | d k r    f d    _
 n t d	 |   d  S(   Ni   i    R
   c           ` s    j  j     S(   N(   R   R   (    (   t   reduce_paramsR   (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRD     s    t   simplec           ` s    j  j     S(   N(   R   R   (    (   R   R   (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRD     s    R   c           ` s    j  j     S(   N(   R   R   (    (   R   R   (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRD     s    s"   Unknown rank reduction method '%s'(    (   R   R   R   RP   R   R   R1   R   R   R   t   _reduceRT   (   R   R   t   reduction_methodR   (    (   R   R   s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s&    				

c         C` s=   t  j |  | | |  t |  j |  j d |  j  |  _ d  S(   Ni    (   R   RN   R   R   R)   R"   R   (   R   R    RC   Rg   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRN     s    c         C` s   t  |  j  S(   N(   R   R   (   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    i    c         C` sV   |  j  j |  } t j |  j   sF |  j |  j |  j |  j  n  |  j  j |  S(   N(	   R   R   R   R.   R/   RN   R   R   Rg   (   R   R   RF   t   r(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c         C` s   |  j  j |  S(   N(   R   R   (   R   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c         C` s   |  j  j |  S(   N(   R   R   (   R   R   RF   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c         C` s   |  j  j |  S(   N(   R   R   (   R   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c   
      C` s\   |  j    |  j j |  } | |  j j |  } | t | |  }	 |  j j | |	  d  S(   N(   R   R   R   R   R   R   (
   R   R    R   Rh   R   R   R   R2   R   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s
    
N(   R   R   R6   RP   R   RN   R   R   R   R   R   R   (    (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    				R   c           B` s   e  Z d  Z d   Z RS(   s  
    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

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

       http://www.math.leidenuniv.nl/scripties/Rotten.pdf

    c   
      C` sK   |  j    | } | |  j j |  } | | d }	 |  j j | |	  d  S(   Ni   (   R   R   R   R   (
   R   R    R   Rh   R   R   R   R2   R   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR   	  s
    
(   R   R   R6   R   (    (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s   R   c           B` s>   e  Z d  Z d d d d  Z d d  Z d   Z d   Z RS(	   s  
    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

    References
    ----------
    .. [Ey] V. Eyert, J. Comp. Phys., 124, 271 (1996).

    g{Gz?i   c         C` sG   t  j |   | |  _ | |  _ g  |  _ g  |  _ d  |  _ | |  _ d  S(   N(	   R   R   R   t   MRh   R   RP   Rk   t   w0(   R   R   R   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR   I  s    					i    c   	      C` s   |  j  | } t |  j  } | d k r- | St j | d | j } x. t |  D]  } t |  j | |  | | <qR Wy t	 |  j
 |  } Wn  t k
 r |  j 2|  j 2| SXx? t |  D]1 } | | | |  j | |  j  |  j | 7} q W| S(   Ni    R"   (   R   R   Rh   R   t   emptyR"   R   R   R   R   R   R   (	   R   R   RF   Rh   Ro   t   df_fR   Rk   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR   R  s     /c      
   C` s  | |  j  } t |  j  } | d k r- | St j | d | j } x. t |  D]  } t |  j | |  | | <qR Wt j | | f d | j } x t |  D] } x t |  D] } t |  j | |  j |  | | | f <| | k r |  j	 d k r | | | f c t |  j | |  j |  |  j	 d |  j  8<q q Wq Wt
 | |  }	 x? t |  D]1 }
 | |	 |
 |  j |
 |  j |
 |  j  7} qbW| S(   Ni    R"   i   (   R   R   Rh   R   R   R"   R   R   R   R   R   (   R   R   Rh   Ro   R   R   t   bR   R   Rk   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR   i  s"    'J/c         C` sM  |  j  d k r d  S|  j j |  |  j j |  x< t |  j  |  j  k rq |  j j d  |  j j d  q6 Wt |  j  } t j | | f d | j } x t	 |  D]q }	 xh t	 |	 |  D]W }
 |	 |
 k r |  j
 d } n d } d | t |  j |	 |  j |
  | |	 |
 f <q Wq W| t j | d  j j   7} | |  _ d  S(   Ni    R"   i   i   (   R   Rh   R   R   R   t   popR   R   R"   R   R   R   t   triuR   R   R   (   R   R    R   Rh   R   R   R   Ro   R   R   R   t   wd(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s"    7N(   R   R   R6   RP   R   R   R   R   (    (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s
   		R   c           B` s_   e  Z d  Z d
 d  Z d   Z d d  Z d   Z d d  Z d   Z	 d   Z
 d	   Z RS(   s  
    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
    c         C` s   t  j |   | |  _ d  S(   N(   R   R   R   (   R   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c         C` sF   t  j |  | | |  t j |  j d f d |  j |  j |  _ d  S(   Ni    R"   (   R   RN   R   t   onesR)   R"   R   R   (   R   R    RC   Rg   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRN     s    i    c         C` s   | |  j  S(   N(   R   (   R   R   RF   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c         C` s   | |  j  S(   N(   R   (   R   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c         C` s   | |  j  j   S(   N(   R   R   (   R   R   RF   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c         C` s   | |  j  j   S(   N(   R   R   (   R   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c         C` s   t  j |  j  S(   N(   R   t   diagR   (   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c         C` s*   |  j  | |  j  | | | d 8_  d  S(   Ni   (   R   (   R   R    R   Rh   R   R   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    N(   R   R   R6   RP   R   RN   R   R   R   R   R   R   (    (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s   				R   c           B` sV   e  Z d  Z d	 d  Z d d  Z d   Z d d  Z d   Z d   Z	 d   Z
 RS(
   st  
    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
    c         C` s   t  j |   | |  _ d  S(   N(   R   R   R   (   R   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    i    c         C` s   | |  j  S(   N(   R   (   R   R   RF   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c         C` s   | |  j  S(   N(   R   (   R   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c         C` s   | t  j |  j  S(   N(   R   R   R   (   R   R   RF   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c         C` s   | t  j |  j  S(   N(   R   R   R   (   R   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c         C` s%   t  j t  j |  j d  |  j  S(   Ni    (   R   R  R  R)   R   (   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c         C` s   d  S(   N(    (   R   R    R   Rh   R   R   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    N(   R   R   R6   RP   R   R   R   R   R   R   R   (    (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s   			R   c           B` sb   e  Z d  Z d d d  Z d   Z d d  Z d   Z d d  Z d   Z	 d	   Z
 d
   Z RS(   sF  
    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.

    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
    g      ?c         C` s,   t  j |   | |  _ | |  _ d  |  _ d  S(   N(   R   R   R   t   alphamaxRP   t   beta(   R   R   R  (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    		c         C` sF   t  j |  | | |  |  j t j |  j d f d |  j |  _ d  S(   Ni    R"   (   R   RN   R   R   R  R)   R"   R  (   R   R    RC   Rg   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRN     s    i    c         C` s   | |  j  S(   N(   R  (   R   R   RF   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c         C` s   | |  j  S(   N(   R  (   R   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c         C` s   | |  j  j   S(   N(   R  R   (   R   R   RF   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c         C` s   | |  j  j   S(   N(   R  R   (   R   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    c         C` s   t  j d |  j  S(   Ni(   R   R  R  (   R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR   !  s    c         C` s`   | |  j  d k } |  j | c |  j 7<|  j |  j | <t j |  j d |  j d |  j d  S(   Ni    t   out(   R   R  R   R   t   clipR  (   R   R    R   Rh   R   R   R   t   incr(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR   $  s    N(   R   R   R6   RP   R   RN   R   R   R   R   R   R   (    (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s   				R   c           B` sV   e  Z d  Z d d d d d d  Z d   Z d   Z d d  Z d	   Z d
   Z	 RS(   sA  
    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_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.
    inner_tol, inner_maxiter, ...
        Parameters to pass on to the \"inner\" Krylov solver.
        See `scipy.sparse.linalg.gmres` for details.
    outer_k : int, optional
        Size of the subspace kept across LGMRES nonlinear iterations.
        See `scipy.sparse.linalg.lgmres` for details.
    %(params_extra)s

    See Also
    --------
    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`

    t   lgmresi   i
   c   	      K` st  | |  _  | |  _ t d t j j j d t j j j d t j j j d t j j j	 d t j j j
  j | |  |  _ t d | d |  j   |  _ |  j t j j j k r | |  j d <d	 |  j d <n[ |  j t j j j k r | |  j d
 <d	 |  j d <|  j j d g   |  j j d t  n  xM | j   D]? \ } } | j d  s[t d |   n  | |  j | d <q-Wd  S(   Nt   bicgstabt   gmresR  t   cgst   minresR`   R   t   restrti   t   outer_kt   outer_vt   store_outer_Avt   inner_s   Unknown parameter %si   (   t   preconditionerR~   R   R   R   R   R  R  R  R  R  t   gett   methodt	   method_kwt
   setdefaultRS   R   t
   startswithRT   (	   R   R~   R  t   inner_maxitert   inner_MR  R   t   keyR   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR   y  s,    		c         C` sT   t  |  j  j   } t  |  j  j   } |  j t d |  t d |  |  _ d  S(   Ni   (   R|   R+   R   R   R~   t   omega(   R   t   mxt   mf(    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyt   _update_diff_step  s    c         C` s   t  |  } | d k r  d | S|  j | } |  j |  j | |  |  j | } t j t j |   r t j t j |   r t d   n  | S(   Ni    s$   Function returned non-finite results(	   R   R  Rg   R+   R   R   R/   R.   RT   (   R   R2   t   nvt   scR   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    %1i    c         C` s^   d |  j  k r3 |  j |  j | |  j   \ } } n' |  j |  j | d | |  j  \ } } | S(   NRF   (   R  R  t   op(   R   t   rhsRF   t   solRs   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR     s    $'c         C` sZ   | |  _  | |  _ |  j   |  j d  k	 rV t |  j d  rV |  j j | |  qV n  d  S(   NRX   (   R+   R   R!  R  RP   R   RX   (   R   R    R   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRX     s    		
c         C` s   t  j |  | | |  | |  _ | |  _ t j j j |   |  _ |  j	 d  k rn t j | j  j d |  _	 n  |  j   |  j d  k	 r t |  j d  r |  j j | | |  q n  d  S(   Ng      ?i   RN   g      ?(   R   RN   R+   R   R   R   R   t   aslinearoperatorR$  R~   RP   R   R   R"   R   R!  R  R   (   R   R    R   Rg   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyRN     s    		
N(
   R   R   R6   RP   R   R!  R   R   RX   RN   (    (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyR   /  s   H	$		
	
c      
   C` s:  t  | j  \ } } } } t t | t |  |   } d j g  | D] \ } } d | | f ^ qH  }	 |	 r d |	 }	 n  d j g  | D] \ } } d | | f ^ q  }
 |
 r |
 d }
 n  d } | t d |  d |	 d | j d |
  } i  } | j t	    t
 | |  | |  } | j | _ t |  | S(	   s  
    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`

    s   , s   %s=%rs   %s=%ss  
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   t   jact   kwkw(   t   _getargspecR   t   listR   R   t   joinR   R   RX   t   globalsR   R6   R9   (   R   R(  t   argst   varargst   varkwt   defaultst   kwargsR   R2   t   kw_strt   kwkw_strt   wrappert   nsRg   (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyt   _nonlin_wrapper  s$    	 22


(G   R6   t
   __future__R    R   R   RY   t   numpyR   t   scipy._lib.sixR   R   R   t   scipy.linalgR   R   R   R	   R
   R   R   R   R   t   scipy.sparse.linalgR   t   scipy.sparseR   R   t   scipy._lib._utilR   R*  t
   linesearchR   R   t   __all__t	   ExceptionR   R!   R&   R-   R3   R   t   stripR7   R9   RP   RS   RR   Rt   RW   t   objectRL   R   R   RM   R   R   R   R   R   R   R   R   R   R7  R   R   R   R   R   R   R   (    (    (    s4   /tmp/pip-build-7oUkmx/scipy/scipy/optimize/nonlin.pyt   <module>j   sh   .					)			
,CD	`],/(:	(