
    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  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.
    )OptionalTupleN)AffAtom)reshape)vec)
Constraint)
Expression)	key_utilsc                        e Zd ZdZdd fdZdefdZdefdZd Zd Z	de
edf   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 )indexa  Indexing/slicing into an Expression.

    CVXPY supports NumPy-like indexing semantics via the Expression
    class' overloading of the ``[]`` operator. This is a low-level class
    constructed by that operator, and it should not be instantiated directly.

    Parameters
    ----------
    expr : Expression
        The expression indexed/sliced into.
    key :
        The index/slicing key (i.e. expr[key[0],key[1]])
    
    Examples
    --------
    >>> import cvxpy as cp
    >>> import numpy as np
    >>> x = cp.Variable((2, 3, 4))
    >>> x[..., 2].shape
    (2, 3)
    >>> x[..., np.newaxis].shape
    (2, 3, 4, 1)
    >>> x[1, 2].shape
    (4,)
    returnc                     |-|| _         t        j                  ||j                        | _        n|| _         || _        t
        t        |   |       y N)	_orig_keykuvalidate_keyshapekeysuperr   __init__)selfexprr   orig_key	__class__s       S/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/atoms/affine/index.pyr   zindex.__init__:   sC     DNsDJJ7DH%DNDHeT#D)    c                      y)zIs the atom log-log convex?T r   s    r   is_atom_log_log_convexzindex.is_atom_log_log_convexD       r   c                      y)zIs the atom log-log concave?Tr   r   s    r   is_atom_log_log_concavezindex.is_atom_log_log_concaveH   r!   r   c                     ddt        | j                        dz
  z  z   dz   }| j                  d   j                         |t	        j
                  | j                        z  z   S )z.String representation of the index expression.z[%sz, %s   ]r   )lenr   argsnamer   to_str)r   	inner_strs     r   r)   z
index.nameL   sP    FCM!O44s:	yy|  "Y4881D%DDDr   c                 &    |d   | j                      S )z-Returns the index/slice into the given value.r   )r   r   valuess     r   numericzindex.numericQ   s    ay((r   .c                     t        j                  | j                  | j                  | j                  d   j                        S )*Returns the shape of the index expression.r   )r   r   r   r   r(   r   s    r   shape_from_argszindex.shape_from_argsU   s+    xx$..$))A,2D2DEEr   c                 2    | j                   | j                  gS )z&Returns the (row slice, column slice).)r   r   r   s    r   get_datazindex.get_dataY   s    $..))r   r   c                 D    t        j                  |d   ||d         }|g fS )  Index/slice into the expression.

        Parameters
        ----------
        arg_objs : list
            LinExpr for each argument.
        shape : tuple
            The shape of the resulting expression.
        data : tuple
            A tuple of slices.
        r   )lur   )r   arg_objsr   dataobjs        r   graph_implementationzindex.graph_implementation]   s(     hhx{E473Ryr   r   )r   N)__name__
__module____qualname____doc__r   boolr    r#   r)   r/   r   intr2   listr4   loLinOpr   r;   __classcell__r   s   @r   r   r      s    4*  E
)FsCx F*$ *
 6:$S#X	rxxj))	*r   r   c            
            e Zd ZdZdeddf fdZdefdZdefdZde	fdZ
d	 Zdeed
f   fdZdefdZedeeej(                        fd       Z	 ddedeed
f   deej.                  ee   f   fdZ xZS )special_indexzIndexing using logical indexing or a list of indices.

    Parameters
    ----------
    expr : Expression
        The expression being indexed/sliced into.
    key : tuple
        ndarrays or lists.
    r   r   Nc                 0   || _         t        j                  |      }t        j                  |j
                        }t        j                  ||j                  d      }||   | _        | j                  j                  | _	        t        t        | 3  |       y )NForder)r   r   cast_to_constnparangesizer   r   _select_mat_shaper   rH   r   )r   r   r   idx_matr   s       r   r   zspecial_index.__init__z   sp    ""4())DII&**Wdjj<"3<&&,,mT+D1r   c                      y)z$Is the atom log-log convex?
        Tr   r   s    r   r    z$special_index.is_atom_log_log_convex        r   c                      y)z%Is the atom log-log concave?
        Tr   r   s    r   r#   z%special_index.is_atom_log_log_concave   rU   r   c                     t        j                  | j                        }| j                  d   j	                          d| dS )z6String representation of the special index expression.r   [r&   )r   special_key_to_strr   r(   r)   )r   key_strs     r   r)   zspecial_index.name   s;    ''1))A,##%&ay22r   c                 &    |d   | j                      S )z6Returns the index/slice into the given value.
        r   r   r-   s     r   r/   zspecial_index.numeric   s     ay""r   .c                     | j                   S )r1   )rR   r   s    r   r2   zspecial_index.shape_from_args   s    {{r   c                     | j                   gS )zReturns the key.r\   r   s    r   r4   zspecial_index.get_data   s    zr   c                 N   t        j                  | j                  | j                  j                  d      }t	        j
                  | j                  d   j                  d      }t        ||   t        | j                  d   d      z  | j                  d      }|j                  S )zGives the (sub/super)gradient of the expression w.r.t. each variable.

        Matrix expressions are vectorized, so the gradient is a matrix.
        None indicates variable values unknown or outside domain.
        rJ   rK   r   cscformat)
rN   r   rQ   rP   sp	eye_arrayr(   r   rR   grad)r   
select_vecidentitylowereds       r   re   zspecial_index.grad   s     ZZ 0 0$2B2B2G2GsS
<<		! 1 1%@Z 3tyy|3#??KK

 ||r   r8   r   c                    | j                   }| j                   j                  }t        j                  ||j                  d      }|d   }t        j                  | j                  d   j                  d      }t        j                  || j                  d   j                  f      }	||   }
t        j                  |
|
j                  d      }t        j                  ||	|
j                  d   f      }t        j                  ||      }|g fS )r6   rJ   rK   r   r`   ra   T)sparse)rQ   r   rN   r   rP   rc   rd   r(   r7   create_constmul_expr)r   r8   r   r9   
select_matfinal_shaperf   argrg   vec_argmul_mat	mul_constrl   r:   s                 r   r;   z"special_index.graph_implementation   s     %%
&&,,ZZ
JOO3G
qk<<		! 1 1%@**S499Q<#4#4"67:&OOGW]]4H	;;y'GMM!4D3FGjj;/Ryr   r   )r<   r=   r>   r?   r	   r   r@   r    r#   strr)   r/   r   rA   r2   rB   r4   propertyr   rc   	csc_arrayre   rC   rD   r   r;   rE   rF   s   @r   rH   rH   o   s    2Z 2 2 
 
3c 3
#
sCx $  htBLL12  $ "&&*#(c? +0$z:J0J*Kr   rH   )r?   typingr   r   numpyrN   scipy.sparserj   rc   cvxpy.lin_ops.lin_oplin_opslin_oprC   cvxpy.lin_ops.lin_utils	lin_utilsr7   cvxpy.atoms.affine.affine_atomr   cvxpy.atoms.affine.reshaper   cvxpy.atoms.affine.vecr   cvxpy.constraints.constraintr   cvxpy.expressions.expressionr	   cvxpy.utilitiesr
   r   r   rH   r   r   r   <module>r      sP    #   ! ! $ $ 2 . & 3 3 +MG M`[G [r   