
    bi                     f    d Z ddlmZmZ ddl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y)a,  
Copyright, the CVXPY authors

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

    https://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)Atom)is_param_affinec                       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 ZdefdZedeej,                     deej,                  ej,                  f   fd       Z xZS )dotsorta   Computes :math:`\langle sort\left(vec(X)\right), sort\left(vec(W)\right) \rangle`,
    where :math:`X` is an expression and :math:`W` is constant.

    | Both arguments are flattened, i.e., we define :math:`x=vec(X)`, :math:`w=vec(W)`.
    | If the length of :math:`w` is less than the length of :math:`x`,
     it is conceptually padded with zeros.
    | When the length of :math:`w` is larger than the length of :math:`x`, an exception is raised.

    `dotsort` is a generalization of `sum_largest` and `sum_smallest`:

    | `sum_largest(x, 3)` is equivalent to `dotsort(x,[1,1,1])`
    | `sum_largest(x, 3.5)` is equivalent to `dotsort(x,[1,1,1,0.5])`
    | `sum_smallest(x,3)` is equivalent to `-dotsort(x, [-1,-1,-1])`

    When the constant argument is not a boolean vector, `dotsort` can be considered as a
    weighted sum of :math:`x`, where the largest weight is
    assigned to the largest entry in :math:`x`, etc..
    returnc                 .    t         t        |   ||       y )N)superr   __init__)selfXW	__class__s      N/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/atoms/dotsort.pyr   zdotsort.__init__/   s    gt%a+    c                     | j                   d   j                         st        d      | j                   d   j                  | j                   d   j                  k  rt        d      t        t
        |           y )N   z The W argument must be constant.r   z8The size of of W must be less or equal to the size of X.)argsis_constant
ValueErrorsizer   r   validate_arguments)r   r   s    r   r   zdotsort.validate_arguments2   s`    yy|'')?@@99Q<tyy|000WXXgt/1r   c                     | j                  |      \  }}t        j                  |      t        j                  |      z  S )z
        Returns the inner product of the sorted values of vec(X) and the sorted
        (and potentially padded) values of vec(W).
        )_get_args_from_valuesnpsort)r   valuesxw_paddeds       r   numericzdotsort.numeric:   s4    
 0088wwqzBGGH---r   c                     | j                  |      \  }}t        j                  |      }t        |      }t        j                  |      }t        j                  ||t        j                  |      ff|df      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   )shape)r   r   argsortlenr   sp	csc_arrayzeros)r   r   r   r    indicesnsorted_ws          r   _gradzdotsort._gradB   sf     0088**Q-F778$h"((1+(>?1vNOOr   .c                     t               S )z8Returns the (row, col) shape of the expression.
        )tupler   s    r   shape_from_argszdotsort.shape_from_argsU   s     wr   c                 *   | j                   d   j                         }| j                   d   j                         }| j                   d   j                         }| j                   d   j                         }|xr |xs |xr |}|xr |xs |xr |}||fS )zCReturns sign (is positive, is negative) of the expression.
        r   r   )r   	is_nonneg	is_nonpos)r   x_posx_negw_posw_negis_positiveis_negatives          r   sign_from_argszdotsort.sign_from_argsZ   s     		!&&(		!&&(		!&&(		!&&(<EOe<EOeK''r   c                     t         j                  j                         r;| j                  d   }| j                  d   }|j	                         xs t        |      S y)zIs the atom convex?
        r   r   T)uscopesdpp_scope_activer   r   r   )r   r   r   s      r   is_atom_convexzdotsort.is_atom_convexi   sG     88$$&		!A		!A==?8oa&88r   c                      y)zIs the atom concave?
        F r/   s    r   is_atom_concavezdotsort.is_atom_concavet   s     r   c                 <    | j                   d   j                         S )z;Is the composition non-decreasing in argument idx?
        r   )r   r2   r   idxs     r   is_incrzdotsort.is_incry        yy|%%''r   c                 <    | j                   d   j                         S )z;Is the composition non-increasing in argument idx?
        r   )r   r3   rD   s     r   is_decrzdotsort.is_decr~   rG   r   c                      y)z2Returns None, W is stored as an argument.
        NrA   r/   s    r   get_datazdotsort.get_data        r   c                      y)z,Is the expression piecewise linear?
        TrA   r/   s    r   is_pwlzdotsort.is_pwl   rL   r   r   c                     | d   j                         }| d   j                         }t        j                  |      }||d t        |       ||fS )Nr   r   )flattenr   
zeros_liker%   )r   r   wr    s       r   r   zdotsort._get_args_from_values   sP     1I1I==##a&({r   )r	   N)__name__
__module____qualname____doc__r   r   r!   r,   r   intr0   boolr:   r?   rB   rF   rI   rK   rN   staticmethodr   r   ndarrayr   __classcell__)r   s   @r   r   r      s    &,2.P&sCx 
(dDj 1 (	 	 
(d (
(d (

 
 d2::&6 RZZ+, r   r   )rV   typingr   r   numpyr   scipy.sparsesparser&   cvxpy.utilities	utilitiesr<   cvxpy.atoms.atomr   %cvxpy.expressions.constants.parameterr   r   rA   r   r   <module>rd      s,         ! Azd zr   