
    biI                         d Z ddlZddlmZmZ ddlZddlmZ	 ddl
mc mZ ddlmc 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)ListTuple)AffAtom)
Constraint)is_param_freec                        e Zd ZdZd fdZej                  d        ZddZde	fdZ
de	fdZdeeef   fdZdee	e	f   fd	Zde	fd
Zde	fdZ	 ddeedf   deej&                  ee   f   fdZ xZS )conva   1D discrete convolution of two vectors.

    The discrete convolution :math:`c` of vectors :math:`a` and :math:`b` of
    lengths :math:`n` and :math:`m`, respectively, is a length-:math:`(n+m-1)`
    vector where

    .. math::

        c_k = \sum_{i+j=k} a_ib_j, \quad k=0, \ldots, n+m-2.

    Parameters
    ----------
    lh_expr : Constant
        A constant 1D vector or a 2D column vector.
    rh_expr : Expression
        A 1D vector or a 2D column vector.
    returnc                 b    t        j                  dt               t        t        |   ||       y )Nz)conv is deprecated. Use convolve instead.)warningswarnDeprecationWarningsuperr	   __init__)selflh_exprrh_expr	__class__s      R/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/atoms/affine/conv.pyr   zconv.__init__2   s$    ACUVdD"7G4    c                     t        t        t        j                  |            }t	        j
                  |d   |d         }|d   j                  dk(  s|d   j                  dk(  r	|dddf   S |S )!Convolve the two values.
        r         N)listmapintffrom_2D_to_1Dnpconvolvendim)r   valuesflat_valuesoutputs       r   numericzconv.numeric6   se     3t116:;[^[^<!9>>Q&)..A"5!T'?"Mr   c                     | j                   d   j                         r| j                   d   j                         st        d      | j                   d   j                         st        d      y)KChecks that both arguments are vectors, and the first is constant.
        r   r   z1The arguments to conv must resolve to a 1-d array,The first argument to conv must be constant.N)args	is_vector
ValueErroris_constantr   s    r   validate_argumentszconv.validate_argumentsA   s\     yy|%%'tyy|/E/E/GPQQyy|'')KLL *r   c                     t         j                  j                         r,| j                  d   }|j	                         xr t        |      S | j                  d   j	                         S zIs the atom convex?
        r   uscopesdpp_scope_activer)   r,   r   r   cs     r   is_atom_convexzconv.is_atom_convexI   N     88$$&		!A==?7}Q'7799Q<++--r   c                 "    | j                         S zIs the atom concave?
        r7   r-   s    r   is_atom_concavezconv.is_atom_concaveT        ""$$r   c                     | j                   d   j                  }| j                   d   j                  }||z   dz
  }| j                   d   j                  dk(  s| j                   d   j                  dk(  r|dfS |fS )0The sum of the argument dimensions - 1.
        r   r   r   )r)   sizer!   )r   	lh_length	rh_lengthoutput_lengths       r   shape_from_argszconv.shape_from_argsY   sx     IIaL%%	IIaL%%	!I-199Q<!TYYq\%6%6!%;!1%%!##r   c                 v    t         j                  j                  | j                  d   | j                  d         S zSame as times.
        r   r   r2   signmul_signr)   r-   s    r   sign_from_argszconv.sign_from_argsd   )     vvtyy|TYYq\::r   c                 <    | j                   d   j                         S z;Is the composition non-decreasing in argument idx?
        r   r)   	is_nonnegr   idxs     r   is_incrzconv.is_incri        yy|%%''r   c                 <    | j                   d   j                         S z;Is the composition non-increasing in argument idx?
        r   r)   	is_nonposrP   s     r   is_decrzconv.is_decrn   rS   r   shape.c                 @    t        j                  |d   |d   |      g fS at  Convolve two vectors.

        Parameters
        ----------
        arg_objs : list
            LinExpr for each argument.
        shape : tuple
            The shape of the resulting expression.
        data :
            Additional data required by the atom.

        Returns
        -------
        tuple
            (LinOp for objective, list of constraints)
        r   r   lur	   r   arg_objsrY   datas       r   graph_implementationzconv.graph_implementations   $    & Xa[%8"==r   r
   NN)__name__
__module____qualname____doc__r   r   numpy_numericr%   r.   boolr7   r<   r   intrD   rJ   rR   rX   loLinOpr   r   ra   __classcell__)r   s   @r   r	   r	      s    $5  M	. 	.% %
	$sCx 	$;dDj 1 ;
(d (
(d ( 6:>$S#X>	rxxj))	*>r   r	   c                       e Zd ZdZej
                  d        ZddZdefdZ	defdZ
deeef   fdZdeeef   fd	Zdefd
ZdefdZ	 ddeedf   deej$                  ee   f   fdZy)r    a   1D discrete convolution of two vectors.

    The discrete convolution :math:`c` of vectors :math:`a` and :math:`b` of
    lengths :math:`n` and :math:`m`, respectively, is a length-:math:`(n+m-1)`
    vector where

    .. math::

        c_k = \sum_{i+j=k} a_ib_j, \quad k=0, \ldots, n+m-2.

    Matches numpy.convolve

    Parameters
    ----------
    lh_expr : Constant
        A constant scalar or 1D vector.
    rh_expr : Expression
        A scalar or 1D vector.
    c                 :    t        j                  |d   |d         S )r   r   r   )r   r    )r   r"   s     r   r%   zconvolve.numeric   s     {{6!9fQi00r   r
   Nc                     | j                   d   j                  dk  r| j                   d   j                  dk  st        d      | j                   d   j                         st        d      y)r'   r   r   z+The arguments to conv must be scalar or 1D.r(   N)r)   r!   r+   r,   r-   s    r   r.   zconvolve.validate_arguments   s`     yy|  A%TYYq\->->!-CJKKyy|'')KLL *r   c                     t         j                  j                         r,| j                  d   }|j	                         xr t        |      S | j                  d   j	                         S r0   r1   r5   s     r   r7   zconvolve.is_atom_convex   r8   r   c                 "    | j                         S r:   r;   r-   s    r   r<   zconvolve.is_atom_concave   r=   r   c                 x    | j                   d   j                  }| j                   d   j                  }||z   dz
  fS )r?   r   r   )r)   r@   )r   rA   rB   s      r   rD   zconvolve.shape_from_args   s=     IIaL%%	IIaL%%	I%)++r   c                 v    t         j                  j                  | j                  d   | j                  d         S rF   rG   r-   s    r   rJ   zconvolve.sign_from_args   rK   r   c                 <    | j                   d   j                         S rM   rN   rP   s     r   rR   zconvolve.is_incr   rS   r   c                 <    | j                   d   j                         S rU   rV   rP   s     r   rX   zconvolve.is_decr   rS   r   rY   .c                 @    t        j                  |d   |d   |      g fS r[   r\   r^   s       r   ra   zconvolve.graph_implementation   rb   r   rc   rd   )re   rf   rg   rh   r   ri   r%   r.   rj   r7   r<   r   rk   rD   rJ   rR   rX   rl   rm   r   r   ra    r   r   r    r       s    , 1 1
M	. 	.% %
,sCx ,;dDj 1 ;
(d (
(d ( 6:>$S#X>	rxxj))	*>r   r    )rh   r   typingr   r   numpyr   cvxpy.interface	interfacer   cvxpy.lin_ops.lin_oplin_opslin_oprl   cvxpy.lin_ops.lin_utils	lin_utilsr]   cvxpy.utilities	utilitiesr2   cvxpy.atoms.affine.affine_atomr   cvxpy.constraints.constraintr   %cvxpy.expressions.constants.parameterr   r	   r    ry   r   r   <module>r      sM         ! ! $ $  2 3 ?g>7 g>T^>w ^>r   