ó
    EñiZ.  ã                   ó�   • 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     €€€ÚX/home/mande/repo/quber/.venv/lib/python3.13/site-packages/scipy/optimize/_trustregion.pyÚfunction_wrapperÚ(_wrap_function.<locals>.function_wrapper   s-   ø€ Øˆq‹	�Q‰‹	áœŸš ›
Ð; l°TÑ&9Ò;Ð;ó    © )r   r   r   r   s   `` @r   Ú_wrap_functionr      s/   ú€ ð ˆS€FØÑØ�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;   € Ø�x‰xœ"Ÿ&š& §¡¨1Ó-Ñ-°´b·f²f¸QÇ
Á
È1ÃÓ6NÑ0NÑ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   ó*   € ð �7‰7‰?Ø—i‘i §¡Ó(ˆDŒGØ�w‰wˆ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*   € ð �7‰7‰?Ø—j‘j §¡Ó)ˆDŒGØ�w‰wˆ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   € Ø�;‰;Ñ"Ø—;‘;˜tŸw™w¨Ó*Ð*ä—6’6˜$Ÿ)™) 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   € ð �;‰;ÑÜŸ,™,×+Ñ+¨D¯H©HÓ5ˆDŒKØ�{‰{Ð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_intersectionsÚ4BaseQuadraticSubproblem.get_boundaries_intersectionsZ   s•   € ô �FŠF�1‹LˆØ”—’�q“ÑˆÜ�FŠF�1‹L˜<¨™?Ñ*ˆÜ ŸIšI a¡c¨A¨a©C°©E¡kÓ2Ðð ”$—-’-Ð 1Ó5Ñ5ˆØˆT�Q�q‘S‰\ˆØ�‰T�C‰ZˆÜ�r�hÓÐ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O½U 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   ø€ Ø—7‘7˜1“:—>‘> !Ó$Ð$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à��O�t�OÜÐ7Ó8Ð8Ø˜1ÓÜÐ?Ó@Ð@Ø˜qÓ ÜÐDÓEÐEØÓ/Üð ,ó -ð 	-ô 
�Š�B‹×	Ñ	Ó	!€Bô 
"Ø�S¨$¸ñ
€Bð �&‰&€CØ
�'‰'€CÜ�‡~�~Ø�w‰w‰Ü	�%�‰ð 	Ø
”*Ó
¤
¨4Ô1F× GÑ Gð ˆö	%ô ð Jó Kð 	Kô # 5Ó/�M€FˆEð �Ü�b“'˜#‘+ˆð €Hð (€LØ
€AÞØ�#ˆàÐÜˆzÐ,×-Ñ-Ø(:Ð˜9Ñ%á�1�c ÑBÐ/AÑB€AØ	€Að �)‰)�tÔ
ð	Ø Ÿw™w |Ó4ÑˆAˆ}ñ ˜A›$ˆð ˜‘Uˆ
Ù 
¨C°dÑXÐEWÑXˆ
ð Ÿ5™5 :§>¡>Ñ1ÐØŸe™e oÑ5ÐØ !Ó#ØˆHØØÐ!4Ñ4ˆð �‹:Ø˜DÑ ‰LØ�4‹ZžMÜ˜q ™~Ð/?Ó@ˆLð �‹9ØˆAØˆAö Ø�N‰Nœ2Ÿ7š7 1›:Ô&Ø	ˆQ‰ˆä,¨q°a·e±eÑ<ÐÜ$ XÐ/B×CÑCØð �9‰9�tÓØˆHØð �‹<ØˆHØðo �)‰)�tÖ
ôv ˜IÑ&Ü˜IÑ&ØHØAð	€Oö Ø�q‹=Ü�/ (Ñ+Õ,ä�MŠM˜/¨(Ñ3´^ÐPQÒRÜÐ1°!·%±%¸°Ð;Ô<ÜÐ% a¨ UÐ+Ô,ÜÐ/°·±¸¨{Ð;Ô<ÜÐ/°·±¸¨{Ð;Ô<ÜÐ.¨r¯w©w¸À¹Ñ/BÀ1Ð.EÐFÔGä˜a¨(°a©-ÀØ !§¡¨1¯5©5°r·w±wÀRÇWÁWØ!#§¡¨6°!©9Ñ!4¸!Ø$3°HÑ$=ñ?€Fð
 ÑØŸ™ˆˆv‰æØ#ˆˆyÑà€Møô] �y‰y×$Ñ$ó 	Øˆ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5ðp IMØADØCGØ@EØ@DØ.2õSr   