
    ,Vh                     8    d Z ddlZddlZddlmZ dededefdZy)zLFunctions for performing contractions with array elements which are objects.    N)	ArrayTypeeqarraysreturnc                 >   ddl | j                  d      \  }|j                  d      i t              D ](  \  }}t        ||j                        D ]
  \  }}||<    * t	        fdD              }j                  |t              }t	        fdD              t	        fdD               j                  | D ]M  }	t        t        |	            fd	}
t        j                  t        j                   |
             ||	<   O |S )
a  A ``einsum`` implementation for ``numpy`` arrays with object dtype.
    The loop is performed in python, meaning the objects themselves need
    only to implement ``__mul__`` and ``__add__`` for the contraction to be
    computed. This may be useful when, for example, computing expressions of
    tensors with symbolic elements, but note it will be very slow when compared
    to ``numpy.einsum`` and numeric data types!

    Parameters
    ----------
    eq : str
        The contraction string, should specify output.
    arrays : sequence of arrays
        These can be any indexable arrays as long as addition and
        multiplication is defined on the elements.

    Returns:
    -------
    out : numpy.ndarray
        The output tensor, with ``dtype=object``.
    r   Nz->,c              3   (   K   | ]	  }|     y wN .0ksizess     Q/home/dcms/DCMS/lib/python3.12/site-packages/opt_einsum/backends/object_arrays.py	<genexpr>z object_einsum.<locals>.<genexpr>)   s     .!U1X.   )dtypec              3   ,   K   | ]  }|vs|  y wr
   r   )r   r   outputs     r   r   z object_einsum.<locals>.<genexpr>,   s     6avo!6s   	c              3   (   K   | ]	  }|     y wr
   r   r   s     r   r   z object_einsum.<locals>.<genexpr>-   s     /AuQx/r   c            	   3     K    j                    D ]j  } j                  t        t        |                    fdD        }d t        |      D        }t	        j
                  t        j                  |       l y w)Nc              3   F   K   | ]  }t        fd |D                yw)c              3   (   K   | ]	  }|     y wr
   r   )r   r   coords     r   r   zAobject_einsum.<locals>.gen_inner_sum.<locals>.<genexpr>.<genexpr>5   s     51eAh5r   N)tuple)r   termr   s     r   r   z7object_einsum.<locals>.gen_inner_sum.<locals>.<genexpr>5   s     I$555Is   !c              3   ,   K   | ]  \  }}||     y wr
   r   )r   arraylocs      r   r   z7object_einsum.<locals>.gen_inner_sum.<locals>.<genexpr>6   s     K:5#E#JKs   )ndindexupdatedictzip	functoolsreduceoperatormul)	coo_ilocselementsr   r   inner
inner_sizeinputsnps	      r   gen_inner_sumz$object_einsum.<locals>.gen_inner_sum2   sk     #Z0 ?T#eU"345I&IKVT9JK&&x||X>>	?s   A>B)numpysplitr#   shaper   emptyobjectr    r"   r$   r%   r&   add)r   r   lhsr   r   r   dout_sizeoutcoo_or/   r   r+   r,   r-   r.   r   r   s    `         @@@@@@@r   object_einsumr;   	   s   *  ((4.KCYYs^FE66* eekk* 	DAqE!H	 .v..H
((86(
*C6U66E///JX& 
ES'(	? 	? %%hllMODE

E J    )__doc__r$   r&   opt_einsum.typingr   strr;   r   r<   r   <module>r@      s+    R   '2c 2I 2) 2r<   