o
    ?Hh                     @   s   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ejZG dd dZdddddZd	d
 ZG dd dZG dd dZdS )    N)eigh   )Options)MaxEvalErrorTargetSuccessFeasibleSuccessc                   @   sl   e Zd ZdZdd Zedd Zedd Zedd	 Zej	d
d	 Zedd Z
e
j	dd Z
d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                 C   s  |t j | _dt|jj|jj  }|t j |kr.||t jj	< t|t j
 |g|t j
j	< t|j| _| j|jjd|t j   k}|jj| | j|< |jjd|t j   | jk | j|jj|t j  k@ }t|jj| |t j  |jj| | j|< | j|jjd|t j   k}|jj| | j|< | j|jjd|t j   k |jj|t j  | jk@ }t|jj| |t j  |jj| | j|< t|j|t j f| _td|t j D ]}||jkr||d  r|t j  | j|d |f< q|t j | j|d |f< q|d|j krP|||j d  r#d|t j  | j||j d |f< q|||j d  r?d|t j  | j||j d |f< q|t j  | j||j d |f< q||j d |j }	|d|	 |j  d }
|
|	 |j }| j|
|
d f | j|
|f< | j||d f | j||f< qd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k2 r.   X/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/scipy/_lib/cobyqa/models.py__init__   s`   




$$"zInterpolation.__init__c                 C      | j jd S )t
        Number of variables.

        Returns
        -------
        int
            Number of variables.
        r   r!   shaper"   r.   r.   r/   r   \      
zInterpolation.nc                 C   r1   )
        Number of interpolation points.

        Returns
        -------
        int
            Number of interpolation points.
        r   r3   r5   r.   r.   r/   npth   r6   zInterpolation.nptc                 C      | j S )z
        Interpolation points.

        Returns
        -------
        `numpy.ndarray`, shape (n, npt)
            Interpolation points.
        )r   r5   r.   r.   r/   r!   t      
zInterpolation.xptc                 C   s*   | 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   r4   r   r8   r   )r"   r!   r.   r.   r/   r!      s   

c                 C   r9   )z
        Base point around which the models are expanded.

        Returns
        -------
        `numpy.ndarray`, shape (n,)
            Base point around which the models are expanded.
        )r   r5   r.   r.   r/   r      r:   zInterpolation.x_basec                 C   s&   | 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   r4   r   r   )r"   r   r.   r.   r/   r      s   

c                 C   sD   | j rd|  kr| jk sJ d J d| j| jdd|f  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   r8   r   r!   )r"   r*   r.   r.   r/   point   s   &zInterpolation.pointN)__name__
__module____qualname____doc__r0   propertyr   r8   r!   setterr   r;   r.   r.   r.   r/   r      s     F





r   )r!   aright_scalingr   c           	      C   s  t d durt| jt d rt d t d t d fS tjtjj| jddtd}| j| }|j\}}t	|| d	 || d	 f}d
|j
| d  |d|d|f< d|d||f< |j
|d||d	 df< d||d|f< |||d	 dd|f< t|| d	 }d|d  |d|< |d ||< |||d	 d< t|dd\}}t| jt d< t|t d< t|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!   NrB   rC   r   r   )axisinitialr   r	   r         ?F)check_finite)_cacher   array_equalr!   maxlinalgnormEPSr4   r   Temptyr   r   )	interpolationscale	xpt_scaler   r8   rB   rC   
eig_valueseig_vectorsr.   r.   r/   build_system   s.   


"rV   c                   @   s   e Zd ZdZdd Zdd Zedd Zedd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd Zedd Zed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                 C   sz   || _ | j r|j|jfksJ d|j|jd k r$td|jd  d| ||\| _| _| _}t	
| 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   r4   r8   r   
ValueError
_get_model_const_grad_i_hessr   r   _e_hess)r"   rQ   valuesdebug_r.   r.   r/   r0     s$   zQuadratic.__init__c                 C   s^   | j r|j| jfksJ d||j }| j| j|  d| j|jj| d  || j	 |    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   r4   r   r   r\   r]   r^   r!   rO   r_   r"   xrQ   x_diffr.   r.   r/   __call__(  s   
zQuadratic.__call__c                 C      | j jS )r2   )r]   sizer5   r.   r.   r/   r   E     
zQuadratic.nc                 C   rh   )z
        Number of interpolation points used to define the quadratic model.

        Returns
        -------
        int
            Number of interpolation points used to define the quadratic model.
        )r^   ri   r5   r.   r.   r/   r8   Q  rj   zQuadratic.nptc                 C   s8   | j r|j| jfksJ d||j }| j| || 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`.
        rc   )r   r4   r   r   r]   	hess_prodrd   r.   r.   r/   grad]  s   
zQuadratic.gradc                 C   s(   | j |j| jddtjf |jj   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)r_   r!   r^   r   newaxisrO   )r"   rQ   r.   r.   r/   hessr  s   zQuadratic.hessc                 C   s>   | j r|j| jfksJ d| j| |j| j|jj|    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   r4   r   r_   r!   r^   rO   r"   vrQ   r.   r.   r/   rk     s
   zQuadratic.hess_prodc                 C   s@   | j r|j| jfksJ d|| j | | j|jj| d   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`.
        ro   r   )r   r4   r   r_   r^   r!   rO   rp   r.   r.   r/   curv  s   zQuadratic.curvc           	      C   s   | j r,d|  kr| jk sJ d J d|j| jfks!J d|j| jfks,J d|  j| j| t|| 7  _d| j|< | ||\}}}}|  j	|7  _	|  j
|7  _
|  j|7  _|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   r8   r4   r   r_   r^   r   outerr[   r\   r]   )	r"   rQ   k_newdir_oldvalues_diffconstrl   i_hessill_conditionedr.   r.   r/   update  s,   & 
zQuadratic.updatec                 C   s   | j r|j| jfksJ d| ||| _| ||| _||j }t||j	d|ddtj
f   | j }|  j||j 7  _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   r4   r   r\   rl   r]   r   r   ru   r!   rm   r^   r_   rO   )r"   rQ   
new_x_baseshiftr|   r.   r.   r/   shift_x_base  s   
 zQuadratic.shift_x_basec                 C   s   | j j\}}|jdkr|jd || d ksJ dt| \}}}||ddtjf  }tt|r<tt|sBtj	d|\}}	t
|tk}
|	dd|
f }	d||
  }t|
d }|	|	j| |ddtjf   }||ddtjf  |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.rG   )r!   r4   ndimrV   r   rm   allisfiniterL   LinAlgErrorabsrN   rO   )rQ   rhsr   r8   rB   rC   eig
rhs_scaledrT   rU   large_eig_valuesinv_eig_valuesr{   left_scaled_solutionsr.   r.   r/   solve_systems  s*   "
 zQuadratic.solve_systemsc              
   C   sz   |j | jfksJ d| jj \}}t| t|t|d ggj\}}||df ||d 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.
        rX   r   r   N)	r4   r8   r!   rW   r   r   blockr   rO   )rQ   r`   r   r8   re   r{   r.   r.   r/   r[   B  s"   0zQuadratic._get_modelN)r<   r=   r>   r?   r0   rg   r@   r   r8   rl   rn   rk   rr   r|   r   staticmethodr   r[   r.   r.   r.   r/   rW      s$    "

4
@rW   c                   @   sJ  e Zd ZdZdd Zedd Zedd Zedd	 Zed
d Z	edd Z
edd Zedd Zedd Zdd Zdd Zdd Zdd Zdd Zdd ZdCd!d"ZdCd#d$ZdCd%d&ZdCd'd(ZdCd)d*ZdCd+d,ZdCd-d.ZdCd/d0ZdCd1d2ZdCd3d4Zd5d6 Zd7d8 ZdCd9d:Z d;d< Z!dCd=d>Z"dCd?d@Z#dAdB Z$d S )DModelsz6
    Models for a nonlinear optimization problem.
    c           
   	   C   s~  |t j | _t||| _| jd}|||\}}}t|t j	 tj
| _t|t j	 |jftj
| _t|t j	 |jftj
| _t|t j	 D ]}||t j krSt|dkro|| j|< || j|ddf< || j|ddf< n| j|}|||\| j|< | j|ddf< | j|ddf< |jr|| j|| j|ddf | j|ddf |t j krt| j| |t j kr|| j|| j|ddf | j|ddf |t j krtqHt| j| j|t j | _tj| jtd| _tj| j td| _!t| jD ]}	t| j| jdd|	f |t j | j|	< qt| j D ]}	t| j| jdd|	f |t j | j!|	< q| 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   _interpolationrQ   r;   r   fullr   nan_fun_valri   _cub_val_ceq_valr    MAX_EVALr   fun_valcub_valceq_valis_feasibilitymaxcvFEASIBILITY_TOLr   TARGETr   rW   _funrP   m_nonlinear_ub_cubm_nonlinear_eq_ceq_check_interpolation_conditions)
r"   r#   r$   penaltyx_evalfun_initcub_initceq_initr*   ir.   r.   r/   r0   s  sz   
,

zModels.__init__c                 C   rh   )z~
        Dimension of the problem.

        Returns
        -------
        int
            Dimension of the problem.
        )rQ   r   r5   r.   r.   r/   r     rj   zModels.nc                 C   rh   )r7   )rQ   r8   r5   r.   r.   r/   r8     rj   z
Models.nptc                 C   r1   )z
        Number of nonlinear inequality constraints.

        Returns
        -------
        int
            Number of nonlinear inequality constraints.
        r   )r   r4   r5   r.   r.   r/   r     r6   zModels.m_nonlinear_ubc                 C   r1   )z
        Number of nonlinear equality constraints.

        Returns
        -------
        int
            Number of nonlinear equality constraints.
        r   )r   r4   r5   r.   r.   r/   r     r6   zModels.m_nonlinear_eqc                 C   r9   )z
        Interpolation set.

        Returns
        -------
        `cobyqa.models.Interpolation`
            Interpolation set.
        )r   r5   r.   r.   r/   rQ     r:   zModels.interpolationc                 C   r9   )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   r5   r.   r.   r/   r     r:   zModels.fun_valc                 C   r9   )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   r5   r.   r.   r/   r        zModels.cub_valc                 C   r9   )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   r5   r.   r.   r/   r   ,  r   zModels.ceq_valc                 C   s*   | 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`.
        rc   )r   r4   r   r   rQ   r"   re   r.   r.   r/   fun:  s   z
Models.func                 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`.
        rc   )r   r4   r   r   rl   rQ   r   r.   r.   r/   fun_gradN  s   zModels.fun_gradc                 C   s   | 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   rn   rQ   r5   r.   r.   r/   fun_hessb  s   
zModels.fun_hessc                 C   r   )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`.
        ro   )r   r4   r   r   rk   rQ   r"   rq   r.   r.   r/   fun_hess_prodn     zModels.fun_hess_prodc                 C   r   )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`.
        ro   )r   r4   r   r   rr   rQ   r   r.   r.   r/   fun_curv  r   zModels.fun_curvc                 C   s<   | 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.
        rc   )r   r4   r   rW   rQ   r   rl   )r"   re   modelr.   r.   r/   fun_alt_grad  s   zModels.fun_alt_gradNc                    Z    j rj jfksJ d|du s|j jfksJ dt fd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.
        rc   N!The shape of `mask` is not valid.c                       g | ]}| j qS r.   rQ   .0r   r   r.   r/   
<listcomp>      zModels.cub.<locals>.<listcomp>r   r4   r   r   r   array_get_cubr"   re   maskr.   r   r/   cub  s   z
Models.cubc                    b    j rj jfksJ d|du s|j jfksJ dt fd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.
        rc   Nr   c                       g | ]	}|  jqS r.   rl   rQ   r   r   r.   r/   r         z#Models.cub_grad.<locals>.<listcomp>r   r4   r   r   r   reshaper   r   r.   r   r/   cub_grad     zModels.cub_gradc                    N    j r|du s|j jfksJ dt fd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                       g | ]}|  jqS r.   rn   rQ   r   r5   r.   r/   r     r   z#Models.cub_hess.<locals>.<listcomp>r   )r   r4   r   r   r   r   r   r"   r   r.   r5   r/   cub_hess     zModels.cub_hessc                    r   )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`.
        ro   Nr   c                    r   r.   rk   rQ   r   r   r.   r/   r         z(Models.cub_hess_prod.<locals>.<listcomp>r   r   r"   rq   r   r.   r   r/   cub_hess_prod     zModels.cub_hess_prodc                    r   )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`.
        ro   Nr   c                    r   r.   rr   rQ   r   r   r.   r/   r   ?  r   z#Models.cub_curv.<locals>.<listcomp>r   r   r.   r   r/   cub_curv&     zModels.cub_curvc                    r   )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.
        rc   Nr   c                    r   r.   r   r   r   r.   r/   r   [  r   zModels.ceq.<locals>.<listcomp>r   r4   r   r   r   r   _get_ceqr   r.   r   r/   ceqC  s   z
Models.ceqc                    r   )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.
        rc   Nr   c                    r   r.   r   r   r   r.   r/   r   w  r   z#Models.ceq_grad.<locals>.<listcomp>r   r   r4   r   r   r   r   r   r   r.   r   r/   ceq_grad^  r   zModels.ceq_gradc                    r   )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                    r   r.   r   r   r5   r.   r/   r     r   z#Models.ceq_hess.<locals>.<listcomp>r   )r   r4   r   r   r   r   r   r   r.   r5   r/   ceq_hess|  r   zModels.ceq_hessc                    r   )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`.
        ro   Nr   c                    r   r.   r   r   r   r.   r/   r     r   z(Models.ceq_hess_prod.<locals>.<listcomp>r   r   r   r.   r   r/   ceq_hess_prod  r   zModels.ceq_hess_prodc                    r   )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`.
        ro   Nr   c                    r   r.   r   r   r   r.   r/   r     r   z#Models.ceq_curv.<locals>.<listcomp>r   r   r.   r   r/   ceq_curv  r   zModels.ceq_curvc                 C   s   t | j| j| j| _t| jD ]}t | j| jdd|f | j| j|< qt| j	D ]}t | j| j
dd|f | j| j|< q)| jrG|   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)rW   rQ   r   r   r   r    r   r   r   r   r   r   r   )r"   r   r.   r.   r/   reset_models  s    zModels.reset_modelsc              	   C   s  | j r@d|  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s5J d|j| jfks@J dt	| j}t	| j
j}t	| jj}|| | ||< || | ||ddf< || | ||ddf< || j|< || j
|ddf< || j|ddf< t| jjdd|f }	|| jj | jjdd|f< | j| j||	|}
t| jD ]}|
p| j| | j||	|dd|f }
qt| jD ]}|
p| j| | j||	|dd|f }
q| 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   rs   "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   r8   r4   r   
isinstancefloatr   r   r   r   r   r   r   r   r   r   r   rQ   r!   r   r   r|   r    r   r   r   )r"   rv   x_newr   r   r   fun_diffcub_diffceq_diffrw   r{   r   r.   r.   r/   update_interpolation  sh   &
zModels.update_interpolationc           
      C   s  | j r%|j| jfksJ d|du s%d|  kr | jk s%J d J d|| jj }t| j| j d df}d| jjj	| d  |d| jdf< d|| jdf< ||| jd ddf< t
| j|d }d|| d  |dddf |dddf   }|du rt| j| j d | j}tt
| j|d }|d| jdf }	n!t| j| j d d| }t
| j|d |df }||df }	|| |	d  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   rs   r   r	   r   rG   )r   r4   r   r8   rQ   r   r   rP   r!   rO   rW   r   eyediag)
r"   r   rv   r   new_colinv_new_colbeta	coord_vecalphataur.   r.   r/   determinants@  sN   $0zModels.determinantsc                 C   s   | j r|j| jfksJ d| j| j| | jD ]	}|| j| q| jD ]	}|| j| q&|| jj }| j j|7  _| j j	|ddt
jf 8  _	|tj rX|   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   r4   r   r   r   rQ   r   r   r   r!   r   rm   r   r   r   )r"   r~   r$   r   r   r.   r.   r/   r     s"   


zModels.shift_x_basec                 C      |du r| 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.
        N)r   r   r.   r.   r/   r        zModels._get_cubc                 C   r   )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.
        N)r   r   r.   r.   r/   r     r   zModels._get_ceqc                 C   sX  d}d}d}t | jD ]L}t|t| | j|| j|  g}tjt| 	| j|| j
|ddf  |d}tjt| | j|| j|ddf  |d}qdtt t| j| j }||tjt| jdd kr|tdtd ||tjt| j
dd krtdtd ||tjt| jdd krtd	td dS dS )
zM
        Check the interpolation conditions of all quadratic models.
        rt   NrE   g      $@rG   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    r8   r   rK   r   r   rQ   r;   r   r   r   r   r   sqrtrN   r   warningswarnRuntimeWarning)r"   	error_fun	error_cub	error_ceqr*   tolr.   r.   r/   r     sV   ""z&Models._check_interpolation_conditions)N)%r<   r=   r>   r?   r0   r@   r   r8   r   r   rQ   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   r.   r.   r.   r/   r   n  sT    c












 




 
SJ

r   )r   numpyr   scipy.linalgr   settingsr   utilsr   r   r   finfor   epsrN   r   rI   rV   rW   r   r.   r.   r.   r/   <module>   s     61  z