o
    Ö­jàÅ  ã                   @   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< q×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Úk2© r.   úU/var/www/html/CropPilot/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r±t‚| j| |t j krÛ| | j |¡| j|dd…f | j|dd…f ¡|t j krÛt‚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|	< qÿt| 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 ¡}
q¸t| 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 r¤t | 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r’t dtd¡ ||tjt | j¡dd� krªt 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