
    ^Mh                         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dS )    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d
S )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                 |k    rL||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    rL||dz
           r!|t           j                  | j        |dz
  |f<   :|t           j                 | j        |dz
  |f<   Z|d|j        z  k    r|||j        z
  dz
           r+d|t           j                 z  | j        ||j        z
  dz
  |f<   |||j        z
  dz
           r+d|t           j                 z  | j        ||j        z
  dz
  |f<   |t           j                  | j        ||j        z
  dz
  |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S )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               X/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/scipy/_lib/cobyqa/models.py__init__zInterpolation.__init__   s    gm,26"),"=>>>
7>"Z//,6GGN(),.FGN+- -GGN() wru~~K29<#0G*GGG 	 *,6G)H%&IL3!8884;F[BIL77>+BBBD %'JIL&)@@IL&%
 %
L!
 K29<#0G*GGG 	 *,6G)H%&K"),ww~/F)FFFY\GGN33t{BD %'JIL&)@@IL&%
 %
L! HbdGGK$89::	q''+.// 	7 	7ABDyy$QU+ A*1'.*A)ADHQUAX&&)0)@DHQUAX&&a"$h$QX\2 I03ggn6M0MDHQX\1_--&q24x!|4 I04ww~7N0NDHQX\1_--181H0HDHQX\1_--bd(Q,24/!f*,,q06kRT)"&(2rAv:"6Q"&(2rAv:"6Q%	7 	7    c                 &    | j         j        d         S )t
        Number of variables.

        Returns
        -------
        int
            Number of variables.
        r   r$   shaper%   s    r1   r    zInterpolation.n\        x~a  r3   c                 &    | 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     yr3   c                 d    | j         r!|j        | j        | j        fk    s
J d            || _        dS )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   sR     ; 	29!    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                 X    | j         r|j        | j        fk    s
J d            || _        dS )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   sK     ; 	5<$   4   r3   c                 ~    | j         rd|cxk    r| j        k     sn 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   sZ      ; 	D$$$$DH$$$$$&C$$${TXaaad^++r3   N)__name__
__module____qualname____doc__r2   propertyr    r<   r$   setterr   rB    r3   r1   r
   r
      s         D7 D7 D7L 	! 	! X	! 	! 	! X	! 	 	 X	 	Z  Z  	 	 X	 ]  ], , , , ,r3   r
   )r$   aright_scalingr   c                    t           d         Kt          j        | j        t           d                   r&t           d         t           d         t           d         fS t          j        t          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F29>>-"3!><<cJJJE!E)I_FAs
#'A+sQw{+,,A9;2s::AdsdDSDjMAdsdCiL!AdsdC!GHHnAc4C4iL!AcAghhn HS1Wq[))Ms
*M$3$M##M#'(("15999JGM-..F5M'!**F3K gm4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dS )	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                 R   || _         | j         r|j        |j        fk    s
J d            |j        |j        dz   k     rt	          d|j        dz    d          |                     ||          \  | _        | _        | _        }t          j
        | j        | j        f          | _        dS )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   }222* ?Q&* * *   48??4
 4
0TZq x 011r3   c                     | 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      ; 	J7tvi''')I''']))Kj6!" 1 3f <DD4<'&01	
r3   c                     | j         j        S )r5   )rh   sizer8   s    r1   r    zQuadratic.nE  s     z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        |                     ||          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]  sW      ; 	J7tvi''')I''']))zDNN6=AAAAr3   c                 r    | 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  s<     |m/LBJ'-*;*==
 
 	
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  sb    & ; 	J7tvi''')I'''|a-"3LM-/!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  sb    " ; 	J7tvi''')I'''q lm/1A5#==>	
r3   c                    | j         rTd|cxk    r| j        k     sn 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        |<   |                     ||          \  }}}}| 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  s3   4 ; 	:(((((((((*K(((=%   5   $)   9   	U+bhw.H.HHH!U 040
 0
,tV_ 	u

d

r3   c                 ^   | j         r|j        | j        fk    s
J d             | ||          | _        |                     ||          | _        ||j        z
  }t          j        ||j	        d|ddt          j
        f         z  z
  | j        z            }| xj        ||j        z   z  c_        dS )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   d:}55YYz=99
]11uQQQ
]';!;;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        	                    d          |\  }}	t	          j
        |          t          k    }
|	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  ss   0 "(3HMMcilcAgk999- :99 !-] ; ;=# =BJ77
r{1~~&& 	26"+j2I2I+J+J 	)'':  
 #&
K6*--3!!!!%5"56z*:;;6"2A666 +]Z'>!!!RZ-+HH!
 "M!!!RZ-$@@
 	
r3   c           
      :   |j         | j        fk    s
J d            | j        j         \  }}t                              | 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 | 
 
 
 
0
 
 
 "(3&44H Q  

 

? ay!C!GHHaK.!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         2  2  2D
 
 
: 	 	 X	 	! 	! X	!B B B*
 
 
$
 
 
2
 
 
02 2 2h* * *0 >
 >
 \>
@ (F (F \(F (F (Fr3   ra   c                   l   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$dS )$Modelsz6
    Models for a nonlinear optimization problem.
    c           	         |t           j                 | _        t          ||          | _        | 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    r'|| j        |<   || j        |ddf<   || j        |ddf<   nJ| j                            |          } |||          \  | j        |<   | j        |ddf<   | j        |ddf<   |j        rh|                    | j                            |          | j        |ddf         | j        |ddf                   |t           j                 k    rt0          | j        |         |t           j                 k    rh|                    | j                            |          | j        |ddf         | j        |ddf                   |t           j                 k    rt4          t7          | j        | j        |t           j                           | _        t          j        | j        t6                    | _        t          j        | j         t6                    | _!        t          | j                  D ]?}	t7          | j        | j        dd|	f         |t           j                           | j        |	<   @t          | j                   D ]?}	t7          | j        | j        dd|	f         |t           j                           | j!        |	<   @| j        r| "                                 dS dS )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   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'   penaltyx_evalfun_initcub_initceq_initr-   is
             r1   r2   zModels.__init__s  s   6 gm,+B88 #))!,,')r&'':':$(H 4bf==!5x} ErvNN!5x} ErvNNww{+,, &	$ &	$AGG,---""Avv"*Q%-QT"%-QT""+11!44JL"K KGQad!3T\!QQQ$5G !	&HH&,,Q//LAAA&LAAA& 
 7234 4 &%
 a GGN$;;;HH&,,Q//LAAA&LAAA& 
 7234 4 $# MGM"
 
	
 HT0	BBB	HT0	BBB	t*++ 	 	A$"QQQT"& DIaLL
 t*++ 	 	A$"QQQT"& DIaLL
 ; 	30022222	3 	3r3   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                 &    | 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                 &    | 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                  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.fun:  sF      ; 	J7tvi''')I'''yyD.///r3   c                     | j         r|j        | j        fk    s
J d            | 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_gradN  sH      ; 	J7tvi''')I'''y~~a!3444r3   c                 @    | 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_hessb  s     y~~d0111r3   c                     | j         r|j        | j        fk    s
J d            | 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_prodn  sJ    " ; 	J7tvi''')I'''y""1d&8999r3   c                     | j         r|j        | j        fk    s
J d            | 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  sH    " ; 	J7tvi''')I'''y~~a!3444r3   c                     | j         r|j        | j        fk    s
J d            t          | 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^    , ; 	J7tvi''')I'''$,dlDKHHzz!T/000r3   Nc                       j         r8j         j        fk    s
J d            ||j         j        fk    s
J d            t	          j         fd                     |          D                       S )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   N!The shape of `mask` is not valid.c                 2    g | ]} |j                   S rI   rZ   .0r   r%   rq   s     r1   
<listcomp>zModels.cub.<locals>.<listcomp>  (    KKKeUU1d())KKKr3   r   r7   r    r   r   array_get_cubr%   rq   masks   `` r1   cubz
Models.cub  s    & ; 	37tvi''')I'''<4:#2 $ $ $2$ $ $ xKKKKKt}}T7J7JKKK
 
 	
r3   c                       j         r8j         j        fk    s
J d            ||j         j        fk    s
J d            t	          j         fd                     |          D             d j        f          S )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   Nr   c                 F    g | ]}|                     j                  S rI   ry   rZ   r   s     r1   r   z#Models.cub_grad.<locals>.<listcomp>  :     / / / ZZ4-.. / / /r3   r   r7   r    r   r   reshaper   r   s   `` r1   cub_gradzModels.cub_grad      & ; 	37tvi''')I'''<4:#2 $ $ $2$ $ $ z/ / / / /----/ / /L
 
 	
r3   c                       j         r||j         j        fk    s
J d            t          j         fd                     |          D             d j         j        f          S )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.
        Nr   c                 D    g | ]}|                     j                  S rI   r|   rZ   r   r   r%   s     r1   r   z#Models.cub_hess.<locals>.<listcomp>  (    MMMUZZ*++MMMr3   r   )r   r7   r   r   r   r   r    r%   r   s   ` r1   cub_hesszModels.cub_hess        ; 	3<4:#2 $ $ $2$ $ $ zMMMMt9L9LMMM 
 
 	
r3   c                       j         r8j         j        fk    s
J d            ||j         j        fk    s
J d            t	          j         fd                     |          D             d j        f          S )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~   Nr   c                 F    g | ]}|                     j                  S rI   rx   rZ   r   r   r%   r   s     r1   r   z(Models.cub_hess_prod.<locals>.<listcomp>  :        4#566  r3   r   r   r%   r   r   s   `` r1   cub_hess_prodzModels.cub_hess_prod      & ; 	37tvi''')I'''<4:#2 $ $ $2$ $ $ z    !]]400   L
 
 	
r3   c                       j         r8j         j        fk    s
J d            ||j         j        fk    s
J d            t	          j         fd                     |          D                       S )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~   Nr   c                 F    g | ]}|                     j                  S rI   r   rZ   r   s     r1   r   z#Models.cub_curv.<locals>.<listcomp>?  r   r3   r   r   s   `` r1   cub_curvzModels.cub_curv&      & ; 	37tvi''')I'''<4:#2 $ $ $2$ $ $ x/ / / / /----/ / /
 
 	
r3   c                       j         r8j         j        fk    s
J d            ||j         j        fk    s
J d            t	          j         fd                     |          D                       S )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   Nr   c                 2    g | ]} |j                   S rI   r   r   s     r1   r   zModels.ceq.<locals>.<listcomp>[  r   r3   r   r7   r    r   r   r   _get_ceqr   s   `` r1   ceqz
Models.ceqC  s    $ ; 	37tvi''')I'''<4:#2 $ $ $2$ $ $ xKKKKKt}}T7J7JKKK
 
 	
r3   c                       j         r8j         j        fk    s
J d            ||j         j        fk    s
J d            t	          j         fd                     |          D             d j        f          S )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   Nr   c                 F    g | ]}|                     j                  S rI   r   r   s     r1   r   z#Models.ceq_grad.<locals>.<listcomp>w  r   r3   r   r   r7   r    r   r   r   r	  r   s   `` r1   ceq_gradzModels.ceq_grad^  r   r3   c                       j         r||j         j        fk    s
J d            t          j         fd                     |          D             d j         j        f          S )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.
        Nr   c                 D    g | ]}|                     j                  S rI   r   r   s     r1   r   z#Models.ceq_hess.<locals>.<listcomp>  r   r3   r   )r   r7   r   r   r   r	  r    r   s   ` r1   ceq_hesszModels.ceq_hess|  r   r3   c                       j         r8j         j        fk    s
J d            ||j         j        fk    s
J d            t	          j         fd                     |          D             d j        f          S )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~   Nr   c                 F    g | ]}|                     j                  S rI   r   r   s     r1   r   z(Models.ceq_hess_prod.<locals>.<listcomp>  r   r3   r   r  r   s   `` r1   ceq_hess_prodzModels.ceq_hess_prod  r   r3   c                       j         r8j         j        fk    s
J d            ||j         j        fk    s
J d            t	          j         fd                     |          D                       S )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~   Nr   c                 F    g | ]}|                     j                  S rI   r  r   s     r1   r   z#Models.ceq_curv.<locals>.<listcomp>  r   r3   r  r   s   `` r1   ceq_curvzModels.ceq_curv  r  r3   c                    t          | j        | j        | j                  | _        t          | j                  D ]4}t          | j        | j        dd|f         | j                  | j        |<   5t          | j	                  D ]4}t          | j        | j
        dd|f         | j                  | j        |<   5| j        r|                                  dS dS )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$,LL	t*++ 	 	A$"QQQT" DIaLL
 t*++ 	 	A$"QQQT" DIaLL
 ; 	30022222	3 	3r3   c           	         | j         rd|cxk    r| j        k     sn 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                  }||                     |          z
  ||<   ||                     |          z
  ||ddf<   ||                     |          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        ||	|          }
t-          | j                  D ]6}|
p1| j        |                             | j        ||	|dd|f                   }
7t-          | j                  D ]6}|
p1| j        |                             | j        ||	|dd|f                   }
7| j         r|                                  |
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 ; 	6(((((((((*K(((;46)+++4 ,++gu-- 3 323 3 3=#%   5   =#%   5  
 8DH%%8DL.//8DL.//!DHHUOO3$txx6$txx6 &U!(UAAAX!(UAAAX '$,0E:;;+043E3L+Lqqq%x( )**	
 
 t*++ 	 	A- 11D1D"A	2 2OO t*++ 	 	A- 11D1D"A	2 2OO ; 	300222r3   c                    | j         r;|j        | j        fk    s
J d            |d|cxk    r| j        k     sn 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        |          d         }d||z  dz  z  |dddf         |dddf         z  z
  }|tt          j        | j        | j        z   dz   | j                  }t          j        t                              | j        |          d                   }|d| j        df         }	n_t          j        | j        | j        z   dz   d|           }t                              | 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@  s;   H ; 	1;46)+++4 ,++ e!6!6!6!6dh!6!6!6!6!60 "7!6!6 *11(DHtv-11566t)-/%7C?? 	
$(
A"!$)1q !--d.@'JJ1Meem++gaaadmk!!!Q$>O.OO =tx$&014dh??IG''&   E jj!m,CCtx$&014a%@@I++"  	
 QhE eQh'Ct|c3h&&r3   c                    | j         r|j        | j        fk    s
J d            | j                            | j        |           | j        D ]}|                    | j        |           | j        D ]}|                    | j        |           || j        j        z
  }| j        xj        |z  c_        | j        xj	        |ddt          j        f         z  c_	        |t          j                 r|                                  dS dS )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  s6    ; 	9#(   8  
 		t1:>>>Y 	? 	?Et1:>>>>Y 	? 	?Et1:>>>> T/66!!U*!!%2:"667=! 	30022222	3 	3r3   c                 .    || j         n| 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   s     r1   r   zModels._get_cub       !Ltyydio=r3   c                 .    || j         n| 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 ].}t          j        |t          j        |                     | j                            |                    | j        |         z
            g          }t          j        t          j        | 	                    | j                            |                    | j
        |ddf         z
            |          }t          j        t          j        |                     | j                            |                    | j        |ddf         z
            |          }0dt          j        t                    z  t          | j        | j                  z  }||t          j        t          j        | j                  d          z  k    rt!          j        dt$          d           ||t          j        t          j        | j
                  d          z  k    rt!          j        dt$          d           ||t          j        t          j        | j                  d          z  k    rt!          j        d	t$          d           dS dS )
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[    			tx 	 	AF!3!9!9!!<!<==QO  I HHT/55a8899DLAAA<NN  "	  I HHT/55a8899DLAAA<NN  "	  II RWS\\!C$9$99sRVBF4<$8$8#FFFFFFM!	   sRVBF4<$8$8#FFFFFFM.	   sRVBF4<$8$8#FFFFFFM.	     GFr3   )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        a3 a3 a3F 	$ 	$ X	$ 	& 	& X	& 	% 	% X	% 	% 	% X	% 	# 	# X	# 	 	 X	   X   X0 0 0(5 5 5(
2 
2 
2: : :*5 5 5*1 1 16
 
 
 
8
 
 
 
<
 
 
 
2
 
 
 
@
 
 
 
:
 
 
 
6
 
 
 
<
 
 
 
2
 
 
 
@
 
 
 
:3 3 36Q Q QfH' H' H' H'T3 3 3<> > > > > > > > 1 1 1 1 1r3   r   )r3  numpyr   scipy.linalgr   settingsr   utilsr   r   r   finfor  epsrW   r
   rR   r_   ra   r   rI   r3   r1   <module>r@     s9                    ? ? ? ? ? ? ? ? ? ? bhuoor, r, r, r, r, r, r, r,j D4	F	F.7 .7 .7buF uF uF uF uF uF uF uFpK K K K K K K K K Kr3   