ó
î&]\c           @` 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 d d l m Z m Z d d d	 d
 g Z d d d d „ Z d „  Z d „  Z d „  Z d
 e f d „  ƒ  YZ d S(   s2   Nearly exact trust-region optimization subproblem.i    (   t   divisiont   print_functiont   absolute_importN(   t   normt   get_lapack_funcst   solve_triangulart	   cho_solvei   (   t   _minimize_trust_regiont   BaseQuadraticSubproblemt   _minimize_trustregion_exactt    estimate_smallest_singular_valuet   singular_leading_submatrixt   IterativeSubproblemc         K` s^   | d k r t d ƒ ‚ n  | d k r6 t d ƒ ‚ n  t |  | d | d | d | d t | S(   s  
    Minimization of scalar function of one or more variables using
    a nearly exact trust-region algorithm.

    Options
    -------
    initial_tr_radius : float
        Initial trust-region radius.
    max_tr_radius : float
        Maximum value of the trust-region radius. No steps that are longer
        than this value will be proposed.
    eta : float
        Trust region related acceptance stringency for proposed steps.
    gtol : float
        Gradient norm must be less than ``gtol`` before successful
        termination.
    s9   Jacobian is required for trust region exact minimization.s?   Hessian matrix is required for trust region exact minimization.t   argst   jact   hesst
   subproblemN(   t   Nonet
   ValueErrorR   R   (   t   funt   x0R   R   R   t   trust_region_options(    (    s@   lib/python2.7/site-packages/scipy/optimize/_trustregion_exact.pyR	      s    c         C` s°  t  j |  ƒ }  |  j \ } } | | k r9 t d ƒ ‚ n  t  j | ƒ } t  j | ƒ } xt | ƒ D]} d | | |  j | | f } d | | |  j | | f } | | d |  j | d d … | f | } | | d |  j | d d … | f | }	 t | ƒ t	 | d ƒ t | ƒ t	 |	 d ƒ k rO| | | <| | | d )qd | | | <|	 | | d )qd Wt
 |  | ƒ }
 t	 |
 ƒ } t	 | ƒ } | | } |
 | } | | f S(   sY  Given upper triangular matrix ``U`` estimate the smallest singular
    value and the correspondent right singular vector in O(n**2) operations.

    Parameters
    ----------
    U : ndarray
        Square upper triangular matrix.

    Returns
    -------
    s_min : float
        Estimated smallest singular value of the provided matrix.
    z_min : ndarray
        Estimatied right singular vector.

    Notes
    -----
    The procedure is based on [1]_ and is done in two steps. First it finds
    a vector ``e`` with components selected from {+1, -1} such that the
    solution ``w`` from the system ``U.T w = e`` is as large as possible.
    Next it estimate ``U v = w``. The smallest singular value is close
    to ``norm(w)/norm(v)`` and the right singular vector is close
    to ``v/norm(v)``.

    The estimation will be better more ill-conditioned is the matrix.

    References
    ----------
    .. [1] Cline, A. K., Moler, C. B., Stewart, G. W., Wilkinson, J. H.
           An estimate for the condition number of a matrix.  1979.
           SIAM Journal on Numerical Analysis, 16(2), 368-375.
    s.   A square triangular matrix should be provided.i   iÿÿÿÿN(   t   npt
   atleast_2dt   shapeR   t   zerost   emptyt   ranget   Tt   absR   R   (   t   Ut   mt   nt   pt   wt   kt   wpt   wmt   ppt   pmt   vt   v_normt   w_normt   s_mint   z_min(    (    s@   lib/python2.7/site-packages/scipy/optimize/_trustregion_exact.pyR
   .   s,    "--2



c         C` st   t  j |  ƒ } t  j | ƒ } t  j t  j |  ƒ d d ƒ} t  j | | | ƒ } t  j | | | ƒ } | | f S(   s  
    Given a square matrix ``H`` compute upper
    and lower bounds for its eigenvalues (Gregoshgorin Bounds).
    Defined ref. [1].

    References
    ----------
    .. [1] Conn, A. R., Gould, N. I., & Toint, P. L.
           Trust region methods. 2000. Siam. pp. 19.
    t   axisi   (   R   t   diagR   t   sumt   mint   max(   t   Ht   H_diagt
   H_diag_abst
   H_row_sumst   lbt   ub(    (    s@   lib/python2.7/site-packages/scipy/optimize/_trustregion_exact.pyt   gershgorin_bounds}   s    c         C` sÑ   t  j | d | d … | d f d ƒ |  | d | d f } t |  ƒ } t  j | ƒ } d | | d <| d k rÇ t | d | d … d | d … f | d | d … | d f ƒ | | d *n  | | f S(   s  
    Compute term that makes the leading ``k`` by ``k``
    submatrix from ``A`` singular.

    Parameters
    ----------
    A : ndarray
        Symmetric matrix that is not positive definite.
    U : ndarray
        Upper triangular matrix resulting of an incomplete
        Cholesky decomposition of matrix ``A``.
    k : int
        Positive integer such that the leading k by k submatrix from
        `A` is the first non-positive definite leading submatrix.

    Returns
    -------
    delta : float
        Amount that should be added to the element (k, k) of the
        leading k by k submatrix of ``A`` to make it singular.
    v : ndarray
        A vector such that ``v.T B v = 0``. Where B is the matrix A after
        ``delta`` is added to its element (k, k).
    Ni   i   (   R   R/   t   lenR   R   (   t   AR   R#   t   deltaR    R(   (    (    s@   lib/python2.7/site-packages/scipy/optimize/_trustregion_exact.pyR   ’   s    AQc           B` sJ   e  Z d  Z d Z e j e ƒ j Z d d d d „ Z
 d „  Z d „  Z RS(   sÔ  Quadratic subproblem solved by nearly exact iterative method.

    Notes
    -----
    This subproblem solver was based on [1]_, [2]_ and [3]_,
    which implement similar algorithms. The algorithm is basically
    that of [1]_ but ideas from [2]_ and [3]_ were also used.

    References
    ----------
    .. [1] A.R. Conn, N.I. Gould, and P.L. Toint, "Trust region methods",
           Siam, pp. 169-200, 2000.
    .. [2] J. Nocedal and  S. Wright, "Numerical optimization",
           Springer Science & Business Media. pp. 83-91, 2006.
    .. [3] J.J. More and D.C. Sorensen, "Computing a trust region step",
           SIAM Journal on Scientific and Statistical Computing, vol. 4(3),
           pp. 553-572, 1983.
    g{®Gáz„?gš™™™™™¹?gš™™™™™É?c         C` sß   t  t |  ƒ j | | | | ƒ d |  _ d  |  _ d |  _ | |  _ | |  _ t	 d |  j
 f ƒ \ |  _ t |  j
 ƒ |  _ t |  j
 ƒ \ |  _ |  _ t |  j
 t j ƒ |  _ t |  j
 d ƒ |  _ |  j |  j |  j |  _ d  S(   Niÿÿÿÿi    t   potrft   fro(   R<   (   t   superR   t   __init__t   previous_tr_radiusR   t	   lambda_lbt   nitert   k_easyt   k_hardR   R   t   choleskyR9   t	   dimensionR8   t   hess_gershgorin_lbt   hess_gershgorin_ubR   R   t   Inft   hess_inft   hess_frot   EPSt   CLOSE_TO_ZERO(   t   selft   xR   R   R   t   hesspRC   RD   (    (    s@   lib/python2.7/site-packages/scipy/optimize/_trustregion_exact.pyR?   ×   s    					c         C` sã   t  d |  j | t |  j |  j |  j ƒ ƒ } t  d t |  j j ƒ  ƒ |  j | t |  j |  j |  j ƒ ƒ } | |  j	 k  r– t  |  j
 | ƒ } n  | d k r« d } n+ t  t j | | ƒ | |  j | | ƒ } | | | f S(   sé   Given a trust radius, return a good initial guess for
        the damping factor, the lower bound and the upper bound.
        The values were chosen accordingly to the guidelines on
        section 7.3.8 (p. 192) from [1]_.
        i    (   R1   t   jac_magR0   RG   RK   RJ   R   t   diagonalRH   R@   RA   R   t   sqrtt   UPDATE_COEFF(   RN   t	   tr_radiust	   lambda_ubRA   t   lambda_initial(    (    s@   lib/python2.7/site-packages/scipy/optimize/_trustregion_exact.pyt   _initial_values   s    	c         C` sT  |  j  | ƒ \ } } } |  j } t } t } d |  _ xöt r.| rN t } nA |  j | t j | ƒ } |  j | d t d t d t ƒ\ }	 }
 |  j d 7_ |
 d k ræ|  j	 |  j
 k ræt |	 t f |  j ƒ } t | ƒ } | | k r| d k rt } Pn  t |	 | d d ƒ} t | ƒ } | | d | | | } | | } | | k  r°t |	 ƒ \ } } |  j | | | ƒ \ } } t | | g d	 t ƒ} t j | t j | | ƒ ƒ } | d | d | | | d } | |  j k rú| | | 7} Pn  | } t | | | d ƒ } |  j | t j | ƒ } |  j | d t d t d t ƒ\ } }
 |
 d k rs| } t } qãt | | ƒ } t t j | | ƒ | |  j | | ƒ } q+t | | ƒ | } | |  j k r×Pn  | } | } q9 |
 d k rÁ|  j	 |  j
 k rÁ| d k r)t j | ƒ } t } Pn  t |	 ƒ \ } } | } | d | d |  j | | d k rv| | } Pn  | } t | | | d ƒ } t t j | | ƒ | |  j | | ƒ } q9 t | |	 |
 ƒ \ } } t | ƒ } t | | | | d ƒ } t t j | | ƒ | |  j | | ƒ } q9 W| |  _ | |  _ | |  _ | | f S(
   s   Solve quadratic subproblemi    t   lowert   overwrite_at   cleani   t   transR   i   t   key(   RX   RF   t   Truet   FalseRB   R   R   t   eyeRE   RQ   RM   R   R   R   R   R
   t   get_boundaries_intersectionsR0   R   t   dotRD   R1   RS   RT   RC   R   R   RA   t   lambda_currentR@   (   RN   RU   Rc   RA   RV   R    t   hits_boundaryt   already_factorizedR2   R   t   infoR!   t   p_normR"   R*   t   delta_lambdat
   lambda_newR+   R,   t   tat   tbt   step_lent   quadratic_termt   relative_errort   cR;   R(   R)   (    (    s@   lib/python2.7/site-packages/scipy/optimize/_trustregion_exact.pyt   solve  s’    				
"		'
			N(   t   __name__t
   __module__t   __doc__RT   R   t   finfot   floatt   epsRL   R   R?   RX   Rp   (    (    (    s@   lib/python2.7/site-packages/scipy/optimize/_trustregion_exact.pyR   ¼   s   (	(    (   Rs   t
   __future__R    R   R   t   numpyR   t   scipy.linalgR   R   R   R   t   _trustregionR   R   t   __all__R   R	   R
   R8   R   R   (    (    (    s@   lib/python2.7/site-packages/scipy/optimize/_trustregion_exact.pyt   <module>   s   "		O		*