
    ^Mh<                     x    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dS )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IterativeSubproblem c                     |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.
    Nz9Jacobian 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         a/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/scipy/optimize/_trustregion_exact.pyr
   r
      sv    ( { / 0 0 	0D>> 0 / 0 0 	0!#r :#D-@: :$8: : :    c                    t          j        |           } | j        \  }}||k    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7DAqAvvIJJJ 	A
A 1XX  !fAqD	!1gQT"qsttWqs1Q3447|B&qsttWqs1Q3447|B&r77T"a[[ CGGd2qkk$999AaDAacddGGAaDAacddGG 	AA!WWF!WWF VOE JE%<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@   {   su     WQZZFJq		***J	#j0	1	1B	#j0	1	1Br6Mr   c                 R   t          j        |d|dz
  |dz
  f         dz            | |dz
  |dz
  f         z
  }t          |           }t          j        |          }d||dz
  <   |dk    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   r7   lenr    r   )Ar%   r*   deltar'   r/   s         r   r   r      s    6 F1TacT1Q3Y<?##a!QqSk1EAA 	AAacF 	Avv"1TacT4AaC4Z=1TacT1Q3Y<-@@$1Q3$!8Or   c                   b     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{Gz?N皙?皙?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  | _        d S )Nr   r   )potrffro)super__init__previous_tr_radius	lambda_lbniterk_easyk_hardr   r   choleskyrC   	dimensionr@   hess_gershgorin_lbhess_gershgorin_ubr   r   infhess_infhess_froEPSCLOSE_TO_ZERO)	selfxr   r   r   hessprQ   rR   	__class__s	           r   rM   zIterativeSubproblem.__init__   s     	Cd+++ #%
 
 **tylCC TY&7	&B&B	$#TY//TY.. "^dh6Fr   c           
         t          d| j        |z  t          | j         | j        | j                  z             }t          dt          | j                                                   | j        |z  t          | j        | j        | j                  z
            }|| j	        k     rt          | j
        |          }|dk    rd}n3t          t          j        ||z            || j        ||z
  z  z             }|||fS )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   )r9   jac_magr8   rU   rY   rX   r   diagonalrV   rN   rO   r   sqrtUPDATE_COEFF)r\   	tr_radius	lambda_ubrO   lambda_initials        r   _initial_valuesz#IterativeSubproblem._initial_values   s    4<	1C9P8P8<8<5G 5G G H H	
 C	 2 2 4 4555Y.T5L59]59]2D 2D DE E	 t...DNI66I >>NN Y)>!?!?!*T->	)@S-T!TV VN y)33r   c                 \   |                      |          \  }}}| j        }d}d}d| _        	 |rd}n;| j        |t	          j        |          z  z   }|                     |ddd          \  }	}
| xj        dz  c_        |
dk    r| j        | j        k    rt          |	df| j
                   }t          |          }||k    r
|dk    rd}nt          |	|d          }t          |          }||z  dz  ||z
  z  |z  }||z   }||k     r<t          |	          \  }}|                     |||          \  }}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   }|                     |ddd          \  }}
|
dk    r|}d}nt'          ||          }t'          t	          j        ||z            || j        ||z
  z  z             }nMt!          ||z
            |z  }|| j        k    rn-|}|}n%|
dk    r| j        | j        k    r|dk    rt	          j        |          }d}nt          |	          \  }}|}|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             }not1          ||	|
          \  }}t          |          }t'          ||||dz  z  z             }t'          t	          j        ||z            || j        ||z
  z  z             }h|| _        || _        || _        ||fS )
zSolve quadratic subproblemTFr   )loweroverwrite_acleanr   r#   )transrB   )key)rh   rT   rP   r   r   eyerS   ra   r[   r   r   r   r   r   get_boundaries_intersectionsr8   r$   dotrR   r9   rc   rd   rQ   r    r   rO   lambda_currentrN   )r\   re   rr   rO   rf   r'   hits_boundaryalready_factorizedr:   r%   infor(   p_normr)   r1   delta_lambda
lambda_newr2   r3   tatbstep_lenquadratic_termrelative_errorcrE   r/   r0   s                               r   solvezIterativeSubproblem.solve  s    04/C/CI/N/N,	9N"
J	 " 4%*""InRVAYY66--49.2 ( 4 44 JJ!OJJ qyyT\D,>>> q%j48)44a Y&&>Q+>+>$)M %Q555a !'v1VI5EFyP+l:
I%%#CA#F#FLE5!>>q%?HJ JFB  #B8555H &(VArva||%<%<N (0{UAX'=)7.TU:U)U'WN%44X-- !/I #I~q/H I II 	Jrvayy$88A"mmAU8=26 , 8 8GAt qyy)3-1** %(	:$>$>	 *-GI	$9::%(99Y;N(OO* * &)));%<%<y%HN%44 !/I &0NNt|t/AAA "Q&&A$)M?BBu$ aK%(*{^3ilBC C 5(A +		>E1H+DEE	 "%GI	122 19Y3F GG" " 6aDAAqa  	>E&!)O+KLL	 "%GI	122 19Y3F GG" "OJ	X #,"+-r   )NrG   rH   )__name__
__module____qualname____doc__rd   r   finfofloatepsrZ   rM   rh   r   __classcell__)r_   s   @r   r   r      s         , L
"(5//
C04$''G 'G 'G 'G 'G 'GR4 4 4>Y  Y  Y  Y  Y  Y  Y r   r   )r   NN)r   numpyr   scipy.linalgr   r   r   r   _trustregionr   r	   __all__r
   r   r@   r   r   r   r   r   <module>r      s   8 8    % % % % % % % % % % % % K K K K K K K K" " ": : : :>L L L^  *' ' 'T|  |  |  |  | 1 |  |  |  |  | r   