o
    Ö­j&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 )Ú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   ó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&   úk/var/www/html/CropPilot/venv/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 )Néÿÿÿÿr   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 )Ú
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 )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+   éÈ   é�  épþÿÿr   )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 )	Ú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   ó,   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‡   gæWs€`ŽÚ?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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ƒ d|j› d�}|jdvs©J |ƒ‚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,   ç:Œ0âŽyE>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)g®Gáz®ï?g®Gáz®ï¿éöÿÿÿ)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_optgš™™™™™é¿r:  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 )Nú0Initial guess is not within the specified boundsr.  úNelder-Mead©rÐ   r!   gü©ñÒMbP?)Ú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#