
    bi;                     R    d Z ddlmZmZmZ ddl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.
    )ListOptionalTupleN)Atomc                        e Zd ZdZddee   deddf fdZdeedf   fdZ	d	 Z
d fd
Zdeeej                        fdZd Z xZS )AxisAtomzM
    An abstract base class for atoms that can be applied along an axis.
    Naxiskeepdimsreturnc                 H    || _         || _        t        t        |   |       y )N)r	   r
   superr   __init__)selfexprr	   r
   	__class__s       P/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/atoms/axis_atom.pyr   zAxisAtom.__init__   s     	 h&t,    .c                    t        | j                  d   j                        }t        |      }| j                  | j
                  rdt        |      z  S dS t        | j                  t              r| j                  dk\  r| j                  n| j                  |z   }|dk  rt        d| j                   d|        | j
                  rd||<   t        |      S |d| ||dz   d z   }t        |      S | j                  D cg c]  }|dk\  r|n||z    }}t        d |D              r3t        d| j                  D cg c]  }|| k  s
| c}d    d|        | j
                  r|D ]  }d||<   	 t        |      S t        t        |            D cg c]  }||vs||    c}|dd t        |      S c c}w c c}w c c}w )	z
        Returns the shape of the atom after applying a function along an axis.
        Handles negative axis inputs by normalizing them to positive indices.
        r   N)    axis ) is out of bounds for array of dimension r   c              3   &   K   | ]	  }|d k    yw)r   Nr   ).0r	   s     r   	<genexpr>z+AxisAtom.shape_from_args.<locals>.<genexpr>8   s     -4!8-s   )listargsshapelenr	   r
   
isinstanceint
ValueErroranyrangetuple)r   r   ndimr	   axesis         r   shape_from_argszAxisAtom.shape_from_args#   s   
 TYYq\''(5z99(,4#e*$=2=		3' $		Q499DII4DDaxU499+-VW[V\]^}}d U| etuT!VW~5 U| DH99M4DAID4$;6MDM---UTYY#OT$$,D#OPQ#R"SFtfN O}}  $D"#E$K$ U| /4CJ.?Q1D=E!HQaU| N#O Rs   =F28F7
F7
	F<F<c                 2    | j                   | j                  gS )z>
        Returns the axes and the keepdims parameter.
        )r	   r
   )r   s    r   get_datazAxisAtom.get_dataB   s     		4==))r   c                 2   | j                   xt        | j                   t              r| j                   gn| j                   }| j                  d   j                  }|D ]'  }|dk  r||z  }||k\  s|dk  st        d| d|        t        t        | #          y)zB
        Checks that each axis is within the valid range.
        Nr   r   r   )	r	   r    r!   r   r&   r"   r   r   validate_arguments)r   r'   dimr	   r   s       r   r-   zAxisAtom.validate_argumentsH   s     99 ",TYY"<DII;$))D))A,##C c!8CKD3;$($uTF2[\_[`%abb	c
 	h02r   c                    | j                   | j                  d   j                  dk  rgt        j                  |d   j
                  | j                  d   j                  df      }| j                  |      }|t        j                  |      }|gS | j                  d   j                  \  }}| j                   dk(  rt        j                  ||z  |ft              }t        |      D ]  }|d   dd|f   }| j                  |      j
                  }|dgc S t        j                  |      j                         }t        j                  ||z  ||z  |z   dz
  |      }t        j                   |      |z  }	|t        j                  |||	ff||z  |f      z   } |gS t        j"                  |d         }t        j                  ||z  |ft              }t        |      D ]  }|dd|f   }| j                  |      j
                  }|dgc S t        j                  |||dz
  |z  z   |      }t        j                   |      |z  }	|t        j                  t        j                  |      d   ||	ff||z  |f      z   } |gS )aU  
        Gives the (sub/super)gradient of the atom w.r.t. each argument.

        Matrix expressions are vectorized, so the gradient is a matrix.
        Takes axis into account.

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

        Returns:
            A list of SciPy CSC sparse matrices or None.
        Nr      r   )dtype)r   )r	   r   r&   npreshapeTsize_column_gradsp	csc_arrayr   floatr$   arrayflattenlinspaceones	transpose)
r   valuesvalueDmnr(   drowcols
             r   
_axis_gradzAxisAtom._axis_gradV   sT    99		! 1 1A 5JJvay{{TYYq\->->,BCE!!%(A}LLO8 s
5 99Q<%%DAqyyA~LL!A#q7q 
:A"1IadOE))%022Ay $vHHQK//1++ac1Q3q57A6C''1,q.CBLL!c3Z121a: :A
:. s
 fQi0LL!A#q7q :A"1a4LE))%022Ay $v++aAaC7A6C''1,q.CBLL"((1+a.3*)E121a: :A: s
r   c                     t               )a  
        Gives the (sub/super)gradient of the atom w.r.t. a column argument.

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

        Args:
            value: A numeric value for a column.

        Returns:
            A SciPy sparse matrix or None.
        )NotImplementedError)r   r@   s     r   r6   zAxisAtom._column_grad   s     "##r   )NF)r   N)__name__
__module____qualname____doc__r   r!   boolr   r   r)   r+   r-   r   r7   r8   rG   r6   __classcell__)r   s   @r   r   r      se    -8C= -4 -TX -
sCx >*3-HT",,-?$@ -^$r   r   )rM   typingr   r   r   numpyr2   scipy.sparsesparser7   cvxpy.atoms.atomr   r   r   r   r   <module>rU      s)     ) (   !x$t x$r   