Ë
    âQ(h]7  ã                   ój   — d dl Z d dlZddlmZ  ej
                  e«      j                  Zd„ Z	d„ Z
d„ Zy)é    Né   )Úget_arrays_tolc           	      óL  ‡— |�rNt        | t        «      sJ ‚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«      }t        | |‰||||«      \  }}	t        |  | ˆfd„||||«      \  }
}t!        |	«      t!        |«      k\  r|n|
}|r[t        j                  ||k  «      sJ ‚t        j                  ||k  «      sJ ‚t        j"                  j%                  |«      d|z  k  sJ ‚|S )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 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                 ó   •—  ‰| «       S )N© )ÚxÚcurvs    €úc/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/scipy/_lib/cobyqa/subsolvers/geometry.pyú<lambda>z!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Ü˜"œbŸj™jÔ)¨b¯h©h¸$¿*¹*Ò.DÐDÐDÜ˜"œbŸj™jÔ)¨b¯h©h¸$¿*¹*Ò.DÐDÐDÜ˜%¤Ô'Ð'Ð'Ü˜%¤Ô&Ð&Ð&Ü˜R Ó$ˆÜ�v‰v�b˜C‘iÔ Ð Ð Ü�v‰v�b˜S˜D‘jÔ!Ð!Ð!Ü�{‰{˜5Ô! e¨c¢kÐ1Ð1Ü	�‰�B˜Ó	€BÜ	�‰�B˜Ó	€Bô
 ! ¨¨d°B¸¸EÀ5ÓI�M€Eˆ6Ü Ø	ˆØ	ˆÛØ
Ø
ØØó�M€Eˆ6ô ˜“K¤3 v£;Ò.‰5°E€DáÜ�v‰v�b˜D‘jÔ!Ð!Ð!Ü�v‰v�d˜b‘jÔ!Ð!Ð!Ü�y‰y�~‰~˜dÓ# c¨E¡kÒ1Ð1Ð1Ø€Kr   c                 ó  — |�r•t        | t        «      sJ ‚t        |t        j                  «      r|j                  dk(  sJ ‚t        j                  |«      j                  |«      sJ ‚t        |t        j                  «      r+|j                  dk(  r|j                  d   |j                  k(  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«      }t        j                   |«      }	| }
t        j"                  j%                  |d¬«      }|t        j&                   kD  |j(                  t*         |z  kD  z  }|t        j&                   kD  |j(                  t*        |z  k  z  }|t        j&                  k  |j(                  t*        |z  kD  z  }|t        j&                  k  |j(                  t*         |z  k  z  }t        j,                  t        j.                  ||j                  «      |   |j(                  |   z  «      }t        j0                  |dt        j&                   ¬«      }t        j.                  t        j2                  |«      |j                  d   «      }t        j,                  t        j.                  ||j                  «      |   |j(                  |   z  «      }t        j0                  |dt        j&                  ¬«      }t        j.                  t        j2                  |«      |j                  d   «      }t        j,                  t        j.                  ||j                  «      |   |j(                  |   z  «      }t        j0                  |dt        j&                   ¬«      }t        j.                  t        j2                  |«      |j                  d   «      }t        j,                  t        j.                  ||j                  «      |   |j(                  |   z  «      }t        j0                  |dt        j&                  ¬«      }t        j.                  t        j2                  |«      |j                  d   «      }t5        |j                  d   «      D �]S  }||   t*        |z  kD  rt1        |||   z  d«      }nŒ't1        t7        ||   ||   «      d«      }t7        t1        ||   ||   «      d«      }||dd…|f   z  } ||dd…|f   «      }|dk\  r|t*         |z  k  s|dk  r|t*         |z  kD  rt1        | |z  d«      }nt        j&                  }|dk\  r|t*        |z  kD  s|dk  r|t*        |z  k  rt7        | |z  d«      }nt        j&                   }t7        ||«      }t1        | |«      }| ||z  z   d|d	z  z  |z  z   }| ||z  z   d|d	z  z  |z  z   }||k  r/| ||z  z   d|d	z  z  |z  z   } t9        | «      t9        |«      kD  r|}| }||kD  r/| ||z  z   d|d	z  z  |z  z   }!t9        |!«      t9        |«      kD  r|}|!}t9        |«      t9        |«      k\  r<t9        |«      t9        |
«      kD  r%t        j:                  ||dd…|f   z  ||«      }	|}
�Œÿt9        |«      t9        |«      kD  s�Œt9        |«      t9        |
«      kD  s�Œ1t        j:                  ||dd…|f   z  ||«      }	|}
�ŒV |r[t        j                  ||	k  «      sJ ‚t        j                  |	|k  «      sJ ‚t        j"                  j%                  |	«      d
|z  k  sJ ‚|	S )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)r0   Ú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_geometryrY   j   sç  € òx Ü˜%¤Ô'Ð'Ð'Ü˜$¤§
¡
Ô+°·	±	¸Q²Ð>Ð>Ü× Ñ  Ó&×+Ñ+¨DÔ1Ð1Ð1ä�sœBŸJ™JÔ'Ø—‘˜A’Ø—	‘	˜!‘ §	¡	Ò)ð	
ð*ô ˜"œbŸj™jÔ)¨b¯h©h¸$¿*¹*Ò.DÐDÐDÜ˜"œbŸj™jÔ)¨b¯h©h¸$¿*¹*Ò.DÐDÐDÜ˜%¤Ô'Ð'Ð'Ü˜%¤Ô&Ð&Ð&Ü˜R Ó$ˆÜ�v‰v�b˜C‘iÔ Ð Ð Ü�v‰v�b˜S˜D‘jÔ!Ð!Ð!Ü�{‰{˜5Ô! e¨c¢kÐ1Ð1Ü	�‰�B˜Ó	€BÜ	�‰�B˜Ó	€Bô �=‰=˜Ó€DØ€EÜ�Y‰Y�^‰^˜C aˆ^Ó(€Fð ”b—f‘f�W‘ §¡¬$¨°©Ñ!3Ñ4€HØ”b—f‘f�W‘ §¡¬°©Ñ!2Ñ3€HØ”R—V‘V‘ §¡¬¨r©	Ñ 1Ñ2€HØ”R—V‘V‘ §¡¬¨°©
Ñ 2Ñ3€Hô —=‘=Ü
�‰˜˜HŸN™NÓ+¨HÑ5¸¿¹¸h¹ÑGó€Lô —6‘6˜,¨Q¼¿¹¸Ô@€Lä—?‘?¤2§=¡=°Ó#>ÀÇ	Á	È!ÁÓM€Lä—=‘=Ü
�‰˜˜HŸN™NÓ+¨HÑ5¸¿¹¸h¹ÑGó€Lô —6‘6˜,¨Q¼¿¹Ô?€LÜ—?‘?¤2§=¡=°Ó#>ÀÇ	Á	È!ÁÓM€Lä—=‘=Ü
�‰˜˜HŸN™NÓ+¨HÑ5¸¿¹¸h¹ÑGó€Lô —6‘6˜,¨Q¼¿¹¸Ô@€LÜ—?‘?¤2§=¡=°Ó#>ÀÇ	Á	È!ÁÓM€Lä—=‘=Ü
�‰˜˜HŸN™NÓ+¨HÑ5¸¿¹¸h¹ÑGó€Lô —6‘6˜,¨Q¼¿¹Ô?€LÜ—?‘?¤2§=¡=°Ó#>ÀÇ	Á	È!ÁÓM€Lä�3—9‘9˜Q‘<Ó ó Jˆà�!‰9”t˜e‘|Ò#Ü˜5 6¨!¡9Ñ,¨cÓ2‰Hð äœ3˜|¨A™°¸Q±Ó@À#ÓFˆÜœ3˜|¨A™°¸Q±Ó@À#ÓFˆð ˜3šq !˜t™9Ñ$ˆ	Ù˜šQ ˜T™“Oˆ	à˜ÒØœT˜E IÑ-Ò-Ø˜CÒØœT˜E IÑ-Ò-ä  ) ¨iÑ!7¸Ó=‰NäŸV™VˆNà˜ÒØœD 9Ñ,Ò,Ø˜CÒØœD 9Ñ,Ò,ä  ) ¨iÑ!7¸Ó=‰Nä Ÿf™f˜WˆNô ˜ ,Ó/ˆ	Ü˜˜	 <Ó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Ü—7‘7˜9 sª1¨a¨4¡yÑ0°"°bÓ9ˆDØŠEÜ�‹^œc )›nÔ,´°Y³Ä#ÀeÃ*Ô1LÜ—7‘7˜9 sª1¨a¨4¡yÑ0°"°bÓ9ˆDØŠEðUJñX Ü�v‰v�b˜D‘jÔ!Ð!Ð!Ü�v‰v�d˜b‘jÔ!Ð!Ð!Ü�y‰y�~‰~˜dÓ# c¨E¡kÒ1Ð1Ð1Ø€Kr   c                 ó  — |dk  |dkD  z  }|dkD  |dk  z  }t        j                  |«      }	||   |	|<   ||   |	|<   t         j                  j                  |	«      |kD  rÔ||z  }
	 t         j                  j                  ||
   «      }t        j                  |dz  |	|
    |	|
    z  z
  «      }|t
        t        |«      z  kD  rt        ||z  d«      }nn`|||
   z  |	|
<   |
|	|k  z  }|
|	|kD  z  }t        j                  |«      st        j                  |«      sn||   |	|<   ||   |	|<   |
||z   z  }
ŒÎ||	z  }|dk\  �rXt         j                  j                  |	«      }|t
        |z  kD  rt        ||z  d«      }nd} ||	«      }|t
         |z  k  rt        | |z  d«      }nt         j                  }|t         j                   kD  |	t
        |z  k  z  }|t         j                  k  |	t
        |z  kD  z  }t        j                  ||   |	|   z  t         j                  ¬«      }t        j                  ||   |	|   z  t         j                  ¬«      }t        ||«      }t        |||«      }t        j                  ||	z  ||«      }| ||z  z   d|dz  z  |z  z   }nt        j                  |«      }| }|r[t        j                  ||k  «      sJ ‚t        j                  ||k  «      sJ ‚t         j                  j                  |«      d|z  k  sJ ‚||fS )zM
    Same as `bound_constrained_cauchy_step` without the absolute value.
    r   r3   )r1   r2   r   )r   r5   r    r!   Úsqrtr8   r   r;   Úanyr6   r>   r?   r   )r"   r#   r   r$   r%   r&   r'   Úfixed_xlÚfixed_xuÚcauchy_stepÚworkingÚg_normÚdelta_reducedÚmurO   rB   rL   rP   Ú
alpha_quadÚi_xlÚi_xuÚalpha_xlÚalpha_xuÚalpha_bdÚalphar-   rA   s                              r   r   r   8  s!  € ð
 �S‘˜T C™ZÑ(€HØ�S‘˜T C™ZÑ(€Hô —-‘- Ó%€KØ˜x™L€K�ÑØ˜x™L€K�ÑÜ	‡y�y‡~�~�kÓ" UÒ*Ø˜XÑ%ˆØä—Y‘Y—^‘^ D¨¡MÓ2ˆFÜŸG™GØ�s‘
˜[¨'¨Ñ2°[À'ÀÑ5JÑJÑJóˆMð œœs =Ó1Ñ1Ò1Ü˜¨Ñ/°Ó5‘àØ#%¨¨W©Ñ#5ˆK˜Ñ ð  +°Ñ"2Ñ3ˆHØ +°Ñ"2Ñ3ˆHÜ—6‘6˜(Ô#¬B¯F©F°8Ô,<àØ$& x¡LˆK˜Ñ!Ø$& x¡LˆK˜Ñ!Ø (¨XÑ"5Ð 6Ñ6ˆGð) ð. �{Ñ"€IØ�CÓä—‘—‘ Ó,ˆØ”D˜5‘LÒ Ü˜5 6™>¨3Ó/‰Hð ˆHñ ˜Ó%ˆ	Øœ�u˜yÑ(Ò(Ü˜i˜Z¨)Ñ3°SÓ9‰JäŸ™ˆJð ”b—f‘f�W‘ ¬t°b©yÑ!8Ñ9ˆØ”R—V‘V‘ ¬d°R©iÑ 7Ñ8ˆÜ—6‘6˜"˜T™( [°Ñ%6Ñ6ÄÇÁÔGˆÜ—6‘6˜"˜T™( [°Ñ%6Ñ6ÄÇÁÔGˆÜ�x Ó*ˆô �H˜j¨(Ó3ˆÜ�w‰w�u˜{Ñ*¨B°Ó3ˆØ˜ 	Ñ)Ñ)¨C°%¸±*Ñ,<¸yÑ,HÑH‰ô �}‰}˜TÓ"ˆØˆáÜ�v‰v�b˜D‘jÔ!Ð!Ð!Ü�v‰v�d˜b‘jÔ!Ð!Ð!Ü�y‰y�~‰~˜dÓ# c¨E¡kÒ1Ð1Ð1Ø�ˆ;Ðr   )r   Únumpyr   Úutilsr   Úfinfor   Útinyr8   r.   rY   r   r	   r   r   ú<module>ro      s9   ðÛ ã å "ð €r‡x�x�ƒ×Ñ€ò\ò~Kó\Kr   