
    binD                        d Z ddlZ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ZddlmZ ddlmZmZ ddlmZ ddlmZ ddlmZmZ ddlmZ dd	lmZ dd
lmZ ddl m!Z!  G d de      Z" G d de      Z#y)a,  
Copyright 2013 Steven Diamond

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
    N)linalg)MinimizeProblem)upper_tri_to_full)1SECOND_ARG_SHOULD_NOT_BE_EXPRESSION_ERROR_MESSAGE)Constant	Parameter)Variable)INSTALLED_MI_SOLVERS)BaseTest)partial_optimizec                      e Zd ZdZdQdZdQdZdQdZdQdZdQdZdQdZ	dQd	Z
dQd
ZdQdZdQdZdQdZdQdZdQdZdQdZdQdZdQdZdQdZdQdZdQdZdQdZdQdZdQdZdQdZd ZdQdZdQdZdQdZdQdZdQdZ dQd Z!dQd!Z"dQd"Z#dQd#Z$dQd$Z%dQd%Z&dQd&Z'dQd'Z(dQd(Z)dQd)Z*dQd*Z+dQd+Z,dQd,Z-dQd-Z.dQd.Z/dQd/Z0dQd0Z1dQd1Z2dQd2Z3dQd3Z4dQd4Z5dQd5Z6dQd6Z7 e8jr                  d7e:v d8      dQd9       Z;dQd:Z<dQd;Z=dQd<Z>dQd=Z?dQd>Z@dQd?ZAdQd@ZBdQdAZCdQdBZDdQdCZEdQdDZFdQdEZGdQdFZHdQdGZIdQdHZJdQdIZKdQdJZLdQdKZMdQdLZNdQdMZOdQdNZPdQdOZQdQdPZRy)R	TestAtomsz" Unit tests for the atoms module. Nc                     t        d      | _        t        dd      | _        t        dd      | _        t        dd      | _        t        dd      | _        t        d	d
      | _        y )Naname   xyr   r   AB   r   C)r
   r   r   r   r   r   r   selfs    Q/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/tests/test_atoms.pysetUpzTestAtoms.setUp)   sT    s#!#&!#&&s+&s+&s+    c                 4   | j                   | j                  z   }|j                         }| j                  t	        |      t	        |      u        | j                  |j                  |j                         | j                  |j                  |j                  u        | j                  |j                         |j                                |j                  | j                  | j                  g      }| j                  t	        |      t	        |      u        | j                  |j                  d   | j                  u        | j                  |j                  d   | j                  u        | j                  |j                         |j                                y)z7Test the copy function for AddExpresion class.
        argsr      N)r   r   copy
assertTruetypeassertEqualr$   assertFalseget_datar   r   r   atomr&   s      r   test_add_expr_copyzTestAtoms.test_add_expr_copy3   s    vvyy{T
d4j01 	DII.dii/0$--/:yytvvtvv.y/T
d4j01		!./		!./$--/:r!   c                 <   | j                   | j                  z   }t        j                  |      }| j	                  |j
                  t                      | j	                  |j                  t        j                         |j                         sJ | j                         sJ | j	                  t        j                  |      j                  t        j                         | j	                  t        j                  |       j                  t        j                         y)z!Test the norm_inf class.
        N)r   r   cpnorm_infr)   shapetuple	curvaturesCONVEX	is_convex
is_concaver   expr-   s      r   test_norm_infzTestAtoms.test_norm_infE   s     ffTVVm{{3UW-2~~!!###T*44ahh?dU+55qxx@r!   c                    | j                   | j                  z   }t        j                  |      }| j	                  |j
                  t                      | j	                  |j                  t        j                         | j	                  t        j                  |      j                  t        j                         | j	                  t        j                  |       j                  t        j                         y)zTest the norm1 class.
        N)
r   r   r0   norm1r)   r2   r3   r4   r5   r6   r9   s      r   
test_norm1zTestAtoms.test_norm1R   s     ffTVVmxx}UW-2$11188<4%22AHH=r!   c                    | j                  t              5 }t        j                  t        j                         dg       ddd       | j                  t        j                        dv        | j                  t              5 }t        j                  dt        j                         g       ddd       | j                  t        |j                        dv        t        j                         }t        j                         }| j                  t              5 }t        j                  ||g      dk   ddd       | j                  t        |j                        dv        y# 1 sw Y   $xY w# 1 sw Y   xY w# 1 sw Y   IxY w)z*Test that list input is rejected.
        r%   NzzThe input must be a single CVXPY Expression, not a list. Combine Expressions using atoms such as bmat, hstack, and vstack.)	assertRaises	Exceptionr0   maxr
   r'   str	exceptionnorm)r   cmr   r   s       r   test_list_inputzTestAtoms.test_list_input]   s;    y) 	'RFFBKKM1%&	'BLL)PQ 	R y) 	(RGGQ&'	(BLL)PQ 	R KKMKKMy) 	!RGGQFOq 	!BLL)PQ 	R!	' 	'	( 	(	! 	!s#   *E"*E/E;"E,/E8;Fc                     t        j                  d      }| j                  t              5 }t        j                  |d       ddd       | j                  t        j                        dv        y# 1 sw Y   0xY w)z:Test that norm exceptions are raised as expected.
        r   nucNz+Unsupported norm option nuc for non-matrix.)r0   r
   r@   rA   rE   r'   rC   rD   )r   r   rF   s      r   test_norm_exceptionszTestAtoms.test_norm_exceptionst   sc     KKNy) 	RGGAu	BLL)9; 	<	 	s   A11A:c                     t        dd      }t        j                  | j                  |      }|j	                         rJ y)zTest quad_form atom.
        r   T	symmetricN)r	   r0   	quad_formr   is_dcp)r   Pexprs      r   test_quad_formzTestAtoms.test_quad_form}   s5     f-||DFFA&;;=  =r!   c                    ddl m} dD ]  }t        |      }t        |      }||z   }ddddddd	d
 |dd      f	D ]m  }t        j                  ||      }| j                  |j                  |       |dkD  s|dk  r+| j                  |j                  t        j                         n|dk(  r+| j                  |j                  t        j                         nZ|dk(  r+| j                  |j                  t        j                         n*| j                  |j                  t        j                         |dk7  r*| j                  |j                  t        j                         |j                         }| j!                  t#        |      t#        |      u        | j                  |j$                  |j$                         | j'                  |j$                  |j$                  u        | j                  |j)                         |j)                                |j                  | j*                  g      }| j!                  t#        |      t#        |      u        | j!                  |j$                  d   | j*                  u        | j                  |j)                         |j)                                p  t        j                  d	d      j,                  dk(  sJ y)zTest the power class.
        r   )Fraction)r%   r%   r   r%   r   r   r%   r   r   g@gq=
ףp?gffffff      r#   N)	fractionsrT   r
   r0   powerr)   r2   r4   r5   r6   AFFINECONSTANTCONCAVEsignNONNEGr&   r'   r(   r$   r*   r+   r   value)	r   rT   r2   r   r   r:   pr-   r&   s	            r   
test_powerzTestAtoms.test_power   s    	'- "	CEAAa%C1ac2tXa^C CxxQ'  U3q5AE$$T^^QXX>!V$$T^^QXX>!V$$T^^QZZ@$$T^^QYY?6$$TYY9 yy{T
d4j 89   DII6  dii!78  $--/Byytvvhy/T
d4j 89		! 67  $--/B;C"	CH xxA$$)))r!   c                    t        d      }t        j                  |      }| j                  |j                  t
        j                         | j                  |j                  t
        j                         t        d      }t        j                  |      }| j                  |j                  t
        j                         | j                  |j                  t
        j                         y )NTposneg)r
   r0   xexpr)   r4   r5   r6   r`   ra   assertNotEqualr_   NONPOSr   r   r-   s      r   	test_xexpzTestAtoms.test_xexp   s    wwqz2AHH- wwqzDNNAII6AHH-r!   c                    t        j                  | j                        }| j                  |j                  t                      | j                  |j                  t        j                         | j                  |j                  t        j                         |j                         }| j                  t        |      t        |      u        | j                  |j                  |j                         | j                  |j                  |j                  u        | j                  |j!                         |j!                                |j                  | j"                  g      }| j                  t        |      t        |      u        | j                  |j                  d   | j"                  u        | j                  |j!                         |j!                                t%        j&                  t(        t*              5  t        j                  | j                  | j"                         d d d        y # 1 sw Y   y xY w)Nr#   r   match)r0   geo_meanr   r)   r2   r3   r4   r5   r_   r`   ra   r&   r'   r(   r$   r*   r+   r   pytestraises	TypeErrorr   r,   s      r   test_geo_meanzTestAtoms.test_geo_mean   sx   {{466"UW-3AHH-yy{T
d4j01 	DII.dii/0$--/:yytvvhy'T
d4j01		!./$--/: ]]G 	( KK'		( 	( 	(s   +H55H>c                 2   t        j                  | j                        }| j                  |j                  t                      | j                  |j                  t        j                         | j                  |j                  t        j                         y )N)r0   harmonic_meanr   r)   r2   r3   r4   r5   r_   r`   ra   r   r-   s     r   test_harmonic_meanzTestAtoms.test_harmonic_mean   s\    'UW-3AHH-r!   c                 R   t        j                  | j                  d      }| j                  |j                  t                      | j                  |j                  t        j                         | j                  |j                  t        j                         t        j                  | j                  d      }| j                  |j                  t                      | j                  |j                  t        j                         | j                  |j                  t        j                         t        j                  | j                  d      }| j                  |j                  t                      | j                  |j                  t        j                         | j                  |j                  t        j                         t        j                  | j                  dd      }| j                  |j                  d       t        j                  | j                  ddd	      }| j                  |j                  d
       t        j                  | j                  ddd	      }| j                  |j                  d       t        j                  | j                  d      }| j                  |j                  t                      | j                  |j                  t        j                         | j                  |j                  t        j                         t        j                  | j                  d      }| j                  |j                  t                      | j                  |j                  t        j                         | j                  |j                  t        j                         t        j                  | j                  t        j                        }| j                  |j                  t                      | j                  |j                  t        j                         | j                  |j                  t        j                         t        j                  | j                  d      }| j                  |j                  t                      | j                  |j                  t        j                         | j                  |j                  t        j                         t        j                  | j                  d      }| j                  |j                  t                      | j                  |j                  t        j                         | j                  |j                  t        j                         t        j                  | j                  d      }| j                  |j                  t                      | j                  |j                  t        j                         | j                  |j                  t        j                         t        j                  | j                  d      }| j                  |j                  t                      | j                  |j                  t        j                         | j                  |j                  t        j                         t        j                  | j                  d      }| j                  |j                  t                      | j                  |j                  t        j                         | j                  |j                  t        j                         |j!                         }| j#                  t%        |      t%        |      u        | j                  |j&                  |j&                         | j)                  |j&                  |j&                  u        | j                  |j+                         |j+                                |j!                  | j,                  g      }| j#                  t%        |      t%        |      u        | j#                  |j&                  d   | j,                  u        | j                  |j+                         |j+                                y )N      ?)rc   r%   r   r   axisr   Tr~   keepdimsr%   r   r   r%   infInf      ?ffffff?grX   gr#   )r0   pnormr   r)   r2   r3   r4   r5   r6   r`   ra   rE   r   npr   r_   r&   r'   r(   r$   r*   r+   r   )r   r-   rQ   r&   s       r   
test_pnormzTestAtoms.test_pnorm   sE   xx#&UW-2AHH-xx!$UW-2AHH-xx!$UW-2AHH-wwtvvqq)T*wwtvvqq48V,wwtvvqq48V,xx%(UW-2AHH-xx%(UW-2AHH-xx"&&)UW-2AHH-xx"%UW-3AHH-xx"%UW-3AHH-xx#&UW-3AHH-xx"%UW-3AHH-xx$'UW-3AHH- yy{T
d4j01 	DII.dii/0$--/:yytvvhy'T
d4j01		!./$--/:r!   c           	      v   ddt         j                  ddfD ]   }| j                  | j                  fD ]  }t	        j
                  ||      }| j                  |j                  t                      | j                  |j                  t        j                         | j                  |j                  t        j                         t        j                  j                  |j                   |_        | j#                  |j                   t         j$                  j                  |j                   |              # y)zq
        Matrix 1-norm, 2-norm (sigma_max), infinity-norm,
            Frobenius norm, and nuclear-norm.
        r%   r   frorI   )ordN)r   r   r   r   r0   rE   r)   r2   r3   r4   r5   r6   r`   ra   randomrandnrb   assertAlmostEqualr   )r   rc   varr-   s       r   test_matrix_normszTestAtoms.test_matrix_norms,  s    
 Qu- 	UA' UwwsA  UW5  :  AHH5IIOOSYY7	&&tzz299>>#))QR>3STU	U 	r!   c                    t        j                  t        j                  | j                        | j                        }| j                  |j                  t        j                         t        j                  t        j                  | j                         | j                        }| j                  |j                  t        j                         t        j                  t        j                  | j                        | j                        }| j                  |j                  t        j                         |j                         rJ | j                  t              5 }t        j                  | j                  | j                         d d d        | j                  t        j                        d       y # 1 sw Y   /xY w)Nz6The second argument to quad_over_lin must be a scalar.)r0   quad_over_linsquarer   r   r)   r4   r5   r6   sqrtUNKNOWNrO   r@   rA   rC   rD   r   r-   rF   s      r   test_quad_over_linzTestAtoms.test_quad_over_lin;  s    		$&& 1466:2466!2 2DFF;283;;=   y) 	-RTVVTVV,	-R\\*Q	S	- 	-s   +F77G c                    d}t        j                  t        |      5  t        j                  d       ddd       t        j                  t        |      5  t        j
                  d       ddd       y# 1 sw Y   CxY w# 1 sw Y   yxY w)z1Test arg count for max and min variants.
        z;__init__\(\) missing 1 required positional argument: 'arg2'rp   r%   N)rs   rt   ru   r0   maximumminimum)r   error_messages     r   test_elemwise_arg_countz!TestAtoms.test_elemwise_arg_countK  sm     W]]9M: 	JJqM	 ]]9M: 	JJqM	 		 		 	s   A6B6A?Bc                    t        j                  | j                  | j                        }| j	                  |j
                  t                      | j	                  |j                  t        j                         | j                  t              5 }t        j                  | j                  | j                         ddd       | j	                  t        j                        d       | j                  t              5 }t        j                  t        d      | j                         ddd       | j	                  t        |j                        d       y# 1 sw Y   xY w# 1 sw Y   ;xY w)z'Test for the matrix_frac atom.
        Nz;The second argument to matrix_frac must be a square matrix.r   z:The arguments to matrix_frac have incompatible dimensions.)r0   matrix_fracr   r   r)   r2   r3   r4   r5   r6   r@   rA   r   rC   rD   r
   r   s      r   test_matrix_fraczTestAtoms.test_matrix_fracU  s     ~~dffdff-UW-2y) 	+RNN466466*	+R\\*V	X y) 	0RNN8A;/	0R\\*U	W	+ 	+
	0 	0s   +E;*EEE(c                 R   | j                  t        j                  d      j                  t        j
                         | j                  t        j                  d      j                  t        j                         | j                  t        j                  t                     j                  t        j                         | j                  t        j                  d      j                  t        j                         | j                  t        j                  t        d      dd      j                  d       | j                  t        j                  t        d      dd      j                  d	       | j                  t        j                  t        d      d
      j                  d       | j                  t              5 }t        j                  | j                  d
       ddd       | j                  t        j                        d       | j                  t              5 }t        j                  t        d      d
      j                   ddd       | j                  t        |j                        d       | j                  t               5 }t        j                  | j                  | j                         ddd       | j                  t        |j                        t        j                  j"                         y# 1 sw Y   "xY w# 1 sw Y   xY w# 1 sw Y   `xY w)zTest max.
        r%   r   r   Tr   r%   rW   r%   r   r}   r   rY   N0axis 4 is out of bounds for array of dimension 10axis 1 is out of bounds for array of dimension 1)r)   r0   rB   r`   r5   ra   rl   r
   r   ZEROr2   r@   rA   r   rC   rD   
ValueError__EXPR_AXIS_ERROR__r   rF   s     r   test_maxzTestAtoms.test_maxf  s	    	2!((3
+00!))<0 	!dCII4P 0q4HNNPVW 0q9??F y) 	#RFF466"	#R\\*,^_y) 	.RFF8A;Q'--	.R\\*,^_z* 	#bFF466466"	#R\\*BFF,F,FG	# 	#	. 	.	# 	#s$   "L&+L+LLLL&c                    | j                  t        j                  d      j                  t        j
                         | j                  t        j                  d      j                  t        j                         | j                  t        j                  t                     j                  t        j                         | j                  t        j                  d      j                  t        j                         | j                  t        j                  t        d      d      j                  t                      | j                  t        j                  t        d      d      j                  d       | j                  t        j                  t        d      d      j                  d       | j                  t              5 }t        j                  | j                  d	       d
d
d
       | j                  t        j                         d       | j                  t              5 }t        j                  t        d      d      j                   d
d
d
       | j                  t        |j                         d       | j                  t"              5 }t        j                  | j                  | j                         d
d
d
       | j                  t        |j                         t        j                  j$                         t        j                  d      }t'        j(                  d      j+                  d      }t'        j,                  d      }t        j                  |dd      }t        j.                  t        j0                  ||z               }t        j2                  |||k(  g      }|j5                          y
# 1 sw Y   xY w# 1 sw Y   }xY w# 1 sw Y   xY w)zTest min.
        r%   r   r   r   r}   rW   r   r   rY   Nr   r      r   Tr   )r)   r0   minr`   r5   ra   rl   r
   r   r   r2   r3   r@   rA   r   rC   rD   r   r   r   arangereshapeonesMaximizesumr   solve)r   rF   XX_valcrQ   objprobs           r   test_minzTestAtoms.test_min  s    	2!((3
+00!))<0 	!4::EGD 0q9??F 0q9??F y) 	#RFF466"	#R\\*,^_y) 	.RFF8A;Q'--	.R\\*,^_z* 	#bFF466466"	#R\\*BFF,F,FG KK		!$$V,GGFOvvaa$/kk"&&*+zz#U
|,

%	# 	#	. 	.	# 	#s$   "O,+O+OOOO)c           
         | j                  t        j                  dd      j                  t        j
                         | j                  t        j                  dt                     j                  t        j
                         | j                  t        j                  dd      j                  t        j
                         | j                  t        j                  dd      j                  t        j
                         | j                  t        j                  t               d      j                  t        j
                         | j                  t        j                  t               t                     j                  t        j                         | j                  t        j                  t               d      j                  t        j                         | j                  t        j                  dd      j                  t        j                         | j                  t        j                  dd      j                  t        j                         | j                  t        j                  dd      j                  t        j                         | j                  t        j                  dt               ddt               d      j                  t        j
                         | j                  t        j                  dt        d            j                  t        j
                         | j                  t        j                  dt        d            j                  d       y Nr%   r   r   r   rX   r   )r)   r0   r   r`   r5   ra   r
   r   r   rl   r2   r   s    r   test_maximum_signzTestAtoms.test_maximum_sign  s
   Aq)..9Axz277BAr*//:Aq)..9HJ277BHJ
;@@!))LHJ388!))DAq)..7Ar*//8B+00!((; 	B
Ar8:qINN	# 	Ax{388	#Ax{399	r!   c           
         | j                  t        j                  dd      j                  t        j
                         | j                  t        j                  dt                     j                  t        j                         | j                  t        j                  dd      j                  t        j                         | j                  t        j                  dd      j                  t        j                         | j                  t        j                  t               d      j                  t        j                         | j                  t        j                  t               t                     j                  t        j                         | j                  t        j                  t               d      j                  t        j                         | j                  t        j                  dd      j                  t        j                         | j                  t        j                  dd      j                  t        j                         | j                  t        j                  dd      j                  t        j                         | j                  t        j                  dt               ddt               d      j                  t        j                         | j                  t        j                  dt        d            j                  t        j                         | j                  t        j                  dt        d            j                  d       y r   )r)   r0   r   r`   r5   ra   r
   r   rl   r   r2   r   s    r   test_minimum_signzTestAtoms.test_minimum_sign  s
   Aq)..9Axz277CAr*//:Aq)..7HJ277BHJ
;@@!))LHJ388!((CAq)..7Ar*//:B+00!((; 	B
Ar8:qINN	# 	B499	#B4::	r!   c           
      b	   | j                  t        j                  d      j                  t        j
                         | j                  t        j                  t        ddg            j                  t        j                         | j                  t        j                  t        ddg            j                  t        j                         | j                  t        j                  t        d            j                  t        j                         | j                  t        j                  t        d            j                  t                      | j                  t        j                  t        d            j                  t        j                         | j                  t        j                  t        d      d      j                  d       t        j                  ddgg      }| j                  t        j                  |t        j                   t        d            z        j                  t        j                         | j                  t        j                  t        d      d	      j                  t                      | j                  t        j                  t        d
      dd      j                  d       | j                  t        j                  t        d
      dd      j                  d       | j                  t        j                  t        d
      d	      j                  d       | j#                  t$              5 }t        j                  | j&                  d	       ddd       | j                  t)        j*                        d       | j#                  t$              5 }t        j                  t        d      d	      j                   ddd       | j                  t)        |j*                        d       t-        j.                  d      }| j                  t        j                  |      j0                  d       t-        j.                  d      }| j3                  t        j                  |d	      j0                  g d       y# 1 sw Y   )xY w# 1 sw Y   xY w)zTest the sum atom.
        r%   rX   r   r   T)r   rU   r   r}   rW   r   r   Fr   r   rY   Nr   r   r   )r%   r%   r%   )r)   r0   r   r`   r5   ra   r   r   r4   r^   r
   r2   r3   r]   r   arrayr   r@   rA   r   rC   rD   sp	eye_arrayrb   assertItemsAlmostEqual)r   matrF   r   s       r   test_sumzTestAtoms.test_sum  s    	2!R 1277C!R 12<<ajjI,11199=,22EG<,66A 04@FFOhhBy!bii&< <=GGS 	!4::EGD 0q4HNNPVW 0q5IOOQUV 0q9??F y) 	#RFF466"	#R\\*J	Ly) 	.RFF8A;Q'--	.R\\*,^_LLO!,LLO##BFF11$5$;$;YG	# 	#	. 	.s   "R3+R%R"%R.c                    | j                  t        j                  ddg| j                        j                  t
        j                         | j                  t        j                  ddg| j                        j                  t
        j                         | j                  t        j                  ddg| j                        j                  d       t        dd      }t        dd      }| j                  t        j                  ||      j                  t
        j                         | j                  t        j                  ||      j                  t
        j                         | j                  t        j                  ||      j                  t
        j                         | j                  t        j                  |t        j                  | j                              j                  t
        j                         | j                  t        j                  ddgd      j                  d       | j                  t        j                  d| j                        j                  | j                  j                         | j                  t        j                  | j                  ddg      j                  t
        j                         | j                  t        j                  | j                  ddg      j                  t
        j                         | j                  t        j                  | j                  ddg      j                  d       y)	z Test the multiply atom.
        r%   rX   r   r   TnonnegnonposN)r)   r0   multiplyr   r`   r5   r   r4   r]   r2   r	   ra   rl   r   r_   r   )r   	pos_param	neg_params      r   test_multiplyzTestAtoms.test_multiply  s    	aWdff5::AIIFaWdff5??JaWdff5;;TBa-	a-	Y	:??JY	:??JY	:??JY		$&&0ABLLaiiX 	aWa066=Q/55tvv||DTVVaW5::AIIFTVVaW5??JTVVaW5;;TBr!   c                    t        j                  | j                  | j                  | j                  g      }| j	                  |j                         d       | j	                  |j                  d       t        j                  | j                  | j                  | j                  g      }| j	                  |j                         d       | j	                  |j                  d       g }t        | j                  j                  d         D ]   }|j                  | j                  |          " t        j                  |      }| j	                  |j                  d       | j                  t              5 }t        j                  | j                  dg       d d d        | j	                  t        j                        d       | j                  t              5 }t        j                  | j                  t!        d	      g       d d d        | j	                  t        |j                        d       | j                  t"              5 }t        j                          d d d        t        j                  d
t!        d      g      }| j	                  |j                  d       y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   _xY w)NzVstack(x, y, x)r   zVstack(A, C, B))   r   r   r   r%   z>All the input dimensions except for axis 0 must match exactly.r   r   r   )r0   vstackr   r   r)   r   r2   r   r   r   rangeappendr@   rA   rC   rD   r
   ru   )r   r-   entriesirF   rQ   s         r   test_vstackzTestAtoms.test_vstack  s   yy$&&$&&$&&12&78V,yy$&&$&&$&&12&78V,tvv||A' 	&ANN466!9%	&yy!V, y) 	#RIItvvqk"	#R\\*Y	[ y) 	-RIItvvx{+,	-R\\*Y	[ y) 	RIIK	 yy!Xd^,-V,	# 	#
	- 	-
	 	s$   2"J+J*J6J'*J36J?c                    t        j                  | j                  | j                  gd      }| j	                  |j                         d       | j	                  |j                  d       | j                  t              5  t        j                  | j                  | j                  gd      }d d d        t        j                  | j                  | j                  gd       }| j	                  |j                  d       t        j                  | j                  | j                  gd      }| j	                  |j                  d       | j                  t              5  t        j                  | j                  | j                  gd      }d d d        t        j                  | j                  | j                  gd      }| j	                  |j                  d       t        j                  | j                  | j                  gd      }| j	                  |j                  d	       t        j                  | j                  | j                  gd       }| j	                  |j                  d
       | j                  t              5  t        j                  | j                  | j                  gd       d d d        | j                  t              5  t        j                  | j                  | j                  gd       d d d        | j                  t              5  t        j                  | j                  | j                  gd       d d d        y # 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   {xY w# 1 sw Y   y xY w)Nr   r}   zConcatenate(x, y, 0)rY   r%   )
   rZ   r   )r   rY   )rY   r   )   r   )r0   concatenater   r   r)   r   r2   r@   r   r   r   r   r   ry   s     r   test_concatenatezTestAtoms.test_concatenate3  s   ~~tvvtvv.Q7&<=T*z* 	<>>466466"2;D	<
 ~~tvvtvv.T:U+~~tvvtvv.Q7V,z* 	<>>466466"2;D	< ~~tvvtvv.Q7V,~~tvvtvv.Q7V,~~tvvtvv.T:T*z* 	5NNDFFDFF+!4	5 z* 	5NNDFFDFF+!4	5 z* 	5NNDFFDFF+!4	5 	5A	< 	<	< 	<	5 	5	5 	5	5 	5s<   ?.M.M.M).M5.NMM&)M25M>N
c                    t        j                  | j                  dd      }| j                  |j                  t
        j                         | j                  |j                  t
        j                         | j                  |j                  d       t        j                  |dd      }| j                  |j                  d       t        j                  t        j                  | j                        dd      }| j                  |j                  t
        j                         | j                  |j                  t
        j                         | j                  |j                  d       | j                  t              5 }t        j                  | j                   dd       ddd       | j                  t#        j$                        d       t'        j(                  d	      }t'        j                  |d
d      }t        j                  |d
d      }| j+                  ||j,                         t        j.                  d
      }t        j0                  t        j2                  d      ||k(  g      }|j5                  t         j6                         | j+                  ||j,                         t'        j                  |d	d      }t        j                  |d	d      }	| j+                  ||	j,                         t        j.                  d	      }
t        j0                  t        j2                  d      |
|	k(  g      }|j5                  t         j6                         | j+                  ||
j,                         t'        j8                  g dg dg dg dg      }|j                  dd      }t        j.                  |j                        }t        j                  |dd      }t        j0                  t        j2                  d      ||k(  g      }|j5                  t         j6                         | j+                  ||j,                         | j+                  ||j,                         y# 1 sw Y   xY w)z Test the reshape class.
        rY   r%   Forderr   r   rZ   rY   Nz"Invalid reshape dimensions (5, 4).r   r   r   r   solver)r   r%   r   )r   rY   rZ   )r   r   r   )	   r      )r   r   )r0   r   r   r)   r`   r5   r   r4   r]   r2   r   r   ra   r6   r@   rA   r   rC   rD   r   r   r   rb   r
   r   r   r   SCSr   )r   rQ   rF   r   A_npA_cpr   r   a_npa_cpr   b
b_reshaped
X_reshapeds                 r   test_reshapezTestAtoms.test_reshape\  s    zz$&&&4AII.2V,zz$c2V,zz"))DFF+V3?AHH-2V,y) 	2RJJtvvvS1	2R\\*=	? IIbMzz!V3/zz!V3/##D$**5KKzz"++a.19+6

"&&
!##D!''2zz$#.zz$#.##D$**5KKOzz"++a.19+6

"&&
!##D!''2 HH	
  YYvSY1
KK ZZ65
zz"++a.:+C*DE

"&&
!##J
0@0@A##Aqww/O	2 	2s   #Q++Q5c                    t        j                  d      }t        j                  d      }g d}g d}t	        ||      D ]k  \  }}t        j
                  ||d      }| j                  |j                  |       t        j
                  ||      }| j                  |j                  |       m t        j                  t        d      5  t        j
                  |dd       d	d	d	       t        j                  t        d
      5  t        j
                  |dd       d	d	d	       t        j                  t        d      5  t        j
                  |dd       d	d	d	       t        j                  t        d      5  t        j
                  |dd       d	d	d	       t        j                  g dg dg      }	t        j
                  |	dd      }
t        j                  |
j                  |	j                  dd            sJ t        j
                  |	dd      }
t        j                  |
j                  |	j                  dd            sJ y	# 1 sw Y   pxY w# 1 sw Y   AxY w# 1 sw Y   xY w# 1 sw Y   xY w)z>
        Test the reshape class with -1 in the shape.
        rW   ))rX   r%   )r%   rX   )rX   r   rX   )rX   ))r   r%   )r%   r   r   r   r   r   r   zCannot reshape expressionrp   )r   rX   NzOnly one)rX   rX   zInvalid reshape dimensions)rX   r   z'Specified dimension must be nonnegative)rX   r   r%   r   r   rY   rZ   r   rX   r   )r0   r
   r   r   zipr   r)   r2   rs   rt   r   AssertionErrorr   allcloserb   )r   rQ   
numpy_exprshapesexpected_shapesr2   expected_shapeexpr_reshapednumpy_expr_reshapedr   
A_reshapeds              r   test_reshape_negative_onez#TestAtoms.test_reshape_negative_one  s   
 {{6"WWV_
7>%(%A 	H!E>JJtU#>M]00.A"$**Z"?066G	H ]]:-HI 	1JJtWC0	1 ]]>< 	2JJtXS1	2 ]]:-IJ 	1JJtWC0	1 ]]>1Z[ 	2JJtXS1	2 HHi+,ZZ2S1
{{:++QYYrY-EFFFZZ2S1
{{:++QYYrY-EFFF!	1 	1	2 	2	1 	1	2 	2s0   II I)<I6II&)I36I?c                    t        j                  | j                  d      }| j                  |j                  t
        j                         | j                  |j                  t
        j                         | j                  |j                  d       t        j                  | j                  d      }| j                  |j                  d       t        j                  t        j                  | j                        d      }| j                  |j                  t
        j                         | j                  |j                  t
        j                         | j                  |j                  d       y)zTest the vec atom.
        r   r   r   r   r   N)r0   vecr   r)   r`   r5   r   r4   r]   r2   r   r   r   ra   r6   r   rQ   s     r   test_veczTestAtoms.test_vec  s     vvdffC(AII.2T*vvdffC(T*vvbii's3AHH-2T*r!   c                 `   t        j                  | j                        }| j                  |j                  t
        j                         | j                  |j                  t
        j                         | j                  |j                  d       t        j                  | j                        }| j                  |j                  t
        j                         | j                  |j                  t
        j                         | j                  |j                  d       t        j                  | j                  j                        }| j                  |j                  t
        j                         | j                  |j                  t
        j                         | j                  |j                  d       t        j                  ddgddgg      }t        j                  |      }| j                  |j                  t
        j                         | j                  |j                  t
        j                         | j                  |j                  d       | j!                  t"              5 }t        j                  | j$                         ddd       | j                  t'        j(                        d       t        j                  ddg      }t        j                  |      }| j+                  |j-                                t        j                  |       }| j+                  |j/                                t        j                  t        j                  ddg            }| j1                  |j-                                | j1                  |j/                                y# 1 sw Y   xY w)	zTest the diag atom.
        r   r   r%   rX   Nz9Argument to diag must be a 1-d array or 2-d square array.      ?       @)r0   diagr   r)   r`   r5   r   r4   r]   r2   r   Tr   r   ra   r^   r@   rA   r   rC   rD   r'   is_psdis_nsdr*   )r   rQ   
psd_matrixrF   ws        r   	test_diagzTestAtoms.test_diag  s]    wwtvvAII.2V,wwtvvAII.2T*wwtvvxx AII.2V,XX2wQ01
wwz"AHH-4T*y) 	RGGDFFO	R\\*T	V HHc3Z wwqz&wwr{&wwrxxB()''	 	s   - N##N-c                 "   t        j                  g dg dg dg      }t        j                  g d      }g d}|D ]  }t        j                  ||      }t        j                  ||      }t        j                  ||      }t        j                  ||      }| j	                  |j
                  |       | j	                  |j
                  |        t        j                  t        d      d      }	| j                  |	j                  d       y	)
z(Test matrix to vector on scalar matricesr   r   )r   r   r   )r   r%   rX   r   )krZ   r%   $   N)	r   r   r0   r  r   rb   r
   r)   size)
r   test_matrixtest_vectoroffsetsoffsetr   r   r   r   r   s
             r   test_diag_offsetzTestAtoms.test_diag_offset  s    hh	9i@Ahhy) 	:F77;&1D77;&1D77;&1D77;&1D''

D9''

D9	: GGHQK#$r!   c                    t        j                  | j                        }| j                  |j                  t
        j                         | j                  |j                  t
        j                         | j                  |j                  t                      | j                  t              5 }t        j                  | j                         ddd       | j                  t        j                        d       y# 1 sw Y   /xY w)zTest the trace atom.
        Nz-Argument to trace must be a 2-d square array.)r0   tracer   r)   r`   r5   r   r4   r]   r2   r3   r@   rA   r   rC   rD   )r   rQ   rF   s      r   
test_tracezTestAtoms.test_trace  s     xxAII.2UW-y) 	RHHTVV	R\\*H	J	 	s   - C;;Dc                     t        j                  dd      }t        j                  dd      }t        j                  |      }t        j                  |      }|j                         sJ |j	                         sJ y)z/Test sign of trace for psd/nsd inputs.
        r   T)PSD)NSDN)r0   r
   r  	is_nonneg	is_nonpos)r   X_psdX_nsd	psd_trace	nsd_traces        r   test_trace_sign_psdzTestAtoms.test_trace_sign_psd  sb     F-F-HHUO	HHUO	""$$$""$$$r!   c                    t        j                  d      }| j                  |j                  t        j
                         | j                  |j                  t        j                         | j                  |j                  t                      t        j                  d      }| j                  |j                  t        j                         y)zTest the log1p atom.
        r%   g      N)r0   log1pr)   r`   r5   ra   r4   r^   r2   r3   rl   r  s     r   
test_log1pzTestAtoms.test_log1p  s|     xx{AHH-4UW-xx~AHH-r!   c                     | j                  t              5 }t        j                  | j                         d d d        | j                  t        j                        d       y # 1 sw Y   /xY w)Nz1Argument to upper_tri must be a 2-d square array.)r@   rA   r0   	upper_trir   r)   rC   rD   r   s     r   test_upper_trizTestAtoms.test_upper_tri"  sT    y) 	!RLL 	!R\\*L	N	! 	!s    A$$A-c           	         t        d      }t        j                  |      }t        j                  g d      |_        |j
                  }t        j                  ddgddgg      }t        j                  ||      sJ t        d      }t        j                  d	g      |_        t        j                  |d
      }|j
                  }t        j                  dd	gddgg      }t        j                  ||      sJ t        j                  g dg dg dg dg      }t        j                  g d      }t        j                  |d
      j
                  }	t        j                  |	|      sJ t        j                  t        d      5  t        j                  t        j                   d	             d d d        t        j                  t        d      5  t        j                  t        j                   d	      d
       d d d        t        j                  t        d      5  t        j                  t        j                   d             d d d        t        j                  t        j                  t        j                  d            j
                  t        j                  t        j                  d      j                  dd            j
                        sJ t        j                  t        j                  dd
      j
                  t        j                  ddgddgg            sJ y # 1 sw Y   xY w# 1 sw Y   =xY w# 1 sw Y   xY w)Nr   )r2   r   r%   r   r   r   r   rY   T)strict)r   r         )r   r         )r   r   r      )r   r   r   r   )r   r0  r1  r2  r3  r4  zmust be a triangular numberrp   zmust be a vectorr   r   )r
   r0   vec_to_upper_trir   r   rb   r   rs   rt   r   r   r   )
r   r   r   actualexpectr   YA_expectr   A_actuals
             r   test_vec_to_upper_trizTestAtoms.test_vec_to_upper_tri(  s   4 "((9%Aq6Aq6*+{{66***4 ((A3-$/Aq6Aq6*+{{66***88_+*)+ , HH-.&&q6<<{{8X...]]:-JK 	81 67	8 ]]:-JK 	E1 6tD	E ]]:-?@ 	;& 9:	; {{		!-33		! 4 4Q :;AA
 	
 
 {{2..q>DDbhhQRTUPVYZ\]X^O_F`aaa!	8 	8	E 	E	; 	;s$   *L-,L:"*M-L7:MMc                     t        dd      D ]e  }t        |      }t        j                  ||dz   z  dz        }||z  j	                  ||fd      }t        j
                  ||j                        reJ  y )Nr   r   r%   r   r   r   )r   r   r   r   r   r   r  )r   nr   vMs        r   test_upper_tri_to_fullz TestAtoms.test_upper_tri_to_fullO  sm    q! 	'A!!$A		!qs)q.)AQAc2A;;q!##&&&		'r!   c           
          t        j                  | j                  d       | j                  t              5 }t        j                  | j                  d       d d d        | j                  t        j                        d       | j                  t              5 }t        j                  | j                  ddg       d d d        | j                  t        |j                        d       t        d      }t        j                  | j                  |       d|_	        | j                  t        j                  d|      j                  d       t        d      }| j                  t              5 }t        j                  | j                  |       d d d        | j                  t        |j                        d       t        j                  | j                  d      }|j                         }| j                  t        |      t        |      u        | j                  |j                  |j                         | j                  |j                  |j                  u        | j                  |j!                         d	   j                  |j!                         d	   j                         |j                  | j"                  g
      }| j                  t        |      t        |      u        | j                  |j                  d	   | j"                  u        | j                  |j!                         d	   j                  |j!                         d	   j                         t        d      }t        j                  d|      }d|_	        | j                  |j                  d       d|_	        | j                  |j                  d       d|_	        | j                  |j                  d       d|_	        | j                  |j                  d       t        j                  dd|z  dz         }d|_	        | j                  |j                  d       d|_	        | j                  |j                  d       d|_	        | j                  |j                  d       t        j$                         }t        j                  d      }t        j&                  t        j(                  |dz  t        j                  d|z  dz
  d|z  dz         z         |dk\  g      }d|_	        |j+                         }| j                  |d       | j                  |j                  d       d|_	        |j+                         }| j                  |d       | j                  |j                  d       d|_	        |j+                         }| j                  |d       | j                  |j                  d       y # 1 sw Y   xY w# 1 sw Y   ,xY w# 1 sw Y   gxY w)Nr%   rX   z6M must be a non-negative scalar constant or Parameter.Tr   r   r   r   r   r#   r	  r   g      ?g      ?g      ?g     ?rZ   g333333?g333333?      @r|           gq=
ףp?)r0   huberr   r@   rA   r)   rC   rD   r	   rb   r   r&   r'   r(   r$   r*   r+   r   r
   r   r   r   )	r   rF   r?  r-   r&   rQ   r   problemresults	            r   
test_huberzTestAtoms.test_huberV  sz   
y) 	!RHHTVVR 	!R\\*Q	S y) 	%RHHTVVaV$	%R\\*Q	S T"
rxx1~33Q7T"y) 	 RHHTVVQ	 R\\*Q	S xx"yy{T
d4j01 	DII.dii/0+114==?13E3K3KLyytvvhy'T
d4j01		!./+114==?13E3K3KL T"xxQtzz3/tzz40tzz62tzz3/ xxTAX_-tzz40tzz84tzz40KKMLL%**R[[A1QA0M)MNQRVYQYPZ[vs+qww,vv.qww,vs+qww,[	! 	!
	% 	%	  	 s#   !W#W&-!W3W#&W03W=c                 ,
   | j                  t              5 }t        j                  | j                  d       ddd       | j                  t        j                        d       | j                  t              5 }t        j                  | j                  d       ddd       | j                  t        |j                        d       | j                  t              5 }t        j                  ddgdd	ggd      j                   ddd       | j                  t        |j                        d
       t        j                  | j                  d      }|j                         }| j                  t        |      t        |      u        | j                  |j                  |j                         | j                  |j                  |j                  u        | j                  |j!                         |j!                                |j                  | j"                  g      }| j                  t        |      t        |      u        | j                  |j                  d   | j"                  u        | j                  |j!                         |j!                                t        j                  t%        d      d      }|j                         }| j                  t        |      t        |      u        t'        j(                  ddgddgg      }t        j                  |d      }d}| j+                  |j                  |       t        j,                  |d      }d}| j+                  |j                  |       t        j                  | j                  d      }|j/                         sJ t&        j0                  j3                  d      }t5        |      }dD ]^  }	t        j                  ||	      }t'        j6                  |       d|	 }
| j+                  |j                  ||
   j9                                ` t'        j(                  g d      }t5        |      }t        j                  |d      }d}| j+                  |j                  |       t        j                  |d      }d}| j+                  |j                  |       t        j                  |d      }d}| j+                  |j                  |       y# 1 sw Y   xY w# 1 sw Y   dxY w# 1 sw Y   xY w)z5Test the sum_largest atom and related atoms.
        rX   N*Second argument must be a positive number.g333333@z'First argument must be a square matrix.r%   r   r   rY   )Input matrix was not Hermitian/symmetric.r#   r   r   r
  rC  r	  r|   rB  ?g?i'  )rZ   2   d      i  i  g      @      @g       @r	        @g     0@333333?gffffff(@g333333?)r@   rA   r0   sum_largestr   r)   rC   rD   lambda_sum_largestr   rb   r&   r'   r(   r$   r*   r+   r   r
   r   r   r   lambda_sum_smallestis_pwlr   r   r   argsortr   )r   rF   r-   r&   r   rQ   expectedr>  r   r   prev_idxs              r   test_sum_largestzTestAtoms.test_sum_largest  s    y) 	'RNN4662&	'R\\*E	G y) 	/R!!$&&#.	/R\\*B	D z* 	=b!!Aq6Aq6"2A6<<	=R\\*D	F ~~dffa(yy{T
d4j01 	DII.dii/0$--/:yytvvhy'T
d4j01		!./$--/:$$Xf%5q9yy{T
d4j01 HHsCj3*-.$$Q,"tzz84 %%a-"tzz84 ~~dffa({{}} IIOOE"QK- 	BA>>!Q'Dzz1"~bq)H""4::q{/@A	B HH./QK~~a%(tzz84 ~~a%"tzz84 ~~a%tzz84S	' 	'
	/ 	/
	= 	=s#   !S/9!S<'T	/S9<T	Tc                 Z   | j                  t              5 }t        j                  | j                  d       ddd       | j                  t        j                        d       t        j                  | j                  d      }|j                         sJ t        j                  g d      }t        |      }t        j                  |d      }d}| j                  |j                  |       t        j                  |d      }d	}| j                  |j                  |       y# 1 sw Y   xY w)
z6Test the sum_smallest atom and related atoms.
        rX   NrI  r   rO  rB  g      @rR  g@)r@   rA   r0   sum_smallestr   r)   rC   rD   rV  r   r   r   r   rb   )r   rF   r-   r>  r   rQ   rX  s          r   test_sum_smallestzTestAtoms.test_sum_smallest  s     y) 	(ROODFFB'	(R\\*E	G tvvq){{}} HH./QKq#&(tzz84 q#&"tzz84)	( 	(s   !D!!D*c           	         t         j                  j                  d       d}t         j                  j                  |      }g d}|D ]  }t	        j
                  ||      }|j                  }t	        j                         }|dd|z
  |z  z  t	        j                  t	        j                  ||z
              z  z   }t	        j                  t	        j                  |            }	|	j                         }
| j                  ||
        d}d}d}t         j                  j                  |      }t         j                  j                  ||      }d}t	        j                  |      }t	        j                  ||z        }|dk\  t	        j
                  ||z  |      |k  g}t	        j                  ||      }|j                         }| j                  |j                  t        j                          | j#                  t        j$                  |             t	        j
                  ||j                  z  |      j                  }| j#                  ||k         y)	z3Test the cvar atom and its use in a linear program.r%   rM  )皙?r   ?gffffff?gGz?rZ   r`  r	  r   N)r   r   seedr   r0   cvarrb   r
   r   rg   r   r   r   r   r)   statusOPTIMALr'   isfinite)r   mr   betasbeta	cvar_atom
cvar_valuealpha	objectiveprob_altcvar_alt_valuer=  r   r   kappaconstraintsr   optimal_values                     r   	test_cvarzTestAtoms.test_cvar  s    			q IIOOA+ 	?D4(I"J KKMEAdFA:q5y8I1J JJIzz"++i"89H%^^-N "":~>	?   IIOOAIIOOAq! KKN KKA&	 FGGAE4 E)
 zz)[1

 	bjj1 	M23 WWQ[$/55

e+,r!   c                    d}t        |      }|ddddf   }|j                         }| j                  t        |      t        |      u        | j	                  |j
                  |j
                         | j                  |j
                  |j
                  u        | j	                  |j                         |j                                t        d      }|j                  |g      }| j                  t        |      t        |      u        | j                  |j
                  d   |u        | j	                  |j                         |j                                y)z*Test the copy function for index.
        r   r   r   r%   )rY   rZ   r#   N)r
   r&   r'   r(   r)   r$   r*   r+   )r   r2   r   r-   r&   r   s         r   
test_indexzTestAtoms.test_indexM  s    UO1ac{yy{T
d4j01 	DII.dii/0$--/:Vyyqcy"T
d4j01		!)*$--/:r!   c           
      Z   t        j                  d      }t        j                  t        j                  ||g      t        j                  t        j                  d      t        j
                  ddgg      j                  g      g      }| j                  |j                  d       t        j                  t        j                  ||g      t        j                  t        j                  d      t        j
                  ddgg      j                  g      g      }| j                  ||       y)zTest the bmat atom.
        rV   r   r%   r   r   N)
r   r   r   hstackzerosr   r  r)   r2   r   )r   v_nprQ   consts       r   	test_bmatzTestAtoms.test_bmatb  s     wwvyy"))T4L1))RXXf%5%'XX1vh%7%9%9%; <= > 	V,		299dD\299bhhv&6&(hhAx&8&:&:&< => ? 	##D%0r!   c                    t        j                  d      }t        dd      }t        j                  ||      }|j                         sJ | j                  |j                  d       t        dd      }t        j                  ||      }|j                         sJ | j                  t              5 }t        j                  | j                  d       ddd       | j                  t        j                        d	       | j                  t              5 }t        j                  d
dgd
dgg| j                         ddd       | j                  t        |j                        d       t        j                         }t        j                         }t        j                  t        j                   t        j                  ||            d
|k  |dk  g      }d|_        |j%                         }| j'                  |d       | j'                  |j"                  d       t        j                  t        j                   t        j                  ||            d
|k  |dk  g      }t)        j*                  t        j,                        5  |j%                  d       ddd       y# 1 sw Y   xY w# 1 sw Y   rxY w# 1 sw Y   yxY w)zTest the conv atom.
        rV   r   Tr   r   r   rX   N,The first argument to conv must be constant.r   r%   z1The arguments to conv must resolve to a 1-d array      enforce_dpp)r   r   r	   r0   convr!  r)   r2   r"  r@   rA   r   rC   rD   r
   r   r   rb   r   r   rs   rt   DPPError	r   r   r   rQ   rF   r   rc   rE  rF  s	            r   	test_convzTestAtoms.test_convo  s    GGFOa%wwq!}~~V,a%wwq!}~~y) 	 RGGDFFB	 R\\*G	Iy) 	.RGGaVaV$dff-	.R\\*L	N
 KKMLLN**R[[A7!q&!q&9IJvr*qww***R[[A7!q&!q&9IJ]]2;;' 	,MMdM+	, 	,+	  	 	. 	."	, 	,s$   1!J'J+J8J(+J58Kc                    dD ]  }t        j                  d      }t        j                  ||      }t        j                  d      j                  d      }t        j                  ||      }t        j                  t        j                  t        j                  |            ||k(  g      }|j                          t        j                  |j                  |      rJ  y )Nr   r%   rY   r   r}   r0  )r0   r
   cumsumr   r   r   r   r   r   r   r   rb   r   r~   r   rQ   x_valtargetr   s          r   test_cumsumzTestAtoms.test_cumsum  s     		3DF#A99QT*DIIbM))&1EYYu40F::bkk"&&,7!u*FDJJL;;tzz6222		3r!   c                    dD ]  }t        j                  dd      }t        j                  ||      }t        j                  d      dz   j                  d      }t        j                  ||      }t        j                  t        j                  t        j                  |            ||k(  g      }|j                  d       t        j                  |j                  |      rJ  y )	Nr  r  Trf   r}   r0  r%   )gp)r0   r
   cumprodr   r   r   r   r   r   r   r   rb   r  s          r   test_cumprodzTestAtoms.test_cumprod  s     
	3DF-A::ad+DYYr]1_--f5EZZD1F::bkk"&&,7!u*FDJJ$J;;tzz6222
	3r!   c                 ,   t        j                  d      }t        dd      }t        j                  ||      }|j                         sJ | j                  |j                  d       t        dd      }t        j                  ||      }|j                         sJ | j                  t              5 }t        j                  | j                  | j                         ddd       | j                  t        j                        d       y# 1 sw Y   /xY w)	zTest the kron atom.
        r   r   Tr   )r   r   r   Nz/At least one argument to kron must be constant.)r   r   r	   r0   kronr!  r)   r2   r"  r@   rA   r   rC   rD   )r   r   r   rQ   rF   s        r   test_kron_exprzTestAtoms.test_kron_expr  s     GGFOfT*wwq!}~~V,fT*wwq!}~~y) 	$RGGDFFDFF#	$R\\*J	L	$ 	$s   1+D

Dc                 j   t        j                  d      }t        dd      }t        j                  ||      }|j                         sJ | j                  |j                  d       t        dd      }t        j                  ||      }|j                         sJ | j                  t              5 }t        j                  | j                  d       ddd       | j                  t        j                        d	       t        j                  t         d
      5  t        j                  ddgddgg| j                         ddd       t        j"                         }t        j                         }t        j$                  t        j&                  t        j                  ||            d|k  |dk  g      }d|_        |j+                  t        j,                        }| j/                  |d       | j/                  |j(                  d       t        j$                  t        j&                  t        j                  ||            d|k  |dk  g      }t        j                  t        j0                        5  |j+                  d       ddd       y# 1 sw Y   xY w# 1 sw Y   ]xY w# 1 sw Y   yxY w)z Test the convolve atom.
        r   r   Tr   r   r   rX   Nr|  zscalar or 1Drp   r   r%   r}  )canon_backendr~  )r   r   r	   r0   convolver!  r)   r2   r"  r@   rA   r   rC   rD   rs   rt   r   r
   r   r   rb   r   CPP_CANON_BACKENDr   r  r  s	            r   test_convolvezTestAtoms.test_convolve  s    GGDMa%{{1a ~~T*a%{{1a ~~y) 	$RKK#	$R\\*G	I]]:^< 	2KK!Q!Q($&&1	2
 KKMLLN**R[[Q):;a1fa1f=MNR-A-ABvr*qww***R[[Q):;a1fa1f=MN]]2;;' 	,MMdM+	, 	,'	$ 	$	2 	2	, 	,s$   1!J'J3J)JJ&)J2c                    t        j                  g dg dg      }t        j                  |      }|j	                         sJ |j
                  dk(  sJ t        j                  |j                  d      sJ t        j                  |d      }|j	                         sJ |j
                  dk(  sJ t        j                  |j                  t        j                  g d            sJ t        j                  |d	      }|j	                         sJ |j
                  d
k(   t        j                  |j                  t        j                  ddg            sJ t        j                  |dd      }|j	                         sJ |j
                  dk(  sJ t        j                  |j                  t        j                  g dg            sJ t        j                  |d	d      }|j	                         sJ |j
                  dk(  sJ t        j                  |j                  t        j                  dgdgg            sJ t        j                  d      }t        j                  |      }|j                  dk(  sJ y)zTest the ptp atom.
        )      $@g      $rP  )rQ  rC  g       g      4@r   r}   r   )rY   r   g      @r%   r   g      @Tr   r   r   r6   N)r   r   r0   ptpr!  r2   iscloserb   r   r
   r4   )r   r   rQ   r   s       r   test_ptpzTestAtoms.test_ptp  s    HH&78vvay~~zzRzz$**c***vvaa ~~zzT!!!{{4::rxx'=>>>vvaa ~~

d{{4::rxxc
';<<<vvaD!~~zzV###{{4::rxx'?@@@vvaD!~~zzV###{{4::rxx#'?@@@KKOvvay~~)))r!   c                 n   t        j                  g dg dg      }t        j                  |      }t        j                  |      }t        j
                  |      }|j                         sJ |j                         sJ |j                         sJ t        j                  |j                         |j                        sJ t        j                  |j	                         |j                        sJ t        j                  |j                         |j                        sJ dD ]  }t        j                  ||      }t        j
                  ||      }t        j                  |j	                  |      |j                        sJ t        j                  |j                  |      |j                        rJ  dD ]  }dD ]  }t        j                  |||      }t        j
                  |||      }|j                  |j                  ||      j                  k(  sJ |j                  |j                  ||      j                  k(  sJ t        j                  |j                  ||      |j                        sJ t        j                  |j                  ||      |j                        rJ   y)z'Test the mean, std, var atoms.
        )r  r  rP  )rQ  rC  r|   r  )ddof)TFr   N)r   r   r0   meanr   stdr!  r  rb   r2   r   )r   r   	expr_meanexpr_varexpr_stdr  r~   r   s           r   
test_statszTestAtoms.test_stats  s!    HHo}56GGAJ	66!966!9""$$$!!###!!###zz!&&(IOO444zz!%%'8>>222zz!%%'8>>222 	@Dvvad+Hvvad+H::aeee.???::aeee.???	@  	XD) XGGAD8D	66!$B !&&dX&*N*T*TTTT~~D8)L)R)RRRR{{166th6#GYYY{{155dX5#FWWWX	Xr!   c                    d}t        |      t        |      }}t        t        j                  t        j                  |            | |k  ||k  g      }t        ||g|g      }| j                  |j                  t        j                         t        t        j                  t        j                  |            | |k  ||k  g      }t        ||g|g      }| j                  |j                  t        j                         t        t        j                  t        j                  |d               | |k  ||k  g      }t        ||g|g      }| j                  |j                         d       | j                  |j                         d       y)z1Test DCP properties of partial optimize.
        r   r   FN)r
   r   r0   r   r   r   r)   r4   r5   r6   r   r_   r   r7   r8   )r   dimsr   tp2gs         r   test_partial_optimize_dcpz#TestAtoms.test_partial_optimize_dcp!  s4    ~x~1R[[+qbAgqAv->?R!qc*ahh/R[[+qbAgqAv->?R!qc*aii0R[[1Q41QB!GQ!V3DER!qc*./r!   c                    d}t        |      t        |      }}dg|z  }t        t        j                  t        j                  |            | |k  ||k  g      }|j                          t        t        j                  t        j                  |            | |k  ||k  g      }t        ||g|g      }t        t        j                  |      ||k(  g      }|j                          | j                  |j                  |j                         t        t        j                  t        j                  |             | |k  ||k  g      }t        ||g      }t        t        j                  |      ||k(  g      }|j                          | j                  |j                  |j                          t        t        j                  t        j                  |            | |k  ||k  g      }t        ||g      }t        t        j                  |      ||k(  g      }|j                          | j                  |j                  |j                         t        ||g      }t        t        j                  |      ||k(  g      }|j                          | j                  |j                  |j                         | j                  t              5 }	t        |      }ddd       | j                  t        	j                        d       | j                  t              5 }	t        |g |g      }ddd       | j                  t        |	j                        d       y# 1 sw Y   xY w# 1 sw Y   ;xY w)z(Test the partial_optimize atom.
        r   )opt_vars)dont_opt_varsNz@partial_optimize called with neither opt_vars nor dont_opt_vars.z`If opt_vars and new_opt_vars are both specified, they must contain all variables in the problem.)r
   r   r0   r   r   r   r   r   rb   r   r@   rA   r)   rC   rD   )
r   r  r   r  xvalp1r  r  p3rF   s
             r    test_partial_optimize_eval_1normz*TestAtoms.test_partial_optimize_eval_1norm4  s    ~x~1tDyR[[+qbDj$!)-DE

 R[[+qbAgqAv->?R!qc*R[[^a4i[1

rxx2 R[[,rQwQ.?@R1#.R[[^a4i[1

rxx"((3
 R[[+qbAgqAv->?R1#.R[[^a4i[1

rxx2 Rs3R[[^a4i[1

rxx2y) 	%R $A	%R\\*[	] y) 	.R R!-A	.R\\*L		% 	%
	. 	.s   MM&M#&M/c                 h   d}t        |      t        |      }}t        t        t        j                  |            | |k  ||k  g      }t        ||g|g      }t        t        |            }|j                          |j                          | j                  |j                  |j                         y )Nr   	r
   r   r   r0   r   r   r   r   rb   )r   r  r   r  r  r  r  s          r   test_partial_optimize_min_1normz)TestAtoms.test_partial_optimize_min_1normg  s    ~x~1XbffQi(A27AF*;< R!qc*Xa[!



rxx2r!   c                    t        d      t        d      }}t        t        ||z         ||z   dk\  |dk\  |dk\  g      }|j                  t        j
                         t        t        |      ||z   dk\  |dk\  g      }t        ||g|g      }t        t        ||z         |dk\  g      }|j                  t        j
                         | j                  |j                  |j                         y )Nr%   r   rY   rZ   r   )	r
   r   r   r   r0   CLARABELr   r   rb   r   r   r   r  r  r  r  s          r   $test_partial_optimize_simple_problemz.TestAtoms.test_partial_optimize_simple_problemu  s    {HQK1 Xac]QqSAXqAvqAv$>?
$ Xa[1Q3!8Q!V"45R!qc*Xac]Q!VH-
$rxx2r!   HIGHSzHiGHS solver is not installed.c                    t        d      t        d      }}t        t        ||z         ||z   dk\  |dk\  |dk\  g      }|j                  t        j
                         t        t        |      ||z   dk\  |dk\  g      }t        ||g|g      }t        t        ||z         |dk\  g      }|j                  t        j
                         | j                  |j                  |j                         y )NT)boolean)integerr   rY   rZ   r   )	r
   r   r   r   r0   r  r   r   rb   r  s          r   !test_partial_optimize_special_varz+TestAtoms.test_partial_optimize_special_var  s    %x'=1 Xac]QqSAXqAvqAv$>?
! Xa[1Q3!8Q!V"45R!qc*Xac]Q!VH-
!rxx2r!   c                 V   t        d      t        d      }}t        t        |t        j                  |      z         ||z   dk\  |dk\  |dk\  g      }|j                  t        j                  d       t        t        t        j                  |            ||z   dk\  |dk\  g      }t        ||g|gt        j                  d      }t        t        ||z         |dk\  g      }|j                  t        j                  d       | j                  |j                  |j                  d       y )Nr%   r   rY   rZ   g&.>r   epsplaces)
r
   r   r   r0   r:   r   r   r   r   rb   r  s          r   $test_partial_optimize_special_constrz.TestAtoms.test_partial_optimize_special_constr  s    {HQK1 Xa"&&)m,qsaxaa.HI
D) XbffQi(1Q3!8Q!V*<=R!qc"&&dCXac]Q!VH-
D)rxx!<r!   c                 (   t        d      t        d      }}t               }t        t        ||z         ||z   |k\  |dk\  |dk\  g      }d|_        |j                  t        j                  d       t        t        |      ||z   |k\  |dk\  g      }t        ||g|gt        j                  d      }t        t        ||z         |dk\  g      }|j                  t        j                  d       | j                  |j                  |j                         y)z/Test partial optimize with parameters.
        r%   rY   rZ   r   ư>r  N)
r
   r	   r   r   rb   r   r0   r   r   r   )r   r   r   gammar  r  r  r  s           r   test_partial_optimize_paramsz&TestAtoms.test_partial_optimize_params  s     {HQK1 Xac]QqSE\1616$BC
D) Xa[1Q3%<a"89R!qc"&&dCXac]Q!VH-
D)rxx2r!   c                    t               t               }}d}t        t        |      ||z   dk\  g      }|j                  t        j
                  d       |dk\  g}t        t        |      ||z   dk\  g|z         }t        ||g|gt        j
                  d      }||_        d|_        d|d   j                  d   _        |j                  }| j                  ||j                         | j                  |j                  d       | j                  |d   j                  d       t        t        |      ||z   dk\  g      }t        |g ||gt        j
                  d      }||_        d|_        d|j                  d   j                  d   _        |j                  }| j                  ||j                         | j                  |j                  d       | j                  |j                  d   j                  d       y )NrY   r   r  r  i*   r   )r
   r   r   r   r0   r   r   rb   dual_variablesr   
dual_valuerp  )	r   r   r   r  r  constrr  r  rF  s	            r    test_partial_optimize_numeric_fnz*TestAtoms.test_partial_optimize_numeric_fn  s   z8:1 Xa[46Q;-0
D) t)Xa[1Q3!8*v"56R!qc"&&dC,.q	  #)vrxx0qww+vay33R8 Xa[1Q3!8*-RaVBFFE46q((+1vqww/qww+r~~a0;;R@r!   c                    d}t        |d      }t        |d      }t        t        t        j                  |            | |k  ||k  g      }t        ||g|g      }t        t        t        |            |g      }t        t        |            }|j                          |j                          | j                  |j                  |j                         y)z-Minimize the 1-norm in the usual way
        r   r   r   r  Nr  )r   r  r   r  r  r  g2r  s           r   test_partial_optimize_stackedz'TestAtoms.test_partial_optimize_stacked  s     T$T$XbffQi(A27AF*;< R!qc*ghqk2QC:Xb\"



rxx2r!   c                    t        d      }t        t        d|z         |dk\  g      }|j                  t        j
                  d       | j                  |j                  d       | j                  |j                  d       y)	z-Test the NonNegative Variable class.
        Tr   rZ   r   gh㈵>r  r   N)r
   r   r   r   r0   r   r   rb   )r   r   rc   s      r   test_nonnegative_variablez#TestAtoms.test_nonnegative_variable  sd     D!HQqSMAF8,	rvv4(qww*qww*r!   c                    t        d      }t        t        j                  |dd            }t	        ||t        j                  d      k(  g      }|j                  t        j                        }| j                  |d       y)zTest mixed norm.
        rZ   rZ   r   r%   r   rZ   N)
r
   r   r0   
mixed_normr   r   r   r   r   r   )r   r   r   r   rF  s        r   test_mixed_normzTestAtoms.test_mixed_norm  se     Vr}}Qq12sQ"''&/123266*vq)r!   c                    t        j                  ddgddgg      }t        d      }t        t	        j
                  |d            }t	        j                  |||k(  g      }|j                  t        j                        }| j                  |t	        j
                  |d      j                  d       t        t	        j
                  |t         j                              }t	        j                  |||k(  g      }|j                  t        j                        }| j                  |t	        j
                  |t         j                        j                  d       y)	zCTest that norm1 and normInf match definition for matrices.
        r%   r   r   rY   r   r   r  N)r   r   r
   r   r0   rE   r   r   r   r   rb   r   )r   r   r   r   r   rF  s         r   test_mat_normszTestAtoms.test_mat_norms  s     HHq!fq!f%&Vrwwq!}%zz#Qx(266*vrwwq!}':':1Erwwq"&&)*zz#Qx(266*vrwwq"&&'9'?'?Jr!   c                 &   t        j                         }d|k  |dk  g}t         j                  j                  |      }d|_        | j                  |j                  d       d|_        | j                  |j                  t        j                         y )Nr   r%   r   rC  r   )r0   r
   
transforms	indicatorrb   r)   r   r   )r   r   rp  rQ   s       r   test_indicatorzTestAtoms.test_indicator  sm    KKMAvqAv&}}&&{3S)RVV,r!   c                     | j                  t              5 }t        j                  ddgddgg      j                   d d d        | j                  t        j                        d       y # 1 sw Y   /xY wNr%   r   r   rY   rJ  )r@   r   r0   log_detrb   r)   rC   rD   r   s     r   test_log_detzTestAtoms.test_log_det  sc    z* 	/bJJAA'(..	/R\\*D	F	/ 	/   &A**A3c                     | j                  t              5 }t        j                  ddgddgg      j                   d d d        | j                  t        j                        d       y # 1 sw Y   /xY wr  )r@   r   r0   
lambda_maxrb   r)   rC   rD   r   s     r   test_lambda_maxzTestAtoms.test_lambda_max  sc    z* 	2bMMAq6Aq6*+11	2R\\*D	F	2 	2r  c                    t        j                  d      }t        j                  d      }| j	                  t        j
                  |d      j                  t        j
                  |d      j                         | j	                  t        j
                  |d      j                  t        j
                  |d      j                         t        j                  g dg      }t        j                  d|      }t        j
                  |d      }| j                  |j                  t        j
                  |d             t        j
                  |d      }| j                  |j                  t        j
                  |d             t        j                  t        d	      5  t        j
                  |d      j                   d
d
d
       y
# 1 sw Y   y
xY w)zTest the diff atom.
        )   r   r   r}   r%   r%   r   r   rY   rZ   )r%   rZ   rb   z< k elementsrp   N)r0   r
   r   rw  r)   diffr2   r   r   rb   rs   rt   r   )r   r   r   x1x2rQ   s         r   	test_diffzTestAtoms.test_diff  sB    KK!HHX+11+11	3+11+11	3
 XX'([[r*wwr"##DJJ0CDwwr"##DJJ0CD]]:^< 	&GGBQ%%	& 	& 	&s   %"GGc           	         | j                  t        j                  | j                        j                  t
        j                         | j                  t        j                  | j                        j                  t
        j                         t        dd      D ]n  }| j                  t        j                  t        j                  j                  j!                  |            t        j                  |      j"                  d d       p t%        d      }t'        t        j(                  t        j                  |                   }t+        ||dk(  g      }|j-                  t        j.                        }| j                  | dt        j                  t        j                  j                  j!                  d            z  d d       y )	NrZ   g{Gz?)r  deltar   r   r   rY   )r)   r0   log_normcdfr   r`   r5   rl   r4   r_   r   r   r   logscipystatsrE   cdfrb   r
   r   r   r   r   r  )r   r   r   r   r   rF  s         r   test_log_normcdfzTestAtoms.test_log_normcdf5  sC   /44ahh?/99199Er1 	A""u{{''++A./q!''	 # 	 Vrvvr~~a0012sQ!VH%2;;/GQ 0 0 4 4Q 788T 	 	
r!   c                 0   t        j                  d      }t        j                  d      }t        j                  t        j
                  ||            }t        j                  ||dk\  g      }|j                  t        j                         t        j                  |j                  |      sJ t        j                  t        j                  ||            }t        j                  ||dk\  g      }|j                  t        j                         t        j                  |j                  |      sJ t        j                  d      }t        j                  d      }t        j                  d      |_
        t        j                  t        j
                  ||            }t        j                  ||dk\  g      }|j                  t        j                         t        j                  |j                  |j                        sJ t        j                  t        j                  ||            }t        j                  ||dk\  g      }|j                  t        j                         t        j                  |j                  |j                        sJ y)zTest scalar product.
        r   r%   r   N)r   r   r0   r
   r   vdotr   r   r   r   rb   scalar_productr	   )r   rc   r>  r   r   s        r   	test_vdotzTestAtoms.test_vdotI  s    GGDMKKkk"''!Q-(zz#Qx(

"&&
!{{177A&&&kk"++Aq12zz#Qx(

"&&
!{{177A&&& LLKK''$-kk"''!Q-(zz#Qx(

"&&
!{{177AGG,,,kk"++Aq12zz#Qx(

"&&
!{{177AGG,,,r!   c           	      >   t        j                  d      }t        d      }t        j                  ||      }| j                  |j                  d       t        d      }t        j                  ||      }| j                  |j                  d       t        j                  d      }t        j                  ||      }t        j                  ||      }t        j                  |j                  |d      sJ t        j                  t        j                  dd	      t        j                  dd	      j                        sJ t        j                  t        j                  d|      t        j                  d|      j                        sJ t        j                  d
      j                  d      }t        j                  t        d      5  t        j                  ||       ddd       t        j                  t        d      5  t        j                  ||       ddd       t        j                  t        j                  t        j                   dd	gdd
gg      d      j                  t        j                   g d            sJ y# 1 sw Y   xY w# 1 sw Y   {xY w)zTest the outer atom.
        r   r   r   rW   r   r_  atolr   r   rY   r   zx must be a 1-d arrayrp   Nzy must be a 1-d arrayr%   r   r   )r%   r   r   rY   )r   r   r
   r0   outerr)   r2   r	   r   rb   r   r   rs   rt   r   r  r   )r   r   r   rQ   r   dtrue_valr   s           r   
test_outerzTestAtoms.test_outerh  s    GGDMTNxx1~V, dOxx1~V,GGDMxx1~88Aq>{{4::xd;;; {{288Aq>288Aq>+?+?@@@{{288Aq>288Aq>+?+?@@@ IIaL  (]]:-DE 	HHQN	]]:-DE 	HHQN	 {{266"((QFQF+;"<CHNN88L13 	3 3	 		 	s   JJJJc                    t        j                  d      }t        j                  t        j                  |            }t        j                  |t        j
                  |      dk\  g      }|j                  t         j                         t        j                  |j                  t        j                  d            sJ y)zTest conj.
        r   r%   r   N)r0   r
   r   r   r   conjr   r   r   r   rb   r   )r   r>  r   r   s       r   	test_conjzTestAtoms.test_conj  sy     KKkk"&&)$zz#
a01

"&&
!{{177BGGDM222r!   c                 l   t        j                  dd      }t        j                  |d      }t        j                  j                  |      }t        j                  t        j
                  |      j                  |d      sJ t        j                  d      }t        j                  t        j
                  |            }t        j                  t        j                  |      ||k(  g      }|j                  t        j                        }t        j                  ||j                         d      sJ y)	z-Test the approximation of log-gamma.
        r%   r   r   r   r_  r  r   r	  N)r   r   r   r  specialloggammar   r0   rb   r
   r   r   r   r   r   r  )r   r   r  r   costr   rF  s          r   test_loggammazTestAtoms.test_loggamma  s     IIaJJq&!==))!,{{2;;q>//EEE KKvvbkk!n%zz"++d+6($266*zz&(,,.s;;;r!   c                    t         j                  j                  d       t         j                  j                  d      dt         j                  j                  d      z  z   }|t        j                  |      z  }t         j                  j                  d      dt         j                  j                  d      z  z   }|t        j                  |      z  }t         j                  j                  d      dt         j                  j                  d      z  z   }|t        j                  |      z  }t        j                  ||      }t        j                  ||      }t        j                  ||      }t        j                  |j                  d      }||_        t        j                  |g dd	
      }t        j                  |g dd
      }	t        j                  |ddgd
      }
t        j                  |ddgd
      }t        j                  |	dd	gd
      }t        j                  |j                  |      sJ t        j                  |	j                  |      sJ t        j                  |
j                  |      sJ t        j                  |j                  |      sJ t        j                  |j                  |      sJ y)z
        Test partial_trace atom.
        rho_ABC = rho_A \otimes rho_B \otimes rho_C.
        Here \otimes signifies Kronecker product.
        Each rho_i is normalized, i.e. Tr(rho_i) = 1.
        r%   rY   rY                 ?r  r   Tr2   complex)rY   r   r   r   r}   rY   r   r   N)r   r   ra  r  r  r0   r
   r2   rb   partial_tracer   )r   rho_Arho_Brho_Crho_ABrho_ACtemprho_ABCrho_AB_testrho_AC_test
rho_A_test
rho_B_test
rho_C_tests                r   test_partial_tracezTestAtoms.test_partial_trace  s    			q 		  (2bii.>.>v.F+FF% 		  (2bii.>.>v.F+FF% 		  (2bii.>.>v.F+FF% && wwvu%++DJJ= &&w	B&&w	B%%kAq6B
%%kAq6B
%%kAq6B
 {{;,,f555{{;,,f555{{:++U333{{:++U333{{:++U333r!   c                    t        j                  d      }| j                  t              5 }t        j                  |ddgd       ddd       | j                  t        j                        d       t        j                  d      }| j                  t              5 }t        j                  |ddgd	       ddd       | j                  t        |j                        d
       t        j                  d      }| j                  t              5 }t        j                  |ddgd       ddd       | j                  t        |j                        d       y# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   HxY w)1Test exceptions raised by partial trace.
        r  r   r   r   r  r~   Nz.partial_trace only supports 2-d square arrays.r   r   rX   9Invalid axis argument, should be between 0 and 2, got -1.rY   BDimension of system doesn't correspond to dimension of subsystems.)r0   r
   r@   r   r  r)   rC   rD   r   r   rF   s      r   test_partial_trace_exceptionsz'TestAtoms.test_partial_trace_exceptions  s:    KKz* 	5bQaV!4	5R\\*I	K KKz* 	6bQaV"5	6R\\*T	V KKz* 	5bQaV!4	5R\\*]	_	5 	5	6 	6	5 	5#   EE%E1E"%E.1E:c                    t         j                  j                  d       t         j                  j                  d      dt         j                  j                  d      z  z   }|t        j                  |      z  }t         j                  j                  d      dt         j                  j                  d      z  z   }|t        j                  |      z  }t         j                  j                  d      dt         j                  j                  d      z  z   }|t        j                  |      z  }t        j                  t        j                  ||      |j
                        }t        j                  t        j                  ||j
                        |      }t        j                  t        j                  |j
                  |      |      }t        j                  t        j                  ||      |      }t        j                  |j                  d      }||_	        t        j                  |g dd	
      }	t        j                  |g dd
      }
t        j                  |g dd
      }t        j                  |	j                  |      sJ t        j                  |
j                  |      sJ t        j                  |j                  |      sJ y)z
        Test out the partial_transpose atom.
        rho_ABC = rho_A \otimes rho_B \otimes rho_C.
        Here \otimes signifies Kronecker product.
        Each rho_i is normalized, i.e. Tr(rho_i) = 1.
        r%   )r   r   r  r"  r  Tr  )r   r   rY   r   r}   r   N)r   r   ra  r  r  r  r0   r
   r2   rb   partial_transposer   )r   r  r  r  rho_TCrho_TBrho_TAr  r  rho_TC_testrho_TB_testrho_TA_tests               r   test_partial_transposez TestAtoms.test_partial_transpose  s    			q 		  (2bii.>.>v.F+FF% 		  (2bii.>.>v.F+FF% 		  (2bii.>.>v.F+FF% .80%8%0%8 wwrwwue,e4++DJJ= **7IAF**7IAF**7IAF {{;,,f555{{;,,f555{{;,,f555r!   c                    t        j                  d      }| j                  t              5 }t        j                  |ddgd       ddd       | j                  t        j                        d       t        j                  d      }| j                  t              5 }t        j                  |ddgd	       ddd       | j                  t        |j                        d
       t        j                  d      }| j                  t              5 }t        j                  |ddgd       ddd       | j                  t        |j                        d       y# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   HxY w)r   r  r   r   r   r!  Nz2partial_transpose only supports 2-d square arrays.r"  rX   r#  rY   r$  )r0   r
   r@   r   r)  r)   rC   rD   r%  s      r   !test_partial_transpose_exceptionsz+TestAtoms.test_partial_transpose_exceptions  s:    KKz* 	9b  !Qa8	9R\\*M	O KKz* 	:b  !Qb9	:R\\*T	V KKz* 	9b  !Qa8	9R\\*]	_	9 	9	: 	:	9 	9r'  c                    t        d      }t        j                  |      }| j                  |j                  t
        j                         | j                  |j                  t
        j                         t        d      }t        j                  |      }| j                  |j                  t
        j                         | j                  |j                  t
        j                         y)zTest log_sum_exp sign.
        Tr   r   N)
r
   r0   log_sum_expr)   r4   r5   r6   r`   ra   r   rm   s      r   test_log_sum_expzTestAtoms.test_log_sum_exp  s     D!~~a 2AHH- D!~~a 2AII.r!   c                 |   t        j                  d      }t        j                  |dd      }t        j                  |d      }| j                  |j                  |       t        j                  |      j                  d      }| j                  |j                  |       t        j                  |dd      }t        j                  |d      }| j                  |j                  |       t        j                  |      j                  d      }| j                  |j                  |       t        d      }t        j                  |dd      }t        j                  |d      }t        j                  t        j                  d      ||k(  g      j                          | j                  |j                  |       t        j                  |      j                  d      }t        j                  t        j                  d      ||k(  g      j                          | j                  |j                  |       t        j                  |dd      }t        j                  |d      }t        j                  t        j                  d      ||k(  g      j                          | j                  |j                  |       t        j                  |      j                  d      }t        j                  t        j                  d      ||k(  g      j                          | j                  |j                  |       y)zTest flatten and vec.r   )r   rZ   r   r   r   r   N)r   r   r   r0   r  r   rb   r   flattenr
   r   r   r   )r   r   reshapedrQ   r   s        r   test_flattenzTestAtoms.test_flatten+  sG    IIbM::as3vvhc*##DJJ2{{8$,,3,7##DJJ2::as3vvhc*##DJJ2{{8$,,3,7##DJJ2 V::as3vvas#


2;;q>DAI;/557##AGGX6{{1~%%C%0


2;;q>DAI;/557##AGGX6::as3vvas#


2;;q>DAI;/557##AGGX6{{1~%%C%0


2;;q>DAI;/557##AGGX6r!   c                    d}t        j                  ||fd      }|dz	  g}|t        j                  |      dk(  gz  }t        j                  t        j                  t        j
                  |            |      }|j                          | j                  |j                  |dz         |j                  }t        j                  |      |z  }| j                  ||d       |dz	  g}d}t        j                  j                  ||      }||d	k\  |dk  gz  }t        j                  t        j                  t        j
                  ||z  |j                  z              |      }||j                  z  }	t        j                  t!        j"                  |	      d	z        }
|j                  d
       | j%                  |j                  |
k         y)zTest tr_inv atom. rZ   TrL   r   r%   r   rY   r  rX   )verboseN)r0   r
   r  r   r   tr_invr   r   rb   r   eyer   r   r   r  r   LAeigvalshr'   )r   r  r   rp  r   X_actualX_expectr=  r?  MMnaiveRess              r   test_tr_invzTestAtoms.test_tr_invM  s~    KKA$/ AvhHHQK1
 	
 zz"++biil3[A

tzz1a407766!9q=##Hhq#A
 Avh IIOOAq!GF
 	
 zz"++biiA&<={KW66"++b/R/0

4
  	

X-.r!   returnN)S__name__
__module____qualname____doc__r    r.   r;   r>   rG   rJ   rR   rd   rn   rv   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r'  r*  r-  r;  r@  rG  rZ  r]  rr  rt  rz  r  r  r  r  r  r  r  r  r  r  r  unittest
skipUnlessr   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r&  r0  r2  r5  r9  rD  r  r!   r   r   r   &   s   ,,;$A	>R.<!)*X.(6.J;XS W"H2!H64!HFC.-B&5R70r!GF+ %(N% J
%.N%bN'Q-fL5\52;-z;*1!,F
33L ,B*B"XH0&1f33 X$88:Z[3 \3=3&A@3"+*K-FF&.
(-> 3D3<"&4P_*$6L_*/ 7D+/r!   r   c                   `    e Zd ZdZd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)TestDotsortz" Unit tests for the dotsort atom. Nc                 8    t        j                  d      | _        y )NrZ   )r0   r
   r   r   s    r   r    zTestDotsort.setUp}  s    Qr!   c                    t        j                  g d      }t        j                  g d      }t        j                  | j                  |      }|j                         sJ |j                  d      sJ t        j                  t        j                  |      | j                  |k(  g      }|j                          | j                  |j                  j                  t        j                  t        j                  |      dd               y )Nr%   r   r   r  r   )r%   r%   r%   r   r   r   )r   r   r0   dotsortr   r7   is_incrr   r   r   r   rl  rb   r   sortr   r  r  rQ   r   s        r   test_sum_k_largest_equivalencez*TestDotsort.test_sum_k_largest_equivalence  s    )*HH\"zz$&&!$~~||Azz"++d+dffo->?

t~~33RVVBGGEN23<O5PQr!   c                    t        j                  g d      }t        j                  g d      }t        j                  | j                  |       }|j                         sJ |j                  d      sJ t        j                  t        j                  |      | j                  |k(  g      }|j                          | j                  |j                  j                  t        j                  t        j                  |      d d              y )NrQ  )rX   rX   rX   r   r   r   )r   r   r0   rR  r   r8   is_decrr   r   r   r   rl  rb   r   rT  rU  s        r   test_sum_k_smallest_equivalencez+TestDotsort.test_sum_k_smallest_equivalence  s    )*HH_%

4661%%   ||Azz"++d+dffo->?

t~~33RVVBGGEN2A<N5OPr!   c                 <   t        j                  g d      }t        j                  g d      }t        j                  | j                  |      }|j                         sJ |j                  d      rJ |j                  d      rJ t        j                  t        j                  |      | j                  |k(  g      }|j                          | j                  |j                  j                  t        j                  |      t        j                  |      z         y )NrQ  )rX   rZ   r   r   rZ   r   )r   r   r0   rR  r   r7   rS  rX  r   r   r   r   rl  rb   rT  rU  s        r   test_1DzTestDotsort.test_1D  s    )*HH%&zz$&&!$~~<<?""<<?""zz"++d+dffo->?

t~~33RWWU^bggaj5PQr!   c                    t        j                  d      }t        j                  d      j	                  d      }t        j                  d      j	                  d      }t        j
                  |      }||d |j                  d   d |j                  d   f<   t        j                  ||      }|j                         sJ |j                  d      sJ t        j                  t        j                  |      ||k(  g      }|j                          | j                  |j                  j                  t        j                   |j#                               t        j                   |j#                               z         y )Nr     rY   r   r   r%   )r0   r
   r   r   r   
zeros_liker2   rR  r7   rS  r   r   r   r   rl  rb   rT  r7  r   r   r  r  w_paddedrQ   r   s          r   test_2DzTestDotsort.test_2D  s   KK		"%%f-IIaL  (=='-.!''!*kqwwqzk)*zz!Q~~||Azz"++d+a5j\:

t~~33!wwu}}7"''(BRBRBT:UU	Wr!   c                    t        j                  g d      }d}t        j                  | j                  |      }|j                         sJ |j                  d      sJ t        j                  t        j                  |      | j                  |k(  g      }|j                          | j                  |j                  j                  t        j                  t        j                  |      dd               t        j                         }t        j                  dg      }d}t        j                  ||      }|j                         sJ |j                  d      sJ t        j                  t        j                  |      ||k(  g      }|j                          | j                  |j                  j                  t        j                  t        j                  |      dd               y )NrQ  r%   r   rX   )r   r   r0   rR  r   r7   rS  r   r   r   r   rl  rb   r   rT  r
   )r   r  r  rQ   r   r   s         r   test_0DzTestDotsort.test_0D  sT   )*zz$&&!$~~||Azz"++d+dffo->?

t~~33RVVBGGEN23<O5PQ KKM!zz!Q~~||Azz"++d+a5j\:

t~~33RVVBGGEN23<O5PQr!   c                    t        j                  d      }t        j                  d      j                  d      }t        j                  d      j                  d      }t        j                  |      }||d |j                  d   d |j                  d   f<   t        j                  ||      }|j                         sJ |j                  d      sJ t        j                  t        j                  |      g       }|j                          | j                  |j                  j                  t        j                  |j!                               t        j                  |j!                               z         y )Nr]  r  rY   r   r   r%   )r   r   r   r^  r2   r0   rR  r7   rS  r   r   r   r   rl  rb   rT  r7  r_  s          r   test_constantzTestDotsort.test_constant  s
   IIbM		"%%f-IIaL  (=='-.!''!*kqwwqzk)*zz!Q~~||Azz"++d+R0

t~~33!wwu}}7"''(BRBRBT:UU	Wr!   c           
      "   t        j                  g d      }t        j                  | j                  t        j
                  dd            j                  d      sJ t        j                  | j                  t        j
                  dd            j                  d      sJ t        j                  | j                  t        j
                  dd            j                  d      rJ t        j                  | j                  t        j
                  dd            j                  d      sJ t        j
                  dddg	      }t        j                  | j                  |      }|j                  d      rJ |j                  d      rJ t        j                  t        j                  |      | j                  |k(  g      }|j                  d
       | j                  |j                  j                  t        j                  |      t        j                  t        j                  g d            z         ddg|_        |j                  d
       | j                  |j                  j                  t        j                  |      t        j                  t        j                  g d            z         t        j
                  dddg	      }d|z  }t        j                  | j                  |      }t        j                  t        j                  |      | j                  |k(  g      }|j                  d
       | j                  |j                  j                  t        j                  |      t        j                  t        j                  g d            z         ddg|_        |j                  d
       | j                  |j                  j                  t        j                  |      t        j                  t        j                  g d            z         t        j                  g d      }t        j
                  d	      }|dz  }t        j                  ||      }t        j                  t        j                  |            }	|	j                  d
       | j                  |j                  d       d|_        |	j                  d
       | j                  |j                  d       t        j                   t"        d      5  t        j                  g d      }t        j
                  d	      }|dz  }t        j                  | j                  |      }t        j                  t        j                  |      | j                  |k(  g      }	|	j                          | j                  |j                  d       d|_        |	j                          | j                  |j                  d       d d d        y # 1 sw Y   y xY w)NrQ  r   Trf   r   r   rh   r%   r  r~  )r%   r   r   r   r   rX   )rX   rX   r   r   r   )r   r   r   r   r   )r   r   r   r   r   r   r0  r   z8You are solving a parameterized problem that is not DPP.rp   r  r  rZ   )r   r   r0   rR  r   r	   rS  rX  r   r   r   r   rl  rb   rT  rs   warnsUserWarning)
r   r  w_prQ   r   parameter_affine_expressionx_constrc   	p_squaredrE  s
             r   test_parameterzTestDotsort.test_parameter  s4   )*zz$&&",,qd";<DDQGGGzz$&&",,q">?GGJJJ::dffbll1$&?@HHKKKzz$&&",,qd";<DDQGGGll1QF+zz$&&#&<<?""<<?""zz"++d+dffo->?

t
$t~~33!wwu~8Q0RR	TH	

t
$t~~33!wwu~AR8S0TT	V ll1QF+&'#g#zz$&&"=>zz"++d+dffo->?

t
$t~~33!wwu~8Q0RR	TH	

t
$t~~33!wwu~AR8S0TT	V ((9%LLq!F	zz'9-**R[[./$'tzz:6$'tzz=9 \\+ Z\ 	>HH_-E1%AQI::dffi0DjjT!2TVVu_4EFGMMO""4::z:AGMMO""4::}=	> 	> 	>s   C)XXc                    t        j                  g d      }ddg}t        j                  | j                  |      }t        j
                  t        j                  || j                  z        d| j                  k  |dk  t        j                  | j                        dk(  g      }|j                          | j                  |j                  d       | j                  | j                  j                  d d |z  d       y )Nr   r%   r   rX   rX   333333?皙?r   r%   r   )r   r   r0   rR  r   r   r   r   r   r   rb   r   rr  rQ   r   s        r   	test_listzTestDotsort.test_list  s    HH&'#Jzz$&&!$zz"++a$&&j1AKBFFSWSYSYN^_L_3`a

tzz1-tvv||BQ/!3Q7r!   c                    t        j                  g d      }ddg}t        j                  t        j                  | j
                        |      }t        j                  t        j                  || j
                  z        d| j
                  k  |dk  t        j                  | j
                        dk(  g      }|j                          | j                  |j                  d       | j                  t        j                  t        j                  | j
                  j                              dd  t        j                  |      z  d       y )Nro  r   g?r   r   r%   r   )r   r   r0   rR  r:   r   r   r   r   r   r   rb   rT  rr  s        r   test_compositionzTestDotsort.test_composition  s    HH&'#Jzz"&&.!,zz"++a$&&j1AKBFFSWSYSYN^_L_3`a

tzz1-rwwrvvdffll';<RSABGGAJNPQRr!   c                 h   t        j                  ddg      }t        j                  | j                  |      }|j                         }| j                  t        |      t        |      u        | j                  |j                  |j                         | j                  |j                  |j                  u        | j                  |j                  d   |j                  d   u        | j                  |j                  d   |j                  d   u        |j                  | j                  |g      }| j                  |j                  |j                  u        | j                  |j                  d   |j                  d   u        | j                  |j                  d   |j                  d   u        y )Nr%   r   r   r#   )r   r   r0   rR  r   r&   r'   r(   r)   r$   r*   )r   r  r-   r&   s       r   	test_copyzTestDotsort.test_copy$  s<   HHaVzz$&&!$yy{T
d4j01 	DII.dii/0		!		!45		!		!45 yytvvqky*dii/0		!		!451156r!   c                    t        j                  g d      }t        j                  ddg      }t        j                  | j                  |      }t        j
                  t        j                  || j                  z        d| j                  k  |dk  t        j                  | j                        dk(  g      }|j                          | j                  |j                  d       | j                  | j                  j                  d d |z  d       t        j                  g d      }t        j                  g d      }t        j                  | j                  |      }t        j
                  t        j                  || j                  z        d| j                  k  |dk  t        j                  | j                        dk(  g      }|j                          | j                  |j                  d       | j                  t        j                  | j                  j                        dd  t        j                  |      z  d       y )	Nro  rp  rq  r   r%   r   )rp  rq  rK  r   )r   r   r0   rR  r   r   r   r   r   r   rb   rT  rr  s        r   test_non_fixed_xzTestDotsort.test_non_fixed_x7  s   HH&'HHc3Z zz$&&!$zz"++a$&&j1AKBFFSWSYSYN^_L_3`a

tzz1-tvv||BQ/!3Q7 HH&'HH_%zz$&&!$zz"++a$&&j1AKBFFSWSYSYN^_L_3`a

tzz1-rwwtvv||4RS9BGGAJFJr!   c           
         | j                  t              5 }t        j                  | j                  g d       d d d        | j                  t        j                        d       | j                  t              5 }t        j                  | j                  t        j                  d             d d d        | j                  t        |j                        d       | j                  t              5 }t        j                  g d| j                         d d d        | j                  t        |j                        d       | j                  t              5 }t        j                  t        j                  t        j                  t        j                  | j                        ddg                  j                          d d d        dt        |j                        v sJ t        j                  d	
      }|d	z  }| j                  t              5 }t        j                  t        j                  t        j                  | j                  |                  j                  d       d d d        dt        |j                        v sJ y # 1 sw Y   Sx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   VxY w)N)r%   r   r   rY   rZ   r   z8The size of of W must be less or equal to the size of X.r   z The W argument must be constant.r   rX   r%   z!Problem does not follow DCP rulesr   r  Tr~  z7You are solving a parameterized problem that is not DPP)r@   rA   r0   rR  r   r)   rC   rD   r
   r   r   absr   r	   )r   rF   rc   rl  s       r   test_exceptionszTestDotsort.test_exceptionsI  s   y) 	3RJJtvv12	3R\\*S	U y) 	/RJJtvvr{{1~.	/R\\*;	= y) 	*RJJy$&&)	*R\\*;	= y) 	QRJJr{{2::bffTVVnr1g#FGHNNP	Q2c",,6GGGG LLq!F	y) 	[RJJr{{2::dffi#@ABHHUYHZ	[HCPRP\P\L]]]]7	3 	3	/ 	/	* 	*	Q 	Q	[ 	[s=   #J
;4J1#J$A*J1AJ=
JJ!$J.1J:=KrE  )rG  rH  rI  rJ  r    rV  rY  r[  ra  rc  re  rm  rt  rv  rx  rz  r}  r  r!   r   rN  rN  z  sP    , RQ
RW R,W;>z8S7&K$^r!   rN  )$rJ  rK  numpyr   rs   r  scipy.sparsesparser   scipy.statsr   r>  cvxpyr0   cvxpy.settingssettingsr5   r   r   cvxpy.atoms.affine.upper_trir   cvxpy.atoms.errormsgr   cvxpy.expressions.constantsr   r	   cvxpy.expressions.variabler
    cvxpy.reductions.solvers.definesr   cvxpy.tests.base_testr   !cvxpy.transforms.partial_optimizer   r   rN  r  r!   r   <module>r     s^              # : R ; / A * >R/ R/h:l^( l^r!   