o
    `^h%  ã                   @   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ÚhessÚ
subproblem)Ú
ValueErrorÚcallabler   ÚDoglegSubproblem)ÚfunÚx0r   r   r   Útrust_region_optionsr   r   ú`/home/air/shanriGPT/back/venv/lib/python3.10/site-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)Ú_cauchy_pointr   ÚhesspÚnpÚdot)ÚselfÚgÚ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)Ú_newton_pointr   r   ÚscipyÚlinalgÚ
cho_factorÚ	cho_solve)r   r   ÚBÚ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   Únormr   Úget_boundaries_intersections)	r   Útrust_radiusÚp_bestÚhits_boundaryÚp_uÚp_u_normÚ
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)r0   Únumpyr   Úscipy.linalgr   Ú_trustregionr   r   Ú__all__r   r   r   r   r   r   Ú<module>   s    
