
    bi                     F    d Z ddlZddlZddlmZ ddlmZ  G d de      Z	y)a4  
Copyright 2019, the cvxpy developers.

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)SolverError)BaseTestc                   p    e Zd ZdZddZddZddZddZddZddZ	dd	Z
dd
ZddZddZddZddZy)TestSupportFunctionsz
    Test the implementation of support function atoms.

    Relevant source code includes:
        cvxpy.atoms.suppfunc
        cvxpy.transforms.suppfunc
        cvxpy.reductions.dcp2cone.canonicalizers.suppfunc_canon
    Nc                    t         j                  j                  d       d}t        j                  |f      }t        j
                  |g       }t         j                  j                  |      }t        j                  |f      } |||z
        dk  g}t        j                  ||z        }t        j                  ||      }|j                  d       |j                  }	t        j                  ||      }
| j                  t        |	|
z
        d       |j                  }	|}
| j                  t         j                  j                  |	|
z
  d      d       |d   j!                         }| j                  |d	       y )
Nr      shapeCLARABELsolverư>   ord:0yE>)nprandomseedcpVariablesuppfuncrandnMinimizeProblemsolvevaluedotassertLessEqualabslinalgnorm	violation)selfnxsigmaaycons	objectiveprobactualexpectedviols               T/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/tests/test_suppfunc.pytest_RnzTestSupportFunctions.test_Rn"   s&   
		qKKqd#Ar"IIOOAKKqd#a!e!"KKA&	zz)T*

*
%66!Q<S(!23T:RYY^^FX,=1^EtLAw  "T4(    c                 T   d}t         j                  j                  d       t         j                  j                  |      }t	        j
                  |f      }t	        j                  |t	        j                  ||z
  d      dk  g      }t         j                  j                  |      }t	        j
                  |f      }t	        j                  t	        j                   ||            ||k(  g      }|j                  d       |j                  }||z  t         j                  j                  |t         j                        z   }	| j                  t        ||	z
        d       | j                  t        |j                   j"                  j                  |j                  z
        d       y )N      r	   r   r   r   gh㈵>)r   r   r   r   r   r   r   r"   r   r   r   r   r!   infr   r    r+   expr
r$   r%   r(   r&   r'   r)   y_varr,   r-   r.   s
             r0   test_vector1normz%TestSupportFunctions.test_vector1norm6   s'   
		qIIOOAKKqd#AAq 1Q 678IIOOA1$'zz"++eEl3a5j\B

*
%q5299>>!>88S(!23T:S!4!4!:!:TZZ!GH$Or2   c                 8   d}t         j                  j                  d       t         j                  j                  |      }t	        j
                  |f      }t	        j                  |t	        j                  ||z
  d      dk  g      }t         j                  j                  |      }t	        j
                  |f      }t	        j                  t	        j                   ||            ||k(  g      }|j                  d       |j                  }||z  t         j                  j                  |d      z   }	| j                  t        ||	z
        d       | j                  t        |j                  j                   j                  |j                  z
        d       y )	Nr4   r5   r	   r   r   r   r   r   )r   r   r   r   r   r   r   r"   r   r   r   r   r!   r   r    r+   r7   r8   s
             r0   test_vector2normz%TestSupportFunctions.test_vector2normE   s#   
		qIIOOAKKqd#AAq 1Q 678IIOOA1$'zz"++eEl3a5j\B

*
%q5299>>!>33S(!23T:S!4!4!:!:TZZ!GH$Or2   c                 @   t         j                  j                  d       d\  }}t         j                  j                  ||      }t	        j
                  ||f      }t	        j                  ||d d df   dk(  g      }t	        j
                  ||f      } |||z
        dk  g}t	        j                  t	        j                  |j                  d                  }t	        j                  ||      }	|	j                  d       t        j                  t        j                  |d	f      |d d d	gf   g      }
|j                  }| j                  t         j                   j#                  ||
z
  d
      d       |d   j%                         }| j                  |d       y )Nr   )   r   r	   r   Forderr   r   r5   r   r   )r   r   r   r   r   r   r   r   sum_squaresflattenr   r   hstackzerosr   r   r!   r"   r#   )r$   rowscolsr(   r&   r'   r)   r*   r+   r,   expectr-   r/   s                r0   test_rectangular_variablez.TestSupportFunctions.test_rectangular_variableT   s@   
		q
dIIOOD$'KKtTl+A!Q$1~.KKtTl+a!e!"KKqyysy/C DE	zz)T*

*
%BHHD!95qQCyABRYY^^FVO^CTJAw  "T4(r2   c                    t         j                  j                  d       d}t        j                  ||f      }t        j
                  ||dz	  g      }t         j                  j                  ||      }t        j                  ||f      }t        j                  t        j                  |j                  d      |j                  d      z               } ||      dk  g}t        j                  ||      }|j                  dd	       |d   j                         }	| j                  |	d
       t         j                  j!                  |j"                        d   }
| j                  t        j$                  |
      d
       y )Nr   r4   r	   r   r?   r@   SCSr   r   epsr   )r   r   r   r   r   r   r   r   r"   ravelrC   r   r   r#   r   r!   eighr   max)r$   r%   Xr'   AYr+   r*   r,   r/   eigss              r0   test_psd_dualconez&TestSupportFunctions.test_psd_dualconee   s   
		qKKq!f%AQx(IIOOAq!KKq!f%KKc(:QYYSY=Q(Q RS	aAzz)T*

%T
*Aw  "T4(yy~~agg&q)RVVD\40r2   c                    t         j                  j                  d       d\  }}t         j                  j                  ||      }t         j                  j                  |d      }t        j                  ||f      }t        j                  |t        j                  |      dk  g      }t        j                  ||f      }||k(  g}t        j                  t        j                   ||            |      }	|	j                  dd	       |	j                  }
t        j                  |      }| j                  t!        |
|z
        d
       y )Nr4   )r4   r>   F)
compute_uvr	   r5   rK   r   rL   r   )r   r   r   r   r!   svdr   r   r   	sigma_maxr   r   r   r   sumr   r    )r$   rF   rG   rR   A_svrQ   r'   rS   r*   r,   r-   rH   s               r0   test_largest_singvaluez+TestSupportFunctions.test_largest_singvalueu   s    
		q
dIIOOD$'yy}}Q5}1KKtTl+AQ1 456KKtTl+Qxzz"++eAh/6

%T
*S&148r2   c                 t   t        j                  d      }t        j                  |d         t        j                  d      k  t        j                  |d          t        j                  d      k  g}t        j                  ||      }t        j                  d      }|d   } ||      dk  g}t        j
                  t        j                  |      |      }|j                  d       |d   j                         }| j                  |d       | j                  t        |j                  dz
        d       y )	N)r5   r	   r   r5   r   r   r   )r   r   expr   r   r   r   r   r#   r   r    r   )	r$   r&   tempconsr'   r)   obj_exprr*   r,   r/   s	            r0   test_expcone_1z#TestSupportFunctions.test_expcone_1   s    KKd#FF1Q4LBFF1I-rvvqte}q	/IJAx(KKd#Q4aAzz"++h/6

*
%Aw  "T4(SB0$7r2   c                    t        j                  d      }t        j                  |      dk  t        j                  |      dk\  |dk\  t        j                  |d   |d         |d   z   |d   z
  |d   z   dk  g}t        j                  ||      }t        j                  d      }t        j                  g d	      } ||       }t        j                  |      }||k(  g}t        j                  ||      }	|	j                  d
       |	j                  }
|j                  }d}| j                  t        |
|z
        d       | j                  t        ||z
        d       y )Nr4   r	   g      ?g?g{Gz?r5   r   r   )r^   r   r   gc?r   )r   r   rZ   kl_divr   r   arrayMaximizer   r   r   r   r    )r$   r&   r`   r'   r)   r(   r7   r+   r*   r,   
epi_actualdirect_actualrH   s                r0   test_expcone_2z#TestSupportFunctions.test_expcone_2   s,   KKd#FF1I$bffQi3&6T	IIadAaD)AaD01Q47!A$>!CEAx(KKd#HH\"ayKK%	Qxzz)T*

*
%ZZ


Sf!45t<S!78$?r2   c                    t         j                  j                  d       d}t         j                  j                  ||      }|j                  |z  }t        j                  ||f      }t        j                  |d|z  t        j                  |      dk  g      }t        j                  ||f      }||k(  g} ||      }t        j                  t        j                  |      |      }|j                  dd       |j                  }	|j                  }
| j                  t        |	|
z
        d	       t        j                  |      }| j                  t        |	|z
        d
       y )Nr>   r4   r	   r   r5   rK   r   rL   r   g-C6?)r   r   r   r   Tr   r   r   
lambda_maxr   r   r   r   r   r    trace)r$   r%   rR   rQ   r'   rS   r*   r7   r,   actual1actual2rH   s               r0   test_basic_lmiz#TestSupportFunctions.test_basic_lmi   s   
		qIIOOAq!CC!GKKq!f%AQa(8A(=>?KKq!f%QxQxzz"++d+T2

%T
*****S7!23T:!S6!12D9r2   c           	         d}t        j                  |f      }t        j                  |t        j                  |t        j
                  j                  |      z
  d      dk  g      }t        j                  |f      }t        j                  t        j                   ||            t        j
                  j                  |      |k(  g      }| j                  t        d      5  |j                  d       d d d        y # 1 sw Y   y xY w)Nr4   r	   r   r5   z .*could not be reduced to a QP.*OSQPr   )r   r   r   r"   r   r   r   r   r   assertRaisesRegexr   r   )r$   r%   r&   r'   r9   r,   s         r0   test_invalid_solverz(TestSupportFunctions.test_invalid_solver   s    KKqd#ABIIOOA,?(? Cq HIJ1$'zz"++eEl3biiooa6IU6R5ST##?A 	&JJfJ%	& 	& 	&s   %DD
c                     t        j                  dd      }| j                  t              5  t        j                  |g        d d d        y # 1 sw Y   y xY w)N)r   r   T)r
   	symmetric)r   r   assertRaises
ValueErrorr   )r$   r&   s     r0   test_invalid_variablez*TestSupportFunctions.test_invalid_variable   sC    KKf5z* 	KK2	 	 	s   AAc                     t        j                  d      }t        j                  d      }||z  dk(  g}| j                  t              5  t        j
                  ||       d d d        y # 1 sw Y   y xY w)Nrd   r	   r5   )r   r   	Parameterrz   r{   r   )r$   r&   r(   r*   s       r0   test_invalid_constraintz,TestSupportFunctions.test_invalid_constraint   s^    KKd#LLt$A
|z* 	!KK4 	! 	! 	!s   A++A4)returnN)__name__
__module____qualname____doc__r1   r:   r<   rI   rU   r\   rb   rl   rs   rw   r|   r    r2   r0   r   r      sF    )(PP)"1 98@&:$&
!r2   r   )
r   numpyr   cvxpyr   cvxpy.errorr   cvxpy.tests.base_testr   r   r   r2   r0   <module>r      s&       # *s!8 s!r2   