
    doiZ.                         S r SSKrSSKrSSKrSSKrSSKJrJ	r	J
r
JrJr  SSKJr  SSKJr  / rS r " S S	5      r      SS
 jrg)zTrust-region optimization.    N   )_check_unknown_options_status_messageOptimizeResult_prepare_scalar_function_call_callback_maybe_halt)HessianUpdateStrategy)
FD_METHODSc                 4   ^ ^^ S/mT c  TS 4$ UU U4S jnTU4$ )Nr   c                 ^   > TS==   S-  ss'   T" [         R                  " U 5      /UT-   Q76 $ )Nr   r   )npcopy)xwrapper_argsargsfunctionncallss     W/var/www/html/land-ocr/venv/lib/python3.13/site-packages/scipy/optimize/_trustregion.pyfunction_wrapper(_wrap_function.<locals>.function_wrapper   s-    q	Q	
;lT&9;;     )r   r   r   r   s   `` @r   _wrap_functionr      s/     SFt|<
 ###r   c                   z    \ rS rSrSrSS jrS r\S 5       r\S 5       r	\S 5       r
S	 r\S
 5       rS rS rSrg)BaseQuadraticSubproblem   a9  
Base/abstract class defining the quadratic model for trust-region
minimization. Child classes must implement the ``solve`` method.

Values of the objective function, Jacobian and Hessian (if provided) at
the current iterate ``x`` are evaluated on demand and then stored as
attributes ``fun``, ``jac``, ``hess``.
Nc                     Xl         S U l        S U l        S U l        S U l        S U l        S U l        X l        X0l        X@l	        XPl
        g N)_x_f_g_h_g_mag_cauchy_point_newton_point_fun_jac_hess_hessp)selfr   funjachesshessps         r   __init__ BaseQuadraticSubproblem.__init__(   sG    !!		
r   c                     U R                   [        R                  " U R                  U5      -   S[        R                  " XR	                  U5      5      -  -   $ )Ng      ?)r+   r   dotr,   r.   r*   ps     r   __call__ BaseQuadraticSubproblem.__call__5   s;    xx"&&1--bffQ

16N0NNNr   c                 t    U R                   c   U R                  U R                  5      U l         U R                   $ )z1Value of objective function at current iteration.)r    r&   r   r*   s    r   r+   BaseQuadraticSubproblem.fun8   *     77?ii(DGwwr   c                 t    U R                   c   U R                  U R                  5      U l         U R                   $ )z=Value of Jacobian of objective function at current iteration.)r!   r'   r   r8   s    r   r,   BaseQuadraticSubproblem.jac?   r:   r   c                 t    U R                   c   U R                  U R                  5      U l         U R                   $ )z<Value of Hessian of objective function at current iteration.)r"   r(   r   r8   s    r   r-   BaseQuadraticSubproblem.hessF   s*     77?jj)DGwwr   c                     U R                   b  U R                  U R                  U5      $ [        R                  " U R                  U5      $ r   )r)   r   r   r2   r-   r3   s     r   r.   BaseQuadraticSubproblem.hesspM   s6    ;;";;tww**66$))Q''r   c                     U R                   c.  [        R                  R                  U R                  5      U l         U R                   $ )zAMagnitude of jacobian of objective function at current iteration.)r#   scipylinalgnormr,   r8   s    r   jac_magBaseQuadraticSubproblem.jac_magS   s2     ;;,,++DHH5DK{{r   c                 F   [         R                  " X"5      nS[         R                  " X5      -  n[         R                  " X5      US-  -
  n[        R                  " XU-  SU-  U-  -
  5      nU[        R                  " Xu5      -   nU* SU-  -  n	SU-  U-  n
[        X/5      $ )z
Solve the scalar quadratic equation ``||z + t d|| == trust_radius``.
This is like a line-sphere intersection.
Return the two values of t, sorted from low to high.
      )r   r2   mathsqrtcopysignsorted)r*   zdtrust_radiusabcsqrt_discriminantauxtatbs              r   get_boundaries_intersections4BaseQuadraticSubproblem.get_boundaries_intersectionsZ   s     FF1LqFF1L<?* IIacAaCEk2 $-- 155TQqS\TCZrhr   c                     [        S5      e)Nz9The solve method should be implemented by the child class)NotImplementedError)r*   rQ   s     r   solveBaseQuadraticSubproblem.solveq   s    ! #4 5 	5r   )r$   r    r&   r!   r#   r"   r(   r)   r'   r%   r   )NN)__name__
__module____qualname____firstlineno____doc__r/   r5   propertyr+   r,   r-   r.   rE   rY   r]   __static_attributes__r   r   r   r   r      sq    O      (   .5r   r   c                   ^& [        U5        Uc  [        S5      eUc  Uc  [        S5      eUc  [        S5      eSU	s=::  a  S:  d  O  [        S5      eUS::  a  [        S5      eUS::  a  [        S	5      eXx:  a  [        S
5      e[        R                  " U5      R                  5       n[        XX4UUS9m&T&R                  n T&R                  n[        U5      (       a  T&R                  nOD[        U5      (       a  O3U[        ;   d  [        U[        5      (       a	  SnU&4S jnO[        S5      e[        XR5      u  nnUc  [        U5      S-  nSnUnUnU(       a  U/n0 n[!        US5      (       a  UUS'   U" UXXE40 UD6nSnUR"                  U
:  Ga   UR%                  U5      u  nnU" U5      nUU-   nU" UXXE40 UD6nUR                  UR                  -
  n UR                  U-
  n!U!S::  a  SnOU U!-  n"U"S:  a  US-  nOU"S:  a  U(       a  [+        SU-  U5      nU"U	:  a  UnUnU(       a%  WR-                  [        R.                  " U5      5        US-  n[1        UUR                  S9n#[3        UU#5      (       a  O/UR"                  U
:  a  SnOUU:  a  SnOUR"                  U
:  a  GM  [4        S   [4        S   SS4n$U(       a  US:X  a  [7        U$U   5        O[8        R:                  " U$U   [<        SS9  [7        SUR                  S 35        [7        SUS 35        [7        ST&R>                  S 35        [7        ST&R@                  S 35        [7        ST&RB                  US   -   S 35        [1        UUS:H  UUR                  URD                  T&R>                  T&R@                  T&RB                  US   -   UU$U   S 9
n%Ub  UR                  U%S!'   U(       a  WU%S"'   U%$ ! [        R&                  R(                   a    Sn GMZ  f = f)#av  
Minimization of scalar function of one or more variables using a
trust-region algorithm.

Options for the trust-region algorithm are:
    initial_trust_radius : float
        Initial trust radius.
    max_trust_radius : float
        Never propose steps that are longer than this value.
    eta : float
        Trust region related acceptance stringency for proposed steps.
    gtol : float
        Gradient norm must be less than `gtol`
        before successful termination.
    maxiter : int
        Maximum number of iterations to perform.
    disp : bool
        If True, print convergence message.
    inexact : bool
        Accuracy to solve subproblems. If True requires less nonlinear
        iterations, but more vector products. Only effective for method
        trust-krylov.
    workers : int, map-like callable, optional
        A map-like callable, such as `multiprocessing.Pool.map` for evaluating
        any numerical differentiation in parallel.
        This evaluation is carried out as ``workers(fun, iterable)``.
        Only for 'trust-krylov', 'trust-ncg'.

        .. versionadded:: 1.16.0
    subproblem_maxiter : int, optional
        Maximum number of iterations to perform per subproblem. Only affects
        trust-exact. Default is 25.

        .. versionadded:: 1.17.0


This function is called by the `minimize` function.
It is not supposed to be called directly.
Nz7Jacobian is currently required for trust-region methodsz_Either the Hessian or the Hessian-vector product is currently required for trust-region methodszBA subproblem solving strategy is required for trust-region methodsr   g      ?zinvalid acceptance stringencyz%the max trust radius must be positivez)the initial trust radius must be positivez?the initial trust radius must be less than the max trust radius)r,   r-   r   workersc                 D   > TR                  U 5      R                  U5      $ r   )r-   r2   )r   r4   r   sfs      r   r.   %_minimize_trust_region.<locals>.hessp   s    771:>>!$$r      MAXITER_DEFAULTmaxiter   rH   g      ?r   )r   r+   successz:A bad approximation caused failure to predict improvement.z3A linalg error occurred, such as a non-psd Hessian.)
stacklevelz!         Current function value: fz         Iterations: rP   z         Function evaluations: z         Gradient evaluations: z         Hessian evaluations: )
r   ro   statusr+   r,   nfevnjevnhevnitmessager-   allvecs)#r   
ValueError	Exceptionr   asarrayflattenr   r+   gradcallabler-   r
   
isinstancer	   r   lenhasattrrE   r]   rC   LinAlgErrorminappendr   r   r   r   printwarningswarnRuntimeWarningrs   ngevru   r,   )'r+   x0r   r,   r-   r.   
subprobleminitial_trust_radiusmax_trust_radiusetagtolrm   disp
return_allcallbackinexactrg   subproblem_maxiterunknown_optionsnhesspwarnflagrQ   r   rx   subproblem_init_kwmkr4   hits_boundarypredicted_value
x_proposed
m_proposedactual_reductionpredicted_reductionrhointermediate_resultstatus_messagesresultri   s'                                         @r   _minimize_trust_regionr   v   sb   Z ?+
{ # $ 	$| J K 	K 0 1 	1OtO7881?@@q DEE/ , - 	- 
B			!B 
"S$
B &&C
''C~~ww	% 	
*

41F G G 	%  J K 	K #5/MFE b'#+ H (L
A#z,--(:9%1cB/ABA	A ))t
	 ww|4A} A$ U

CdXEWX
 55:>>1eeo5!#H!44 :D L4ZMq~/?@L 9AA NN2771:&	Q,qaee<$X/BCC 99tH <Ho ))t
v I&I&HA	O q=/(+,MM/(3^PQR1!%%;<%aU+,/{;</{;<.rww/B1.EFGa(a- !155rwwRWW!#6!9!4!$3H$=?F
 v#yM] yy$$ 	H	s   O% % P
	P
)r   NNNNg      ?g     @@g333333?g-C6?NFFNTNN)rc   rK   r   numpyr   scipy.linalgrB   	_optimizer   r   r   r   r   'scipy.optimize._hessian_update_strategyr	   (scipy.optimize._differentiable_functionsr
   __all__r   r   r   r   r   r   <module>r      sZ         3 3 J ?
$U5 U5p IMADCG@E@D.2Sr   