
    tKg$                         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y)    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y)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       y )N         FT)r   r   r   )r   r   r   r   r   npinfselfmlbubenforce_feasibility	strict_lb	strict_ubs          i/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/scipy/optimize/tests/test_constraints.py+test_scalarvalue_unique_enforce_feasibilityz<TestStrictBounds.test_scalarvalue_unique_enforce_feasibility   s    #,R-@-. 0	9 	9w"&&&AB9rvvrvvrvv&>?",R-@-. 0	9 	9i09i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       y )Nr      r   r   )r         FTr   r   s          r   +test_vectorvalue_unique_enforce_feasibilityz<TestStrictBounds.test_vectorvalue_unique_enforce_feasibility   s    #,R.A./ 1	9 	9w"&&&AB9rvvrvvrvv&>?",R.A./ 1	9 	9i09i0r   c                     d}d}d}g d}t        ||||      \  }}t        |t        j                   dt        j                   g       t        |t        j                  dt        j                  g       y )Nr   r   r   )FTFr   r   s          r   +test_scalarvalue_vector_enforce_feasibilityz<TestStrictBounds.test_scalarvalue_vector_enforce_feasibility/   sh    2,R-@-. 0	9 	9wBFF7&;<9rvvq"&&&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       y )Nr   r!   r   r$   )TFTr"   )r   r   r   r   r   s          r   +test_vectorvalue_vector_enforce_feasibilityz<TestStrictBounds.test_vectorvalue_vector_enforce_feasibility:   sk    BFF^1,R-@-. 0	9 	9q266'1o69q"&&"&&&9:r   N)__name__
__module____qualname__r   r%   r'   r)    r   r   r   r   
   s    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      }|j                  g d      dkD  j                         sJ |j                  g d      dk(  j                         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	      }|j                  g d	      dkD  j                         sJ |j                  g d      dk(  j                         sJ fd}fd}d }	t        |t         j                   d||	|      }
t        j                  t        t        |
|       t        |
g d      }|j                  g d	      dkD  j                         sJ |j                  g d      dk(  j                         sJ y )N)r               ?F   r!   )FTT)dtyper   )g      ?      )r"   r   r   r   )r#   r   r   r$   )   r      r   )TTT)r   r8   r   c                 &    j                  |       S N)dotxAs    r   funz2test_prepare_constraint_infeasible_x0.<locals>.fun]   s    uuQx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((#6dCB/0F
MM*0&"=	F2rNI	6BLL#a',,...LL(A-22444	,	B
,l;<A((#5TBa"&&!-@AF
MM*0&"=	,Q;(
*BLL&*//111LL)*a/44666& $C"&&!S$$79I
MM*0)R@	I'7	8BLL&*//111LL)*a/44666r   c                     d } t        | ddgddg      }t        |ddg      }t        |j                  ddg      ddg       t        j
                  j                  |j                  ddg      dd	g       t        j
                  j                  |j                  ddg      d
dg       t        j
                  j                  |j                  d	dg      ddg       t        j
                  j                  |j                  d	dg      ddg       y )Nc                 `    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   s8    xx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JJ""2<<c
#;b#YGJJ""2<<c
#;dAYGJJ""2<<d#<tQiHJJ""2<<a#9D$<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      |       y )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      s    	BFF7Aq/	"B	1bffb/	"B,F(R3V<6(R35EF8L("&&"a8,G5(R35EF7L(RVVQ7F1(Aq13EF?N("&&"&&!<n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       y )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hhBFF7B'(Ghh1bff~&Gf%FBr7#r7#w"((A3-1#!?@Ff%FBrRVVGQ<(rBFFA;'r   c                   *    e Zd Zd Zd Zd Zd Zd Zy)
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                          y )	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    $2664 XXsRVVGn%rxxbff'>?XXsRVVGn%rxxbff'>XXtUm$&	
D T]F4./Gvyy'**5vyy'**5v33W5J5JK
r   c                     t        ddgddg      }t        |j                  t        j                        sJ t        |j
                  t        j                        sJ y )NrY   rt   )r   r   )r   
isinstancer   r   ndarrayr   )r   bs     r   
test_arrayzTestBounds.test_array   sF    sCjc3Z0!$$

+++!$$

+++r   c                 "   t               }t        t        j                  t        j                         t        j                  t        j                              }|j                  |j                  k(  sJ |j
                  |j
                  k(  sJ y r:   )r   r   asarrayr   r   r   )r   b1b2s      r   test_defaultszTestBounds.test_defaults   sY    XBJJw'BFF);<uu~~uu~~r   c           	         d}t        j                  t        |      5  t        t	        j
                  ddg      ddg       d d d        t        j                  t        |      5  t        ddgt	        j
                  ddg             d d d        d}t        j                  t        |      5  t        ddgddgt	        j
                  ddg             d d d        d}t        j                  t        |      5  t        ddgg d	       d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   XxY w# 1 sw Y   y xY w)
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   s    @]]:W53==!Q(1a&1 6]]:W5Aq63==!Q01 6 ;]]:W5Aq6Aq6d|1LM 6 K]]:W5Aq69% 65 6555 65 65s/   $D%$D!.(D-;D9D!D*-D69Ec                     t        dd      }ddg}t        j                  j                  |j	                  |      ddgddgf       y )Nr   rX   r   r"   r#   )r   r   rZ   assert_allcloseresidual)r   rP   rO   s      r   test_residualzTestBounds.test_residual   s@    A!W


""6??2#6!Q!Q8HIr   N)r*   r+   r,   r{   r   r   r   r   r-   r   r   rq   rq      s    L ,&Jr   rq   c                       e Zd Zd Zd Zd Zy)TestLinearConstraintc                    t        j                  d      }t        |      }t        |t         j                   t         j                        }t	        |j
                  |j
                         t	        |j                  |j                         y )Nr   )r   eyer   r   r   r   r   )r   r>   lclc2s       r   r   z"TestLinearConstraint.test_defaults   sU    FF1Ia q266'2662255#&&)255#&&)r   c           	      f   t        j                  d      }d}t        j                  t        |      5  t        |ddgg d       d d d        d}t        j                  t        |      5  t        |t        j                  ddg      ddg       d d d        t        j                  t        |      5  t        |ddgt        j                  ddg             d d d        d	}t        j                  t        |      5  t        j                  d
d
g      }t        |ddgddg|       d d d        t        j                  d      }d}t        j                  t        |      5  t        |       d d d        y # 1 sw Y   3xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   uxY w# 1 sw Y   y xY w)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>   FF1II]]:W5QA	2 6 ;]]:W5Qq!f 51v> 6]]:W5QAq!f(=> 6 :]]:W5MM4,7MQAAmL 6 HHY9]]:W5Q 65! 65 6555 65 65s;   E6*%F2%F<+F!F'6F FFF$'F0c                     t        j                  d      }t        |dd      }ddg}t         j                  j	                  |j                  |      ddgddgf       y )Nr   r   r   rX   r"   r#   )r   r   r   rZ   r   r   )r   r>   r   rO   s       r   r   z"TestLinearConstraint.test_residual   sO    FF1IaQ'!W


""2;;r?aVaV4DEr   N)r*   r+   r,   r   r   r   r-   r   r   r   r      s    * ,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      s]      6 8 8 8
9;x 9;x&7RI$N4( /J /Jd"F "Fr   