
    (ph]7                     j    S SK r S SKrSSKJr  \R
                  " \5      R                  rS r	S r
S rg)    N   )get_arrays_tolc           	        ^ U(       Ga  [        U [        5      (       d   e[        U[        R                  5      (       a  UR                  S:X  d   e[
        R                  " T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[        X45      n[        R                  " X7:*  5      (       d   e[        R                  " XG* :  5      (       d   e[        R                  " U5      (       a  US:  d   e[        R                  " US5      n[        R                  " US5      n[        XTX4XV5      u  p[        U * U* U4S jUUUU5      u  p[!        U	5      [!        U5      :  a  UOU
nU(       af  [        R                  " X<:*  5      (       d   e[        R                  " X:*  5      (       d   e[        R"                  R%                  U5      SU-  :  d   eU$ )ao  
Maximize approximately the absolute value of a quadratic function subject
to bound constraints in a trust region.

This function solves approximately

.. math::

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

by maximizing the objective function along the constrained Cauchy
direction.

Parameters
----------
const : float
    Constant :math:`c` as shown above.
grad : `numpy.ndarray`, shape (n,)
    Gradient :math:`g` as shown above.
curv : callable
    Curvature of :math:`H` along any vector.

        ``curv(s) -> float``

    returns :math:`s^{\mathsf{T}} 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`.

Notes
-----
This function is described as the first alternative in Section 6.5 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.
           c                    > T" U 5      * $ )N )xcurvs    X/var/www/html/venv/lib/python3.13/site-packages/scipy/_lib/cobyqa/subsolvers/geometry.py<lambda>!cauchy_geometry.<locals>.<lambda>[   s    47(    皙?)
isinstancefloatnpndarrayndiminspect	signaturebindshapeboolr   allisfiniteminimummaximum_cauchy_geomabslinalgnorm)constgradr   xlxudeltadebugtolstep1q_val1step2q_val2steps     `          r   cauchy_geometryr/      s   t %''''$

++		Q>>  &++D1111"bjj))bhh$**.DDD"bjj))bhh$**.DDD%''''%&&&&R$vvbi    vvbDj!!!!{{5!!eck11	B	B	B	B
 !dBEIME 		

ME K3v;.5EDvvbj!!!!vvdj!!!!yy~~d#cEk111Kr   c                    U(       Ga  [        U [        5      (       d   e[        U[        R                  5      (       a  UR                  S:X  d   e[
        R                  " U5      R                  U5      (       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                  UR                  :X  d   e[        U[        R                  5      (       a  UR                  UR                  :X  d   e[        U[        5      (       d   e[        U[        5      (       d   e[        XE5      n[        R                  " XH:*  5      (       d   e[        R                  " XX* :  5      (       d   e[        R                  " U5      (       a  US:  d   e[        R                  " US5      n[        R                  " US5      n[        R                   " U5      n	U n
[        R"                  R%                  USS9nU[        R&                  * :  UR(                  [*        * U-  :  -  nU[        R&                  * :  UR(                  [*        U-  :  -  nU[        R&                  :  UR(                  [*        U-  :  -  nU[        R&                  :  UR(                  [*        * U-  :  -  n[        R,                  " [        R.                  " XLR                  5      U   UR(                  U   -  5      n[        R0                  " US[        R&                  * S9n[        R.                  " [        R2                  " U5      UR                  S   5      n[        R,                  " [        R.                  " XMR                  5      U   UR(                  U   -  5      n[        R0                  " US[        R&                  S9n[        R.                  " [        R2                  " U5      UR                  S   5      n[        R,                  " [        R.                  " X_R                  5      U   UR(                  U   -  5      n[        R0                  " US[        R&                  * S9n[        R.                  " [        R2                  " U5      UR                  S   5      n[        R,                  " [        R.                  " X^R                  5      U   UR(                  U   -  5      n[        R0                  " US[        R&                  S9n[        R.                  " [        R2                  " U5      UR                  S   5      n[5        UR                  S   5       GHg  nUU   [*        U-  :  a  [1        XkU   -  S5      nOM(  [1        [7        UU   UU   5      S5      n[7        [1        UU   UU   5      S5      nXSS2U4   -  nU" USS2U4   5      nUS:  a  U[*        * U-  :  d  US::  a  U[*        * U-  :  a  [1        U* U-  S5      nO[        R&                  nUS:  a  U[*        U-  :  d  US::  a  U[*        U-  :  a  [7        U* U-  S5      nO[        R&                  * n[7        UU5      n[1        U* U5      nU UU-  -   SUS	-  -  U-  -   nU UU-  -   SUS	-  -  U-  -   nUU:  a0  U UU-  -   SUS	-  -  U-  -   n [9        U 5      [9        U5      :  a  UnU nUU:  a0  U UU-  -   SUS	-  -  U-  -   n![9        U!5      [9        U5      :  a  UnU!n[9        U5      [9        U5      :  a>  [9        U5      [9        U
5      :  a&  [        R:                  " UUSS2U4   -  XE5      n	Un
GM  [9        U5      [9        U5      :  d  GM)  [9        U5      [9        U
5      :  d  GMD  [        R:                  " UUSS2U4   -  XE5      n	Un
GMj     U(       af  [        R                  " XI:*  5      (       d   e[        R                  " X:*  5      (       d   e[        R"                  R%                  U	5      S
U-  :  d   eU	$ )a  
Maximize approximately the absolute value of a quadratic function subject
to bound constraints in a trust region.

This function solves approximately

.. math::

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

by maximizing the objective function along given straight lines.

Parameters
----------
const : float
    Constant :math:`c` as shown above.
grad : `numpy.ndarray`, shape (n,)
    Gradient :math:`g` as shown above.
curv : callable
    Curvature of :math:`H` along any vector.

        ``curv(s) -> float``

    returns :math:`s^{\mathsf{T}} H s`.
xpt : `numpy.ndarray`, shape (n, npt)
    Points defining the straight lines. The straight lines considered are
    the ones passing through the origin and the points in `xpt`.
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`.

Notes
-----
This function is described as the second alternative in Section 6.5 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   )axis)r1   initialN      ?       @r   )r   r   r   r   r   r   r   r   r   sizer   r   r   r   r   r   
zeros_liker!   r"   infTTINY
atleast_2dbroadcast_tomax
atleast_1drangeminr    clip)"r#   r$   r   xptr%   r&   r'   r(   r)   r.   q_vals_normi_xl_posi_xl_negi_xu_posi_xu_negalpha_xl_posalpha_xl_negalpha_xu_negalpha_xu_poskalpha_tralpha_bd_posalpha_bd_neg	grad_step	curv_stepalpha_quad_posalpha_quad_neg	alpha_pos	alpha_neg	q_val_pos	q_val_negq_val_quad_posq_val_quad_negs"                                     r   spider_geometryrZ   j   s   x %''''$

++		Q>>  &++D1111sBJJ''A		!		)	
* "bjj))bhh$**.DDD"bjj))bhh$**.DDD%''''%&&&&R$vvbi    vvbDj!!!!{{5!!eck11	B	B	B	B ==DEYY^^Ca^(F bffW$!34HbffW!23HRVVr	 12HRVV
 23H ==
NN+H5hGL 66,Q@L??2==#>		!ML==
NN+H5hGL 66,Q?L??2==#>		!ML==
NN+H5hGL 66,Q@L??2==#>		!ML==
NN+H5hGL 66,Q?L??2==#>		!ML399Q< !9te|#5!9,c2H 3|AQ@#F3|AQ@#F q!t9$	QTO	TEI--CTEI-- )i!7=NVVND9,,CD9,, )i!7=N ffWN ,/		<0	I	))C)S.,@9,LL 	 I	))C)S.,@9,LL 	 I% 9,-++i78 
 >"S^3*	*	I% 9,-++i78 
 >"S^3*	*	y>S^+IU0K779s1a4y0"9DE^c)n,Y#e*1L779s1a4y0"9DEU !X vvbj!!!!vvdj!!!!yy~~d#cEk111Kr   c                 8   US:  US:  -  nUS:  US:  -  n[         R                  " U5      n	X7   X'   XH   X'   [         R                  R                  U	5      U:  a  Xx-  n
 [         R                  R                  X   5      n[         R                  " US-  X)    X)    -  -
  5      nU[
        [        U5      -  :  a  [        X-  S5      nOOdXU
   -  X'   XU:  -  nXU:  -  n[         R                  " U5      (       d  [         R                  " U5      (       d  OX7   X'   XH   X'   XU-  ) -  n
M  X-  nUS:  GaU  [         R                  R                  U	5      nU[
        U-  :  a  [        X_-  S5      nOSnU" U	5      nU[
        * U-  :  a  [        U* U-  S5      nO[         R                  nU[         R                  * :  U	[
        U-  :  -  nU[         R                  :  U	[
        U-  :  -  n[         R                  " UU   U	U   -  [         R                  S9n[         R                  " UU   U	U   -  [         R                  S9n[        UU5      n[        UUU5      n[         R                  " UU	-  X45      nU UU-  -   SUS-  -  U-  -   nO[         R                  " U5      nU nU(       ah  [         R                  " UU:*  5      (       d   e[         R                  " UU:*  5      (       d   e[         R                  R                  U5      SU-  :  d   eUU4$ )zE
Same as `bound_constrained_cauchy_step` without the absolute value.
r   r4   )r2   r3   r   )r   r6   r!   r"   sqrtr9   r    r<   anyr7   r?   r@   r   )r#   r$   r   r%   r&   r'   r(   fixed_xlfixed_xucauchy_stepworkingg_normdelta_reducedmurP   rC   rM   rQ   
alpha_quadi_xli_xualpha_xlalpha_xualpha_bdalphar.   rB   s                              r   r   r   8  s   
 STCZ(HSTCZ(H --%KLKLK	yy~~k"U*%YY^^DM2FGGs
[2[5JJJM s=111/5#%W#5K  "23H"23H66(##BFF8,<,<$&LK!$&LK!X"5 66G) . "IC,D5L 5>3/H H %	uy((iZ)3S9JJ bffWtby!89RVVdRi 7866"T([%66G66"T([%66Gx* Hj(3wwu{*B3	))C%*,<y,HH }}T"vvbDj!!!!vvdbj!!!!yy~~d#cEk111;r   )r   numpyr   utilsr   finfor   tinyr9   r/   rZ   r   r	   r   r   <module>rp      s7      " 
xx\~K\Kr   