
    (ph                         S SK r S SKrS SKJr  SSKJrJr  SSKJ	r	J
r
  SSKJrJrJrJrJr  SSKJr  SSKJr  \R*                  " \5      R.                  r\R*                  " \5      R2                  r " S	 S
5      rg)    N)
lsq_linear   )Models	Quadratic)Options	Constants)cauchy_geometryspider_geometrynormal_byrd_omojokuntangential_byrd_omojokun$constrained_tangential_byrd_omojokun)qr_tangential_byrd_omojokun)get_arrays_tolc                      \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	\S 5       r
\S 5       r\S	 5       r\R                  S
 5       r\S 5       r\R                  S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       rS rS rS rS rS rS rS rS rS-S jrS rS r S  r!S! r"S" r#S# r$S$ r%S% r&S.S& jr'S' r(S( r)S) r*S* r+S+ r,S,r-g)/TrustRegion   z
Trust-region framework.
c                 R   SU l         Xl        [        U R                  X R                  5      U l        X0l        SU l        U R                  5         [        R                  " U R                  5      U l        [        R                  " U R                  5      U l        [        R                  " U R                  5      U l        [        R                  " U R                   5      U l        U R%                  U R&                  5        U[(        R*                     U l        U R.                  U l        g)aa  
Initialize the trust-region framework.

Parameters
----------
pb : `cobyqa.problem.Problem`
    Problem to solve.
options : dict
    Options of the solver.
constants : dict
    Constants of the solver.

Raises
------
`cobyqa.utils.MaxEvalError`
    If the maximum number of evaluations is reached.
`cobyqa.utils.TargetSuccess`
    If a nearly feasible point has been found with an objective
    function value below the target.
`cobyqa.utils.FeasibleSuccess`
    If a feasible point has been found for a feasibility problem.
`numpy.linalg.LinAlgError`
    If the initial interpolation system is ill-defined.
        r   N)_penalty_pbr   penalty_models
_constants_best_indexset_best_indexnpzerosm_linear_ub_lm_linear_ubm_linear_eq_lm_linear_eqm_nonlinear_ub_lm_nonlinear_ubm_nonlinear_eq_lm_nonlinear_eqset_multipliersx_bestr   RHOBEG_resolution
resolution_radius)selfpboptions	constantss       N/var/www/html/venv/lib/python3.13/site-packages/scipy/_lib/cobyqa/framework.py__init__TrustRegion.__init__   s    4  dhh>#   XXd&6&67XXd&6&67 ")<)< = ")<)< =T[[) #7>>2    c                 .    U R                   R                  $ )zD
Number of variables.

Returns
-------
int
    Number of variables.
)r   nr,   s    r0   r5   TrustRegion.nL   s     xxzzr3   c                 .    U R                   R                  $ )zl
Number of linear inequality constraints.

Returns
-------
int
    Number of linear inequality constraints.
)r   r   r6   s    r0   r   TrustRegion.m_linear_ubX        xx###r3   c                 .    U R                   R                  $ )zh
Number of linear equality constraints.

Returns
-------
int
    Number of linear equality constraints.
)r   r    r6   s    r0   r    TrustRegion.m_linear_eqd   r:   r3   c                 .    U R                   R                  $ )zr
Number of nonlinear inequality constraints.

Returns
-------
int
    Number of nonlinear inequality constraints.
)r   r"   r6   s    r0   r"   TrustRegion.m_nonlinear_ubp        xx&&&r3   c                 .    U R                   R                  $ )zn
Number of nonlinear equality constraints.

Returns
-------
int
    Number of nonlinear equality constraints.
)r   r$   r6   s    r0   r$   TrustRegion.m_nonlinear_eq|   r?   r3   c                     U R                   $ )zF
Trust-region radius.

Returns
-------
float
    Trust-region radius.
)r+   r6   s    r0   radiusTrustRegion.radius        ||r3   c                     Xl         U R                  U R                  [        R                     U R
                  -  ::  a  U R
                  U l         gg)za
Set the trust-region radius.

Parameters
----------
radius : float
    New trust-region radius.
N)r+   rC   r   r   DECREASE_RADIUS_THRESHOLDr*   )r,   rC   s     r0   rC   rD      sE     KKyBBCoo  ??DL	r3   c                     U R                   $ )z
Resolution of the trust-region framework.

The resolution is a lower bound on the trust-region radius.

Returns
-------
float
    Resolution of the trust-region framework.
r)   r6   s    r0   r*   TrustRegion.resolution   s     r3   c                     Xl         g)z
Set the resolution of the trust-region framework.

Parameters
----------
resolution : float
    New resolution of the trust-region framework.
NrI   )r,   r*   s     r0   r*   rJ      s
     &r3   c                     U R                   $ )zB
Penalty parameter.

Returns
-------
float
    Penalty parameter.
)r   r6   s    r0   r   TrustRegion.penalty   s     }}r3   c                     U R                   $ )z
Models of the objective function and constraints.

Returns
-------
`cobyqa.models.Models`
    Models of the objective function and constraints.
)r   r6   s    r0   modelsTrustRegion.models   rE   r3   c                     U R                   $ )zh
Index of the best interpolation point.

Returns
-------
int
    Index of the best interpolation point.
)r   r6   s    r0   
best_indexTrustRegion.best_index   s     r3   c                 `    U R                   R                  R                  U R                  5      $ )z
Best interpolation point.

Its value is interpreted as relative to the origin, not the base point.

Returns
-------
`numpy.ndarray`
    Best interpolation point.
)rO   interpolationpointrR   r6   s    r0   r'   TrustRegion.x_best   s#     {{((..t??r3   c                 H    U R                   R                  U R                     $ )zv
Value of the objective function at `x_best`.

Returns
-------
float
    Value of the objective function at `x_best`.
)rO   fun_valrR   r6   s    r0   fun_bestTrustRegion.fun_best   s     {{""4??33r3   c                 P    U R                   R                  U R                  SS24   $ )z
Values of the nonlinear inequality constraints at `x_best`.

Returns
-------
`numpy.ndarray`, shape (m_nonlinear_ub,)
    Values of the nonlinear inequality constraints at `x_best`.
N)rO   cub_valrR   r6   s    r0   cub_bestTrustRegion.cub_best   "     {{""4??A#566r3   c                 P    U R                   R                  U R                  SS24   $ )z
Values of the nonlinear equality constraints at `x_best`.

Returns
-------
`numpy.ndarray`, shape (m_nonlinear_eq,)
    Values of the nonlinear equality constraints at `x_best`.
N)rO   ceq_valrR   r6   s    r0   ceq_bestTrustRegion.ceq_best
  r`   r3   c                 $   U R                   R                  U5      U R                  U R                  R                  R
                  U-  U R                  R                  R                  -
  -  -   U R                  U R                  R                  R                  U-  U R                  R                  R                  -
  -  -   U R                  U R                   R                  U5      -  -   U R                  U R                   R                  U5      -  -   $ )z
Evaluate the Lagrangian model at a given point.

Parameters
----------
x : `numpy.ndarray`, shape (n,)
    Point at which the Lagrangian model is evaluated.

Returns
-------
float
    Value of the Lagrangian model at `x`.
)rO   funr   r   lineara_ubb_ubr!   a_eqb_eqr#   cubr%   ceqr,   xs     r0   	lag_modelTrustRegion.lag_model  s     KKOOA  xx##a'$((//*>*>>@@   xx##a'$((//*>*>>@@
 ##dkkooa&889 ##dkkooa&889	
r3   c                    U R                   R                  U5      U R                  U R                  R                  R
                  -  -   U R                  U R                  R                  R                  -  -   U R                  U R                   R                  U5      -  -   U R                  U R                   R                  U5      -  -   $ )a  
Evaluate the gradient of the Lagrangian model at a given point.

Parameters
----------
x : `numpy.ndarray`, shape (n,)
    Point at which the gradient of the Lagrangian model is evaluated.

Returns
-------
`numpy.ndarray`, shape (n,)
    Gradient of the Lagrangian model at `x`.
)rO   fun_gradr   r   rg   rh   r!   rj   r#   cub_gradr%   ceq_gradrn   s     r0   lag_model_gradTrustRegion.lag_model_grad.  s     KK  #  488??#7#778  488??#7#778 ##dkk&:&:1&==> ##dkk&:&:1&==	>	
r3   c                    U R                   R                  5       nU R                  S:  a)  XR                  U R                   R	                  5       -  -  nU R
                  S:  a)  XR                  U R                   R                  5       -  -  nU$ )z
Evaluate the Hessian matrix of the Lagrangian model at a given point.

Returns
-------
`numpy.ndarray`, shape (n, n)
    Hessian matrix of the Lagrangian model at `x`.
r   )rO   fun_hessr"   r#   cub_hessr$   r%   ceq_hess)r,   hesss     r0   lag_model_hessTrustRegion.lag_model_hessD  sw     {{##%"))DKK,@,@,BBBD"))DKK,@,@,BBBDr3   c                     U R                   R                  U5      U R                  U R                   R                  U5      -  -   U R                  U R                   R                  U5      -  -   $ )at  
Evaluate the right product of the Hessian matrix of the Lagrangian
model with a given vector.

Parameters
----------
v : `numpy.ndarray`, shape (n,)
    Vector with which the Hessian matrix of the Lagrangian model is
    multiplied from the right.

Returns
-------
`numpy.ndarray`, shape (n,)
    Right product of the Hessian matrix of the Lagrangian model with
    `v`.
)rO   fun_hess_prodr#   cub_hess_prodr%   ceq_hess_prodr,   vs     r0   lag_model_hess_prodTrustRegion.lag_model_hess_prodT  sa    $ KK%%a(##dkk&?&?&BBC##dkk&?&?&BBC	
r3   c                     U R                   R                  U5      U R                  U R                   R                  U5      -  -   U R                  U R                   R                  U5      -  -   $ )a  
Evaluate the curvature of the Lagrangian model along a given direction.

Parameters
----------
v : `numpy.ndarray`, shape (n,)
    Direction along which the curvature of the Lagrangian model is
    evaluated.

Returns
-------
float
    Curvature of the Lagrangian model along `v`.
)rO   fun_curvr#   cub_curvr%   ceq_curvr   s     r0   lag_model_curvTrustRegion.lag_model_curvk  s_      KK  ###dkk&:&:1&==>##dkk&:&:1&==>	
r3   c                 |    UU R                   R                  U R                  5      SU R                  U5      -  -   -  $ )a%  
Evaluate the objective function of the SQP subproblem.

Parameters
----------
step : `numpy.ndarray`, shape (n,)
    Step along which the objective function of the SQP subproblem is
    evaluated.

Returns
-------
float
    Value of the objective function of the SQP subproblem along `step`.
g      ?)rO   rs   r'   r   r,   steps     r0   sqp_funTrustRegion.sqp_fun  s?     KK  -D,,T223
 	
r3   c                     U R                   R                  U R                  5      U R                   R                  U R                  5      U-  -   $ )as  
Evaluate the linearization of the nonlinear inequality constraints.

Parameters
----------
step : `numpy.ndarray`, shape (n,)
    Step along which the linearization of the nonlinear inequality
    constraints is evaluated.

Returns
-------
`numpy.ndarray`, shape (m_nonlinear_ub,)
    Value of the linearization of the nonlinear inequality constraints
    along `step`.
)rO   rl   r'   rt   r   s     r0   sqp_cubTrustRegion.sqp_cub  =    " KKOODKK(kk""4;;/$67	
r3   c                     U R                   R                  U R                  5      U R                   R                  U R                  5      U-  -   $ )am  
Evaluate the linearization of the nonlinear equality constraints.

Parameters
----------
step : `numpy.ndarray`, shape (n,)
    Step along which the linearization of the nonlinear equality
    constraints is evaluated.

Returns
-------
`numpy.ndarray`, shape (m_nonlinear_ub,)
    Value of the linearization of the nonlinear equality constraints
    along `step`.
)rO   rm   r'   ru   r   s     r0   sqp_ceqTrustRegion.sqp_ceq  r   r3   Nc                 >   Ub  Ub  Uc  U R                  XR                  5      u  p#nUnU R                  S:  ac  U R                   R                  XUS9n[        R
                  " U5      (       a.  XPR                  [        R                  R                  U5      -  -  nU$ )a  
Evaluate the merit function at a given point.

Parameters
----------
x : `numpy.ndarray`, shape (n,)
    Point at which the merit function is evaluated.
fun_val : float, optional
    Value of the objective function at `x`. If not provided, the
    objective function is evaluated at `x`.
cub_val : `numpy.ndarray`, shape (m_nonlinear_ub,), optional
    Values of the nonlinear inequality constraints. If not provided,
    the nonlinear inequality constraints are evaluated at `x`.
ceq_val : `numpy.ndarray`, shape (m_nonlinear_eq,), optional
    Values of the nonlinear equality constraints. If not provided,
    the nonlinear equality constraints are evaluated at `x`.

Returns
-------
float
    Value of the merit function at `x`.
r   )r]   rb   )r   r   r   	violationr   count_nonzerolinalgnorm)r,   ro   rY   r]   rb   m_valc_vals          r0   meritTrustRegion.merit  s    . ?go(,LL(A%Gg==3HH&&q7&KE&&)>>>r3   c                    [         R                  " U R                  R                  R                  /U R
                  R                  U5      //5      n[         R                  " U R                  R                  R                  U R                  R                  R                  U-  -
  U R
                  R                  U5      * /5      n[         R                  " U R                  R                  R                  /U R
                  R                  U5      //5      n[         R                  " U R                  R                  R                  U R                  R                  R                  U-  -
  U R
                  R                  U5      * /5      nX#XE4$ )a  
Get the linearizations of the constraints at a given point.

Parameters
----------
x : `numpy.ndarray`, shape (n,)
    Point at which the linearizations of the constraints are evaluated.

Returns
-------
`numpy.ndarray`, shape (m_linear_ub + m_nonlinear_ub, n)
    Left-hand side matrix of the linearized inequality constraints.
`numpy.ndarray`, shape (m_linear_ub + m_nonlinear_ub,)
    Right-hand side vector of the linearized inequality constraints.
`numpy.ndarray`, shape (m_linear_eq + m_nonlinear_eq, n)
    Left-hand side matrix of the linearized equality constraints.
`numpy.ndarray`, shape (m_linear_eq + m_nonlinear_eq,)
    Right-hand side vector of the linearized equality constraints.
)r   blockr   rg   rh   rO   rt   ri   rl   rj   ru   rk   rm   )r,   ro   aubbubaeqbeqs         r0   get_constraint_linearizations)TrustRegion.get_constraint_linearizations  s-   ( hh%%&%%a()
 hh$$txx';';a'??##
 hh%%&%%a()
 hh$$txx';';a'??##
 !!r3   c                    U R                  U R                  5      u  p#pEU R                  R                  R                  U R                  -
  nU R                  R                  R
                  U R                  -
  nU R                  [        R                     U R                  -  n[        UUUUUUUU[        R                     40 U R                  D6n	U[        R                     (       a  [        Xg5      n
[        R                  " X-   U:  5      (       d   [        R                  " XyU
-
  :  5      (       a  [         R"                  " S[$        S5        [        R&                  R)                  U	5      SU-  :  a  [         R"                  " S[$        S5        [        R*                  " U R                  S-  X-  -
  5      nXi-  nXy-  n[        R,                  " X2U	-  -
  S5      nU R.                  R1                  U R                  5      U R3                  U	5      -   nU R                  R4                  S;   a7  [7        UU R2                  UUUU[        R                     40 U R                  D6nO+[9        UU R2                  UUUUUUUS   4	0 U R                  D6nU[        R                     (       a  [        Xg5      n
[        R                  " X-   U:  5      (       d   [        R                  " X|U
-
  :  5      (       a  [         R"                  " S	[$        S5        [        R&                  R)                  X-   5      S[        R*                  " S5      -  U R                  -  :  a  [         R"                  " S
[$        S5        X4$ )a  
Get the trust-region step.

The trust-region step is computed by solving the derivative-free
trust-region SQP subproblem using a Byrd-Omojokun composite-step
approach. For more details, see Section 5.2.3 of [1]_.

Parameters
----------
options : dict
    Options of the solver.

Returns
-------
`numpy.ndarray`, shape (n,)
    Normal step.
`numpy.ndarray`, shape (n,)
    Tangential step.

References
----------
.. [1] T. M. Ragonneau. *Model-Based Derivative-Free Optimization
   Methods and Software*. PhD thesis, Department of Applied
   Mathematics, The Hong Kong Polytechnic University, Hong Kong, China,
   2022. URL: https://theses.lib.polyu.edu.hk/handle/200/12294.
z6the normal step does not respect the bound constraint.   皙?z=the normal step does not respect the trust-region constraint.       @r   )unconstrainedzbound-constraineddebugz;The tangential step does not respect the bound constraints.z<The trial step does not respect the trust-region constraint.)r   r'   r   boundsxlxur   r   BYRD_OMOJOKUN_FACTORrC   r   r   DEBUGr   r   anywarningswarnRuntimeWarningr   r   sqrtmaximumrO   rs   r   typer   r   )r,   r.   r   r   r   r   r   r   rC   normal_steptolg_besttangential_steps                r0   get_trust_region_step!TrustRegion.get_trust_region_step  s   8 "??L#XX__$++-XX__$++- !?!?@4;;N*GMM"

 oo

 7==! (C{(2-..vvb#4455L"
 yy~~k*S6\9""	 c)K,EEF

jj[00#6%%dkk2T5M5M6
 
 88==BB6((& //O C((  //O 7==! (Cvvo+b011RVVs**6 6 #"	 		{<=$t{{23 ""	 ++r3   c                   ^ ^ U[         R                     (       a  UT R                  :w  d   S5       e[        R                  " [        R
                  " ST R                  R                  U5      5      n[        T R                  R                  UU[         R                     5      mTR                  T R                  T R                  R                  5      nT R                  R                  R                  T R                  -
  nT R                  R                  R                  T R                  -
  n[!        SUUU 4S jUUT R"                  U[         R                     5      nT R                  R%                  T R                  U-   U5      nT R                  R                  R&                  T R                  R                  R&                  SS2T R                  [        R(                  4   -
  n	U	SS2T R                  S/4   U	SS2ST R                  /4'   [+        SUUU 4S jU	SS2SS24   UUT R"                  U[         R                     5      n
T R                  R%                  T R                  U
-   U5      n[-        U5      [-        U5      :  a  U
nUnT R                  R.                  S;   Ga  T R1                  T R                  5      u  pp[3        XV5      n[3        U5      nUU* :*  nUU:  nUU:  n[5        UUUUU5      u  nnUSS2US24   USS2US24   R6                  U-  -  n[        R8                  R;                  U5      nSUs=:  a  T R                  R<                  :  Ga  O  GOU[>        T R"                  -  :  Ga  T R"                  U-  U-  n
TRA                  U
T R                  R                  5      S:  a  U
* n
[        RB                  " XZ-
  X-
  /5      nX-  U-
  nX-  U-
  n[E        S	 UU[        R,                  " U5      4 5       5      n[        RD                  " [        R,                  " U
U)    5      SS
9n[        RD                  " [        R,                  " U
U)    5      US
9n[        RD                  " [        R,                  " UU) SS24   U
-  5      US
9n[G        SU-  S[        R8                  R;                  U
5      -  5      nUU::  a[  T R                  R%                  T R                  U
-   U5      n[-        U5      S[-        U5      -  :  a  [        RH                  " XU5      nU[         R                     (       a  [3        XV5      n[        RJ                  " UU-   U:  5      (       d   [        RJ                  " XgU-
  :  5      (       a  [L        RN                  " S[P        S5        [        R8                  R;                  U5      ST R"                  -  :  a  [L        RN                  " S[P        S5        U$ )a  
Get the geometry-improving step.

Three different geometry-improving steps are computed and the best one
is returned. For more details, see Section 5.2.7 of [1]_.

Parameters
----------
k_new : int
    Index of the interpolation point to be modified.
options : dict
    Options of the solver.

Returns
-------
`numpy.ndarray`, shape (n,)
    Geometry-improving step.

Raises
------
`numpy.linalg.LinAlgError`
    If the computation of a determinant fails.

References
----------
.. [1] T. M. Ragonneau. *Model-Based Derivative-Free Optimization
   Methods and Software*. PhD thesis, Department of Applied
   Mathematics, The Hong Kong Polytechnic University, Hong Kong, China,
   2022. URL: https://theses.lib.polyu.edu.hk/handle/200/12294.
z8The index `k_new` must be different from the best index.r   r   c                 P   > TR                  U TR                  R                  5      $ NcurvrO   rU   r   lagr,   s    r0   <lambda>/TrustRegion.get_geometry_step.<locals>.<lambda>      chhq$++";";<r3   Nr   c                 P   > TR                  U TR                  R                  5      $ r   r   r   s    r0   r   r     r   r3   )zlinearly constrainedznonlinearly constrainedc              3   L   #    U  H  n[         R                  " US S9v   M     g7f)r   initialN)r   max).0arrays     r0   	<genexpr>0TrustRegion.get_geometry_step.<locals>.<genexpr>  s"       !8 FF5#.!8s   "$r         $@g{Gz?g?z9The geometry step does not respect the bound constraints.r   r   z?The geometry step does not respect the trust-region constraint.))r   r   rR   r   squeezeeyerO   nptr   rU   gradr'   r   r   r   r   r	   rC   determinantsxptnewaxisr
   absr   r   r   r   Tr   r   r5   TINYr   r   r   minclipr   r   r   r   )r,   k_newr.   	coord_vecg_lagr   r   r   sigmar   step_alt	sigma_altr   r   r   r   tol_bdtol_ubfree_xlfree_xufree_ubn_actq
g_lag_projnorm_g_lag_projcbdrl   rm   	maxcv_valr   r   s   `                             @r0   get_geometry_stepTrustRegion.get_geometry_step  s   > 7==!(JIJ( JJrvva%@A	KK%%GMM"

 dkk&?&?@ XX__$++-XX__$++-<KKGMM"
 ((t);UC KK%%))kk''++At

,JKL 	 (+1t.B+B'CA4??##$"<12JKKGMM"	
 KK,,T[[8-CUK	y>CJ&DE 88== 
 

 224;;? Cc#B+F#C(FVGmGFlGVmG 3HE1 1ef91ef9%)?@J iinnZ8O5%488::%%/D4;;<N*N KK/9ZG88Hdkk&?&?@3F (yH hhx}=>ns*ns*  "%sBFF3K!8  	 ffRVVHgX$67EffRVVHgX$67EffRVVC!$4x$?@#N$*dRYY^^H-E&EF# $ 8 8h.!I 9~s5z)99!wwxR87==! (CvvdSj2o&&"&&Sj*A*A#"	 yy~~d#cDKK&77/"	 r3   c                    U R                  U R                  5      u  p4pVU R                  R                  R                  U R                  -
  nU R                  R                  R
                  U R                  -
  n[        R                  R                  U5      n	[        UUUUUUU	U[        R                     40 U R                  D6n
U[        R                     (       a  [        Xx5      n[        R                  " X-   U:  5      (       d   [        R                  " XU-
  :  5      (       a  [        R                   " S["        S5        [        R                  R                  U
5      SU	-  :  a  [        R                   " S["        S5        U
$ )z
Get the second-order correction step.

Parameters
----------
step : `numpy.ndarray`, shape (n,)
    Trust-region step.
options : dict
    Options of the solver.

Returns
-------
`numpy.ndarray`, shape (n,)
    Second-order correction step.
zHThe second-order correction step does not respect the bound constraints.r   r   zNThe second-order correction step does not respect the trust-region constraint.)r   r'   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r,   r   r.   r   r   r   r   r   r   rC   soc_stepr   s               r0    get_second_order_correction_step,TrustRegion.get_second_order_correction_step  s3   " "??L#XX__$++-XX__$++-%'GMM"

 oo

 7==! (Cvvhnr)**bffRS.5H.I.I5"	 yy~~h'#,6;"	 r3   c                    U R                  U R                  U R                  U R                  U R                  5      nU R                  U R                  U-   X#U5      nU R                  U R                  SU R
                  R                  U R                  5      U R
                  R                  U R                  5      5      nU R                  U R                  U-   U R                  U5      U R                  U5      U R                  U5      5      n[        Xx-
  5      [        [        XV-
  5      -  :  a  XV-
  [        Xx-
  5      -  $ g)a  
Get the reduction ratio.

Parameters
----------
step : `numpy.ndarray`, shape (n,)
    Trust-region step.
fun_val : float
    Objective function value at the trial point.
cub_val : `numpy.ndarray`, shape (m_nonlinear_ub,)
    Nonlinear inequality constraint values at the trial point.
ceq_val : `numpy.ndarray`, shape (m_nonlinear_eq,)
    Nonlinear equality constraint values at the trial point.

Returns
-------
float
    Reduction ratio.
r         )r   r'   rZ   r^   rc   rO   rl   rm   r   r   r   r   r   )	r,   r   rY   r]   rb   	merit_old	merit_newmerit_model_oldmerit_model_news	            r0   get_reduction_ratioTrustRegion.get_reduction_ratioI  s   ( JJKKMMMMMM	
	 JJt{{T17WM	**KKKKOODKK(KKOODKK(	
 **KK$LLLLLL	
 01D3!<
 5
 
 )S1.   r3   c                    U R                  U R                  5      u  p#pE[        [        R                  R                  [        R                  " [        R                  " SU* 5      U/5      5      [        R                  R                  [        R                  " [        R                  " SX!-  U-
  5      XA-  U-
  /5      5      -
  S5      nU R                  U5      n[        R                  R                  [        R                  " U R                  U R                  U R                  U R                  /5      5      n[        U5      [        [        U5      -  :  a  [        XU-  5      nU R                  n	U R                   U R"                  [$        R&                     U-  ::  a?  [        U R"                  [$        R(                     U-  S5      U l        U R+                  5         XR                  :H  $ )zr
Increase the penalty parameter.

Parameters
----------
step : `numpy.ndarray`, shape (n,)
    Trust-region step.
r         ?)r   r'   r   r   r   r   r   r   r   r   r!   r#   r%   r   r   rR   r   r   r   PENALTY_INCREASE_THRESHOLDPENALTY_INCREASE_FACTORr   )
r,   r   r   r   r   r   	viol_diffsqp_val	thresholdbest_index_saves
             r0   increase_penaltyTrustRegion.increase_penaltyy  s    "??L#IINN

3- iinn

3
S(89
S(  #
	& ,,t$IINNHH&&&&))))		
	 y>D3w<//I':;I//MMyCCD  	 A ABYNDM !//11r3   c                 v    [        U R                  U R                  5       5      U l        U R                  5         g)z!
Decrease the penalty parameter.
N)r   r   _get_low_penaltyr   r6   s    r0   decrease_penaltyTrustRegion.decrease_penalty  s+     DMM4+@+@+BCr3   c           
         U R                   nU R                  U R                  U R                  R                  U   U R                  R
                  USS24   U R                  R                  USS24   5      nU R                  R                  U R                  U R                  R
                  USS24   U R                  R                  USS24   5      nS[        -  [        U R                  R                  U R                  R                  5      -  [        [        U5      S5      -  n[        U R                  R                  5       GH  nXPR                   :w  d  M  U R                  R                  R!                  U5      nU R                  UU R                  R                  U   U R                  R
                  USS24   U R                  R                  USS24   5      nU R                  R                  UU R                  R
                  USS24   U R                  R                  USS24   5      nXr:  d  XrU-   :  d  M  X:  d  GM  UnUnUnGM     Xl        g)z"
Set the index of the best point.
Nr   r  )rR   r   r'   rO   rY   r]   rb   r   maxcvEPSr   r5   r   r   rangerU   rV   r   )	r,   rR   m_bestr_bestr   kx_valr   r_vals	            r0   r   TrustRegion.set_best_index  s    __
KKKK
+KK
A.KK
A.	
 KKKK
A.KK
A.
 $++--12 #f+s#$ 	 t{{'AOO#1177:

KK''*KK''1-KK''1-	 KK''1-KK''1-
 >esl&:u~!"J"F"F# ($ &r3   c                    [         R                  " U R                  R                  R                  U R                  R                  R                  SS2U R
                  [         R                  4   -
  S-  SS9nUc  SnUnOU R                  R                  U5      n[         R                  " SU[        U R                  [        R                     U R                  -  U R                  5      S-  -  5      S-  nSX@R
                  '   [         R                  " U[         R                   " U5      -  5      nU[         R"                  " X%   5      4$ )aO  
Get the index of the interpolation point to remove.

If `x_new` is not provided, the index returned should be used during
the geometry-improvement phase. Otherwise, the index returned is the
best index for included `x_new` in the interpolation set.

Parameters
----------
x_new : `numpy.ndarray`, shape (n,), optional
    New point to be included in the interpolation set.

Returns
-------
int
    Index of the interpolation point to remove.
float
    Distance between `x_best` and the removed point.

Raises
------
`numpy.linalg.LinAlgError`
    If the computation of a determinant fails.
Nr   r   axisr  g      @r   )r   sumrO   rU   r   rR   r   r   r   r   r   r   LOW_RADIUS_FACTORrC   r*   argmaxr   r   )r,   x_newdist_sqr   weightsk_maxs         r0   get_index_to_removeTrustRegion.get_index_to_remove  s"   2 &&))--++++//4??BJJ0NOP 	
 
 =EGKK,,U3E

	(C(CD++&
 	 
  (,GOO$		'BFF5M12bgggn---r3   c                    [         R                  R                  U5      nX R                  [        R
                     ::  a1  U =R                  U R                  [        R                     -  sl        gX R                  [        R                     ::  a:  [        U R                  [        R                     U R                  -  U5      U l        g[        U R                  [        R                     U R                  -  [        U R                  [        R                     U R                  -  U R                  [        R                     U-  5      5      U l        g)z
Update the trust-region radius.

Parameters
----------
step : `numpy.ndarray`, shape (n,)
    Trust-region step.
ratio : float
    Reduction ratio.
N)r   r   r   r   r   	LOW_RATIOrC   DECREASE_RADIUS_FACTOR
HIGH_RATIOr   r   INCREASE_RADIUS_FACTORINCREASE_RADIUS_THRESHOLD)r,   r   ratios_norms       r0   update_radiusTrustRegion.update_radius  s     %OOI$7$788KK4??9+K+KLLKooi&:&:;;	 @ @A++DK 	 @ @A++OOI$D$DEkk"OOI$G$GH	DKr3   c                    U R                   [        R                     U[        R                     -  U R
                  :  a1  U =R
                  U R                   [        R                     -  sl        OU R                   [        R                     U[        R                     -  U R
                  :  a:  [        R                  " U R
                  U[        R                     -  5      U l        OU[        R                     U l        [        U R                   [        R                     U R                  -  U R
                  5      U l        g)zx
Enhance the resolution of the trust-region framework.

Parameters
----------
options : dict
    Options of the solver.
N)r   r   LARGE_RESOLUTION_THRESHOLDr   RHOENDr*   DECREASE_RESOLUTION_FACTORMODERATE_RESOLUTION_THRESHOLDr   r   r   r'  r+   r,   r.   s     r0   enhance_resolutionTrustRegion.enhance_resolution9  s     OOI@@Agnn%&oo OOt44  O OOICCDgnn%&oo !ggdoo(/(?'@ ADO &gnn5DO OOI<<=LOO
r3   c                 x    U R                   R                  [        R                  " U R                  5      U5        g)zd
Shift the base point to `x_best`.

Parameters
----------
options : dict
    Options of the solver.
N)rO   shift_x_baser   copyr'   r4  s     r0   r8  TrustRegion.shift_x_baseZ  s%     	  !5w?r3   c           	      p   U R                   R                  R                  U-  U R                   R                  R                  :  nU R                  S:  nU R                   R
                  R                  U:  nU R                   R
                  R                  U:*  n[        R                  " U5      n[        R                  " U5      n[        R                  " U5      n[        R                  " U5      n	Xg-   U R                  -   U R                  -   S:  Ga"  [        R                  " U R                   R                  5      n
[        R                  XSS24   * XSS24   U R                   R                  R                  USS24   U R                  R!                  X5      U R                   R                  R"                  U R                  R%                  U5      4   nU R                  R'                  U5      n[        R(                  " UR*                  S   [        R,                  * 5      nSUSX-   U-   U-   & [/        UR0                  U* U[        R,                  4SS9nUR2                  X-   X-   U-    U R4                  U'   SU R4                  U) '   UR2                  UU	-   U-   UU	-   U-   U-    U R6                  U'   SU R6                  U) '   UR2                  UU	-   U-   U-   UU	-   U-   U-   U R                  -    U R8                  SS& UR2                  X-   U-   U-   U R                  -   S U R:                  SS& gg)a	  
Set the Lagrange multipliers.

This method computes and set the Lagrange multipliers of the linear and
nonlinear constraints to be the QP multipliers.

Parameters
----------
x : `numpy.ndarray`, shape (n,)
    Point at which the Lagrange multipliers are computed.
r   r   Nbvls)r   method)r   rg   rh   ri   r^   r   r   r   r   r   r    r$   r   r5   r_rO   rt   rj   ru   rs   fullshapeinfr   r   ro   r   r#   r!   r%   )r,   ro   incl_linear_ubincl_nonlinear_ubincl_xlincl_xur   r"   m_xlm_xuidentityc_jacr   xl_lmress                  r0   r&   TrustRegion.set_multiplierse  s    --1TXX__5I5II MMS0((//$$)((//$$)&&~6))*;<(( (4+;+;;%%&()* vvdhhjj)HEE1*%%!$$$^Q%67$$Q:$$$$Q')E [[))!,FGGEKKNRVVG4EBEE>DK+->?rvv	C 25DK+52D~. 36D/7:uu" !!8D!!"34 9<D!!#4"45$'EE !! "&"" !"! ""	"#		%Dq! (+uuk)N:T=M=MMN(D!!!$a*r3   c                 H   [         R                  U R                  R                  R                  [         R
                  S S 24   U R                  R                  R                  R                  -   U R                  R                  R                  R                  -  U R                  R                  R                  [         R
                  S S 24   -
  U R                  R                  4   nU R                  R                  R                  [         R
                  S S 24   U R                  R                  R                  R                  -   U R                  R                  R                  R                  -  U R                  R                  R                  [         R
                  S S 24   -
  n[         R                  " UU* U R                  R                   U R                  R                   * /5      n[         R                  " X/5      n[         R"                  " USS9n[         R$                  " USS9nUU R&                  [(        R*                     U-  :  n[         R,                  " U5      (       a  [         R"                  " U R                  R.                  5      n[         R$                  " U R                  R.                  5      n[         R0                  " SXF   5      n	[         R2                  " XV   U	-
  5      n
U
[4        X-
  -  :  a	  X-
  U
-  nU$ [         R6                  n U$ SnU$ )Nr   r  r   )r   c_rO   rU   x_baser   r   r   r   rg   rh   ri   r]   rj   rk   r   rb   nanminnanmaxr   r   THRESHOLD_RATIO_CONSTRAINTSr   rY   minimumr   r   rA  )r,   r_val_ubr_val_eqr  c_minc_maxindicesf_minf_max	c_min_negc_diffr   s               r0   r  TrustRegion._get_low_penalty  sa   55))00Q?++++//112 hhoo""$$	%
 hhoo""2::q=12 KK!
 KK%%,,RZZ];kk''++--.HHOO  ""# &*XX__%9%9"**a-%HI 88	##$$$	
 (-.		%a(		%a(ooiCCDuLM 	 66'??IIdkk112EIIdkk112E

37IVVENY67F.. =F2
  &&  Gr3   )r   r   r!   r   r%   r#   r   r   r   r+   r)   rC   r*   )NNNr   ).__name__
__module____qualname____firstlineno____doc__r1   propertyr5   r   r    r"   r$   rC   setterr*   r   rO   rR   r'   rZ   r^   rc   rp   rv   r}   r   r   r   r   r   r   r   r   r   r   r   r	  r  r   r#  r-  r5  r8  r&   r  __static_attributes__ r3   r0   r   r      s   .'` 	 	 	$ 	$ 	$ 	$ 	' 	' 	' 	' 	 	 ]]+ +"     	& 	& 	 	 	 	 	  	  @ @ 	4 	4 	7 	7 	7 	7
0
, 
.
*
(
*
*@,"\r,hUn0d.`62p(&T5.n@
B	@IV(r3   r   )r   numpyr   scipy.optimizer   rO   r   r   settingsr   r   
subsolversr	   r
   r   r   r   subsolvers.optimr   utilsr   finfofloattinyr   epsr  r   rf  r3   r0   <module>rq     sZ      % % (  : ! 
xxhhuoA Ar3   