a
    Relg                     @   s>  d dl Zd dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
mZmZmZ d dl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eZG dd deZdd Z ej!j"ddd d!d" Z#G d#d$ d$Z$dS )%    N)
block_diag)
csc_matrix)TestCaseassert_array_almost_equalassert_array_lessassert_assert_allclosesuppress_warnings)NonlinearConstraintLinearConstraintBoundsminimizeBFGSSR1c                   @   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   sJ   |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/sunrise/env/lib/python3.9/site-packages/scipy/optimize/tests/test_minimize_constrained.py__init__   s    zMaratos.__init__c                 C   s(   d|d d |d d  d  |d  S N   r      r&   r#   xr&   r&   r'   fun    s    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   s   dt d S Nr0   r*   r   eyer,   r&   r&   r'   hess&   s    zMaratos.hessc                 C   sL   dd }| j d u rdd }n| j }| jd u r6dd }n| j}t|dd||S )Nc                 S   s   | d d | d d  S Nr   r*   r+   r&   r-   r&   r&   r'   r.   +   s    zMaratos.constr.<locals>.func                 S   s   d| d  d| d  ggS r)   r&   r8   r&   r&   r'   jac/   s    zMaratos.constr.<locals>.jacc                 S   s   d|d  t d S Nr*   r   r4   r-   vr&   r&   r'   r6   5   s    zMaratos.constr.<locals>.hessr+   r   r    r
   r#   r.   r9   r6   r&   r&   r'   constr)   s    



zMaratos.constr)r   NN
__name__
__module____qualname____doc__r(   r.   r2   r6   propertyr?   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#   rG   rH   r$   r   r    r%   r&   r&   r'   r(   E   s    zMaratosTestArgs.__init__c                 C   s   | j |ks| j|krt d S N)rG   rH   
ValueError)r#   rG   rH   r&   r&   r'   
_test_argsO   s    zMaratosTestArgs._test_argsc                 C   s4   |  || d|d d |d d  d  |d  S r)   )rK   r#   r-   rG   rH   r&   r&   r'   r.   S   s    zMaratosTestArgs.func                 C   s.   |  || td|d  d d|d  gS r/   )rK   r   r   rL   r&   r&   r'   r2   W   s    zMaratosTestArgs.gradc                 C   s   |  || dtd S r3   )rK   r   r5   rL   r&   r&   r'   r6   [   s    zMaratosTestArgs.hessc                 C   sL   dd }| j d u rdd }n| j }| jd u r6dd }n| j}t|dd||S )Nc                 S   s   | d d | d d  S r7   r&   r8   r&   r&   r'   r.   a   s    z#MaratosTestArgs.constr.<locals>.func                 S   s   d| d  d| d  ggS r/   r&   r8   r&   r&   r'   r9   e   s    z#MaratosTestArgs.constr.<locals>.jacc                 S   s   d|d  t d S r:   r4   r;   r&   r&   r'   r6   k   s    z$MaratosTestArgs.constr.<locals>.hessr+   r=   r>   r&   r&   r'   r?   _   s    



zMaratosTestArgs.constr)r   NN)rA   rB   rC   rD   r(   rK   r.   r2   r6   rE   r?   r&   r&   r&   r'   rF   =   s   

rF   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   sJ   |d t j }t |t |g| _t ddg| _|| _|| _d | _	d S r   r   r"   r&   r&   r'   r(   {   s    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+   r0   r1   r,   r&   r&   r'   r.      s    & zMaratosGradInFunc.func                 C   s   dS )NTr&   r#   r&   r&   r'   r2      s    zMaratosGradInFunc.gradc                 C   s   dt d S r3   r4   r,   r&   r&   r'   r6      s    zMaratosGradInFunc.hessc                 C   sL   dd }| j d u rdd }n| j }| jd u r6dd }n| j}t|dd||S )Nc                 S   s   | d d | d d  S r7   r&   r8   r&   r&   r'   r.      s    z%MaratosGradInFunc.constr.<locals>.func                 S   s   d| d  d| d  ggS r/   r&   r8   r&   r&   r'   r9      s    z%MaratosGradInFunc.constr.<locals>.jacc                 S   s   d|d  t d S r:   r4   r;   r&   r&   r'   r6      s    z&MaratosGradInFunc.constr.<locals>.hessr+   r=   r>   r&   r&   r'   r?      s    



zMaratosGradInFunc.constr)r   NN)
rA   rB   rC   rD   r(   r.   rE   r2   r6   r?   r&   r&   r&   r'   rM   s   s   

rM   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.      s    zHyperbolicIneq.func                 C   s   |d d |d d gS )Nr   r*   r+   rQ   r&   r,   r&   r&   r'   r2      s    zHyperbolicIneq.gradc                 C   s
   t dS Nr*   r4   r,   r&   r&   r'   r6      s    zHyperbolicIneq.hessc                 C   sN   dd }| j d u rdd }n| j }| jd u r6dd }n| j}t|dtj||S )Nc                 S   s   d| d d  | d  S )Nr+   r   r&   r8   r&   r&   r'   r.      s    z"HyperbolicIneq.constr.<locals>.func                 S   s   d| d d d  dggS )Nr   r+   r*   r&   r8   r&   r&   r'   r9      s    z"HyperbolicIneq.constr.<locals>.jacc                 S   s2   d|d  t d| d d d  dgddgg S )Nr*   r   r+      r1   r;   r&   r&   r'   r6      s    $z#HyperbolicIneq.constr.<locals>.hessg      ?r   r    r
   r   rP   r>   r&   r&   r'   r?      s    



zHyperbolicIneq.constr)NNr@   r&   r&   r&   r'   rO      s   
rO   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 )NrS   r+   )r   randomRandomStateuniformr   Z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+   rS          @r   Z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+   rS   r*        pr   )r   r_   Z
zeros_like)r#   r-   ZxmZxm_m1Zxm_p1Zderr&   r&   r'   r2      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 )Nre   rS   r+   rd   )dtypei  r   r*   rc      )r   Z
atleast_1ddiagzeroslenrf   )r#   r-   HZdiagonalr&   r&   r'   r6      s    
0$0zRosenbrock.hessc                 C   s   dS )Nr&   r&   rN   r&   r&   r'   r?      s    zRosenbrock.constrN)r*   r   r@   r&   r&   r&   r'   rV      s   

rV   c                   @   s&   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   s,   t | d| ddg| _ddg| _d | _d S )Nr*   rS         gn?g$?rV   r(   r   r   r!   r#   r[   r&   r&   r'   r(     s    

zIneqRosenbrock.__init__c                 C   s   ddgg}d}t |tj |S Nr+   r*   r   r   rP   )r#   ArH   r&   r&   r'   r?     s    
zIneqRosenbrock.constrN)r   rA   rB   rC   rD   r(   rE   r?   r&   r&   r&   r'   rl      s   
rl   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?rb   r   )rV   r(   r   r   r   r!   ro   r&   r&   r'   r(     s    
zBoundedRosenbrock.__init__N)r   )rA   rB   rC   rD   r(   r&   r&   r&   r'   rt     s   	rt   c                   @   s&   e Zd ZdZdddZedd ZdS )	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   s,   t | d| ddg| _ddg| _d | _d S )Nr*   rS   rm   gWs`?g|\*?rn   ro   r&   r&   r'   r(   /  s    

zEqIneqRosenbrock.__init__c                 C   s8   ddgg}d}ddgg}d}t |tj |t |||fS rp   rq   )r#   ZA_ineqZb_ineqZA_eqZb_eqr&   r&   r'   r?   5  s    


zEqIneqRosenbrock.constrN)r   rs   r&   r&   r&   r'   ru   %  s   	
ru   c                   @   sN   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.
    rc   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   rW   rX   r\   rY   r   r   r   hstackr   r   r   r    r!   )
r#   rw   r[   r   r    phithetar-   yzr&   r&   r'   r(   M  s    
zElec.__init__c                 C   s>   |d | j  }|| j d| j   }|d| j  d  }|||fS rR   rw   )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 rI   r   )r#   r-   r~   r   r   dxdydzr&   r&   r'   _compute_coordinate_deltasc  s
    zElec._compute_coordinate_deltasc                 C   st   |  |\}}}tjdd, |d |d  |d  d }W d    n1 sN0    Y  d|t|< dt| S )Nignoredivider*   rm   r   rQ   )r   r   errstatediag_indices_fromr`   )r#   r-   r   r   r   Zdm1r&   r&   r'   r.   j  s
    :zElec.func           	      C   s   |  |\}}}tjdd, |d |d  |d  d }W d    n1 sN0    Y  d|t|< tj|| dd }tj|| dd }tj|| dd }t|||fS )Nr   r   r*         r   r+   r^   )r   r   r   r   r`   rx   )	r#   r-   r   r   r   dm3Zgrad_xZgrad_yZgrad_zr&   r&   r'   r2   q  s    :z	Elec.gradc              	   C   s  |  |\}}}|d |d  |d  d }tjdd  |d }|d }W d    n1 s^0    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*   rQ   r   r   r   rT   r+   r^   )r   r   r   Zarangerw   r`   Zvstackrx   )r#   r-   r   r   r   dr   Zdm5iZHxxZHxyZHxzZHyyZHyzZHzzrk   r&   r&   r'   r6   ~  s4    &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   rN   r&   r'   r.     s    zElec.constr.<locals>.func                    sN     | \}}}dt| }dt| }dt| }tt|||fS rR   )r   r   rh   r   rx   )r-   r~   r   r   ZJxZJyZJzrN   r&   r'   r9     s
    zElec.constr.<locals>.jacc                 S   s   dt | }t|||S rR   )r   rh   r   )r-   r<   Dr&   r&   r'   r6     s    zElec.constr.<locals>.hessr   rU   r>   r&   rN   r'   r?     s    


zElec.constr)rc   r   NN)rA   rB   rC   rD   r(   r   r   r.   r2   r6   rE   r?   r&   r&   r&   r'   rv   ?  s     
&rv   c                   @   s\   e Zd Zejj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 )TestTrustRegionConstrc                 C   s  t  t ddt t dt dt dt t tddtt dtdt dt t t t t	ddt	dddt	dt dt	ddt dg}|D ],}|j
dd	fD ]}|jdt td
dtddfD ]}|dv r|dv rq|j
du r|dv rqt :}|td t|j|jd|||j|jd}W d    n1 s40    Y  |jd urtt|j|jdd |jdkrtt|jd |jdkrt|jd |jdkrt|jd |jdv rtdqqqd S )N2-point)r    )r   r    3-pointr*   r}   )rw   r    )rw   r   r    FZdamp_update)Zexception_strategyZskip_update)r   r   csF)r   r   r   T)r   Fdelta_grad == 0.0trust-constrmethodr9   r6   r!   constraints   decimalr+   :0yE>tr_interior_pointr   rT   Invalid termination condition.)r   r   rM   rO   r   rV   rl   ru   rt   rv   r2   r6   r	   filterUserWarningr   r.   r   r!   r?   r   r   r-   statusr   
optimality	tr_radiusr   barrier_parameterRuntimeError)r#   Zlist_of_problemsprobr2   r6   supresultr&   r&   r'   test_list_of_problems  sp    




&

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   s   | d d S rp   r&   r8   r&   r&   r'   r.     s    z<TestTrustRegionConstr.test_default_jac_and_hess.<locals>.funrb   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   s   | d d S rp   r&   r8   r&   r&   r'   r.     s    z4TestTrustRegionConstr.test_default_hess.<locals>.funr   r   r   r   )r   r!   r   r9   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   r9   r6   zL-BFGS-Br   )r   r9   r   r   r   )	rV   r   r.   r   r2   r6   r   r-   r   )r#   r   r   Zresult1Z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rNt|j	 jdd |j
dkrdt|jd |j
dkrt|jd |jd	krt|jd |j
d
v rtdd S )Nc                    s     | }||S rI   )r6   dot)r-   prk   r   r&   r'   hessp  s    
z/TestTrustRegionConstr.test_hessp.<locals>.hesspr   )r   r9   r   r!   r   r*   r   r+   r   r   r   r   )r   r   r.   r   r2   r!   r?   r   r   r-   r   r   r   r   r   r   r   )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rJt	|j
|jdd |jdkr`t|jd	 |jdkrt|jd	 |jd
krt|jd	 |jdv rtdd S )NrG      )rG   r   r   r   r*   r   r+   r   r   r   r   )rF   r   r.   r   r2   r6   r!   r?   r   r   r-   r   r   r   r   r   r   r   )r#   r   r   r&   r&   r'   	test_args8  s"    





zTestTrustRegionConstr.test_argsc              	   C   sV   t  }d}tjt|d* t|j|jddd|jd W d    n1 sH0    Y  d S )Nz9Whenever the gradient is estimated via finite-differencesmatchr   r   )r   r9   r6   r   )r   pytestraisesrJ   r   r.   r   r?   )r#   r   messager&   r&   r'   test_raise_exceptionP  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'   callback\  s    z7TestTrustRegionConstr.test_issue_9044.<locals>.callbackc                 S   s   | d S rR   r&   r8   r&   r&   r'   <lambda>`      z7TestTrustRegionConstr.test_issue_9044.<locals>.<lambda>r   c                 S   s   d|  S rR   r&   r8   r&   r&   r'   r   `  r   c                 S   s   dS rR   r&   r8   r&   r&   r'   r   a  r   r   )r9   r6   r   r   successr   rS   r+   r   )r   r   get)r#   r   r   r&   r&   r'   test_issue_9044W  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 sp0    Y  |d sJ d S )Nr   rQ   c                 S   s    | d }| d }|d |d  S )Nr   r+   r*   r&   )r-   x1Zx2r&   r&   r'   objr  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_15093j  s    $z&TestTrustRegionConstr.test_issue_15093N)rA   rB   rC   r   markZslowr   r   r   r   r   r   r   r   r   r&   r&   r&   r'   r     s   
;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   s   | d d | d d  S r7   r&   r8   r&   r&   r'   function  s    z;TestEmptyConstraint.test_empty_constraint.<locals>.functionc                 S   s   t d| d  d| d  gS )Nr]   r   r+   r1   r8   r&   r&   r'   functionjacobian  s    zCTestEmptyConstraint.test_empty_constraint.<locals>.functionjacobianc                 S   s   d| S )Nr]   r&   r;   r&   r&   r'   functionhvp  s    z>TestEmptyConstraint.test_empty_constraint.<locals>.functionhvpc                 S   s    t | d d | d d  gS r7   r1   r8   r&   r&   r'   
constraint  s    z=TestEmptyConstraint.test_empty_constraint.<locals>.constraintc                 S   s    t d| d  d| d  ggS )Nr*   r   rb   r+   r1   r8   r&   r&   r'   constraintjacobian  s    zETestEmptyConstraint.test_empty_constraint.<locals>.constraintjacobianc                 S   s   t ddgddgg|d  S )Nr]   r   g       r   r1   r;   r&   r&   r'   constraintlcoh  s    zATestEmptyConstraint.test_empty_constraint.<locals>.constraintlcohr   r]   r   )r   r9   r   r   r!   r+   r   r0   r   )	r
   r   rP   r   r   r   absr-   r   )
r#   r   r   r   r   r   r   Z
startpointr!   r   r&   r&   r'   test_empty_constraint  s&    
z)TestEmptyConstraint.test_empty_constraintN)rA   rB   rC   rD   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 sF0    Y  t|dt j}t	| ddg |d d S )Nc                 S   s   | d d | d d  S r7   r&   r8   r&   r&   r'   opt  s    ztest_bug_11886.<locals>.optr+   rS   r*   )r   )
r   Ztestingr	   r   PendingDeprecationWarningZmatrixrh   r   rP   r   )r   r   rr   Zlin_consr&   r&   r'   test_bug_11886  s    
2r   z(Known bug in trust-constr; see gh-11649.T)reasonstrictc                     s   t ddgddgddfdd  fdd}  fd	d
} fdd}td}t|dtjt|ddg}t| |d|d}|jsJ  |j |d j|d 	|j  k r|d j
k sn J t||j|d j
 t| |d|d}t|j	|j	 d S )NrS   r+   T)lbubr   c                    s,   t |  jksJ t |  jks(J d S rI   )r   allr   r   r8   )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   r0   r*   r+   )r   expr8   r   r&   r'   r     s    ztest_gh11649.<locals>.objc                    s    |  | d d | d  S r7   r&   r8   r   r&   r'   nce  s    ztest_gh11649.<locals>.ncec                    s    |  | d | d  S )Nr   r+   r&   r8   r   r&   r'   nci  s    ztest_gh11649.<locals>.nci)gGz?gGzr   )r.   r   r   r!   r   r   Zslsqp)r   r   r   r
   rP   r   r   r-   r   r.   r   r   )r   r   r   r   Znlcsr   refr&   )r   r   r'   test_gh11649  s(    



2r   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_optgg      @g      "@r         @      @c                 C   s   t  }t }|td t|jddgd|d}t|j|j	
 sHJ t|j	|j
 s`J t||j	|jszJ tj|j	|ddsJ W d    n1 s0    Y  d S )N0Initial guess is not within the specified boundsr   Nelder-Meadr   r!   gMbP?)Zatol)rV   r	   r   r   r   r.   r   Z
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   sz   t  }tddgddg}t H}|td t|jddgd|d}t|j	ddgsXJ W d    n1 sl0    Y  d S )Nr   r   r   r      r   r   
rV   r   r	   r   r   r   r.   r   r   r-   r#   r   r!   r   r   r&   r&   r'   test_equal_all_bounds  s    z+TestBoundedNelderMead.test_equal_all_boundsc                 C   sz   t  }tddgddg}t H}|td t|jddgd|d}t|j	dd	gsXJ W d    n1 sl0    Y  d S )
Nr   r   g      4@r   r   r   r   r   g      0@r   r   r&   r&   r'   test_equal_one_bounds	  s    z+TestBoundedNelderMead.test_equal_one_boundsc                 C   sh   t  }d}tjt|d< ttj dgddg}t|jddgd|d	 W d    n1 sZ0    Y  d S )
Nz:An upper bound is less than the corresponding lower bound.r   r   r   g      r   rT   r   r   )	rV   r   r   rJ   r   r   rP   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)r   c                 C   sh   t  }d}tjt|d< ttj dgddg}t|jddgd|d	 W d    n1 sZ0    Y  d S )
Nr   r   r   r   r   r   r   r   r   )	rV   r   Zwarnsr   r   r   rP   r   r.   r   r&   r&   r'   test_outside_bounds_warning  s    z1TestBoundedNelderMead.test_outside_bounds_warningN)rA   rB   rC   r   r   Zparametrizer   r   rP   rV   r   r   r   r   r   xfailr   r&   r&   r&   r'   r     s   
	r   )%numpyr   r   Zscipy.linalgr   Zscipy.sparser   Znumpy.testingr   r   r   r   r   r	   Zscipy.optimizer
   r   r   r   r   r   r   rF   rM   rO   rV   rl   rt   ru   rv   r   r   r   r   r   r   r   r&   r&   r&   r'   <module>   s.     -6/.. G4
#