
    bi                     V    d Z ddlmZ ddlZddlmZ ddlm	Z
 ddlm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.
    )TupleN)Atomc                        e Zd ZdZd fdZd fdZd Zd Zdee	df   fdZ
deeef   fd	Zdefd
ZdefdZdefdZdefdZdefdZd Z xZS )sum_largestz9
    Sum of the largest k values in the expression X
    returnc                 :    || _         t        t        |   |       y N)ksuperr   __init__)selfxr
   	__class__s      R/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/atoms/sum_largest.pyr   zsum_largest.__init__   s    k4)!,    c                 ^    | j                   dk  rt        d      t        t        |           y)z,Verify that k is a positive number.
        r   z*Second argument must be a positive number.N)r
   
ValueErrorr   r   validate_arguments)r   r   s    r   r   zsum_largest.validate_arguments#   s)     66Q;IJJk435r   c                    |d   j                         }t        |      }t        t        j                  | j
                              }| j
                  |z
  }|dkD  r<t        j                  | t        ||      dz
        d| }||   j                         }nd}|dkD  r>||k  r9t        j                  | |      d|dz    }||   j                         }	|||	z  z  }|S )z
        Returns the sum of the k largest entries of the matrix.
        For non-integer k, uses linear interpolation.
        r      kthNg        )	flattenlenintnpfloorr
   argpartitionminsum)
r   valuesvaluenk_floork_fracindicesresultindices_next
next_values
             r   numericzsum_largest.numeric*   s    
 q	!!#Jbhhtvv&''!Q;oouf#gq/A2EFxPG7^'')FF A:'A+??E6w?1ML|,002Jfz))Fr   c                    t        j                  |d   j                         j                        }t	        |      }t        t        j                  | j                              }| j                  |z
  }t        j                  | j                  d   j                  d   | j                  d   j                  d   z  df      }|dkD  r-t        j                  | t        ||      dz
        d| }d||<   |dkD  rC||k  r>t        j                  | |      d|dz    }|t        j                  ||            }	|||	<   t        j                   |      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   N)intffrom_2D_to_1Dr   Tr   r   r   r   r
   zerosargsshaper   r   argminsp	csc_array)
r   r!   r"   r#   r$   r%   Dr&   r(   next_idxs
             r   _gradzsum_largest._gradE   s#    ""6!9#4#4#6#8#89Jbhhtvv&''!HHdiil((+DIIaL,>,>q,AA1EFQ;oouf#gq/A2EFxPGAgJA:'A+??E6w?1ML#BIIeL.A$BCH AhKQ  r   .c                     t               S )z8Returns the (row, col) shape of the expression.
        )tupler   s    r   shape_from_argszsum_largest.shape_from_argse   s     wr   c                 v    | j                   d   j                         | j                   d   j                         fS )zCReturns sign (is positive, is negative) of the expression.
        r   )r0   	is_nonneg	is_nonposr:   s    r   sign_from_argszsum_largest.sign_from_argsj   s1     		!&&($))A,*@*@*BCCr   c                      y)zIs the atom convex?
        T r:   s    r   is_atom_convexzsum_largest.is_atom_convexp        r   c                      y)zIs the atom concave?
        FrA   r:   s    r   is_atom_concavezsum_largest.is_atom_concaveu        r   c                      y)z;Is the composition non-decreasing in argument idx?
        TrA   r   idxs     r   is_incrzsum_largest.is_incrz   rC   r   c                      y)z;Is the composition non-increasing in argument idx?
        FrA   rH   s     r   is_decrzsum_largest.is_decr   rF   r   c                 :    t        d | j                  D              S )z&Is the atom piecewise linear?
        c              3   <   K   | ]  }|j                           y wr	   )is_pwl).0args     r   	<genexpr>z%sum_largest.is_pwl.<locals>.<genexpr>   s     5C3::<5s   )allr0   r:   s    r   rO   zsum_largest.is_pwl   s     5499555r   c                     | j                   gS )z!Returns the parameter k.
        )r
   r:   s    r   get_datazsum_largest.get_data   s     xr   )r   N)__name__
__module____qualname____doc__r   r   r*   r7   r   r   r;   boolr?   rB   rE   rJ   rL   rO   rU   __classcell__)r   s   @r   r   r      s    -66!@sCx 
DdDj 1 D 
 
d 
d 
6 6
r   r   )rY   typingr   numpyr   scipy.sparsesparser3   cvxpy.interface	interfacer,   cvxpy.atoms.atomr   r   rA   r   r   <module>rc      s)         !r$ rr   