
    ^Mh]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    e/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/scipy/_lib/cobyqa/subsolvers/geometry.py<lambda>z!cauchy_geometry.<locals>.<lambda>[   s    4477(     皙?)
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"bj))Dbh$*.D.D.D.D"bj))Dbh$*.D.D.D.D%'''''%&&&&&R$$vbCi     vbSDj!!!!!{5!!1eckkkk	B		B	B		B
 !dBE5IIME6 		

 ME6 KK3v;;..55ED 2vbDj!!!!!vdbj!!!!!y~~d##cEk1111Kr   c                 <   |rt          | 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sBJ''	
A	!	))))"bj))Dbh$*.D.D.D.D"bj))Dbh$*.D.D.D.D%'''''%&&&&&R$$vbCi     vbSDj!!!!!{5!!1eckkkk	B		B	B		B =DEY^^Ca^((F bfW$!34HbfW!23HRVr	 12HRV
 23H =
HN++H5hG L 6,Q@@@L?2=#>#>	!MML=
HN++H5hG L 6,Q???L?2=#>#>	!MML=
HN++H5hG L 6,Q@@@L?2=#>#>	!MML=
HN++H5hG L 6,Q???L?2=#>#>	!MML39Q<   J J!9te|##56!9,c22HH 3|AQ@@#FF3|AQ@@#FF 3qqq!t9$	DQQQTOO	TEI---CTEI--- )i!7==NNVND9,,,CD9,,, )i!7==NN fWN ,//		<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0K0K79s111a4y0"b99DEE^^c)nn,,Y#e**1L1L79s111a4y0"b99DE 2vbDj!!!!!vdbj!!!!!y~~d##cEk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TCZ(HSTCZ(H -%%KxLKxLK	y~~k""U**X%	7Y^^DM22FGs
['2['5JJJ M s=11111/55#%W#5K  +"23H+"23H6(## BF8,<,< $&xLK!$&xLK!(X"5 66G)	7. {"IC,,D5L  56>3//HH H D%%	uy(((iZ)3S99JJJ bfWtby!89RVdRi 786"T([%66GGG6"T([%66GGGx** Hj(33wu{*B33	))C%*,<y,HH }T"" 2vbDj!!!!!vdbj!!!!!y~~d##cEk1111;r   )r   numpyr   utilsr   finfor   tinyr8   r.   rY   r   r	   r   r   <module>ro      s         " " " " " " rx\ \ \~K K K\K K K K Kr   