
    bi                     l    d Z ddlZddlZddlZddlmc mZ ddl	m
Z ddlmZ ddlmZ  G d de      Zy)a6  
Copyright 2013 Steven Diamond, Eric Chu

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)prune_constants)BaseTestc                   F    e Zd ZdZd
dZd
dZd
dZd Zd
dZd
dZ	d
d	Z
y)TestConvolutionz Unit tests for convolution. Nc                    d}t        j                  |      }t        j                  g d      }t        j                  g d      }t        j                  g d      }t	        j
                  t        d      5  t        j                  ||      }ddd       j                         sJ | j                  |j                  d       | j                  |j                  |j                  j                         | j                  |j                  |       t        j                  ||      }|j                         sJ | j                  |j                  d       t        j                  t        j                   t        j"                  |d	            ||k(  g      }|j%                  t         j&                  
      }| j)                  |t+        |      d       | j                  |j                  |       t        j                  d|      }| j                  |j                  d       | j                  |j                  |j                  j                         | j                  |j                  d|z         t        j                  |d      }| j                  |j                  d       | j                  |j                  |j                  j                         | j                  |j                  d|z         y# 1 sw Y   exY w)zTest 1D convolution.
                 r   r   r
   g      ?              ?g      @g      @g      ?zUse convolvematchN   r
   solverplacesr   r   )cpVariablenparraypytestwarnsDeprecationWarningconvis_constantassertEqualshapevalueassertItemsAlmostEqualconvolve	is_affineProblemMinimizenormsolveSCSassertAlmostEqualsum	selfnxfgf_conv_gexprprobresults	            W/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/tests/test_convolution.pytest_1D_convzTestConvolution.test_1D_conv   s$    KKNHHYHH[!8834\\,NC 	!771a=D	!!!!T*TZZ%5%56##DJJ9{{1a ~~T*zz"++bggdA&67F8%266*vs8}Q?##DJJ9 {{1a T*TZZ%5%56##DJJA6{{1a T*TZZ%5%56##DJJA65	! 	!s   8K22K<c                 x   d}t        j                  |      }t        j                  g d      }t        j                  g d      }t        j                  g d      }t        j                  ||      }|j                         sJ | j                  |j                  d       | j                  |j                  |j                  j                         | j                  |j                  |       t        j                  ||      }|j                         sJ | j                  |j                  d       t        j                  t        j                  t        j                  |d            ||k(  g      }|j                  t         j                        }| j!                  |t#        |      d       | j                  |j                  |       t        j                  d	|      }| j                  |j                  d
       | j                  |j                  |j                  j                         | j                  |j                  d	|z         t        j                  |d	      }| j                  |j                  d
       | j                  |j                  |j                  j                         | j                  |j                  d	|z         t%        j&                  t(        d      5  t        j                  ||dddf         }ddd       t%        j&                  t(        d      5  t        j                  |dddf   |      }ddd       t%        j&                  t(        d      5  t        j                  |dddf   |dddf         }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 convolve.
        r   r	   r   r   r   r
   r   r   r   r   zmust be scalar or 1Dr   N)r   r   r   r   r&   r!   r"   r#   r$   r%   r'   r(   r)   r*   r+   r,   r-   r.   r   raises
ValueErrorr/   s	            r9   test_convolvezTestConvolution.test_convolveB   s    KKNHHYHH[!8834{{1a !!!T*TZZ%5%56##DJJ9{{1a ~~T*zz"++bggdA&67F8%266*vs8}Q?##DJJ9 {{1a T*TZZ%5%56##DJJA6{{1a T*TZZ%5%56##DJJA6]]:-CD 	.;;q!AtG*-D	. ]]:-CD 	.;;qDz1-D	. ]]:-CD 	7;;qDz1QW:6D	7 	7	. 	.	. 	.	7 	7s$   (N)N$*%N0N!$N-0N9c                    |j                  t        j                        \  }}|d   }|j                  t        j                        \  }}}}|t        j
                     |t        j                     z   }|j                  ||      \  }	}
}|t        j
                     |t        j                     z   }t        |      }t        j                  |||	|
|      \  }}t        j                  t        d|dz               }t        j                  |j                  d         } |||       | j!                  ||z  |        |||       | j!                  d|z  |z  |       t        j                  t        |j                  d               }t        j                  |j                  d         } |||       | j!                  |j"                  |z  |        |||       | j!                  d|j"                  z  |z  |       y )Nr   Ar
   r   r   )get_problem_datar   r,   canonicalizesEQLEQ_get_var_offsetsr   	iterativeget_mul_funcsr   r   rangezerosr#   r%   T)r0   r7   datadimsr@   	objective
constr_mapr   all_ineqvar_offsets	var_sizesx_lengthconstraintsAmulATmulvecr8   s                    r9   prob_mat_vs_mul_funcsz%TestConvolution.prob_mat_vs_mul_funcsn   s   **"&&*9
dI.2.?.?.G+	:tVadd#j&77+/+@+@AI,K(Y &AEE)::%k2--k4.99.68e hhuQ
+,!''!*%S&##AGV4S&##AaC#Iv6hhuQWWQZ()!''!*%c6##ACC#Iv6c6##AaccECK8    c                     t        j                  |      }t        j                  |      }t        j                  ||f      }t        |      D ]#  }d||<    |||       ||dd|f<   |dz  }|dz  }% |S )z3Convert a multiplier function to a matrix.
        r   Nr   )r   rJ   rI   )r0   funcrowscolstest_vecr8   matrixis           r9   mat_from_funczTestConvolution.mat_from_func   s|     88D>$4,'t 	AHQK6"!F1a4LMHaKF	 rY   c           
          d}t         j                  j                  |d      }t         j                  j                  dd      }t        j                  |df      }t        j
                  ||      }t        j                  t        j                  t        j                  ||d|                   }t        j                  |g       }|j                  t        j                         |j                  t        j                  u sJ t         j                  j                  |      }t         j                  j                  d      }t        j                  |      }t        j                  ||      }t        j                  t        j                  t        j                  ||d|                   }t        j                  |g       }|j                  t        j                         |j                  t        j                  u sJ y)z)Test a problem with convolution.
        r   r
   r   r   r   N)r   randomrandnr   r   r    r)   r.   multiplyr(   r+   CLARABELstatus	UNBOUNDEDr&   )r0   Nyhr2   vobjr7   s           r9   test_conv_probzTestConvolution.test_conv_prob   sU    IIOOAq!IIOOAq!KKAGGAqMkk"&&Q!A!789zz#r"

"++
&{{bll*** IIOOAIIOOAKKNKK1kk"&&Q!A!789zz#r"

"++
&{{bll***rY   c                    t         j                  t         j                  fD ]  }t        j                  d      }t        j                  t        j
                  t        j                   |dt        j                  d|                        |dk\  g      }|j                  t         j                         |j                  t         j                  k(  rJ  y)z#Convolution with 0D input.
        )r
   r   r   r   N)r   r    r&   r   r(   r)   maxre   r+   rf   rg   OPTIMAL)r0   r[   r2   problems       r9   test_0D_convzTestConvolution.test_0D_conv   s     WWbkk* 		0DD!AjjFF4BKKA$678 a	G MMM->>RZZ///		0rY   c                    t        j                  ddg      }t        j                  d      }t        j                  ||      }t        j
                  t        j                  t        j                  |                  }|j                  d       t        j                  g d      }t        j                  ||      }t        j                  g d      }| j                  |j                  |d	       y )
Nr   r   r   rf   r   )r   r          @)r   r   r   ru   
   r   )r   r   r   r   r&   r(   r)   r*   r+   r%   r$   )r0   abproductr7   crexpecteds           r9   test_sparse_convolutionz'TestConvolution.test_sparse_convolution   s    
 HHc3Z KKN++a#zz"++bggg&678

*
%HH_%KK18801##AGGXb#ArY   )returnN)__name__
__module____qualname____doc__r:   r>   rX   ra   rn   rs   r}    rY   r9   r   r      s+    '"7H*7X98+00BrY   r   )r   numpyr   r   cvxpyr   cvxpy.problems.iterativeproblemsrG   cvxpy.settingssettingsrC   cvxpy.lin_ops.tree_matr   cvxpy.tests.base_testr   r   r   rY   r9   <module>r      s4        , ,  2 *tBh tBrY   