
    ,Vh              	          U d Z ddlZddlmZmZmZ ddlmZ ddlm	Z
 ddlmZ ddlmZ ddlmZ dd	lmZ g d
ZddddddddZd'dedededefdZdej,                  iZeeeef   ef   ed<   	 ddlZej6                  ed<   ej8                  ed<   ej:                  ed<   ej6                  ed<   ej8                  ed<   d(dedededefdZi Z eee!f   ed<   dede!fd Z"i Z#eee!f   ed!<   dede!fd"Z$ejJ                  ejJ                  ejJ                  ejJ                  e
jJ                  d#Z&ejN                  ejN                  ejN                  ejN                  e
jN                  d#Z(d$ Z%d% Z'dede!fd&Z)y# e$ r Y w xY w))zHandles dispatching array operations to the correct backend library, as well
as converting arrays to backend formats and then potentially storing them as
constants.
    N)AnyDictTuple)cupy)jax)object_arrays)
tensorflow)theano)torch)get_func
has_einsumhas_tensordotbuild_expressionevaluate_constantshas_backendz
dask.arrayztheano.tensorzopt_einsum.backends.torchz	jax.numpyzautograd.numpyzmars.tensor)daskr
   r   r   jaxlibautogradmarsfuncbackenddefaultreturnc                     	 t        j                  t        j                  ||            }|t	        ||       S t	        || |      S # t
        $ r d}t        |j                  ||             w xY w)zTry and import ``{backend}.{func}``.
    If library is installed and func is found, return the func;
    otherwise if default is provided, return default;
    otherwise raise an error.
    z{} doesn't seem to provide the function {} - see https://optimized-einsum.readthedocs.io/en/latest/backends.html for details on which functions are required for which contractions.)	importlibimport_module_aliasesgetgetattrAttributeErrorformat)r   r   r   lib	error_msgs        L/home/dcms/DCMS/lib/python3.12/site-packages/opt_einsum/backends/dispatch.py_import_funcr%   %   su    	>%%hll7G&DE%,_wsD!U'#tW:UU >R 	
 Y--gt<==>s   6A A 'A-)einsumobject_cached_funcs)	tensordotnumpy)	transposer*   )r&   r*   )r)   r'   )r+   r'   c                 l    	 t         | |f   S # t        $ r t        | ||      }|t         | |f<   |cY S w xY w)ziReturn ``{backend}.{func}``, e.g. ``numpy.einsum``,
    or a default func if provided. Cache result.
    )r(   KeyErrorr%   )r   r   r   fns       r$   r   r   J   sF    T7]++ $1')dGm$	s   
 #33_has_einsumc                     	 t         |    S # t        $ r; 	 t        d|        dt         | <   n# t        $ r dt         | <   Y nw xY wt         |    cY S w xY w)zCCheck if ``{backend}.einsum`` exists, cache result for performance.r&   TF)r/   r-   r   r    r   s    r$   r   r   Z   s`    	$7## $	)Xw'#'K  	)#(K 	) 7##$-    	A+AA AA  AA_has_tensordotc                     	 t         |    S # t        $ r; 	 t        d|        dt         | <   n# t        $ r dt         | <   Y nw xY wt         |    cY S w xY w)zFCheck if ``{backend}.tensordot`` exists, cache result for performance.r)   TF)r3   r-   r   r    r1   s    r$   r   r   k   s`    	'g&& '	,['*&*N7# 	,&+N7#	, g&&'r2   )r	   r
   r   r   r   c                      t        |    ||      S )zxBuild an expression, based on ``expr`` and initial arrays ``arrays``,
    that evaluates using backend ``backend``.
    )CONVERT_BACKENDSr   arraysexprs      r$   r   r      s     G$VT22    c                      t        |    ||      S )zConvert constant arrays to the correct backend, and perform as much of
    the contraction of ``expr`` with these as possible.
    )EVAL_CONSTS_BACKENDSr7   s      r$   r   r      s      (66r:   c                 .    | j                         t        v S )zChecks if the backend is known.)lowerr6   r1   s    r$   r   r      s    ==?...r:   )N)r*   N)*__doc__r   typingr   r   r   opt_einsum.backendsr   _cupyr   _jaxr   r	   _tensorflowr
   _theanor   _torch__all__r   strr%   object_einsumr(   __annotations__r*   npr)   r+   r&   ModuleNotFoundErrorr   r/   boolr   r3   r   r   r6   r   r<   r    r:   r$   <module>rO      s  
  # # - + - 9 1 / ( >s >S >3 ># >* -55-tE#s(OS() 
	,.LLM(),.LLM())+M%&-/\\M)*-/\\M)*
	3 	 	 	 	  "T#t)_ !$ $ $ #%S$Y $'3 '4 '" ..&&""$$    00(($$&&"" 37/ / /i  		s   /AE6 6E>=E>