o
    `^hä  ã                   @   sN   d 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„Z
G dd	„ d	eƒZdS )z$Newton-CG trust-region optimization.é    Né   )Ú_minimize_trust_regionÚBaseQuadraticSubproblem© c                 K   sF   |du rt dƒ‚|du r|du rt dƒ‚t| |f||||tdœ|¤ŽS )a2  
    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.

    Nz<Jacobian is required for Newton-CG trust-region minimizationzdEither the Hessian or the Hessian-vector product is required for Newton-CG trust-region minimization)ÚargsÚjacÚhessÚhesspÚ
subproblem)Ú
ValueErrorr   ÚCGSteihaugSubproblem)ÚfunÚx0r   r   r   r	   Útrust_region_optionsr   r   ú]/home/air/shanriGPT/back/venv/lib/python3.10/site-packages/scipy/optimize/_trustregion_ncg.pyÚ_minimize_trust_ncg   s   ÿþr   c                   @   s   e Zd ZdZdd„ ZdS )r   z:Quadratic subproblem solved by a conjugate gradient methodc                 C   sj  t  | j¡}tdt | j¡ƒ| j }| j|k rd}||fS |}| j}| }	 |  |¡}t  ||¡}	|	dkr]|  	|||¡\}
}||
|  }|||  }| |ƒ| |ƒk rU|}n|}d}||fS t  ||¡}||	 }|||  }t
j |¡|krŠ|  	|||¡\}
}|||  }d}||fS |||  }t  ||¡}t |¡|k r£d}||fS || }| ||  }|}|}|}q&)a´  
        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      à?FTr   )ÚnpÚ
zeros_liker   ÚminÚmathÚsqrtÚjac_magr	   ÚdotÚget_boundaries_intersectionsÚscipyÚlinalgÚnorm)ÚselfÚtrust_radiusÚp_originÚ	toleranceÚhits_boundaryÚzÚrÚdÚBdÚdBdÚtaÚtbÚpaÚpbÚ
p_boundaryÚ	r_squaredÚalphaÚz_nextÚr_nextÚr_next_squaredÚ	beta_nextÚd_nextr   r   r   Úsolve,   sN   

ØzCGSteihaugSubproblem.solveN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r3   r   r   r   r   r   *   s    r   )r   NNN)r7   r   Únumpyr   Úscipy.linalgr   Ú_trustregionr   r   Ú__all__r   r   r   r   r   r   Ú<module>   s    
