
    4[g"                         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dd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                 "   |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<    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)selfpboptions
max_radiusvery_close_xl_idxclose_xl_idxvery_close_xu_idxclose_xu_idxkspreadk1k2s               S/var/www/html/bid-api/venv/lib/python3.12/site-packages/scipy/_lib/cobyqa/models.py__init__zInterpolation.__init__   sJ    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''++./A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% 0    c                 4    | j                   j                  d   S )t
        Number of variables.

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

        Returns
        -------
        int
            Number of interpolation points.
        r   r6   r8   s    r1   nptzInterpolation.npth   r9   r3   c                     | j                   S )z
        Interpolation points.

        Returns
        -------
        `numpy.ndarray`, shape (n, npt)
            Interpolation points.
        )r"   r8   s    r1   r$   zInterpolation.xptt   s     yyr3   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   r7   r    r<   r"   )r%   r$   s     r1   r$   zInterpolation.xpt   sG     ;;99!  2 22  	r3   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   r8   s    r1   r   zInterpolation.x_base   s     ||r3   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   r7   r    r   )r%   r   s     r1   r   zInterpolation.x_base   s>     ;;<<$  545  r3   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     r1   pointzInterpolation.point   sO      ;;$DHH$C&CC$C&CC${{TXXad^++r3   N)__name__
__module____qualname____doc__r2   propertyr    r<   r$   setterr   rB    r3   r1   r
   r
      s    D7L 	! 	! 	! 	! 	 	 	ZZ   	 	 ]] ,r3   r
   )r$   aright_scalingr   c                    t         d   At        j                  | j                  t         d         rt         d   t         d   t         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         d<   t        j                  |      t         d<   t        j                  |      t         d<   ||ft         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.
    r$   NrJ   rK   r   r   )axisinitialr   r   r         ?F)check_finite)_cacher   array_equalr$   maxlinalgnormEPSr7   r   Temptyr   r   )	interpolationscale	xpt_scaler    r<   rJ   rK   
eig_valueseig_vectorss	            r1   build_systemr_      s   " e} R^^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GGM--.F5M''!*F3K ggm4F? +.F6Nmj+666r3   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   r7   r<   r    
ValueError
_get_model_const_grad_i_hessr   r   _e_hess)r%   rZ   valuesdebug_s        r1   r2   zQuadratic.__init__  s    $ ;;<<!!$  545  }22F ??Q&'q*  48??4
0TZq xx 01r3   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   r7   r    r   rg   rh   ri   r$   rX   rj   r%   xrZ   x_diffs       r1   __call__zQuadratic.__call__(  s      ;;77tvvi'I)II'])))KKjj6!" 1 1 3 3f <DD4<<'&01	
r3   c                 .    | j                   j                  S )r5   )rh   sizer8   s    r1   r    zQuadratic.nE  s     zzr3   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.
        )ri   ru   r8   s    r1   r<   zQuadratic.nptQ  s     ||   r3   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`.
        ro   )r   r7   r    r   rh   	hess_prodrp   s       r1   gradzQuadratic.grad]  sS      ;;77tvvi'I)II'])))zzDNN6=AAAr3   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)rj   r$   ri   r   newaxisrX   )r%   rZ   s     r1   hesszQuadratic.hessr  sG     ||m//LLBJJ'-*;*;*=*==
 
 	
r3   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   r7   r    rj   r$   ri   rX   r%   vrZ   s      r1   rx   zQuadratic.hess_prod  si    & ;;77tvvi'I)II'||a-"3"3LLM--//!34#
 
 	
r3   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   r7   r    rj   ri   r$   rX   r   s      r1   curvzQuadratic.curv  sf    " ;;77tvvi'I)II'q llm//11A5#==>	
r3   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<   r7   r    rj   ri   r   outerrf   rg   rh   )	r%   rZ   k_newdir_oldvalues_diffconstry   i_hessill_conditioneds	            r1   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
r3   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   r7   r    rg   ry   rh   r   r   r   r$   r{   ri   rj   rX   )r%   rZ   
new_x_baseshiftr   s        r1   shift_x_basezQuadratic.shift_x_base  s     ;;##(  989  :}5YYz=9
]111uQ

]';!;;t||K
 	))r3   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.rP   )r$   r7   ndimr_   r   r{   allisfiniterU   LinAlgErrorabsrW   rX   )rZ   rhsr    r<   rJ   rK   eig
rhs_scaledr]   r^   large_eig_valuesinv_eig_valuesr   left_scaled_solutionss                 r1   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-$@@
 	
r3   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.
        rc   r   r   N)	r7   r<   r$   ra   r   r   blockr   rX   )rZ   rk   r    r<   rq   r   s         r1   rf   zQuadratic._get_modelB  s    4 || 
 
 	10	1 
 ""((3&44HH Q a

? ay!C!GHaK.!DSD!G*oEEr3   N)rC   rD   rE   rF   r2   rs   rG   r    r<   ry   r|   rx   r   r   r   staticmethodr   rf   rI   r3   r1   ra   ra      s     2D
: 	 	 	! 	!B*
$
2
02h*0 >
 >
@ (F (Fr3   ra   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<   nV| 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J|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)a  
        Initialize the models.

        Parameters
        ----------
        pb : `cobyqa.problem.Problem`
            Problem to be solved.
        options : dict
            Options of the solver.

        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   rB   r   fullr!   nan_fun_valru   _cub_val_ceq_valr#   MAX_EVALr   fun_valcub_valceq_valis_feasibilitymaxcvFEASIBILITY_TOLr   TARGETr   ra   _funrY   m_nonlinear_ub_cubm_nonlinear_eq_ceq_check_interpolation_conditions)	r%   r&   r'   x_evalfun_initcub_initceq_initr-   is	            r1   r2   zModels.__init__s  sc   0 gmm,+B8 ##))!,')&z$(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 $#K -P MMGMM"
	
 HHT00	B	HHT00	B	t**+A$""QT"&DIIaL , t**+A$""QT"&DIIaL , ;;002 r3   c                 .    | j                   j                  S )z~
        Dimension of the problem.

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

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

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

        Returns
        -------
        `cobyqa.models.Interpolation`
            Interpolation set.
        )r   r8   s    r1   rZ   zModels.interpolation  s     """r3   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   r8   s    r1   r   zModels.fun_val  s     }}r3   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   r8   s    r1   r   zModels.cub_val       }}r3   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   r8   s    r1   r   zModels.ceq_val(  r   r3   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`.
        ro   )r   r7   r    r   rZ   r%   rq   s     r1   funz
Models.fun6  s@      ;;77tvvi'I)II'yyD..//r3   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`.
        ro   )r   r7   r    r   ry   rZ   r   s     r1   fun_gradzModels.fun_gradJ  sD      ;;77tvvi'I)II'yy~~a!3!344r3   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   r8   s    r1   fun_hesszModels.fun_hess^  s     yy~~d0011r3   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   r7   r    r   rx   rZ   r%   r   s     r1   fun_hess_prodzModels.fun_hess_prodj  sF    " ;;77tvvi'I)II'yy""1d&8&899r3   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   r7   r    r   r   rZ   r   s     r1   fun_curvzModels.fun_curv  sD    " ;;77tvvi'I)II'yy~~a!3!344r3   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.
        ro   )r   r7   r    ra   rZ   r   ry   )r%   rq   models      r1   fun_alt_gradzModels.fun_alt_grad  s\    , ;;77tvvi'I)II'$,,dllDKKHzz!T//00r3   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.
        ro   !The shape of `mask` is not valid.)r   r7   r    r   r   array_get_cubrZ   r%   rq   maskr   s       r1   cubz
Models.cub  s    & ;;77tvvi'I)II'<4::##2 $ 323  xx7;}}T7JK7JeU1d(()7J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.
        ro   r   )	r   r7   r    r   r   reshaper   ry   rZ   r   s       r1   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   r7   r   r   r   r   r|   rZ   r    r%   r   r   s      r1   cub_hesszModels.cub_hess        ;;<4::##2 $ 323  zz9=t9LM9LUZZ**+9L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   r7   r    r   r   r   r   rx   rZ   r%   r   r   r   s       r1   cub_hess_prodzModels.cub_hess_prod      & ;;77tvvi'I)II'<4::##2 $ 323  zz "]]400E 4#5#560 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   r7   r    r   r   r   r   r   rZ   r   s       r1   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.
        ro   r   )r   r7   r    r   r   r   _get_ceqrZ   r   s       r1   ceqz
Models.ceq?  s    $ ;;77tvvi'I)II'<4::##2 $ 323  xx7;}}T7JK7JeU1d(()7J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.
        ro   r   r   )	r   r7   r    r   r   r   r   ry   rZ   r   s       r1   ceq_gradzModels.ceq_gradZ  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   r7   r   r   r   r   r|   rZ   r    r   s      r1   ceq_hesszModels.ceq_hessx  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   r7   r    r   r   r   r   rx   rZ   r   s       r1   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   r7   r    r   r   r   r   r   rZ   r   s       r1   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)ra   rZ   r   r   r   r#   r   r   r   r   r   r   r   )r%   r   s     r1   reset_modelszModels.reset_models  s     d00$,,L	t**+A$""QT"DIIaL , t**+A$""QT"DIIaL , ;;002 r3   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<   r7   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               r1   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r3   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   rP   )r   r7   r    r<   rZ   r   r   rY   r$   rX   ra   r   eyediag)
r%   r  r   r   new_colinv_new_colbeta	coord_vecalphataus
             r1   determinantszModels.determinants<  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&&r3   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   r7   r    r   r   rZ   r   r   r   r$   r   r{   r   r   r   )r%   r   r'   r   r   s        r1   r   zModels.shift_x_base  s    ;;##(  989 
 			t11:>YYEt11:> YYEt11:>  T//666!!U*!%2::"667==!002 "r3   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     r1   r   zModels._get_cub       !Ltyy=diio=r3   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     r1   r   zModels._get_ceq  r  r3   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   NrN   g      $@rP   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   rB   r   r   r   r   r   sqrtrW   r    warningswarnRuntimeWarning)r%   	error_fun	error_cub	error_ceqr-   tols         r1   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r3   )N)%rC   rD   rE   rF   r2   rG   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   rI   r3   r1   r   r   n  s'   ]3~ 	$ 	$ 	& 	& 	% 	% 	% 	% 	# 	# 	 	    0(5(
2:*5*16
8
<
2
@
:
6
<
2
@
:36QfH'T3<> > 1r3   r   )r  numpyr   scipy.linalgr   settingsr   utilsr   r   r   finfor  epsrW   r
   rR   r_   ra   r   rI   r3   r1   <module>r(     sl        ? ? bhhuor, r,j D4	F.7buF uFpG Gr3   