o
    Eb%                     @   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u 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   D/usr/lib/python3/dist-packages/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u 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u r| 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r,|||  }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    
