
    ,Vh                     0   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y)#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 }| j                  d      d   j                  d      }|D ].  }|D cg c]  }||   	 }}|j                  t        |             0 t        |      S c c}w )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,)]
        ```

    z->r   ,)_default_dim_dictsplitappendtuple)r   r   shapestermstermxdimss          B/home/dcms/DCMS/lib/python3.12/site-packages/opt_einsum/testing.pybuild_shapesr%      sz    $ *FLLq!'',E #+/0aq!00eDk"# = 1s   A,array_functionc                    |+t        j                  d      }|j                  j                  }g }t	        | |      D ]<  }|r|j                   ||        |j                  t        j                                > 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)
        ```

    numpy)r   )pytestimportorskiprandomrandr%   r   r   )r   r   r&   npviewsshapes         r$   build_viewsr0   -   st    *   )Ef^D *LL/0LL)	*
 <    n
regularityn_outd_mind_maxseed
global_dimreturn_size_dictTc                     y N r2   r3   r4   r5   r6   r7   r8   r9   s           r$   rand_equationr>   O   s     ,/r1   Fc                      y r;   r<   r=   s           r$   r>   r>   ]   s     r1   c           	      :   t        j                  d      }||j                  j                  |       | |z  dz  z   }	t	        |       D 
cg c]  }
d }}
g t	        |	      D ci c]+  }t        |      |j                  j                  ||dz         - c}fd}t        |j                  j                  t         |                         D ]l  \  }}|| k  r||xx   |z  cc<   |j                  j                  d|       }|||   v r$|j                  j                  d|       }|||   v r$||xx   |z  cc<   n |rOt        |	      }|j                  j                  ||dz         |<   t	        |       D ]  }||xx   |z  cc<    |z  dj                  |j                  j                              dj                  dj                  |            }|D cg c]  }t        fd	|D               }}||f}|r|fz   S |S c c}
w c c}w c c}w )
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(   r       c               3   z   K   t              D ](  \  } }| k  rj                  |       | !| | * y wr;   )	enumerater   )iixr4   output	size_dicts     r$   genzrand_equation.<locals>.gen   sC     y) 	EAr5yb! 	s   8;r   z{}->{}r   c              3   (   K   | ]	  }|     y wr;   r<   ).0rF   rH   s     r$   	<genexpr>z rand_equation.<locals>.<genexpr>   s     /bIbM/s   )r)   r*   r+   r7   ranger   randintrD   permutationlistjoinformatr   )r2   r3   r4   r5   r6   r7   r8   r9   r-   num_inds_inputsrE   rI   rF   wheregdimeqopr   retrG   rH   s     `                  @@r$   r>   r>   j   s   ` 
		W	%B
		t :~"U*H(#Qb#F#FMRS[_]A		 1 1% CC]I	 29900ce=> 
 2q51IOI II%%a+Eu%		))!Q/ u% 5MRM
  (#))++E519=	$q 	A1II	$ WWRYY**623F	&)6	2B ;AABe/B//AFAv,Ci\!!
e $ ^P Bs   	H(0H#Hpathc                     t        j                  d      }| D cg c]  } |j                  j                  |  c}S c c}w )zBuild random numpy arrays from a path.

    Parameters:
        path: The path to build arrays from.

    Returns:
    The resulting arrays.
    r(   )r)   r*   r+   r,   )r[   r-   r"   s      r$   build_arrays_from_tuplesr]      s7     
		W	%B(,-1NBIINNA---s    =r;   )NN).....)......)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%   r0   boolr>   r]   r<   r1   r$   <module>rj      s   &  M M M  ( B B$	B\62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 3-h h h 5hS#X./sH}1EEFhV.8 .S	 .r1   