
    ,Vh                     X   d Z ddlmZmZmZmZmZmZmZm	Z	 ddl
mZmZ g dZdZg dZ e eee            Ze	dee   dee   d	efd
       Ze	dee   deeef   d	efd       Zdeded	efdZdee   dee   ded	eee   ee   eef   fdZdee   dededeeef   d	ef
dZded	efdZy)z9Contains helper functions for opt_einsum testing scripts.    )Any
CollectionDict	FrozenSetIterableListTupleoverload)ArrayIndexType	ArrayType)compute_size_by_dictfind_contraction
flop_countabcdefghijklmopqABC)            r   r   r      r   r   r   r   r      r   r   r   r   r   indicesidx_dictreturnc                      y N r   r   s     B/home/dcms/DCMS/lib/python3.12/site-packages/opt_einsum/helpers.pyr   r      s    NQ    c                      y r   r   r   s     r   r   r      s    UXr   c                 (    d}| D ]
  }|||   z  } |S )a  Computes the product of the elements in indices based on the dictionary
    idx_dict.

    Parameters
    ----------
    indices : iterable
        Indices to base the product on.
    idx_dict : dictionary
        Dictionary of index _sizes

    Returns:
    -------
    ret : int
        The resulting product.

    Examples:
    --------
    >>> compute_size_by_dict('abbc', {'a': 2, 'b':3, 'c':5})
    90

       r   )r   r   retis       r   r   r      s+    , C x{Jr   	positions
input_sets
output_setc                     t        |      fdt        | d      D        }t        j                  | } |j                   }||z  }||z
  }j	                  |       |||fS )a  Finds the contraction for a given set of input and output sets.

    Parameters
    ----------
    positions : iterable
        Integer positions of terms used in the contraction.
    input_sets : list
        List of sets that represent the lhs side of the einsum subscript
    output_set : set
        Set that represents the rhs side of the overall einsum subscript

    Returns:
    -------
    new_result : set
        The indices of the resulting contraction
    remaining : list
        List of sets that have not been contracted, the new set is appended to
        the end of this list
    idx_removed : set
        Indices removed from the entire contraction
    idx_contraction : set
        The indices used in the current contraction

    Examples:
    --------
    # A simple dot product test case
    >>> pos = (0, 1)
    >>> isets = [set('ab'), set('bc')]
    >>> oset = set('ac')
    >>> find_contraction(pos, isets, oset)
    ({'a', 'c'}, [{'a', 'c'}], {'b'}, {'a', 'b', 'c'})

    # A more complex case with additional terms in the contraction
    >>> pos = (0, 2)
    >>> isets = [set('abd'), set('ac'), set('bdc')]
    >>> oset = set('ac')
    >>> find_contraction(pos, isets, oset)
    ({'a', 'c'}, [{'a', 'c'}, {'a', 'c'}], {'b', 'd'}, {'a', 'b', 'c', 'd'})
    c              3   @   K   | ]  }j                  |        y wr   )pop).0r$   	remainings     r   	<genexpr>z#find_contraction.<locals>.<genexpr>_   s     H1immAHs   T)reverse)listsorted	frozensetunionappend)	r%   r&   r'   inputsidx_contract
idx_remain
new_resultidx_removedr,   s	           @r   r   r   2   sr    X Z IHy$(GHF??F+L!!!9-Jl*J+KZ y+|;;r   idx_contractioninner	num_termssize_dictionaryc                 P    t        | |      }t        d|dz
        }|r|dz  }||z  S )a  Computes the number of FLOPS in the contraction.

    Parameters
    ----------
    idx_contraction : iterable
        The indices involved in the contraction
    inner : bool
        Does this contraction require an inner product?
    num_terms : int
        The number of terms in a contraction
    size_dictionary : dict
        The size of each of the indices in idx_contraction

    Returns:
    -------
    flop_count : int
        The total number of FLOPS required for the contraction.

    Examples:
    --------
    >>> flop_count('abc', False, 1, {'a': 2, 'b':3, 'c':5})
    30

    >>> flop_count('abc', True, 2, {'a': 2, 'b':3, 'c':5})
    60

    r"   )r   max)r9   r:   r;   r<   overall_size	op_factors         r   r   r   j   s8    B (ILAy1}%IQ	)##r   arrayc                     t        | d      ryy)N__array_interface__TF)hasattr)rA   s    r   has_array_interfacerE      s    u+,r   N)__doc__typingr   r   r   r   r   r   r	   r
   opt_einsum.typingr   r   __all___valid_chars_sizesdictzip_default_dim_dictintr   strr   boolr   rE   r   r   r   <module>rR      s?   ? T T T 7
D$	B\623  
 Q(3- Q49 Q Q 
 Q 
 X*S/ XT#s(^ XPS X 
 X#   85<#5<^$5< 5< 9S>4/OP	5<p&$_&$&$ &$ #s(^	&$
 	&$Ry Y r   