a
    Re$                     @   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   Y/var/www/sunrise/env/lib/python3.9/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
 sJ |	g ddk sJ 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
 s8J |	g ddk sTJ  fdd} fdd}dd }	t|t j d||	|}
ttt|
| t|
g d}|	g d	dk
 sJ |	g ddk sJ 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]   s    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   s    z3test_prepare_constraint_infeasible_x0.<locals>.hess)r   arrayr   boolr   pytestraises
ValueErrorr   	violationanyallr   r   )r   r   x0r   boundspcZlinearr4   r5   r8   Z	nonlinearr   r2   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   r9   r0   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}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   rJ   ))rF   r   rF   N)rF   rJ   rF   )rK   NN)NrJ   ))Nr%   )r   r%   )r   r%   r%   )rN   rL   )r   N)r   r   rO   rO   r   )rN   rN   rN   )r   r9   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     rP   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   rM   rF   r   )r   r9   r   r	   r   )rB   Zlb_trueZub_truer   r   r   r   r   test_old_bounds_to_new   s    

&rQ   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   )r9   r   )      g      @rS   T      ?g       @F)numpyr9   r   r   r   evalreprr   r   r   keep_feasible)r   r9   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&J t|jtjs8J d S )NrG   rT   )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s2J |j|jksBJ 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@0    Y  t jt|d( tddgtddg W d    n1 s0    Y  d}t jt|d0 tddgddgtddgd W d    n1 s0    Y  d}t jt|d" tddgg d	 W d    n1 s0    Y  d S )
Nz,Lower and upper bounds must be dense arrays.matchr   r   z&`keep_feasible` must be a dense array.TrX   z6`lb`, `ub`, and `keep_feasible` must be broadcastable.r   )r;   r<   r=   r   r6   	coo_array)r   messager   r   r   test_input_validation   s    66>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$   rZ   r]   r`   rf   rj   r   r   r   r   rR      s
   rR   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   r`      s
    
z"TestLinearConstraint.test_defaultsc                 C   s  t d}d}tjt|d$ t|ddgg d W d    n1 sF0    Y  d}tjt|d* t|tddgddg W d    n1 s0    Y  tjt|d* t|ddgtddg W d    n1 s0    Y  d	}tjt|d6 td
d
g}t|ddgddg|d W d    n1 s40    Y  t d}d}tjt|d t| W d    n1 sz0    Y  d S )Nr   z5`lb`, `ub`, and `keep_feasible` must be broadcastablera   r   r   r   z&Constraint limits must be dense arraysr   z%`keep_feasible` must be a dense arrayTrc   )r   r   r   z%`A` must have exactly two dimensions.)	r   rl   r;   r<   r=   r   r6   rd   empty)r   r3   re   rX   r   r   r   rf      s"    
2888
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   rg   r   rF   r   r   )r   rl   r   rH   rh   ri   )r   r3   rm   rA   r   r   r   rj      s    
z"TestLinearConstraint.test_residualN)r"   r#   r$   r`   rf   rj   r   r   r   r   rk      s   rk   )r;   rU   r   Znumpy.testingr   r   Zscipy.sparsesparser6   Zscipy.optimize._constraintsr   r   r   r   r   r	   r
   r   rD   rI   rP   rQ   rR   rk   r   r   r   r   <module>   s   $<)2