
    (ph                         S SK r S SKrS SKJr  SSKJr  SSKJrJ	r	J
r
  \R                  " \5      R                  r " S S5      rSSSSS.rS	 r " S
 S5      r " S S5      rg)    N)eigh   )Options)MaxEvalErrorTargetSuccessFeasibleSuccessc                       \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	\	R                  S 5       r	\S 5       r\R                  S	 5       rS
 rSrg)Interpolation   z
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                 D   U[         R                     U l        S[        R                  " UR
                  R                  UR
                  R                  -
  5      -  nU[         R                     U:  a`  X2[         R                  R                  '   [        R                  " U[         R                     U/5      U[         R                  R                  '   [        R                  " UR                  5      U l        U R                  UR
                  R                  SU[         R                     -  -   :*  nUR
                  R                  U   U R                  U'   UR
                  R                  SU[         R                     -  -   U R                  :  U R                  UR
                  R                  U[         R                     -   :*  -  n[        R                  " UR
                  R                  U   U[         R                     -   UR
                  R                  U   5      U R                  U'   U R                  UR
                  R                  SU[         R                     -  -
  :  nUR
                  R                  U   U R                  U'   U R                  UR
                  R                  SU[         R                     -  -
  :  UR
                  R                  U[         R                     -
  U R                  :*  -  n[        R                   " UR
                  R                  U   U[         R                     -
  UR
                  R                  U   5      U R                  U'   [        R"                  " UR$                  U[         R&                     45      U l        [+        SU[         R&                     5       GH  nXR$                  ::  a[  XhS-
     (       a(  U[         R                     * U R,                  US-
  U4'   MG  U[         R                     U R,                  US-
  U4'   Mn  USUR$                  -  ::  a  XHUR$                  -
  S-
     (       a6  SU[         R                     -  U R,                  XR$                  -
  S-
  U4'   M  XhUR$                  -
  S-
     (       a7  SU[         R                     -  U R,                  XR$                  -
  S-
  U4'   GM   U[         R                     * U R,                  XR$                  -
  S-
  U4'   GMU  XR$                  -
  S-
  UR$                  -  n	USU	-   UR$                  -  -
  S-
  n
X-   UR$                  -  nU R,                  XS-   4   U R,                  X4'   U R,                  XS-   4   U R,                  X4'   GM     g)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               K/var/www/html/venv/lib/python3.13/site-packages/scipy/_lib/cobyqa/models.py__init__Interpolation.__init__   s.    gmm,266")),,"=>>
7>>"Z/,6GNN((),.FFGNN+-GGNN(() wwruu~KK299<<#0G*GGG 	 *,6G)H%&IILL3!8884;;F[[BIILL77>>+BBBD %'JJIILL&)@@IILL&%
L!
 KK299<<#0G*GGG 	 *,6G)H%&KK")),,ww~~/F)FFFYY\\GGNN33t{{BD %'JJIILL&)@@IILL&%
L! HHbddGGKK$89:	q''++./ADDy$U+*1'..*A)ADHHQUAX&)0)@DHHQUAX&a"$$h$X\203ggnn6M0MDHHQX\1_-&244x!|404ww~~7N0NDHHQX\1_-181H0HDHHQX\1_-dd(Q,244/!f*,,q0kRTT)"&((2Av:"6"&((2Av:"6% 0    c                 4    U R                   R                  S   $ )D
Number of variables.

Returns
-------
int
    Number of variables.
r   r%   shaper&   s    r2   r!   Interpolation.n\        xx~~a  r5   c                 4    U R                   R                  S   $ )Z
Number of interpolation points.

Returns
-------
int
    Number of interpolation points.
r   r8   r:   s    r2   nptInterpolation.npth   r<   r5   c                     U R                   $ )zb
Interpolation points.

Returns
-------
`numpy.ndarray`, shape (n, npt)
    Interpolation points.
)r#   r:   s    r2   r%   Interpolation.xptt   s     yyr5   c                     U R                   (       a-  UR                  U R                  U R                  4:X  d   S5       eXl        g)zz
Set the interpolation points.

Parameters
----------
xpt : `numpy.ndarray`, shape (n, npt)
    New interpolation points.
z The shape of `xpt` is not valid.N)r   r9   r!   r?   r#   )r&   r%   s     r2   r%   rB      sE     ;;99!  2 22  	r5   c                     U R                   $ )z
Base point around which the models are expanded.

Returns
-------
`numpy.ndarray`, shape (n,)
    Base point around which the models are expanded.
)r   r:   s    r2   r   Interpolation.x_base   s     ||r5   c                 v    U R                   (       a"  UR                  U R                  4:X  d   S5       eXl        g)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   r9   r!   r   )r&   r   s     r2   r   rE      s<     ;;<<$  545  r5   c                     U R                   (       a&  SUs=::  a  U R                  :  d   S5       e   S5       eU R                  U R                  SS2U4   -   $ )z
Get the `k`-th interpolation point.

The return point is relative to the origin.

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

Returns
-------
`numpy.ndarray`, shape (n,)
    `k`-th interpolation point.
r   zThe index `k` is not valid.N)r   r?   r   r%   )r&   r.   s     r2   pointInterpolation.point   sO      ;;$DHH$C&CC$C&CC${{TXXad^++r5   )r   r   r#   N)__name__
__module____qualname____firstlineno____doc__r3   propertyr!   r?   r%   setterr   rH   __static_attributes__ r5   r2   r
   r
      s    D7L 	! 	! 	! 	! 	 	 	ZZ   	 	 ]] ,r5   r
   )r%   aright_scalingr   c                    [         S   bG  [        R                  " U R                  [         S   5      (       a  [         S   [         S   [         S   4$ [        R                  " [        R
                  R                  U R                  SS9[        S9nU R                  U-  nUR                  u  p4[        R                  " XC-   S	-   XC-   S	-   45      nS
UR                  U-  S-  -  USU2SU24'   SUSU2U4'   UR                  USU2US	-   S24'   SXTSU24'   X%US	-   S2SU24'   [        R                  " XC-   S	-   5      nSUS-  -  USU& US-  Xd'   XUS	-   S& [        USS9u  px[        R                  " U R                  5      [         S'   [        R                  " U5      [         S'   [        R                  " U5      [         S'   Xx4[         S'   XVXx44$ )aa  
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%   NrS   rT   r   r   )axisinitialr   r   r         ?F)check_finite)_cacher   array_equalr%   maxlinalgnormEPSr9   r    Temptyr   r   )	interpolationscale	xpt_scaler!   r?   rS   rT   
eig_valueseig_vectorss	            r2   build_systemrh      s   " e} R^^6%=& & c{F?3VF^CCFF299>>-"3"3!><cJE!!E)I__FA
#'A+sw{+,A9;;2s::AdsdDSDjMAdsdCiL!AdsdC!GHnA4C4iL!cAghn HHSWq[)Ms
*M$3M##'("159JGGM--.F5M''!*F3K ggm4F? .F6Nj666r5   c                       \ rS rSrSrS rS r\S 5       r\S 5       r	S r
S rS	 rS
 rS rS r\S 5       r\S 5       rSrg)	Quadratic   a  
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                    X0l         U R                   (       a"  UR                  UR                  4:X  d   S5       eUR                  UR                  S-   :  a  [	        SUR                  S-    S35      eU R                  UU5      u  U l        U l        U l        n[        R                  " U R                  U R                  45      U l        g)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   r9   r?   r!   
ValueError
_get_model_const_grad_i_hessr   r    _e_hess)r&   rc   valuesdebug_s        r2   r3   Quadratic.__init__  s    $ ;;<<!!$  545  }22F ??Q&'q*  48??4
0TZq xx 01r5   c                 :   U R                   (       a"  UR                  U R                  4:X  d   S5       eXR                  -
  nU R                  U R
                  U-  -   SU R                  UR                  R                  U-  S-  -  X0R                  -  U-  -   -  -   $ )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   r9   r!   r   rq   rr   rs   r%   ra   rt   r&   xrc   x_diffs       r2   __call__Quadratic.__call__(  s      ;;77tvvi'I)II')))KKjj6!" 1 1 3 3f <DD<<'&01	
r5   c                 .    U R                   R                  $ )r7   )rr   sizer:   s    r2   r!   Quadratic.nE  s     zzr5   c                 .    U R                   R                  $ )z
Number of interpolation points used to define the quadratic model.

Returns
-------
int
    Number of interpolation points used to define the quadratic model.
)rs   r   r:   s    r2   r?   Quadratic.nptQ  s     ||   r5   c                     U R                   (       a"  UR                  U R                  4:X  d   S5       eXR                  -
  nU R                  U R                  X25      -   $ )aZ  
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`.
rz   )r   r9   r!   r   rr   	hess_prodr{   s       r2   gradQuadratic.grad]  sO      ;;77tvvi'I)II')))zzDNN6AAAr5   c                     U R                   UR                  U R                  SS2[        R                  4   UR                  R
                  -  -  -   $ )z
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)rt   r%   rs   r   newaxisra   )r&   rc   s     r2   hessQuadratic.hessr  sG     ||m//LLBJJ'-*;*;*=*==
 
 	
r5   c                     U R                   (       a"  UR                  U R                  4:X  d   S5       eU R                  U-  UR                  U R
                  UR                  R                  U-  -  -  -   $ )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   r9   r!   rt   r%   rs   ra   r&   vrc   s      r2   r   Quadratic.hess_prod  si    & ;;77tvvi'I)II'||a-"3"3LLM--//!34#
 
 	
r5   c                     U R                   (       a"  UR                  U R                  4:X  d   S5       eXR                  -  U-  U R                  UR
                  R                  U-  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   r9   r!   rt   rs   r%   ra   r   s      r2   curvQuadratic.curv  sd    " ;;77tvvi'I)II'q llm//11A5#==>	
r5   c                 4   U R                   (       aj  SUs=::  a  U R                  :  d   S5       e   S5       eUR                  U R                  4:X  d   S5       eUR                  U R                  4:X  d   S5       eU =R                  U R
                  U   [        R                  " X35      -  -  sl        SU R
                  U'   U R                  UU5      u  pVpxU =R                  U-  sl	        U =R                  U-  sl
        U =R
                  U-  sl        U$ )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?   r9   r!   rt   rs   r   outerrp   rq   rr   )	r&   rc   k_newdir_oldvalues_diffconstr   i_hessill_conditioneds	            r2   updateQuadratic.update  s   4 ;;((K*KK(K*KK(==%  656  $$)  :9:  	U+bhhw.HHH!U 040
,V 	u

d
r5   c                    U R                   (       a"  UR                  U R                  4:X  d   S5       eU " X!5      U l        U R	                  X!5      U l        X!R                  -
  n[        R                  " UUR                  SUSS2[        R                  4   -  -
  U R                  -  5      nU =R                  XDR                  -   -  sl        g)a  
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   r9   r!   rq   r   rr   r   r   r   r%   r   rs   rt   ra   )r&   rc   
new_x_baseshiftr   s        r2   shift_x_baseQuadratic.shift_x_base  s     ;;##(  989  :5YYz9
111uQ

]';!;;t||K
 	))r5   c                    U R                   R                  u  p#UR                  S:X  a  UR                  S   X2-   S-   :X  d   S5       e[        U 5      u  pEnXSS2[        R
                  4   -  n[        R                  " [        R                  " U5      5      (       a/  [        R                  " [        R                  " U5      5      (       d  [        R                  R                  S5      eUu  p[        R                  " U5      [        :  n
U	SS2U
4   n	SX   -  n[        R                  " U
S5      ) nU	U	R                  U-  USS2[        R
                  4   -  -  nXSS2[        R
                  4   -  U4$ )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.rY   )r%   r9   ndimrh   r   r   allisfiniter^   LinAlgErrorabsr`   ra   )rc   rhsr!   r?   rS   rT   eig
rhs_scaledrf   rg   large_eig_valuesinv_eig_valuesr   left_scaled_solutionss                 r2   solve_systemsQuadratic.solve_systems  sQ   0 ""((HHMciilcgk9	.-	.9 !-] ;# BJJ77
r{{1~&&266"++j2I+J+J))'': 
 #&
66*-3!!%5"56z;;66"2A66 +]]Z'>!RZZ-+HH!
 "!RZZ-$@@
 	
r5   c           
      J   UR                   U R                  4:X  d   S5       eU R                  R                   u  p#[        R	                  U [
        R                  " U[
        R                  " US-   5      //5      R                  5      u  pEXCS4   XCS-   S2S4   USU2S4   U4$ )a3  
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.
rm   r   r   N)	r9   r?   r%   rj   r   r   blockr    ra   )rc   ru   r!   r?   r|   r   s         r2   rp   Quadratic._get_modelB  s    4 || 
 
 	10	1 
 ""((&44HH Q a

 ay!!GHaK.!DSD!G*oEEr5   )rq   r   rt   rr   rs   N)rJ   rK   rL   rM   rN   r3   r~   rO   r!   r?   r   r   r   r   r   r   staticmethodr   rp   rQ   rR   r5   r2   rj   rj      s     2D
: 	 	 	! 	!B*
$
2
02h*0 >
 >
@ (F (Fr5   rj   c                   \   \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	\S 5       r
\S 5       r\S	 5       r\S
 5       r\S 5       rS rS rS rS rS rS rS%S jrS%S jrS%S jrS%S jrS%S jrS%S jrS%S jrS%S jrS%S jrS%S jrS rS r S%S jr!S  r"S%S! jr#S%S" jr$S# r%S$r&g)&Modelsin  z.
Models for a nonlinear optimization problem.
c           	         U[         R                     U l        [        X5      U l        U R
                  R                  S5      nU" XC5      u  pVn[        R                  " U[         R                     [        R                  5      U l        [        R                  " U[         R                     UR                  4[        R                  5      U l        [        R                  " U[         R                     UR                  4[        R                  5      U l        [        U[         R                     5       GH  nX[         R                      :  a  ["        eUS:X  a3  XPR$                  U'   X`R&                  USS24'   XpR(                  USS24'   OWU R
                  R                  U5      nU" UU5      u  U R$                  U'   U R&                  USS24'   U R(                  USS24'   UR*                  (       ai  UR-                  U R
                  R                  U5      U R&                  USS24   U R(                  USS24   5      U[         R.                     ::  a  [0        eU R                  U   U[         R2                     ::  d  GMQ  UR-                  U R
                  R                  U5      U R&                  USS24   U R(                  USS24   5      U[         R.                     ::  d  GM  [4        e   [7        U R
                  U R                  U[         R                     5      U l        [        R:                  " U R<                  [6        S9U l        [        R:                  " U R@                  [6        S9U l!        [        U R<                  5       HI  n	[7        U R
                  U R&                  SS2U	4   U[         R                     5      U R>                  U	'   MK     [        U R@                  5       HI  n	[7        U R
                  U R(                  SS2U	4   U[         R                     5      U RB                  U	'   MK     U R                  (       a  U RE                  5         gg)a  
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
   _interpolationrc   rH   r   fullr"   nan_fun_valr   _cub_val_ceq_valr$   MAX_EVALr   fun_valcub_valceq_valis_feasibilitymaxcvFEASIBILITY_TOLr   TARGETr   rj   _funrb   m_nonlinear_ub_cubm_nonlinear_eq_ceq_check_interpolation_conditions)
r&   r'   r(   penaltyx_evalfun_initcub_initceq_initr.   is
             r2   r3   Models.__init__s  s[   6 gmm,+B8 ##))!,')&':$H 4bff=!5x}} ErvvN!5x}} ErvvNww{{+,AG,,--""Av"*Q%-QT"%-QT"++11!4JLKGQad!3T\\!Q$5G !!HH&&,,Q/LLA&LLA&
 72234 &%
 a GGNN$;;HH&&,,Q/LLA&LLA&
 72234 $#M -R MMGMM"
	
 HHT00	B	HHT00	B	t**+A$""QT"&DIIaL , t**+A$""QT"&DIIaL , ;;002 r5   c                 .    U R                   R                  $ )zN
Dimension of the problem.

Returns
-------
int
    Dimension of the problem.
)rc   r!   r:   s    r2   r!   Models.n  s     !!###r5   c                 .    U R                   R                  $ )r>   )rc   r?   r:   s    r2   r?   
Models.npt  s     !!%%%r5   c                 4    U R                   R                  S   $ )zr
Number of nonlinear inequality constraints.

Returns
-------
int
    Number of nonlinear inequality constraints.
r   )r   r9   r:   s    r2   r   Models.m_nonlinear_ub       ||!!!$$r5   c                 4    U R                   R                  S   $ )zn
Number of nonlinear equality constraints.

Returns
-------
int
    Number of nonlinear equality constraints.
r   )r   r9   r:   s    r2   r   Models.m_nonlinear_eq  r   r5   c                     U R                   $ )zZ
Interpolation set.

Returns
-------
`cobyqa.models.Interpolation`
    Interpolation set.
)r   r:   s    r2   rc   Models.interpolation  s     """r5   c                     U R                   $ )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   r:   s    r2   r   Models.fun_val  s     }}r5   c                     U R                   $ )z
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   r:   s    r2   r   Models.cub_val       }}r5   c                     U R                   $ )z
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   r:   s    r2   r   Models.ceq_val,  r   r5   c                     U R                   (       a"  UR                  U R                  4:X  d   S5       eU R                  XR                  5      $ )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`.
rz   )r   r9   r!   r   rc   r&   r|   s     r2   fun
Models.fun:  s>      ;;77tvvi'I)II'yy..//r5   c                     U R                   (       a"  UR                  U R                  4:X  d   S5       eU R                  R	                  XR
                  5      $ )af  
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`.
rz   )r   r9   r!   r   r   rc   r   s     r2   fun_gradModels.fun_gradN  sB      ;;77tvvi'I)II'yy~~a!3!344r5   c                 L    U R                   R                  U R                  5      $ )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   rc   r:   s    r2   fun_hessModels.fun_hessb  s     yy~~d0011r5   c                     U R                   (       a"  UR                  U R                  4:X  d   S5       eU R                  R	                  XR
                  5      $ )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   r9   r!   r   r   rc   r&   r   s     r2   fun_hess_prodModels.fun_hess_prodn  sD    " ;;77tvvi'I)II'yy""1&8&899r5   c                     U R                   (       a"  UR                  U R                  4:X  d   S5       eU R                  R	                  XR
                  5      $ )ai  
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   r9   r!   r   r   rc   r   s     r2   fun_curvModels.fun_curv  sB    " ;;77tvvi'I)II'yy~~a!3!344r5   c                     U R                   (       a"  UR                  U R                  4:X  d   S5       e[        U R                  U R
                  U R                   5      nUR                  XR                  5      $ )a  
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.
rz   )r   r9   r!   rj   rc   r   r   )r&   r|   models      r2   fun_alt_gradModels.fun_alt_grad  sZ    , ;;77tvvi'I)II'$,,dllDKKHzz!//00r5   Nc           	      F   U R                   (       aG  UR                  U R                  4:X  d   S5       eUb"  UR                  U R                  4:X  d   S5       e[        R
                  " U R                  U5       Vs/ s H  o3" XR                  5      PM     sn5      $ s  snf )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.
rz   !The shape of `mask` is not valid.)r   r9   r!   r   r   array_get_cubrc   r&   r|   maskr   s       r2   cub
Models.cub  s    & ;;77tvvi'I)II'<4::##2 $ 323  xx7;}}T7JK7JeU1(()7JK
 	
K   <Bc           	      t   U R                   (       aG  UR                  U R                  4:X  d   S5       eUb"  UR                  U R                  4:X  d   S5       e[        R
                  " U R                  U5       Vs/ s H  nUR                  XR                  5      PM      snSU R                  45      $ s  snf )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.
rz   r   )	r   r9   r!   r   r   reshaper  r   rc   r  s       r2   cub_gradModels.cub_grad      & ;;77tvvi'I)II'<4::##2 $ 323  zz---/- ZZ--.-/L
 	
/   <%B5c                 D   U R                   (       a%  Ub"  UR                  U R                  4:X  d   S5       e[        R                  " U R                  U5       Vs/ s H  o"R                  U R                  5      PM     snSU R                  U R                  45      $ s  snf )aV  
Evaluate the Hessian matrices of the quadratic models of the nonlinear
inequality functions.

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

Returns
-------
`numpy.ndarray`
    Hessian matrices of the quadratic models of the nonlinear
    inequality functions.
r   r	  )	r   r9   r   r   r
  r  r   rc   r!   r&   r  r   s      r2   cub_hessModels.cub_hess        ;;<4::##2 $ 323  zz9=t9LM9LZZ**+9LM 
 	
M   $Bc           	      t   U R                   (       aG  UR                  U R                  4:X  d   S5       eUb"  UR                  U R                  4:X  d   S5       e[        R
                  " U R                  U5       Vs/ s H  nUR                  XR                  5      PM      snSU R                  45      $ s  snf )aJ  
Evaluate the right product of the Hessian matrices of the quadratic
models of the nonlinear inequality functions with a given vector.

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

Returns
-------
`numpy.ndarray`
    Right products of the Hessian matrices of the quadratic models of
    the nonlinear inequality functions with `v`.
r   r   r	  )	r   r9   r!   r   r   r
  r  r   rc   r&   r   r  r   s       r2   cub_hess_prodModels.cub_hess_prod      & ;;77tvvi'I)II'<4::##2 $ 323  zz "]]400E #5#560 L
 	
r  c           	      Z   U R                   (       aG  UR                  U R                  4:X  d   S5       eUb"  UR                  U R                  4:X  d   S5       e[        R
                  " U R                  U5       Vs/ s H  nUR                  XR                  5      PM      sn5      $ s  snf )a  
Evaluate the curvature of the quadratic models of the nonlinear
inequality functions along a given direction.

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

Returns
-------
`numpy.ndarray`
    Curvature of the quadratic models of the nonlinear inequality
    functions along `v`.
r   r   )	r   r9   r!   r   r   r  r  r   rc   r  s       r2   cub_curvModels.cub_curv&      & ;;77tvvi'I)II'<4::##2 $ 323  xx---/- ZZ--.-/
 	
/   <%B(c           	      F   U R                   (       aG  UR                  U R                  4:X  d   S5       eUb"  UR                  U R                  4:X  d   S5       e[        R
                  " U R                  U5       Vs/ s H  o3" XR                  5      PM     sn5      $ s  snf )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.
rz   r   )r   r9   r!   r   r   r  _get_ceqrc   r  s       r2   ceq
Models.ceqC  s    $ ;;77tvvi'I)II'<4::##2 $ 323  xx7;}}T7JK7JeU1(()7JK
 	
Kr  c           	      t   U R                   (       aG  UR                  U R                  4:X  d   S5       eUb"  UR                  U R                  4:X  d   S5       e[        R
                  " U R                  U5       Vs/ s H  nUR                  XR                  5      PM      snSU R                  45      $ s  snf )a  
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.
rz   r   r	  )	r   r9   r!   r   r   r
  r   r   rc   r  s       r2   ceq_gradModels.ceq_grad^  r  r  c                 D   U R                   (       a%  Ub"  UR                  U R                  4:X  d   S5       e[        R                  " U R                  U5       Vs/ s H  o"R                  U R                  5      PM     snSU R                  U R                  45      $ s  snf )aR  
Evaluate the Hessian matrices of the quadratic models of the nonlinear
equality functions.

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

Returns
-------
`numpy.ndarray`
    Hessian matrices of the quadratic models of the nonlinear equality
    functions.
r   r	  )	r   r9   r   r   r
  r   r   rc   r!   r  s      r2   ceq_hessModels.ceq_hess|  r  r  c           	      t   U R                   (       aG  UR                  U R                  4:X  d   S5       eUb"  UR                  U R                  4:X  d   S5       e[        R
                  " U R                  U5       Vs/ s H  nUR                  XR                  5      PM      snSU R                  45      $ s  snf )aD  
Evaluate the right product of the Hessian matrices of the quadratic
models of the nonlinear equality functions with a given vector.

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

Returns
-------
`numpy.ndarray`
    Right products of the Hessian matrices of the quadratic models of
    the nonlinear equality functions with `v`.
r   r   r	  )	r   r9   r!   r   r   r
  r   r   rc   r  s       r2   ceq_hess_prodModels.ceq_hess_prod  r  r  c           	      Z   U R                   (       aG  UR                  U R                  4:X  d   S5       eUb"  UR                  U R                  4:X  d   S5       e[        R
                  " U R                  U5       Vs/ s H  nUR                  XR                  5      PM      sn5      $ s  snf )a  
Evaluate the curvature of the quadratic models of the nonlinear
equality functions along a given direction.

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

Returns
-------
`numpy.ndarray`
    Curvature of the quadratic models of the nonlinear equality
    functions along `v`.
r   r   )	r   r9   r!   r   r   r  r   r   rc   r  s       r2   ceq_curvModels.ceq_curv  r  r  c                    [        U R                  U R                  U R                  5      U l        [        U R                  5       HB  n[        U R                  U R                  SS2U4   U R                  5      U R                  U'   MD     [        U R                  5       HB  n[        U R                  U R                  SS2U4   U R                  5      U R                  U'   MD     U R                  (       a  U R                  5         gg)z
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)rj   rc   r   r   r   r$   r   r   r   r   r   r   r   )r&   r   s     r2   reset_modelsModels.reset_models  s     d00$,,L	t**+A$""QT"DIIaL , t**+A$""QT"DIIaL , ;;002 r5   c           	         U R                   (       a  SUs=::  a  U R                  :  d   S5       e   S5       eUR                  U R                  4:X  d   S5       e[	        U[
        5      (       d   S5       eUR                  U R                  4:X  d   S5       eUR                  U R                  4:X  d   S5       e[        R                  " U R                  5      n[        R                  " U R                  R                  5      n[        R                  " U R                  R                  5      nX0R                  U5      -
  Xa'   X@R                  U5      -
  XqSS24'   XPR                  U5      -
  XSS24'   X0R                  U'   X@R                  USS24'   XPR                  USS24'   [        R                   " U R"                  R$                  SS2U4   5      n	X R"                  R&                  -
  U R"                  R$                  SS2U4'   U R(                  R+                  U R"                  UU	U5      n
[-        U R                  5       H>  nU
=(       d2    U R.                  U   R+                  U R"                  UU	USS2U4   5      n
M@     [-        U R                  5       H>  nU
=(       d2    U R0                  U   R+                  U R"                  UU	USS2U4   5      n
M@     U R                   (       a  U R3                  5         U
$ )aG  
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?   r9   r!   
isinstancefloatr   r   r   r    r   r   r   r  r!  r   r   rc   r%   r   r   r   r$   r   r   r   )r&   r   x_newr   r   r   fun_diffcub_diffceq_diffr   r   r   s               r2   update_interpolationModels.update_interpolation  s   8 ;;((K*KK(K*KK(;;466)+ 545+gu-- 323-==##%  656  ==##%  656 
 88DHH%88DLL../88DLL../!HHUO3$xx6$xx6 &U!(UAX!(UAX ''$,,00E:;+03E3E3L3L+Lq%x( ))**	
 t**+A- 11D1D""A	2O , t**+A- 11D1D""A	2O , ;;002r5   c                 x   U R                   (       aL  UR                  U R                  4:X  d   S5       eUb'  SUs=::  a  U R                  :  d    S5       e   S5       eXR                  R
                  -
  n[        R                  " U R                  U R                  -   S-   S45      nSU R                  R                  R                  U-  S-  -  USU R                  2S4'   SX@R                  S4'   X4U R                  S-   S2S4'   [        R                  U R                  U5      S   nSX3-  S-  -  USS2S4   USS2S4   -  -
  nUc  [        R                  " U R                  U R                  -   S-   U R                  5      n[        R                  " [        R                  U R                  U5      S   5      nUSU R                  2S4   n	Oa[        R                  " U R                  U R                  -   S-   SU* 5      n[        R                  U R                  U5      S   US4   nXRS4   n	X-  U	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.
r3  Nr   r   r   r   r   rY   )r   r9   r!   r?   rc   r   r   rb   r%   ra   rj   r   eyediag)
r&   r6  r   r   new_colinv_new_colbeta	coord_vecalphataus
             r2   determinantsModels.determinants@  s=   H ;;;;466)+ 545+ e!6dhh!61016!61016 **111((DHHtvv-1156t))--//%7C?? 	
$((
A"!$)1q !--d.@.@'J1Mem++gadmk!Q$>O.OO =txx$&&014dhh?IGG''&& E jj!m,Ctxx$&&014a%@I++"" 	
 QhE Qh'C|c3h&&r5   c                    U R                   (       a"  UR                  U R                  4:X  d   S5       eU R                  R	                  U R
                  U5        U R                   H  nUR	                  U R
                  U5        M!     U R                   H  nUR	                  U R
                  U5        M!     XR
                  R                  -
  nU R
                  =R                  U-  sl        U R
                  =R                  USS2[        R                  4   -  sl	        U[        R                     (       a  U R                  5         gg)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   r9   r!   r   r   rc   r   r   r   r%   r   r   r   r   r   )r&   r   r(   r   r   s        r2   r   Models.shift_x_base  s	    ;;##(  989 
 			t11:>YYEt11:> YYEt11:>  //666!!U*!%2::"667==!002 "r5   c                 >    Uc  U R                   $ U R                   U   $ )a  
Get the quadratic models of the nonlinear inequality constraints.

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

Returns
-------
`numpy.ndarray`
    Quadratic models of the nonlinear inequality constraints.
)r   r&   r  s     r2   r  Models._get_cub       !Ltyy=diio=r5   c                 >    Uc  U R                   $ U R                   U   $ )a  
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   rJ  s     r2   r   Models._get_ceq  rL  r5   c                 $   SnSnSn[        U R                  5       GH2  n[        R                  " U[        R                  " U R                  U R                  R                  U5      5      U R                  U   -
  5      /5      n[        R                  " [        R                  " U R                  U R                  R                  U5      5      U R                  USS24   -
  5      US9n[        R                  " [        R                  " U R                  U R                  R                  U5      5      U R                  USS24   -
  5      US9nGM5     S[        R                  " [        5      -  [        U R                  U R                  5      -  nX[        R                  " [        R                  " U R                  5      SS9-  :  a  [         R"                  " S[$        S5        X%[        R                  " [        R                  " U R                  5      SS9-  :  a  [         R"                  " S[$        S5        X5[        R                  " [        R                  " U R                  5      SS9-  :  a  [         R"                  " S	[$        S5        gg)
z=
Check the interpolation conditions of all quadratic models.
r   NrW   g      $@rY   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   r]   r   r   rc   rH   r   r  r   r!  r   sqrtr`   r!   warningswarnRuntimeWarning)r&   	error_fun	error_cub	error_ceqr.   tols         r2   r   &Models._check_interpolation_conditions  s    			txxAFF!3!3!9!9!!<=QOI HHT//55a89DLLA<NN "	I HHT//55a89DLLA<NN "	I !* RWWS\!C$99RVVBFF4<<$8#FFFMM!	 RVVBFF4<<$8#FFFMM.	 RVVBFF4<<$8#FFFMM.	 Gr5   )r   r   r   r   r   r   r   r   )N)'rJ   rK   rL   rM   rN   r3   rO   r!   r?   r   r   rc   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r!  r$  r'  r*  r-  r0  r:  rE  r   r  r   r   rQ   rR   r5   r2   r   r   n  s'   a3F 	$ 	$ 	& 	& 	% 	% 	% 	% 	# 	# 	 	    0(5(
2:*5*16
8
<
2
@
:
6
<
2
@
:36QfH'T3<> > 1r5   r   )rQ  numpyr   scipy.linalgr   settingsr   utilsr   r   r   finfor5  epsr`   r
   r[   rh   rj   r   rR   r5   r2   <module>r_     sj        ? ? 	hhuor, r,j D4	F.7buF uFpK Kr5   