o
    ?Hh$                     @   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   e/home/air/sanwanet/gpt-api/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
 sJ |	g ddk sJ  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)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_fg333333r   r)   r           g333333?g?g{Gz?r   g333333gRQ?g{Gz?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 sw   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 )Nr   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 sw   Y  t d}d}tjt|d t| W d    d S 1 sw   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