L i<pdZddlZddlmZmZmZmZddlm Z m Z gdZ d dZ dZ d Zd ZGd d e Zy)z2Nearly exact trust-region optimization subproblem.N)normget_lapack_funcssolve_triangular cho_solve)_minimize_trust_regionBaseQuadraticSubproblem)_minimize_trustregion_exact estimate_smallest_singular_valuesingular_leading_submatrixIterativeSubproblemc t| tdt|s tdt||f|||td|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.)argsjachess subproblem) ValueErrorcallablerr )funx0rrrtrust_region_optionss g/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/optimize/_trustregion_exact.pyr r s[& {/0 0 D>/0 0 !#r :#D-@ :$8 ::ctj|}|j\}}||k7r tdtj|}tj |}t |D]}d||z |j||fz }d||z |j||fz }||dzd|j|dzd|f|zz}||dzd|j|dzd|f|zz} t|t|dzt|t| dzk\r|||<|||dzd|||<| ||dzdt||} t| } t|} | | z } | | z }| |fS)aYGiven 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.rN) np atleast_2dshaperzerosemptyrangeTabsrr)Umnpwkwpwmpppmvv_normw_norms_minz_mins rr r +sD aA 77DAqAvIJJ  A  A1X !fAqD !1gQT " qstWqss1Q347|B & qstWqss1Q347|B & r7T"a[ CGd2qk$9 9AaDAacdGAaDAacdG  AA !WF !WF VOE JE %<rctj|}tj|}tjtj|d}tj||z|z }tj ||z |z}||fS)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)rdiagr#summinmax)HH_diag H_diag_abs H_row_sumslbubs rgershgorin_boundsr?zsoWWQZFJq *J #j0 1B #j0 1B r6Mrc(tj|d|dz |dz fdz||dz |dz fz }t|}tj|}d||dz <|dk7r/t |d|dz d|dz f|d|dz |dz f |d|dz ||fS)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)rr6lenrr)Ar$r)deltar&r.s rr r s6 FF1TacT1Q3Y<? #a!QqSk 1E AA  A AacF Av"1TacT4AaC4Z=1TacT1Q3Y<-@$1Q3 !8OrcpeZdZdZdZej ejZ dfd Z dZ dZ xZ S)r aQuadratic 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?ct|||||d|_d|_d|_||_||_td|jf\|_ t|j|_ t|j\|_ |_t|jt j"|_t|jd|_|j|j(z|j$z|_y)Nrr)potrffro)super__init__previous_tr_radius lambda_lbniterk_easyk_hardrrcholeskyrB dimensionr?hess_gershgorin_lbhess_gershgorin_ubrrinfhess_infhess_froEPS CLOSE_TO_ZERO) selfxrrrhessprNrO __class__s rrJzIterativeSubproblem.__init__s Cd+#%   **tyylC TYY&7 &B $  #TYY/ TYY. "^^dhh6Frc ,td|j|z t|j |j|j z}tdt|j j |j|z t|j|j|j z }||jkrt|j|}|dk(rd}n5ttj||z||j||z zz}|||fS)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)r8jac_magr7rRrVrUrdiagonalrSrKrLrsqrt UPDATE_COEFF)rY tr_radius lambda_ubrLlambda_initials r_initial_valuesz#IterativeSubproblem._initial_valuess 4<< 1C9P9P8P8< 8< 5GGH C 2 2 455 Y.T5L5L59]]59]]2DDE t.. .DNNI6I >N Y)>!?!*T->-> )@S-T!TVNy)33rc@|j|\}}}|j}d}d}d|_ |rd}n=|j|t j |zz}|j |ddd\} } |xjdz c_ dk(r|j|jkDrt df|j } t| } | |kr |dk(rd}nt| | d} t| }| |z dz| |z z|z }||z}| |kr0t| \}}|j| ||\}}t||gt }t j"| t j"| }|dz|dzz|||dzzzz }||j$kr | ||zz } n|}t'|||dzz }|j|t j |zz}|j |ddd\}} | dk(r|}d}nt'||}t't j(||z||j*||z zz}nIt!| |z |z }||j,krn)|}|}n!| dk(r|j|jkr|dk(rt j.|} d}nt \}}|}|dz|dzz|j$|z|dzzkr||z} n|}t'|||dzz }t't j(||z||j*||z zz}net1 | \}}t|}t'||||dzz z}t't j(||z||j*||z zz}Y||_||_||_| |fS) zSolve quadratic subproblemTFr)lower overwrite_acleanrr")transrA)key)rerQrMrreyerPr^rXrrrrr get_boundaries_intersectionsr7r#dotrOr8r`rarNrr rLlambda_currentrK)rYrbrorLrcr& hits_boundaryalready_factorizedr9r$infor'p_normr(r0 delta_lambda lambda_newr1r2tatbstep_lenquadratic_termrelative_errorcrDr.r/s rsolvezIterativeSubproblem.solves 04/C/CI/N, 9 NN " "%*"IInRVVAY66--49.2(44 JJ!OJqyT\\D,>,>>q%j488)4aY&>Q+>$)M%Q5a!'v 1VI5EFyP +l: I%#CA#FLE5!>>q%?HJFB #B85H&(VVArvva|% *-GGI $9:%(9(99Y;N(OO*&)));%E1H+DE "%GGI 12 1 19Y3F GG"6aDAqa >E&!)O+KL "%GGI 12 1 19Y3F GG"OX#,"+-r)Ng?g?)__name__ __module__ __qualname____doc__rarfinfofloatepsrWrJrer| __classcell__)r\s@rr r s<,L "((5/  C04$''GR4>Y rr )NN)rnumpyr scipy.linalgrrrr _trustregionrr __all__r r r?r r rrrrsE8%%K " :