o
    ñh]7  ã                   @   sD   d dl Z d dlZddlmZ e e¡jZdd„ Z	dd„ Z
dd	„ ZdS )
é    Né   )Úget_arrays_tolc                    sŠ  |rmt | tƒs	J ‚t |tjƒr|jdksJ ‚t ˆ ¡ |¡s J ‚t |tjƒr,|j|jks.J ‚t |tjƒr:|j|jks<J ‚t |tƒsCJ ‚t |t	ƒsJJ ‚t
||ƒ}t ||k¡sXJ ‚t || k¡sbJ ‚t |¡rk|dksmJ ‚t |d¡}t |d¡}t| |ˆ ||||ƒ\}}	t|  | ‡ fdd„||||ƒ\}
}t|	ƒt|ƒkr¡|n|
}|rÃt ||k¡s®J ‚t ||k¡s·J ‚tj |¡d| 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
   ˆ | ƒ S )N© )Úx©Úcurvr   úY/var/www/vscode/kcb/lib/python3.10/site-packages/scipy/_lib/cobyqa/subsolvers/geometry.pyÚ<lambda>[   s   
 z!cauchy_geometry.<locals>.<lambda>çš™™™™™ñ?)Ú
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Ústepr   r   r
   Úcauchy_geometry   s<   :

ù	r+   c           "      C   s°  |r‚t | tƒs	J ‚t |tjƒr|jdksJ ‚t |¡ |¡s J ‚t |tjƒr3|jdkr3|jd |j	ks5J ‚t |tjƒrA|j|jksCJ ‚t |tjƒrO|j|jksQJ ‚t |tƒsXJ ‚t |t
ƒs_J ‚t||ƒ}t ||k¡smJ ‚t || k¡swJ ‚t |¡r€|dks‚J ‚t |d¡}t |d¡}t |¡}	| }
tjj|dd}|tj k|jt | k@ }|tj k|jt| k @ }|tjk |jt| k@ }|tjk |jt | k @ }t t ||j¡| |j|  ¡}tj|dtj d}t t |¡|jd ¡}t t ||j¡| |j|  ¡}tj|dtjd}t t |¡|jd ¡}t t ||j¡| |j|  ¡}tj|dtj d}t t |¡|jd ¡}t t ||j¡| |j|  ¡}tj|dtjd}t t |¡|jd ¡}t|jd ƒD ]>}|| t| krˆt|||  dƒ}nqrtt|| || ƒdƒ}tt|| || ƒdƒ}||dd…|f  }||dd…|f ƒ}|dkrÃ|t | k sÐ|dkrÙ|t | krÙt| | dƒ}ntj}|dkrè|t| ksô|dkrý|t| k rýt| | dƒ}ntj }t||ƒ}t| |ƒ}| ||  d|d	  |  }| ||  d|d	  |  }||k rH| ||  d|d	  |  } t| ƒt|ƒkrH|}| }||krh| ||  d|d	  |  }!t|!ƒt|ƒkrh|}|!}t|ƒt|ƒkrt|ƒt|
ƒkrt ||dd…|f  ||¡}	|}
qrt|ƒt|ƒkr°t|ƒt|
ƒkr°t ||dd…|f  ||¡}	|}
qr|rÖt ||	k¡s¿J ‚t |	|k¡sÉJ ‚tj |	¡d
| 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)r,   Ú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_negr   r   r
   Úspider_geometryj   sÎ   <
ÿ


ÿÿÿÿ




ÿÿ
ÿþÿ
ÿþÿ$$€rU   c                 C   sŒ  |dk |dk@ }|dk|dk @ }t  |¡}	|| |	|< || |	|< t j |	¡|kr‹||B }
	 t j ||
 ¡}t  |d |	|
  |	|
    ¡}|tt|ƒ krWt|| dƒ}nn3|||
  |	|
< |
|	|k @ }|
|	|k@ }t  |¡swt  |¡swn|| |	|< || |	|< |
||B  @ }
q.||	 }|dkrt j |	¡}|t| kr¨t|| dƒ}nd}||	ƒ}|t | k r¾t| | dƒ}nt j	}|t j	 k|	t| k @ }|t j	k |	t| k@ }t j
|| |	|  t j	d}t j
|| |	|  t j	d}t
||ƒ}t
|||ƒ}t  ||	 ||¡}| ||  d|d  |  }nt  |¡}| }|rBt  ||k¡s+J ‚t  ||k¡s5J ‚t j |¡d| k sBJ ‚||fS )zM
    Same as `bound_constrained_cauchy_step` without the absolute value.
    r   Tr/   )r-   r.   r   )r   r1   r   r   Úsqrtr4   r   r7   Úanyr2   r:   r;   r   )r   r    r	   r!   r"   r#   r$   Úfixed_xlÚfixed_xuÚcauchy_stepÚworkingÚg_normÚdelta_reducedÚmurK   r>   rH   rL   Ú
alpha_quadÚi_xlÚi_xuÚalpha_xlÚalpha_xuÚalpha_bdÚalphar*   r=   r   r   r
   r   8  sb   
ÿì


r   )r   Únumpyr   Úutilsr   Úfinfor   Útinyr4   r+   rU   r   r   r   r   r
   Ú<module>   s    _ O