
    AVh,                     p   d Z ddl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	 ddl
mZ ddlmZ dd	lmZ eZd
Zej&                  ej(                  ej*                  ej,                  ej.                  fZd Zd Z edg       ej6                  dd              Zd Zej<                  j?                  de       y)z"Tensor __getitem__ override logic.    N)dtypes)ops)tensor)tensor_shape)gen_math_ops)dispatch)	tf_exportzyOnly integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf.int64 tensors are valid indicesc                 V    t        | t        j                        xr | j                  d u S N)
isinstancer   	Dimensionvalue)ds    ]/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/tensor_getitem_override.py_is_undefined_dimensionr   -   s"    	A|--	.	B177d?B    c                 :   t        | t        j                  t        j                  f      ryt        | dd      }|?t        j                  |      t        vs$| j                  r:t        | j                        dk(  r!t        t        dj                  |       z         yy)z6Check if a given value is a valid index into a tensor.Ndtype   z
, got {!r})r   numbersIntegralr   r   getattrr   as_dtype_SUPPORTED_SLICE_DTYPESshapelen	TypeError_SLICE_TYPE_ERRORformat)idxr   s     r   _check_indexr!   1   s    g&&(>(>?@

 #w
%%mvu-5LL	iiC		Na' %(;(;C(@@
AA (ir   z__operators__.getitem)v1c                    ddl m} ddlm} t	        j
                  |       } |!t        j                  r| j                  |      S t        |t              sdt        |t        j                        r|j                  t        j                  k(  s-t        |t        j                        r&|j                  t        k(  r|j!                  | |      S t        |t"        t$        f      s|g}g g g }}}d}d\  }	}
d\  }}d}|D ]  }t        |t&              rd}|j(                  |j*                  |j,                  fD ]  }|t        |t        j                        s |j                  t        j.                  k(  rt        j.                  }N|j                  t        j0                  k(  r$|t        j.                  k7  rt        j0                  }|j                  t        j2                  k(  s|t        j2                  } |j(                  Ft5        |j(                        s1t7        |j(                         |j9                  |j(                         n>|#|j9                  |j;                  d|             n|j9                  d       |d|z  z  }|j*                  Ft5        |j*                        s1t7        |j*                         |j9                  |j*                         n>|#|j9                  |j;                  d|             n|j9                  d       |d|z  z  }|j,                  Gt5        |j,                        s2t7        |j,                         |j9                  |j,                         n|$|j9                  |j;                  d|             nl|j9                  d       nY|t<        u r=|j9                  d       |j9                  d       |j9                  d       |d|z  z  }n||j>                  u r<|j9                  d       |j9                  d       |j9                  d       |	d|z  z  }	nt7        |       |j9                  |       |j9                  |dz          t        |t        j                        rg|j                  t        j2                  k(  s|j                  t        j.                  k(  r-|j9                  |j;                  d|j                               n|j9                  d       |
d|z  z  }
|dz  } t	        j@                  dd	| g|z   |z   |z   d
      5 }|rsddlm!} |jE                  |      |jE                  |      |jE                  |      }}}|j                  t        j.                  k(  s:|j                  t        j.                  k(  s|j                  t        j.                  k(  r|j                  t        j.                  k7  r$tG        jH                  |t        j.                        }|j                  t        j.                  k7  r$tG        jH                  |t        j.                        }|j                  t        j.                  k7  rjtG        jH                  |t        j.                        }nD|j                  t        j2                  k(  r&|j                  t        j2                  k(  r|j                  t        j2                  k(  r|j                  t        j2                  k7  r$tG        jH                  |t        j2                        }|j                  t        j2                  k7  r$tG        jH                  |t        j2                        }|j                  t        j2                  k7  rLtG        jH                  |t        j2                        }n'|j;                  g t        j0                        }|x}x}}|jK                  | ||||||
|	|||      cddd       S # 1 sw Y   yxY w)am	  Overload for Tensor.__getitem__.

  This operation extracts the specified region from the tensor.
  The notation is similar to NumPy with the restriction that
  currently only support basic indexing. That means that
  using a non-scalar tensor as input is not currently allowed.

  Some useful examples:

  ```python
  # Strip leading and trailing 2 elements
  foo = tf.constant([1,2,3,4,5,6])
  print(foo[2:-2])  # => [3,4]

  # Skip every other row and reverse the order of the columns
  foo = tf.constant([[1,2,3], [4,5,6], [7,8,9]])
  print(foo[::2,::-1])  # => [[3,2,1], [9,8,7]]

  # Use scalar tensors as indices on both dimensions
  print(foo[tf.constant(0), tf.constant(2)])  # => 3

  # Insert another dimension
  foo = tf.constant([[1,2,3], [4,5,6], [7,8,9]])
  print(foo[tf.newaxis, :, :]) # => [[[1,2,3], [4,5,6], [7,8,9]]]
  print(foo[:, tf.newaxis, :]) # => [[[1,2,3]], [[4,5,6]], [[7,8,9]]]
  print(foo[:, :, tf.newaxis]) # => [[[1],[2],[3]], [[4],[5],[6]],
  [[7],[8],[9]]]

  # Ellipses (3 equivalent operations)
  foo = tf.constant([[1,2,3], [4,5,6], [7,8,9]])
  print(foo[tf.newaxis, :, :])  # => [[[1,2,3], [4,5,6], [7,8,9]]]
  print(foo[tf.newaxis, ...])  # => [[[1,2,3], [4,5,6], [7,8,9]]]
  print(foo[tf.newaxis])  # => [[[1,2,3], [4,5,6], [7,8,9]]]

  # Masks
  foo = tf.constant([[1,2,3], [4,5,6], [7,8,9]])
  print(foo[foo > 2])  # => [3, 4, 5, 6, 7, 8, 9]
  ```

  Notes:
    - `tf.newaxis` is `None` as in NumPy.
    - An implicit ellipsis is placed at the end of the `slice_spec`
    - NumPy advanced indexing is currently not supported.

  Purpose in the API:

    This method is exposed in TensorFlow's API so that library developers
    can register dispatching for `Tensor.__getitem__` to allow it to handle
    custom composite tensors & other custom objects.

    The API symbol is not intended to be called by users directly and does
    appear in TensorFlow's generated documentation.

  Args:
    tensor: An tensor.Tensor object.
    slice_spec: The arguments to Tensor.__getitem__.
    var: In the case of variable slice assignment, the Variable object to slice
      (i.e. tensor is the read-only view of this variable).

  Returns:
    The appropriate slice of "tensor", based on "slice_spec".

  Raises:
    ValueError: If a slice range is negative size.
    TypeError: If the slice indices aren't int, slice, ellipsis,
      tf.newaxis or scalar int32/int64 tensors.
  r   )constant_op)	array_opsN)r   mask)r   r   )r   r   strided_sliceF)skip_on_eager)array_ops_stack)
begin_maskend_maskshrink_axis_masknew_axis_maskellipsis_maskvarname)&tensorflow.python.frameworkr$   tensorflow.python.opsr%   r   convert_to_tensor_numpy_style_slicing_numpy_style_getitemr   bool
tensor_libTensorr   r   npndarrayboolean_masklisttuple
_BaseSlicestartstopstepint64int32int16r   r!   appendconstantEllipsisnewaxis
name_scoper)   stackr   castr'   )r   
slice_specr/   r$   r%   beginendstridesindexr-   r,   r*   r+   r.   sr   tr0   r)   packed_begin
packed_endpacked_strides	var_emptys                          r   _slice_helperrW   A   s   L 6-  (&[S--&&z22T"
Z!2!2
3&++-
Z
,$&!!j!AA	Ju	.JBg%
%$(!-!*h- >a!Z e( !9Jq**;*;<
77fll",,%WW$&,,)>,,%WW$,,% 
	%<QWW%EQWWQWW
,,{++AU+;
<
,,q/qEz"
	
	$;AFF$CQVV

166
**[))!5)9
:
**Q-Q%Z 	
	$;AFF$CQVVqvv
..--au-=
>
..
	
hll1o	jjmnnQU
#m	
i	ll1o	jjmnnQU
#m1oll1o	jjQ Q
))
*ww&,,&!''V\\*A{++AQWW+=>q1:&	QJE}>B ~~
x%'#-7 , #7



&



$



( !/Jl 


,


fll
*


&,,
.-%**<F,v||+#((V\\B*6<</',,^V\\J.&,,.,  FLL0-%**<F,v||+#((V\\B*6<</',,^V\\J.&&r&>i3<<l<Z."")## # C, , ,s   'J9_**_3c                 8    t        | j                         ||       S )a  Creates a slice helper object given a variable.

  This allows creating a sub-tensor from part of the current contents
  of a variable. See `tf.Tensor.__getitem__` for detailed examples
  of slicing.

  This function in addition also allows assignment to a sliced range.
  This is similar to `__setitem__` functionality in Python. However,
  the syntax is different so that the user can capture the assignment
  operation for grouping or passing to `sess.run()` in TF1.
  For example,

  ```python
  import tensorflow as tf
  A = tf.Variable([[1,2,3], [4,5,6], [7,8,9]], dtype=tf.float32)
  print(A[:2, :2])  # => [[1,2], [4,5]]

  A[:2,:2].assign(22. * tf.ones((2, 2))))
  print(A) # => [[22, 22, 3], [22, 22, 6], [7,8,9]]
  ```

  Note that assignments currently do not support NumPy broadcasting
  semantics.

  Args:
    var: An `ops.Variable` object.
    slice_spec: The arguments to `Tensor.__getitem__`.

  Returns:
    The appropriate slice of "tensor", based on "slice_spec".
    As an operator. The operator also has a `assign()` method
    that can be used to generate an assignment operator.

  Raises:
    ValueError: If a slice range is negative size.
    TypeError: TypeError: If the slice indices aren't int, slice,
      ellipsis, tf.newaxis or int32/int64 tensors.

  )rW   r   )r/   rL   s     r   _slice_helper_varrY     s    R 
syy{J	44r   __getitem__r   ) __doc__r   numpyr9   r1   r   r   r   r7   r   r2   r   tensorflow.python.utilr    tensorflow.python.util.tf_exportr	   slicer>   r   rD   rC   	int32_refrB   	int64_refr   r   r!   add_dispatch_supportrW   rY   r8   _override_operator r   r   <module>re      s     )   . + < 4 . + 6
 
  "<<v7G7G!<<)9)9; CB  "r*	H  +HV)5X    $ $]M Br   