o
    h&m                     @   s6  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
mZ d dlmZmZmZmZmZmZmZ G dd dZG dd	 d	ZG d
d dZG dd dZG dd dZG dd deZG dd deZG dd deZG dd dZG dd dZG dd dZdd Zdd Z d d! Z!d"d# Z"G d$d% d%Z#dS )&    N)
block_diag)
csc_matrix)assert_array_almost_equalassert_array_lessassert_suppress_warnings)NonlinearConstraintLinearConstraintBoundsminimizeBFGSSR1rosenc                   @   s>   e Zd ZdZdddZdd Zdd	 Zd
d Zedd Z	dS )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                 C   J   |d t j }t |t |g| _t ddg| _|| _|| _d | _	d S N         ?        
nppicossinx0arrayx_opt
constr_jacconstr_hessboundsselfdegreesr   r    rads r&   b/var/www/vscode/kcb/lib/python3.10/site-packages/scipy/optimize/tests/test_minimize_constrained.py__init__      
zMaratos.__init__c                 C   s(   d|d d |d d  d  |d  S N   r      r&   r#   xr&   r&   r'   fun!      (zMaratos.func                 C   s"   t d|d  d d|d  gS N   r   r,   r   r   r-   r&   r&   r'   grad$   s   "zMaratos.gradc                 C      dt d S Nr2   r+   r   eyer-   r&   r&   r'   hess'      zMaratos.hessc                 C   L   dd }| j d u rdd }n| j }| jd u rdd }n| j}t|dd||S )Nc                 S      | d d | d d  S Nr   r+   r,   r&   r.   r&   r&   r'   r/   ,      zMaratos.constr.<locals>.func                 S      d| d  d| d  ggS r*   r&   r>   r&   r&   r'   jac0      zMaratos.constr.<locals>.jacc                 S      d|d  t d S Nr+   r   r7   r.   vr&   r&   r'   r9   6      zMaratos.constr.<locals>.hessr,   r   r    r   r#   r/   rA   r9   r&   r&   r'   constr*      



zMaratos.constrr   NN
__name__
__module____qualname____doc__r(   r/   r4   r9   propertyrJ   r&   r&   r&   r'   r      s    
r   c                   @   sF   e Zd ZdZdddZdd Zdd	 Zd
d Zdd Ze	dd Z
dS )MaratosTestArgsr   r   Nc                 C   sV   |d t j }t |t |g| _t ddg| _|| _|| _|| _	|| _
d | _d S r   )r   r   r   r   r   r   r   r   r    abr!   )r#   rT   rU   r$   r   r    r%   r&   r&   r'   r(   F   s   
zMaratosTestArgs.__init__c                 C   s   | j |ks
| j|krt d S N)rT   rU   
ValueError)r#   rT   rU   r&   r&   r'   
_test_argsP   s   zMaratosTestArgs._test_argsc                 C   s4   |  || d|d d |d d  d  |d  S r*   )rX   r#   r.   rT   rU   r&   r&   r'   r/   T   s   (zMaratosTestArgs.func                 C   s.   |  || td|d  d d|d  gS r1   )rX   r   r   rY   r&   r&   r'   r4   X   s   "zMaratosTestArgs.gradc                 C   s   |  || dtd S r6   )rX   r   r8   rY   r&   r&   r'   r9   \   s   zMaratosTestArgs.hessc                 C   r;   )Nc                 S   r<   r=   r&   r>   r&   r&   r'   r/   b   r?   z#MaratosTestArgs.constr.<locals>.func                 S   r@   r1   r&   r>   r&   r&   r'   rA   f   rB   z#MaratosTestArgs.constr.<locals>.jacc                 S   rC   rD   r7   rE   r&   r&   r'   r9   l   rG   z$MaratosTestArgs.constr.<locals>.hessr,   rH   rI   r&   r&   r'   rJ   `   rK   zMaratosTestArgs.constrrL   )rN   rO   rP   rQ   r(   rX   r/   r4   r9   rR   rJ   r&   r&   r&   r'   rS   >   s    

rS   c                   @   sB   e Zd ZdZdddZdd Zedd	 Zd
d Zedd Z	dS )MaratosGradInFuncr   r   Nc                 C   r   r   r   r"   r&   r&   r'   r(   |   r)   zMaratosGradInFunc.__init__c                 C   sJ   d|d d |d d  d  |d  t d|d  d d|d  gfS )Nr+   r   r,   r2   r3   r-   r&   r&   r'   r/      s   & zMaratosGradInFunc.func                 C      dS )NTr&   r#   r&   r&   r'   r4         zMaratosGradInFunc.gradc                 C   r5   r6   r7   r-   r&   r&   r'   r9      r:   zMaratosGradInFunc.hessc                 C   r;   )Nc                 S   r<   r=   r&   r>   r&   r&   r'   r/      r?   z%MaratosGradInFunc.constr.<locals>.func                 S   r@   r1   r&   r>   r&   r&   r'   rA      rB   z%MaratosGradInFunc.constr.<locals>.jacc                 S   rC   rD   r7   rE   r&   r&   r'   r9      rG   z&MaratosGradInFunc.constr.<locals>.hessr,   rH   rI   r&   r&   r'   rJ      rK   zMaratosGradInFunc.constrrL   )
rN   rO   rP   rQ   r(   r/   rR   r4   r9   rJ   r&   r&   r&   r'   rZ   t   s    

rZ   c                   @   s>   e Zd ZdZdddZdd Zdd Zd	d
 Zedd Z	dS )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                 C   s2   ddg| _ ddg| _|| _|| _tdtj| _d S )Nr   g~T>?g~1[?)r   r   r   r    r
   r   infr!   )r#   r   r    r&   r&   r'   r(      s
   

zHyperbolicIneq.__init__c                 C   s(   d|d d d  d|d d d   S )N      ?r   r+   r,   r&   r-   r&   r&   r'   r/      r0   zHyperbolicIneq.func                 C   s   |d d |d d gS )Nr   r+   r,   r`   r&   r-   r&   r&   r'   r4      r?   zHyperbolicIneq.gradc                 C   s
   t dS Nr+   r7   r-   r&   r&   r'   r9      s   
zHyperbolicIneq.hessc                 C   sN   dd }| j d u rdd }n| j }| jd u rdd }n| j}t|dtj||S )Nc                 S   s   d| d d  | d  S )Nr,   r   r&   r>   r&   r&   r'   r/      r?   z"HyperbolicIneq.constr.<locals>.func                 S   s   d| d d d  dggS )Nr   r,   r+   r&   r>   r&   r&   r'   rA      rB   z"HyperbolicIneq.constr.<locals>.jacc                 S   s2   d|d  t d| d d d  dgddgg S )Nr+   r   r,      r3   rE   r&   r&   r'   r9      s   $z#HyperbolicIneq.constr.<locals>.hessg      ?r   r    r   r   r_   rI   r&   r&   r'   rJ      s   



zHyperbolicIneq.constr)NNrM   r&   r&   r&   r'   r^      s    
r^   c                   @   s>   e Zd ZdZdddZdd Zdd	 Zd
d Zedd Z	dS )
RosenbrockzRosenbrock function.

    The following optimization problem:
        minimize sum(100.0*(x[1:] - x[:-1]**2.0)**2.0 + (1 - x[:-1])**2.0)
    r+   r   c                 C   s2   t j|}|dd|| _t || _d | _d S )Nrb   r,   )r   randomRandomStateuniformr   onesr   r!   )r#   nrandom_staterngr&   r&   r'   r(      s   
zRosenbrock.__init__c                 C   sP   t |}t jd|dd  |d d d  d  d|d d  d  dd}|S )Ng      Y@r,   rb          @r   axis)r   asarraysum)r#   r.   rr&   r&   r'   r/      s
   
:zRosenbrock.func                 C   s   t |}|dd }|d d }|dd  }t |}d||d   d||d   |  dd|   |dd< d|d  |d |d d   dd|d    |d< d|d |d d   |d< |S )	Nr,   rb   r+        pr   )r   rp   
zeros_like)r#   r.   xmxm_m1xm_p1derr&   r&   r'   r4      s   


4zRosenbrock.gradc                 C   s   t |}t d|d d  dt d|d d  d }t jt||jd}d|d d  d|d   d |d< d	|d< d
d|dd d   d|dd    |dd< |t | }|S )Nrv   rb   r,   ru   )dtypei  r   r+   rt      )r   
atleast_1ddiagzeroslenr|   )r#   r.   Hdiagonalr&   r&   r'   r9      s   
0$0zRosenbrock.hessc                 C   r[   )Nr&   r&   r\   r&   r&   r'   rJ      r]   zRosenbrock.constrN)r+   r   rM   r&   r&   r&   r'   re      s    

re   c                   @   &   e Zd ZdZdddZedd ZdS )	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.
    r   c                 C   ,   t | d| ddg| _ddg| _d | _d S )Nr+   rb         gn?g$?re   r(   r   r   r!   r#   rk   r&   r&   r'   r(        


zIneqRosenbrock.__init__c                 C   s   ddgg}d}t |tj |S Nr,   r+   r	   r   r_   )r#   ArU   r&   r&   r'   rJ     s   
zIneqRosenbrock.constrNr   rN   rO   rP   rQ   r(   rR   rJ   r&   r&   r&   r'   r      s
    
r   c                   @   s   e Zd ZdZdddZdS )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.
    r   c                 C   s6   t | d| ddg| _d | _tddgddg| _d S )Nr+   gɿg?rs   r   )re   r(   r   r   r
   r!   r   r&   r&   r'   r(     s   
zBoundedRosenbrock.__init__Nr   )rN   rO   rP   rQ   r(   r&   r&   r&   r'   r     s    	r   c                   @   r   )	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.
    r   c                 C   r   )Nr+   rb   r   gWs`?g|\*?r   r   r&   r&   r'   r(   0  r   zEqIneqRosenbrock.__init__c                 C   s8   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_eqr&   r&   r'   rJ   6  s   


zEqIneqRosenbrock.constrNr   r   r&   r&   r&   r'   r   &  s
    
	r   c                   @   sR   e Zd ZdZ		dddZdd Zd	d
 Zdd Zdd Zdd Z	e
dd ZdS )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.
    rt   r   Nc           
      C   s   || _ tj|| _| jddtj | j }| jtj tj| j }t|t| }t|t| }t|}	t	|||	f| _
d | _|| _|| _d | _d S )Nr   r+   )n_electronsr   rf   rg   rl   rh   r   r   r   hstackr   r   r   r    r!   )
r#   r   rk   r   r    phithetar.   yzr&   r&   r'   r(   N  s   

zElec.__init__c                 C   s>   |d | j  }|| j d| j   }|d| j  d  }|||fS ra   r   )r#   r.   x_coordy_coordz_coordr&   r&   r'   _get_cordinates^  s   
zElec._get_cordinatesc                 C   sV   |  |\}}}|d d d f | }|d d d f | }|d d d f | }|||fS rV   r   )r#   r.   r   r   r   dxdydzr&   r&   r'   _compute_coordinate_deltasd  s
   
zElec._compute_coordinate_deltasc                 C   st   |  |\}}}tjdd |d |d  |d  d }W d    n1 s'w   Y  d|t|< dt| S )Nignoredivider+   r   r   r`   )r   r   errstatediag_indices_fromrq   )r#   r.   r   r   r   dm1r&   r&   r'   r/   k  s   zElec.func           	      C   s   |  |\}}}tjdd |d |d  |d  d }W d    n1 s'w   Y  d|t|< tj|| dd }tj|| dd }tj|| dd }t|||fS )Nr   r   r+         r   r,   rn   )r   r   r   r   rq   r   )	r#   r.   r   r   r   dm3grad_xgrad_ygrad_zr&   r&   r'   r4   r  s   z	Elec.gradc              	   C   s  |  |\}}}|d |d  |d  d }tjdd |d }|d }W d    n1 s/w   Y  t| j}d|||f< d|||f< |d|d  |  }	tj|	d	d
 |	||f< d| | | }
tj|
d	d
 |
||f< d| | | }tj|d	d
 |||f< |d|d  |  }tj|d	d
 |||f< d| | | }tj|d	d
 |||f< |d|d  |  }tj|d	d
 |||f< tt|	|
|ft|
||ft|||ff}|S )Nr+   r`   r   r   r   rc   r,   rn   )r   r   r   aranger   rq   vstackr   )r#   r.   r   r   r   dr   dm5iHxxHxyHxzHyyHyzHzzr   r&   r&   r'   r9     s6   
z	Elec.hessc                    sX    fdd} j d u r fdd}n j } jd u rdd }n j}t|tj d||S )Nc                    s,     | \}}}|d |d  |d  d S )Nr+   r,   r   )r.   r   r   r   r\   r&   r'   r/     s   zElec.constr.<locals>.func                    sN     | \}}}dt| }dt| }dt| }tt|||fS ra   )r   r   r   r   r   )r.   r   r   r   JxJyJzr\   r&   r'   rA     s
   zElec.constr.<locals>.jacc                 S   s   dt | }t|||S ra   )r   r   r   )r.   rF   Dr&   r&   r'   r9     s   zElec.constr.<locals>.hessr   rd   rI   r&   r\   r'   rJ     s   


zElec.constr)rt   r   NN)rN   rO   rP   rQ   r(   r   r   r/   r4   r9   rR   rJ   r&   r&   r&   r'   r   @  s    
&r   c                   @   s&  e Zd Ze eddee dede de e eddee dede de e	 e
 e eddedddede dedde dgZejjejd	eejd
dejddde eddeddfdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#S )$TestTrustRegionConstr2-point)r    )r   r    3-pointr+   r   )r   r    )r   r   r    probr4   )	prob.gradr   Fr9   	prob.hessdamp_update)exception_strategyskip_updatec              
   C   sV  |dkr|j n|}|dkr|jn|}|dv r|dv rtd |j du r-|dv r-td t|to:|d	ko:t|t}|rBtd
 t }|	t
d t|j|jd|||j|jd}W d    n1 sew   Y  |jd urt|j|jdd |jdkrt|jd |jdkrt|jd |jdkrt|jd d|j d}|jdvsJ |d S )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/ Acceleratedelta_grad == 0.0trust-constrmethodrA   r9   r!   constraints   decimalr,   :0yE>r+   tr_interior_pointzInvalid termination condition: .>   r   rc   )r4   r9   pytestskip
isinstancer   r   xfailr   filterUserWarningr   r/   r   r!   rJ   r   r   r.   statusr   
optimality	tr_radiusr   barrier_parameter)r#   r   r4   r9   	sensitivesupresultmessager&   r&   r'   test_list_of_problems  sB   








z+TestTrustRegionConstr.test_list_of_problemsc                 C   s4   dd }dg}t |dg|dd}t|jddd	 d S )
Nc                 S      | d d S r   r&   r>   r&   r&   r'   r/        z<TestTrustRegionConstr.test_default_jac_and_hess.<locals>.funrs   r+   r   r   )r   r!   r   r,   r   r   r   r   r.   r#   r/   r!   resr&   r&   r'   test_default_jac_and_hess  s   z/TestTrustRegionConstr.test_default_jac_and_hessc                 C   s6   dd }dg}t |dg|ddd}t|jdd	d
 d S )Nc                 S   r   r   r&   r>   r&   r&   r'   r/   	  r   z4TestTrustRegionConstr.test_default_hess.<locals>.funr   r   r   r   )r   r!   r   rA   r,   r   r   r   r   r&   r&   r'   test_default_hess  s   z'TestTrustRegionConstr.test_default_hessc                 C   s   t  }t|j|jd|j|jd}t|j|jddd}t|j|jddd}t|j|jdd t|j|jdd t|j|jdd d S )	Nr   )r   rA   r9   zL-BFGS-Br   )r   rA   r   r   r   )	re   r   r/   r   r4   r9   r   r.   r   )r#   r   r   result1result2r&   r&   r'   test_no_constraints  s    


z)TestTrustRegionConstr.test_no_constraintsc              	      s   t    fdd}t j jd j| j jd} jd ur't|j	 jdd |j
dkr2t|jd |j
dkrHt|jd |jd	krHt|jd |j
d
v rQtdd S )Nc                    s     | }||S rV   )r9   dot)r.   pr   r   r&   r'   hessp#  s   

z/TestTrustRegionConstr.test_hessp.<locals>.hesspr   )r   rA   r   r!   r   r+   r   r,   r   r   r   rc   Invalid termination condition.)r   r   r/   r   r4   r!   rJ   r   r   r.   r   r   r   r   r   r   RuntimeError)r#   r   r   r&   r   r'   
test_hessp   s&   





z TestTrustRegionConstr.test_hesspc              
   C   s   t dd}t|j|jdd|j|j|j|jd}|jd ur%t	|j
|jdd |jdkr0t|jd	 |jdkrFt|jd	 |jd
krFt|jd	 |jdv rOtdd S )NrT      )rT   r   r   r   r+   r   r,   r   r   r   r   )rS   r   r/   r   r4   r9   r!   rJ   r   r   r.   r   r   r   r   r   r   r   )r#   r   r   r&   r&   r'   	test_args=  s$   





zTestTrustRegionConstr.test_argsc              	   C   sX   t  }d}tjt|d t|j|jddd|jd W d    d S 1 s%w   Y  d S )Nz9Whenever the gradient is estimated via finite-differencesmatchr   r   )r   rA   r9   r   )r   r   raisesrW   r   r/   r   rJ   )r#   r   r   r&   r&   r'   test_raise_exceptionU  s   "z*TestTrustRegionConstr.test_raise_exceptionc                 C   sd   dd }t dd dgdd dd |dd	}t|d
 t|dddk t|dddk d S )Nc                 S   s   t d|v  t d|v  d S )Nnitniter)r   )r.   infor&   r&   r'   callbacka  s   z7TestTrustRegionConstr.test_issue_9044.<locals>.callbackc                 S   s   | d S ra   r&   r>   r&   r&   r'   <lambda>e      z7TestTrustRegionConstr.test_issue_9044.<locals>.<lambda>r   c                 S   s   d|  S ra   r&   r>   r&   r&   r'   r  e  r  c                 S   r[   ra   r&   r>   r&   r&   r'   r  f  s    r   )rA   r9   r  r   successr   rb   r,   r  )r   r   get)r#   r  r   r&   r&   r'   test_issue_9044\  s   z%TestTrustRegionConstr.test_issue_9044c                 C   s   t ddg}dd }tt ddgt ddgdd}t }|td td	|||d
}W d    n1 s8w   Y  |d sCJ d S )Nr   r`   c                 S   s    | d }| d }|d |d  S )Nr   r,   r+   r&   )r.   x1x2r&   r&   r'   objw  s   z3TestTrustRegionConstr.test_issue_15093.<locals>.objr   T)keep_feasibler   r   )r   r/   r   r!   r  )r   r   r
   r   r   r   r   )r#   r   r  r!   r   r   r&   r&   r'   test_issue_15093o  s   z&TestTrustRegionConstr.test_issue_15093N)rN   rO   rP   r   r   rZ   r^   r   re   r   r   r   r   list_of_problemsr   markthread_unsafeparametrizer   r   r   r   r   r   r   r  r  r&   r&   r&   r'   r     sN    


'r   c                   @   s   e Zd ZdZdd ZdS )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           
   	   C   s   dd }dd }dd }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|d}	tt|	jtddgdd d S )Nc                 S   r<   r=   r&   r>   r&   r&   r'   function  r?   z;TestEmptyConstraint.test_empty_constraint.<locals>.functionc                 S   s   t d| d  d| d  gS )Nrm   r   r,   r3   r>   r&   r&   r'   functionjacobian     zCTestEmptyConstraint.test_empty_constraint.<locals>.functionjacobianc                 S   s   d| S )Nrm   r&   rE   r&   r&   r'   functionhvp  s   z>TestEmptyConstraint.test_empty_constraint.<locals>.functionhvpc                 S   s    t | d d | d d  gS r=   r3   r>   r&   r&   r'   
constraint      z=TestEmptyConstraint.test_empty_constraint.<locals>.constraintc                 S   s    t d| d  d| d  ggS )Nr+   r   rs   r,   r3   r>   r&   r&   r'   constraintjacobian  r  zETestEmptyConstraint.test_empty_constraint.<locals>.constraintjacobianc                 S   s   t ddgddgg|d  S )Nrm   r   g       r   r3   rE   r&   r&   r'   constraintlcoh  r  zATestEmptyConstraint.test_empty_constraint.<locals>.constraintlcohr   rm   r   )r   rA   r   r   r!   r,   r   r2   r   )	r   r   r_   r
   r   r   absr.   r   )
r#   r  r  r  r  r  r  
startpointr!   r   r&   r&   r'   test_empty_constraint  s*   
"
z)TestEmptyConstraint.test_empty_constraintN)rN   rO   rP   rQ   r  r&   r&   r&   r'   r    s    r  c                  C   sv   dd } t j }|t t t ddg}W d    n1 s#w   Y  t|dt j}t	| ddg |d d S )Nc                 S   r<   r=   r&   r>   r&   r&   r'   opt  r?   ztest_bug_11886.<locals>.optr,   rb   r+   )r   )
r   testingr   r   PendingDeprecationWarningmatrixr   r	   r_   r   )r  r   r   lin_consr&   r&   r'   test_bug_11886  s   
r#  c                     s   t ddgddgddfdd  fdd}  fd	d
}dd } fdd}td}t|dtjt|dd|dg}t| |d|d} |j |d j|d |j  k r`|d j	k scJ  J d S )Nrb   r,   T)lbubr  c                    s,   t |  jks
J t |  jksJ d S rV   )r   allr$  r%  r>   )bndsr&   r'   assert_inbounds  s   z%test_gh11649.<locals>.assert_inboundsc                    sZ    |  t | d d| d d  d| d d   d| d  | d   d| d   d  S )Nr   r2   r+   r,   )r   expr>   r(  r&   r'   r    s   Rztest_gh11649.<locals>.objc                    s    |  | d d | d  S r=   r&   r>   r*  r&   r'   nce  s   ztest_gh11649.<locals>.ncec                 S   s   t d| d  dgS r*   r3   r>   r&   r&   r'   nce_jac  rG   ztest_gh11649.<locals>.nce_jacc                    s    |  | d | d  S )Nr   r,   r&   r>   r*  r&   r'   nci  s   ztest_gh11649.<locals>.nci)gGz?gGz)rA   r   )r/   r   r   r!   r   r   )
r
   r   r   r   r_   r   r.   r$  r/   r%  )r  r+  r,  r-  r   nlcsr   r&   )r(  r'  r'   test_gh11649  s   

8r0  c                     s   d} t jt| d/ td}tddtd }t fdd||d	}tt	|d
|gd W d    n1 s;w   Y  tj
 }|t tt	|d
|gddid W d    d S 1 sbw   Y  d S )Nz:...more equality constraints than independent variables...r   )r+      )rc   r+   )rc   c                    s    |  S rV   r&   r>   r   r&   r'   r    r  z3test_gh20665_too_many_constraints.<locals>.<lambda>)r$  r%  r   r   r   factorization_methodSVDFactorization)r   r   options)r   r   rW   r   ri   r   reshaper   r   r   r  r   r   r   )r   r   r   gr   r&   r2  r'   !test_gh20665_too_many_constraints  s   

"r9  c               	   C   s   dd } dd }t  "}|td |td t|ddgdt| d	d	d
}W d    n1 s0w   Y  |js=|jdks?J d S )Nc                 S   s8   | \}}ddg\}}d|d |d   |d |d   S )N      @      @r   r+   r&   )uu1u2rT   rU   r&   r&   r'   lsf  s   $ztest_issue_18882.<locals>.lsfc                 S   s   t | d S ra   )r   rq   )r<  r&   r&   r'   of  r:   ztest_issue_18882.<locals>.ofr   zSingular Jacobian matrix.r   r   r   r3  r   )r   r   r   r   r   r  constr_violation)r?  r@  r   r   r&   r&   r'   test_issue_18882  s   
	rB  c                
   @   s   e Zd Ze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d	 Zd
d Zdd Zdd Zejjdddd ZdS )TestBoundedNelderMeadzbounds, x_optgr:  g      "@r   r;        @c                 C   s   t  }t J}|td t|jddgd|d}t|j|j	
 s$J t|j	|j
 s0J t||j	|js=J tj|j	|ddsHJ W d    d S 1 sSw   Y  d S )N0Initial guess is not within the specified boundsr.  Nelder-Meadr   r!   gMbP?)atol)re   r   r   r   r   r/   r   
less_equalr$  r.   r&  r%  allclose)r#   r!   r   r   r   r   r&   r&   r'   test_rosen_brock_with_bounds  s   "z2TestBoundedNelderMead.test_rosen_brock_with_boundsc                 C   s|   t  }tddgddg}t %}|td t|jddgd|d}t|j	ddgs,J W d    d S 1 s7w   Y  d S )Nr;  rD  rE  r.     rF  rG  
re   r
   r   r   r   r   r/   r   rJ  r.   r#   r   r!   r   r   r&   r&   r'   test_equal_all_bounds%     "z+TestBoundedNelderMead.test_equal_all_boundsc                 C   s|   t  }tddgddg}t %}|td t|jddgd|d}t|j	dd	gs,J W d    d S 1 s7w   Y  d S )
Nr;  rD  g      4@rE  r.  rL  rF  rG  g      0@rM  rN  r&   r&   r'   test_equal_one_bounds0  rP  z+TestBoundedNelderMead.test_equal_one_boundsc                 C   j   t  }d}tjt|d ttj dgddg}t|jddgd|d	 W d    d S 1 s.w   Y  d S )
Nz:An upper bound is less than the corresponding lower bound.r   r   r;  g      r.  rc   rF  rG  )	re   r   r   rW   r
   r   r_   r   r/   r#   r   r   r!   r&   r&   r'   test_invalid_bounds;  s   "z)TestBoundedNelderMead.test_invalid_boundsz5Failing on Azure Linux and macOS builds, see gh-13846)reasonc                 C   rR  )
NrE  r   r   r;  rD  r.  rL  rF  rG  )	re   r   warnsr   r
   r   r_   r   r/   rS  r&   r&   r'   test_outside_bounds_warningD  s   "z1TestBoundedNelderMead.test_outside_bounds_warningN)rN   rO   rP   r   r  r  r
   r   r_   re   r   rK  rO  rQ  rT  r   rW  r&   r&   r&   r'   rC    s    
	rC  )$numpyr   r   scipy.linalgr   scipy.sparser   numpy.testingr   r   r   r   scipy.optimizer   r	   r
   r   r   r   r   r   rS   rZ   r^   re   r   r   r   r   r   r  r#  r0  r9  rB  rC  r&   r&   r&   r'   <module>   s.    $	-6/.. K5#