
    bi                         d dl Z d dlZd dlmZ ddlmZ ddlmZm	Z	m
Z
  ej                  e      j                  Z G d d      Zd Z G d	 d
      Z G d d      Zy)    N)eigh   )Options)MaxEvalErrorTargetSuccessFeasibleSuccessc                       e Zd ZdZd Zed        Zed        Zed        Zej                  d        Zed        Z
e
j                  d        Z
d	 Zy
)Interpolationz
    Interpolation set.

    This class stores a base point around which the models are expanded and the
    interpolation points. The coordinates of the interpolation points are
    relative to the base point.
    c                 0   |t         j                     | _        dt        j                  |j
                  j                  |j
                  j                  z
        z  }|t         j                     |kD  r`||t         j                  j                  <   t        j                  |t         j                     |g      |t         j                  j                  <   t        j                  |j                        | _        | j                  |j
                  j                  d|t         j                     z  z   k  }|j
                  j                  |   | j                  |<   |j
                  j                  d|t         j                     z  z   | j                  k  | j                  |j
                  j                  |t         j                     z   k  z  }t        j                  |j
                  j                  |   |t         j                     z   |j
                  j                  |         | j                  |<   | j                  |j
                  j                  d|t         j                     z  z
  k\  }|j
                  j                  |   | j                  |<   | j                  |j
                  j                  d|t         j                     z  z
  k  |j
                  j                  |t         j                     z
  | j                  k  z  }t        j                   |j
                  j                  |   |t         j                     z
  |j
                  j                  |         | j                  |<   t        j"                  |j$                  |t         j&                     f      | _        t+        d|t         j&                           D ]  }||j$                  k  rU||dz
     r'|t         j                      | j,                  |dz
  |f<   B|t         j                     | j,                  |dz
  |f<   h|d|j$                  z  k  r|||j$                  z
  dz
     r6d|t         j                     z  | j,                  ||j$                  z
  dz
  |f<   |||j$                  z
  dz
     r7d|t         j                     z  | j,                  ||j$                  z
  dz
  |f<   |t         j                      | j,                  ||j$                  z
  dz
  |f<   F||j$                  z
  dz
  |j$                  z  }	|d|	z   |j$                  z  z
  dz
  }
|
|	z   |j$                  z  }| j,                  |
|
dz   f   | j,                  |
|f<   | j,                  ||dz   f   | j,                  ||f<    d| _        y)z
        Initialize the interpolation set.

        Parameters
        ----------
        pb : `cobyqa.problem.Problem`
            Problem to be solved.
        options : dict
            Options of the solver.
              ?r             @g       N)r   DEBUG_debugnpminboundsxuxlRHOBEGvalueRHOENDcopyx0_x_basex_baseminimummaximumzerosnNPT_xptrangexpt
_lhs_cache)selfpboptions
max_radiusvery_close_xl_idxclose_xl_idxvery_close_xu_idxclose_xu_idxkspreadk1k2s               S/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/scipy/_lib/cobyqa/models.py__init__zInterpolation.__init__   sX    gmm,266")),,"=>>
7>>"Z/,6GGNN((),.FFGNN+-GGNN(() wwruu~KK299<<#0G*GGG 	 *,6G)H%&IILL3!8884;;F[[BIILL77>>+BBBD %'JJIILL&)@@IILL&%
L!
 KK299<<#0G*GGG 	 *,6G)H%&KK")),,ww~~/F)FFFYY\\GGNN33t{{BD %'JJIILL&)@@IILL&%
L! HHbddGGKK$89:	q''++./ 	7ABDDy$QU+*1'..*A)ADHHQUAX&)0)@DHHQUAX&a"$$h$QX\203ggnn6M0MDHHQX\1_-&q244x!|404ww~~7N0NDHHQX\1_-181H0HDHHQX\1_-bdd(Q,244/!f*,,q06kRTT)"&((2rAv:"6Q"&((2rAv:"6Q%	7&     c                 4    | j                   j                  d   S )t
        Number of variables.

        Returns
        -------
        int
            Number of variables.
        r   r$   shaper&   s    r2   r    zInterpolation.n]        xx~~a  r4   c                 4    | j                   j                  d   S )
        Number of interpolation points.

        Returns
        -------
        int
            Number of interpolation points.
        r   r7   r9   s    r2   nptzInterpolation.npti   r:   r4   c                     | j                   S )z
        Interpolation points.

        Returns
        -------
        `numpy.ndarray`, shape (n, npt)
            Interpolation points.
        )r"   r9   s    r2   r$   zInterpolation.xptu   s     yyr4   c                     | j                   r,|j                  | j                  | j                  fk(  sJ d       || _        y)z
        Set the interpolation points.

        Parameters
        ----------
        xpt : `numpy.ndarray`, shape (n, npt)
            New interpolation points.
        z The shape of `xpt` is not valid.N)r   r8   r    r=   r"   )r&   r$   s     r2   r$   zInterpolation.xpt   sG     ;;99!  2 22  	r4   c                     | j                   S )z
        Base point around which the models are expanded.

        Returns
        -------
        `numpy.ndarray`, shape (n,)
            Base point around which the models are expanded.
        )r   r9   s    r2   r   zInterpolation.x_base   s     ||r4   c                 l    | j                   r!|j                  | j                  fk(  sJ d       || _        y)z
        Set the base point around which the models are expanded.

        Parameters
        ----------
        x_base : `numpy.ndarray`, shape (n,)
            New base point around which the models are expanded.
        z#The shape of `x_base` is not valid.N)r   r8   r    r   )r&   r   s     r2   r   zInterpolation.x_base   s>     ;;<<$  545  r4   c                     | j                   r$d|cxk  r| j                  k  sJ d        J d       | j                  | j                  dd|f   z   S )a<  
        Get the `k`-th interpolation point.

        The return point is relative to the origin.

        Parameters
        ----------
        k : int
            Index of the interpolation point.

        Returns
        -------
        `numpy.ndarray`, shape (n,)
            `k`-th interpolation point.
        r   zThe index `k` is not valid.N)r   r=   r   r$   )r&   r.   s     r2   pointzInterpolation.point   sO      ;;$DHH$C&CC$C&CC${{TXXad^++r4   N)__name__
__module____qualname____doc__r3   propertyr    r=   r$   setterr   rC    r4   r2   r
   r
      s    EN 	! 	! 	! 	! 	 	 	ZZ   	 	 ]] ,r4   r
   c                 v   | j                   }|1t        j                  | j                  |d         r|d   |d   |d   fS t        j                  t        j
                  j                  | j                  d      t              }| j                  |z  }|j                  \  }}t        j                  ||z   d	z   ||z   d	z   f      }d
|j                  |z  dz  z  |d|d|f<   d|d||f<   |j                  |d||d	z   df<   d||d|f<   |||d	z   dd|f<   t        j                  ||z   d	z         }d|dz  z  |d| |dz  ||<   |||d	z   d t        |d      \  }}	t        j                  | j                        t        j                  |      t        j                  |      ||	fd}
|
| _         ||||	ffS )a  
    Build the left-hand side matrix of the interpolation system. The
    matrix below stores W * diag(right_scaling),
    where W is the theoretical matrix of the interpolation system. The
    right scaling matrices is chosen to keep the elements in
    the matrix well-balanced.

    Parameters
    ----------
    interpolation : `cobyqa.models.Interpolation`
        Interpolation set.
    Nr$   aright_scalingr   r   )axisinitialr   r   r         ?F)check_finite)r$   rL   rM   r   )r%   r   array_equalr$   maxlinalgnormEPSr8   r   Temptyr   r   )interpolation_cachescale	xpt_scaler    r=   rL   rM   
eig_valueseig_vectors	new_caches              r2   build_systemra      s    %%F bnn6%= c{F?3VF^CCFF299>>-"3"3!><cJE!!E)I__FAs
#'A+sQw{+,A9;;2s::AdsdDSDjMAdsdCiL!AdsdC!GHnAc4C4iL!AcAghn HHS1Wq[)Ms
*M$3M##M#'("159J ww}(()WWQZ/[)	I  )Mmj+666r4   c                       e Zd ZdZd Zd Zed        Zed        Zd Z	d Z
d Zd	 Zd
 Zd Zed        Zed        Zy)	Quadratica:  
    Quadratic model.

    This class stores the Hessian matrix of the quadratic model using the
    implicit/explicit representation designed by Powell for NEWUOA [1]_.

    References
    ----------
    .. [1] M. J. D. Powell. The NEWUOA software for unconstrained optimization
       without derivatives. In G. Di Pillo and M. Roma, editors, *Large-Scale
       Nonlinear Optimization*, volume 83 of Nonconvex Optim. Appl., pages
       255--297. Springer, Boston, MA, USA, 2006. `doi:10.1007/0-387-30065-1_16
       <https://doi.org/10.1007/0-387-30065-1_16>`_.
    c                    || _         | j                   r!|j                  |j                  fk(  sJ d       |j                  |j                  dz   k  rt	        d|j                  dz    d      | j                  ||      \  | _        | _        | _        }t        j                  | j                  | j                  f      | _        y)a  
        Initialize the quadratic model.

        Parameters
        ----------
        interpolation : `cobyqa.models.Interpolation`
            Interpolation set.
        values : `numpy.ndarray`, shape (npt,)
            Values of the interpolated function at the interpolation points.
        debug : bool
            Whether to make debugging tests during the execution.

        Raises
        ------
        `numpy.linalg.LinAlgError`
            If the interpolation system is ill-defined.
        #The shape of `values` is not valid.r   z4The number of interpolation points must be at least .N)r   r8   r=   r    
ValueError
_get_model_const_grad_i_hessr   r   _e_hess)r&   rZ   valuesdebug_s        r2   r3   zQuadratic.__init__  s    $ ;;<<!!$  545  }22F ??Q&'q*  48??4
0TZq xx 01r4   c                 2   | j                   r!|j                  | j                  fk(  sJ d       ||j                  z
  }| j                  | j
                  |z  z   d| j                  |j                  j                  |z  dz  z  || j                  z  |z  z   z  z   S )a  
        Evaluate the quadratic model at a given point.

        Parameters
        ----------
        x : `numpy.ndarray`, shape (n,)
            Point at which the quadratic model is evaluated.
        interpolation : `cobyqa.models.Interpolation`
            Interpolation set.

        Returns
        -------
        float
            Value of the quadratic model at `x`.
        The shape of `x` is not valid.r   r   )
r   r8   r    r   ri   rj   rk   r$   rX   rl   r&   xrZ   x_diffs       r2   __call__zQuadratic.__call__*  s      ;;77tvvi'I)II'])))KKjj6!" 1 1 3 3f <DD4<<'&01	
r4   c                 .    | j                   j                  S )r6   )rj   sizer9   s    r2   r    zQuadratic.nG  s     zzr4   c                 .    | j                   j                  S )z
        Number of interpolation points used to define the quadratic model.

        Returns
        -------
        int
            Number of interpolation points used to define the quadratic model.
        )rk   rw   r9   s    r2   r=   zQuadratic.nptS  s     ||   r4   c                     | j                   r!|j                  | j                  fk(  sJ d       ||j                  z
  }| j                  | j                  ||      z   S )a  
        Evaluate the gradient of the quadratic model at a given point.

        Parameters
        ----------
        x : `numpy.ndarray`, shape (n,)
            Point at which the gradient of the quadratic model is evaluated.
        interpolation : `cobyqa.models.Interpolation`
            Interpolation set.

        Returns
        -------
        `numpy.ndarray`, shape (n,)
            Gradient of the quadratic model at `x`.
        rq   )r   r8   r    r   rj   	hess_prodrr   s       r2   gradzQuadratic.grad_  sS      ;;77tvvi'I)II'])))zzDNN6=AAAr4   c                     | j                   |j                  | j                  ddt        j                  f   |j                  j
                  z  z  z   S )a;  
        Evaluate the Hessian matrix of the quadratic model.

        Parameters
        ----------
        interpolation : `cobyqa.models.Interpolation`
            Interpolation set.

        Returns
        -------
        `numpy.ndarray`, shape (n, n)
            Hessian matrix of the quadratic model.
        N)rl   r$   rk   r   newaxisrX   )r&   rZ   s     r2   hesszQuadratic.hesst  sG     ||m//LLBJJ'-*;*;*=*==
 
 	
r4   c                     | j                   r!|j                  | j                  fk(  sJ d       | j                  |z  |j                  | j
                  |j                  j                  |z  z  z  z   S )a.  
        Evaluate the right product of the Hessian matrix of the quadratic model
        with a given vector.

        Parameters
        ----------
        v : `numpy.ndarray`, shape (n,)
            Vector with which the Hessian matrix of the quadratic model is
            multiplied from the right.
        interpolation : `cobyqa.models.Interpolation`
            Interpolation set.

        Returns
        -------
        `numpy.ndarray`, shape (n,)
            Right product of the Hessian matrix of the quadratic model with
            `v`.
        The shape of `v` is not valid.)r   r8   r    rl   r$   rk   rX   r&   vrZ   s      r2   rz   zQuadratic.hess_prod  si    & ;;77tvvi'I)II'||a-"3"3LLM--//!34#
 
 	
r4   c                     | j                   r!|j                  | j                  fk(  sJ d       || j                  z  |z  | j                  |j
                  j                  |z  dz  z  z   S )a  
        Evaluate the curvature of the quadratic model along a given direction.

        Parameters
        ----------
        v : `numpy.ndarray`, shape (n,)
            Direction along which the curvature of the quadratic model is
            evaluated.
        interpolation : `cobyqa.models.Interpolation`
            Interpolation set.

        Returns
        -------
        float
            Curvature of the quadratic model along `v`.
        r   r   )r   r8   r    rl   rk   r$   rX   r   s      r2   curvzQuadratic.curv  sf    " ;;77tvvi'I)II'q llm//11A5#==>	
r4   c                 &   | j                   rfd|cxk  r| j                  k  sJ d        J d       |j                  | j                  fk(  sJ d       |j                  | j                  fk(  sJ d       | xj                  | j
                  |   t        j                  ||      z  z  c_        d| j
                  |<   | j                  ||      \  }}}}| xj                  |z  c_	        | xj                  |z  c_
        | xj
                  |z  c_        |S )a  
        Update the quadratic model.

        This method applies the derivative-free symmetric Broyden update to the
        quadratic model. The `knew`-th interpolation point must be updated
        before calling this method.

        Parameters
        ----------
        interpolation : `cobyqa.models.Interpolation`
            Updated interpolation set.
        k_new : int
            Index of the updated interpolation point.
        dir_old : `numpy.ndarray`, shape (n,)
            Value of ``interpolation.xpt[:, k_new]`` before the update.
        values_diff : `numpy.ndarray`, shape (npt,)
            Differences between the values of the interpolated nonlinear
            function and the previous quadratic model at the updated
            interpolation points.

        Raises
        ------
        `numpy.linalg.LinAlgError`
            If the interpolation system is ill-defined.
        r   The index `k_new` is not valid.z$The shape of `dir_old` is not valid.z(The shape of `values_diff` is not valid.        )r   r=   r8   r    rl   rk   r   outerrh   ri   rj   )	r&   rZ   k_newdir_oldvalues_diffconstr{   i_hessill_conditioneds	            r2   updatezQuadratic.update  s   4 ;;((K*KK(K*KK(==%  656  $$)  :9:  	U+bhhw.HHH!U 040
,tV_ 	u

d
r4   c                    | j                   r!|j                  | j                  fk(  sJ d        | ||      | _        | j	                  ||      | _        ||j                  z
  }t        j                  ||j                  d|ddt        j                  f   z  z
  | j                  z        }| xj                  ||j                  z   z  c_        y)aB  
        Shift the point around which the quadratic model is defined.

        Parameters
        ----------
        interpolation : `cobyqa.models.Interpolation`
            Previous interpolation set.
        new_x_base : `numpy.ndarray`, shape (n,)
            Point that will replace ``interpolation.x_base``.
        'The shape of `new_x_base` is not valid.r   N)r   r8   r    ri   r{   rj   r   r   r   r$   r}   rk   rl   rX   )r&   rZ   
new_x_baseshiftr   s        r2   shift_x_basezQuadratic.shift_x_base  s     ;;##(  989  :}5YYz=9
]111uQ

]';!;;t||K
 	))r4   c                    | j                   j                  \  }}|j                  dk(  r|j                  d   ||z   dz   k(  sJ d       t        |       \  }}}||ddt        j
                  f   z  }t	        j                  t	        j                  |            r(t	        j                  t	        j                  |            st        j                  j                  d      |\  }}	t	        j                  |      t        kD  }
|	dd|
f   }	d||
   z  }t	        j                  |
d       }|	|	j                  |z  |ddt        j
                  f   z  z  }||ddt        j
                  f   z  |fS )a  
        Solve the interpolation systems.

        Parameters
        ----------
        interpolation : `cobyqa.models.Interpolation`
            Interpolation set.
        rhs : `numpy.ndarray`, shape (npt + n + 1, m)
            Right-hand side vectors of the ``m`` interpolation systems.

        Returns
        -------
        `numpy.ndarray`, shape (npt + n + 1, m)
            Solutions of the interpolation systems.
        `numpy.ndarray`, shape (m, )
            Whether the interpolation systems are ill-conditioned.

        Raises
        ------
        `numpy.linalg.LinAlgError`
            If the interpolation systems are ill-defined.
        r   r   r   z The shape of `rhs` is not valid.Nz(The interpolation system is ill-defined.rQ   )r$   r8   ndimra   r   r}   allisfiniterU   LinAlgErrorabsrW   rX   )rZ   rhsr    r=   rL   rM   eig
rhs_scaledr^   r_   large_eig_valuesinv_eig_valuesr   left_scaled_solutionss                 r2   solve_systemszQuadratic.solve_systems  sZ   0 ""((3HHMciilcAgk9	.-	.9 !-] ;=# =BJJ77
r{{1~&266"++j2I+J))'': 
 #&
K66*-3!!%5"56z*:;;66"2A66 +]]Z'>!RZZ-+HH!
 "M!RZZ-$@@
 	
r4   c           
      L   |j                   | j                  fk(  sJ d       | j                  j                   \  }}t        j	                  | t        j                  |t        j                  |dz         gg      j                        \  }}||df   ||dz   ddf   |d|df   |fS )a  
        Solve the interpolation system.

        Parameters
        ----------
        interpolation : `cobyqa.models.Interpolation`
            Interpolation set.
        values : `numpy.ndarray`, shape (npt,)
            Values of the interpolated function at the interpolation points.

        Returns
        -------
        float
            Constant term of the quadratic model.
        `numpy.ndarray`, shape (n,)
            Gradient of the quadratic model at ``interpolation.x_base``.
        `numpy.ndarray`, shape (npt,)
            Implicit Hessian matrix of the quadratic model.

        Raises
        ------
        `numpy.linalg.LinAlgError`
            If the interpolation system is ill-defined.
        re   r   r   N)	r8   r=   r$   rc   r   r   blockr   rX   )rZ   rm   r    r=   rs   r   s         r2   rh   zQuadratic._get_modelD  s    4 || 
 
 	10	1 
 ""((3&44HH Q a

? ay!C!GHaK.!DSD!G*oEEr4   N)rD   rE   rF   rG   r3   ru   rH   r    r=   r{   r~   rz   r   r   r   staticmethodr   rh   rJ   r4   r2   rc   rc      s     2D
: 	 	 	! 	!B*
$
2
02h*0 >
 >
@ (F (Fr4   rc   c                   :   e Zd ZdZd Zed        Zed        Zed        Zed        Z	ed        Z
ed        Zed	        Zed
        Zd Zd Zd Zd Zd Zd Zd#dZd#dZd#dZd#dZd#dZd#dZd#dZd#dZd#dZd#dZd Zd Zd#dZ d Z!d#d Z"d#d!Z#d" Z$y)$Modelsz6
    Models for a nonlinear optimization problem.
    c           	         |t         j                     | _        t        ||      | _        | j
                  j                  d      } |||      \  }}}t        j                  |t         j                     t        j                        | _        t        j                  |t         j                     |j                  ft        j                        | _        t        j                  |t         j                     |j                  ft        j                        | _        t        |t         j                           D ]  }||t         j                      k\  rt"        |dk(  r6|| j$                  |<   || j&                  |ddf<   || j(                  |ddf<   nW| j
                  j                  |      } |||      \  | j$                  |<   | j&                  |ddf<   | j(                  |ddf<   |j*                  rh|j-                  | j
                  j                  |      | j&                  |ddf   | j(                  |ddf         |t         j.                     k  rt0        | j                  |   |t         j2                     k  sK|j-                  | j
                  j                  |      | j&                  |ddf   | j(                  |ddf         |t         j.                     k  st4         t7        | j
                  | j                  |t         j                           | _        t        j:                  | j<                  t6              | _        t        j:                  | j@                  t6              | _!        t        | j<                        D ]H  }	t7        | j
                  | j&                  dd|	f   |t         j                           | j>                  |	<   J t        | j@                        D ]H  }	t7        | j
                  | j(                  dd|	f   |t         j                           | jB                  |	<   J | j                  r| jE                          yy)aE  
        Initialize the models.

        Parameters
        ----------
        pb : `cobyqa.problem.Problem`
            Problem to be solved.
        options : dict
            Options of the solver.
        penalty : float
            Penalty parameter used to select the point in the filter to forward
            to the callback function.

        Raises
        ------
        `cobyqa.utils.MaxEvalError`
            If the maximum number of evaluations is reached.
        `cobyqa.utils.TargetSuccess`
            If a nearly feasible point has been found with an objective
            function value below the target.
        `cobyqa.utils.FeasibleSuccess`
            If a feasible point has been found for a feasibility problem.
        `numpy.linalg.LinAlgError`
            If the interpolation system is ill-defined.
        r   N)dtype)#r   r   r   r
   _interpolationrZ   rC   r   fullr!   nan_fun_valrw   _cub_val_ceq_valr#   MAX_EVALr   fun_valcub_valceq_valis_feasibilitymaxcvFEASIBILITY_TOLr   TARGETr   rc   _funrY   m_nonlinear_ub_cubm_nonlinear_eq_ceq_check_interpolation_conditions)
r&   r'   r(   penaltyx_evalfun_initcub_initceq_initr.   is
             r2   r3   zModels.__init__u  sx   6 gmm,+B8 ##))!,')&'':$(H 4bff=!5x}} ErvvN!5x}} ErvvNww{{+, &	$AGG,,--""Av"*Q%-QT"%-QT"++11!4JLKGQad!3T\\!Q$5G !!HH&&,,Q/LLA&LLA&
 72234 &%
 a GGNN$;;HH&&,,Q/LLA&LLA&
 72234 $#M&	$R MMGMM"
	
 HHT00	B	HHT00	B	t**+ 	A$""QT"&DIIaL	 t**+ 	A$""QT"&DIIaL	 ;;002 r4   c                 .    | j                   j                  S )z~
        Dimension of the problem.

        Returns
        -------
        int
            Dimension of the problem.
        )rZ   r    r9   s    r2   r    zModels.n  s     !!###r4   c                 .    | j                   j                  S )r<   )rZ   r=   r9   s    r2   r=   z
Models.npt  s     !!%%%r4   c                 4    | j                   j                  d   S )z
        Number of nonlinear inequality constraints.

        Returns
        -------
        int
            Number of nonlinear inequality constraints.
        r   )r   r8   r9   s    r2   r   zModels.m_nonlinear_ub       ||!!!$$r4   c                 4    | j                   j                  d   S )z
        Number of nonlinear equality constraints.

        Returns
        -------
        int
            Number of nonlinear equality constraints.
        r   )r   r8   r9   s    r2   r   zModels.m_nonlinear_eq  r   r4   c                     | j                   S )z
        Interpolation set.

        Returns
        -------
        `cobyqa.models.Interpolation`
            Interpolation set.
        )r   r9   s    r2   rZ   zModels.interpolation  s     """r4   c                     | j                   S )z
        Values of the objective function at the interpolation points.

        Returns
        -------
        `numpy.ndarray`, shape (npt,)
            Values of the objective function at the interpolation points.
        )r   r9   s    r2   r   zModels.fun_val  s     }}r4   c                     | j                   S )a1  
        Values of the nonlinear inequality constraint functions at the
        interpolation points.

        Returns
        -------
        `numpy.ndarray`, shape (npt, m_nonlinear_ub)
            Values of the nonlinear inequality constraint functions at the
            interpolation points.
        )r   r9   s    r2   r   zModels.cub_val        }}r4   c                     | j                   S )a-  
        Values of the nonlinear equality constraint functions at the
        interpolation points.

        Returns
        -------
        `numpy.ndarray`, shape (npt, m_nonlinear_eq)
            Values of the nonlinear equality constraint functions at the
            interpolation points.
        )r   r9   s    r2   r   zModels.ceq_val.  r   r4   c                     | j                   r!|j                  | j                  fk(  sJ d       | j                  || j                        S )a  
        Evaluate the quadratic model of the objective function at a given
        point.

        Parameters
        ----------
        x : `numpy.ndarray`, shape (n,)
            Point at which to evaluate the quadratic model of the objective
            function.

        Returns
        -------
        float
            Value of the quadratic model of the objective function at `x`.
        rq   )r   r8   r    r   rZ   r&   rs   s     r2   funz
Models.fun<  s@      ;;77tvvi'I)II'yyD..//r4   c                     | j                   r!|j                  | j                  fk(  sJ d       | j                  j	                  || j
                        S )a  
        Evaluate the gradient of the quadratic model of the objective function
        at a given point.

        Parameters
        ----------
        x : `numpy.ndarray`, shape (n,)
            Point at which to evaluate the gradient of the quadratic model of
            the objective function.

        Returns
        -------
        `numpy.ndarray`, shape (n,)
            Gradient of the quadratic model of the objective function at `x`.
        rq   )r   r8   r    r   r{   rZ   r   s     r2   fun_gradzModels.fun_gradP  sD      ;;77tvvi'I)II'yy~~a!3!344r4   c                 L    | j                   j                  | j                        S )z
        Evaluate the Hessian matrix of the quadratic model of the objective
        function.

        Returns
        -------
        `numpy.ndarray`, shape (n, n)
            Hessian matrix of the quadratic model of the objective function.
        )r   r~   rZ   r9   s    r2   fun_hesszModels.fun_hessd  s     yy~~d0011r4   c                     | j                   r!|j                  | j                  fk(  sJ d       | j                  j	                  || j
                        S )a'  
        Evaluate the right product of the Hessian matrix of the quadratic model
        of the objective function with a given vector.

        Parameters
        ----------
        v : `numpy.ndarray`, shape (n,)
            Vector with which the Hessian matrix of the quadratic model of the
            objective function is multiplied from the right.

        Returns
        -------
        `numpy.ndarray`, shape (n,)
            Right product of the Hessian matrix of the quadratic model of the
            objective function with `v`.
        r   )r   r8   r    r   rz   rZ   r&   r   s     r2   fun_hess_prodzModels.fun_hess_prodp  sF    " ;;77tvvi'I)II'yy""1d&8&899r4   c                     | j                   r!|j                  | j                  fk(  sJ d       | j                  j	                  || j
                        S )a  
        Evaluate the curvature of the quadratic model of the objective function
        along a given direction.

        Parameters
        ----------
        v : `numpy.ndarray`, shape (n,)
            Direction along which the curvature of the quadratic model of the
            objective function is evaluated.

        Returns
        -------
        float
            Curvature of the quadratic model of the objective function along
            `v`.
        r   )r   r8   r    r   r   rZ   r   s     r2   fun_curvzModels.fun_curv  sD    " ;;77tvvi'I)II'yy~~a!3!344r4   c                     | j                   r!|j                  | j                  fk(  sJ d       t        | j                  | j
                  | j                         }|j                  || j                        S )ap  
        Evaluate the gradient of the alternative quadratic model of the
        objective function at a given point.

        Parameters
        ----------
        x : `numpy.ndarray`, shape (n,)
            Point at which to evaluate the gradient of the alternative
            quadratic model of the objective function.

        Returns
        -------
        `numpy.ndarray`, shape (n,)
            Gradient of the alternative quadratic model of the objective
            function at `x`.

        Raises
        ------
        `numpy.linalg.LinAlgError`
            If the interpolation system is ill-defined.
        rq   )r   r8   r    rc   rZ   r   r{   )r&   rs   models      r2   fun_alt_gradzModels.fun_alt_grad  s\    , ;;77tvvi'I)II'$,,dllDKKHzz!T//00r4   Nc           	      2   | j                   rD|j                  | j                  fk(  sJ d       |!|j                  | j                  fk(  sJ d       t	        j
                  | j                  |      D cg c]  } ||| j                         c}      S c c}w )a;  
        Evaluate the quadratic models of the nonlinear inequality functions at
        a given point.

        Parameters
        ----------
        x : `numpy.ndarray`, shape (n,)
            Point at which to evaluate the quadratic models of the nonlinear
            inequality functions.
        mask : `numpy.ndarray`, shape (m_nonlinear_ub,), optional
            Mask of the quadratic models to consider.

        Returns
        -------
        `numpy.ndarray`
            Values of the quadratic model of the nonlinear inequality
            functions.
        rq   !The shape of `mask` is not valid.)r   r8   r    r   r   array_get_cubrZ   r&   rs   maskr   s       r2   cubz
Models.cub  s    & ;;77tvvi'I)II'<4::##2 $ 323  xx7;}}T7JKeU1d(()K
 	
K   3Bc           	      ^   | j                   rD|j                  | j                  fk(  sJ d       |!|j                  | j                  fk(  sJ d       t	        j
                  | j                  |      D cg c]  }|j                  || j                          c}d| j                  f      S c c}w )a`  
        Evaluate the gradients of the quadratic models of the nonlinear
        inequality functions at a given point.

        Parameters
        ----------
        x : `numpy.ndarray`, shape (n,)
            Point at which to evaluate the gradients of the quadratic models of
            the nonlinear inequality functions.
        mask : `numpy.ndarray`, shape (m_nonlinear_eq,), optional
            Mask of the quadratic models to consider.

        Returns
        -------
        `numpy.ndarray`
            Gradients of the quadratic model of the nonlinear inequality
            functions.
        rq   r   )	r   r8   r    r   r   reshaper   r{   rZ   r   s       r2   cub_gradzModels.cub_grad      & ;;77tvvi'I)II'<4::##2 $ 323  zz---/ ZZ4--. /L
 	
/   3#B*c                 0   | j                   r#|!|j                  | j                  fk(  sJ d       t        j                  | j                  |      D cg c]  }|j                  | j                         c}d| j                  | j                  f      S c c}w )a  
        Evaluate the Hessian matrices of the quadratic models of the nonlinear
        inequality functions.

        Parameters
        ----------
        mask : `numpy.ndarray`, shape (m_nonlinear_ub,), optional
            Mask of the quadratic models to consider.

        Returns
        -------
        `numpy.ndarray`
            Hessian matrices of the quadratic models of the nonlinear
            inequality functions.
        r   r   )	r   r8   r   r   r   r   r~   rZ   r    r&   r   r   s      r2   cub_hesszModels.cub_hess        ;;<4::##2 $ 323  zz9=t9LMUZZ**+M 
 	
M   "Bc           	      ^   | j                   rD|j                  | j                  fk(  sJ d       |!|j                  | j                  fk(  sJ d       t	        j
                  | j                  |      D cg c]  }|j                  || j                          c}d| j                  f      S c c}w )a  
        Evaluate the right product of the Hessian matrices of the quadratic
        models of the nonlinear inequality functions with a given vector.

        Parameters
        ----------
        v : `numpy.ndarray`, shape (n,)
            Vector with which the Hessian matrices of the quadratic models of
            the nonlinear inequality functions are multiplied from the right.
        mask : `numpy.ndarray`, shape (m_nonlinear_ub,), optional
            Mask of the quadratic models to consider.

        Returns
        -------
        `numpy.ndarray`
            Right products of the Hessian matrices of the quadratic models of
            the nonlinear inequality functions with `v`.
        r   r   r   )	r   r8   r    r   r   r   r   rz   rZ   r&   r   r   r   s       r2   cub_hess_prodzModels.cub_hess_prod      & ;;77tvvi'I)II'<4::##2 $ 323  zz "]]40 4#5#56 L
 	
r   c           	      D   | j                   rD|j                  | j                  fk(  sJ d       |!|j                  | j                  fk(  sJ d       t	        j
                  | j                  |      D cg c]  }|j                  || j                          c}      S c c}w )az  
        Evaluate the curvature of the quadratic models of the nonlinear
        inequality functions along a given direction.

        Parameters
        ----------
        v : `numpy.ndarray`, shape (n,)
            Direction along which the curvature of the quadratic models of the
            nonlinear inequality functions is evaluated.
        mask : `numpy.ndarray`, shape (m_nonlinear_ub,), optional
            Mask of the quadratic models to consider.

        Returns
        -------
        `numpy.ndarray`
            Curvature of the quadratic models of the nonlinear inequality
            functions along `v`.
        r   r   )	r   r8   r    r   r   r   r   r   rZ   r   s       r2   cub_curvzModels.cub_curv(      & ;;77tvvi'I)II'<4::##2 $ 323  xx---/ ZZ4--. /
 	
/   3#Bc           	      2   | j                   rD|j                  | j                  fk(  sJ d       |!|j                  | j                  fk(  sJ d       t	        j
                  | j                  |      D cg c]  } ||| j                         c}      S c c}w )a)  
        Evaluate the quadratic models of the nonlinear equality functions at a
        given point.

        Parameters
        ----------
        x : `numpy.ndarray`, shape (n,)
            Point at which to evaluate the quadratic models of the nonlinear
            equality functions.
        mask : `numpy.ndarray`, shape (m_nonlinear_eq,), optional
            Mask of the quadratic models to consider.

        Returns
        -------
        `numpy.ndarray`
            Values of the quadratic model of the nonlinear equality functions.
        rq   r   )r   r8   r    r   r   r   _get_ceqrZ   r   s       r2   ceqz
Models.ceqE  s    $ ;;77tvvi'I)II'<4::##2 $ 323  xx7;}}T7JKeU1d(()K
 	
Kr   c           	      ^   | j                   rD|j                  | j                  fk(  sJ d       |!|j                  | j                  fk(  sJ d       t	        j
                  | j                  |      D cg c]  }|j                  || j                          c}d| j                  f      S c c}w )aZ  
        Evaluate the gradients of the quadratic models of the nonlinear
        equality functions at a given point.

        Parameters
        ----------
        x : `numpy.ndarray`, shape (n,)
            Point at which to evaluate the gradients of the quadratic models of
            the nonlinear equality functions.
        mask : `numpy.ndarray`, shape (m_nonlinear_eq,), optional
            Mask of the quadratic models to consider.

        Returns
        -------
        `numpy.ndarray`
            Gradients of the quadratic model of the nonlinear equality
            functions.
        rq   r   r   )	r   r8   r    r   r   r   r   r{   rZ   r   s       r2   ceq_gradzModels.ceq_grad`  r   r   c                 0   | j                   r#|!|j                  | j                  fk(  sJ d       t        j                  | j                  |      D cg c]  }|j                  | j                         c}d| j                  | j                  f      S c c}w )a  
        Evaluate the Hessian matrices of the quadratic models of the nonlinear
        equality functions.

        Parameters
        ----------
        mask : `numpy.ndarray`, shape (m_nonlinear_eq,), optional
            Mask of the quadratic models to consider.

        Returns
        -------
        `numpy.ndarray`
            Hessian matrices of the quadratic models of the nonlinear equality
            functions.
        r   r   )	r   r8   r   r   r   r   r~   rZ   r    r   s      r2   ceq_hesszModels.ceq_hess~  r   r   c           	      ^   | j                   rD|j                  | j                  fk(  sJ d       |!|j                  | j                  fk(  sJ d       t	        j
                  | j                  |      D cg c]  }|j                  || j                          c}d| j                  f      S c c}w )a  
        Evaluate the right product of the Hessian matrices of the quadratic
        models of the nonlinear equality functions with a given vector.

        Parameters
        ----------
        v : `numpy.ndarray`, shape (n,)
            Vector with which the Hessian matrices of the quadratic models of
            the nonlinear equality functions are multiplied from the right.
        mask : `numpy.ndarray`, shape (m_nonlinear_eq,), optional
            Mask of the quadratic models to consider.

        Returns
        -------
        `numpy.ndarray`
            Right products of the Hessian matrices of the quadratic models of
            the nonlinear equality functions with `v`.
        r   r   r   )	r   r8   r    r   r   r   r   rz   rZ   r   s       r2   ceq_hess_prodzModels.ceq_hess_prod  r   r   c           	      D   | j                   rD|j                  | j                  fk(  sJ d       |!|j                  | j                  fk(  sJ d       t	        j
                  | j                  |      D cg c]  }|j                  || j                          c}      S c c}w )at  
        Evaluate the curvature of the quadratic models of the nonlinear
        equality functions along a given direction.

        Parameters
        ----------
        v : `numpy.ndarray`, shape (n,)
            Direction along which the curvature of the quadratic models of the
            nonlinear equality functions is evaluated.
        mask : `numpy.ndarray`, shape (m_nonlinear_eq,), optional
            Mask of the quadratic models to consider.

        Returns
        -------
        `numpy.ndarray`
            Curvature of the quadratic models of the nonlinear equality
            functions along `v`.
        r   r   )	r   r8   r    r   r   r   r   r   rZ   r   s       r2   ceq_curvzModels.ceq_curv  r   r   c                    t        | j                  | j                  | j                        | _        t        | j                        D ]A  }t        | j                  | j                  dd|f   | j                        | j                  |<   C t        | j                        D ]A  }t        | j                  | j                  dd|f   | j                        | j                  |<   C | j                  r| j                          yy)a9  
        Set the quadratic models of the objective function, nonlinear
        inequality constraints, and nonlinear equality constraints to the
        alternative quadratic models.

        Raises
        ------
        `numpy.linalg.LinAlgError`
            If the interpolation system is ill-defined.
        N)rc   rZ   r   r   r   r#   r   r   r   r   r   r   r   )r&   r   s     r2   reset_modelszModels.reset_models  s     d00$,,L	t**+ 	A$""QT"DIIaL	 t**+ 	A$""QT"DIIaL	 ;;002 r4   c           	         | j                   rd|cxk  r| j                  k  sJ d        J d       |j                  | j                  fk(  sJ d       t	        |t
              sJ d       |j                  | j                  fk(  sJ d       |j                  | j                  fk(  sJ d       t        j                  | j                        }t        j                  | j                  j                        }t        j                  | j                  j                        }|| j                  |      z
  ||<   || j                  |      z
  ||ddf<   || j                  |      z
  ||ddf<   || j                  |<   || j                  |ddf<   || j                  |ddf<   t        j                   | j"                  j$                  dd|f         }	|| j"                  j&                  z
  | j"                  j$                  dd|f<   | j(                  j+                  | j"                  ||	|      }
t-        | j                        D ]8  }|
xs2 | j.                  |   j+                  | j"                  ||	|dd|f         }
: t-        | j                        D ]8  }|
xs2 | j0                  |   j+                  | j"                  ||	|dd|f         }
: | j                   r| j3                          |
S )a  
        Update the interpolation set.

        This method updates the interpolation set by replacing the `knew`-th
        interpolation point with `xnew`. It also updates the function values
        and the quadratic models.

        Parameters
        ----------
        k_new : int
            Index of the updated interpolation point.
        x_new : `numpy.ndarray`, shape (n,)
            New interpolation point. Its value is interpreted as relative to
            the origin, not the base point.
        fun_val : float
            Value of the objective function at `x_new`.
            Objective function value at `x_new`.
        cub_val : `numpy.ndarray`, shape (m_nonlinear_ub,)
            Values of the nonlinear inequality constraints at `x_new`.
        ceq_val : `numpy.ndarray`, shape (m_nonlinear_eq,)
            Values of the nonlinear equality constraints at `x_new`.

        Raises
        ------
        `numpy.linalg.LinAlgError`
            If the interpolation system is ill-defined.
        r   r   "The shape of `x_new` is not valid.z The function value is not valid.z$The shape of `cub_val` is not valid.z$The shape of `ceq_val` is not valid.N)r   r=   r8   r    
isinstancefloatr   r   r   r   r   r   r   r   r   r   r   rZ   r$   r   r   r   r#   r   r   r   )r&   r   x_newr   r   r   fun_diffcub_diffceq_diffr   r   r   s               r2   update_interpolationzModels.update_interpolation  s   8 ;;((K*KK(K*KK(;;466)+ 545+gu- 323-==##%  656  ==##%  656 
 88DHH%88DLL../88DLL../!DHHUO3$txx6$txx6 &U!(UAX!(UAX ''$,,00E:;+043E3E3L3L+Lq%x( ))**	
 t**+ 	A- 11D1D""A	2O	 t**+ 	A- 11D1D""A	2O	 ;;002r4   c                 f   | j                   rG|j                  | j                  fk(  sJ d       |$d|cxk  r| j                  k  sJ d        J d       || j                  j
                  z
  }t        j                  | j                  | j                  z   dz   df      }d| j                  j                  j                  |z  dz  z  |d| j                  df<   d|| j                  df<   ||| j                  dz   ddf<   t        j                  | j                  |      d   }d||z  dz  z  |dddf   |dddf   z  z
  }|t        j                  | j                  | j                  z   dz   | j                        }t        j                  t        j                  | j                  |      d         }|d| j                  df   }	nat        j                  | j                  | j                  z   dz   d|       }t        j                  | j                  |      d   |df   }||df   }	||z  |	dz  z   S )	a  
        Compute the normalized determinants of the new interpolation systems.

        Parameters
        ----------
        x_new : `numpy.ndarray`, shape (n,)
            New interpolation point. Its value is interpreted as relative to
            the origin, not the base point.
        k_new : int, optional
            Index of the updated interpolation point. If `k_new` is not
            specified, all the possible determinants are computed.

        Returns
        -------
        {float, `numpy.ndarray`, shape (npt,)}
            Determinant(s) of the new interpolation system.

        Raises
        ------
        `numpy.linalg.LinAlgError`
            If the interpolation system is ill-defined.

        Notes
        -----
        The determinants are normalized by the determinant of the current
        interpolation system. For stability reasons, the calculations are done
        using the formula (2.12) in [1]_.

        References
        ----------
        .. [1] M. J. D. Powell. On updating the inverse of a KKT matrix.
           Technical Report DAMTP 2004/NA01, Department of Applied Mathematics
           and Theoretical Physics, University of Cambridge, Cambridge, UK,
           2004.
        r  Nr   r   r   r   r   rQ   )r   r8   r    r=   rZ   r   r   rY   r$   rX   rc   r   eyediag)
r&   r  r   r   new_colinv_new_colbeta	coord_vecalphataus
             r2   determinantszModels.determinantsB  sI   H ;;;;466)+ 545+ e!6dhh!61016!61016 **111((DHHtvv-1156t))--//%7C?? 	
$((
A"!$)1q !--d.@.@'J1Meem++gadmk!Q$>O.OO =txx$&&014dhh?IGG''&& E jj!m,Ctxx$&&014a%@I++"" 	
 QhE eQh'Ct|c3h&&r4   c                 ~   | j                   r!|j                  | j                  fk(  sJ d       | j                  j	                  | j
                  |       | j                  D ]  }|j	                  | j
                  |         | j                  D ]  }|j	                  | j
                  |         || j
                  j                  z
  }| j
                  xj                  |z  c_        | j
                  xj                  |ddt        j                  f   z  c_	        |t        j                     r| j                          yy)z
        Shift the base point without changing the interpolation set.

        Parameters
        ----------
        new_x_base : `numpy.ndarray`, shape (n,)
            New base point.
        options : dict
            Options of the solver.
        r   N)r   r8   r    r   r   rZ   r   r   r   r$   r   r}   r   r   r   )r&   r   r(   r   r   s        r2   r   zModels.shift_x_base  s    ;;##(  989 
 			t11:>YY 	?Et11:>	?YY 	?Et11:>	? T//666!!U*!%2::"667==!002 "r4   c                 <    || j                   S | j                   |   S )ao  
        Get the quadratic models of the nonlinear inequality constraints.

        Parameters
        ----------
        mask : `numpy.ndarray`, shape (m_nonlinear_ub,), optional
            Mask of the quadratic models to return.

        Returns
        -------
        `numpy.ndarray`
            Quadratic models of the nonlinear inequality constraints.
        )r   r&   r   s     r2   r   zModels._get_cub       !Ltyy=diio=r4   c                 <    || j                   S | j                   |   S )ak  
        Get the quadratic models of the nonlinear equality constraints.

        Parameters
        ----------
        mask : `numpy.ndarray`, shape (m_nonlinear_eq,), optional
            Mask of the quadratic models to return.

        Returns
        -------
        `numpy.ndarray`
            Quadratic models of the nonlinear equality constraints.
        )r   r  s     r2   r   zModels._get_ceq  r  r4   c                    d}d}d}t        | j                        D ]1  }t        j                  |t        j                  | j                  | j                  j                  |            | j                  |   z
        g      }t        j                  t        j                  | j                  | j                  j                  |            | j                  |ddf   z
        |      }t        j                  t        j                  | j                  | j                  j                  |            | j                  |ddf   z
        |      }4 dt        j                  t              z  t        | j                  | j                        z  }||t        j                  t        j                  | j                        d      z  kD  rt!        j"                  dt$        d       ||t        j                  t        j                  | j                        d      z  kD  rt!        j"                  dt$        d       ||t        j                  t        j                  | j                        d      z  kD  rt!        j"                  d	t$        d       yy)
zM
        Check the interpolation conditions of all quadratic models.
        r   NrO   g      $@rQ   zJThe interpolation conditions for the objective function are not satisfied.r   zVThe interpolation conditions for the inequality constraint function are not satisfied.zTThe interpolation conditions for the equality constraint function are not satisfied.)r#   r=   r   rT   r   r   rZ   rC   r   r   r   r   r   sqrtrW   r    warningswarnRuntimeWarning)r&   	error_fun	error_cub	error_ceqr.   tols         r2   r   z&Models._check_interpolation_conditions  s    			txx 	AFF!3!3!9!9!!<=QOI HHT//55a89DLLA<NN "	I HHT//55a89DLLA<NN "	I	* RWWS\!C$99sRVVBFF4<<$8#FFFMM!	 sRVVBFF4<<$8#FFFMM.	 sRVVBFF4<<$8#FFFMM.	 Gr4   )N)%rD   rE   rF   rG   r3   rH   r    r=   r   r   rZ   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r   r   r   r   rJ   r4   r2   r   r   p  s'   a3F 	$ 	$ 	& 	& 	% 	% 	% 	% 	# 	# 	 	    0(5(
2:*5*16
8
<
2
@
:
6
<
2
@
:36QfH'T3<> > 1r4   r   )r  numpyr   scipy.linalgr   settingsr   utilsr   r   r   finfor  epsrW   r
   ra   rc   r   rJ   r4   r2   <module>r+     s\        ? ? bhhuos, s,l27juF uFpK Kr4   