
    bim                     z   d dl Z 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 d dlmZmZmZmZmZmZmZ  G d d      Z G d d	      Z G d
 d      Z G d d      Z G d d      Z G d de      Z G d de      Z G d de      Z G d d      Z G d d      Z G d d      Zd Zd Z d Z!d Z" G d  d!      Z#y)"    N)
block_diag)	csc_array)assert_array_almost_equalassert_array_lessassert_)NonlinearConstraintLinearConstraintBoundsminimizeBFGSSR1rosenc                   :    e Zd ZdZddZd Zd Zd Zed        Z	y)	MaratosProblem 15.4 from Nocedal and Wright

    The following optimization problem:
        minimize 2*(x[0]**2 + x[1]**2 - 1) - x[0]
        Subject to: x[0]**2 + x[1]**2 - 1 = 0
    Nc                     |dz  t         j                  z  }t        j                  |      t        j                  |      g| _        t        j
                  ddg      | _        || _        || _        d | _	        y N         ?        
nppicossinx0arrayx_opt
constr_jacconstr_hessboundsselfdegreesr   r    radss        i/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/scipy/optimize/tests/test_minimize_constrained.py__init__zMaratos.__init__   Z    s{255 66$<.XXsCj)
$&    c                 <    d|d   dz  |d   dz  z   dz
  z  |d   z
  S N   r       r#   xs     r&   funzMaratos.fun"   s0    !A$'AaD!G#a'(1Q4//r)   c                 N    t        j                  d|d   z  dz
  d|d   z  g      S N   r   r-   r   r   r/   s     r&   gradzMaratos.grad%   s*    xx1Q41QqT6*++r)   c                 2    dt        j                  d      z  S Nr4   r,   r   eyer/   s     r&   hesszMaratos.hess(       {r)   c                     d }| j                   d }n| j                   }| j                  d }n| j                  }t        |dd||      S )Nc                 $    | d   dz  | d   dz  z   S Nr   r,   r-   r.   r0   s    r&   r1   zMaratos.constr.<locals>.fun-       Q47QqT1W$$r)   c                 $    d| d   z  d| d   z  ggS r+   r.   r@   s    r&   jaczMaratos.constr.<locals>.jac1        1Q41Q4())r)   c                 >    d|d   z  t        j                  d      z  S Nr,   r   r9   r0   vs     r&   r;   zMaratos.constr.<locals>.hess7       1vbffQi''r)   r-   r   r    r   r#   r1   rC   r;   s       r&   constrzMaratos.constr+   T    	% ??"* //C#( ##D"31c488r)   <   NN
__name__
__module____qualname____doc__r'   r1   r6   r;   propertyrL   r.   r)   r&   r   r      s/    0, 9 9r)   r   c                   @    e Zd ZdZd	dZd Zd Zd Zd Ze	d        Z
y)
MaratosTestArgsr   Nc                    |dz  t         j                  z  }t        j                  |      t        j                  |      g| _        t        j
                  ddg      | _        || _        || _        || _	        || _
        d | _        y r   )r   r   r   r   r   r   r   r   r    abr!   )r#   rY   rZ   r$   r   r    r%   s          r&   r'   zMaratosTestArgs.__init__G   sh    s{255 66$<.XXsCj)
$&r)   c                 T    | j                   |k7  s| j                  |k7  r
t               y N)rY   rZ   
ValueError)r#   rY   rZ   s      r&   
_test_argszMaratosTestArgs._test_argsQ   s$    66Q;$&&A+, &r)   c                 `    | j                  ||       d|d   dz  |d   dz  z   dz
  z  |d   z
  S r+   )r^   r#   r0   rY   rZ   s       r&   r1   zMaratosTestArgs.funU   s>    1!A$'AaD!G#a'(1Q4//r)   c                 r    | j                  ||       t        j                  d|d   z  dz
  d|d   z  g      S r3   )r^   r   r   r`   s       r&   r6   zMaratosTestArgs.gradY   s8    1xx1Q41QqT6*++r)   c                 V    | j                  ||       dt        j                  d      z  S r8   )r^   r   r:   r`   s       r&   r;   zMaratosTestArgs.hess]   s"    1{r)   c                     d }| j                   d }n| j                   }| j                  d }n| j                  }t        |dd||      S )Nc                 $    | d   dz  | d   dz  z   S r?   r.   r@   s    r&   r1   z#MaratosTestArgs.constr.<locals>.func   rA   r)   c                 $    d| d   z  d| d   z  ggS r3   r.   r@   s    r&   rC   z#MaratosTestArgs.constr.<locals>.jacg   rD   r)   c                 >    d|d   z  t        j                  d      z  S rF   r9   rG   s     r&   r;   z$MaratosTestArgs.constr.<locals>.hessm   rI   r)   r-   rJ   rK   s       r&   rL   zMaratosTestArgs.constra   rM   r)   rN   )rQ   rR   rS   rT   r'   r^   r1   r6   r;   rU   rL   r.   r)   r&   rW   rW   ?   s4    0, 9 9r)   rW   c                   D    e Zd ZdZddZd Zed        Zd Zed        Z	y)	MaratosGradInFuncr   Nc                     |dz  t         j                  z  }t        j                  |      t        j                  |      g| _        t        j
                  ddg      | _        || _        || _        d | _	        y r   r   r"   s        r&   r'   zMaratosGradInFunc.__init__}   r(   r)   c                     d|d   dz  |d   dz  z   dz
  z  |d   z
  t        j                  d|d   z  dz
  d|d   z  g      fS )Nr,   r   r-   r4   r5   r/   s     r&   r1   zMaratosGradInFunc.fun   s\    1Q47QqT1W$q()AaD0!AaD&(AadF+,. 	.r)   c                      y)NTr.   r#   s    r&   r6   zMaratosGradInFunc.grad   s    r)   c                 2    dt        j                  d      z  S r8   r9   r/   s     r&   r;   zMaratosGradInFunc.hess   r<   r)   c                     d }| j                   d }n| j                   }| j                  d }n| j                  }t        |dd||      S )Nc                 $    | d   dz  | d   dz  z   S r?   r.   r@   s    r&   r1   z%MaratosGradInFunc.constr.<locals>.fun   rA   r)   c                 $    d| d   z  d| d   z  ggS r3   r.   r@   s    r&   rC   z%MaratosGradInFunc.constr.<locals>.jac   rD   r)   c                 >    d|d   z  t        j                  d      z  S rF   r9   rG   s     r&   r;   z&MaratosGradInFunc.constr.<locals>.hess   rI   r)   r-   rJ   rK   s       r&   rL   zMaratosGradInFunc.constr   rM   r)   rN   )
rQ   rR   rS   rT   r'   r1   rU   r6   r;   rL   r.   r)   r&   rh   rh   u   s>    .   9 9r)   rh   c                   :    e Zd ZdZddZd Zd Zd Zed        Z	y)	HyperbolicIneqa  Problem 15.1 from Nocedal and Wright

    The following optimization problem:
        minimize 1/2*(x[0] - 2)**2 + 1/2*(x[1] - 1/2)**2
        Subject to: 1/(x[0] + 1) - x[1] >= 1/4
                                   x[0] >= 0
                                   x[1] >= 0
    Nc                     ddg| _         ddg| _        || _        || _        t	        dt
        j                        | _        y )Nr   g~T>?g~1[?)r   r   r   r    r
   r   infr!   )r#   r   r    s      r&   r'   zHyperbolicIneq.__init__   s:    a&)
$&Q'r)   c                 <    d|d   dz
  dz  z  d|d   dz
  dz  z  z   S )N      ?r   r,   r-   r.   r/   s     r&   r1   zHyperbolicIneq.fun   s/    AaD1Hq= 3!s
Q#666r)   c                 "    |d   dz
  |d   dz
  gS )Nr   r,   r-   rw   r.   r/   s     r&   r6   zHyperbolicIneq.grad   s    !q!A$*%%r)   c                 ,    t        j                  d      S Nr,   r9   r/   s     r&   r;   zHyperbolicIneq.hess   s    vvayr)   c                     d }| j                   d }n| j                   }| j                  d }n| j                  }t        |dt        j                  ||      S )Nc                 $    d| d   dz   z  | d   z
  S )Nr-   r   r.   r@   s    r&   r1   z"HyperbolicIneq.constr.<locals>.fun   s    adQh<!A$&&r)   c                 $    d| d   dz   dz  z  dggS )Nr   r-   r,   r.   r@   s    r&   rC   z"HyperbolicIneq.constr.<locals>.jac   s!    QqTAXM)2.//r)   c                 b    d|d   z  t        j                  d| d   dz   dz  z  dgddgg      z  S )Nr,   r   r-      r5   rG   s     r&   r;   z#HyperbolicIneq.constr.<locals>.hess   sE    1vbhhAaD1Hq=!(<)*A(0 1 1 1r)   g      ?r   r    r   r   ru   rK   s       r&   rL   zHyperbolicIneq.constr   sX    	' ??"0 //C#1 ##D"3bffc4@@r)   )NNrP   r.   r)   r&   rs   rs      s1    (7& A Ar)   rs   c                   :    e Zd ZdZddZd Zd Zd Zed        Z	y)	
RosenbrockzRosenbrock function.

    The following optimization problem:
        minimize sum(100.0*(x[1:] - x[:-1]**2.0)**2.0 + (1 - x[:-1])**2.0)
    c                     t         j                  j                  |      }|j                  dd|      | _        t        j
                  |      | _        d | _        y )Nr~   r-   )r   randomRandomStateuniformr   onesr   r!   )r#   nrandom_staterngs       r&   r'   zRosenbrock.__init__   s@    ii##L1++b!Q'WWQZ
r)   c                     t        j                  |      }t        j                  d|dd  |d d dz  z
  dz  z  d|d d z
  dz  z   d      }|S )Ng      Y@r-   r~          @r   axis)r   asarraysum)r#   r0   rs      r&   r1   zRosenbrock.fun   sZ    JJqMFF5AabEAcrFCK/#55QsVc8IIr)   c                 8   t        j                  |      }|dd }|d d }|dd  }t        j                  |      }d||dz  z
  z  d||dz  z
  z  |z  z
  dd|z
  z  z
  |dd d|d   z  |d   |d   dz  z
  z  dd|d   z
  z  z
  |d<   d|d   |d   dz  z
  z  |d<   |S )	Nr-   r~   r,        pr   )r   r   
zeros_like)r#   r0   xmxm_m1xm_p1ders         r&   r6   zRosenbrock.grad   s    JJqMqW#2!"mmABM*EBEM*R/023q2v,?Ab	!!qtQw/!q1Q4x.@A22)*B
r)   c                    t        j                  |      }t        j                  d|d d z  d      t        j                  d|d d z  d      z
  }t        j                  t	        |      |j
                        }d|d   dz  z  d|d   z  z
  dz   |d<   d	|d<   d
d|dd dz  z  z   d|dd  z  z
  |dd |t        j                  |      z   }|S )Nr   r~   r-   r   )dtypei  r   r,   r      )r   
atleast_1ddiagzeroslenr   )r#   r0   Hdiagonals       r&   r;   zRosenbrock.hess   s    MM!GGD1Sb6M1%afb(AA88CF!''2QqT1WnsQqTz1A5ta"gqj00312;>2!!r)   c                      y)Nr.   r.   rl   s    r&   rL   zRosenbrock.constr   s    r)   N)r,   r   rP   r.   r)   r&   r   r      s/    
  r)   r   c                   (    e Zd ZdZddZed        Zy)IneqRosenbrockzRosenbrock subject to inequality constraints.

    The following optimization problem:
        minimize sum(100.0*(x[1] - x[0]**2)**2.0 + (1 - x[0])**2)
        subject to: x[0] + 2 x[1] <= 1

    Taken from matlab ``fmincon`` documentation.
    c                 d    t         j                  | d|       ddg| _        ddg| _        d | _        y )Nr,   r~         gn?g$?r   r'   r   r   r!   r#   r   s     r&   r'   zIneqRosenbrock.__init__	  s2    D!\2t*f%
r)   c                 H    ddgg}d}t        |t        j                   |      S Nr-   r,   r	   r   ru   )r#   ArZ   s      r&   rL   zIneqRosenbrock.constr  s'    VHBFF7A..r)   Nr   rQ   rR   rS   rT   r'   rU   rL   r.   r)   r&   r   r      s      / /r)   r   c                       e Zd ZdZddZy)BoundedRosenbrocka  Rosenbrock subject to inequality constraints.

    The following optimization problem:
        minimize sum(100.0*(x[1] - x[0]**2)**2.0 + (1 - x[0])**2)
        subject to:  -2 <= x[0] <= 0
                      0 <= x[1] <= 2

    Taken from matlab ``fmincon`` documentation.
    c                 |    t         j                  | d|       ddg| _        d | _        t	        ddgddg      | _        y )Nr,   gɿg?r   r   )r   r'   r   r   r
   r!   r   s     r&   r'   zBoundedRosenbrock.__init__   s<    D!\2+
b!Wq!f-r)   Nr   )rQ   rR   rS   rT   r'   r.   r)   r&   r   r     s    .r)   r   c                   (    e Zd ZdZddZed        Zy)EqIneqRosenbrocka*  Rosenbrock subject to equality and inequality constraints.

    The following optimization problem:
        minimize sum(100.0*(x[1] - x[0]**2)**2.0 + (1 - x[0])**2)
        subject to: x[0] + 2 x[1] <= 1
                    2 x[0] + x[1] = 1

    Taken from matlab ``fimincon`` documentation.
    c                 d    t         j                  | d|       ddg| _        ddg| _        d | _        y )Nr,   r~   r   gWs`?g|\*?r   r   s     r&   r'   zEqIneqRosenbrock.__init__1  s2    D!\2t*w'
r)   c                 p    ddgg}d}ddgg}d}t        |t        j                   |      t        |||      fS r   r   )r#   A_ineqb_ineqA_eqb_eqs        r&   rL   zEqIneqRosenbrock.constr7  sJ    a&Ax "&&&9 tT24 	4r)   Nr   r   r.   r)   r&   r   r   '  s      4 4r)   r   c                   J    e Zd ZdZ	 	 d
dZd Zd Zd Zd Zd Z	e
d	        Zy)Eleca  Distribution of electrons on a sphere.

    Problem no 2 from COPS collection [2]_. Find
    the equilibrium state distribution (of minimal
    potential) of the electrons positioned on a
    conducting sphere.

    References
    ----------
    .. [1] E. D. Dolan, J. J. Mor'{e}, and T. S. Munson,
           "Benchmarking optimization software with COPS 3.0.",
            Argonne National Lab., Argonne, IL (US), 2004.
    Nc                    || _         t        j                  j                  |      | _        | j                  j                  ddt        j                  z  | j                         }| j                  j                  t        j                   t        j                  | j                         }t        j                  |      t        j                  |      z  }t        j                  |      t        j                  |      z  }t        j                  |      }	t        j                  |||	f      | _
        d | _        || _        || _        d | _        y )Nr   r,   )n_electronsr   r   r   r   r   r   r   r   hstackr   r   r   r    r!   )
r#   r   r   r   r    phithetar0   yzs
             r&   r'   zElec.__init__O  s    &99((6hhq!bee)T-=-=>  "%%0@0@AFF5MBFF3K'FF5MBFF3K'FF5M))Q1I&
$&r)   c                     |d | j                    }|| j                   d| j                   z   }|d| j                   z  d  }|||fS rz   r   )r#   r0   x_coordy_coordz_coords        r&   _get_cordinateszElec._get_cordinates_  sW    %T%%&D$$Q)9)9%9:A((()*((r)   c                 ~    | j                  |      \  }}}|d d d f   |z
  }|d d d f   |z
  }|d d d f   |z
  }|||fS r\   r   )r#   r0   r   r   r   dxdydzs           r&   _compute_coordinate_deltaszElec._compute_coordinate_deltase  s^    $($8$8$;!'QW'QW'QW'2rzr)   c                 
   | j                  |      \  }}}t        j                  d      5  |dz  |dz  z   |dz  z   dz  }d d d        dt        j                  |      <   dt        j                  |      z  S # 1 sw Y   9xY w)Nignoredivider,   r   r   rw   )r   r   errstatediag_indices_fromr   )r#   r0   r   r   r   dm1s         r&   r1   zElec.funl  s    44Q7
B[[) 	2q52q5=2q5(T1C	2)*B  %&RVVC[  	2 	2s   A99Bc                    | j                  |      \  }}}t        j                  d      5  |dz  |dz  z   |dz  z   dz  }d d d        dt        j                  |      <   t        j                  ||z  d       }t        j                  ||z  d       }t        j                  ||z  d       }t        j
                  |||f      S # 1 sw Y   xY w)Nr   r   r,         r   r-   r   )r   r   r   r   r   r   )	r#   r0   r   r   r   dm3grad_xgrad_ygrad_zs	            r&   r6   z	Elec.grads  s    44Q7
B[[) 	2q52q5=2q5(T1C	2)*B  %&&&c**&&c**&&c**yy&&&122	2 	2s   C

Cc           	         | j                  |      \  }}}|dz  |dz  z   |dz  z   dz  }t        j                  d      5  |dz  }|dz  }d d d        t        j                  | j                        }d||f<   d||f<   |d|dz  z  |z  z
  }	t        j
                  |	d	
       |	||f<   d|z  |z  |z  }
t        j
                  |
d	
       |
||f<   d|z  |z  |z  }t        j
                  |d	
       |||f<   |d|dz  z  |z  z
  }t        j
                  |d	
       |||f<   d|z  |z  |z  }t        j
                  |d	
       |||f<   |d|dz  z  |z  z
  }t        j
                  |d	
       |||f<   t        j                  t        j                  |	|
|f      t        j                  |
||f      t        j                  |||f      f      }|S # 1 sw Y   xY w)Nr,   rw   r   r   r   r   r-   r   )r   r   r   aranger   r   vstackr   )r#   r0   r   r   r   dr   dm5iHxxHxyHxzHyyHyzHzzr   s                   r&   r;   z	Elec.hess  s   44Q7
BURU]RU"s*[[) 	r'Cr'C	 IId&&'AqD	AqD	AAIO#VVCa((AqD	2glS VVCa((AqD	2glS VVCa((AqD	AAIO#VVCa((AqD	2glS VVCa((AqD	AAIO#VVCa((AqD	IIIIsCo&IIsCo&IIsCo&
  A	 	s    GGc                       fd} j                    fd}n j                   } j                  d }n j                  }t        |t        j                   d||      S )Nc                 V    j                  |       \  }}}|dz  |dz  z   |dz  z   dz
  S )Nr,   r-   r   )r0   r   r   r   r#   s       r&   r1   zElec.constr.<locals>.fun  s;    (,(<(<Q(?%GWgA:
*WaZ7!;;r)   c                     j                  |       \  }}}dt        j                  |      z  }dt        j                  |      z  }dt        j                  |      z  }t        t        j                  |||f            S rz   )r   r   r   r   r   )r0   r   r   r   JxJyJzr#   s          r&   rC   zElec.constr.<locals>.jac  sm    ,0,@,@,C)')))))) BB<!899r)   c                 L    dt        j                  |      z  }t        |||      S rz   )r   r   r   )r0   rH   Ds      r&   r;   zElec.constr.<locals>.hess  s"    
N!!Q**r)   r   r   rK   s   `   r&   rL   zElec.constr  s[    	< ??": //C#+ ##D"3C>>r)   )r   r   NN)rQ   rR   rS   rT   r'   r   r   r1   r6   r;   rU   rL   r.   r)   r&   r   r   A  sB     67.2 )!3$L ? ?r)   r   c                   L   e Zd Z e        ed       e e              ed e              e        e        ed       e e              ed e              e        e	        e
        e        ed       edd       ed e              edd e             gZej                  j!                  d	e      ej                  j!                  d
d      ej                  j!                  dddd d d f      d                      Zd Zd Zd Zd Zd Zd Zd Zd Zy)TestTrustRegionConstr2-point)r    )r   r    3-pointr,   r   )r   r    )r   r   r    probr6   )	prob.gradr   Fr;   	prob.hessc                      t               S r\   )r   r.   r)   r&   <lambda>zTestTrustRegionConstr.<lambda>  s    ce r)   c                      t        d      S )Ndamp_updateexception_strategyr   r.   r)   r&   r   zTestTrustRegionConstr.<lambda>      dm.T r)   c                      t        d      S )Nskip_updater  r  r.   r)   r&   r   zTestTrustRegionConstr.<lambda>  r  r)   c           
          |dk(  r|j                   n|}t        |      r |       n|}|dk(  r|j                  n|}|dv r|dv rt        j                  d       |j                   du r|dv rt        j                  d       t        |t              xr |d	k(  xr t        |t              }|rt        j                  d
       t        j                         5  t        j                  ddt               t        |j                  |j                  d|||j                   |j"                        }d d d        |j$                  Gt'        j(                  |j$                  d       |j*                  dk(  rt-        |j.                  d       j*                  dk(  r;t-        |j0                  d       |j2                  dk(  rt-        |j4                  d       d|j*                   d}|j*                  dvsJ |       y # 1 sw Y   xY w)Nr   r   >   Fcsr   r   >   r  r   r   z+Numerical Hessian needs analytical gradientT>   Fr   z6prob.grad incompatible with grad in {'3-point', False}r   z3Seems sensitive to initial conditions w/ Accelerater   delta_grad == 0.0trust-constrmethodrC   r;   r!   constraints   decimalr-   :0yE>r,   tr_interior_pointzInvalid termination condition: .>   r   r   )r6   callabler;   pytestskip
isinstancer   r   xfailwarningscatch_warningsfilterwarningsUserWarningr   r1   r   r!   rL   r   r   r0   statusr   
optimality	tr_radiusr  barrier_parameter)r#   r   r6   r;   	sensitiveresultmessages          r&   test_list_of_problemsz+TestTrustRegionConstr.test_list_of_problems  s    !K/tyyT!$tvT K/tyyT7744KKEF99);!;KKPQ&78 0TY=N 0#D$/ 	LLNO$$& 	7##H.A;Odhh%3"&T%)[[*.++	7F	7 ::!%fhh

./1 }}!!&"3"3T:==Af..5}} 33!&":":DA 4FMM?!D}}F*3G3*/	7 	7s   AG44G=c                 `    d }dg}t        |dg|d      }t        |j                  dd       y )	Nc                     | dz
  dz  S r   r.   r@   s    r&   r1   z<TestTrustRegionConstr.test_default_jac_and_hess.<locals>.fun      Ea<r)   r   r,   r   r
  )r   r!   r  r-   r  r  r   r   r0   r#   r1   r!   ress       r&   test_default_jac_and_hessz/TestTrustRegionConstr.test_default_jac_and_hess  s0    	 svf^L!#%%A6r)   c                 b    d }dg}t        |dg|dd      }t        |j                  dd	       y )
Nc                     | dz
  dz  S r   r.   r@   s    r&   r1   z4TestTrustRegionConstr.test_default_hess.<locals>.fun
  r'  r)   r(  r   r
  r   )r   r!   r  rC   r-   r  r  r)  r*  s       r&   test_default_hessz'TestTrustRegionConstr.test_default_hess	  s5    	 svf^$&!#%%A6r)   c                    t               }t        |j                  |j                  d|j                  |j
                        }t        |j                  |j                  dd      }t        |j                  |j                  dd      }t        |j                  |j                  d       t        |j                  |j                  d       t        |j                  |j                  d       y )	Nr
  )r  rC   r;   zL-BFGS-Br   )r  rC   r   r  r  )	r   r   r1   r   r6   r;   r   r0   r   )r#   r   r"  result1result2s        r&   test_no_constraintsz)TestTrustRegionConstr.test_no_constraints  s    |$((DGG!/"iidii9 488TWW",(* 488TWW",(* 	"&((DJJB!'))TZZC!'))TZZCr)   c           	         t               fd}t        j                  j                  dj                  |j
                  j                        }j                  "t        |j                  j                  d       |j                  dk(  rt        |j                  d       |j                  dk(  r;t        |j                  d       |j                  dk(  rt        |j                  d       |j                  d	v rt!        d
      y )Nc                 H    j                  |       }|j                  |      S r\   )r;   dot)r0   pr   r   s      r&   hesspz/TestTrustRegionConstr.test_hessp.<locals>.hessp$  s    		!A558Or)   r
  )r  rC   r8  r!   r  r,   r  r-   r  r  r   r   Invalid termination condition.)r   r   r1   r   r6   r!   rL   r   r   r0   r  r   r  r  r  r   RuntimeError)r#   r8  r"  r   s      @r&   
test_hesspz TestTrustRegionConstr.test_hessp!  s    y	 $((DGG!/"iiu!%&*kk	3 ::!%fhh

AF ==Af//6==Af..5}} 33!&":":DA==F"?@@ #r)   c           
      &   t        dd      }t        |j                  |j                  dd|j                  |j
                  |j                  |j                        }|j                  "t        |j                  |j                  d       |j                  dk(  rt        |j                  d	       |j                  dk(  r;t        |j                  d	       |j                  d
k(  rt        |j                   d	       |j                  dv rt#        d      y )NrY      )rY   r>  r
  r  r,   r  r-   r  r  r9  r:  )rW   r   r1   r   r6   r;   r!   rL   r   r   r0   r  r   r  r  r  r   r;  )r#   r   r"  s      r&   	test_argszTestTrustRegionConstr.test_args>  s    sC($((DGGZ!/"iidii!%&*kk	3 ::!%fhh

AF ==Af//6==Af..5}} 33!&":":DA==F"?@@ #r)   c           	          t               }d}t        j                  t        |      5  t	        |j
                  |j                  ddd|j                         d d d        y # 1 sw Y   y xY w)Nz9Whenever the gradient is estimated via finite-differencesmatchr
  r   )r  rC   r;   r  )r   r  raisesr]   r   r1   r   rL   )r#   r   r#  s      r&   test_raise_exceptionz*TestTrustRegionConstr.test_raise_exceptionV  sR    yM]]:W5 	>TXXtww~9#>	> 	> 	>s   0A!!A*c                     d }t        d dgd d |d      }t        |j                  d             t        |j                  d	d
      dk(         t        |j                  dd
      dk(         y )Nc                 8    t        d|v        t        d|v        y )Nnitniter)r   )r0   infos     r&   callbackz7TestTrustRegionConstr.test_issue_9044.<locals>.callbackb  s    ETM"GtO$r)   c                     | dz  S rz   r.   r@   s    r&   r   z7TestTrustRegionConstr.test_issue_9044.<locals>.<lambda>f  s
    AqD r)   r   c                     d| z  S rz   r.   r@   s    r&   r   z7TestTrustRegionConstr.test_issue_9044.<locals>.<lambda>f  s
    QqS r)   c                      yrz   r.   r@   s    r&   r   z7TestTrustRegionConstr.test_issue_9044.<locals>.<lambda>g  s    r)   r
  )rC   r;   rJ  r  successrG  r~   r-   rH  )r   r   get)r#   rJ  r"  s      r&   test_issue_9044z%TestTrustRegionConstr.test_issue_9044]  sh    
	% .1#=*X!/1 	

9%&

5"%*+ 	

7B'1,-r)   c                 \   t        j                  ddg      }d }t        t        j                  ddg      t        j                  ddg      d      }t        j                         5  t        j
                  ddt               t        d	|||
      }d d d        d   sJ y # 1 sw Y   xY w)Nr   rw   c                 ,    | d   }| d   }|dz  |dz  z   S )Nr   r-   r,   r.   )r0   x1x2s      r&   objz3TestTrustRegionConstr.test_issue_15093.<locals>.objx  s'    1B1B7R1W$$r)   r   T)keep_feasibler   r	  r
  )r  r1   r   r!   rN  )r   r   r
   r  r  r  r  r   )r#   r   rU  r!   r"  s        r&   test_issue_15093z&TestTrustRegionConstr.test_issue_15093p  s     XXr3i 	%
 "b*BHHb"X,>&*, $$& 	##H.A;O%	F	 i   	 	s   '+B""B+N)rQ   rR   rS   r   r   rh   rs   r   r   r   r   r   r   list_of_problemsr  markparametrizer$  r,  r/  r3  r<  r?  rD  rP  rW  r.   r)   r&   r   r     s:   	I6CE29#%H)+&(&9=&46:&)376;"&((*)++	B>y),0#1( [[V%56[[V%DE[[Vk9m&T&T&V W%4W F 7
%4P77D A:A0>.&!r)   r   c                       e Zd ZdZd Zy)TestEmptyConstrainta  
    Here we minimize x^2+y^2 subject to x^2-y^2>1.
    The actual minimum is at (0, 0) which fails the constraint.
    Therefore we will find a minimum on the boundary at (+/-1, 0).

    When minimizing on the boundary, optimize uses a set of
    constraints that removes the constraint that sets that
    boundary.  In our case, there's only one constraint, so
    the result is an empty constraint.

    This tests that the empty constraint works.
    c           	         d }d }d }d }d }d }t        |dt        j                  ||      }ddg}t        t        j                   t        j                   gt        j                  t        j                  g      }t	        ||d	|||g|
      }	t        t        |	j                        t        j                  ddg      d       y )Nc                 $    | d   dz  | d   dz  z   S r?   r.   r@   s    r&   functionz;TestEmptyConstraint.test_empty_constraint.<locals>.function  rA   r)   c                 H    t        j                  d| d   z  d| d   z  g      S )Nr   r   r-   r5   r@   s    r&   functionjacobianzCTestEmptyConstraint.test_empty_constraint.<locals>.functionjacobian  s&    88R!Wb1g.//r)   c                     d|z  S )Nr   r.   rG   s     r&   functionhvpz>TestEmptyConstraint.test_empty_constraint.<locals>.functionhvp  s    a4Kr)   c                 L    t        j                  | d   dz  | d   dz  z
  g      S r?   r5   r@   s    r&   
constraintz=TestEmptyConstraint.test_empty_constraint.<locals>.constraint  s)    88QqT1WqtQw./00r)   c                 J    t        j                  d| d   z  d| d   z  gg      S )Nr,   r   r   r-   r5   r@   s    r&   constraintjacobianzETestEmptyConstraint.test_empty_constraint.<locals>.constraintjacobian  s)    88a!fb1g./00r)   c                 D    t        j                  ddgddgg      |d   z  S )Nr   r   g       r   r5   rG   s     r&   constraintlcohzATestEmptyConstraint.test_empty_constraint.<locals>.constraintlcoh  s'    88b"XCy12QqT99r)   r   r   r
  )r  rC   r8  r  r!   r-   r   r4   r  )	r   r   ru   r
   r   r   absr0   r   )
r#   r_  ra  rc  re  rg  ri  
startpointr!   r"  s
             r&   test_empty_constraintz)TestEmptyConstraint.test_empty_constraint  s    	%	0		1	1	: )R);^M
 "X
"&&266'*RVVRVV,<=

!l
 	"#fhh-1a&1A1Mr)   N)rQ   rR   rS   rT   rl  r.   r)   r&   r\  r\    s    %Nr)   r\  c                  >   d } t        j                         5  t        j                  dt               t	        j
                  t	        j                  ddg            }d d d        t        dt        j                        }t        | ddgz  |       y # 1 sw Y   7xY w)Nc                 $    | d   dz  | d   dz  z   S r?   r.   r@   s    r&   optztest_bug_11886.<locals>.opt  s    tQwqtQwr)   r   r-   r~   r,   )r  )
r  r  simplefilterPendingDeprecationWarningr   matrixr   r	   ru   r   )ro  r   lin_conss      r&   test_bug_11886rt    s{     
	 	 	" 'h(ABIIbggq!fo&'  2rvv.HS!QC%x0' 's   ABBc                     t        ddgddgd      fdfd} fd}d }fd	}t        j                  d
      }t        |dt        j                        t        |dd|      g}t        | |d|      } |j                         |d   j                  |d   j                  |j                        cxk  r|d   j                  k  sJ  J y )Nr~   r-   T)lbubrV  c                     t        j                  | j                  k\        sJ t        j                  | j                  k        sJ y r\   )r   allrv  rw  )r0   bndss    r&   assert_inboundsz%test_gh11649.<locals>.assert_inbounds  s7    vva477l###vva477l###r)   c                      |        t        j                  | d         d| d   dz  z  d| d   dz  z  z   d| d   z  | d   z  z   d| d   z  z   dz   z  S )Nr   r4   r,   r-   )r   expr0   r{  s    r&   rU  ztest_gh11649.<locals>.obj  sf    vvad|QqtQwY1Q472QqtVAaD[@1QqT6IAMNNr)   c                 0     |        | d   dz  | d   z   S r?   r.   r~  s    r&   nceztest_gh11649.<locals>.nce  s!    tQw1~r)   c                 <    t        j                  d| d   z  dg      S r+   r5   r@   s    r&   nce_jacztest_gh11649.<locals>.nce_jac  s    xx1Q4$$r)   c                 *     |        | d   | d   z  S )Nr   r-   r.   r~  s    r&   nciztest_gh11649.<locals>.nci  s    tAaDyr)   )gGz?gGz)rC   r
  )r1   r   r  r!   r  r   )
r
   r   r   r   ru   r   r0   rv  r1   rw  )	rU  r  r  r  r   nlcsr+  r{  rz  s	          @@r&   test_gh11649r    s     b"X1a&=D$O% 
-	 BS"&&1Qw79D sr.D2CCEE7::QCEE*7T!WZZ77777r)   c            	         d} t        j                  t        |       5  t        j                  d      }t        j
                  d      j                  d      t        j                  d      c}t        fd||      }t        t        |d	|g
       d d d        t        j                         5  t        j                  dt               t        t        d	gddi       d d d        y # 1 sw Y   XxY w# 1 sw Y   y xY w)Nz:...more equality constraints than independent variables...rA  )r,      )r   r,   )r   c                     | z  S r\   r.   )r0   r   s    r&   r   z3test_gh20665_too_many_constraints.<locals>.<lambda>  s    4!8 r)   )rv  rw  r
  r  r  r   factorization_methodSVDFactorization)r  r  options)r  rC  r]   r   r   r   reshaper   r   r   r  r  rp  r  )r#  r   r   gr   s       @r&   !test_gh20665_too_many_constraintsr    s     KG	z	1 DWWT]YYq\))&12774=
d 3F>sC	D 
	 	 	" Gh4>s02DE	GG GD DG Gs   A5C+02C7+C47D c                  @   d } d }t        j                         5  t        j                  ddt               t        j                  ddt               t	        |ddgdt        | dd      	      }d d d        j                  s|j                  d
kD  sJ y # 1 sw Y   'xY w)Nc                 N    | \  }}ddg\  }}d|dz  |dz  z  z   |dz  |dz  z  z
  S )N      @      @r   r,   r.   )uu1u2rY   rZ   s        r&   lsfztest_issue_18882.<locals>.lsf  s@    BSz1RUQT\!BEAqDL00r)   c                 2    t        j                  | dz        S rz   )r   r   )r  s    r&   ofztest_issue_18882.<locals>.of  s    vvad|r)   r   r	  zSingular Jacobian matrix.r   r
  r   r  r  )r  r  r  r  r   r   rN  constr_violation)r  r  r+  s      r&   test_issue_18882r    s    1
 
	 	 	" 
*={K*E{S#J!+CA6	

 #"6"6"=>>"=
 
s   ABBc                   |   e Zd Zej                  j                  d eej                   ej                         e	       j                  f eej                   d      ddgf edej                        ddgf eddgddg      ddgfg      d        Zd	 Zd
 Zd Zej                  j                  d      d        Zy)TestBoundedNelderMeadzbounds, x_optgr  g      "@r   r        @c                    t               }t        j                         5  d}t        j                  d|t               t        |j                  ddgd|      }t        j                  |j                  |j                        j                         sJ t        j                  |j                  |j                        j                         sJ t        j                  |j                  |j                        |j                        sJ t        j                  |j                  |d      sJ 	 d d d        y # 1 sw Y   y xY w)N0Initial guess is not within the specified boundsr   r  Nelder-Meadr  r!   gMbP?)atol)r   r  r  r  r  r   r1   r   
less_equalrv  r0   ry  rw  allclose)r#   r!   r   r   msgr"  s         r&   test_rosen_brock_with_boundsz2TestBoundedNelderMead.test_rosen_brock_with_bounds  s     |$$& 		<DC##Hc;?dhhc
%2%+-F ==FHH599;;;==699599;;;;;txx16::>>>;;vxxU;;;		< 		< 		<s   DD55D>c                 @   t               }t        ddgddg      }t        j                         5  t        j                  ddt
               t        |j                  ddgd|      }t        j                  |j                  ddg      sJ 	 d d d        y # 1 sw Y   y xY w)	Nr  r  r   r  r     r  r  r   r
   r  r  r  r  r   r1   r   r  r0   r#   r   r!   r"  s       r&   test_equal_all_boundsz+TestBoundedNelderMead.test_equal_all_bounds&  s    |c
S#J/$$& 	5##B dhha%2%+-F ;;vxx#s444	5 	5 	5   ABBc                 @   t               }t        ddgddg      }t        j                         5  t        j                  ddt
               t        |j                  ddgd|	      }t        j                  |j                  dd
g      sJ 	 d d d        y # 1 sw Y   y xY w)Nr  r  g      4@r   r  r  r  r  r  g      0@r  r  s       r&   test_equal_one_boundsz+TestBoundedNelderMead.test_equal_one_bounds3  s    |c
S$K0$$& 	6##B dhha%2%+-F ;;vxx#t555	6 	6 	6r  c                     t               }d}t        j                  t        |      5  t	        t
        j                   dgddg      }t        |j                  ddgd|	       d d d        y # 1 sw Y   y xY w)
Nz:An upper bound is less than the corresponding lower bound.rA  r   r  g      r  r   r  r  )	r   r  rC  r]   r
   r   ru   r   r1   r#   r   r#  r!   s       r&   test_invalid_boundsz)TestBoundedNelderMead.test_invalid_bounds@  se    |N]]:W5 	$bffWcNS$K8FTXXQx)"$	$ 	$ 	$   ;A,,A5z5Failing on Azure Linux and macOS builds, see gh-13846)reasonc                     t               }d}t        j                  t        |      5  t	        t
        j                   dgddg      }t        |j                  ddgd|	       d d d        y # 1 sw Y   y xY w)
Nr  rA  r   r  r  r  r  r  r  )	r   r  warnsr  r
   r   ru   r   r1   r  s       r&   test_outside_bounds_warningz1TestBoundedNelderMead.test_outside_bounds_warningI  sg     |D\\+W5 	$bffWcNS#J7FTXXQx)"$	$ 	$ 	$r  N)rQ   rR   rS   r  rY  rZ  r
   r   ru   r   r   r  r  r  r  r  r  r.   r)   r&   r  r    s    [[_%rvvgrvv6
8J8JK%rvvgt4tTlC%c2662S#J?%sCj3*=BxH !<!<56$ [[ - .$.$r)   r  )$r  numpyr   r  scipy.linalgr   scipy.sparser   numpy.testingr   r   r   scipy.optimizer   r	   r
   r   r   r   r   r   rW   rh   rs   r   r   r   r   r   r   r\  rt  r  r  r  r  r.   r)   r&   <module>r     s       # "7 7# # #*9 *9Z39 39l,9 ,9^+A +A\+ +\/Z /,.
 ."4z 44|? |?~H! H!T2N 2Nj	1 8FG?(A$ A$r)   