
    4[gȱ                         d dl Z d dlZd dlmZ ddlmZ  ej                  e      j                  Z
 ej                  e      j                  Zd Zd Zd Zd Zd	 Zd
 Zd Zd Zy)    N)qr   )get_arrays_tolc                    |r<t        | t        j                        r| j                  dk(  sJ t	        j
                  |      j                  |       sJ t        |t        j                        r|j                  | j                  k(  sJ t        |t        j                        r|j                  | j                  k(  sJ t        |t              sJ t        |t              sJ t        ||      }t        j                  ||k        sJ t        j                  || k\        sJ t        j                  |      r|dkD  sJ t        j                  |d      }t        j                  |d      }| j                  }t        j                   |       } t        j                   |       }	|dk  | dk  z  |dkD  | dkD  z  z  }
t        j"                  |       }t        j"                  |      }| |
    ||
<   d}d}d}|t        j$                  |
      k  rA| |z  }|dt&        z  |z  t)        dt        j*                  j-                  |             z  k\  rn	 t/        |||      }| |z  d|z  k  rn ||      }||z  }|t2        t5        |      z  kD  rt)        | |z  d      }nt        j6                  }t9        ||      }| |d|z  |z  z   z  d|z  k  rn|t        j6                   kD  |t2         t        j4                  ||z
        z  k  z  }|t        j6                  k  |t2        t        j4                  ||z
        z  kD  z  }t        j:                  |t        j6                        }t        j:                  |t        j6                        }t        j                  ||   ||   z
  ||   z  d      ||<   t        j                  ||   ||   z
  ||   z  d      ||<   t        j8                  |      }t        j8                  |      }t9        ||      }t9        ||      }|dkD  rEt        j<                  ||
   |||
   z  z   ||
   ||
         ||
<   | ||z  z  } |||d|z  |z  z   z  z  }|t9        ||      k  r+| |
   ||
   z  |z  }|||
   z  | |
   z
  ||
<   d||
 <   |dz  }n||k  rW||k  rt        j>                  |      }||   ||<   nt        j>                  |      }||   ||<   d|
|<   | |
    ||
<   d||
 <   d}n=||k  rtA        |      }||   ||<   d|
|<   ||k  rtA        |      }||   ||<   d|
|<   d	}n|t        j$                  |
      k  rA|jC                  d
d	      r|rt        j                   |      }|	|z  d|z   ||      z  z   } t        j$                  |
      dkD  r||
   ||
   z  }!| |
   | |
   z  }"| |
   ||
   z  }#t        jD                  t)        |!|"z  |#dz  z
  d             }|#||
   z  |!| |
   z  z
  ||
<   d||
 <   |d|z  k\  s6t        jF                  |t2         t        j4                  ||
         z  k\        rn||
xx   | z  cc<   t        jH                  |      }$t        jH                  |      }%||
   dz  ||
   dz  z   ||
   dz  z
  |$|
<   ||
   dz  ||
   dz  z   ||
   dz  z
  |%|
<   t        jD                  |$|$dkD           ||$dkD     z
  |$|$dkD  <   t        jD                  |%|%dkD           ||%dkD     z   |%|%dkD  <   t        j                  ||z
  d      }&t        j                  ||z
  d      }'|$t2        |&z  kD  }|%t2        |'z  kD  }t        jJ                  |      }(t        jJ                  |      })t        j                  |(|   |&|   |$|   z        |(|<   t        j                  |)|   |'|   |%|   z        |)|<   t        j8                  |(      }*t        j8                  |)      }+t9        |*|+      }, ||      }- ||      }||-z  }.||z  }||z  }/d}0tM        |0dz
  |,z  dz         }0t        jN                  |,|0z  |,|0      }1d|1z  d|1dz  z   z  }2|2|#|1z  |z
  |1|.z  z
  |2|1|/z  d||.z
  z  z
  z  z   z  }3t        j                  |3dk        rnt        jP                  |3      }4d|1|4   dz  z
  d|1|4   dz  z   z  }5|5||
   z  |2|4   ||
   z  z   ||
<   | |5dz
  |-z  |2|4   |z  z   z  } ||3|4   z  }|,dk  rC|4|0dz
  k(  r;|*|,k  rtA        |(      }||   ||<   d|
|<   |+|,k  rtA        |)      }||   ||<   d|
|<   nnt        j$                  |
      dkD  r|	|z  d|z   ||      z  z   | kD  r|}|r[t        j                  ||k        sJ t        j                  ||k        sJ t        j*                  j-                  |      d|z  k  sJ |S # t0        $ r Y w xY w)ad  
    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                                                         ]/var/www/html/bid-api/venv/lib/python3.12/site-packages/scipy/_lib/cobyqa/subsolvers/optim.pytangential_byrd_omojokunrr      s
   @ $

+		Q>>  +00666"bjj)bhh$**.DDD"bjj)bhh$**.DDD%'''%&&&R$vvbCi   vvbSDj!!!{{5!eck11	B	B	B	B 			A774=DI STCZ(b3h4#:-FGG ==D	t	B=.BwK	AF
bw'
')eckAoC1E(FFF	 r51H 9w$-/ B-w,TCL((gX/5JJ Hj)6WsU{W445F bffWtebffR$Y.?&?!?@RVVTBFF29,=%= =>||D"&&1||D"&&1ZZXT
"bh.
T  ZZXT
"bh.
T 66,'66,'x* E8$3;GGW7 3377DM
 EGO#Dewuw)>>??F3x** MGG$44?DG,tG}<BwKBxLFAX 5 		,/ iU		,/ iU"GEN=.BwKBxLA
 5 - iU!&5 - iU!&#{ bw'
'@ zz-&+;GGDM	#i/#	/IE
 3
 
 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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 DM)Ju,=7,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 tcDj9T?::_LDvvbDj!!!vvdbj!!!yy~~d#cEk111KY ! 		s   g 	g"!g"c	                    |r,t        | t        j                        r| j                  dk(  sJ t	        j
                  |      j                  |       sJ t        |t        j                        r|j                  | j                  k(  sJ t        |t        j                        r|j                  | j                  k(  sJ t        |t        j                        r+|j                  dk(  r|j                  d   | j                  k(  sJ t        |t        j                        r+|j                  dk(  r|j                  |j                  d   k(  sJ t        |t        j                        r+|j                  dk(  r|j                  d   | j                  k(  sJ t        |t              sJ t        |t              sJ t        ||      }
t        j                  ||
k        sJ t        j                  ||
 k\        sJ t        j                  ||
 k\        sJ t        j                  |      r|dkD  sJ t        j                  |d      }t        j                  |d      }t        j                  |d      }| j                  }t        j                   |       } t        j                   |       }|dk  | dk  z  }|dkD  | dkD  z  }|dkD  || z  dkD  z  }t#        |||||      \  }}t        j$                  |       }|dd|df    |dd|df   j&                  | z  z  }t        j                   |      }d}d}d}|||z
  k  rH| |z  }|dt(        z  |z  t+        dt        j,                  j/                  |             z  k\  rn	 t1        |||      }| |z  d	|z  k  rn ||      }||z  }|t4        t7        |      z  kD  rt+        | |z  d      }nt        j8                  }t;        ||      }| |d
|z  |z  z   z  d	|z  k  rn||t        j8                   kD  z  |t4         t        j6                  ||z
        z  k  z  }||t        j8                  k  z  |t4        t        j6                  ||z
        z  kD  z  }t        j<                  |t        j8                        } t        j<                  |t        j8                        }!t        j                  ||   ||   z
  ||   z  d      | |<   t        j                  ||   ||   z
  ||   z  d      |!|<   t        j:                  |       }"t        j:                  |!      }#t;        |"|#      }$||z  }%||%t4        t        j6                  |      z  kD  z  }&t        j<                  |t        j8                        }'||&   |%|&   z  |'|&<   t        j:                  |'t        j8                        }(t;        ||$|(      }|dkD  rRt        j>                  |||z  z   ||      }| ||z  z  } t        j                  d|||%z  z
        }|||d
|z  |z  z   z  z  }|t;        ||$|(      k  r;|dd|df   |dd|df   j&                  | z  z  })|)|z  |z  }*|*|z  |)z
  }|dz  }n||k  r|"|k  r#t        j@                  |       }+||+   ||+<   d||+<   nB|#|k  r#t        j@                  |!      }+||+   ||+<   d||+<   nt        j@                  |'      }+d||+<   t#        |||||      \  }}|dd|df    |dd|df   j&                  | z  z  }d}nd|"|k  rtC        |       }+||+   ||+<   d||+<   |#|k  rtC        |!      }+||+   ||+<   d||+<   |(|k  rtC        |'      }+d||+<   t#        |||||      \  }}d}n
|||z
  k  rH|	jE                  dd      rs|rp||k  rjt        j                   |      },||k  r$|dd|df   |dd|df   j&                  |z  z  }-|dd|df   |dd|df   j&                  | z  z  })|-|-z  }.|)|)z  }/|)|-z  }0t        jF                  t+        |.|/z  |0dz  z
  d             }|dd|df   |dd|df   j&                  |0|z  |.| z  z
  z  z  }|d|z  k\  s3t        jH                  |t4         t        j6                  |      z  k\        rn:|| z  }t        jJ                  |      }1t        jJ                  |      }2t        j                  ||z
  d      }3t        j                  ||z
  d      }4||   dz  |3|   |3|   d|-|   z  z
  z  z
  |1|<   ||   dz  |4|   |4|   d|-|   z  z   z  z
  |2|<   t        jF                  |1|1dkD           ||1dkD     z
  |1|1dkD  <   t        jF                  |2|2dkD           ||2dkD     z   |2|2dkD  <   |1t4        |3z  kD  }|2t4        |4z  kD  }t        jL                  |      }5t        jL                  |      }6t        j                  |5|   |3|   |1|   z        |5|<   t        j                  |6|   |4|   |2|   z        |6|<   t        j:                  |5      }7t        j:                  |6      }8t;        |7|8      }9t        j$                  |      }:||-z  };||z  }%|%|   dz  ||   ||   d|;|   z  z   z  z
  |:|<   t        jF                  |:|:dkD           |%|:dkD     z   |:|:dkD  <   |:t4        |z  kD  }&t        jN                  |      }<t        j                  |<|&   ||&   |:|&   z        |<|&<   t        j:                  |<d      }=t;        |9|=      }> ||-      }? ||      }|-|?z  }@||z  }|-|z  }Ad}BtQ        |Bdz
  |>z  dz         }Bt        jR                  |>|Bz  |>|B      }Cd|Cz  d|Cdz  z   z  }D|D|0|Cz  |z
  |Dd
|Cdz  z  |@z  d|Cz  |Az  z
  d
|z  z   z  z
  z  }Et        j                  |Edk        rnt        jT                  E      }FdC|F   dz  z
  d|C|F   dz  z   z  }Gt        j>                  ||Gdz
  |-z  z   D|F   |z  z   ||      }| |Gdz
  |?z  |D|F   |z  z   z  } t        j                  d||Gdz
  |;z  z
  |D|F   |%z  z
        }||E|F   z  }|>dk  rjFBdz
  k(  rb|7|>k  rtC        |5      }+||+   ||+<   d||+<   |8|>k  rtC        |6      }+||+   ||+<   d||+<   |=|>k  rtC        |<      }+d||+<   t#        |||||      \  }}nn||k  r$||z  d
|z   ||      z  z   ||,z  d
|,z   ||,      z  z   kD  r|,}|rt        ||      }
t        j                  ||k        sJ t        j                  ||k        sJ t        j                  ||z  ||
z   k        sJ t        j                  t        j6                  ||z        |
k        sJ t        j,                  j/                  |      d|z  k  sJ |S # t2        $ r Y Mw xY w)af
  
    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"bjj)bhh$**.DDD"bjj)bhh$**.DDDsBJJ'A		!		)	
*
 sBJJ'ACIIaL(	
)
 sBJJ'A		!		)	
* %'''%&&&R$vvbCi   vvbSDj!!!vvccTk"""{{5!eck11	B	B	B	B
**S#
C 			A774=DI CxD3J'GCxD3J'GSyS4Z#-.G*3Wgw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!!!vvcDjC#I-...vvbffS4Z(C/000yy~~d#cEk111Ke ! 		s   =w 	wwc           	         |rt        | t        j                        r| j                  dk(  sJ t        |t        j                        r+|j                  dk(  r|j                  | j
                  d   k(  sJ t        |t        j                        r.|j                  dk(  r|j
                  d   | j
                  d   k(  sJ t        |t        j                        r+|j                  dk(  r|j                  |j
                  d   k(  sJ t        |t        j                        r|j
                  | j
                  d   fk(  sJ t        |t        j                        r|j
                  | j
                  d   fk(  sJ t        |t              sJ t        |t              sJ t        ||      }	t        j                  ||	k        sJ t        j                  ||	 k\        sJ t        j                  |      r|dkD  sJ t        j                  |d      }t        j                  |d      }| j
                  \  }
}t        j                  |j                  | z  t        j                  d|       f   }|dk  |d| dk  z  }|dkD  |d| dkD  z  }|dk  }|dkD  | |d| z  ||d z
  dkD  z  }t        | ||||      \  }}t        j                   ||z  ||d ||d z  z         }t        j"                  |      }|dd|df    |dd|df   j                  |z  z  }|||d z   }d}d}d}|||
z   |z
  k  r<||z  }|dt$        z  |z  t'        dt        j(                  j+                  |            z  k\  rn	 t-        ||d| |      }	 t3        |t-        ||d ||d |            }| |z  d	|z  k  rnt        j                  |j                  ||d| z  z  ||d f   }||z  }|t4        t7        |      z  kD  rt'        | |z  d      }nt        j0                  }t3        ||      }| |d
|z  |z  z   z  d	|z  k  rn6||t        j0                   kD  z  |d| t4         t        j6                  ||z
        z  k  z  } ||t        j0                  k  z  |d| t4        t        j6                  ||z
        z  kD  z  }!|||d t4         t        j6                  ||d       z  k  z  }"t        j8                  |t        j0                        }#t        j8                  |t        j0                        }$t        j8                  |t        j0                        }%t        j                  ||    ||    z
  |d| |    z  d      |#| <   t        j                  ||!   ||!   z
  |d| |!   z  d      |$|!<   t        j                  ||d |"    ||d |"   z  d      |%|"<   t        j2                  |#      }&t        j2                  |$      }'t        j2                  |%t        j0                        }(t3        |&|'|(      })| |d| z  ||d z
  }*||*t4        t        j6                  |      z  kD  z  }+t        j8                  |t        j0                        },||+   |*|+   z  |,|+<   t        j2                  |,t        j0                        }-t3        ||)|-      }|dkD  rUt        j:                  |||d| z  z   ||      }|||z  z  }t        j                  d|||*z  z
        }|||d
|z  |z  z   z  z  }|t3        ||)|-      k  r;|dd|df   |dd|df   j                  |z  z  }.|.|z  |z  }/|/|z  |.z
  }|dz  }n||k  r|&|k  r#t        j<                  |#      }0||0   ||0<   d||0<   nb|'|k  r#t        j<                  |$      }0||0   ||0<   d||0<   n:|(|k  rt        j<                  |%      }0d||0<   nt        j<                  |,      }0d||0<   t        | ||||      \  }}|dd|df    |dd|df   j                  |z  z  }d}n=|&|k  rt?        |#      }0||0   ||0<   d||0<   |'|k  rt?        |$      }0||0   ||0<   d||0<   d}n|||
z   |z
  k  r<|jA                  dd      ro|rlt        jB                  |      }1||z  }2| j                  t        j                  | |z  |z
  d      z  |j                  ||z  |z
  z  z   }t        j"                  |      }t        jD                  |2      dkD  r||2   ||2   z  }3||2   ||2   z  }4||2   ||2   z  }5t        j                   t'        |3|4z  |5dz  z
  d             }|5||2   z  |3||2   z  z
  ||2<   d||2 <   |d|z  k\  s6t        jF                  |t4         t        j6                  ||2         z  k\        rn||2xx   | z  cc<   t        j"                  |      }6t        j"                  |      }7||2   dz  ||2   dz  z   ||2   dz  z
  |6|2<   ||2   dz  ||2   dz  z   ||2   dz  z
  |7|2<   t        j                   |6|6dkD           ||6dkD     z
  |6|6dkD  <   t        j                   |7|7dkD           ||7dkD     z   |7|7dkD  <   t        j                  ||z
  d      }8t        j                  ||z
  d      }9|6t4        |8z  kD  } |7t4        |9z  kD  }!t        jH                  |      }:t        jH                  |      };t        j                  |:|    |8|    |6|    z        |:| <   t        j                  |;|!   |9|!   |7|!   z        |;|!<   t        j2                  |:      }<t        j2                  |;      }=t3        |<|=      }>d}?tK        |?dz
  |>z  dz         }?t        jL                  |>|?z  |>|?      }@t        j                  | |z  |z
  d      }A||z  |z
  }Bt        jB                  |      }Cd|C|2 <   t        jN                  |?      }DtQ        |?      D ]}  }Ed@|E   z  d|@|E   dz  z   z  }Ft        j:                  ||F||@|E   Cz  z
  z  z   ||      }Gt        j                  | |Gz  |z
  d      }H||Gz  |z
  }Id
A|Az  B|Bz  z   |H|Hz  z
  |I|Iz  z
  z  D|E<    t        j                  Ddk        rnt        jR                  D      }Jd@|J   dz  z
  d|@|J   dz  z   z  }Kd|@|J   z  d|@|J   dz  z   z  }F|K||2   z  |F||2   z  z   ||2<   | j                  t        j                  | |z  |z
  d      z  |j                  ||z  |z
  z  z   }||D|J   z  }|>dk  rCJ|?dz
  k(  r;|<|>k  rt?        |:      }0||0   ||0<   d|2|0<   |=|>k  rt?        |;      }0||0   ||0<   d|2|0<   nnt        jD                  |2      dkD  rt        j                  | |z  |z
  d      }At        j                  | |1z  |z
  d      }L||z  |z
  }B||1z  |z
  }M|A|Az  |B|Bz  z   |L|Lz  |M|Mz  z   kD  r|1}|r[t        j                  ||k        sJ t        j                  ||k        sJ t        j(                  j+                  |      d|z  k  sJ |S # t.        $ r t        j0                  }Y 
w xY w# t.        $ r Y 
w xY w)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     sT   F #rzz*sxx1}<<sBJJ'ACIIaL(	
)
 sBJJ'A		!		!,	
-
 sBJJ'ACIIaL(	
) "bjj)bhh399Q</.III"bjj)bhh399Q</.III%'''%&&&R$vvbCi   vvbSDj!!!{{5!eck11	B	B	B	B YYNK55#rzz#t445DCxD!HsN+GCxD!HsN+GsJSyS48^d12h6<=G&HE1 ''#)d12hab&99:K 88A;D
AuvI,!AuvI,..4/	0B$qr(NE	AF
a+o%
%)eckAoC1E(FFF	 r"1vu5H	8YtABx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$QR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"b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!		,/!&.Wgz7HE1 AuvI,!AuvI,..4"78BA
 5 - iU!&5 - iU!&#q a+o%
%v zz-&+;GGDM	G#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$   0y y, y)(y),	y98y9c                 D   |j                   }t        j                  |      }t        t        j                  |g| | d d f   g|| d d f    g|| d d f   gg      j
                  d      \  }}}	t        j                  t        j                  t        j                  |            dt        z  |z  t        j                  j                  |d t        j                  |j                        d t        j                  |j                        f   d      z  k\        }
|
|fS 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
gXq[!"G8Q;''(7(A+&'		
 !
GAq! 
rwwqz

 ))..,RVVAGG_,.?qww.??@q.
IJ	JE !8O    c                    | j                   \  }}t        j                  |      }t        j                  |      }t        t        j                  | | d d f   || d d f    gt        j
                  |t        j                  |      z
  |f      || d d f    g|| d d f    t        j
                  |t        j                  |      z
  |f      g|| d d f   t        j
                  |t        j                  |      z
  |f      gg      j                  d      \  }	}
}t        j                  t        j                  t        j                  |
            dt        z  ||z   z  t        j                  j                  |
d t        j                  |
j                         d t        j                  |
j                         f   d      z  k\        }||	fS 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	
& !+GAq!. 
rwwqz
{? ))..,RVVAGG_,.?qww.??@q.
IJ	JE !8Or   c                 B   | |z  }||z  }|dz  | | z  z
  }t        j                  t        |dz  ||z  z   d            }|dk  r,|t        t	        ||z
        z  kD  rt        ||z
  |z  d      }|S t	        ||z         t        |z  kD  rt        |||z   z  d      }|S t
        )Nr   r   )r   r4   r&   r+   r,   r*   )rF   rG   r?   step_sdsd_sq
dist_tr_sqtemprL   s           rq   r)   r)     s    RiGGEdTk)J773w|ej&88#>?D#~%$TG^)<"<<w%/5
 O	 
TG^	tj0	0zTG^4c: O  r   c                 X    t        j                  | t        j                  |       k\        S N)r   flatnonzeror&   xs    rq   _argmaxr         >>!rvvay.))r   c                 X    t        j                  | t        j                  |       k        S 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      sl       " rxxbhhuosl	`Fzz0"J**r   