o
    2h                     @   s  d Z ddlZddlmZmZmZmZmZmZm	Z	m
Z
 ddlZddlmZ ddlmZmZmZ dZg dZeeeeZd$ded	eeeef  d
eedf fddZ	d%ded	eeeef  dee d
ee fddZe
					d&dedededededee deded d
eeeeeef f fddZe
						d'dedededededee deded d
eeef fddZ						d(dedededededee deded
e	eeeeeef f eeef f fd dZd!ed
ee fd"d#ZdS ))z Testing routines for opt_einsum.    N)AnyDictListLiteralOptionalTupleUnionoverload)
get_symbol)	ArrayTypePathTypeTensorShapeTypeabcdefghijklmopqABC)            r   r   r      r   r   r   r   r      r   r   r   r   r   stringdimension_dictreturn.c                    sV    du rt  g }| dd d}|D ]} fdd|D }|t| qt|S )a  Builds random tensor shapes for testing.

    Parameters:
        string: List of tensor strings to build
        dimension_dict: Dictionary of index sizes, defaults to indices size of 2-7

    Returns:
        The resulting shapes.

    Examples:
        ```python
        >>> shapes = build_shapes('abbc', {'a': 2, 'b':3, 'c':5})
        >>> shapes
        [(2, 3), (3, 3, 5), (5,)]
        ```

    Nz->r   ,c                    s   g | ]} | qS  r   .0xr   r   M/var/www/html/chatgem/venv/lib/python3.10/site-packages/opt_einsum/testing.py
<listcomp>(   s    z build_shapes.<locals>.<listcomp>)_default_dim_dictsplitappendtuple)r   r   shapestermstermdimsr   r   r   build_shapes   s   r(   array_functionc                 C   sZ   |du rt d}|jj}g }t| |dD ]}|r!|||  q|t  qt|S )a  Builds random numpy arrays for testing.

    Parameters:
        string: List of tensor strings to build
        dimension_dict: Dictionary of index _sizes
        array_function: Function to build the arrays, defaults to np.random.rand

    Returns:
        The resulting views.

    Examples:
        ```python
        >>> view = build_views('abbc', {'a': 2, 'b':3, 'c':5})
        >>> view[0].shape
        (2, 3, 3, 5)
        ```

    Nnumpyr   )pytestimportorskiprandomrandr(   r"   r#   )r   r   r)   npviewsshaper   r   r   build_views-   s   
r2   n
regularityn_outd_mind_maxseed
global_dimreturn_size_dictTc                C      d S Nr   r3   r4   r5   r6   r7   r8   r9   r:   r   r   r   rand_equationO   s   r>   Fc                 C   r;   r<   r   r=   r   r   r   r>   ]   s   
r   	   c                    s  t d|durj| | | d  }dd t| D }	g  fddt|D fdd	}
tjt|
 D ]3\}}|| k rS|	|  |7  < qBjd
| }||	| v rmjd
| }||	| v s`|	|  |7  < qB|rt	|}j d |< t| D ]
}|	|  |7  < q|7 d
jdd
|	}fdd|	D }||f}|r|f S |S )a9  Generate a random contraction and shapes.

    Parameters:
        n: Number of array arguments.
        regularity: 'Regularity' of the contraction graph. This essentially determines how
            many indices each tensor shares with others on average.
        n_out: Number of output indices (i.e. the number of non-contracted indices).
            Defaults to 0, i.e., a contraction resulting in a scalar.
        d_min: Minimum dimension size.
        d_max: Maximum dimension size.
        seed: If not None, seed numpy's random generator with this.
        global_dim: Add a global, 'broadcast', dimension to every operand.
        return_size_dict: Return the mapping of indices to sizes.

    Returns:
        eq: The equation string.
        shapes: The array shapes.
        size_dict: The dict of index sizes, only returned if ``return_size_dict=True``.

    Examples:
        ```python
        >>> eq, shapes = rand_equation(n=10, regularity=4, n_out=5, seed=42)
        >>> eq
        'oyeqn,tmaq,skpo,vg,hxui,n,fwxmr,hitplcj,kudlgfv,rywjsb->cebda'

        >>> shapes
        [(9, 5, 4, 5, 4),
        (4, 4, 8, 5),
        (9, 4, 6, 9),
        (6, 6),
        (6, 9, 7, 8),
        (4,),
        (9, 3, 9, 4, 9),
        (6, 8, 4, 6, 8, 6, 3),
        (4, 7, 8, 8, 6, 9, 6),
        (9, 5, 3, 3, 9, 5)]
        ```
    r*   Nr   c                 S   s   g | ]}d qS ) r   )r   _r   r   r   r      s    z!rand_equation.<locals>.<listcomp>c                    s$   i | ]}t |j d  qS )   )r
   r-   randint)r   i)r7   r6   r/   r   r   
<dictcomp>   s   $ z!rand_equation.<locals>.<dictcomp>c                  3   s>    t D ]\} }|  k r| |V  q|V  |V  qd S r<   )	enumerater"   )rD   ix)r5   output	size_dictr   r   gen   s   
zrand_equation.<locals>.genr   rB   r@   z{}->{}r   c                    s"   g | ]}t  fd d|D qS )c                 3   s    | ]} | V  qd S r<   r   )r   rG   rI   r   r   	<genexpr>   s    z+rand_equation.<locals>.<listcomp>.<genexpr>)r#   )r   oprK   r   r   r      s   " )r+   r,   r-   r8   rangerF   permutationlistrC   r
   joinformat)r3   r4   r5   r6   r7   r8   r9   r:   num_indsinputsrJ   rD   rG   wheregdimeqr$   retr   )r7   r6   r5   r/   rH   rI   r   r>   j   s:   
0
pathc                    s   t d  fdd| D S )zBuild random numpy arrays from a path.

    Parameters:
        path: The path to build arrays from.

    Returns:
    The resulting arrays.
    r*   c                    s   g | ]} j j| qS r   )r-   r.   r   r/   r   r   r      s    z,build_arrays_from_tuples.<locals>.<listcomp>)r+   r,   )rY   r   rZ   r   build_arrays_from_tuples   s   
	r[   r<   )NN).....)......)r   r   r?   NFF)__doc__r-   typingr   r   r   r   r   r   r   r	   r+   opt_einsum.parserr
   opt_einsum.typingr   r   r   _valid_chars_sizesdictzipr    strintr(   r2   boolr>   r[   r   r   r   r   <module>   s    (,
"	

	$	
k