
    h                     T    S r SSKrSSKrSSKrSSKJrJr  / r	SS jr
 " S S\5      rg)	z$Newton-CG trust-region optimization.    N   )_minimize_trust_regionBaseQuadraticSubproblemc           	      j    Uc  [        S5      eUc  Uc  [        S5      e[        X4X#UU[        S.UD6$ )a  
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.

z<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_optionss          W/var/www/html/shao/venv/lib/python3.13/site-packages/scipy/optimize/_trustregion_ncg.py_minimize_trust_ncgr      s_    ( { ( ) 	)| O P 	P!# :D(-:N:$8: :    c                       \ rS rSrSrS rSrg)r   *   z:Quadratic subproblem solved by a conjugate gradient methodc                 J   [         R                  " U R                  5      n[        S[        R
                  " U R                  5      5      U R                  -  nU R                  U:  a  SnX$4$ UnU R                  nU* n U R                  U5      n[         R                  " Xx5      n	U	S::  a>  U R                  XWU5      u  pXZU-  -   nX[U-  -   nU " U5      U " U5      :  a  UnOUnSnX4$ [         R                  " Xf5      nX-  nUUU-  -   n[        R                  R                  U5      U:  a   U R                  XWU5      u  pX[U-  -   nSnX4$ UUU-  -   n[         R                  " UU5      n[        R
                  " U5      U:  a  SnUU4$ UU-  nU* UU-  -   nUnUnUnGM&  )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_nexts                         r   solveCGSteihaugSubproblem.solve,   s   2 ==* TYYt||45D	 <<)#!M** HHB  AB&&-Cax
 ::1NaZaZ8d2h&!#J!#J $!00qIOE]F||  (L8 ::1NaZ
 $!00^FVVFF3Nyy(94 %},,&2IWy1},F AAAQ r    N)__name__
__module____qualname____firstlineno____doc__r8   __static_attributes__r:   r   r   r   r   *   s    DRr   r   )r:   NNN)r?   r   numpyr   scipy.linalgr   _trustregionr   r   __all__r   r   r:   r   r   <module>rE      s-    *    K
:>T2 Tr   