
    ^Mhȱ                         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dS )    N)qr   )get_arrays_tolc                    |r7t          | t          j                  r| j        dk    sJ t	          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k    sJ t          j        |d          }t          j        |d          }| j        }t          j        |           } t          j        |           }	|dk     | dk     z  |dk    | dk    z  z  }
t          j        |           }t          j        |          }| |
          ||
<   d}d}d}|t          j        |
          k     r| |z  }|dt&          z  |z  t)          dt          j                            |                     z  k    rnc	 t/          |||          }n# t0          $ r Y nCw xY w| |z  d|z  k    rn0 ||          }||z  }|t2          t5          |          z  k    rt)          | |z  d          }nt          j        }t9          ||          }| |d|z  |z  z   z  d|z  k    rn|t          j         k    |t2           t          j        ||z
            z  k     z  }|t          j        k     |t2          t          j        ||z
            z  k    z  }t          j        |t          j                  }t          j        |t          j                  }t          j        ||         ||         z
  ||         z  d          ||<   t          j        ||         ||         z
  ||         z  d          ||<   t          j        |          }t          j        |          }t9          ||          }t9          ||          }|dk    rPt          j        ||
         |||
         z  z   ||
         ||
                   ||
<   | ||z  z  } |||d|z  |z  z   z  z  }|t9          ||          k     r7| |
         ||
         z  |z  }|||
         z  | |
         z
  ||
<   d||
 <   |dz  }n||k     r_||k    r t          j        |          }||         ||<   nt          j        |          }||         ||<   d|
|<   | |
          ||
<   d||
 <   d}nM||k    rtA          |          }||         ||<   d|
|<   ||k    rtA          |          }||         ||<   d|
|<   d	}n|t          j        |
          k     |!                    d
d	          r|rt          j        |          }|	|z  d|z   ||          z  z   } t          j        |
          dk    r;||
         ||
         z  }!| |
         | |
         z  }"| |
         ||
         z  }#t          j"        t)          |!|"z  |#dz  z
  d                     }|#||
         z  |!| |
         z  z
  ||
<   d||
 <   |d|z  k    s9t          j#        |t2           t          j        ||
                   z  k              rnx||
xx         | z  cc<   t          j$        |          }$t          j$        |          }%||
         dz  ||
         dz  z   ||
         dz  z
  |$|
<   ||
         dz  ||
         dz  z   ||
         dz  z
  |%|
<   t          j"        |$|$dk                       ||$dk             z
  |$|$dk    <   t          j"        |%|%dk                       ||%dk             z   |%|%dk    <   t          j        ||z
  d          }&t          j        ||z
  d          }'|$t2          |&z  k    }|%t2          |'z  k    }t          j%        |          }(t          j%        |          })t          j        |(|         |&|         |$|         z            |(|<   t          j        |)|         |'|         |%|         z            |)|<   t          j        |(          }*t          j        |)          }+t9          |*|+          }, ||          }- ||          }||-z  }.||z  }||z  }/d}0tM          |0dz
  |,z  dz             }0t          j'        |,|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          j(        |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     rT|4|0dz
  k    rK|*|,k    rtA          |(          }||         ||<   d|
|<   |+|,k    rtA          |)          }||         ||<   d|
|<   nnt          j        |
          dk    ;|	|z  d|z   ||          z  z   | k    r|}|r\t          j        ||k              sJ t          j        ||k              sJ t          j                            |          d|z  k     sJ |S )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                                                         b/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/scipy/_lib/cobyqa/subsolvers/optim.pytangential_byrd_omojokunrr      s   @  
2$
++>	Q ++0066666"bj))Dbh$*.D.D.D.D"bj))Dbh$*.D.D.D.D%'''''%&&&&&R$$vbCi     vbSDj!!!!!{5!!1eckkkk	B		B	B		B 		A74==DI STCZ(b3h4#:-FGG =D	t		B=.BwK	AF
bw''
'
')eckAoC1E1E(F(FFFF	 r511HH  	 	 	E	
 9w$-// )B--w,TCLL(((gX/55JJJ Hj))6WsU{W445FF bfWtebfR$Y.?.?&?!?@RVTBF29,=,=%= =>|D"&11|D"&11ZXT
"bh.
 
T  ZXT
"bh.
 
T 6,''6,''x** E8$$3;;GW7 3377 DM
 EGO#Dewuw)>>??F3x**** MGG$44?DG,tG}<BwKBxLFAAX 5  	,// iU	,// iU"GEN=.BwKBxLAA
 5  -- iU!&5  -- iU!&#{ bw''
'
'@ zz-&& o+; oGDMM	#i/#	/IIE
 E
 3
 
 w''!++ 7md7m3G7md7m3GWW5Iws7W#4y#~#EsKKLLLG#d7m3gW6MMBwKBxL%&.((BFD526"W+#6#666- -( wKKKG8#KKK
 hqkkGhqkkGW$r'{c'99BwK3<NN G W$r'{c'99BwK3<NN G #.//"Ws]2CC GcM" #.//"Ws]2CC GcM" jC00GjdC00GTG^+DTG^+DwqzzHwqzzHZ- HTN  Z- HTN 6(##D6(##DtT??D "	$IimmGy(I7lG'>L
 IY]d2Q677ID9$4dIFFIyC)S.,@AJ#I%i'( |+cWy5H.IIKKJ vjC'((  Ij))Ey/366i&#--I DM)Ju,=7,KK M Y_	1Ju4E4OOODj''F
 czzey1}444<<#H--E"$U)DK%*GEN4<<#H--E"$U)DK%*GENI w''!++P tcDj99T??::_LLD 2vbDj!!!!!vdbj!!!!!y~~d##cEk1111Ks   	I 
I)(I)c	                 8   |r	t          | t          j                  r| j        dk    sJ t	          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k    sJ t          j        |d          }t          j        |d          }t          j        |d          }| j        }t          j        |           } t          j        |           }|dk     | dk     z  }|dk    | dk    z  }|dk    || z  dk    z  }t#          |||||          \  }}t          j        |           }|dd|df          |dd|df         j        | z  z  }t          j        |          }d}d}d}|||z
  k     r| |z  }|dt(          z  |z  t+          dt          j                            |                     z  k    rn[	 t1          |||          }n# t2          $ r Y n;w xY w| |z  d	|z  k    rn( ||          }||z  }|t4          t7          |          z  k    rt+          | |z  d          }nt          j        }t;          ||          }| |d
|z  |z  z   z  d	|z  k    rn||t          j         k    z  |t4           t          j        ||z
            z  k     z  }||t          j        k     z  |t4          t          j        ||z
            z  k    z  }t          j        |t          j                  } t          j        |t          j                  }!t          j        ||         ||         z
  ||         z  d          | |<   t          j        ||         ||         z
  ||         z  d          |!|<   t          j        |           }"t          j        |!          }#t;          |"|#          }$||z  }%||%t4          t          j        |          z  k    z  }&t          j        |t          j                  }'||&         |%|&         z  |'|&<   t          j        |'t          j                  }(t;          ||$|(          }|dk    rPt          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  }n1||k     r|"|k    r%t          j         |           }+||+         ||+<   d||+<   nD|#|k    r%t          j         |!          }+||+         ||+<   d||+<   nt          j         |'          }+d||+<   t#          |||||          \  }}|dd|df          |dd|df         j        | z  z  }d}n}|"|k    rtC          |           }+||+         ||+<   d||+<   |#|k    rtC          |!          }+||+         ||+<   d||+<   |(|k    rtC          |'          }+d||+<   t#          |||||          \  }}d}n
|||z
  k     |	"                    dd          r|r	||k     rt          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          j#        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          j$        |t4           t          j        |          z  k              rn|| z  }t          j%        |          }1t          j%        |          }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          j#        |1|1dk                       ||1dk             z
  |1|1dk    <   t          j#        |2|2dk                       ||2dk             z   |2|2dk    <   |1t4          |3z  k    }|2t4          |4z  k    }t          j&        |          }5t          j&        |          }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          j#        |:|:dk                       |%|:dk             z   |:|:dk    <   |:t4          |z  k    }&t          j'        |          }<t          j        |<|&         ||&         |:|&         z            |<|&<   t          j        |<d          }=t;          |9|=          }> ||-          }? ||          }|-|?z  }@||z  }|-|z  }Ad}BtQ          |Bdz
  |>z  dz             }Bt          j)        |>|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          j*        |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     r|F|Bdz
  k    r{|7|>k    rtC          |5          }+||+         ||+<   d||+<   |8|>k    rtC          |6          }+||+         ||+<   d||+<   |=|>k    rtC          |<          }+d||+<   t#          |||||          \  }}nn||k     ||z  d
|z   ||          z  z   ||,z  d
|,z   ||,          z  z   k    r|,}|rt          ||          }
t          j        ||k              sJ t          j        ||k              sJ t          j        ||z  ||
z   k              sJ t          j        t          j        ||z            |
k              sJ t          j                            |          d|z  k     sJ |S )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  sT   f  2$
++>	Q ++0066666"bj))Dbh$*.D.D.D.D"bj))Dbh$*.D.D.D.DsBJ''	
A	!	)))) sBJ''	
ACIaL(((( sBJ''	
A	!	))))%'''''%&&&&&R$$vbCi     vbSDj!!!!!vccTk"""""{5!!1eckkkk	B		B	B		B
*S#

C 		A74==DI CxD3J'GCxD3J'GSyS4Z#-.G*3WgwOOHE1 =D
AAAuvvI,!AAAuvvI,.4/	0BGCLLE	AF
a%i--)eckAoC1E1E(F(FFFF	 r511HH  	 	 	E	
 9w$-// )B--w,TCLL(((gX/55JJJ Hj))6WsU{W445FF "w,'2rDy8I8I0I+IJ"rv+&"tbfR$Y6G6G/G*GH|D"&11|D"&11ZXT
"bh.
 
T  ZXT
"bh.
 
T 6,''6,''x** r&4"&--#778|C00"4[6$<7T6,777 E8X..3;;74%"*,b"55DEGO#DJsEEFN$:;;Eewuw)>>??F3x84444 !!!UVV)!!!UVV)(=>I'72DY&BFAAX 5  	,// iU!&U""	,// iU!&	,//!&2 HE1 AAAuvvI,!AAAuvvI,.4"78BAA
 5  -- iU!&5  -- iU!&5  --!&2 HE1  $k a%i--p zz-&& Z+; Z		GDMM	aii !!!UVV)!!!UVV)(=>I!!!UVV)!!!UVV)(=>I)+G)+G!I-Iws7W#4y#~#EsKKLLLG111eff9!!!UVV))d"2Wt^"CDB %&.((BFD526"::--- -( 7(NB hqkkGhqkkGjC00GjdC0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wqzzHwqzzHZ- HTN  Z- HTN 6(##D6(##DtT??D mE**GYH2XF%g#5ggx'8!889  GG #.//&32GG GcM" TE\)D|E**HZdgdm+ HTN 6(C000DdOOE "	),,IimmG!I-I7lG$w.L
 IY]e3a788IEI$5uiHHIyC)S.,@AJ#I%)S.(94Io45Gm$	J vjC'((  Ij))Ey/366i&#--I 7	C944z%7H27MM D
 Y_	1Ju4E4OOODJs?h./U#f,- E j''F
 s{{u	A555==#H--E"$U)DK%*GEN5==#H--E"$U)DK%*GEN5==#H--E%*GEN6 qq c aiij tcDj99,
 ,
 
 
	!C)Oii	6J6J$JJK K D 2R$$vbDj!!!!!vdbj!!!!!vcDjC#I-.....vbfS4Z((C/00000y~~d##cEk1111Ks    M2 2
N ?N 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k    sJ t          j        |d          }t          j        |d          }| j        \  }
}t          j        |j        | z  t          j        d|           f         }|dk     |d|         dk     z  }|dk    |d|         dk    z  }|dk     }|dk    | |d|         z  ||d         z
  dk    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                            |                    z  k    rn	 t-          ||d|         |          }n# t.          $ r t          j        }Y nw xY w	 t3          |t-          ||d         ||d         |                    }n# t.          $ r Y nw xY w| |z  d	|z  k    rn&t          j        |j        ||d|         z  z  ||d         f         }||z  }|t4          t7          |          z  k    rt'          | |z  d          }nt          j        }t3          ||          }| |d
|z  |z  z   z  d	|z  k    rn||t          j         k    z  |d|         t4           t          j        ||z
            z  k     z  } ||t          j        k     z  |d|         t4          t          j        ||z
            z  k    z  }!|||d         t4           t          j        ||d                   z  k     z  }"t          j        |t          j                  }#t          j        |t          j                  }$t          j        |t          j                  }%t          j        ||          ||          z
  |d|         |          z  d          |#| <   t          j        ||!         ||!         z
  |d|         |!         z  d          |$|!<   t          j        ||d         |"          ||d         |"         z  d          |%|"<   t          j        |#          }&t          j        |$          }'t          j        |%t          j                  }(t3          |&|'|(          })| |d|         z  ||d         z
  }*||*t4          t          j        |          z  k    z  }+t          j        |t          j                  },||+         |*|+         z  |,|+<   t          j        |,t          j                  }-t3          ||)|-          }|dk    rXt          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<   nd|'|k    r%t          j        |$          }0||0         ||0<   d||0<   n9|(|k    rt          j        |%          }0d||0<   nt          j        |,          }0d||0<   t          | ||||          \  }}|dd|df          |dd|df         j        |z  z  }d}nM|&|k    rt?          |#          }0||0         ||0<   d||0<   |'|k    rt?          |$          }0||0         ||0<   d||0<   d}n|||
z   |z
  k     |                     dd          r|rt          j!        |          }1||z  }2| j        t          j        | |z  |z
  d          z  |j        ||z  |z
  z  z   }t          j        |          }t          j"        |2          dk    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    s9t          j#        |t4           t          j        ||2                   z  k              rn9||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k                       ||6dk             z
  |6|6dk    <   t          j        |7|7dk                       ||7dk             z   |7|7dk    <   t          j        ||z
  d          }8t          j        ||z
  d          }9|6t4          |8z  k    } |7t4          |9z  k    }!t          j$        |          }:t          j$        |          };t          j        |:|          |8|          |6|          z            |:| <   t          j        |;|!         |9|!         |7|!         z            |;|!<   t          j        |:          }<t          j        |;          }=t3          |<|=          }>d}?tK          |?dz
  |>z  dz             }?t          j&        |>|?z  |>|?          }@t          j        | |z  |z
  d          }A||z  |z
  }Bt          j!        |          }Cd|C|2 <   t          j'        |?          }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nt          j)        |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     rT|J|?dz
  k    rK|<|>k    rt?          |:          }0||0         ||0<   d|2|0<   |=|>k    rt?          |;          }0||0         ||0<   d|2|0<   nnt          j"        |2          dk    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   k    r|1}|r\t          j	        ||k              sJ t          j	        ||k              sJ t          j                            |          d|z  k     sJ |S )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  2#rz**<sx1}}}}sBJ''	
ACIaL(((( sBJ''	
A	!	!,,,, sBJ''	
ACIaL(((("bj))Ibh39Q</.I.I.I.I"bj))Ibh39Q</.I.I.I.I%'''''%&&&&&R$$vbCi     vbSDj!!!!!{5!!1eckkkk	B		B	B		B YNK5#rz#t4445DCxD!HsN+GCxD!HsN+GsJSyS48^d122h6<=G& HE1 '#)d122habb&99::K 8A;;D
AAAuvvI,!AAAuvvI,.4/	0B$qrr(NE	AF
a+o%
%
%)eckAoC1E1E(F(FFFF	 r"1"vu55HH  	 	 	vHHH	
	8YtABBxABB%M%MNNHH  	 	 	D	
 9w$-// %r"1"v.12267w,TCLL(((gX/55JJJ Hj))6WsU{W445FF "w,'2bqb6TEBF29<M<M4M+MN"rv+&"RaR&4"&d:K:K3K*KL122$QRR1A1A)A AB|D"&11|D"&11,sBF33ZXT
"b!fTl2
 
T  ZXT
"b!fTl2
 
T $&:!""XgABB0$
 $
  6,''6,''f_bf===x;77 r"1"v122&&4"&--#778|C00"4[6$<7T6,777 E8X..3;;74%"RaR&.0"b99DEGO#DJsEEFN$:;;Eewuw)>>??F3x84444 !!!UVV)!!!UVV)(=>I'72DY&BFAAX 5  	,// iU!&U""	,// iU!&%%	/22$)
5!!	,//!&.Wgz7 HE1 AAAuvvI,!AAAuvvI,.4"78BAA
 5  -- iU!&5  -- iU!&#q a+o%
%
%v zz-&& ~+; ~GDMM	G#urz#*s"2C88835$J<
 
 Xa[[w''!++ 7md7m3G7md7m3GWW5Iws7W#4y#~#EsKKLLLG#d7m3gW6MMBwKBxL%&.((BFD526"W+#6#666- -( wKKKG8#KKK
 hqkkGhqkkGW$r'{c'99BwK3<NN G W$r'{c'99BwK3<NN G #.//"Ws]2CC GcM" #.//"Ws]2CC GcM" jC00GjdC00GTG^+DTG^+DwqzzHwqzzHZ- HTN  Z- HTN 6(##D6(##DtT??D
 IY]d2Q677ID9$4dIFFIz#*s"2C88HTzC'HI"%Iwh),,J9%%  )A,.#	!8K2KL	79Yq\I-E(EFF 
  "z#.3*>DD"X~3 #x')*"\12 #\12!
1 vjC'((  Ij))Ey/366i&#--I y//)E"2c"99;I%W5	BwK8OODM52:cDj3&6<<<sud
S @ D j''F
 czzey1}444<<#H--E"$U)DK%*GEN4<<#H--E"$U)DK%*GENW w''!++^ :cDj3.44
3?S#8#>>:#i#-x(X"55m+mm.KKL L D 2vbDj!!!!!vdbj!!!!!y~~d##cEk1111Ks$   9N N,+N,0/O   
O-,O-c                 2   |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
                            |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vayyH
gXqqq[!"G8QQQ;''(7(AAA+&'		
 	
 
 
 
GAq! 
rwqzz

 )..,RVAG__,.?qw.??@q.
I
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                            |
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   YNKJ$$J
 !!!$!!!,,
 HkB,<Z,H,HH!LMMQQQ//
  !!!,,Ha""27";";;[IJJ
 x{+Ha""27";";;[IJJ	
 	
& +  GAq!. 
rwqzz
{? )..,RVAG__,.?qw.??@q.
I
IJ	J E !8Or   c                 t   | |z  }||z  }|dz  | | z  z
  }t          j        t          |dz  ||z  z   d                    }|dk    r5|t          t	          ||z
            z  k    rt          ||z
  |z  d          }n<t	          ||z             t          |z  k    rt          |||z   z  d          }nt
          |S )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73w|ej&88#>>??D#~~%$TG^)<)<"<<<w%/55	TG^		tj0	0	0zTG^4c::Or   c                 V    t          j        | t          j        |           k              S N)r   flatnonzeror&   xs    rq   _argmaxr         >!rvayy.)))r   c                 V    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      s               " " " " " " rxbhuoos s sl	` ` `Fz z zz  0" " "J  * * ** * * * *r   