ó
î&]\c           @` s‘   d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l Z	 d d l
 m Z m Z g  Z d	 d d d d „ Z d e f d „  ƒ  YZ d S(
   s$   Newton-CG trust-region optimization.i    (   t   divisiont   print_functiont   absolute_importNi   (   t   _minimize_trust_regiont   BaseQuadraticSubproblemc         K` sp   | d k r t d ƒ ‚ n  | d k rB | d k rB t d ƒ ‚ n  t |  | d | d | d | d | d t | S(	   s2  
    Minimization of scalar function of one or more variables using
    the Newton conjugate gradient trust-region algorithm.

    Options
    -------
    initial_trust_radius : float
        Initial trust-region radius.
    max_trust_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.

    s<   Jacobian is required for Newton-CG trust-region minimizationsd   Either the Hessian or the Hessian-vector product is required for Newton-CG trust-region minimizationt   argst   jact   hesst   hesspt
   subproblemN(   t   Nonet
   ValueErrorR   t   CGSteihaugSubproblem(   t   funt   x0R   R   R   R   t   trust_region_options(    (    s>   lib/python2.7/site-packages/scipy/optimize/_trustregion_ncg.pyt   _minimize_trust_ncg   s    	R   c           B` s   e  Z d  Z d „  Z RS(   s:   Quadratic subproblem solved by a conjugate gradient methodc         C` s   t  j |  j ƒ } t d t j |  j ƒ ƒ |  j } |  j | k  rS t } | | f S| } |  j } | } xt rû|  j	 | ƒ } t  j
 | | ƒ }	 |	 d k r|  j | | | ƒ \ }
 } | |
 | } | | | } |  | ƒ |  | ƒ k  r÷ | } n | } t } | | f St  j
 | | ƒ } | |	 } | | | } t j j | ƒ | k rˆ|  j | | | ƒ \ }
 } | | | } t } | | f S| | | } t  j
 | | ƒ } t j | ƒ | k  rÍt } | | f S| | } | | | } | } | } | } ql Wd S(   s´  
        Solve the subproblem using a conjugate gradient method.

        Parameters
        ----------
        trust_radius : float
            We are allowed to wander only this far away from the origin.

        Returns
        -------
        p : ndarray
            The proposed step.
        hits_boundary : bool
            True if the proposed step is on the boundary of the trust region.

        Notes
        -----
        This is algorithm (7.2) of Nocedal and Wright 2nd edition.
        Only the function that computes the Hessian-vector product is required.
        The Hessian itself is not required, and the Hessian does
        not need to be positive semidefinite.
        g      à?i    N(   t   npt
   zeros_likeR   t   mint   matht   sqrtt   jac_magt   Falset   TrueR   t   dott   get_boundaries_intersectionst   scipyt   linalgt   norm(   t   selft   trust_radiust   p_origint	   tolerancet   hits_boundaryt   zt   rt   dt   Bdt   dBdt   tat   tbt   pat   pbt
   p_boundaryt	   r_squaredt   alphat   z_nextt   r_nextt   r_next_squaredt	   beta_nextt   d_next(    (    s>   lib/python2.7/site-packages/scipy/optimize/_trustregion_ncg.pyt   solve.   sL    "
			




(   t   __name__t
   __module__t   __doc__R4   (    (    (    s>   lib/python2.7/site-packages/scipy/optimize/_trustregion_ncg.pyR   ,   s   (    (   R7   t
   __future__R    R   R   R   t   numpyR   t   scipy.linalgR   t   _trustregionR   R   t   __all__R
   R   R   (    (    (    s>   lib/python2.7/site-packages/scipy/optimize/_trustregion_ncg.pyt   <module>   s   