
    bi                         d 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 ddlmZ dd	lmZ d
ededej:                  fdZ G d de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.
    )ListTupleN)AffAtom)MulExpression)AxisAtom)
Constraint)
Expression)Variabledimaxisreturnc                     t        j                  t        j                  |       t        j                  | dz
         gddg| | fd      }|dk(  r|S |j                  S )aK  Return a sparse matrix representation of first order difference operator.

    Parameters
    ----------
    dim : int
       The length of the matrix dimensions.
    axis : int
       The axis to take the difference along.

    Returns
    -------
    sp.csc_array
        A square matrix representing first order difference.
       r   csc)offsetsshapeformat)spdiags_arraynponesT)r   r   mats      T/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/atoms/affine/cumsum.pyget_diff_matr      sX     .."''#,q)9(9:QGs!C !)3&&    c                        e Zd ZdZddededdf fdZej                  d        Z	d Z
deed	f   fd
Zd Zd Z	 ddeed	f   deej                   ee   f   fdZ xZS )cumsumz
    Cumulative sum of the elements of an expression.

    Attributes
    ----------
    expr : CVXPY expression
        The expression being summed.
    axis : int
        The axis to sum across if 2D.
    exprr   r   Nc                 .    t         t        |   ||       y N)superr   __init__)selfr    r   	__class__s      r   r$   zcumsum.__init__?   s    fd$T40r   c                 J    t        j                  |d   | j                        S )zW
        Returns the cumulative sum of elements of an expression over an axis.
        r   r   )r   r   r   )r%   valuess     r   numericzcumsum.numericB   s    
 yy33r   c                 R    | j                   d   j                  dkD  rt        d      y )Nr      zdcumsum is only implemented for 1D or 2D arrays and might not work as expected for higher dimensions.)argsndimUserWarningr%   s    r   validate_argumentszcumsum.validate_argumentsI   s/    99Q<q :  !r   .c                 4    | j                   d   j                  S )zThe same as the input.r   )r-   r   r0   s    r   shape_from_argszcumsum.shape_from_argsP   s    yy|!!!r   c                    |d   j                   | j                     }t        j                  t	        j
                  t	        j                  ||f                  }t        | j                  d   j                         }| j                  dk(  r!t        ||      j                  |      d   }|gS t        ||j                        j                  |      d   }|gS )a+  Gives the (sub/super)gradient of the atom w.r.t. each argument.

        Matrix expressions are vectorized, so the gradient is a matrix.

        Args:
            values: A list of numeric values for the arguments.

        Returns:
            A list of SciPy CSC sparse matrices or None.
        r   r   )r   r   r   	csc_arrayr   trilr   r
   r-   r   _gradr   )r%   r)   r   r   vargrads         r   r7   zcumsum._gradT   s     Qioodii(ll277277C:#678tyy|))*99> c*008;D v !cee,226:1=Dvr   c                     | j                   gS )zReturns the axis being summed.r(   r0   s    r   get_datazcumsum.get_datah   s    		{r   r   c                 (   t        j                  |      }|d   }||   }t        ||      }t        j                  |||fd      }|dk(  rt        j                  ||      }nt        j
                  ||      }|t        j                  |d   |      gfS )a  Cumulative sum via difference matrix.

        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   T)sparse)lu
create_varr   create_constmul_expr	rmul_expr	create_eq)	r%   arg_objsr   dataYr   r   diff_matdiffs	            r   graph_implementationzcumsum.graph_implementationl   s    * MM% AwDkT*??8c3ZE19;;x+D<<8,DBLL!d3455r   )r   r"   )__name__
__module____qualname____doc__r	   intr$   r   numpy_numericr*   r1   r   r3   r7   r;   loLinOpr   r   rI   __classcell__)r&   s   @r   r   r   4   s    	1Z 1s 14 1 4 4"sCx "(
 6:6$S#X6	rxxj))	*6r   r   ) rM   typingr   r   numpyr   scipy.sparser=   r   cvxpy.lin_ops.lin_oplin_opslin_oprP   cvxpy.lin_ops.lin_utils	lin_utilsr>   cvxpy.atoms.affine.affine_atomr   #cvxpy.atoms.affine.binary_operatorsr   cvxpy.atoms.axis_atomr   cvxpy.constraints.constraintr   cvxpy.expressions.expressionr	   cvxpy.expressions.variabler
   rN   r5   r   r    r   r   <module>rb      sa       ! ! $ $ 2 = * 3 3 /'c ' ' '*V6Wh V6r   