§
    fŠtj]7  ã                   ó^   — d dl Z d dlZddlmZ  ej        e¦  «        j        Zd„ Z	d„ Z
d„ ZdS )é    Né   )Úget_arrays_tolc           	      ól  ‡— |�rNt          | t          ¦  «        sJ ‚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          ¦  «        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¦  «        }t          | |‰||||¦  «        \  }}	t          |  | ˆfd„||||¦  «        \  }
}t!          |	¦  «        t!          |¦  «        k    r|n|
}|r\t          j        ||k    ¦  «        sJ ‚t          j        ||k    ¦  «        sJ ‚t          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/html/CA-Chatbot/venv/lib/python3.11/site-packages/scipy/_lib/cobyqa/subsolvers/geometry.pyú<lambda>z!cauchy_geometry.<locals>.<lambda>[   s   ø€ �4�4˜‘7”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 ñ 2Ý˜%¥Ñ'Ô'Ð'Ð'Ð'Ý˜$¥¤
Ñ+Ô+Ð>°´	¸Q²°°Ð>ÝÔ  Ñ&Ô&×+Ò+¨DÑ1Ô1Ð1Ð1Ð1Ý˜"�bœjÑ)Ô)ÐD¨b¬h¸$¼*Ò.DÐ.DÐ.DÐDÝ˜"�bœjÑ)Ô)ÐD¨b¬h¸$¼*Ò.DÐ.DÐ.DÐDÝ˜%¥Ñ'Ô'Ð'Ð'Ð'Ý˜%¥Ñ&Ô&Ð&Ð&Ð&Ý˜R Ñ$Ô$ˆÝŒv�b˜C’iÑ Ô Ð Ð Ð ÝŒv�b˜S˜D’jÑ!Ô!Ð!Ð!Ð!ÝŒ{˜5Ñ!Ô!Ð1 e¨c¢k k kÐ1Ý	Œ�B˜Ñ	Ô	€BÝ	Œ�B˜Ñ	Ô	€Bõ
 ! ¨¨d°B¸¸EÀ5ÑIÔI�M€Eˆ6Ý Ø	ˆØ	ˆØÐÐÐØ
Ø
ØØñô �M€Eˆ6õ ˜‘K”K¥3 v¡;¤;Ò.Ð.ˆ5ˆ5°E€Dàð 2ÝŒv�b˜D’jÑ!Ô!Ð!Ð!Ð!ÝŒv�d˜b’jÑ!Ô!Ð!Ð!Ð!ÝŒy�~Š~˜dÑ#Ô# c¨E¡kÒ1Ð1Ð1Ð1Ø€Kr   c                 ó<  — |�r‹t          | t          ¦  «        sJ ‚t          |t          j        ¦  «        r|j        dk    sJ ‚t          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k    sJ ‚t          j        |d¦  «        }t          j        |d¦  «        }t          j        |¦  «        }	| }
t          j                             |d¬¦  «        }|t          j         k    |j        t*           |z  k    z  }|t          j         k    |j        t*          |z  k     z  }|t          j        k     |j        t*          |z  k    z  }|t          j        k     |j        t*           |z  k     z  }t          j        t          j        ||j        ¦  «        |         |j        |         z  ¦  «        }t          j        |dt          j         ¬¦  «        }t          j        t          j        |¦  «        |j        d         ¦  «        }t          j        t          j        ||j        ¦  «        |         |j        |         z  ¦  «        }t          j        |dt          j        ¬¦  «        }t          j        t          j        |¦  «        |j        d         ¦  «        }t          j        t          j        ||j        ¦  «        |         |j        |         z  ¦  «        }t          j        |dt          j         ¬¦  «        }t          j        t          j        |¦  «        |j        d         ¦  «        }t          j        t          j        ||j        ¦  «        |         |j        |         z  ¦  «        }t          j        |dt          j        ¬¦  «        }t          j        t          j        |¦  «        |j        d         ¦  «        }t5          |j        d         ¦  «        D �]Ð}||         t*          |z  k    rt1          |||         z  d¦  «        }nŒ2t1          t7          ||         ||         ¦  «        d¦  «        }t7          t1          ||         ||         ¦  «        d¦  «        }||dd…|f         z  } ||dd…|f         ¦  «        }|dk    r|t*           |z  k     s|dk    r$|t*           |z  k    rt1          | |z  d¦  «        }nt          j        }|dk    r|t*          |z  k    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     r8| ||z  z   d|d	z  z  |z  z   } t9          | ¦  «        t9          |¦  «        k    r|}| }||k    r8| ||z  z   d|d	z  z  |z  z   }!t9          |!¦  «        t9          |¦  «        k    r|}|!}t9          |¦  «        t9          |¦  «        k    rGt9          |¦  «        t9          |
¦  «        k    r't          j        ||dd…|f         z  ||¦  «        }	|}
�Œkt9          |¦  «        t9          |¦  «        k    rEt9          |¦  «        t9          |
¦  «        k    r%t          j        ||dd…|f         z  ||¦  «        }	|}
�ŒÒ|r\t          j        ||	k    ¦  «        sJ ‚t          j        |	|k    ¦  «        sJ ‚t          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 ñ 2Ý˜%¥Ñ'Ô'Ð'Ð'Ð'Ý˜$¥¤
Ñ+Ô+Ð>°´	¸Q²°°Ð>ÝÔ  Ñ&Ô&×+Ò+¨DÑ1Ô1Ð1Ð1Ð1å�s�BœJÑ'Ô'ð	
à”˜A’�Ø”	˜!” ¤	Ò)Ð)Ð)ð*õ ˜"�bœjÑ)Ô)ÐD¨b¬h¸$¼*Ò.DÐ.DÐ.DÐDÝ˜"�bœjÑ)Ô)ÐD¨b¬h¸$¼*Ò.DÐ.DÐ.DÐDÝ˜%¥Ñ'Ô'Ð'Ð'Ð'Ý˜%¥Ñ&Ô&Ð&Ð&Ð&Ý˜R Ñ$Ô$ˆÝŒv�b˜C’iÑ Ô Ð Ð Ð ÝŒv�b˜S˜D’jÑ!Ô!Ð!Ð!Ð!ÝŒ{˜5Ñ!Ô!Ð1 e¨c¢k k kÐ1Ý	Œ�B˜Ñ	Ô	€BÝ	Œ�B˜Ñ	Ô	€Bõ Œ=˜ÑÔ€DØ€EÝŒY�^Š^˜C aˆ^Ñ(Ô(€Fð •b”f�W’ ¤­$¨°©Ò!3Ñ4€HØ•b”f�W’ ¤­°©Ò!2Ñ3€HØ•R”V’ ¤­¨r©	Ò 1Ñ2€HØ•R”V’ ¤­¨°©
Ò 2Ñ3€Hõ ”=Ý
Œ˜˜HœNÑ+Ô+¨HÔ5¸¼¸h¼ÑGñô €Lõ ”6˜,¨Q½¼¸Ð@Ñ@Ô@€Lå”?¥2¤=°Ñ#>Ô#>ÀÄ	È!ÄÑMÔM€Lå”=Ý
Œ˜˜HœNÑ+Ô+¨HÔ5¸¼¸h¼ÑGñô €Lõ ”6˜,¨Q½¼Ð?Ñ?Ô?€LÝ”?¥2¤=°Ñ#>Ô#>ÀÄ	È!ÄÑMÔM€Lå”=Ý
Œ˜˜HœNÑ+Ô+¨HÔ5¸¼¸h¼ÑGñô €Lõ ”6˜,¨Q½¼¸Ð@Ñ@Ô@€LÝ”?¥2¤=°Ñ#>Ô#>ÀÄ	È!ÄÑMÔM€Lå”=Ý
Œ˜˜HœNÑ+Ô+¨HÔ5¸¼¸h¼ÑGñô €Lõ ”6˜,¨Q½¼Ð?Ñ?Ô?€LÝ”?¥2¤=°Ñ#>Ô#>ÀÄ	È!ÄÑMÔM€Lå�3”9˜Q”<Ñ Ô ð Jñ Jˆà�!Œ9•t˜e‘|Ò#Ð#Ý˜5 6¨!¤9Ñ,¨cÑ2Ô2ˆHˆHð å�3˜|¨Aœ°¸Q´Ñ@Ô@À#ÑFÔFˆÝ�3˜|¨Aœ°¸Q´Ñ@Ô@À#ÑFÔFˆð ˜3˜q˜q˜q !˜tœ9Ñ$ˆ	Ø�D˜˜Q˜Q˜Q ˜Tœ‘O”Oˆ	à˜ÒÐØ�T˜E IÑ-Ò-Ð-Ø˜CÒÐØ�T˜E IÑ-Ò-Ð-å  ) ¨iÑ!7¸Ñ=Ô=ˆNˆNåœVˆNà˜ÒÐØ�D 9Ñ,Ò,Ð,Ø˜CÒÐØ�D 9Ñ,Ò,Ð,å  ) ¨iÑ!7¸Ñ=Ô=ˆNˆNå œf˜WˆNõ ˜ ,Ñ/Ô/ˆ	Ý˜˜	 <Ñ0Ô0ˆ	à�I 	Ñ)Ñ)¨C°)¸S±.Ñ,@À9Ñ,LÑLð 	ð �I 	Ñ)Ñ)¨C°)¸S±.Ñ,@À9Ñ,LÑLð 	ð ˜IÒ%Ð%àØ  9Ñ,ñ-à˜¨Ñ+Ñ+¨iÑ7ñ8ð õ
 �>Ñ"Ô"¥S¨¡^¤^Ò3Ð3Ø*�	Ø*�	Ø˜IÒ%Ð%àØ  9Ñ,ñ-à˜¨Ñ+Ñ+¨iÑ7ñ8ð õ
 �>Ñ"Ô"¥S¨¡^¤^Ò3Ð3Ø*�	Ø*�	Ýˆy‰>Œ>�S ™^œ^Ò+Ð+µ°I±´ÅÀUÁÄÒ0KÐ0KÝ”7˜9 s¨1¨1¨1¨a¨4¤yÑ0°"°bÑ9Ô9ˆDØˆE‰EÝ�‰^Œ^�c )™nœnÒ,Ð,µ°Y±´Å#ÀeÁ*Ä*Ò1LÐ1LÝ”7˜9 s¨1¨1¨1¨a¨4¤yÑ0°"°bÑ9Ô9ˆDØˆEùàð 2ÝŒv�b˜D’jÑ!Ô!Ð!Ð!Ð!ÝŒv�d˜b’jÑ!Ô!Ð!Ð!Ð!ÝŒy�~Š~˜dÑ#Ô# c¨E¡kÒ1Ð1Ð1Ð1Ø€Kr   c                 óŠ  — |dk     |dk    z  }|dk    |dk     z  }t          j        |¦  «        }	||         |	|<   ||         |	|<   t           j                             |	¦  «        |k    rï||z  }
	 t           j                             ||
         ¦  «        }t          j        |dz  |	|
          |	|
          z  z
  ¦  «        }|t
          t          |¦  «        z  k    rt          ||z  d¦  «        }nni|||
         z  |	|
<   |
|	|k     z  }|
|	|k    z  }t          j        |¦  «        st          j        |¦  «        sn ||         |	|<   ||         |	|<   |
||z   z  }
Œé||	z  }|dk    �rjt           j                             |	¦  «        }|t
          |z  k    rt          ||z  d¦  «        }nd} ||	¦  «        }|t
           |z  k     rt          | |z  d¦  «        }nt           j	        }|t           j	         k    |	t
          |z  k     z  }|t           j	        k     |	t
          |z  k    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                             |¦  «        d|z  k     sJ ‚||fS )zM
    Same as `bound_constrained_cauchy_step` without the absolute value.
    r   Tr3   )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  su  € ð
 �S’˜T CšZÑ(€HØ�S’˜T CšZÑ(€Hõ ”- Ñ%Ô%€KØ˜xœL€K�ÑØ˜xœL€K�ÑÝ	„y‡~‚~�kÑ"Ô" UÒ*Ð*Ø˜XÑ%ˆð	7å”Y—^’^ D¨¤MÑ2Ô2ˆFÝœGØ�s‘
˜[¨'¨Ô2°[À'ÀÔ5JÑJÑJñô ˆMð ��s =Ñ1Ô1Ñ1Ò1Ð1Ý˜¨Ñ/°Ñ5Ô5��àØ#%¨¨W¬Ñ#5ˆK˜Ñ ð  +°Ò"2Ñ3ˆHØ +°Ò"2Ñ3ˆHÝ”6˜(Ñ#Ô#ð ­B¬F°8Ñ,<Ô,<ð àØ$& x¤LˆK˜Ñ!Ø$& x¤LˆK˜Ñ!Ø (¨XÑ"5Ð 6Ñ6ˆGð)	7ð. �{Ñ"€IØ�CÒÑå”—’ Ñ,Ô,ˆØ•D˜5‘LÒ Ð Ý˜5 6™>¨3Ñ/Ô/ˆHˆHð ˆHð �D˜Ñ%Ô%ˆ	Ø��u˜yÑ(Ò(Ð(Ý˜i˜Z¨)Ñ3°SÑ9Ô9ˆJˆJåœˆJð •b”f�W’ ­t°b©yÒ!8Ñ9ˆØ•R”V’ ­d°R©iÒ 7Ñ8ˆÝ”6˜"˜Tœ( [°Ô%6Ñ6ÅÄÐGÑGÔGˆÝ”6˜"˜Tœ( [°Ô%6Ñ6ÅÄÐGÑGÔGˆÝ�x Ñ*Ô*ˆõ �H˜j¨(Ñ3Ô3ˆÝŒw�u˜{Ñ*¨B°Ñ3Ô3ˆØ˜ 	Ñ)Ñ)¨C°%¸±*Ñ,<¸yÑ,HÑHˆˆõ Œ}˜TÑ"Ô"ˆØˆàð 2ÝŒv�b˜D’jÑ!Ô!Ð!Ð!Ð!ÝŒv�d˜b’jÑ!Ô!Ð!Ð!Ð!ÝŒy�~Š~˜dÑ#Ô# c¨E¡kÒ1Ð1Ð1Ð1Ø�ˆ;Ðr   )r   Únumpyr   Úutilsr   Úfinfor   Útinyr8   r.   rY   r   r	   r   r   ú<module>ro      s„   ðØ €€€à Ð Ð Ð à "Ð "Ð "Ð "Ð "Ð "ð €r„x��„Ô€ð\ð \ð \ð~Kð Kð Kð\Kð Kð Kð Kð Kr   