
    (phȱ                         S SK r S SKrS SKJr  SSKJr  \R                  " \5      R                  r
\R                  " \5      R                  rS rS rS rS rS	 rS
 rS rS rg)    N)qr   )get_arrays_tolc                    U(       Gan  [        U [        R                  5      (       a  U R                  S:X  d   e[        R
                  " U5      R                  U 5      (       d   e[        U[        R                  5      (       a  UR                  U R                  :X  d   e[        U[        R                  5      (       a  UR                  U R                  :X  d   e[        U[        5      (       d   e[        U[        5      (       d   e[        X#5      n[        R                  " X':*  5      (       d   e[        R                  " X7* :  5      (       d   e[        R                  " U5      (       a  US:  d   e[        R                  " US5      n[        R                  " US5      nU R                  n[        R                   " U 5      n [        R                   " U 5      n	US:  U S:  -  US:  U S:  -  -  n
[        R"                  " U 5      n[        R"                  " U5      nX
   * X'   SnSnSnU[        R$                  " U
5      :  GaH  X-  nUS[&        -  U-  [)        S[        R*                  R-                  U 5      5      -  :  a  GO [/        XU5      nU* U-  SU-  ::  a  GOU" U5      nUU-  nU[2        [5        U5      -  :  a  [)        U* U-  S5      nO[        R6                  n[9        UU5      nU* USU-  U-  -   -  SU-  ::  a  GOU[        R6                  * :  U[2        * [        R4                  " X+-
  5      -  :  -  nU[        R6                  :  U[2        [        R4                  " X;-
  5      -  :  -  n[        R:                  " U[        R6                  5      n[        R:                  " U[        R6                  5      n[        R                  " UU   UU   -
  UU   -  S5      UU'   [        R                  " UU   UU   -
  UU   -  S5      UU'   [        R8                  " U5      n[        R8                  " U5      n[9        UU5      n[9        UU5      nUS:  aA  [        R<                  " X   UX   -  -   X*   X:   5      X'   U UU-  -  n UUUSU-  U-  -   -  -  nU[9        UU5      :  a&  X
   UU
   -  U-  nUX   -  X
   -
  X'   SX) '   US-  nOUU:  aW  UU::  a  [        R>                  " U5      nUU   UU'   O[        R>                  " U5      nUU   UU'   SU
U'   X
   * X'   SX) '   SnO?UU::  a  [A        U5      nUU   UU'   SU
U'   UU::  a  [A        U5      nUU   UU'   SU
U'   S	nOU[        R$                  " U
5      :  a  GMH  URC                  S
S	5      (       Ga,  U(       Ga$  [        R                   " U5      nU	U-  SU-  U" U5      -  -   n [        R$                  " U
5      S:  Ga  X   X   -  n!X
   X
   -  n"X
   X   -  n#[        RD                  " [)        U!U"-  U#S-  -
  S5      5      * nU#X   -  U!X
   -  -
  X'   SX) '   USU-  :  d<  [        RF                  " U[2        * [        R4                  " X   5      -  :  5      (       a  GO$X==   U* -  ss'   [        RH                  " U5      n$[        RH                  " U5      n%X   S-  X   S-  -   X*   S-  -
  U$U
'   X   S-  X   S-  -   X:   S-  -
  U%U
'   [        RD                  " U$U$S:     5      UU$S:     -
  U$U$S:  '   [        RD                  " U%U%S:     5      UU%S:     -   U%U%S:  '   [        R                  " X-
  S5      n&[        R                  " X;-
  S5      n'U$[2        U&-  :  nU%[2        U'-  :  n[        RJ                  " U5      n([        RJ                  " U5      n)[        R                  " U(U   U&U   U$U   -  5      U(U'   [        R                  " U)U   U'U   U%U   -  5      U)U'   [        R8                  " U(5      n*[        R8                  " U)5      n+[9        U*U+5      n,U" U5      n-U" U5      nUU--  n.UU-  nUU-  n/Sn0[M        U0S-
  U,-  S-   5      n0[        RN                  " U,U0-  U,U05      n1SU1-  SU1S-  -   -  n2U2U#U1-  U-
  U1U.-  -
  U2U1U/-  SUU.-
  -  -
  -  -   -  n3[        R                  " U3S:*  5      (       a  O[        RP                  " U35      n4SU1U4   S-  -
  SU1U4   S-  -   -  n5U5X   -  U2U4   X   -  -   X'   U U5S-
  U--  U2U4   U-  -   -  n UU3U4   -  nU,S:  aF  U4U0S-
  :X  a=  U*U,::  a  [A        U(5      nUU   UU'   SU
U'   U+U,::  a  [A        U)5      nUU   UU'   SU
U'   OO[        R$                  " U
5      S:  a  GM  X-  SU-  U" U5      -  -   U :  a  UnU(       af  [        R                  " X+:*  5      (       d   e[        R                  " X:*  5      (       d   e[        R*                  R-                  U5      SU-  :  d   eU$ ! [0         a     GM  f = f)a  
Minimize approximately a quadratic function subject to bound constraints in
a trust region.

This function solves approximately

.. math::

    \min_{s \in \mathbb{R}^n} \quad g^{\mathsf{T}} s + \frac{1}{2}
    s^{\mathsf{T}} H s \quad \text{s.t.} \quad
    \left\{ \begin{array}{l}
        l \le s \le u\\
        \lVert s \rVert \le \Delta,
    \end{array} \right.

using an active-set variation of the truncated conjugate gradient method.

Parameters
----------
grad : `numpy.ndarray`, shape (n,)
    Gradient :math:`g` as shown above.
hess_prod : callable
    Product of the Hessian matrix :math:`H` with any vector.

        ``hess_prod(s) -> `numpy.ndarray`, shape (n,)``

    returns the product :math:`H s`.
xl : `numpy.ndarray`, shape (n,)
    Lower bounds :math:`l` as shown above.
xu : `numpy.ndarray`, shape (n,)
    Upper bounds :math:`u` as shown above.
delta : float
    Trust-region radius :math:`\Delta` as shown above.
debug : bool
    Whether to make debugging tests during the execution.

Returns
-------
`numpy.ndarray`, shape (n,)
    Approximate solution :math:`s`.

Other Parameters
----------------
improve_tcg : bool, optional
    If True, a solution generated by the truncated conjugate gradient
    method that is on the boundary of the trust region is improved by
    moving around the trust-region boundary on the two-dimensional space
    spanned by the solution and the gradient of the quadratic function at
    the solution (default is True).

Notes
-----
This function implements Algorithm 6.2 of [1]_. It is assumed that the
origin is feasible with respect to the bound constraints and that `delta`
is finite and positive.

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.
           r   F      $      ?:0yE>      ?Timprove_tcg       @:0yE      皙?))
isinstancenpndarrayndiminspect	signaturebindshapefloatboolr   allisfiniteminimummaximumsizecopy
zeros_likecount_nonzeroEPSmaxlinalgnorm	_alpha_trZeroDivisionErrorTINYabsinfmin	full_likeclipargmin_argmingetsqrtanyzerosonesintlinspaceargmax)6grad	hess_prodxlxudeltadebugkwargstoln	grad_origfree_bdstepsdkreductboundary_reachedgrad_sdalpha_trhess_sdcurv_sd
alpha_quadalphai_xli_xuall_alpha_xlall_alpha_xualpha_xlalpha_xualpha_bdbetai_new	step_basestep_comparatorstep_sqgrad_sq	grad_steptemp_xltemp_xudist_xldist_xuall_t_xlall_t_xut_xlt_xut_bd	hess_step	curv_stepcurv_step_sd	n_samples	t_samples
sin_values
all_reducti_max	cos_values6                                                         U/var/www/html/venv/lib/python3.13/site-packages/scipy/_lib/cobyqa/subsolvers/optim.pytangential_byrd_omojokunrr      s
   @ $

++		Q>>  +006666"bjj))bhh$**.DDD"bjj))bhh$**.DDD%''''%&&&&R$vvbi    vvbDj!!!!{{5!!eck11	B	B	B	B 			A774=DI STCZ(b3h4#:-FGG ==D	t	B=.BK	AF
bw'
')eckAoC1E(FFF	 51H 9w$-/ B-w,TCL((gX/5JJ Hj)6WsU{W445F bffWtebffRY.?&?!?@RVVTBFF29,=%= =>||D"&&1||D"&&1ZZXT
"bh.
T  ZZXT
"bh.
T 66,'66,'x* E8$3;GG 33DM
 EGO#Dewuw)>>??F3x** MGG$44?D,t}<BKBxLFAX 5 		,/ iU		,/ iU"GEN=.BKBxLA
 5 - iU!&5 - iU!&#{ bw'
'@ zz-&&+;GGDM	#i/#	/IE
 3
 
 w'!+ mdm3Gmdm3G5Iwws7W#4y#~#EsKLLG#dm3g6MMBKBxL%&.(BFFD5266"+#666- - KG8#K
 hhqkGhhqkG$r{c'99BK3<NN G $r{c'99BK3<NN G #./"Ws]2CC GcM" #./"Ws]2CC GcM" jjC0GjjC0GTG^+DTG^+DwwqzHwwqzHZZ-HTN  ZZ-HTN 66(#D66(#DtT?D "$ImGy(I7lG'>L
 IY]d2Q67ID9$4dIFIyC)S.,@AJ#I%i'( |+cWy5H.IIKKJ vvjC'(( IIj)Ey/366i&#--I DM)Ju,=,KK M Y_	1Ju4E4OOODj''F
 czey1}44<#H-E"$U)DK%*GEN4<#H-E"$U)DK%*GENI w'!+P cDj9T?::_LDvvbj!!!!vvdj!!!!yy~~d#cEk111KY ! 		s   	i 
iic	                 0   U(       Gax  [        U [        R                  5      (       a  U R                  S:X  d   e[        R
                  " U5      R                  U 5      (       d   e[        U[        R                  5      (       a  UR                  U R                  :X  d   e[        U[        R                  5      (       a  UR                  U R                  :X  d   e[        U[        R                  5      (       a-  UR                  S:X  a  UR                  S   U R                  :X  d   e[        U[        R                  5      (       a-  UR                  S:X  a  UR                  UR                  S   :X  d   e[        U[        R                  5      (       a-  UR                  S:X  a  UR                  S   U R                  :X  d   e[        U[        5      (       d   e[        U[        5      (       d   e[        X#5      n
[        R                  " X*:*  5      (       d   e[        R                  " X:* :  5      (       d   e[        R                  " XZ* :  5      (       d   e[        R                  " U5      (       a  US:  d   e[        R                  " US5      n[        R                  " US5      n[        R                  " US5      nU R                  n[        R                   " U 5      n [        R                   " U 5      nUS:  U S:  -  nUS:  U S:  -  nUS:  X@-  S:  -  n[#        XFXU5      u  nn[        R$                  " U 5      nUSS2US24   * USS2US24   R&                  U -  -  n[        R                   " U5      nSnSnSnUUU-
  :  Ga`  U U-  nUS[(        -  U-  [+        S[        R,                  R/                  U 5      5      -  :  a  GO [1        UUU5      nU* U-  S	U-  ::  a  GOU" U5      nUU-  nU[4        [7        U5      -  :  a  [+        U* U-  S5      nO[        R8                  n[;        UU5      nU* US
U-  U-  -   -  S	U-  ::  a  GOX[        R8                  * :  -  U[4        * [        R6                  " UU-
  5      -  :  -  nX[        R8                  :  -  U[4        [        R6                  " UU-
  5      -  :  -  n[        R<                  " U[        R8                  5      n [        R<                  " U[        R8                  5      n![        R                  " UU   UU   -
  UU   -  S5      U U'   [        R                  " UU   UU   -
  UU   -  S5      U!U'   [        R:                  " U 5      n"[        R:                  " U!5      n#[;        U"U#5      n$UU-  n%UU%[4        [        R6                  " U5      -  :  -  n&[        R<                  " U[        R8                  5      n'UU&   U%U&   -  U'U&'   [        R:                  " U'[        R8                  S9n([;        UU$U(5      nUS:  aS  [        R>                  " UUU-  -   X#5      nU UU-  -  n [        R                  " SUUU%-  -
  5      nUUUS
U-  U-  -   -  -  nU[;        UU$U(5      :  a;  USS2US24   USS2US24   R&                  U -  -  n)U)U-  U-  n*U*U-  U)-
  nUS-  nGOUU:  a  U"U::  a$  [        R@                  " U 5      n+UU+   UU+'   SUU+'   OEU#U::  a$  [        R@                  " U!5      n+UU+   UU+'   SUU+'   O[        R@                  " U'5      n+SUU+'   [#        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   R&                  U -  -  nSnOgU"U::  a  [C        U 5      n+UU+   UU+'   SUU+'   U#U::  a  [C        U!5      n+UU+   UU+'   SUU+'   U(U::  a  [C        U'5      n+SUU+'   [#        UUUUU5      u  nnSnOUUU-
  :  a  GM`  U	RE                  SS5      (       Ga  U(       Ga  UU:  Ga  [        R                   " U5      n,UU:  GaL  USS2US24   USS2US24   R&                  U-  -  n-USS2US24   USS2US24   R&                  U -  -  n)U-U--  n.U)U)-  n/U)U--  n0[        RF                  " [+        U.U/-  U0S-  -
  S5      5      * nUSS2US24   USS2US24   R&                  U0U-  U.U -  -
  -  -  nUSU-  :  d:  [        RH                  " U[4        * [        R6                  " U5      -  :  5      (       a  GOYUU* -  n[        RJ                  " U5      n1[        RJ                  " U5      n2[        R                  " UU-
  S5      n3[        R                  " UU-
  S5      n4UU   S-  U3U   U3U   SU-U   -  -
  -  -
  U1U'   UU   S-  U4U   U4U   SU-U   -  -   -  -
  U2U'   [        RF                  " U1U1S:     5      UU1S:     -
  U1U1S:  '   [        RF                  " U2U2S:     5      UU2S:     -   U2U2S:  '   U1[4        U3-  :  nU2[4        U4-  :  n[        RL                  " U5      n5[        RL                  " U5      n6[        R                  " U5U   U3U   U1U   -  5      U5U'   [        R                  " U6U   U4U   U2U   -  5      U6U'   [        R:                  " U55      n7[        R:                  " U65      n8[;        U7U85      n9[        R$                  " U5      n:UU--  n;UU-  n%U%U   S-  UU   UU   SU;U   -  -   -  -
  U:U'   [        RF                  " U:U:S:     5      U%U:S:     -   U:U:S:  '   U:[4        U-  :  n&[        RN                  " U5      n<[        R                  " U<U&   UU&   U:U&   -  5      U<U&'   [        R:                  " U<SS9n=[;        U9U=5      n>U" U-5      n?U" U5      nU-U?-  n@UU-  nU-U-  nASnB[Q        UBS-
  U>-  S-   5      nB[        RR                  " U>UB-  U>UB5      nCSUC-  SUCS-  -   -  nDUDU0UC-  U-
  UDS
UCS-  -  U@-  SUC-  UA-  -
  S
U-  -   -  -
  -  nE[        R                  " UES:*  5      (       a  GO[        RT                  " WE5      nFSWCUF   S-  -
  SUCUF   S-  -   -  nG[        R>                  " UUGS-
  U--  -   WDUF   U-  -   UU5      nU UGS-
  U?-  UDUF   U-  -   -  n [        R                  " SUUGS-
  U;-  -
  UDUF   U%-  -
  5      nUUEUF   -  nU>S:  an  WFWBS-
  :X  ae  U7U>::  a  [C        U55      n+UU+   UU+'   SUU+'   U8U>::  a  [C        U65      n+UU+   UU+'   SUU+'   U=U>::  a  [C        U<5      n+SUU+'   [#        UUUUU5      u  nnOO	UU:  a  GML  UU-  S
U-  U" U5      -  -   UU,-  S
U,-  U" U,5      -  -   :  a  U,nU(       a  [        X#5      n
[        R                  " UU:*  5      (       d   e[        R                  " UU:*  5      (       d   e[        R                  " UU-  XZ-   :*  5      (       d   e[        R                  " [        R6                  " UU-  5      U
:*  5      (       d   e[        R,                  R/                  U5      SU-  :  d   eU$ ! [2         a     GM  f = f)az	  
Minimize approximately a quadratic function subject to bound and linear
constraints in a trust region.

This function solves approximately

.. math::

    \min_{s \in \mathbb{R}^n} \quad g^{\mathsf{T}} s + \frac{1}{2}
    s^{\mathsf{T}} H s \quad \text{s.t.} \quad
    \left\{ \begin{array}{l}
        l \le s \le u,\\
        A_{\scriptscriptstyle I} s \le b_{\scriptscriptstyle I},\\
        A_{\scriptscriptstyle E} s = 0,\\
        \lVert s \rVert \le \Delta,
    \end{array} \right.

using an active-set variation of the truncated conjugate gradient method.

Parameters
----------
grad : `numpy.ndarray`, shape (n,)
    Gradient :math:`g` as shown above.
hess_prod : callable
    Product of the Hessian matrix :math:`H` with any vector.

        ``hess_prod(s) -> `numpy.ndarray`, shape (n,)``

    returns the product :math:`H s`.
xl : `numpy.ndarray`, shape (n,)
    Lower bounds :math:`l` as shown above.
xu : `numpy.ndarray`, shape (n,)
    Upper bounds :math:`u` as shown above.
aub : `numpy.ndarray`, shape (m_linear_ub, n)
    Coefficient matrix :math:`A_{\scriptscriptstyle I}` as shown above.
bub : `numpy.ndarray`, shape (m_linear_ub,)
    Right-hand side :math:`b_{\scriptscriptstyle I}` as shown above.
aeq : `numpy.ndarray`, shape (m_linear_eq, n)
    Coefficient matrix :math:`A_{\scriptscriptstyle E}` as shown above.
delta : float
    Trust-region radius :math:`\Delta` as shown above.
debug : bool
    Whether to make debugging tests during the execution.

Returns
-------
`numpy.ndarray`, shape (n,)
    Approximate solution :math:`s`.

Other Parameters
----------------
improve_tcg : bool, optional
    If True, a solution generated by the truncated conjugate gradient
    method that is on the boundary of the trust region is improved by
    moving around the trust-region boundary on the two-dimensional space
    spanned by the solution and the gradient of the quadratic function at
    the solution (default is True).

Notes
-----
This function implements Algorithm 6.3 of [1]_. It is assumed that the
origin is feasible with respect to the bound and linear constraints, and
that `delta` is finite and positive.

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.
r   r   r   r   NFr	   r
   r   r   initialTr   r   r   r   r   r   )+r   r   r   r   r   r   r   r   r!   r   r   r   r   r   r   r    r"   qr_tangential_byrd_omojokunr#   Tr%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   	ones_liker8   r9   r:   )Hr;   r<   r=   r>   aubbubaeqr?   r@   rA   rB   rC   rD   free_xlfree_xufree_ubn_actqrF   rG   residrH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   aub_sdi_uball_alpha_ubalpha_ub	grad_projrX   rY   rZ   	step_projr\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   temp_ubaub_stepall_t_ubt_ubt_minrh   ri   rj   rk   rl   rm   rn   ro   rp   sH                                                                           rq   $constrained_tangential_byrd_omojokunr   C  s   f $

++		Q>>  +006666"bjj))bhh$**.DDD"bjj))bhh$**.DDDsBJJ''A		!		)	
*
 sBJJ''ACIIaL(	
)
 sBJJ''A		!		)	
* %''''%&&&&R$vvbi    vvbDj!!!!vvcTk""""{{5!!eck11	B	B	B	B
**S#
C 			A774=DI CxD3J'GCxD3J'GSySZ#-.G*3WwOHE1 ==D
AuvI,!AuvI,..4/	0BGGCLE	AF
a%i-)eckAoC1E(FFF	 r51H 9w$-/ B-w,TCL((gX/5JJ Hj)6WsU{W445F w,'2rDy8I0I+IJrvv+&"tbffR$Y6G/G*GH||D"&&1||D"&&1ZZXT
"bh.
T  ZZXT
"bh.
T 66,'66,'x* r&4"&&-#778||C0"4[6$<7T66,7 E8X.3;774%"*,b5DEGO#DJJsEEFN$:;Eewuw)>>??F3x844 !UV)!UV)(=>I'72DY&BFAX 5 		,/ iU!&U"		,/ iU!&		,/!&2HE1 AuvI,!AuvI,..4"78BA
 5 - iU!&5 - iU!&5 -!&2HE1  $k a%i-p zz-&&+;	GGDM	ai !UV)!UV)(=>I!UV)!UV)(=>I)+G)+G!I-Iwws7W#4y#~#EsKLLG1ef9!UV))d"2Wt^"CDB %&.(BFFD5266":--- - 7(NB hhqkGhhqkGjjC0GjjdC0G!'{c1GG4D 37);#;;5  GG  "'{c1GG4D 37);#;;5  GG #./"Ws]2CC GcM" #./"Ws]2CC GcM" TG^+DTG^+DwwqzHwwqzHZZ-HTN  ZZ-HTN 66(#D66(#DtT?D mmE*GYH2XF%g#5ggx'8!889  GG #./&32GG GcM" TE\)D||E*HZZdgdm+HTN 66(C0DdOE "),ImG!I-I7lG$w.L
 IY]e3a78IEI$5uiHIyC)S.,@AJ#I%)S.(94Io45Gm$	J vvjC'(( IIj)Ey/366i&#--I 77	C944z%7H27MMD
 Y_	1Ju4E4OOODJJs?h./U#f,-E j''F
 s{u	A55=#H-E"$U)DK%*GEN5=#H-E"$U)DK%*GEN5=#H-E%*GEN6q c aij tcDj9,
 
 
	!C)Oi	6J$JJK DR$vvbDj!!!!vvdbj!!!!vvcDjCI-....vvbffS4Z(C/0000yy~~d#cEk111Ke ! 		s   z 
zzc           	      L   U(       Ga7  [        U [        R                  5      (       a  U R                  S:X  d   e[        U[        R                  5      (       a-  UR                  S:X  a  UR                  U R
                  S   :X  d   e[        U[        R                  5      (       a0  UR                  S:X  a   UR
                  S   U R
                  S   :X  d   e[        U[        R                  5      (       a-  UR                  S:X  a  UR                  UR
                  S   :X  d   e[        U[        R                  5      (       a  UR
                  U R
                  S   4:X  d   e[        U[        R                  5      (       a  UR
                  U R
                  S   4:X  d   e[        U[        5      (       d   e[        U[        5      (       d   e[        XE5      n	[        R                  " XI:*  5      (       d   e[        R                  " XY* :  5      (       d   e[        R                  " U5      (       a  US:  d   e[        R                  " US5      n[        R                  " US5      nU R
                  u  p[        R                  UR                  U* -  [        R                  " SU* 5      4   nUS:  USU S:  -  nUS:  USU S:  -  nUS:  nUS:  XSU -  XS -
  S:  -  n[        U UUUU5      u  nn[        R                   " X3-  XS XS -  -   5      n[        R"                  " U5      nUSS2US24   * USS2US24   R                  U-  -  nXUS -   nSnSnSnUX-   U-
  :  GaP  UU-  nUS[$        -  U-  ['        S[        R(                  R+                  U5      5      -  :  a  GO [-        UUSU U5      n [3        U[-        XS UUS U5      5      nU* U-  S	U-  ::  a  GO[        R                  UR                  UUSU -  -  UUS 4   nUU-  nU[4        [7        U5      -  :  a  ['        U* U-  S5      nO[        R0                  n[3        UU5      nU* US
U-  U-  -   -  S	U-  ::  a  GOGX[        R0                  * :  -  USU [4        * [        R6                  " UU-
  5      -  :  -  n X[        R0                  :  -  USU [4        [        R6                  " UU-
  5      -  :  -  n!UUUS [4        * [        R6                  " XS 5      -  :  -  n"[        R8                  " U[        R0                  5      n#[        R8                  " U[        R0                  5      n$[        R8                  " U[        R0                  5      n%[        R                  " UU    UU    -
  USU U    -  S5      U#U '   [        R                  " UU!   UU!   -
  USU U!   -  S5      U$U!'   [        R                  " XS U"   * UUS U"   -  S5      U%U"'   [        R2                  " U#5      n&[        R2                  " U$5      n'[        R2                  " U%[        R0                  S9n([3        U&U'U(5      n)U USU -  UUS -
  n*UU*[4        [        R6                  " U5      -  :  -  n+[        R8                  " U[        R0                  5      n,UU+   U*U+   -  U,U+'   [        R2                  " U,[        R0                  S9n-[3        UU)U-5      nUS:  aV  [        R:                  " UUUSU -  -   XE5      nUUU-  -  n[        R                  " SUUU*-  -
  5      nUUUS
U-  U-  -   -  -  nU[3        UU)U-5      :  a;  USS2US24   USS2US24   R                  U-  -  n.U.U-  U-  n/U/U-  U.-
  nUS-  nGOUU:  a  U&U::  a$  [        R<                  " U#5      n0UU0   UU0'   SUU0'   OgU'U::  a$  [        R<                  " U$5      n0UU0   UU0'   SUU0'   O=U(U::  a  [        R<                  " U%5      n0SUU0'   O[        R<                  " U,5      n0SUU0'   [        XXU5      u  nnUSS2US24   * USS2US24   R                  U-  -  nSnO?U&U::  a  [?        U#5      n0UU0   UU0'   SUU0'   U'U::  a  [?        U$5      n0UU0   UU0'   SUU0'   SnOUX-   U-
  :  a  GMP  URA                  SS5      (       Ga  U(       Ga  [        RB                  " U5      n1X-  n2U R                  [        R                  " U U-  U-
  S5      -  UR                  UU-  U-
  -  -   n[        R"                  " U5      n[        RD                  " U25      S:  Ga  UU2   UU2   -  n3UU2   UU2   -  n4UU2   UU2   -  n5[        R                   " ['        U3U4-  U5S-  -
  S5      5      * nU5UU2   -  U3UU2   -  -
  UU2'   SUU2) '   USU-  :  d=  [        RF                  " U[4        * [        R6                  " UU2   5      -  :  5      (       a  GOUU2==   U* -  ss'   [        R"                  " U5      n6[        R"                  " U5      n7UU2   S-  UU2   S-  -   UU2   S-  -
  U6U2'   UU2   S-  UU2   S-  -   UU2   S-  -
  U7U2'   [        R                   " U6U6S:     5      UU6S:     -
  U6U6S:  '   [        R                   " U7U7S:     5      UU7S:     -   U7U7S:  '   [        R                  " UU-
  S5      n8[        R                  " UU-
  S5      n9U6[4        U8-  :  n U7[4        U9-  :  n![        RH                  " U5      n:[        RH                  " U5      n;[        R                  " U:U    U8U    U6U    -  5      U:U '   [        R                  " U;U!   U9U!   U7U!   -  5      U;U!'   [        R2                  " U:5      n<[        R2                  " U;5      n=[3        U<U=5      n>Sn?[K        U?S-
  U>-  S-   5      n?[        RL                  " U>U?-  U>U?5      n@[        R                  " U U-  U-
  S5      nAUU-  U-
  nB[        RB                  " U5      nCSUCU2) '   [        RN                  " U?5      nD[Q        U?5       H  nESW@UE   -  SU@UE   S-  -   -  nF[        R:                  " UUFUU@UE   WC-  -
  -  -   UU5      nG[        R                  " U UG-  U-
  S5      nHUUG-  U-
  nIS
WAUA-  WBUB-  -   UHUH-  -
  UIUI-  -
  -  WDUE'   M     [        R                  " WDS:*  5      (       a  GO[        RR                  " WD5      nJSW@UJ   S-  -
  SU@UJ   S-  -   -  nKSU@UJ   -  SU@UJ   S-  -   -  nFUKUU2   -  UFUU2   -  -   UU2'   U R                  [        R                  " U U-  U-
  S5      -  UR                  UU-  U-
  -  -   nUUDUJ   -  nU>S:  aF  WJU?S-
  :X  a=  U<U>::  a  [?        U:5      n0UU0   UU0'   SU2U0'   U=U>::  a  [?        U;5      n0UU0   UU0'   SU2U0'   OO[        RD                  " U25      S:  a  GM  [        R                  " U U-  U-
  S5      nA[        R                  " U U1-  U-
  S5      nLUU-  U-
  nBUU1-  U-
  nMUAUA-  UBUB-  -   ULUL-  UMUM-  -   :  a  U1nU(       ah  [        R                  " UU:*  5      (       d   e[        R                  " UU:*  5      (       d   e[        R(                  R+                  U5      SU-  :  d   eU$ ! [.         a    [        R0                  n GNLf = f! [.         a     GN@f = f)a  
Minimize approximately a linear constraint violation subject to bound
constraints in a trust region.

This function solves approximately

.. math::

    \min_{s \in \mathbb{R}^n} \quad \frac{1}{2} \big( \lVert \max \{
    A_{\scriptscriptstyle I} s - b_{\scriptscriptstyle I}, 0 \} \rVert^2 +
    \lVert A_{\scriptscriptstyle E} s - b_{\scriptscriptstyle E} \rVert^2
    \big) \quad \text{s.t.}
    \quad
    \left\{ \begin{array}{l}
        l \le s \le u,\\
        \lVert s \rVert \le \Delta,
    \end{array} \right.

using a variation of the truncated conjugate gradient method.

Parameters
----------
aub : `numpy.ndarray`, shape (m_linear_ub, n)
    Matrix :math:`A_{\scriptscriptstyle I}` as shown above.
bub : `numpy.ndarray`, shape (m_linear_ub,)
    Vector :math:`b_{\scriptscriptstyle I}` as shown above.
aeq : `numpy.ndarray`, shape (m_linear_eq, n)
    Matrix :math:`A_{\scriptscriptstyle E}` as shown above.
beq : `numpy.ndarray`, shape (m_linear_eq,)
    Vector :math:`b_{\scriptscriptstyle E}` as shown above.
xl : `numpy.ndarray`, shape (n,)
    Lower bounds :math:`l` as shown above.
xu : `numpy.ndarray`, shape (n,)
    Upper bounds :math:`u` as shown above.
delta : float
    Trust-region radius :math:`\Delta` as shown above.
debug : bool
    Whether to make debugging tests during the execution.

Returns
-------
`numpy.ndarray`, shape (n,)
    Approximate solution :math:`s`.

Other Parameters
----------------
improve_tcg : bool, optional
    If True, a solution generated by the truncated conjugate gradient
    method that is on the boundary of the trust region is improved by
    moving around the trust-region boundary on the two-dimensional space
    spanned by the solution and the gradient of the quadratic function at
    the solution (default is True).

Notes
-----
This function implements Algorithm 6.4 of [1]_. It is assumed that the
origin is feasible with respect to the bound constraints and that `delta`
is finite and positive.

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.
r   r   r   r   NFr	   r
   r   r   rt   Tr   r   r   r   r   r   )*r   r   r   r   r!   r   r   r   r   r   r   r   r    r_rw   qr_normal_byrd_omojokunr4   r6   r%   r&   r'   r(   r)   r*   r-   r.   r+   r,   r/   r0   r1   r2   r3   r"   r$   r5   r7   r8   r9   emptyranger:   )Nry   rz   r{   beqr=   r>   r?   r@   rA   rB   m_linear_ubrC   r;   r|   r}   
free_slackr~   r   r   delta_slackrF   rG   r   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   i_slackrS   rT   all_alpha_slackrU   rV   alpha_slackrW   r   r   r   r   r   rX   rY   rZ   rE   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   rk   rl   resid_ubresid_eqr   rn   i	sin_valuestep_altresid_ub_altresid_eq_altro   rp   resid_ub_baseresid_eq_basesN                                                                                 rq   normal_byrd_omojokunr     s]   F #rzz**sxx1}<<sBJJ''ACIIaL(	
)
 sBJJ''A		!		!,	
-
 sBJJ''ACIIaL(	
) "bjj))bhh399Q</.III"bjj))bhh399Q</.III%''''%&&&&R$vvbi    vvbDj!!!!{{5!!eck11	B	B	B	B YYNK55#rzz#t445DCxD!HsN+GCxD!HsN+GsJSyS8^d2h6<=G&HE1 ''#)d2hb&99:K 88A;D
AuvI,!AuvI,..4/	0Bqr(NE	AF
ao%
%)eckAoC1E(FFF	 r"1vu5H	8YtBxAB%MNH 9w$-/ %%r"1v.1267w,TCL((gX/5JJ Hj)6WsU{W445F w,'2bq6TEBFF29<M4M+MNrvv+&"Ra&4"&&d:K3K*KL12$R1A)A AB||D"&&1||D"&&1,,sBFF3ZZXT
"b!fTl2
T  ZZXT
"b!fTl2
T $&::"XgAB0$
  66,'66,'ff_bff=x;7 r"1v12&&4"&&-#778||C0"4[6$<7T66,7 E8X.3;774%"Ra&.0"9DEGO#DJJsEEFN$:;Eewuw)>>??F3x844 !UV)!UV)(=>I'72DY&BFAX 5 		,/ iU!&U"		,/ iU!&%		/2$)
5!		,/!&.g7HE1 AuvI,!AuvI,..4"78BA
 5 - iU!&5 - iU!&#q ao%
%v zz-&&+;GGDM	#uurzz#*s"2C88355$J<
 
 XXa[w'!+ 7md7m3G7md7m3GWW5Iwws7W#4y#~#EsKLLG#d7m3gW6MMBwKBxL%&.(BFFD5266"W+#666- - wKG8#K
 hhqkGhhqkGW$r'{c'99BwK3<NN G W$r'{c'99BwK3<NN G #./"Ws]2CC GcM" #./"Ws]2CC GcM" jjC0GjjdC0GTG^+DTG^+DwwqzHwwqzHZZ-HTN  ZZ-HTN 66(#D66(#DtT?D
 IY]d2Q67ID9$4dIFIzz#*s"2C8HTzC'HI"%Iwh),J9%)A,.#	!8K2KL	779Yq\I-E(EFF
  "zz#.3*>D"X~3 #x')*"\12 #\12!
1 & vvjC'(( IIj)Ey/366i&#--I y//)E"2c"99;I%W5	BwK8OODM552::cDj3&6<<suud
S @ D j''F
 czey1}44<#H-E"$U)DK%*GEN4<#H-E"$U)DK%*GENW w'!+^ ::cDj3.4

3?S#8#>:#i#-x(X"55m+mm.KKL DvvbDj!!!!vvdbj!!!!yy~~d#cEk111Km ! 	vvH	 ! 		s$   5{4 | 4||
|#"|#c                 <   UR                   n[        R                  " U5      n[        [        R                  " U/X) S S 24   /Xb) S S 24   * /Xc) S S 24   //5      R
                  SS9u  pxn	[        R                  " [        R                  " [        R                  " U5      5      S[        -  U-  [        R                  R                  US [        R                  " UR                  5      2S [        R                  " UR                  5      24   SS9-  :  5      n
X4$ NT)pivotingg      $@r   )axis)r!   r   eyer   blockrw   r$   r,   diagr%   r'   r(   r.   r   )ry   r{   r|   r}   r~   rC   identityr   r_r   s              rq   rv   rv   c  s   AvvayH
Xq[!"8Q;''((A+&'		
 !
GA! 
rwwqz

 ))..,RVVAGG_,.?qww.??@q.
IJ	JE 8O    c                    U R                   u  pV[        R                  " U5      n[        R                  " U5      n[        [        R                  " X) S S 24   X) S S 24   * /[        R
                  " U[        R                  " U5      -
  U45      X) S S 24   * /Xq) S S 24   * [        R
                  " U[        R                  " U5      -
  U45      /Xr) S S 24   [        R
                  " U[        R                  " U5      -
  U45      //5      R                  SS9u  pn[        R                  " [        R                  " [        R                  " U
5      5      S[        -  Xe-   -  [        R                  R                  U
S [        R                  " U
R                   5      2S [        R                  " U
R                   5      24   SS9-  :  5      nX4$ r   )r   r   r   r   r   r6   r$   rw   r,   r   r%   r'   r(   r.   )ry   r|   r}   r   r~   r   rC   
identity_n
identity_mr   r   r   r   s                rq   r   r   {  s   YYNKJ$J
 !$!,,
 HHkB,<,<Z,HH!LMQ//
  !,,HHa""2"27";;[IJ
 x{+HHa""2"27";;[IJ	
& !+GA!. 
rwwqz
? ))..,RVVAGG_,.?qww.??@q.
IJ	JE 8Or   c                 :   X-  nX-  nUS-  X -  -
  n[         R                  " [        US-  XE-  -   S5      5      nUS::  a+  U[        [	        Xc-
  5      -  :  a  [        Xc-
  U-  S5      nU$ [	        Xc-   5      [        U-  :  a  [        XVU-   -  S5      nU$ [
        e)Nr   r   )r   r4   r&   r+   r,   r*   )rF   rG   r?   step_sdsd_sq
dist_tr_sqtemprL   s           rq   r)   r)     s    iGGEdk)J773w|e&88#>?D#~%$T^)<"<<%/5
 O	 
T^	tj0	0zG^4c: O  r   c                 \    [         R                  " U [         R                  " U 5      :  5      $ N)r   flatnonzeror&   xs    rq   _argmaxr         >>!rvvay.))r   c                 \    [         R                  " U [         R                  " U 5      :*  5      $ r   )r   r   r.   r   s    rq   r2   r2     r   r   )r   numpyr   scipy.linalgr   utilsr   finfor   tinyr+   epsr%   rr   r   r   rv   r   r)   r   r2    r   rq   <module>r      sh       " 
xxhhuosl	`Fzz0"J**r   