a
    Sicy                     @   sH   d Z ddlZddlmZ g dZedddd	d
Zdd Zdd ZdS )zM
Required functions for optimized contractions of numpy arrays using theano.
    N   )to_backend_cache_wrap)	to_theanobuild_expressionevaluate_constantsT)	constantsFc                 C   sH   ddl }t| tjrD|r$|j| S |jj| jdgt| j	 d S | S )zDConvert a numpy array to ``theano.tensor.TensorType`` instance.
    r   NF)dtypeZbroadcastable)
theano
isinstancenpndarraytensorconstant
TensorTyper   lenshape)arrayr   r	    r   V/var/www/html/django/DPS/env/lib/python3.9/site-packages/opt_einsum/backends/theano.pyr      s     r   c                    sT   ddl dd | D }|j|dd}fdd|D }||  fdd	}|S )
z>Build a theano function based on ``arrays`` and ``expr``.
    r   Nc                 S   s   g | ]}t |qS r   r   ).0r   r   r   r   
<listcomp>        z$build_expression.<locals>.<listcomp>r	   )backendc                    s   g | ]}t | jjs|qS r   r
   r   ZTensorConstantr   xr	   r   r   r   $   r   c                     s    fdd| D  S )Nc                    s   g | ]}t | jjs|qS r   r   r   r   r   r   r   (   r   z=build_expression.<locals>.theano_contract.<locals>.<listcomp>r   )arraysgraphr	   r   r   theano_contract'   s    z)build_expression.<locals>.theano_contract)r	   	_contractfunction)r   exprin_varsZout_varZ	graph_insr!   r   r   r   r      s    r   c                 C   s8   dd | D } || ddd\}}dd |D }||fS )Nc                 S   s   g | ]}t |d dqS )Tr   r   r   r   r   r   r   /   r   z&evaluate_constants.<locals>.<listcomp>r	   T)r   r   c                 S   s(   g | ] }|d u rd nt | ddqS )NTr&   )r   evalr   r   r   r   r   3   r   r   )const_arraysr$   new_opsnew_contraction_listr   r   r   r   -   s    r   )F)	__doc__numpyr   sharingr   __all__r   r   r   r   r   r   r   <module>   s   