o
    2h                     @   s:   d Z ddlZddlZddlmZ dededefddZdS )	zLFunctions for performing contractions with array elements which are objects.    N)	ArrayTypeeqarraysreturnc                    s   ddl | d\}|di t D ]\}}t||jD ]\}}||< q!qtfddD }j|td}tfddD tfd	dD j| D ]}	tt|	 fd
d}
t	
tj|
 ||	< qX|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       | ]} | V  qd S N .0ksizesr	   \/var/www/html/chatgem/venv/lib/python3.10/site-packages/opt_einsum/backends/object_arrays.py	<genexpr>)       z object_einsum.<locals>.<genexpr>)dtypec                 3   s    | ]	}| vr|V  qd S r   r	   r
   )outputr	   r   r   ,       c                 3   r   r   r	   r
   r   r	   r   r   -   r   c                  3   s`    j  D ]'} tt|  fddD }dd t |D }ttj|V  qd S )Nc                 3   s&    | ]}t  fd d|D V  qdS )c                 3   r   r   r	   r
   coordr	   r   r   5   r   zAobject_einsum.<locals>.gen_inner_sum.<locals>.<genexpr>.<genexpr>N)tuple)r   termr   r	   r   r   5   s   $ z7object_einsum.<locals>.gen_inner_sum.<locals>.<genexpr>c                 s   s    | ]	\}}|| V  qd S r   r	   )r   arraylocr	   r	   r   r   6   r   )ndindexupdatedictzip	functoolsreduceoperatormul)coo_ilocselements)r   r   inner
inner_sizeinputsnpr	   r   gen_inner_sum2   s   z$object_einsum.<locals>.gen_inner_sum)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)   r   r   r   object_einsum	   s"   

r6   )__doc__r   r!   opt_einsum.typingr   strr6   r	   r	   r	   r   <module>   s
    