Ë
    âQ(hÆ<  ã                   óp   — d Z ddlZddlmZmZmZmZ ddlm	Z	m
Z
 g d¢Zdd„Zd„ Zd	„ Zd
„ Z G d„ de
«      Zy)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                 ót   — |€t        d«      ‚t        |«      st        d«      ‚t        | |f|||t        dœ|¤ŽS )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.
    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         ú_/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/scipy/optimize/_trustregion_exact.pyr
   r
      s[   € ð( €{Üð /ó 0ð 	0ä�DŒ>Üð /ó 0ð 	0ä! # rð :°¸#ÀDÜ-@ñ:à$8ñ:ð :ó    c                 óÆ  — t        j                  | «      } | j                  \  }}||k7  rt        d«      ‚t        j                  |«      }t        j
                  |«      }t        |«      D ]Æ  }d||   z
  | j                  ||f   z  }d||   z
  | j                  ||f   z  }||dz   d | j                  |dz   d…|f   |z  z   }||dz   d | j                  |dz   d…|f   |z  z   }	t        |«      t        |d«      z   t        |«      t        |	d«      z   k\  r|||<   |||dz   d Œº|||<   |	||dz   d ŒÈ t        | |«      }
t        |
«      }t        |«      }||z  }|
|z  }||fS )aY  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   ,   sŠ  € ôD 	�‰�aÓ€AØ�7‰7�D€A€qàˆA‚vÜÐIÓJÐJô 	�‰�‹€AÜ
�‰�‹€Aô �1‹Xò ˆØ��!‘‰f˜Ÿ™˜A˜q˜D™	Ñ!ˆØ��1‘‰g˜Ÿ™˜Q ˜T™Ñ"ˆØˆq�‰sˆtˆW�q—s‘s˜1˜Q™3™4 ˜7‘| B‘Ñ&ˆØˆq�‰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ˆa‰DØˆAˆa�‰cˆd‰GàˆAˆa‰DØˆAˆa�‰cˆd‰Gðô 	˜˜AÓ€Aä�!‹W€FÜ�!‹W€Fð �V‰O€Eð �‰J€Eà�%ˆ<Ðr   c                 ó  — t        j                  | «      }t        j                  |«      }t        j                  t        j                  | «      d¬«      }t        j                  ||z   |z
  «      }t        j
                  ||z
  |z   «      }||fS )a  
    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?   {   so   € ô �W‰W�Q‹Z€FÜ—‘˜“€JÜ—‘œŸ™˜q›	¨Ô*€JÜ	�‰�˜Ñ# jÑ0Ó	1€BÜ	�‰�˜Ñ# jÑ0Ó	1€Bàˆrˆ6€Mr   c                 ó(  — t        j                  |d|dz
  …|dz
  f   dz  «      | |dz
  |dz
  f   z
  }t        | «      }t        j                  |«      }d||dz
  <   |dk7  r/t	        |d|dz
  …d|dz
  …f   |d|dz
  …|dz
  f    «      |d|dz
   ||fS )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€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                   óp   ‡ — e Zd ZdZdZ ej                  e«      j                  Z		 	 dˆ fd„	Z
d„ Zd„ Zˆ xZS )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         ‰| �  ||||«       d| _        d | _        d| _        || _        || _        t        d| j                  f«      \  | _	        t        | j                  «      | _        t        | j                  «      \  | _        | _        t        | j                  t         j"                  «      | _        t        | j                  d«      | _        | j                  | j(                  z  | j$                  z  | _        y )Nr   r   )ÚpotrfÚfro)ÚsuperÚ__init__Úprevious_tr_radiusÚ	lambda_lbÚniterÚk_easyÚk_hardr   r   ÚcholeskyrB   Ú	dimensionr?   Úhess_gershgorin_lbÚhess_gershgorin_ubr   r   ÚinfÚhess_infÚhess_froÚEPSÚCLOSE_TO_ZERO)	ÚselfÚxr   r   r   ÚhessprN   rO   Ú	__class__s	           €r   rJ   zIterativeSubproblem.__init__Õ   sÍ   ø€ ô 	‰Ñ˜˜C  dÔ+ð #%ˆÔØˆŒàˆŒ
ð ˆŒØˆŒô
 *¨*°t·y±y°lÓC‰ˆŒô ˜TŸY™Y›ˆŒä&7¸¿	¹	Ó&Bñ	$ˆÔØÔ#Ü˜TŸY™Y¬¯©Ó/ˆŒÜ˜TŸY™Y¨Ó.ˆŒð "Ÿ^™^¨d¯h©hÑ6¸¿¹ÑFˆÕr   c           
      ó,  — t        d| j                  |z  t        | j                   | j                  | j
                  «      z   «      }t        dt        | j                  j                  «       «       | j                  |z  t        | j                  | j                  | j
                  «      z
  «      }|| j                  k  rt        | j                  |«      }|dk(  rd}n5t        t        j                  ||z  «      || j                  ||z
  z  z   «      }|||fS )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   rR   rV   rU   r   ÚdiagonalrS   rK   rL   r   ÚsqrtÚUPDATE_COEFF)rY   Ú	tr_radiusÚ	lambda_ubrL   Úlambda_initials        r   Ú_initial_valuesz#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ˆ	ð �t×.Ñ.Ò.Ü˜DŸN™N¨IÓ6ˆIð ˜Š>Ø‰Nä ¤§¡¨°YÑ)>Ó!?Ø!*¨T×->Ñ->À	È)Ñ@SÑ-TÑ!TóVˆNð ˜y¨)Ð3Ð3r   c                 ó@  — | j                  |«      \  }}}| j                  }d}d}d| _        	 |rd}n=| j                  |t	        j
                  |«      z  z   }| j                  |ddd¬«      \  }	}
| xj                  dz  c_        
dk(  �rÙ| j                  | j                  kD  �r¿t        	df| j                   «      }t        |«      }||k  r	|dk(  rd}�n°t        |	|d¬«      }t        |«      }||z  dz  ||z
  z  |z  }||z   }||k  �r0t        |	«      \  }}| j                  |||«      \  }}t        ||gt         ¬	«      }t	        j"                  |t	        j"                  |«      «      }|dz  |dz  z  |||dz  z  z   z  }|| j$                  k  r
|||z  z  }�nê|}t'        |||dz  z
  «      }| j                  |t	        j
                  |«      z  z   }| j                  |ddd¬«      \  }}
|
dk(  r|}d}�nŒt'        ||«      }t'        t	        j(                  ||z  «      || j*                  ||z
  z  z   «      }�nIt!        ||z
  «      |z  }|| j,                  k  r�n)|}|}�n!|
dk(  r·| j                  | j                  k  rž|dk(  rt	        j.                  |«      }d}nèt        	«      \  }}|}|dz  |dz  z  | j$                  |z  |dz  z  k  r||z  }n±|}t'        |||dz  z
  «      }t'        t	        j(                  ||z  «      || j*                  ||z
  z  z   «      }net1        	|
«      \  }}t        |«      }t'        ||||dz  z  z   «      }t'        t	        j(                  ||z  «      || j*                  ||z
  z  z   «      }�ŒY|| _        || _        || _        ||fS )
zSolve quadratic subproblemTFr   )ÚlowerÚoverwrite_aÚcleanr   r"   )ÚtransrA   )Úkey)re   rQ   rM   r   r   ÚeyerP   r^   rX   r   r   r   r   r   Úget_boundaries_intersectionsr7   r#   ÚdotrO   r8   r`   ra   rN   r   r   rL   Úlambda_currentrK   )rY   rb   ro   rL   rc   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   ÚsolvezIterativeSubproblem.solve  s   € ð 04×/CÑ/CÀIÓ/NÑ,ˆ˜	 9Ø�N‰NˆØˆØ"ÐØˆŒ
àñ "Ø%*Ñ"à—I‘I˜n¬R¯V©V°A«YÑ6Ñ6�ØŸ-™-¨°Ø49Ø.2ð (ó 4‘��4ð �JŠJ˜!‰O�Jð �q‹y˜TŸ\™\¨D×,>Ñ,>Ó>ô ˜q %˜j¨4¯8©8¨)Ó4�ä˜a›�ð ˜YÒ&¨>¸QÒ+>Ø$)�MÙô % Q¨°Ô5�ä˜a›�ð !' v¡°Ñ1°V¸IÑ5EÑFÀyÑP�Ø+¨lÑ:�
à˜IÓ%Ü#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 I°	Ñ$9Ó:Ø%¨×(9Ñ(9¸9ÀYÑ;NÑ(OÑOó*šô &)¨°)Ñ);Ó%<¸yÑ%H�NØ%¨¯©Ò4Ùð !/�Ið &0’Nà˜’˜tŸ|™|¨t×/AÑ/AÒAð " QÒ&ÜŸ™ ›�AØ$)�MØä?ÀÓB‘��uØ$�ð ˜a‘K %¨¡(Ñ*Ø—{‘{ ^Ñ3°iÀ±lÑBòCà  5Ñ(�AØð +�	Ü 	¨>¸EÀ1¹HÑ+DÓE�	ô "%Ü—G‘G˜I¨	Ñ1Ó2Ø × 1Ñ 1°9¸YÑ3FÑ GÑGó"‘ô 6°a¸¸DÓA‘��qÜ˜a›�ô   	¨>¸EÀ&È!Á)¹OÑ+KÓL�	ô "%Ü—G‘G˜I¨	Ñ1Ó2Ø × 1Ñ 1°9¸YÑ3FÑ GÑGó"�ñO ðX #ˆŒØ,ˆÔØ"+ˆÔà�-ÐÐr   )Ngš™™™™™¹?gš™™™™™É?)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ra   r   ÚfinfoÚfloatÚepsrW   rJ   re   r|   Ú__classcell__)r\   s   @r   r   r   º   s<   ø„ ñð, €Là
ˆ"�(‰(�5‹/×
Ñ
€Cà04Ø$'õ'GòR4ö>Y r   r   )© NN)r€   Únumpyr   Úscipy.linalgr   r   r   r   Ú_trustregionr   r	   Ú__all__r
   r   r?   r   r   r…   r   r   ú<module>rŠ      sE   ðÙ 8Û ÷%ó %ç Kò"€ó:ò>Lò^ò*'ôT| Ð1õ | r   