
    bi                         d Z ddlZddlmZ ddl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  G d d	ee      Z ee      dd
ee   defd       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)wraps)Optional)AffAtom)AxisAtom)
Constraintc            
            e Zd ZdZdd fdZdefdZdefdZd Z	 dde	e
j                     deed	f   dee
j                  e	e   f   fd
Z xZS )Sumar  Sum the entries of an expression over a given axis.

    Parameters
    ----------
    expr : Expression
        The expression to sum the entries of.
    axis : None or int or tuple of ints, optional
        The axis or axes along which to sum. The default (axis=None) will
        sum all of the elements of the expression.

        .. versionadded:: 1.6.0

        If axis is a tuple of ints, a sum is performed on all of the axes
        specified in the tuple.
    keepdims : bool, optional
        If set to true, the axes which are summed over remain in the output
        as dimensions with size one.

    Examples
    --------
    >>> import cvxpy as cp
    >>> x = cp.Variable((2, 3, 4))
    >>> expr = cp.sum(x)
    >>> expr.shape
    ()
    >>> expr = cp.sum(x, axis=0)
    >>> expr.shape
    (3, 4)
    >>> expr = cp.sum(x, axis=(1, 2))
    >>> expr.shape
    (2,)
    returnc                 2    t         t        |   |||       y )Naxiskeepdims)superr	   __init__)selfexprr   r   	__class__s       Q/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/atoms/affine/sum.pyr   zSum.__init__@   s    c4!$TH!E    c                      y)zIs the atom log-log convex?T r   s    r   is_atom_log_log_convexzSum.is_atom_log_log_convexC   s    r   c                      y)zIs the atom log-log concave?Fr   r   s    r   is_atom_log_log_concavezSum.is_atom_log_log_concaveG   s    r   c                 L   t        j                  |d         r\t        j                  |d   j	                  | j
                              }| j                  s| j
                  |j                         }|S t        j                  |d   | j
                  | j                        }|S )zSums the entries of value.r   )r   r   )intf	is_sparsenpasarraysumr   r   flatten)r   valuesresults      r   numericzSum.numericK   s{    >>&)$ZZq	499 =>F==TYY%:)  VVF1IDIINFr   arg_objsshape.c                    |\  }}t        |d   j                        dkD  s|dvr t        j                  |d   |||      }|g fS |t        j                  |d   |      }|g fS |dk(  rq|r|d   j                  d   df}n|d   j                  d   f}t        j                  t        j                  |      |      }t        j                  |d   ||      }|g fS |rd|d   j                  d   f}n|d   j                  d   f}t        j                  t        j                  |      |      }t        j                  ||d   |      }|g fS )a`  
        Sum the linear expression's entries.

        Parameters
        ----------
        arg_objs : list
            LinExpr for each argument.
        shape : int or tuple of ints
            The shape of the resulting expression.
        data : [axis, keepdims] or None
            The axis and keepdims parameters of the sum expression.
        r      >   Nr      )r'   r   r   )r'   r*   )	lenr'   lusum_entriescreate_constr   ones	rmul_exprmul_expr)	r   r&   r'   datar   r   objconst_shaper/   s	            r   graph_implementationzSum.graph_implementationU   s]     h x{  !A%\)A..!ExXC$ Ry! |nnXa[> Ry #+A;#4#4Q#7";K#+A;#4#4Q#7"9Krww{';[Ill8A;e< Ry #$hqk&7&7&:";K#+A;#4#4Q#7"9Krww{';[Ikk$U;Ryr   NF)r
   N)N)__name__
__module____qualname____doc__r   boolr   r   r%   listloLinOptupleintr   r5   __classcell__)r   s   @r   r	   r	      ss    BF   "&&&*288n&#(c?& +0$z:J0J*K&r   r	   r   r   c                 f    t        | t              rt        j                  |       S t	        | ||      S )z 
    Wrapper for Sum class.
    )
isinstancer<   builtinsr!   r	   )r   r   r   s      r   r!   r!   ~   s,    
 $||D!!4x((r   r6   )r:   rD   	functoolsr   typingr   numpyr   cvxpy.interface	interfacer   cvxpy.lin_ops.lin_oplin_opslin_opr=   cvxpy.lin_ops.lin_utils	lin_utilsr,   cvxpy.atoms.affine.affine_atomr   cvxpy.atoms.axis_atomr   cvxpy.constraints.constraintr   r	   r@   r;   r!   r   r   r   <module>rR      sk         ! ! $ $ 2 * 3](G ]@ s)HSM )D ) )r   