
    4[g<                     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/html/bid-api/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 )aZ  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
        Estimatied 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77DAqAvIJJ 	A
A 1X!fAqD	!1gQT"qstWqss1Q347|B&qstWqss1Q347|B&r7T"a[ CGd2qk$99AaDAacdGAaDAacdG  	AA!WF!WF 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?   {   so     WWQZFJq	*J	#j0	1B	#j0	1Br6Mr   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 FF1TacT1Q3Y<?#a!QqSk1EAA 	AAacF 	Av"1TacT4AaC4Z=1TacT1Q3Y<-@$1Q3!8Or   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{Gz?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+ #%
 
 **tyylC TYY&7		&B	$#TYY/TYY. "^^dhh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 )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   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<<	1C9P9P8P8<8<5G G H	
 C		 2 2 455Y.T5L5L59]]59]]2D DE	 t...DNNI6I >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  rt        	df| j                         }t        |      }||k  r	|dk(  rd}nt        |	|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}nt'        ||      }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}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         }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/CI/N,	9NN"
 "%*"IInRVVAY66--49.2 ( 44 JJ!OJ qyT\\D,>,>> q%j488)4a Y&>Q+>$)M %Q5a !'v1VI5EFyP+l:
I%#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* %(	:$>	 *-GGI	$9:%(9(99Y;N(OO* &)));%<y%HN%4 !/I &0Nt||t/A/AA "Q&A$)M?Bu$ aK%(*{{^3ilBC 5(A +		>E1H+DE	 "%GGI	12 1 19Y3F GG" 6aDAqa  	>E&!)O+KL	 "%GGI	12 1 19Y3F 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/

C04$''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   