o
    Ö­jÀ$  ã                   @   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_feasibilityÚ	strict_lbÚ	strict_ub© r   úb/var/www/html/CropPilot/venv/lib/python3.10/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)Údtyper   )g      Ð?é   é   )r   r   r   r   )r   r   r   r    )é   r   é   r   )TTT)éöÿÿÿr   r0   r   c                    s
   ˆ   | ¡S ©N)Údot©Úx©ÚAr   r   Úfun]   ó   
z2test_prepare_constraint_infeasible_x0.<locals>.func                    s   ˆ S r1   r   r3   r5   r   r   Újac`   s   z2test_prepare_constraint_infeasible_x0.<locals>.jacc                 S   s
   t  d¡S )N)r   r   )ÚspsÚ
csr_matrix)r4   Úvr   r   r   Úhessc   r8   z3test_prepare_constraint_infeasible_x0.<locals>.hess)r   Úarrayr   Úboolr   ÚpytestÚraisesÚ
ValueErrorr   Ú	violationÚanyÚallr   r   )r   r   Úx0r   ÚboundsÚpcÚlinearr7   r9   r=   Ú	nonlinearr   r5   r   Ú%test_prepare_constraint_infeasible_x0F   s:   ÿÿrK   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>   r3   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   rC   r   ÚtestingÚassert_almost_equal)rL   ÚnlcrH   r   r   r   Útest_violationo   s    rR   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   rS   ))rM   r   ©rM   N)rM   rS   rM   )rT   ©NN)NrS   ))Nr'   )r   r'   )r   r'   r'   )rW   rU   )r   N)©r   r   rX   rX   r   )rW   rW   rW   )r   r>   r   r   r   )	r   r   rG   Úbounds_single_lbÚbounds_no_lbÚbounds_single_ubÚbounds_no_ubÚbounds_single_bothÚbounds_no_bothr   r   r   Útest_new_bounds_to_old�   s    r_   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   rT   rV   rM   r   )r   r>   r   r	   r   )rG   Úlb_trueÚub_truer   r   r   r   r   Útest_old_bounds_to_new›   s   

&rb   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      @rd   Tç      ð?g       @F)Únumpyr>   r   r   r   ÚevalÚreprr   r   r   Úkeep_feasible)r   r>   r   ÚargsrG   Ú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 )NrN   re   )r   r   )r   Ú
isinstancer   r   Ú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 r1   )r   r   Ú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©ri   z6`lb`, `ub`, and `keep_feasible` must be broadcastable.r   )r@   rA   rB   r   r:   Ú	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   rM   r   r   r   )r   r   rO   Úassert_allcloseÚresidual)r   rG   rF   r   r   r   Útest_residual×   s   
$zTestBounds.test_residualN)r$   r%   r&   rl   rp   rt   rz   r~   r   r   r   r   rc   «   s    rc   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   r6   ÚlcÚlc2r   r   r   rt   Þ   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 broadcastableru   r   r   r   z&Constraint limits must be dense arraysr   z%`keep_feasible` must be a dense arrayTrw   )r   r   r   z%`A` must have exactly two dimensions.)	r   r€   r@   rA   rB   r   r:   rx   Úempty)r   r6   ry   ri   r   r   r   rz   å   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   r{   r   rM   r   r   )r   r€   r   rO   r|   r}   )r   r6   r�   rF   r   r   r   r~   û   s   
$z"TestLinearConstraint.test_residualN)r$   r%   r&   rt   rz   r~   r   r   r   r   r   Ý   s    r   )r@   rf   r   Únumpy.testingr   r   Úscipy.sparseÚsparser:   Úscipy.optimize._constraintsr   r   r   r   r   r	   r
   r   rK   rR   r_   rb   rc   r   r   r   r   r   Ú<module>   s    $<)2