U
    Gvf%  ã                   @   sF   d Z ddlZddlZddlmZmZ g Zd
dd„Z	G dd	„ d	eƒZ
dS )z"Dog-leg trust-region optimization.é    Né   )Ú_minimize_trust_regionÚBaseQuadraticSubproblem© c                 K   s<   |dkrt dƒ‚t|ƒs t dƒ‚t| |f|||tdœ|—ŽS )a   
    Minimization of scalar function of one or more variables using
    the dog-leg 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.

    Nz,Jacobian is required for dogleg minimizationz+Hessian is required for dogleg minimization)ÚargsÚjacÚhessZ
subproblem)Ú
ValueErrorÚcallabler   ÚDoglegSubproblem)ZfunZx0r   r   r   Ztrust_region_optionsr   r   úF/tmp/pip-unpacked-wheel-96ln3f52/scipy/optimize/_trustregion_dogleg.pyÚ_minimize_dogleg	   s    ÿþr   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	r   z0Quadratic subproblem solved by the dogleg methodc                 C   s@   | j dkr:| j}|  |¡}t ||¡t ||¡  | | _ | j S )zV
        The Cauchy point is minimal along the direction of steepest descent.
        N)Z_cauchy_pointr   ZhesspÚnpÚdot)ÚselfÚgZBgr   r   r   Úcauchy_point)   s
    

 zDoglegSubproblem.cauchy_pointc                 C   s:   | j dkr4| j}| j}tj |¡}tj ||¡ | _ | j S )zS
        The Newton point is a global minimum of the approximate function.
        N)Z_newton_pointr   r   ÚscipyÚlinalgZ
cho_factorZ	cho_solve)r   r   ÚBZcho_infor   r   r   Únewton_point3   s    
zDoglegSubproblem.newton_pointc           	      C   sŠ   |   ¡ }tj |¡|k r$d}||fS |  ¡ }tj |¡}||krX|||  }d}||fS |  ||| |¡\}}||||   }d}||fS )aŒ  
        Minimize a function using the dog-leg trust-region algorithm.

        This algorithm requires function values and first and second derivatives.
        It also performs a costly Hessian decomposition for most iterations,
        and the Hessian is required to be positive definite.

        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
        -----
        The Hessian is required to be positive definite.

        References
        ----------
        .. [1] Jorge Nocedal and Stephen Wright,
               Numerical Optimization, second edition,
               Springer-Verlag, 2006, page 73.
        FT)r   r   r   Znormr   Zget_boundaries_intersections)	r   Ztrust_radiusZp_bestZhits_boundaryZp_uZp_u_normZ
p_boundaryÚ_Útbr   r   r   Úsolve>   s     "ÿzDoglegSubproblem.solveN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   &   s   
r   )r   NN)r   Znumpyr   Zscipy.linalgr   Z_trustregionr   r   Ú__all__r   r   r   r   r   r   Ú<module>   s   
