
    ^Mh$                         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e          Zd Zd Zd Zd	 Z G d
 d          Z G d d          ZdS )    N)TestCaseassert_array_equal)BoundsLinearConstraintNonlinearConstraintPreparedConstraintnew_bounds_to_oldold_bound_to_newstrict_boundsc                   &    e Zd Zd Zd Zd Zd ZdS )TestStrictBoundsc                    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_ubs          e/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/scipy/optimize/tests/test_constraints.py+test_scalarvalue_unique_enforce_feasibilityz<TestStrictBounds.test_scalarvalue_unique_enforce_feasibility   s    #,R-@-. 0  0	9 	9w"&&ABBB9rvrvrv&>???",R-@-. 0  0	9 	9iii0009iii00000    c                    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   s          r   +test_vectorvalue_unique_enforce_feasibilityz<TestStrictBounds.test_vectorvalue_unique_enforce_feasibility   s    YYYY#,R.A./ 1  1	9 	9w"&&ABBB9rvrvrv&>???",R.A./ 1  1	9 	9iii0009iii00000r   c                     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   s          r   +test_scalarvalue_vector_enforce_feasibilityz<TestStrictBounds.test_scalarvalue_vector_enforce_feasibility/   s}    222,R-@-. 0  0	9 	9wBF7&;<<<9rvq"&&9:::::r   c                     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   s          r   +test_vectorvalue_vector_enforce_feasibilityz<TestStrictBounds.test_vectorvalue_vector_enforce_feasibility:   s    YYBF^111,R-@-. 0  0	9 	9q26'1o6669q"&"&&9:::::r   N)__name__
__module____qualname__r   r%   r'   r)    r   r   r   r   
   sP        1 1 1$1 1 1$	; 	; 	;	; 	; 	; 	; 	;r   r   c                     t          j        g d          } t          j        dt           j        dg          }t          j        g d          }t          j        g dt                    }t	          | ||          }t          j        t          t          ||           t          t	          | |          g d          }|	                    g d          dk    
                                sJ |	                    g d          dk                                    sJ t          j        g d	          }t          j        g d	g d
g dg          t          j        g dt                    }t          t           j         d|          }t          j        t          t          ||           t          t          t           j         d          g d	          }|	                    g d	          dk    
                                sJ |	                    g d          dk                                    sJ fd}fd}d }	t          |t           j         d||	|          }
t          j        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   r8   r   c                 .                         |           S N)dotxAs    r   funz2test_prepare_constraint_infeasible_x0.<locals>.fun]   s    uuQxxr   c                     S r:   r-   r<   s    r   jacz2test_prepare_constraint_infeasible_x0.<locals>.jac`   s    r   c                 *    t          j        d          S )N)r   r   )sps
csr_matrix)r=   vs     r   hessz3test_prepare_constraint_infeasible_x0.<locals>.hessc   s    ~f%%%r   )r   arrayr   boolr   pytestraises
ValueErrorr   	violationanyallr   r   )r   r   x0r   boundspclinearr?   rA   rF   	nonlinearr>   s              @r   %test_prepare_constraint_infeasible_x0rT   F   s   	+++		B	3#	$	$B	)))		B(#6#6#6dCCCB/00F
M*0&"===	F2rNNIII	6	6BLL##a',,.....LL((A-2244444	,,,		B
,,,lll;<<A(#5#5#5TBBBa"&!-@AAF
M*0&"===	,Q;;(LL
* 
*BLL&&*//11111LL)))**a/4466666        & & & $C"&!S$$79 9I
M*0)R@@@	I'7'7'7	8	8BLL&&*//11111LL)))**a/446666666r   c                  `   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                 v    t          j        | d         dz  | d         z   | d         dz  | d         z
  g          S )Nr   r   r"   )r   rG   )r=   s    r   cons_fztest_violation.<locals>.cons_fp   s9    x1QqT)1Q419qt+;<===r   g333333r   r1   r"           g333333?g?g{Gz?r   g333333gRQ?g{Gz?g=
ףp=?)r   r   r   rL   r   testingassert_almost_equal)rW   nlcrQ   s      r   test_violationr]   o   s   > > > fr7maV
<
<C	C#q	*	*Br||S!H--Bx888J""2<<c
#;#;b#YGGGJ""2<<c
#;#;dAYGGGJ""2<<d#<#<tQiHHHJ""2<<a#9#9D$<HHHHHr   c                     t          j        t           j         ddg          } t          j        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   r_   ))rX   r   rX   N)rX   r_   rX   )r`   NN)Nr_   ))Nr/   )r   r/   )r   r/   r/   )rc   ra   )r   N)r"   r   rd   rd   r"   )rc   rc   rc   )r   rG   r   r   r	   )	r   r   rP   bounds_single_lbbounds_no_lbbounds_single_ubbounds_no_ubbounds_single_bothbounds_no_boths	            r   test_new_bounds_to_oldrk      sb   	BF7Aq/	"	"B	1bfb/	"	"B,,,F(R33V<<<666(R335EFFF888L("&"a88,GGG555(R335EFFF777L(RVQ77FFF111(Aq113EFFF???N("&"&!<<nMMMMMr   c                     ddgddf} t          j        dt           j         dg          }t          j        ddt           j        g          }t          |           \  }}t	          ||           t	          ||           t           j         t           j        ft          j        dg          t          j        dg          fg} t          |           \  }}t	          |t           j         dg           t	          |t           j        dg           d S )Nr"   r   r`   rb   rX   r   )r   rG   r   r
   r   )rP   lb_trueub_truer   r   s        r   test_old_bounds_to_newro      s    !fi,FhBF7B'((Gh1bf~&&Gf%%FBr7###r7###w"(A3--1#!?@Ff%%FBrRVGQ<(((rBFA;'''''r   c                   ,    e Zd Zd Zd Zd Zd Zd ZdS )
TestBoundsc           	      \   ddl m}m} ddt          j        dft          j        dt          j         g          t          j        dt          j        g          ft          j        dt          j         g          t          j        dt          j        g          t          j        ddg          ffD ]|}t	          | }t          t          t	          |                     }t          |j        |j                   t          |j	        |j	                   t          |j
        |j
                   }d S )	Nr   )rG   r   )      g      @rs   T      ?g       @F)numpyrG   r   r   r   evalreprr   r   r   keep_feasible)r   rG   r   argsrP   bounds2s         r   	test_reprzTestBounds.test_repr   s   $$$$$$$$264 XsRVGn%%rxbf'>'>?XsRVGn%%rxbf'>'>XtUm$$&	
 	L 	LD T]F4..//Gvy'*555vy'*555v3W5JKKKK	L 	Lr   c                     t          ddgddg          }t          |j        t          j                  sJ t          |j        t          j                  sJ d S )NrY   rt   )r   r   )r   
isinstancer   r   ndarrayr   )r   bs     r   
test_arrayzTestBounds.test_array   sV    sCjc3Z000!$
+++++!$
+++++++r   c                     t                      }t          t          j        t          j                   t          j        t          j                            }|j        |j        k    sJ |j        |j        k    sJ d S r:   )r   r   asarrayr   r   r   )r   b1b2s      r   test_defaultszTestBounds.test_defaults   sZ    XXBJw''BF););<<u~~~~u~~~~~~r   c           	         d}t          j        t          |          5  t          t	          j        ddg          ddg           d d d            n# 1 swxY w Y   t          j        t          |          5  t          ddgt	          j        ddg                     d d d            n# 1 swxY w Y   d}t          j        t          |          5  t          ddgddgt	          j        ddg                     d d d            n# 1 swxY w Y   d}t          j        t          |          5  t          ddgg d	           d d d            d S # 1 swxY w 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!   )rI   rJ   rK   r   rC   	coo_array)r   messages     r   test_input_validationz TestBounds.test_input_validation   se   @]:W555 	2 	23=!Q((1a&111	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2]:W555 	2 	2Aq63=!Q00111	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 ;]:W555 	N 	NAq6Aq6d|1L1LMMMM	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N K]:W555 	& 	&Aq6999%%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&sG   'AAA7'B**B.1B.+D		DD1EEEc                     t          dd          }ddg}t          j                            |                    |          ddgddgf           d S )Nr   rX   r   r"   r#   )r   r   rZ   assert_allcloseresidual)r   rP   rO   s      r   test_residualzTestBounds.test_residual   sO    A!W

""6??2#6#6!Q!Q8HIIIIIr   N)r*   r+   r,   r{   r   r   r   r   r-   r   r   rq   rq      sg        L L L , , ,  & & &J J J J Jr   rq   c                        e Zd Zd Zd Zd ZdS )TestLinearConstraintc                     t          j        d          }t          |          }t          |t           j         t           j                  }t	          |j        |j                   t	          |j        |j                   d S )Nr   )r   eyer   r   r   r   r   )r   r>   lclc2s       r   r   z"TestLinearConstraint.test_defaults   sa    F1IIa  q26'262225#&)))25#&)))))r   c           	         t          j        d          }d}t          j        t          |          5  t          |ddgg d           d d d            n# 1 swxY w Y   d}t          j        t          |          5  t          |t          j        ddg          ddg           d d d            n# 1 swxY w Y   t          j        t          |          5  t          |ddgt          j        ddg                     d d d            n# 1 swxY w Y   d	}t          j        t          |          5  t          j        d
d
g          }t          |ddgddg|           d d d            n# 1 swxY w Y   t          j        d          }d}t          j        t          |          5  t          |           d d d            d S # 1 swxY w Y   d S )Nr   z5`lb`, `ub`, and `keep_feasible` must be broadcastabler   r"   r   r!   z&Constraint limits must be dense arraysr   z%`keep_feasible` must be a dense arrayTr   )r   r   r#   z%`A` must have exactly two dimensions.)	r   r   rI   rJ   rK   r   rC   r   empty)r   r>   r   rx   s       r   r   z*TestLinearConstraint.test_input_validation   s   F1III]:W555 	3 	3QA			222	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 ;]:W555 	? 	?Qq!f 5 51v>>>	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	?]:W555 	? 	?QAq!f(=(=>>>	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? :]:W555 	M 	MM4,77MQAAmLLLL	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M HY9]:W555 	  	 Q	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 sY   AAA<(B00B47B4(D

DD2.E,,E03E0(GG	G	c                     t          j        d          }t          |dd          }ddg}t           j                            |                    |          ddgddgf           d S )Nr   r   r   rX   r"   r#   )r   r   r   rZ   r   r   )r   r>   r   rO   s       r   r   z"TestLinearConstraint.test_residual   s^    F1IIaQ''!W

""2;;r??aVaV4DEEEEEr   N)r*   r+   r,   r   r   r   r-   r   r   r   r      sF        * * *     ,F F F F Fr   r   )rI   ru   r   numpy.testingr   r   scipy.sparsesparserC   scipy.optimize._constraintsr   r   r   r   r	   r
   r   r   rT   r]   rk   ro   rq   r   r-   r   r   <module>r      sy        6 6 6 6 6 6 6 6      8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
9; 9; 9; 9; 9;x 9; 9; 9;x&7 &7 &7RI I I$N N N4( ( ( /J /J /J /J /J /J /J /Jd"F "F "F "F "F "F "F "F "F "Fr   