
    doi 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         ]/var/www/html/land-ocr/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'   XUS-   S& M  XtU'   XUS-   S& M     [        X5      n
[        U
5      n[        U5      nX-  nX-  nX4$ )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77DAvIJJ 	A
A 1XfAD	!gQT"stWqss1Q347|B&stWqss1Q347|B&r7T"a[ CGd2qk$99aDacdGaDacdG  	A!WF!WF OE JE<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     WWQZFJq	*J	#j0	1B	#j0	1B6Mr   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 FF1TacT1Q3Y<?#a!QqSk1EAA 	AAcF 	Av"1TacT4AaC4Z=1TacT1Q3Y<-@$1Q38Or   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{Gz?   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<<! > ? ? **tyylC TYY&7		&B	$#TYY/TYY. "^^dhh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$ )zGiven 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<<	1C9P9P8P8<8<5G G H	
 C		 2 2 455Y.T5L5L59]]59]]2D DE	 ...DNNI6I >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  pU =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[        XS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  OU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/CI/N,9NN"
jj4<<' "%*"IInRVVAY66--49.2 ( 4 JJ!OJ qyT\\D,>,>> q%j488)4a &>Q+>$)M %Q5a !'1V5EFyP+:
%#CA#FLE5!>>q%?HJFB  #B85H &(VVArvva|%<N (0{UAX'=)7.TU:U)U'WN%4X-- !/I #Iq/H II 		Jrvvay$88A"mmAU8=26 , 8GAt qy)3-1* %(	:$>	 *-GGBFF9+@$AB%(9(99;N(OO* &));%<y%HN%4 !/I &0Nt||t/A/AA "Q&A$)M?Bu$u$aK%(*{{^3ilBC +		E1H+DE	 "%GGI12 1 193F GG" 6aDAqa  	E&!)O+KL	 "%GGBFF9#89: 1 193F GG"O jj4<<'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/

C0415/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   