o
    i]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|
}|rt||ksJ t||ksJ tj|d| k sJ |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   _/home/kim/smarthome/.venv/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tolZstep1Zq_val1Zstep2Zq_val2stepr   r   r
   cauchy_geometry   s<   :

	r'   c           "      C   s  |rt | 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sJ 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rt|||  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rt| | d}ntj}|dkr|t| ks|dkr|t| k rt| | 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rt|t|
krt||dd|f  ||}	|}
qr|rt||	ksJ t|	|ksJ tj|	d
| k sJ |	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TINYZ
atleast_2dZbroadcast_tomaxZ
atleast_1drangeminr   clip)"r   r    r	   Zxptr!   r"   r#   r$   r%   r&   q_vals_normZi_xl_posZi_xl_negZi_xu_posZi_xu_negZalpha_xl_posZalpha_xl_negZalpha_xu_negZalpha_xu_poskalpha_trZalpha_bd_posZalpha_bd_neg	grad_step	curv_stepZalpha_quad_posZalpha_quad_negZ	alpha_posZ	alpha_negZ	q_val_posZ	q_val_negZq_val_quad_posZq_val_quad_negr   r   r
   spider_geometryj   s   <










$$r;   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rt|| d}nd}||	}|t | k rt| | 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   r-   r   r   sqrtr0   r   r1   anyr.   r3   r4   r   )r   r    r	   r!   r"   r#   r$   Zfixed_xlZfixed_xuZcauchy_stepZworkingZg_normZdelta_reducedmur9   r6   r8   r:   Z
alpha_quadZi_xlZi_xuZalpha_xlZalpha_xuZalpha_bdalphar&   r5   r   r   r
   r   8  sb   



r   )r   numpyr   utilsr   Zfinfor   Ztinyr0   r'   r;   r   r   r   r   r
   <module>   s    _ O