U
    »mœ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 )ÚMaratosú²Problem 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'   úg/home/sam/Atlas/atlas_env/lib/python3.8/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 ©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 )Néÿÿÿÿr   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 )Ú
Rosenbrockz�Rosenbrock 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+   éÈ   é�  épþÿÿr   )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ÚdiagZ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 )	ÚIneqRosenbrockzö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: 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(   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š™™™™™É?rc   r   )rW   r)   r   r   r   r"   ro   r'   r'   r(   r)      s    
zBoundedRosenbrock.__init__N)r   )rB   rC   rD   rE   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+   rT   rm   gæWs€`ŽÚ?gÙ|\*ÆÅ?rn   ro   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 rp   rq   )r$   ZA_ineqZb_ineqZA_eqZb_eqr'   r'   r(   r@   7  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.
    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$   rw   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   ©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 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+   rm   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   rx   )	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arangerw   ra   Zvstackrx   )r$   r.   rƒ   r„   r…   Údr�   Zdm5ÚiZHxxZHxyZHxzZHyyZHyzZHzzrk   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   rx   )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(   rv   A  s       ÿ
&rv   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rØqÂ|j
dkrì|dkrìqÂtƒ �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�rŽt|jdƒ |jdk�rŽt|jdƒ |jdkrÂtdƒ‚qÂqžqŒd 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•   Fzdelta_grad == 0.0útrust-constr©Úmethodr:   r7   r"   Úconstraintsé   ©Údecimalr,   ç:Œ0âŽyE>Útr_interior_point©r   rU   úInvalid termination condition.)r   r   rN   rP   r   rW   rl   ru   rt   rv   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 rp   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 rp   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r�t|jdƒ |jd	kr�t|jdƒ |j
d
kr¢tdƒ‚d S )Nc                    s   ˆ   | ¡}| |¡S rJ   )r7   Údot)r.   Úprk   ©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rŒt|jd	ƒ |jd
krŒt|jd	ƒ |jdkržt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   Útestingr	   r¢   ÚPendingDeprecationWarningÚmatrixri   r   rQ   r   )rÐ   rª   rr   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s†t‚ˆ |jƒ |d j	|d  
|j¡  k r¼|d jk sÂn 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)g®Gáz®ï?g®Gáz®ï¿éöÿÿÿ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_optgš™™™™™é¿g      @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�s�t‚W 5 Q R X d S )Nú0Initial guess is not within the specified boundsrâ   úNelder-Mead©r™   r"   gü©ñÒMbP?)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å   )&Ú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   rl   rt   ru   rv   r“   rÇ   rÔ   rÆ   r÷   rä   rå   r'   r'   r'   r(   Ú<module>   s0     -6/.. ,4ÿ
#