o
     e¹%  ã                   @   s    d dl Z d dlZd dlmZmZ d dlmZ d dl	m
Z
mZmZmZmZmZmZ G dd„ deƒZdd„ Zdd	„ Zd
d„ Zdd„ ZG dd„ dƒZG dd„ dƒZdS )é    N)ÚTestCaseÚassert_array_equal)ÚBoundsÚLinearConstraintÚNonlinearConstraintÚPreparedConstraintÚnew_bounds_to_oldÚold_bound_to_newÚstrict_boundsc                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚTestStrictBoundsc                 C   sŠ   d}d}d}d}t ||||ƒ\}}t|tj tj tj gƒ t|tjtjtjgƒ d}t ||||ƒ\}}t|g d¢ƒ t|g d¢ƒ d S )Né   é   é   FT)r   r   r   )r   r   r   ©r
   r   ÚnpÚinf©ÚselfÚmÚlbÚubÚenforce_feasibilityZ	strict_lbZ	strict_ub© r   úUD:\Projects\ConvertPro\env\Lib\site-packages\scipy/optimize/tests/test_constraints.pyÚ+test_scalarvalue_unique_enforce_feasibility   s"   þþz<TestStrictBounds.test_scalarvalue_unique_enforce_feasibilityc                 C   s’   d}g d¢}g d¢}d}t ||||ƒ\}}t|tj tj tj gƒ t|tjtjtjgƒ d}t ||||ƒ\}}t|g d¢ƒ t|g d¢ƒ d S )Nr   ©é   r   r   )r   é   é   FTr   r   r   r   r   Ú+test_vectorvalue_unique_enforce_feasibility   s"   þþz<TestStrictBounds.test_vectorvalue_unique_enforce_feasibilityc                 C   sV   d}d}d}g d¢}t ||||ƒ\}}t|tj dtj gƒ t|tjdtjgƒ d S )Nr   r   r   )FTFr   r   r   r   r   Ú+test_scalarvalue_vector_enforce_feasibility/   s   þz<TestStrictBounds.test_scalarvalue_vector_enforce_feasibilityc                 C   s^   d}g d¢}ddt jg}g d¢}t||||ƒ\}}t|dt j dgƒ t|dt jt jgƒ d S )Nr   r   r   r   )TFTr   )r   r   r
   r   r   r   r   r   Ú+test_vectorvalue_vector_enforce_feasibility:   s   þz<TestStrictBounds.test_vectorvalue_vector_enforce_feasibilityN)Ú__name__Ú
__module__Ú__qualname__r   r   r    r!   r   r   r   r   r   
   s
    r   c                     sÜ  t  g d¢¡} t  dt jdg¡}t  g d¢¡}t jg d¢td�}t| ||ƒ}t tt||¡ tt| |ƒg d¢ƒ}| 	g d¢¡dk 
¡ sEJ ‚| 	g d¢¡dk ¡ sRJ ‚t  g d	¢¡}t  g d	¢g d
¢g d¢g¡‰ t jg d¢td�}tˆ t j d|ƒ}t tt||¡ ttˆ t j dƒg d	¢ƒ}| 	g d	¢¡dk 
¡ s›J ‚| 	g d¢¡dk ¡ s¨J ‚‡ fdd„}‡ fdd„}dd„ }	t|t j d||	|ƒ}
t tt|
|¡ t|
g d¢ƒ}| 	g d	¢¡dk 
¡ sßJ ‚| 	g d¢¡dk ¡ sìJ ‚d S )N)r   é   é   ç      à?éF   r   )FTT)Zdtyper   )g      Ð?é   é   )r   r   r   r   )r   r   r   r   )é   r   é   r   )TTT)éöÿÿÿr   r-   r   c                    s
   ˆ   | ¡S ©N)Údot©Úx©ÚAr   r   Úfun]   ó   
z2test_prepare_constraint_infeasible_x0.<locals>.func                    s   ˆ S r.   r   r0   r2   r   r   Újac`   s   z2test_prepare_constraint_infeasible_x0.<locals>.jacc                 S   s
   t  d¡S )N)r   r   )ÚspsZ
csr_matrix)r1   Úvr   r   r   Úhessc   r5   z3test_prepare_constraint_infeasible_x0.<locals>.hess)r   Úarrayr   Úboolr   ÚpytestÚraisesÚ
ValueErrorr   Ú	violationÚanyÚallr   r   )r   r   Úx0r   ÚboundsÚpcZlinearr4   r6   r9   Z	nonlinearr   r2   r   Ú%test_prepare_constraint_infeasible_x0F   s:   ÿÿrE   c                  C   s¶   dd„ } t | ddgddgƒ}t|ddgƒ}t| ddg¡ddgƒ tj | dd	g¡dd
g¡ tj | d	d	g¡ddg¡ tj | d
dg¡ddg¡ tj | d
dg¡ddg¡ d S )Nc                 S   s.   t  | d d | d  | d d | d  g¡S )Nr   r   r   )r   r:   r0   r   r   r   Úcons_fp   s   .ztest_violation.<locals>.cons_féÿÿÿÿg333333ë¿r   r'   r   ç        g333333ó?gš™™™™™¹?g{®Gázä?r   g333333ó¿gR¸…ëQÈ?g{®Gáz„?g=
×£p=ò?)r   r   r   r?   r   ÚtestingZassert_almost_equal)rF   ZnlcrD   r   r   r   Útest_violationo   s    rJ   c            	      C   sì   t  t j ddg¡} t  dt jdg¡}g d¢}tt| |dƒ|ƒ g d¢}ttd|dƒ|ƒ g d¢}ttt j |dƒ|ƒ g d¢}tt| d	dƒ|ƒ g d
¢}tt| t jdƒ|ƒ g d¢}ttdddƒ|ƒ g d¢}ttt j t jdƒ|ƒ d S )Nr   r   é
   )©Nr   ©r   N)r   rK   ))rG   r   ©rG   N)rG   rK   rG   )rL   ©NN)NrK   ))Nr%   )r   r%   )r   r%   r%   )rO   rM   )r   N)©r   r   rP   rP   r   )rO   rO   rO   )r   r:   r   r   r   )	r   r   rC   Zbounds_single_lbZbounds_no_lbZbounds_single_ubZbounds_no_ubZbounds_single_bothZbounds_no_bothr   r   r   Útest_new_bounds_to_old�   s    rQ   c                  C   s¬   ddgddf} t  dt j dg¡}t  ddt jg¡}t| ƒ\}}t||ƒ t||ƒ t j t jft  dg¡t  dg¡fg} t| ƒ\}}t|t j dgƒ t|t jdgƒ d S )Nr   r   rL   rN   rG   r   )r   r:   r   r	   r   )rC   Zlb_trueZub_truer   r   r   r   r   Útest_old_bounds_to_new›   s   

&rR   c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )Ú
TestBoundsc              	   C   s¸   ddl m}m} ddtjdft dtj g¡t dtjg¡ft dtj g¡t dtjg¡t ddg¡ffD ]#}t|Ž }ttt|Ž ƒƒ}t|j|jƒ t|j	|j	ƒ t|j
|j
ƒ q6d S )	Nr   )r:   r   )ç      ð¿g      @rT   Tç      ð?g       @F)Únumpyr:   r   r   r   ÚevalÚreprr   r   r   Úkeep_feasible)r   r:   r   ÚargsrC   Zbounds2r   r   r   Ú	test_repr¬   s   
 ÿüõzTestBounds.test_reprc                 C   s<   t ddgddgd�}t|jtjƒsJ ‚t|jtjƒsJ ‚d S )NrH   rU   )r   r   )r   Ú
isinstancer   r   Zndarrayr   )r   Úbr   r   r   Ú
test_array¼   s   zTestBounds.test_arrayc                 C   sF   t ƒ }t t tj ¡t tj¡ƒ}|j|jksJ ‚|j|jks!J ‚d S r.   )r   r   Zasarrayr   r   r   )r   Úb1Úb2r   r   r   Útest_defaultsÂ   s   zTestBounds.test_defaultsc                 C   s,  d}t jt|d�� tt ddg¡ddgƒ W d   ƒ n1 s w   Y  t jt|d�� tddgt ddg¡ƒ W d   ƒ n1 sCw   Y  d}t jt|d�� tddgddgt ddg¡d� W d   ƒ n1 slw   Y  d}t jt|d�� tddgg d	¢ƒ W d   ƒ d S 1 s�w   Y  d S )
Nz,Lower and upper bounds must be dense arrays.©Úmatchr   r   z&`keep_feasible` must be a dense array.T©rY   z6`lb`, `ub`, and `keep_feasible` must be broadcastable.r   )r<   r=   r>   r   r7   Ú	coo_array)r   Úmessager   r   r   Útest_input_validationÈ   s   ÿÿ"ÿ"ÿz TestBounds.test_input_validationc                 C   s6   t ddƒ}ddg}tj | |¡ddgddgf¡ d S )Néþÿÿÿr   rG   r   r   r   )r   r   rI   Úassert_allcloseÚresidual)r   rC   rB   r   r   r   Útest_residual×   s   
$zTestBounds.test_residualN)r"   r#   r$   r[   r^   ra   rg   rk   r   r   r   r   rS   «   s    rS   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚTestLinearConstraintc                 C   sD   t  d¡}t|ƒ}t|t j t jƒ}t|j|jƒ t|j|jƒ d S )Nr   )r   Úeyer   r   r   r   r   )r   r3   ÚlcZlc2r   r   r   ra   Þ   s
   
z"TestLinearConstraint.test_defaultsc                 C   s†  t  d¡}d}tjt|d�� t|ddgg d¢ƒ W d   ƒ n1 s#w   Y  d}tjt|d�� t|t ddg¡ddgƒ W d   ƒ n1 sIw   Y  tjt|d�� t|ddgt ddg¡ƒ W d   ƒ n1 smw   Y  d	}tjt|d�� t d
d
g¡}t|ddgddg|d� W d   ƒ n1 s™w   Y  t  d¡}d}tjt|d�� t|ƒ W d   ƒ d S 1 s¼w   Y  d S )Nr   z5`lb`, `ub`, and `keep_feasible` must be broadcastablerb   r   r   r   z&Constraint limits must be dense arraysr   z%`keep_feasible` must be a dense arrayTrd   )r   r   r   z%`A` must have exactly two dimensions.)	r   rm   r<   r=   r>   r   r7   re   Úempty)r   r3   rf   rY   r   r   r   rg   å   s,   
ÿÿÿþ

"ÿz*TestLinearConstraint.test_input_validationc                 C   sB   t  d¡}t|ddƒ}ddg}t j | |¡ddgddgf¡ d S )Nr   rh   r   rG   r   r   )r   rm   r   rI   ri   rj   )r   r3   rn   rB   r   r   r   rk   û   s   
$z"TestLinearConstraint.test_residualN)r"   r#   r$   ra   rg   rk   r   r   r   r   rl   Ý   s    rl   )r<   rV   r   Znumpy.testingr   r   Zscipy.sparseÚsparser7   Zscipy.optimize._constraintsr   r   r   r   r   r	   r
   r   rE   rJ   rQ   rR   rS   rl   r   r   r   r   Ú<module>   s    $<)2