
    biL                         d dl Z d dlZd dlZd dlmZ d dlZd dl	m
Z
 d dlmZ d dlmZ d dlmZ  G d d      Z G d d	      Zy)
    N)
Inequality)PSD)Equality)CvxAttr2Constrc                      e Zd Zej                  j                  d ej                  ddg       ej                  ddg      gddgg      d        Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)TestAttributessparsityr      r   r
   r      c                 f   t        j                  d|      }t        j                  t        j                  t        j                  |            |dk\  |dk  g      }|j                          t        j                  d      }d||j                  <   t        j                  |j                  |      sJ y )N   r   r	   r
   )cpVariableProblemMinimizesumsolvenpzeros
sparse_idxallclosevalue)selfr	   Xprobzs        V/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/tests/test_attributes.pytest_sparsity_patternz$TestAttributes.test_sparsity_pattern   s|    KK2zz"++bffQi017AF2CD

HHV!,,{{177A&&&    c                 <   t        d t        j                  j                  d      d d D              dk  ry t	        j
                  d      j                  d      }t	        j                  |dz  dk(        }t        j                  d|      }t        j                  t        j                  t        j                  |            |d	k\  |d
k  g      }|j                          t	        j                  d      }d	||<   t	        j                  |j                   |      sJ y )Nc              3   2   K   | ]  }t        |        y w)N)int).0ss     r"   	<genexpr>z9TestAttributes.test_sparsity_condition.<locals>.<genexpr>   s     <AQ<s   .r   )r         )r   r   r   r   r   r   r
   )tuplesysversionsplitr   arangereshapewherer   r   r   r   r   r   r   r   r   )r   datamaskr   r    r!   s         r"   test_sparsity_conditionz&TestAttributes.test_sparsity_condition   s    <!2!23!7!;<<wFyy|##G,xxqA&KK$/zz"++bffQi017AF2CD

HHW${{177A&&&r$   c                     t        j                  t        d      5  t        j                  dg d       d d d        y # 1 sw Y   y xY w)Nz?mismatching number of index arrays for shape; got 3, expected 2matchr   )r   r   r   r   pytestraises
ValueErrorr   r   r   s    r"   test_sparsity_invalid_inputz*TestAttributes.test_sparsity_invalid_input%   s?    ]]: .B C 	CKK)AB	C 	C 	C	   ?Ac                     t        j                  t        d      5  t        j                  dddg       d d d        y # 1 sw Y   y xY w)Nz3all index and data arrays must have the same lengthr9   r   r   r   r
   r   r   r;   r?   s    r"   test_sparsity_incorrect_dimz*TestAttributes.test_sparsity_incorrect_dim*   s>    ]]S
 	> KK&))<=	> 	> 	>rA   c                     t        j                  t        d      5  t        j                  dddg       d d d        y # 1 sw Y   y xY w)Nz)axis 1 index 5 exceeds matrix dimension 3r9   r   rC   )r         r   r;   r?   s    r"   test_sparsity_out_of_boundsz*TestAttributes.test_sparsity_out_of_bounds0   sB    ]]I
 	A KK)Y)?@	A 	A 	ArA   c                     t        j                  t        d      5  t        j                  dg       d d d        y # 1 sw Y   y xY w)Nz!Indices should have 0 dimensions.r9   r   r   r;   r?   s    r"   test_sparsity_0D_variablez(TestAttributes.test_sparsity_0D_variable6   s4    ]]:-PQ 	+KK&*	+ 	+ 	+s	   =Ac                 L   t        j                  dddg      }t        j                  t        j                  t        j                  |            |dk\  |dk  g      }|j                  t         j                        d   d   j                  d   d	k(  sJ t        j                  d      }t        j                  t        j                  t        j                  |            |dk\  |dk  g      }|j                  t         j                        d   d   j                  d   d
k(  sJ y )Nr   r   r   r   r   r
   r   Ar   	   )r   r   r   r   r   get_problem_dataCLARABELshape)r   r   r    s      r"   test_sparsity_reduces_num_varz,TestAttributes.test_sparsity_reduces_num_var:   s    KK&&)9:zz"++bffQi017AF2CD$$R[[1!4S9??BaGGGKKzz"++bffQi017AF2CD$$R[[1!4S9??BaGGGr$   c                 *   t        j                  d      }ddg}t        j                  d|      }t        j                  t        j                  t        j
                  |            ||k\  g      }t        j                  d      }d||d   |d   f<   t        j                  t        d	      5  ||_        d d d        |j                          t        j                  d      }d||j                  <   t        j                  |j                  |      sJ t        j                   t        j"                  d
       |fd      |_        |j                          t        j                  |j                  |      sJ t        j                   g dddgfd      }t        j                   g dddgfd      }||_        |j                          t        j                  |j'                         |j'                               sJ t        j                  |j                  |j'                               sJ t        j                  |j                  |j'                               sJ ||_        |j                          t        j                  |j'                         |j'                               sJ t        j                  |j                  |j'                               sJ t        j                  |j                  |j'                               sJ y # 1 sw Y   TxY w)N
   rT   r   r   r
   r   r   r
   r   r   r   r   r   r
   z]Writing to a sparse CVXPY expression via `.value` is discouraged. Use `.value_sparse` insteadr9   rF   )r   rP   )r   rY   rX   rW   )r   r   r   r
   )r   r   	Parameterr   r   r   r   r   r<   warnsRuntimeWarningr   r   r   r   sp	coo_arrayonesvalue_sparsetoarray)r   r   r	   rL   r    A_valuer!   z1s           r"   test_sparsity_assign_valuez)TestAttributes.test_sparsity_assign_valueC   s8   KK! ,/LLH5zz"++bffQi016(;((8$,.Xa[()\\1
 	
 AG	 	

HHX!,,{{177A&&&
{H&=xH

{{177A&&&LL*\<,HIQYZ\\+lL-IJRZ[

{{199;

555{{177BJJL111{{177AIIK000

{{199;

555{{177BJJL111{{177AIIK0007	 	s   .LLc                 n   t        j                  dddg      }t        j                  t        d      5  t        j                  d      |_        d d d        t        j                  t        d      5  t        j                  dgd	gd	gffd      |_
        d d d        y # 1 sw Y   PxY w# 1 sw Y   y xY w)
Nr   rU   rV   r   z9Parameter value must be zero outside of sparsity pattern.r9   zpInvalid sparsity pattern \(array\(\[0\](, dtype=int32)?\), array\(\[0\](, dtype=int32)?\)\) for Parameter value.r
   r   )r   r[   r<   r=   r>   r   r`   r   r^   r_   ra   )r   rL   s     r"   test_sparsity_incorrect_patternz.TestAttributes.test_sparsity_incorrect_patterng   s    LL<*FG]]Y
 	& ggfoAG	& ]]*
 	E  \\A3!qc
*;VDAN	E 	E		& 	&	E 	Es   B3#B+B(+B4c                 J   ddg}t        j                  d|      }|j                  J t        j                  t        j                  t        j
                  |            |dk\  g      }|j                          t        j                  t        d      5  |j                  }d d d        t        j                  d      }d||j                  <   t        j                  |      sJ |j                  }t        j                  |j                         |      sJ y # 1 sw Y   xxY w)NrU   rV   r   r   r   z_Reading from a sparse CVXPY expression via `.value` is discouraged. Use `.value_sparse` insteadr9   )r   r   r   r   r   r   r   r<   r\   r]   r   r   r   r   ra   rb   )r   r	   r   r    X_valuer!   X_value_sparses          r"   test_sparsity_read_valuez'TestAttributes.test_sparsity_read_valueu   s     ,/KK2wwzz"++bffQi017)<

\\1
 	
 ggG	 HHV!,,{{7A&&&{{>113Q777	 	s   DD"c                 <   t        j                  dt        j                  g d      f      }t        j                  t        j
                  |            }|d   dk\  |d   dk  g}t        j                  ||      }|j                          |j                  dk(  sJ y )Nd   )r
      -   C   Y   r   r
   rT   
infeasible)	r   r   r   arrayr   sum_squaresr   r   status)r   x	objectiveconstraintsproblems        r"   test_infeasible_sparsez%TestAttributes.test_infeasible_sparse   s}    KKrxx0C'D&FGKKq 12	 trz1Q419-**Y4~~---r$   c                    t        j                  dd      }t        j                  t        j                  t        j                  |            |dk\  |dk  g      }|j                          t        j                  d       }t        j                  |j                        r|j                  j                  dk(  sJ t        j                  |j                  j                         |      sJ y )Nr   T)diagr   r
   r   dia)r   r   r   r   r   r   r   eyer^   issparser   formatr   rb   )r   r   r    r!   s       r"   test_diag_value_sparsez%TestAttributes.test_diag_value_sparse   s    KKT*zz"++bffQi017AF2CD

VVAYJ{{177#%(???{{177??,a000r$   c                    t        j                  ddddg      }t        j                  |j                  d   t        j
                  d            sJ t        j                  |j                  d   t        j                  dd            sJ t        j
                  d      t        j                  d      dz  g}t        j                  dd|      }t        j                  |j                  d   t        j
                  d            sJ t        j                  |j                  d   t        j                  d      dz        sJ d dg}t        j                  dd|      }t        j                  |j                  d   t        j                  dt        j                               sJ t        j                  |j                  d   t        j                  dd            sJ dg}t        j                  t        d	      5  t        j                  dd|      }d d d        d}t        j                  t        d	      5  t        j                  dd|      }d d d        t        j
                  d
      t        j                  d
      g}t        j                  t        d	      5  t        j                  dd|      }d d d        ddg}t        j                  t        d	      5  t        j                  dd|      }d d d        t        j                  dg}t        j                  t        d	      5  t        j                  dd|      }d d d        dt        j                   g}t        j                  t        d	      5  t        j                  dd|      }d d d        t        j                  dg}t        j                  t        d	      5  t        j                  dd|      }d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   PxY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)Nr   r   rv   r   rT   nameboundsr
   rG   z%Bounds should be a list of two items.r9   r   z]Bounds should be None, scalars, or arrays with the same dimensions as the variable/parameter.zKInvalid bounds: some upper bounds are less than corresponding lower bounds.z/np.nan is not feasible as lower or upper bound.z*-np.inf is not feasible as an upper bound.z(np.inf is not feasible as a lower bound.)r   r   r   array_equalr   r   fullr`   infr<   r=   r>   nan)r   rv   r   s      r"   test_variable_boundsz#TestAttributes.test_variable_bounds   s[   KKS!R9~~ahhqk288F+;<<<~~ahhqk27762+>??? ((6"BGGFOa$78KKS8~~ahhqk288F+;<<<~~ahhqk2776?Q+>??? KKS8~~ahhqk2776BFF7+CDDD~~ahhqk27761+=>>> ]]:-TU 	=FV<A	= ]]:-TU 	=FV<A	= ((6"BGGFO4]]-
 	=
 FV<A	= Q]].
 	=
 FV<A	= &&"]]O
 	= FV<A	= bffW]]J
 	= FV<A	= &&"]]H
 	= FV<A	= 	=U	= 	=
	= 	=
	= 	=	= 	=	= 	=	= 	=	= 	=sT   ?O0=O=#P
#P1P$ P0P<0O:=P
PP!$P-0P9<Qc                     t        j                  d      }t        j                  d      }t        j                  t        j                  |      |dk(  g      j	                          y )NTnonposbooleanr
   r   r   r   Maximizer   r   rv   ns      r"   test_scalar_boolzTestAttributes.test_scalar_bool   C    KKt$KK%


2;;q>AF8,224r$   c                     t        j                  d      }t        j                  d      }t        j                  t        j                  |      |dk(  g      j	                          y )NTr   integerr
   r   r   s      r"   test_scalar_intzTestAttributes.test_scalar_int   r   r$   c                     t        j                  dd      }t        j                  ddg      }||_        t        j
                  j                  |j                  |j                  t              d       y )Nr   Tr   Fstrict)	r   r   r   rs   r   testingassert_array_equalastypefloatr   rv   vals      r"   test_boolean_var_valuez%TestAttributes.test_boolean_var_value   sS     KK4(hhe}%


%%aggszz%/@%Nr$   c                     t        j                  dd      }t        j                  ddgt              }||_        t        j                  j                  |j
                  |j                  t              d       y )Nr   Tr   r
   dtyper   )
r   r   r   rs   r'   r   r   r   r   r   r   s      r"   test_integer_var_valuez%TestAttributes.test_integer_var_value   sS    KK4(hh1vS)


%%aggszz%/@%Nr$   N)__name__
__module____qualname__r<   markparametrizer   rs   r#   r7   r@   rD   rH   rJ   rQ   re   rg   rk   rz   r   r   r   r   r   r    r$   r"   r   r      s    [[Z8288QF+;XRXXq!f=M*NQWY_P`)ab' c'
'C
>A+H"1HE8*	.1A=F5
5
OOr$   r   c                       e Zd ZddZddZddZddZddZddZddZ	dd	Z
dd
ZddZddZddZddZddZd Zd Zy)TestMultipleAttributesNc                 ,   t        j                  dddd      }t        j                  t        j                  d      dz        }t        j
                  t        j                  d      ||k(  g      }t        |d      }t        |j                  |      d   j                  d         t        u sJ t        |j                  |      d   j                  d         t        u sJ |j                          t        j                  |j                  |      sJ y )	Nr   T)rP   	symmetricnonnegr   r   rG   r   reduce_boundsr
   )r   r   r   rs   r~   r   r   r   typeapplyrx   r   r   r   r   r   r   rv   targetr    new_probs        r"   test_multiple_attributesz/TestMultipleAttributes.test_multiple_attributes   s    KKetD$O"&&)a-(zz"++a.1;-8!$d;HNN4(+77:;zIIIHNN4(+77:;xGGG

{{177F+++r$   c                    t        j                  ddd      }t        j                  t        j                  d      dz        }t        j
                  t        j                  d      ||k(  g      }t        |d      }t        |j                  |      d   j                  d         t        u sJ t        |j                  |      d   j                  d         t        u sJ t        |j                  |      d   j                  d         t        u sJ |j                          t        j                  |j                   |      sJ y )	Nr   TrP   r   r   r   rG   r   r   r
   r   r   r   rs   r~   r   r   r   r   r   rx   r   r   r   r   r   r   r   s        r"   test_nonneg_PSDz&TestMultipleAttributes.test_nonneg_PSD  s    KKed;"&&)a-(zz"++a.1;-8!$d;HNN4(+77:;sBBBHNN4(+77:;zIIIHNN4(+77:;xGGG

{{177F+++r$   c                    t        j                  ddd      }t        j                  t        j                  d      dz        }t        j
                  t        j                  d      || k(  g      }t        |d      }t        |j                  |      d   j                  d         t        u sJ t        |j                  |      d   j                  d         t        u sJ t        |j                  |      d   j                  d         t        u sJ |j                          t        j                  |j                   |       sJ y )	Nr   T)rP   NSDr   r   rG   r   r   r
   r   r   s        r"   test_nonpos_NSDz&TestMultipleAttributes.test_nonpos_NSD  s   KKed;"&&)a-(zz"++a.1<.9!$d;HNN4(+77:;sBBBHNN4(+77:;zIIIHNN4(+77:;xGGG

{{177VG,,,r$   c                 b   t        j                  ddddg      }t        j                  t        j                  t        j                  |            g       }|j                          |j                  dk(  sJ t        j                  |j                  t        j                  d      dz        sJ y )Nr   T      r   )rP   r   r   rY   r   )
r   r   r   r   r   r   r   r   r   r`   r   rv   r    s      r"   test_integer_boundsz*TestMultipleAttributes.test_integer_bounds  sy    KKeT4)Dzz"++bffQi0"5

zzR{{177BGGENR$7888r$   c                 ~   t        j                  ddd      }t        j                  d      }t        j                  t        j
                  t        j                  |            g       }|j                          t        j                  |j                  d      sJ t        j                  |j                  |      sJ y )Nr   T)rP   r   r   r   )
r   r   r   r   r   r   r   r   r   r   )r   rv   r   r    s       r"   test_nonpos_nonneg_variablez2TestMultipleAttributes.test_nonpos_nonneg_variable%  s{    KKeD>%zz"++bffQi0"5

{{4::q))){{177F+++r$   c                     t        j                  t        d      5  t        j                  ddddg       d d d        y # 1 sw Y   y xY w)NzFA CVXPY Variable cannot have more than one of the following attributesr9   r   Tr   )rP   r   r	   r;   r?   s    r"   test_sparse_symmetric_variablez5TestMultipleAttributes.test_sparse_symmetric_variable-  sD    ]]Z
 	Q KKf?OP		Q 	Q 	Qs   A  A	c           	         t        j                  dddgt        j                  ddgddgg      dg      }t        j                  t        j
                  t        j                  |            g       }|j                          t        j                  |j                  d      sJ t        j                  |j                  t        j                  dd	gd	dgg            sJ y )
Nr   r   r   rY   rW   g      rT   )rP   r	   r   r   )
r   r   r   rs   r   r   r   r   r   r   r   s      r"   test_sparse_bounded_variablez3TestMultipleAttributes.test_sparse_bounded_variable4  s    KKeuUm "4*r4j)A BBGIzz"++bffQi0"5

{{4::r***{{177BHHtQi!T-C$DEEEr$   c                    t        j                  dddgd      }t        j                  t        j                  t        j                  |            |dk\  g      }|j                          t        j                  |j                  d      sJ t        j                  |j                  t        j                  d      dz        sJ y )	Nr   r   T)rP   r	   r   g      ir   )
r   r   r   r   r   r   r   r   r   r~   r   s      r"   test_sparse_integer_variablez3TestMultipleAttributes.test_sparse_integer_variable<  s    KKeuUmTJzz"++bffQi01d7)<

{{4::s+++{{177BFF1IN333r$   c                    t        j                  ddd      }t        j                  d       |_        t        j
                  d      }t        j                  t        j                  t        j                  |            ||k\  g      }|j                          t        j                  |j                  t        j                  d             sJ y)z)Test parameters with multiple attributes.r   T)rP   r   r   rZ   Nr   r[   r   r`   r   r   r   r   r   r   r   r   prv   r    s       r"   "test_parameter_multiple_attributesz9TestMultipleAttributes.test_parameter_multiple_attributesC  s     LLvdDA776?"KKf%zz"++bffQi016(;

{{177RWWV_$4555r$   c                    t        j                  ddddg      }t        j                  d      dz  |_        t        j
                  d      }t        j                  t        j                  t        j                  |            ||k\  g      }|j                          t        j                  |j                  t        j                  d      dz        sJ y)	z1Test parameters with bounds and other attributes.r   Tr   rT   )rP   r   r   rG   rZ   Nr   r   s       r"   $test_parameter_bounds_and_attributesz;TestMultipleAttributes.test_parameter_bounds_and_attributesV  s     LLvdAr7C''&/A%KKf%zz"++bffQi016(;

{{177BGGFOa$7888r$   c                    ddg}t        j                  d|d      }t        j                  d      j	                         }||_        t        j                  d      }t        j                  t        j                  t        j                  |            ||k\  g      }|j                          t        j                  |j                  |j                               sJ y)z3Test parameters with sparsity and other attributes.r   r   T)rP   r	   r   r   rZ   N)r   r[   r^   	eye_arraytocoora   r   r   r   r   r   r   r   r   todense)r   r	   r   p_valuerv   r    s         r"   &test_parameter_sparsity_and_attributesz=TestMultipleAttributes.test_parameter_sparsity_and_attributesi  s    F#LLvF ,,q/'') KKf%zz"++bffQi016(;

{{177GOO$5666r$   c                 :   ddg}t        j                  dd|      }|j                         sJ t        |d      sJ t	        j
                  dt              }d|d<   d	|d
<   t        j                  t        j                  t        j                  |d            |d   |d   k(  |d
   |d
   k(  g      }|j                  t         j                         |j                  }t        j                  |      sJ t	        j                  |j!                         |      sJ y)zTest sparse complex variables.r   r   TrP   complexr	   r   r   y      ?      ?r   r   y       @       r
   r
   frosolverN)r   r   
is_complexhasattrr   r   r   r   r   normr   rO   ra   r^   r   r   r   )r   r	   rv   r   r    sparse_values         r"   test_sparse_complex_variablez3TestMultipleAttributes.test_sparse_complex_variable  s    F#KKfdXF ||~~q,''' &0tt zzKK5)*tWt$dGvd|#%

 	

"++
& ~~{{<((({{<//16:::r$   c                 b   ddg}t        j                  dd|      }t        j                  ddg      }t	        j
                  ||fd      }||_        t        j                  dd      }t        j                  t        j                  t        j                  ||z
  d	                  }|j                  t         j                  
       t        j                  |j                  |j                        sJ t        j                  |j                  |j                  j                               sJ y)z&Test complex parameters with sparsity.r   r   Tr   y      ?       @y      @      rZ   )rP   r   r   r   N)r   r[   r   rs   r^   r_   ra   r   r   r   r   r   rO   r   r   r   )r   r	   r   complex_valuessparse_matrixrv   r    s          r"   test_parameter_complex_sparsityz6TestMultipleAttributes.test_parameter_complex_sparsity  s    F#LLvthG 4,/nh%?vN& KKfd3zz"++bgga!eU&;<=

"++
& {{177AGG,,,{{GGNN""$
 	
 
r$   c                    t        j                  ddd      }t        j                  ddgddgg      |_        t        j
                  d      }t        j                  t        j                  t        j                  |            ||k\  g      }|j                          t        j                  |j                  t        j                  ddgddgg            sJ y)	z.Test parameters with PSD and other attributes.r   Tr   r   r   r   rZ   N)r   r[   r   rs   r   r   r   r   r   r   r   r   s       r"   !test_parameter_psd_and_attributesz8TestMultipleAttributes.test_parameter_psd_and_attributes  s    LLv4= ((QFQF+,KKf%zz"++bffQi016(;

{{177BHHq!fq!f-=$>???r$   c           	      ~   t        j                  dddgddg      }t        j                  t        j                  t        j                  |            |t        j                  ddgdd	gg      k\  g      }|j                          |j                  t        j                  d
dgddgg      k(  j                         sJ y)z2Test boolean and integer attributes on a variable.r   r   r   r   )rP   r   r   g      ?g      gffffff@g	@r
   r   r   rF   N)
r   r   r   r   r   r   rs   r   r   allr   s      r"   test_bool_int_variablez-TestMultipleAttributes.test_bool_int_variable  s     KKfvv.>QWHXY zz"++bffQi013+PSUXzAZ8[3[2\]

 288aVaV$455::<<<r$   c                    t        j                  ddd      }|j                         dk(  sJ t        j                  ddddg	      }|j                         d
k(  sJ t        j                  ddddg      }|j                         dk(  sJ y )NrS   rv   T)r   r   z"Variable((10, 10), x, nonneg=True)yr   rT   r   zVariable((10, 10), y, bounds=([[0 0 ... 0 0]
 [0 0 ... 0 0]
 ...
 [0 0 ... 0 0]
 [0 0 ... 0 0]], [[10 10 ... 10 10]
 [10 10 ... 10 10]
 ...
 [10 10 ... 10 10]
 [10 10 ... 10 10]]))r!   r   r   )r   r	   z0Variable((10, 10), z, sparsity=[(0, 1), (0, 2)]))r   r   __repr__)r   rv   r   r!   s       r"   test_variable_reprz)TestMultipleAttributes.test_variable_repr  s    KKs48zz|CCCC KKsAr7;zz|$

 
	
 

 KKsff5EFzz|>
 	
 
r$   )returnN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r$   r"   r   r      sU    	,
,-9,QF46&9&76;6
,	@(
=
r$   r   )r/   numpyr   r<   scipy.sparsesparser^   cvxpyr   cvxpy.constraints.nonposr   cvxpy.constraints.psdr   cvxpy.constraints.zeror    cvxpy.reductions.cvx_attr2constrr   r   r   r   r$   r"   <module>r      s;    
     / % + ;hO hOTt
 t
r$   