ó
ÚÆ÷Xc           @` s=  d  Z  d d l m Z m Z m Z d d l Z d d l m Z m Z m	 Z	 m
 Z
 m Z d d l m Z d d l m Z m Z m Z m Z m Z m Z d d l m Z d	 d
 g Z d d' d d d d d d d d d d d d d „ Z d( d d d d d d d d d d d d d „ Z d
 e f d „  ƒ  YZ e d k r9d „  Z d „  Z d „  Z d e  f d „  ƒ  YZ! d Z" d Z# d Z$ d Z% d) g e$ Z& x$ e' d e$ d ƒ D] Z( d* e& e( <qžWx$ e' d e$ d ƒ D] Z( d+ e& e( <qÅWe e$ f e	 ƒ Z) d! e) (e e e) d" e d# e% d$ e" d% e# ƒ\ Z* Z+ Z, e- e* ƒ e- e+ ƒ e- e, ƒ e e e) d& d d# e% d$ e" d% e# ƒ\ Z* Z+ Z, e- e* ƒ e- e+ ƒ e- e, ƒ e e e) d& d d# e% d$ e" d% e# ƒ\ Z* Z+ Z, e- e* ƒ e- e+ ƒ e- e, ƒ e! ƒ  Z. e e. j/ e) d& d d# e% d$ e" d% e# ƒ\ Z* Z+ Z, e- e* ƒ e- e+ ƒ e- e, ƒ n  d S(,   sR   
Functions
---------
.. autosummary::
   :toctree: generated/

    fmin_l_bfgs_b

i    (   t   divisiont   print_functiont   absolute_importN(   t   arrayt   asarrayt   float64t   int32t   zerosi   (   t   _lbfgsb(   t   approx_fprimet
   MemoizeJact   OptimizeResultt   _check_unknown_optionst   wrap_functiont   _approx_fprime_helper(   t   LinearOperatort   fmin_l_bfgs_bt   LbfgsInvHessProducti
   g    ÐcAgñhãˆµøä>g:Œ0âŽyE>iÿÿÿÿi˜:  i   c      
   C` s8  | r |  } d } n0 | d k r9 t |  ƒ } | j } n |  } | } | d k rZ |
 } n  i
 | d 6|
 d 6| d 6| t j t ƒ j d 6| d 6|	 d 6| d 6| d 6| d	 6| d
 6} t | | d | d | d | | } i | d d 6| d d 6| d d 6| d d 6| d d 6} | d } | d } | | | f S(   sÝ  
    Minimize a function func using the L-BFGS-B algorithm.

    Parameters
    ----------
    func : callable f(x,*args)
        Function to minimise.
    x0 : ndarray
        Initial guess.
    fprime : callable fprime(x,*args), optional
        The gradient of `func`.  If None, then `func` returns the function
        value and the gradient (``f, g = func(x, *args)``), unless
        `approx_grad` is True in which case `func` returns only ``f``.
    args : sequence, optional
        Arguments to pass to `func` and `fprime`.
    approx_grad : bool, optional
        Whether to approximate the gradient numerically (in which case
        `func` returns only the function value).
    bounds : list, optional
        ``(min, max)`` pairs for each element in ``x``, defining
        the bounds on that parameter. Use None or +-inf for one of ``min`` or
        ``max`` when there is no bound in that direction.
    m : int, optional
        The maximum number of variable metric corrections
        used to define the limited memory matrix. (The limited memory BFGS
        method does not store the full hessian but uses this many terms in an
        approximation to it.)
    factr : float, optional
        The iteration stops when
        ``(f^k - f^{k+1})/max{|f^k|,|f^{k+1}|,1} <= factr * eps``,
        where ``eps`` is the machine precision, which is automatically
        generated by the code. Typical values for `factr` are: 1e12 for
        low accuracy; 1e7 for moderate accuracy; 10.0 for extremely
        high accuracy.
    pgtol : float, optional
        The iteration will stop when
        ``max{|proj g_i | i = 1, ..., n} <= pgtol``
        where ``pg_i`` is the i-th component of the projected gradient.
    epsilon : float, optional
        Step size used when `approx_grad` is True, for numerically
        calculating the gradient
    iprint : int, optional
        Controls the frequency of output. ``iprint < 0`` means no output;
        ``iprint = 0``    print only one line at the last iteration;
        ``0 < iprint < 99`` print also f and ``|proj g|`` every iprint iterations;
        ``iprint = 99``   print details of every iteration except n-vectors;
        ``iprint = 100``  print also the changes of active set and final x;
        ``iprint > 100``  print details of every iteration including x and g.
    disp : int, optional
        If zero, then no output.  If a positive number, then this over-rides
        `iprint` (i.e., `iprint` gets the value of `disp`).
    maxfun : int, optional
        Maximum number of function evaluations.
    maxiter : int, optional
        Maximum number of iterations.
    callback : callable, optional
        Called after each iteration, as ``callback(xk)``, where ``xk`` is the
        current parameter vector.
    maxls : int, optional
        Maximum number of line search steps (per iteration). Default is 20.

    Returns
    -------
    x : array_like
        Estimated position of the minimum.
    f : float
        Value of `func` at the minimum.
    d : dict
        Information dictionary.

        * d['warnflag'] is

          - 0 if converged,
          - 1 if too many function evaluations or too many iterations,
          - 2 if stopped for another reason, given in d['task']

        * d['grad'] is the gradient at the minimum (should be 0 ish)
        * d['funcalls'] is the number of function calls made.
        * d['nit'] is the number of iterations.

    See also
    --------
    minimize: Interface to minimization algorithms for multivariate
        functions. See the 'L-BFGS-B' `method` in particular.

    Notes
    -----
    License of L-BFGS-B (FORTRAN code):

    The version included here (in fortran code) is 3.0
    (released April 25, 2011).  It was written by Ciyou Zhu, Richard Byrd,
    and Jorge Nocedal <nocedal@ece.nwu.edu>. It carries the following
    condition for use:

    This software is freely available, but we expect that all publications
    describing work using this software, or all commercial products using it,
    quote at least one of the references given below. This software is released
    under the BSD License.

    References
    ----------
    * R. H. Byrd, P. Lu and J. Nocedal. A Limited Memory Algorithm for Bound
      Constrained Optimization, (1995), SIAM Journal on Scientific and
      Statistical Computing, 16, 5, pp. 1190-1208.
    * C. Zhu, R. H. Byrd and J. Nocedal. L-BFGS-B: Algorithm 778: L-BFGS-B,
      FORTRAN routines for large scale bound constrained optimization (1997),
      ACM Transactions on Mathematical Software, 23, 4, pp. 550 - 560.
    * J.L. Morales and J. Nocedal. L-BFGS-B: Remark on Algorithm 778: L-BFGS-B,
      FORTRAN routines for large scale bound constrained optimization (2011),
      ACM Transactions on Mathematical Software, 38, 1.

    t   dispt   iprintt   maxcort   ftolt   gtolt   epst   maxfunt   maxitert   callbackt   maxlst   argst   jact   boundst   gradt   messaget   taskt   nfevt   funcallst   nitt   statust   warnflagt   funt   xN(   t   NoneR
   t
   derivativet   npt   finfot   floatR   t   _minimize_lbfgsb(   t   funct   x0t   fprimeR   t   approx_gradR   t   mt   factrt   pgtolt   epsilonR   R   R   R   R   R   R'   R   t   optst   rest   dt   fR(   (    (    s4   /tmp/pip-build-X4mzal/scipy/scipy/optimize/lbfgsb.pyR   1   s<    w		

	

g    Ð#>c   /      ` s  t  | ƒ | } |	 ‰ | } | t j t ƒ j } t | ƒ j ƒ  } | j \ } | d# k rl d$ g | } n  t	 | ƒ | k r t
 d ƒ ‚ n  g  | D]C \ } } | t j k r¶ d# n | | t j k rÎ d# n | f ^ q” } | d# k	 r| d k rþ d } q| } n  t ˆ d% ƒ \ } ‰ ˆ d# k r@‡  ‡ ‡ f d †  } n ‡  ‡ ‡ f d †  } t | t ƒ } t | t ƒ } t | t ƒ } i d d& 6d d' 6d d( 6d d) 6} xy t d | ƒ D]h } | | \ } } | d# k	 ré| | | <d } n  | d# k	 r| | | <d } n  | | | f | | <q´W| d k s;t
 d	 ƒ ‚ n  t | t ƒ } t d
 t ƒ } t | f t ƒ } t d | | d | d | | d | t ƒ }  t d | t ƒ }! t d d ƒ }" t d d ƒ }# t d t ƒ }$ t d t ƒ }% t d t ƒ }& d |" (d }' xÚ t j | | | | | | | | | |  |! |" | |# |$ |% |& | ƒ |" j ƒ  }( |( j d ƒ r|| | ƒ \ } } q|( j d ƒ rá|' | k r¡d |" (qâ| d |
 k r»d |" (qâ|' d 7}' | d# k	 râ| | ƒ qâqPqW|" j ƒ  j d ƒ j ƒ  }( |( j d ƒ rd }) n4 | d |
 k r2d }) n |' | k rGd }) n d }) |  d | | !j | | ƒ }* |  | | d | | !j | | ƒ }+ |% d }, t |, | ƒ }- t |* |-  |+ |-  ƒ }. t d | d | d | d d |' d |) d |( d  | d! |) d k d" |. ƒ 	S(*   s±  
    Minimize a scalar function of one or more variables using the L-BFGS-B
    algorithm.

    Options
    -------
    disp : bool
       Set to True to print convergence messages.
    maxcor : int
        The maximum number of variable metric corrections used to
        define the limited memory matrix. (The limited memory BFGS
        method does not store the full hessian but uses this many terms
        in an approximation to it.)
    factr : float
        The iteration stops when ``(f^k -
        f^{k+1})/max{|f^k|,|f^{k+1}|,1} <= factr * eps``, where ``eps``
        is the machine precision, which is automatically generated by
        the code. Typical values for `factr` are: 1e12 for low
        accuracy; 1e7 for moderate accuracy; 10.0 for extremely high
        accuracy.
    ftol : float
        The iteration stops when ``(f^k -
        f^{k+1})/max{|f^k|,|f^{k+1}|,1} <= ftol``.
    gtol : float
        The iteration will stop when ``max{|proj g_i | i = 1, ..., n}
        <= gtol`` where ``pg_i`` is the i-th component of the
        projected gradient.
    eps : float
        Step size used for numerical approximation of the jacobian.
    disp : int
        Set to True to print convergence messages.
    maxfun : int
        Maximum number of function evaluations.
    maxiter : int
        Maximum number of iterations.
    maxls : int, optional
        Maximum number of line search steps (per iteration). Default is 20.

    s    length of x0 != length of boundsi    iÿÿÿÿc         ` s7   ˆ |  ˆ  Œ } t  |  ˆ ˆ d ˆ  d | ƒ} | | f S(   NR   t   f0(   R   (   R(   R:   t   g(   R   R6   R'   (    s4   /tmp/pip-build-X4mzal/scipy/scipy/optimize/lbfgsb.pyt   func_and_grad  s    c         ` s(   ˆ |  ˆ  Œ } ˆ |  ˆ  Œ } | | f S(   N(    (   R(   R:   R<   (   R   R'   R   (    s4   /tmp/pip-build-X4mzal/scipy/scipy/optimize/lbfgsb.pyR=     s    i   i   i   s   maxls must be positive.g        i   i   i   t   S60i   i,   i   t   STARTt   FGt   NEW_Xs+   STOP: TOTAL NO. of ITERATIONS EXCEEDS LIMITs4   STOP: TOTAL NO. of f AND g EVALUATIONS EXCEEDS LIMITt    t   CONVi   R'   R   R"   R$   R%   R    R(   t   successt   hess_invN(   NN(    (   NN(   i   N(   i   i   (   Ni   (   R   R+   R,   R-   R   R   t   ravelt   shapeR)   t   lent
   ValueErrort   infR   R   R   R   t   rangeR   R   t   setulbt   tostringt
   startswitht   stript   reshapet   minR   R   (/   R'   R0   R   R   R   R   R   R   R   R   R   R   R   R   R   t   unknown_optionsR3   R5   R4   t   nt   lt   ut   n_function_evalsR=   t   nbdt   low_bndt	   upper_bndt
   bounds_mapt   iR(   R:   R<   t   wat   iwaR!   t   csavet   lsavet   isavet   dsavet   n_iterationst   task_strR&   t   st   yt   n_bfgs_updatest   n_corrsRE   (    (   R   R6   R'   R   s4   /tmp/pip-build-X4mzal/scipy/scipy/optimize/lbfgsb.pyR.   Í   s¤    +
P		


	
	3


			%
c           B` s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   sM  Linear operator for the L-BFGS approximate inverse Hessian.

    This operator computes the product of a vector with the approximate inverse
    of the Hessian of the objective function, using the L-BFGS limited
    memory approximation to the inverse Hessian, accumulated during the
    optimization.

    Objects of this class implement the ``scipy.sparse.linalg.LinearOperator``
    interface.

    Parameters
    ----------
    sk : array_like, shape=(n_corr, n)
        Array of `n_corr` most recent updates to the solution vector.
        (See [1]).
    yk : array_like, shape=(n_corr, n)
        Array of `n_corr` most recent updates to the gradient. (See [1]).

    References
    ----------
    .. [1] Nocedal, Jorge. "Updating quasi-Newton matrices with limited
       storage." Mathematics of computation 35.151 (1980): 773-782.

    c         C` s¢   | j  | j  k s! | j d k r0 t d ƒ ‚ n  | j  \ } } t t |  ƒ j d t j d | | f ƒ | |  _ | |  _	 | |  _
 d t j d | | ƒ |  _ d S(   s   Construct the operator.i   s0   sk and yk must have matching shape, (n_corrs, n)t   dtypeRG   i   s   ij,ij->iN(   RG   t   ndimRI   t   superR   t   __init__R+   R   t   skt   ykRg   t   einsumt   rho(   t   selfRl   Rm   Rg   RS   (    (    s4   /tmp/pip-build-X4mzal/scipy/scipy/optimize/lbfgsb.pyRk   ‹  s    !			c         C` s=  |  j  |  j |  j |  j f \ } } } } t j | d |  j d t ƒ} | j d k r| | j	 d d k r| | j
 d ƒ } n  t j | ƒ } xV t | d d d ƒ D]> } | | t j | | | ƒ | | <| | | | | } q¢ W| }	 xL t | ƒ D]> } | | t j | | |	 ƒ }
 |	 | | | | |
 }	 q÷ W|	 S(   sE  Efficient matrix-vector multiply with the BFGS matrices.

        This calculation is described in Section (4) of [1].

        Parameters
        ----------
        x : ndarray
            An array with shape (n,) or (n,1).

        Returns
        -------
        y : ndarray
            The matrix-vector product

        Rh   t   copyi   i   iÿÿÿÿ(   Rl   Rm   Rg   Ro   R+   R   Rh   t   TrueRi   RG   RP   R   RK   t   dot(   Rp   R(   Rd   Re   Rg   Ro   t   qt   alphaR[   t   rt   beta(    (    s4   /tmp/pip-build-X4mzal/scipy/scipy/optimize/lbfgsb.pyt   _matvec™  s    *""c   
      C` sJ  |  j  |  j |  j |  j f \ } } } } t j d |  j |  j Œ } | } xø t | ƒ D]ê } | | | d d … t j	 f | | t j	 d d … f | | } | | | d d … t j	 f | | t j	 d d … f | | }	 t j
 | t j
 | |	 ƒ ƒ | | | | d d … t j	 f | | t j	 d d … f } qX W| S(   sü   Return a dense array representation of this operator.

        Returns
        -------
        arr : ndarray, shape=(n, n)
            An array with the same shape and containing
            the same data represented by this `LinearOperator`.

        Rh   N(   Rl   Rm   Rg   Ro   R+   t   eyeRh   RG   RK   t   newaxisRs   (
   Rp   Rd   Re   Rg   Ro   t   It   HkR[   t   A1t   A2(    (    s4   /tmp/pip-build-X4mzal/scipy/scipy/optimize/lbfgsb.pyt   todense»  s    
*DD=#(   t   __name__t
   __module__t   __doc__Rk   Rx   R   (    (    (    s4   /tmp/pip-build-X4mzal/scipy/scipy/optimize/lbfgsb.pyR   r  s   		"t   __main__c         C` sg   d |  d d d } x@ t  d |  j d ƒ D]( } | |  | |  | d d d 7} q- W| d 9} | S(   Ng      Ð?i    i   i   i   (   RK   RG   (   R(   R:   R[   (    (    s4   /tmp/pip-build-X4mzal/scipy/scipy/optimize/lbfgsb.pyR/   Ó  s
    &
c         C` sÃ   t  |  j t ƒ } |  d |  d d } d |  d d d |  d | | d <x` t d | j d d ƒ D]D } | } |  | d |  | d } d | d |  | | | | <qi Wd | | d <| S(   Ni   i    i   i   i   iÿÿÿÿ(   R   RG   R   RK   (   R(   R<   t   t1R[   t   t2(    (    s4   /tmp/pip-build-X4mzal/scipy/scipy/optimize/lbfgsb.pyR   Ú  s    &!"c         C` s   t  |  ƒ t |  ƒ f S(   N(   R/   R   (   R(   (    (    s4   /tmp/pip-build-X4mzal/scipy/scipy/optimize/lbfgsb.pyR=   å  s    t   Problemc           B` s   e  Z d  „  Z RS(   c         C` s
   t  | ƒ S(   N(   R=   (   Rp   R(   (    (    s4   /tmp/pip-build-X4mzal/scipy/scipy/optimize/lbfgsb.pyR'   é  s    (   R€   R   R'   (    (    (    s4   /tmp/pip-build-X4mzal/scipy/scipy/optimize/lbfgsb.pyR†   è  s   i   i   g      ð?id   iœÿÿÿi   R1   R3   R4   R5   R2   (    (    (   NN(   g      ð?id   (   iœÿÿÿid   (0   R‚   t
   __future__R    R   R   t   numpyR+   R   R   R   R   R   t    R   t   optimizeR	   R
   R   R   R   R   t   scipy.sparse.linalgR   t   __all__R)   R   R.   R   R€   R/   R   R=   t   objectR†   R4   R5   RS   R3   R   RK   R[   R0   R(   R:   R9   t   printt   pR'   (    (    (    s4   /tmp/pip-build-X4mzal/scipy/scipy/optimize/lbfgsb.pyt   <module>	   sn   (.—		¢`			








	

