U
    /dd                     @   sJ  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 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)raises)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'   R/tmp/pip-unpacked-wheel-9gxwnfpp/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 Nr1   r+   r   eyer-   r'   r'   r(   hess(   s    zMaratos.hessc                 C   sL   dd }| j d krdd }n| j }| jd k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'   r9   r'   r'   r(   jac1   s    zMaratos.constr.<locals>.jacc                 S   s   d|d  t d S Nr+   r   r5   r.   vr'   r'   r(   r7   7   s    zMaratos.constr.<locals>.hessr,   r    r!   r   r$   r/   r:   r7   r'   r'   r(   constr+   s    



zMaratos.constr)r   NN
__name__
__module____qualname____doc__r)   r/   r3   r7   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$   rH   rI   r%   r    r!   r&   r'   r'   r(   r)   G   s    zMaratosTestArgs.__init__c                 C   s   | j |ks| j|krt d S N)rH   rI   
ValueError)r$   rH   rI   r'   r'   r(   
_test_argsQ   s    zMaratosTestArgs._test_argsc                 C   s4   |  || d|d d |d d  d  |d  S r*   )rL   r$   r.   rH   rI   r'   r'   r(   r/   U   s    zMaratosTestArgs.func                 C   s.   |  || td|d  d d|d  gS r0   )rL   r   r   rM   r'   r'   r(   r3   Y   s    zMaratosTestArgs.gradc                 C   s   |  || dtd S r4   )rL   r   r6   rM   r'   r'   r(   r7   ]   s    zMaratosTestArgs.hessc                 C   sL   dd }| j d krdd }n| j }| jd kr6dd }n| j}t|dd||S )Nc                 S   s   | d d | d d  S r8   r'   r9   r'   r'   r(   r/   c   s    z#MaratosTestArgs.constr.<locals>.func                 S   s   d| d  d| d  ggS r0   r'   r9   r'   r'   r(   r:   g   s    z#MaratosTestArgs.constr.<locals>.jacc                 S   s   d|d  t d S r;   r5   r<   r'   r'   r(   r7   m   s    z$MaratosTestArgs.constr.<locals>.hessr,   r>   r?   r'   r'   r(   r@   a   s    



zMaratosTestArgs.constr)r   NN)rB   rC   rD   rE   r)   rL   r/   r3   r7   rF   r@   r'   r'   r'   r(   rG   ?   s   

rG   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,   r1   r2   r-   r'   r'   r(   r/      s    & zMaratosGradInFunc.func                 C   s   dS )NTr'   r$   r'   r'   r(   r3      s    zMaratosGradInFunc.gradc                 C   s   dt d S r4   r5   r-   r'   r'   r(   r7      s    zMaratosGradInFunc.hessc                 C   sL   dd }| j d krdd }n| j }| jd kr6dd }n| j}t|dd||S )Nc                 S   s   | d d | d d  S r8   r'   r9   r'   r'   r(   r/      s    z%MaratosGradInFunc.constr.<locals>.func                 S   s   d| d  d| d  ggS r0   r'   r9   r'   r'   r(   r:      s    z%MaratosGradInFunc.constr.<locals>.jacc                 S   s   d|d  t d S r;   r5   r<   r'   r'   r(   r7      s    z&MaratosGradInFunc.constr.<locals>.hessr,   r>   r?   r'   r'   r(   r@      s    



zMaratosGradInFunc.constr)r   NN)
rB   rC   rD   rE   r)   r/   rF   r3   r7   r@   r'   r'   r'   r(   rN   u   s   

rN   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,   rR   r'   r-   r'   r'   r(   r3      s    zHyperbolicIneq.gradc                 C   s
   t dS Nr+   r5   r-   r'   r'   r(   r7      s    zHyperbolicIneq.hessc                 C   sN   dd }| j d krdd }n| j }| jd kr6dd }n| j}t|dtj||S )Nc                 S   s   d| d d  | d  S )Nr,   r   r'   r9   r'   r'   r(   r/      s    z"HyperbolicIneq.constr.<locals>.func                 S   s   d| d d d  dggS )Nr   r,   r+   r'   r9   r'   r'   r(   r:      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,      r2   r<   r'   r'   r(   r7      s    $z#HyperbolicIneq.constr.<locals>.hessg      ?r    r!   r   r   rQ   r?   r'   r'   r(   r@      s    



zHyperbolicIneq.constr)NNrA   r'   r'   r'   r(   rP      s   
rP   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 )NrT   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,   rT          @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,   rT   r+        pr   )r   r`   Z
zeros_like)r$   r.   ZxmZxm_m1Zxm_p1Zderr'   r'   r(   r3      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 )Nrf   rT   r,   re   )dtypei  r   r+   rd      )r   Z
atleast_1ddiagzeroslenrg   )r$   r.   HZdiagonalr'   r'   r(   r7      s    
0$0zRosenbrock.hessc                 C   s   dS )Nr'   r'   rO   r'   r'   r(   r@      s    zRosenbrock.constrN)r+   r   rA   r'   r'   r'   r(   rW      s   

rW   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+   rT         gn?g$?rW   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   rQ   )r$   ArI   r'   r'   r(   r@     s    
zIneqRosenbrock.constrN)r   rB   rC   rD   rE   r)   rF   r@   r'   r'   r'   r(   rm      s   
rm   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?rc   r   )rW   r)   r   r   r   r"   rp   r'   r'   r(   r)      s    
zBoundedRosenbrock.__init__N)r   )rB   rC   rD   rE   r)   r'   r'   r'   r(   ru     s   	ru   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+   rT   rn   gWs`?g|\*?ro   rp   r'   r'   r(   r)   1  s    

zEqIneqRosenbrock.__init__c                 C   s8   ddgg}d}ddgg}d}t |tj |t |||fS rq   rr   )r$   ZA_ineqZb_ineqZA_eqZb_eqr'   r'   r(   r@   7  s    


zEqIneqRosenbrock.constrN)r   rt   r'   r'   r'   r(   rv   '  s   	
rv   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.
    rd   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   rX   rY   r]   rZ   r   r   r   hstackr   r   r    r!   r"   )
r$   rx   r\   r    r!   phithetar.   yzr'   r'   r(   r)   O  s    
zElec.__init__c                 C   s>   |d | j  }|| j d| j   }|d| j  d  }|||fS rS   rx   )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 rJ   r   )r$   r.   r   r   r   dxdydzr'   r'   r(   _compute_coordinate_deltase  s
    zElec._compute_coordinate_deltasc              	   C   s`   |  |\}}}tjdd" |d |d  |d  d }W 5 Q R X d|t|< dt| S )Nignoredivider+   rn   r   rR   )r   r   errstatediag_indices_fromra   )r$   r.   r   r   r   Zdm1r'   r'   r(   r/   l  s
    &zElec.func           	   	   C   s   |  |\}}}tjdd" |d |d  |d  d }W 5 Q R X 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   ra   ry   )	r$   r.   r   r   r   dm3Zgrad_xZgrad_yZgrad_zr'   r'   r(   r3   s  s    &z	Elec.gradc              	   C   s  |  |\}}}|d |d  |d  d }tjdd |d }|d }W 5 Q R X 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+   rR   r   r   r   rU   r,   r_   )r   r   r   Zarangerx   ra   Zvstackry   )r$   r.   r   r   r   dr   Zdm5iZHxxZHxyZHxzZHyyZHyzZHzzrl   r'   r'   r(   r7     s4    z	Elec.hessc                    sX    fdd} j d kr$ fdd}n j } jd kr>dd }n j}t|tj d||S )Nc                    s,     | \}}}|d |d  |d  d S )Nr+   r,   r   )r.   r   r   r   rO   r'   r(   r/     s    zElec.constr.<locals>.func                    sN     | \}}}dt| }dt| }dt| }tt|||fS rS   )r   r   ri   r   ry   )r.   r   r   r   ZJxZJyZJzrO   r'   r(   r:     s
    zElec.constr.<locals>.jacc                 S   s   dt | }t|||S rS   )r   ri   r   )r.   r=   Dr'   r'   r(   r7     s    zElec.constr.<locals>.hessr   rV   r?   r'   rO   r(   r@     s    


zElec.constr)rd   r   NN)rB   rC   rD   rE   r)   r   r   r/   r3   r7   rF   r@   r'   r'   r'   r(   rw   A  s       
&rw   c                   @   sT   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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kr|dkrq|j
dkr|dkrqt 0}|td t|j|jd|||j|jd}W 5 Q R X |jd k	r^t|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krtdqqqd S )N2-point)r!   )r    r!   3-pointr+   r~   )rx   r!   )rx   r    r!   FZdamp_update)Zexception_strategyZskip_update)r   r   csF)r   r   r   T)r   Fzdelta_grad == 0.0trust-constrmethodr:   r7   r"   constraints   decimalr,   :0yE>tr_interior_pointr   rU   Invalid termination condition.)r   r   rN   rP   r   rW   rm   rv   ru   rw   r3   r7   r	   filterUserWarningr   r/   r   r"   r@   r   r   r.   statusr   
optimality	tr_radiusr   barrier_parameterRuntimeError)r$   Zlist_of_problemsprobr3   r7   supresultr'   r'   r(   test_list_of_problems  sr    




 

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 rq   r'   r9   r'   r'   r(   r/     s    z<TestTrustRegionConstr.test_default_jac_and_hess.<locals>.funrc   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 rq   r'   r9   r'   r'   r(   r/     s    z4TestTrustRegionConstr.test_default_hess.<locals>.funr   r   r   r   )r   r"   r   r:   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   r:   r7   zL-BFGS-Br   )r   r:   r   r   r   )	rW   r   r/   r   r3   r7   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 k	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
krtdd S )Nc                    s     | }||S rJ   )r7   dot)r.   prl   r   r'   r(   hessp   s    
z/TestTrustRegionConstr.test_hessp.<locals>.hesspr   )r   r:   r   r"   r   r+   r   r,   r   r   r   r   )r   r   r/   r   r3   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 k	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krtdd S )NrH      )rH   r   r   r   r+   r   r,   r   r   r   r   )rG   r   r/   r   r3   r7   r"   r@   r   r   r.   r   r   r   r   r   r   r   )r$   r   r   r'   r'   r(   	test_args:  s$    
 




zTestTrustRegionConstr.test_argsc              
   C   s(   t  }ttt|j|jddd|jd d S )Nr   r   )r   r:   r7   r   )r   r
   rK   r   r/   r   r@   )r$   r   r'   r'   r(   test_raise_exceptionR  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|k t d|k d S )Nnitniter)r   )r.   infor'   r'   r(   callback]  s    z7TestTrustRegionConstr.test_issue_9044.<locals>.callbackc                 S   s   | d S rS   r'   r9   r'   r'   r(   <lambda>a      z7TestTrustRegionConstr.test_issue_9044.<locals>.<lambda>r   c                 S   s   d|  S rS   r'   r9   r'   r'   r(   r   a  r   c                 S   s   dS rS   r'   r9   r'   r'   r(   r   b  r   r   )r:   r7   r   r   successr   rT   r,   r   )r   r   get)r$   r   r   r'   r'   r(   test_issue_9044X  s     z%TestTrustRegionConstr.test_issue_9044N)rB   rC   rD   pytestmarkZslowr   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 r8   r'   r9   r'   r'   r(   functionz  s    z;TestEmptyConstraint.test_empty_constraint.<locals>.functionc                 S   s   t d| d  d| d  gS )Nr^   r   r,   r2   r9   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 r8   r2   r9   r'   r'   r(   
constraint  s    z=TestEmptyConstraint.test_empty_constraint.<locals>.constraintc                 S   s    t d| d  d| d  ggS )Nr+   r   rc   r,   r2   r9   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   r2   r<   r'   r'   r(   constraintlcoh  s    zATestEmptyConstraint.test_empty_constraint.<locals>.constraintlcohr   r^   r   )r   r:   r   r   r"   r,   r   r1   r   )	r   r   rQ   r   r   r   absr.   r   )
r$   r   r   r   r   r   r   Z
startpointr"   r   r'   r'   r(   test_empty_constraintx  s&    
z)TestEmptyConstraint.test_empty_constraintN)rB   rC   rD   rE   r   r'   r'   r'   r(   r   k  s   r   c               	   C   sb   dd } t j $}|t t t ddg}W 5 Q R X t|dt j}t	| ddg |d d S )Nc                 S   s   | d d | d d  S r8   r'   r9   r'   r'   r(   opt  s    ztest_bug_11886.<locals>.optr,   rT   r+   )r   )
r   Ztestingr	   r   PendingDeprecationWarningZmatrixri   r   rQ   r   )r   r   rs   Zlin_consr'   r'   r(   test_bug_11886  s    
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st |j |d j	|d 
|j  k r|d jk sn tt||j|d j t| |d|d}t|j
|j
 d S )NrT   r,   T)lbubZkeep_feasiblec                    s,   t |  jkstt |  jks(td S rJ   )r   allr   AssertionErrorr   r9   )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   r1   r+   r,   )r   expr9   r   r'   r(   obj  s    ztest_gh11649.<locals>.objc                    s    |  | d d | d  S r8   r'   r9   r   r'   r(   nce  s    ztest_gh11649.<locals>.ncec                    s    |  | d | d  S )Nr   r,   r'   r9   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   rQ   r   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	
 sHtt|j	|j
 s`tt||j	|jszttj|j	|ddstW 5 Q R X d S )N0Initial guess is not within the specified boundsr   Nelder-Meadr   r"   gMbP?)Zatol)rW   r	   r   r   r   r/   r   Z
less_equalr   r.   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   sf   t  }tddgddg}t >}|td t|jddgd|d}t|j	ddgsXt
W 5 Q R X d S )Nr   r   r   r      r   r   rW   r   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   sf   t  }tddgddg}t >}|td t|jddgd|d}t|j	dd	gsXt
W 5 Q R X 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   sN   t  }ttdd2 ttj dgddg}t|jddgd|d	 W 5 Q R X d S )
Nz7one of the lower bounds is greater than an upper bound.matchr   r   g      r   rU   r   r   )rW   r
   rK   r   r   rQ   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   sN   t  }ttdd2 ttj dgddg}t|jddgd|d	 W 5 Q R X d S )
Nr   r   r   r   r   r   r   r   r   )rW   r
   r   r   r   rQ   r   r/   r   r'   r'   r(   test_outside_bounds_warning  s    z1TestBoundedNelderMead.test_outside_bounds_warningN)rB   rC   rD   r   r   Zparametrizer   r   rQ   rW   r   r   r   r   r   xfailr   r'   r'   r'   r(   r     s   
	r   )&Znumpyr   r   Zscipy.linalgr   Zscipy.sparser   Znumpy.testingr   r   r   r   r   r	   r
   Zscipy.optimizer   r   r   r   r   r   r   rG   rN   rP   rW   rm   ru   rv   rw   r   r   r   r   r   r   r   r'   r'   r'   r(   <module>   s0     -6/.. ,4
#