o
    ×?HhÆ<  ã                   @   sr   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d	„Zd
d„ Zdd„ Zdd„ ZG d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                 K   s<   |du r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_optionsr   r   úa/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/scipy/optimize/_trustregion_exact.pyr	      s   ÿþr	   c                 C   sV  t  | ¡} | j\}}||krtdƒ‚t  |¡}t  |¡}t|ƒD ]q}d||  | j||f  }d||  | j||f  }||d d… | j|d d…|f |  }||d d… | j|d d…|f |  }	t|ƒt	|dƒ t|ƒt	|	dƒ kr…|||< |||d d…< q |||< |	||d d…< q t
| |ƒ}
t	|
ƒ}t	|ƒ}|| }|
| }||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_minr   r   r   r
   ,   s,   
"


**$
r
   c                 C   sT   t  | ¡}t  |¡}t jt  | ¡dd}t  || | ¡}t  || | ¡}||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Úubr   r   r   Úgershgorin_bounds{   s   

r;   c                 C   s¨   t  |d|d …|d f d ¡| |d |d f  }t| ƒ}t  |¡}d||d < |dkrPt|d|d …d|d …f |d|d …|d f  ƒ|d|d …< ||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   r2   Úlenr   r   )ÚAr!   r&   Údeltar#   r+   r   r   r   r      s   6
Dr   c                       sF   e Zd ZdZdZe e¡jZ			d‡ fdd„	Z
dd	„ Zd
d„ Z‡  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„?Nçš™™™™™¹?çš™™™™™É?c                    s–   t ƒ  ||||¡ d| _d | _d| _|| _|| _td| jfƒ\| _	t
| jƒ| _t| jƒ\| _| _t| jtjƒ| _t| jdƒ| _| j| j | j | _d S )Nr   r   )ÚpotrfÚfro)ÚsuperÚ__init__Úprevious_tr_radiusÚ	lambda_lbÚniterÚk_easyÚk_hardr   r   Úcholeskyr=   Ú	dimensionr;   Úhess_gershgorin_lbÚhess_gershgorin_ubr   r   ÚinfÚhess_infÚhess_froÚEPSÚCLOSE_TO_ZERO)ÚselfÚxr   r   r   ÚhessprI   rJ   ©Ú	__class__r   r   rE   Õ   s   ÿzIterativeSubproblem.__init__c                 C   s¤   t d| j| t| j | j| jƒ ƒ}t dt| j ¡ ƒ | j| t| j| j| jƒ ƒ}|| j	k r5t | j
|ƒ}|dkr<d}nt t || ¡|| j||   ƒ}|||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   )r4   Újac_magr3   rM   rQ   rP   r   ÚdiagonalrN   rF   rG   r   ÚsqrtÚUPDATE_COEFF)rT   Ú	tr_radiusÚ	lambda_ubrG   Úlambda_initialr   r   r   Ú_initial_valuesþ   s$   þþÿ
ÿ
z#IterativeSubproblem._initial_valuesc                 C   s(  |   |¡\}}}| j}d}d}d| _	 |rd}n| j|t |¡  }| j|dddd\}	}
|  jd7  _|
dkr	| j| jkr	t	|	df| j
 ƒ}t|ƒ}||krY|dkrYd}n.t|	|dd}t|ƒ}|| d ||  | }|| }||k rõt|	ƒ\}}|  |||¡\}}t||gtd	}t |t ||¡¡}|d |d  |||d    }|| jkrµ||| 7 }nÒ|}t|||d  ƒ}| j|t |¡  }| j|dddd\}}
|
dkrÞ|}d}n¨t||ƒ}tt || ¡|| j||   ƒ}n‘t|| ƒ| }|| jkrnƒ|}|}n}|
dkr^| j| jkr^|dkr"t |¡}d}net|	ƒ\}}|}|d |d  | j| |d  krA|| }nF|}t|||d  ƒ}tt || ¡|| j||   ƒ}n(t||	|
ƒ\}}t|ƒ}t||||d   ƒ}tt || ¡|| j||   ƒ}q|| _|| _|| _||fS )
zSolve quadratic subproblemTFr   )ÚlowerÚoverwrite_aÚcleanr   r   )Útransr<   )Úkey)r`   rL   rH   r   r   ÚeyerK   rY   rS   r   r   r   r   r
   Úget_boundaries_intersectionsr3   r    ÚdotrJ   r4   r[   r\   rI   r   r   rG   Úlambda_currentrF   )rT   r]   ri   rG   r^   r#   Úhits_boundaryÚalready_factorizedr5   r!   Úinfor$   Úp_normr%   r-   Údelta_lambdaÚ
lambda_newr.   r/   ÚtaÚtbÚstep_lenÚquadratic_termÚrelative_errorÚcr?   r+   r,   r   r   r   Úsolve  s²   
þÿÿ

þ
þ

ÿþþ ø zIterativeSubproblem.solve)Nr@   rA   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r\   r   ÚfinfoÚfloatÚepsrR   rE   r`   rv   Ú__classcell__r   r   rW   r   r   º   s    ÿ)r   )r   NN)rz   Únumpyr   Úscipy.linalgr   r   r   r   Ú_trustregionr   r   Ú__all__r	   r
   r;   r   r   r   r   r   r   Ú<module>   s    
O*