U
    /d                      @   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|dddg t|dddg d S N         FTr
   r   npinfselfmlbubenforce_feasibilityZ	strict_lbZ	strict_ub r   I/tmp/pip-unpacked-wheel-9gxwnfpp/scipy/optimize/tests/test_constraints.py+test_scalarvalue_unique_enforce_feasibility   s"    z<TestStrictBounds.test_scalarvalue_unique_enforce_feasibilityc                 C   s   d}dddg}dddg}d}t ||||\}}t|tj tj tj g t|tjtjtjg d}t ||||\}}t|dddg t|dddg d S )	Nr      r   r         FTr   r   r   r   r   +test_vectorvalue_unique_enforce_feasibility   s"    

z<TestStrictBounds.test_vectorvalue_unique_enforce_feasibilityc                 C   sX   d}d}d}dddg}t ||||\}}t|tj dtj g t|tjdtjg d S r   r   r   r   r   r   +test_scalarvalue_vector_enforce_feasibility/   s    
z<TestStrictBounds.test_scalarvalue_vector_enforce_feasibilityc                 C   sb   d}dddg}ddt jg}dddg}t||||\}}t|dt j dg t|dt jt jg d S )Nr   r   r   r   r   TF)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 dddg} t dt jdg}t dddg}t jd	d
d
gtd}t| ||}ttt|| tt| |dddg}|	dddgdk
 st|	dddgdk stt ddddg}t ddddgddddgddddgg t jd
d
d
gtd}t t j d|}ttt|| tt t j dddddg}|	ddddgdk
 s^t|	ddddgdk s~t fdd} fdd}dd }	t|t j d||	|}
ttt|
| t|
ddddg}|	ddddgdk
 st|	ddddgdk std S )Nr               ?F   r   r   r   FT)Zdtypeg      ?      r   r   r         ic                    s
     | S N)dotxAr   r   fun]   s    z2test_prepare_constraint_infeasible_x0.<locals>.func                    s    S r-   r   r/   r1   r   r   jac`   s    z2test_prepare_constraint_infeasible_x0.<locals>.jacc                 S   s
   t dS )N)r   r   )spsZ
csr_matrix)r0   vr   r   r   hessc   s    z3test_prepare_constraint_infeasible_x0.<locals>.hess)r   arrayr   boolr   pytestraises
ValueErrorr   	violationanyAssertionErrorallr   r   )r   r   x0r   boundspcZlinearr3   r4   r7   Z	nonlinearr   r1   r   %test_prepare_constraint_infeasible_x0F   s:    (
   rD   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   r8   r/   r   r   r   cons_fp   s    ztest_violation.<locals>.cons_fg333333r   r'   r           g333333?g?g{Gz?r   g333333gRQ?g{Gz?g=
ףp=?)r   r   r   r=   r   testingZassert_almost_equal)rE   ZnlcrC   r   r   r   test_violationo   s    rI   c            	      C   s   t t j ddg} t dt jdg}dddg}tt| |d| ddd	g}ttd
|d| dddg}ttt j |d| dddg}tt| dd| dddg}tt| t jd| dddg}ttddd| dddg}ttt j t jd| d S )Nr   r   
   Nr   )r   N)r   rJ   )rF   r   rF   N)rF   rJ   rF   )NN)NrJ   )Nr%   )r   r%   )r   r%   r%   )r   N)r   r   r   )r   r8   r   r   r   )	r   r   rB   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     






rM   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   rK   rL   rF   r   )r   r8   r   r	   r   )rB   Zlb_trueZub_truer   r   r   r   r   test_old_bounds_to_new   s    

&rN   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 ]F}t| }ttt| }t|j|j t|j	|j	 t|j
|j
 qld S )	Nr   )r8   r   )      g      @rP   T      ?g       @F)numpyr8   r   r   r   evalreprr   r   r   Zkeep_feasible)r   r8   r   argsrB   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&tt|jtjs8td S )NrG   rQ   )r   r   )r   
isinstancer   r   Zndarrayr?   r   )r   br   r   r   
test_array   s    zTestBounds.test_arrayc                 C   sF   t  }t ttj ttj}|j|jks2t|j|jksBtd S r-   )r   r   Zasarrayr   r   r?   r   )r   Zb1Zb2r   r   r   test_defaults   s    zTestBounds.test_defaultsc              	   C   s6   d}t jt|d tddgdddg W 5 Q R X d S )Nz6`lb`, `ub`, and `keep_feasible` must be broadcastable.matchr   r   r   )r:   r;   r<   r   )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 )Nr   rF   r   r   r   )r   r   rH   assert_allcloseresidual)r   rB   rA   r   r   r   test_residual   s    
zTestBounds.test_residualN)r"   r#   r$   rV   rY   rZ   r^   rb   r   r   r   r   rO      s
   rO   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   r2   lcZlc2r   r   r   rZ      s
    
z"TestLinearConstraint.test_defaultsc              	   C   sr   t d}d}tjt|d t|ddgdddg W 5 Q R X t d}d}tjt|d t| W 5 Q R X d S )	Nr   z5`lb`, `ub`, and `keep_feasible` must be broadcastabler[   r   r   r   )r   r   r   z%`A` must have exactly two dimensions.)r   rd   r:   r;   r<   r   empty)r   r2   r]   r   r   r   r^      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   rF   r   r   )r   rd   r   rH   r`   ra   )r   r2   re   rA   r   r   r   rb      s    
z"TestLinearConstraint.test_residualN)r"   r#   r$   rZ   r^   rb   r   r   r   r   rc      s   rc   )r:   rR   r   Znumpy.testingr   r   Zscipy.sparsesparser5   Zscipy.optimize._constraintsr   r   r   r   r   r	   r
   r   rD   rI   rM   rN   rO   rc   r   r   r   r   <module>   s   $<)(