a
    Sic                     @   s(   d Z ddlZddlZddlZdd ZdS )zN
Functions for performing contractions with array elements which are objects.
    Nc                    s   |  d\}| di t D ]&\}}t||jD ]\}}||< q:q&tfddD }tj|td}tfddD tfddD tj| D ]:}	tt|	 fdd	}
t	
tj|
 ||	< q|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``.
    z->,c                 3   s   | ]} | V  qd S N .0ksizesr   ]/var/www/html/django/DPS/env/lib/python3.9/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   ,   r   c                 3   s   | ]} | V  qd S r   r   r   r   r   r
   r   -   r   c                  3   s^   t j D ]N} 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   s   | ]} | V  qd S r   r   r   coordr   r
   r   6   r   zAobject_einsum.<locals>.gen_inner_sum.<locals>.<genexpr>.<genexpr>N)tuple)r   termr   r   r
   r   6   r   z7object_einsum.<locals>.gen_inner_sum.<locals>.<genexpr>c                 s   s   | ]\}}|| V  qd S r   r   )r   arraylocr   r   r
   r   7   r   )	npndindexupdatedictzip	functoolsreduceoperatormul)Zcoo_iZlocselements)arraysr   inner
inner_sizeinputsr   r
   gen_inner_sum3   s
    z$object_einsum.<locals>.gen_inner_sum)splitr   shaper   r   emptyobjectr   r   r   r   r   add)eqr   lhsr   r   r   dout_sizeoutZcoo_or#   r   )r   r   r    r!   r"   r   r	   r
   object_einsum
   s    
r.   )__doc__numpyr   r   r   r.   r   r   r   r
   <module>   s   