
    AVh1              	          d 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ej                  ej                  ej                  ej                  ej                   ej"                  ej$                  g      Z e
ddd	g
      ej(                  dd              Z ej,                  d        ej,                  d        ej,                  d        ej,                  d       d ZddZ e
dddg
      ej(                  dd              Z e
dddg
      ej(                  dd              Z e
dddg
      ej(                  dd              Zy)zImplementation of tf.sets.    )dtypes)ops)sparse_tensor)gen_set_ops)dispatch)	tf_exportz	sets.sizezsets.set_size)v1c                    t        j                  | d      } t        | t         j                        st	        d| z        | j
                  j                  j                  t        vr*t	        d| j
                  j                   dt         d      t        j                  | j                  | j
                  | j                  |      S )a  Compute number of unique elements along last dimension of `a`.

  Args:
    a: `SparseTensor`, with indices sorted in row-major order.
    validate_indices: Whether to validate the order and range of sparse indices
      in `a`. Note that setting this to `false` allows for undefined behavior
      when calling this function with invalid indices.

  Returns:
    `int32` `Tensor` of set sizes. For `a` ranked `n`, this is a `Tensor` with
    rank `n-1`, and the same 1st `n-1` dimensions as `a`. Each value is the
    number of unique elements in the corresponding `[0...n-1]` dimension of `a`.

  Raises:
    TypeError: If `a` is an invalid types.
  anamez Expected `SparseTensor`, got %s.zInvalid dtype `` not in supported dtypes: ``.)r   "convert_to_tensor_or_sparse_tensor
isinstanceSparseTensor	TypeErrorvaluesdtype
base_dtype_VALID_DTYPESr   set_sizeindicesdense_shape)r   validate_indicess     O/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/sets_impl.pyr   r      s    & 66qsC!	A}11	2
6:
;;XX^^m3

!((..) *?"	  
		aii1==.
0 0    SetSizeDenseToDenseSetOperationDenseToSparseSetOperationSparseToSparseSetOperationc                    t        j                  | d      } | j                  j                  t        vr t        d| j                   dt         d      t        j                  |d      }|j                  j                  | j                  j                  k7  r&t        d| j                  d|j                  d	      t        | t         j                        rt        |t         j                        s|| d
fS | |dfS )a  Convert to tensor types, and flip order if necessary.

  Args:
    a: `Tensor` or `SparseTensor` of the same type as `b`.
    b: `Tensor` or `SparseTensor` of the same type as `a`.

  Returns:
    Tuple of `(a, b, flipped)`, where `a` and `b` have been converted to
    `Tensor` or `SparseTensor`, and `flipped` indicates whether the order has
    been flipped to make it dense,sparse instead of sparse,dense (since the set
    ops do not support the latter).
  r   r   z'a' has invalid dtype `r   r   bzTypes don't match, z vs .TF)r   r   r   r   r   r   r   r   )r   r#   s     r   %_convert_to_tensors_or_sparse_tensorsr%   D   s     66qsC!WW},

!!'' +?"	  66qsC!WW177---
aggqwwG
HHM../
Q22
3a:	
Aur   c           
      N   t        | t        j                        rt        |t        j                        r]t        j                  | j
                  | j                  | j                  |j
                  |j                  |j                  ||      \  }}}n~t        d      t        |t        j                        r=t        j                  | |j
                  |j                  |j                  ||      \  }}}nt        j                  | |||      \  }}}t        j                  |||      S )a  Compute set operation of elements in last dimension of `a` and `b`.

  All but the last dimension of `a` and `b` must match.

  Args:
    a: `Tensor` or `SparseTensor` of the same type as `b`. If sparse, indices
      must be sorted in row-major order.
    b: `Tensor` or `SparseTensor` of the same type as `a`. Must be
      `SparseTensor` if `a` is `SparseTensor`. If sparse, indices must be sorted
      in row-major order.
    set_operation: String indicating set operation. See
        SetOperationOp::SetOperationFromContext for valid values.
    validate_indices: Whether to validate the order and range of sparse indices
      in `a` and `b`.

  Returns:
    A `SparseTensor` with the same rank as `a` and `b`, and all but the last
    dimension the same. Elements along the last dimension contain the results
    of the set operation.

  Raises:
    TypeError: If inputs are invalid types.
    ValueError: If `a` is sparse and `b` is dense.
  zYSparse,Dense is not supported, but Dense,Sparse is. Please flip the order of your inputs.)r   r   r   r   sparse_to_sparse_set_operationr   r   r   
ValueErrordense_to_sparse_set_operationdense_to_dense_set_operation)r   r#   set_operationr   r   r   shapes          r   _set_operationr-   _   s    2 =--.!]//0*II
))QXXq}}aii
--(8 :gvu  ? @ @!]//0(FF	199ahh}>NPGVU )EE	1m-/GVU		#	#GVU	;;r   zsets.intersectionzsets.set_intersectionc                 >    t        | |      \  } }}t        | |d|      S )aV  Compute set intersection of elements in last dimension of `a` and `b`.

  All but the last dimension of `a` and `b` must match.

  Example:

  ```python
    import tensorflow as tf
    import collections

    # Represent the following array of sets as a sparse tensor:
    # a = np.array([[{1, 2}, {3}], [{4}, {5, 6}]])
    a = collections.OrderedDict([
        ((0, 0, 0), 1),
        ((0, 0, 1), 2),
        ((0, 1, 0), 3),
        ((1, 0, 0), 4),
        ((1, 1, 0), 5),
        ((1, 1, 1), 6),
    ])
    a = tf.sparse.SparseTensor(list(a.keys()), list(a.values()),
                               dense_shape=[2,2,2])

    # b = np.array([[{1}, {}], [{4}, {5, 6, 7, 8}]])
    b = collections.OrderedDict([
        ((0, 0, 0), 1),
        ((1, 0, 0), 4),
        ((1, 1, 0), 5),
        ((1, 1, 1), 6),
        ((1, 1, 2), 7),
        ((1, 1, 3), 8),
    ])
    b = tf.sparse.SparseTensor(list(b.keys()), list(b.values()),
                               dense_shape=[2, 2, 4])

    # `tf.sets.intersection` is applied to each aligned pair of sets.
    tf.sets.intersection(a, b)

    # The result will be equivalent to either of:
    #
    # np.array([[{1}, {}], [{4}, {5, 6}]])
    #
    # collections.OrderedDict([
    #     ((0, 0, 0), 1),
    #     ((1, 0, 0), 4),
    #     ((1, 1, 0), 5),
    #     ((1, 1, 1), 6),
    # ])
  ```

  Args:
    a: `Tensor` or `SparseTensor` of the same type as `b`. If sparse, indices
      must be sorted in row-major order.
    b: `Tensor` or `SparseTensor` of the same type as `a`. If sparse, indices
      must be sorted in row-major order.
    validate_indices: Whether to validate the order and range of sparse indices
      in `a` and `b`.

  Returns:
    A `SparseTensor` whose shape is the same rank as `a` and `b`, and all but
    the last dimension the same. Elements along the last dimension contain the
    intersections.
  intersectionr%   r-   r   r#   r   _s       r   set_intersectionr3      s)    F 2!Q7'!Q	1n.>	??r   zsets.differencezsets.set_differencec                 Z    t        | |      \  } }}|r| }t        | ||rd|      S d|      S )a  Compute set difference of elements in last dimension of `a` and `b`.

  All but the last dimension of `a` and `b` must match.

  Example:

  ```python
    import tensorflow as tf
    import collections

    # Represent the following array of sets as a sparse tensor:
    # a = np.array([[{1, 2}, {3}], [{4}, {5, 6}]])
    a = collections.OrderedDict([
        ((0, 0, 0), 1),
        ((0, 0, 1), 2),
        ((0, 1, 0), 3),
        ((1, 0, 0), 4),
        ((1, 1, 0), 5),
        ((1, 1, 1), 6),
    ])
    a = tf.sparse.SparseTensor(list(a.keys()), list(a.values()),
                               dense_shape=[2, 2, 2])

    # np.array([[{1, 3}, {2}], [{4, 5}, {5, 6, 7, 8}]])
    b = collections.OrderedDict([
        ((0, 0, 0), 1),
        ((0, 0, 1), 3),
        ((0, 1, 0), 2),
        ((1, 0, 0), 4),
        ((1, 0, 1), 5),
        ((1, 1, 0), 5),
        ((1, 1, 1), 6),
        ((1, 1, 2), 7),
        ((1, 1, 3), 8),
    ])
    b = tf.sparse.SparseTensor(list(b.keys()), list(b.values()),
                               dense_shape=[2, 2, 4])

    # `set_difference` is applied to each aligned pair of sets.
    tf.sets.difference(a, b)

    # The result will be equivalent to either of:
    #
    # np.array([[{2}, {3}], [{}, {}]])
    #
    # collections.OrderedDict([
    #     ((0, 0, 0), 2),
    #     ((0, 1, 0), 3),
    # ])
  ```

  Args:
    a: `Tensor` or `SparseTensor` of the same type as `b`. If sparse, indices
      must be sorted in row-major order.
    b: `Tensor` or `SparseTensor` of the same type as `a`. If sparse, indices
      must be sorted in row-major order.
    aminusb: Whether to subtract `b` from `a`, vs vice versa.
    validate_indices: Whether to validate the order and range of sparse indices
      in `a` and `b`.

  Returns:
    A `SparseTensor` whose shape is the same rank as `a` and `b`, and all but
    the last dimension the same. Elements along the last dimension contain the
    differences.

  Raises:
    TypeError: If inputs are invalid types, or if `a` and `b` have
        different types.
    ValueError: If `a` is sparse and `b` is dense.
    errors_impl.InvalidArgumentError: If the shapes of `a` and `b` do not
        match in any dimension other than the last dimension.
  za-bzb-ar0   )r   r#   aminusbr   flippeds        r   set_differencer7      s@    V 81=-!QkG	1we;K	LLE;K	LLr   z
sets.unionzsets.set_unionc                 >    t        | |      \  } }}t        | |d|      S )a  Compute set union of elements in last dimension of `a` and `b`.

  All but the last dimension of `a` and `b` must match.

  Example:

  ```python
    import tensorflow as tf
    import collections

    # [[{1, 2}, {3}], [{4}, {5, 6}]]
    a = collections.OrderedDict([
        ((0, 0, 0), 1),
        ((0, 0, 1), 2),
        ((0, 1, 0), 3),
        ((1, 0, 0), 4),
        ((1, 1, 0), 5),
        ((1, 1, 1), 6),
    ])
    a = tf.sparse.SparseTensor(list(a.keys()), list(a.values()),
                               dense_shape=[2, 2, 2])

    # [[{1, 3}, {2}], [{4, 5}, {5, 6, 7, 8}]]
    b = collections.OrderedDict([
        ((0, 0, 0), 1),
        ((0, 0, 1), 3),
        ((0, 1, 0), 2),
        ((1, 0, 0), 4),
        ((1, 0, 1), 5),
        ((1, 1, 0), 5),
        ((1, 1, 1), 6),
        ((1, 1, 2), 7),
        ((1, 1, 3), 8),
    ])
    b = tf.sparse.SparseTensor(list(b.keys()), list(b.values()),
                               dense_shape=[2, 2, 4])

    # `set_union` is applied to each aligned pair of sets.
    tf.sets.union(a, b)

    # The result will be a equivalent to either of:
    #
    # np.array([[{1, 2, 3}, {2, 3}], [{4, 5}, {5, 6, 7, 8}]])
    #
    # collections.OrderedDict([
    #     ((0, 0, 0), 1),
    #     ((0, 0, 1), 2),
    #     ((0, 0, 2), 3),
    #     ((0, 1, 0), 2),
    #     ((0, 1, 1), 3),
    #     ((1, 0, 0), 4),
    #     ((1, 0, 1), 5),
    #     ((1, 1, 0), 5),
    #     ((1, 1, 1), 6),
    #     ((1, 1, 2), 7),
    #     ((1, 1, 3), 8),
    # ])
  ```

  Args:
    a: `Tensor` or `SparseTensor` of the same type as `b`. If sparse, indices
      must be sorted in row-major order.
    b: `Tensor` or `SparseTensor` of the same type as `a`. If sparse, indices
      must be sorted in row-major order.
    validate_indices: Whether to validate the order and range of sparse indices
      in `a` and `b`.

  Returns:
    A `SparseTensor` whose shape is the same rank as `a` and `b`, and all but
    the last dimension the same. Elements along the last dimension contain the
    unions.
  unionr0   r1   s       r   	set_unionr:   !  s)    V 2!Q7'!Q	1g'7	88r   N)T)TT)__doc__tensorflow.python.frameworkr   r   r   tensorflow.python.opsr   tensorflow.python.utilr    tensorflow.python.util.tf_exportr   	frozensetint8int16int32int64uint8uint16stringr   add_dispatch_supportr   NotDifferentiabler%   r-   r3   r7   r:    r   r   <module>rK      sw   ! . + 5 - + 6
KKv||V\\6<<
MM6==  ;K9:	0  ;0:   i     0 1   1 2   2 36'<T 02IJL	A@ LA@H "35J!KL	LM  MLM^ <\+;<=	J9  >J9r   