ó
    Eñi A  ã                   ór   • S r SSKrSSKJrJrJrJr  SSKJ	r	J
r
  / SQrSS jrS rS	 rS
 r " S S\
5      rg)z2Nearly exact trust-region optimization subproblem.é    N)ÚnormÚget_lapack_funcsÚsolve_triangularÚ	cho_solveé   )Ú_minimize_trust_regionÚBaseQuadraticSubproblem)Ú_minimize_trustregion_exactÚ estimate_smallest_singular_valueÚsingular_leading_submatrixÚIterativeSubproblemc                 ó|   • Uc  [        S5      e[        U5      (       d  [        S5      e[        X4X#U[        S.UD6$ )a’  
Minimization of scalar function of one or more variables using
a nearly exact 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.
subproblem_maxiter : int, optional
    Maximum number of iterations to perform per subproblem. Only affects
    trust-exact. Default is 25.

    .. versionadded:: 1.17.0
z9Jacobian is required for trust region exact minimization.z?Hessian matrix is required for trust region exact minimization.)ÚargsÚjacÚhessÚ
subproblem)Ú
ValueErrorÚcallabler   r   )ÚfunÚx0r   r   r   Útrust_region_optionss         Ú^/home/mande/repo/quber/.venv/lib/python3.13/site-packages/scipy/optimize/_trustregion_exact.pyr
   r
      sY   € ð0 �{Üð /ó 0ð 	0ä�D�>‰>Üð /ó 0ð 	0ä! #ð :°ÀDÜ-@ñ:à$8ñ:ð :ó    c                 ó¶  • [         R                  " U 5      n U R                  u  pX:w  a  [        S5      e[         R                  " U5      n[         R
                  " U5      n[        U5       H¿  nSX5   -
  U R                  XU4   -  nSX5   -
  U R                  XU4   -  nX5S-   S U R                  US-   S2U4   U-  -   nX5S-   S U R                  US-   S2U4   U-  -   n	[        U5      [        US5      -   [        U5      [        U	S5      -   :¼  a  XdU'   XƒUS-   S& M´  XtU'   X“US-   S& MÁ     [        X5      n
[        U
5      n[        U5      nXË-  nX«-  nXÞ4$ )añ  Given upper triangular matrix ``U`` estimate the smallest singular
value and the correspondent right singular vector in O(n**2) operations.

Parameters
----------
U : ndarray
    Square upper triangular matrix.

Returns
-------
s_min : float
    Estimated smallest singular value of the provided matrix.
z_min : ndarray
    Estimated right singular vector.

Notes
-----
The procedure is based on [1]_ and is done in two steps. First, it finds
a vector ``e`` with components selected from {+1, -1} such that the
solution ``w`` from the system ``U.T w = e`` is as large as possible.
Next it estimate ``U v = w``. The smallest singular value is close
to ``norm(w)/norm(v)`` and the right singular vector is close
to ``v/norm(v)``.

The estimation will be better more ill-conditioned is the matrix.

References
----------
.. [1] Cline, A. K., Moler, C. B., Stewart, G. W., Wilkinson, J. H.
       An estimate for the condition number of a matrix.  1979.
       SIAM Journal on Numerical Analysis, 16(2), 368-375.
z.A square triangular matrix should be provided.r   éÿÿÿÿN)ÚnpÚ
atleast_2dÚshaper   ÚzerosÚemptyÚrangeÚTÚabsr   r   )ÚUÚmÚnÚpÚwÚkÚwpÚwmÚppÚpmÚvÚv_normÚw_normÚs_minÚz_mins                  r   r   r   0   se  € ôD 	�Š�aÓ€AØ�7‰7�D€AàƒvÜÐIÓJÐJô 	�Š�‹€AÜ
�Š�‹€Aô �1ŽXˆØ�‘‰f˜Ÿ™˜A˜D™	Ñ!ˆØ�‘‰g˜Ÿ™˜Q˜T™Ñ"ˆØ�‰sˆtˆW�q—s‘s˜1˜Q™3™4 ˜7‘| B‘Ñ&ˆØ�‰sˆtˆW�q—s‘s˜1˜Q™3™4 ˜7‘| B‘Ñ&ˆäˆr‹7”T˜"˜a“[Ñ ¤C¨£G¬d°2°q«kÑ$9Ó9Øˆa‰DØˆa�‰cˆdŠGàˆa‰DØˆa�‰cˆdŠGñ ô 	˜Ó€Aä�!‹W€FÜ�!‹W€Fð ‰O€Eð ‰J€Eàˆ<Ðr   c                 ó  • [         R                  " U 5      n[         R                  " U5      n[         R                  " [         R                  " U 5      SS9n[         R                  " X-   U-
  5      n[         R
                  " X-
  U-   5      nXE4$ )zò
Given a square matrix ``H`` compute upper
and lower bounds for its eigenvalues (Gregoshgorin Bounds).
Defined ref. [1].

References
----------
.. [1] Conn, A. R., Gould, N. I., & Toint, P. L.
       Trust region methods. 2000. Siam. pp. 19.
r   )Úaxis)r   Údiagr#   ÚsumÚminÚmax)ÚHÚH_diagÚ
H_diag_absÚ
H_row_sumsÚlbÚubs         r   Úgershgorin_boundsr?      si   € ô �WŠW�Q‹Z€FÜ—’˜“€JÜ—’œŸš˜q›	¨Ñ*€JÜ	�Š�Ñ# jÑ0Ó	1€BÜ	�Š�Ñ# jÑ0Ó	1€Bàˆ6€Mr   c                 ó(  • [         R                  " USUS-
  2US-
  4   S-  5      XS-
  US-
  4   -
  n[        U 5      n[         R                  " U5      nSXRS-
  '   US:w  a/  [	        USUS-
  2SUS-
  24   USUS-
  2US-
  4   * 5      USUS-
  & X54$ )a·  
Compute term that makes the leading ``k`` by ``k``
submatrix from ``A`` singular.

Parameters
----------
A : ndarray
    Symmetric matrix that is not positive definite.
U : ndarray
    Upper triangular matrix resulting of an incomplete
    Cholesky decomposition of matrix ``A``.
k : int
    Positive integer such that the leading k by k submatrix from
    `A` is the first non-positive definite leading submatrix.

Returns
-------
delta : float
    Amount that should be added to the element (k, k) of the
    leading k by k submatrix of ``A`` to make it singular.
v : ndarray
    A vector such that ``v.T B v = 0``. Where B is the matrix A after
    ``delta`` is added to its element (k, k).
Nr   é   )r   r6   Úlenr   r   )ÚAr$   r)   Údeltar&   r.   s         r   r   r   ”   s´   € ô6 �FŠF�1�T�a˜‘c�T˜1˜Q™3�Y‘< ‘?Ó# a¨!©¨Q¨q©S¨¡kÑ1€EäˆA‹€Aô 	�Š�‹€AØ€Aˆ�c�Fð 	ˆAƒvÜ" 1 T a¨¡c T¨4¨A¨a©C¨4 Z¡=°1°T°a¸±c°T¸1¸Q¹3°Y±<°-Ó@ˆˆ$ˆ1ˆQ‰3ˆàˆ8€Or   c                   ó€   ^ • \ rS rSrSrSrSr\R                  " \	5      R                  r  S	U 4S jjrS rS rSrU =r$ )
r   é¾   a˜  Quadratic subproblem solved by nearly exact iterative method.

Notes
-----
This subproblem solver was based on [1]_, [2]_ and [3]_,
which implement similar algorithms. The algorithm is basically
that of [1]_ but ideas from [2]_ and [3]_ were also used.

References
----------
.. [1] A.R. Conn, N.I. Gould, and P.L. Toint, "Trust region methods",
       Siam, pp. 169-200, 2000.
.. [2] J. Nocedal and  S. Wright, "Numerical optimization",
       Springer Science & Business Media. pp. 83-91, 2006.
.. [3] J.J. More and D.C. Sorensen, "Computing a trust region step",
       SIAM Journal on Scientific and Statistical Computing, vol. 4(3),
       pp. 553-572, 1983.
g{®Gáz„?é   c	                 ó\  >• [         T	U ]  XX45        SU l        S U l        SU l        X`l        Xpl        Uc  U R                  OUU l        U R                  S:  a  [        S5      e[        SU R                  45      u  U l        [        U R                  5      U l        [        U R                  5      u  U l        U l        [%        U R                  [&        R(                  5      U l        [%        U R                  S5      U l        U R                  U R.                  -  U R*                  -  U l        g )Nr   r   zJmaxiter must not be set to a negative number, use np.inf to mean infinite.)ÚpotrfÚfro)ÚsuperÚ__init__Úprevious_tr_radiusÚ	lambda_lbÚniterÚk_easyÚk_hardÚMAXITER_DEFAULTÚmaxiterr   r   r   ÚcholeskyrB   Ú	dimensionr?   Úhess_gershgorin_lbÚhess_gershgorin_ubr   r   ÚinfÚhess_infÚhess_froÚEPSÚCLOSE_TO_ZERO)
ÚselfÚxr   r   r   ÚhessprP   rQ   rS   Ú	__class__s
            €r   rL   ÚIterativeSubproblem.__init__á   sø   ø€ ô 	‰Ñ˜ Ô+ð #%ˆÔØˆŒàˆŒ
ð ŒØŒð
 07©�t×+Ò+ÀGˆŒØ�<‰<˜!ÓÜð >ó ?ð ?ô *¨*°t·y±y°lÓC‰ˆŒô ˜TŸY™Y›ˆŒä&7¸¿	¹	Ó&Bñ	$ˆÔØÔ#Ü˜TŸY™Y¬¯©Ó/ˆŒÜ˜TŸY™Y¨Ó.ˆŒð "Ÿ^™^¨d¯h©hÑ6¸¿¹ÑFˆÕr   c           
      ó(  • [        SU R                  U-  [        U R                  * U R                  U R
                  5      -   5      n[        S[        U R                  R                  5       5      * U R                  U-  [        U R                  U R                  U R
                  5      -
  5      nXR                  :  a  [        U R                  U5      nUS:X  a  SnO3[        [        R                  " X2-  5      X0R                  X#-
  -  -   5      nXCU4$ )zÉGiven a trust radius, return a good initial guess for
the damping factor, the lower bound and the upper bound.
The values were chosen accordingly to the guidelines on
section 7.3.8 (p. 192) from [1]_.
r   )r8   Újac_magr7   rV   rZ   rY   r   ÚdiagonalrW   rM   rN   r   ÚsqrtÚUPDATE_COEFF)r]   Ú	tr_radiusÚ	lambda_ubrN   Úlambda_initials        r   Ú_initial_valuesÚ#IterativeSubproblem._initial_values  sÿ   € ô ˜˜4Ÿ<™<¨	Ñ1´C¸×9PÑ9PÐ8PØ8<¿¹Ø8<¿¹ó5Gñ Gó Hˆ	ô
 ˜œC §	¡	× 2Ñ 2Ó 4Ó5Ð5ØŸ™ YÑ.´°T×5LÑ5LØ59·]±]Ø59·]±]ó2Dñ DóEˆ	ð ×.Ñ.Ó.Ü˜DŸN™N¨IÓ6ˆIð ˜‹>Ø‰Nä ¤§¢¨Ñ)>Ó!?Ø!*×->Ñ->À	Ñ@SÑ-TÑ!TóVˆNð ¨)Ð3Ð3r   c                 óä  • U R                  U5      u  p#nU R                  nSnSnSU l        U R                  U R                  :  Ga•  U(       a  SnO:U R                  U[
        R                  " U5      -  -   nU R                  USSSS9u  pšU =R                  S-  sl        W
S:X  Gaã  U R                  U R                  :”  GaÈ  [        W	S4U R                  * 5      n[        U5      nXÁ::  a
  US:X  a  SnGOç[        X›SS9n[        U5      nXÎ-  S-  XÁ-
  -  U-  nX/-   nXÁ:  Ga?  [        U	5      u  nnU R                  UUU5      u  nn[!        UU/["        S	9n[
        R$                  " U[
        R$                  " WU5      5      nUS-  US-  -  UX!S-  -  -   -  nUU R&                  ::  a
  UUU-  -  nGO)Un[)        X2US-  -
  5      nU R                  U[
        R                  " U5      -  -   nU R                  USSSS9u  nn
U
S:X  a  UnSnGO²[)        UU5      n[)        [
        R*                  " [
        R"                  " X4-  5      5      X0R,                  XC-
  -  -   5      nGO][#        XÁ-
  5      U-  nUU R.                  ::  a  GOXUnUnGO5U
S:X  a¹  U R                  U R                  ::  aŸ  US:X  a  [
        R0                  " U5      nSnGO[        W	5      u  nnUnUU-  nUS-  US-  -  U R&                  U-  US-  -  ::  a  OÚUn[)        X2US-  -
  5      n[)        [
        R*                  " X4-  5      X0R,                  XC-
  -  -   5      nOv[3        WW	U
5      u  nn[        U5      n[)        X2UUS-  -  -   5      n[)        [
        R*                  " [
        R"                  " X4-  5      5      X0R,                  XC-
  -  -   5      nU R                  U R                  :  a  GM•  X0l        X l        Xl        WU4$ )
zSolve quadratic subproblemTFr   )ÚlowerÚoverwrite_aÚcleanr   r"   )ÚtransrA   )Úkey)rj   rU   rO   rS   r   r   ÚeyerT   rc   r\   r   r   r   r   r   Úget_boundaries_intersectionsr7   r#   ÚdotrQ   r8   re   rf   rP   r   r   rN   Úlambda_currentrM   )r]   rg   ru   rN   rh   r&   Úhits_boundaryÚalready_factorizedr9   r$   Úinfor'   Úp_normr(   r0   Údelta_lambdaÚ
lambda_newr1   r2   ÚtaÚtbÚstep_lenÚquadratic_termÚrelative_errorÚcrD   r.   r/   s                               r   ÚsolveÚIterativeSubproblem.solve1  s  € ð 04×/CÑ/CÀIÓ/NÑ,ˆ 9Ø�N‰NˆØˆØ"ÐØˆŒ
à�j‰j˜4Ÿ<™<Ô'ö "Ø%*Ñ"à—I‘I˜n¬R¯VªV°A«YÑ6Ñ6�ØŸ-™-¨°Ø49Ø.2ð (ð 4‘�ð �JŠJ˜!‰O�Jð �qŒy˜TŸ\™\¨D×,>Ñ,>Ô>ô ˜q %˜j¨4¯8©8¨)Ó4�ä˜a›�ð Ó&¨>¸QÓ+>Ø$)�MÙô % Q°Ñ5�ä˜a›�ð !'¡°Ñ1°VÑ5EÑFÀyÑP�Ø+Ñ:�
àÔ%Ü#CÀAÓ#F‘L�E˜5à!×>Ñ>¸qÀ%Ø?HóJ‘F�B˜ô  # B¨ 8´Ñ5�Hô &(§V¢V¨A¬r¯vªv°a¸«|Ó%<�Nð (0°¡{°U¸A±XÑ'=Ø)7¸.ÐTUÉÑ:UÑ)Uñ'W�Nà%¨¯©Ó4Ø˜X¨Ñ-Ñ-˜Ùð !/�IÜ # IÀÀqÁÑ/HÓ I�Ið Ÿ	™	 J¬r¯vªv°a«yÑ$8Ñ8�AØ"Ÿm™m¨A°UØ8=Ø26ð ,ð 8‘G�A�tð ˜q“yà)3˜Ø-1Ò*ô %(¨	°:Ó$>˜	ô *-ÜŸGšG¤B§F¢F¨9Ñ+@Ó$AÓBØ%×(9Ñ(9¸9Ñ;NÑ(OÑOó*šô &)¨Ñ);Ó%<¸yÑ%H�NØ%¨¯©Ó4Ùð !/�Ið &0’Nà˜“˜tŸ|™|¨t×/AÑ/AÓAð " QÓ&ÜŸš ›�AØ$)�MÙä?ÀÓB‘��uØ$�à˜uÑ$�à˜a‘K %¨¡(Ñ*Ø—{‘{ ^Ñ3°iÀ±lÑBóCàð +�	Ü 	¸EÀ1¹HÑ+DÓE�	ô "%Ü—G’G˜IÑ1Ó2Ø× 1Ñ 1°9Ñ3FÑ GÑGó"‘ô 6°a¸¸DÓA‘��qÜ˜a›�ô   	¸EÀ&È!Á)¹OÑ+KÓL�	ô "%Ü—G’GœBŸFšF 9Ñ#8Ó9Ó:Ø× 1Ñ 1°9Ñ3FÑ GÑGó"�ðO �j‰j˜4Ÿ<™<Ö'ðX #ŒØ,ÔØ"+Ôà�-ÐÐr   )r\   rT   rU   rZ   rV   rW   rY   rP   rQ   ru   rN   rS   rO   rM   )Ngš™™™™™¹?gš™™™™™É?N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__rf   rR   r   ÚfinfoÚfloatÚepsr[   rL   rj   r‚   Ú__static_attributes__Ú__classcell__)r`   s   @r   r   r   ¾   sG   ø† ñð, €Lð €Oà
�(Š(�5‹/×
Ñ
€Cà04Ø15÷/Gòb4÷>Y ð Y r   r   )© NN)rˆ   Únumpyr   Úscipy.linalgr   r   r   r   Ú_trustregionr   r	   Ú__all__r
   r   r?   r   r   rŽ   r   r   Ú<module>r“      sF   ðÙ 8Û ÷%ó %ç Kò"€ô :òFLò^ò*'ôTL Ð1õ L r   