
    bi                         d Z ddlZddlmZmZ ddlZdeedf   fdZd Z	dee
   fd	Zdd
ZddefdZde
fdZd Zd ZdefdZdeedf   deedf   fdZd ZdefdZd Zd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)OptionalTupleshape.c                    t        |       } t        d | D              rt        d      t        |       dk(  rt        d      t	        d | D              }t        |       |z
  }|t        |      kD  rt        d      |t        |      k  r2t        t        |       t        ddd      gt        |      |z
  z  z         } t        d t        | |t        t        |                  D              S )	a   Check if the key is a valid index.

    Args:
        key: The key used to index/slice.
        shape: The shape (rows, cols) of the expression.

    Returns:
        The key as a tuple of slices.

    Raises:
        Error: Index/slice out of bounds.
    c              3     K   | ]z  }t        |t              xsd t        |t              xrR t        |j                  t              xs6 t        |j                  t              xs t        |j
                  t               | y wN)
isinstancefloatslicestartstopstep).0ks     T/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/utilities/key_utils.py	<genexpr>zvalidate_key.<locals>.<genexpr>)   sm      6 -. a (Jq%$8 %'qww& &qvvu%&qvvu%( 6s   B Bzfloat is an invalid index type.r   zAn index cannot be empty.c              3   &   K   | ]	  }|d  y w)N    r   elems     r   r   zvalidate_key.<locals>.<genexpr>1   s     74$,Q7s   z Too many indices for expression.Nc              3   >   K   | ]  \  }}}t        |||        y wr   )format_slice)r   slcdimis       r   r   zvalidate_key.<locals>.<genexpr>9   s     a{sCc3*as   )
to_tupleany
IndexErrorlensumtuplelistr   ziprange)keyr   
none_countslicess       r   validate_keyr)      s     3-C
 6 256 6 :;;
3x1}4557377JX
"FE
;<<	#e*	DItT4!8 93u:;N OOPac#ueTWX]T^N_>`aaa    c                 ,    t        | t              r| S | fS )z'Convert key to tuple if necessary.
    )r	   r"   r&   s    r   r   r   <   s     #u
vr*   returnc           	         | yt        | t              rt        | j                  d      }|dk(  rt	        d      |dkD  rkt        j                  t        t        | j                  d      |      d|      }t        j                  t        t        | j                  |      |      d|      }nxt        j                  t        t        | j                  |dz
        |      d|dz
        }t        j                  t        t        | j                  | dz
        |d      d|dz
        }t        |||      S t        |       }t        ||      } d| cxk  r|k  rn nt        | | dz   d      S t        d|||fz        )a  Converts part of a key into a slice with a start and step.

    Uses the same syntax as numpy.

    Args:
        key_val: The value to convert into a slice.
        dim: The length of the dimension being sliced.

    Returns:
        A slice with a start and step.
    Nr   r   zstep length cannot be 0Tz3Index %i is out of bounds for axis %i with size %i.)r	   r   to_intr   
ValueErrornpclipwrap_neg_indexr   r   r   )key_valr   axisr   r   r   orig_key_vals          r   r   r   E   sO    	GU	#gllA&19677AXGGN6'--+CSI1cRE77>&s*CSI1cRDGGN6'--Q+GMrSVWXSXYE77>&tAv*FTRTVX[\]X]^DUD$'' g s3#'A+q11EW[]`Haa r*   c                      | |S t        |       S )z+Convert everything but None to an int.
    )int)valnone_vals     r   r0   r0   j   s     {3xr*   neg_stepc                 ,    | | dk  r|r| dk(  s| |z  } | S )zConverts a negative index into a positive index.

    Args:
        index: The index to convert. Can be None.
        dim: The length of the dimension being indexed.
    r   r/   r   )indexr   r<   s      r   r4   r4   s   s'     UQY"Lr*   c                 "    t        | | dz   d      S )zConverts an index to a slice.

    Args:
        idx: int
            The index.

    Returns:
    slice
        A slice equivalent to the index.
    r   N)r   )idxs    r   index_to_slicerA      s     c!eT""r*   c                 ,   t        |       rt        | j                        S | j                  | j                  fD cg c]  }t	        |       }}| j
                  )| j
                  dk7  r|d   d|d   d| j
                  S |d   d|d   S c c}w )z$Converts a slice into a string.
    r   r   :)is_single_indexstrr   r   none_to_emptyr   )r   r:   	endpointss      r   slice_to_strrH      s     s399~03		388/DEs#EIE
xxA&q\9Q<BB#A,	!55	 Fs   Bc                     | y| S )z&Converts None to an empty string.
     r   )r:   s    r   rF   rF      s     {
r*   c                     | j                   d}n| j                   }| j                  duxr, | j                  duxr | j                  |z   | j                  k\  S )z/Is the slice equivalent to a single index?
    Nr   )r   r   r   )r   r   s     r   rD   rD      sX     xxxx99D  %%		DCHH$%r*   c                    t        |      }g }t        t        |            D ]  }| |   |j                  d       t	        t        j                  | |   j                  | |   j                  z
  | |   j                  z              }|dkD  s"|t        |      k\  st        ||   t              s|j                  t        |d              t        |      S )zFinds the dimensions of a sliced expression.

    Args:
        key: The key used to index/slice.
        shape: The shape (row, col) of the expression.

    Returns:
        The dimensions of the expression as (rows, cols).
    r   r   )r   r%   r    appendr9   r2   ceilr   r   r   r	   r   maxr"   )r&   orig_keyr   dimsr   sizes         r   r   r      s     !HD3u: *q6>KKNrwwAc!fll :CFKKGHIDax1H-HQK1OCaL)* ;r*   c                 &    t        d | D              S )z4Converts a key (i.e. two slices) into a string.
    c              3   2   K   | ]  }t        |        y wr   )rH   r   s     r   r   zto_str.<locals>.<genexpr>   s     4d#4s   )r"   r,   s    r   to_strrU      s     4444r*   c                     t        |       D ]:  }t        |t        j                  t        f      r$t        j                  |      r: y y)z>Does the key contain a list, ndarray, or logical ndarray?
    TF)r   r	   numbersNumberr   r2   isscalar)r&   r   s     r   is_special_slicerZ      s=      4'..%!89R[[=N r*   c                 6   g }| D ]  }t        |t        j                  t        f      r|j	                  t        |             >t        |t              r|j	                  t        |             i|j	                  t        |              dj                  |      S )z2Converts a special key to a string representation.z, )
r	   r2   ndarrayr#   rM   pprint_sequencer   rH   rE   join)r&   key_strsr   s      r   special_key_to_strr`      su    H $a"**d+,OOOA./5!OOLO,OOCF#$ 99Xr*   c                     t        |       |kD  r,|dz  }t        | d|       dd dz   t        | | d       dd z   S t        |       S )z9Shorten the sequence (array or list) for pretty-printing.   Nr/   z, ..., r   )r    rE   )seq	max_elemshalfs      r   r]   r]      sU    
3x)A~3u:s#i/#c4%&k2B122FFFs8Or*   r   )F)   )__doc__rW   typingr   r   numpyr2   r9   r)   r   r   r   r0   boolr4   rA   rH   rF   rD   r   rU   rZ   r`   r]   r   r*   r   <module>rk      s   $  " 
bU38_ bB" "J
 
#5 #	6	%D 	%c3h E#s(O ,5T 
r*   