o
    2h                     @   s2  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
dZe	dee deeef d	efddZdeded	efddZdee dee ded	eee ee eef fddZdee dededeeef d	ef
ddZded	efddZdS )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                 C      d S N r   r   r   r   M/var/www/html/chatgem/venv/lib/python3.10/site-packages/opt_einsum/helpers.pyr         r   c                 C   r   r   r   r   r   r   r   r      r   c                 C   s   d}| D ]}||| 9 }q|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ir   r   r   r      s   	positions
input_sets
output_setc                    s\   t |  fddt| ddD }tj| }|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   s    | ]}  |V  qd S r   )pop).0r!   	remainingr   r   	<genexpr>_   s    z#find_contraction.<locals>.<genexpr>T)reverse)listsorted	frozensetunionappend)r"   r#   r$   inputsidx_contract
idx_remain
new_resultidx_removedr   r'   r   r   2   s   ,


r   idx_contractioninner	num_termssize_dictionaryc                 C   s,   t | |}td|d }|r|d7 }|| 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)r5   r6   r7   r8   overall_size	op_factorr   r   r   r   j   s
   
!r   arrayc                 C   s   t | drdS dS )N__array_interface__TF)hasattr)r<   r   r   r   has_array_interface   s   
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   r?   r   r   r   r   <module>   sB    ( $
8

)