
    Sh                        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Z	 d!ded	eeeef                  dee         d
ee         f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            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            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Zded
ee         f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                     t           g }|                     d          d                             d          }|D ]2}fd|D             }|                    t          |                     3t          |          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                      g | ]
}|         S  r   ).0xr   s     X/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/opt_einsum/testing.py
<listcomp>z build_shapes.<locals>.<listcomp>(   s    000aq!000    )_default_dim_dictsplitappendtuple)r   r   shapestermstermdimss    `    r   build_shapesr*      s    $ *FLLq!'',,E # #00004000eDkk""""==r!   array_functionc                    | t          j        d          }|j        j        }g }t	          | |          D ]C}|r|                     ||            |                    t          j                               Dt          |          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numpy)r   )pytestimportorskiprandomrandr*   r$   r%   )r   r   r+   npviewsshapes         r   build_viewsr5   -   s    *  ))Ef^DDD * * 	*LL/0000LL))))<<r!   n
regularityn_outd_mind_maxseed
global_dimreturn_size_dictTc                    d S Nr   r6   r7   r8   r9   r:   r;   r<   r=   s           r   rand_equationrA   O   s	     ,/3r!   Fc                     d S r?   r   r@   s           r   rA   rA   ]   s	     3r!   r   	   c                    t          j        d          |j                            |           | |z  dz  z   }d t	          |           D             }	g fdt	          |          D             fd}
t          j                            t           |
                                          D ]v\  }}|| k     r|	|xx         |z  cc<   j                            d|           }||	|         v r%j                            d|           }||	|         v %|	|xx         |z  cc<   w|rWt          |          }j                            dz             |<   t	          |           D ]}|	|xx         |z  cc<   |z  d	
                    j                                                d
                    d
                    |	                    }fd|	D             }||f}|r|fz   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                     g | ]}d S ) r   )r   _s     r   r    z!rand_equation.<locals>.<listcomp>   s    ###Qb###r!   c                 h    i | ].}t          |          j                            d z             /S )   )r   r0   randint)r   ir:   r9   r2   s     r   
<dictcomp>z!rand_equation.<locals>.<dictcomp>   s7    ]]]A	 1 1% C C]]]r!   c               3      K   t                    D ]-\  } }| k     r                    |           |V  %|V  |V  .d S r?   )	enumerater$   )rK   ixr8   output	size_dicts     r   genzrand_equation.<locals>.gen   sh      y)) 	 	EAr5yyb!!! 	 	r!   r   rI   rF   z{}->{}r   c                 F    g | ]}t          fd |D                       S )c              3   (   K   | ]}|         V  d S r?   r   )r   rO   rQ   s     r   	<genexpr>z+rand_equation.<locals>.<listcomp>.<genexpr>   s'      //bIbM//////r!   )r%   )r   oprQ   s     r   r    z!rand_equation.<locals>.<listcomp>   s6    AAABe////B/////AAAr!   )r.   r/   r0   r;   rangerN   permutationlistrJ   r   joinformat)r6   r7   r8   r9   r:   r;   r<   r=   num_indsinputsrR   rK   rO   wheregdimeqr&   retr2   rP   rQ   s     ```             @@@r   rA   rA   j   sa   ` 
	W	%	%B
	t :~"U*H##%((###FF]]]]]]US[__]]]I	 	 	 	 	 	 	 2900ccee==>> 
  
 2q551IIIOIIII I%%a++Eu%%	))!Q// u%% 5MMMRMMMM  (##)++E519==	$q 	 	A1IIIIIII$ WWRY**62233F	&))6	2	2B BAAA&AAAFv,C i\!!
r!   pathc                 H    t          j        d          fd| D             S )zBuild random numpy arrays from a path.

    Parameters:
        path: The path to build arrays from.

    Returns:
    The resulting arrays.
    r-   c                 .    g | ]} j         j        | S r   )r0   r1   )r   r   r2   s     r   r    z,build_arrays_from_tuples.<locals>.<listcomp>   s$    ---1NBINA---r!   )r.   r/   )rb   r2   s    @r   build_arrays_from_tuplesre      s/     
	W	%	%B--------r!   r?   )NN).....)......)r   r   rC   NFF)__doc__r0   typingr   r   r   r   r   r   r	   r
   r.   opt_einsum.parserr   opt_einsum.typingr   r   r   _valid_chars_sizesdictzipr"   strintr*   r5   boolrA   re   r   r!   r   <module>rq      s   & &  M M M M M M M M M M M M M M M M M M M M  ( ( ( ( ( ( B B B B B B B B B B$	B	B	BD\62233   htCH~.F RWXgilXlRm    < cg !)$sCx.!9RZ[^R_
9   D 
 
/ 
/

/
/ 
/ 	
/
 
/ 3-
/ 
/ dm
/ 3$sCx.()
/ 
/ 
/ 

/ 
 '*	 	
		 	 		
 	 3-	 	 en	 3=	 	 	 
	 "h h
hh h 	h
 h 3-h h h 5hS#X./sH}1EEFh h h hV.8 .S	 . . . . . .r!   