
    bi)                        d dl mZ d dlZd dlZd dlmZ d dlmZm	Z	m
Z
 d dlmZmZ dZ ee      Zd dlmZ d Zd	 Z ee      dd
ddd       Zy)    )annotationsN)_calculate_new_chunksizes)asarray	blockwiseeinsum_lookup)
cached_maxderived_from4abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ)configc                    |j                  d      }|j                  d      }|j                  d      }t        j                  t        | d               } ||g| d|i|}|j	                  |j
                  d|z  z         S )N
subscriptsncontract_indskernel_dtyper   dtype)   )popr   dispatchtypereshapeshape)operandskwargsr   r   r   einsumchunks          Q/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/dask/array/einsumfuncs.pychunk_einsumr      s~    L)JZZ 01NJJ~&E##D!$56F:@@@@E ==tn'<<==    c                f
   t        |       dk(  rt        d      t        | d   t              rT| d   j	                  dd      }| dd D cg c]  }t        |       } }|D ]  }|dv r|t        vst        d|z         nBt        |       }g }g }t        t        |       d	z        D ]B  }|j                  |j                  d             |j                  |j                  d             D t        |      r|d
   nd}|D 	cg c]  }	t        |	       } }	d}t        |      dz
  }
t        |      D ]L  \  }}|D ]7  }|t        u r|dz  }t        |t              r|t        |   z  }.t        d       ||
k7  sH|dz  }N |A|dz  }|D ]7  }|t        u r|dz  }t        |t              r|t        |   z  }.t        d       d|v sd|v rK|j!                  d      dkD  xs |j!                  d      dkD  }|s|j!                  d      dk7  rt        d      d|v rG|j	                  dd      j	                  dd      j	                  dd      }t        t        t#        |      z
        }dj%                  |      }d}d|v r(|j'                  d      \  }}|j'                  d      }d}n|j'                  d      }d}t        |      D ]  \  }}d|v s|j!                  d      dk7  s|j!                  d      dk7  rt        d      | |   j(                  dk(  rd}n*t+        | |   j,                  d      }|t        |      dz
  z  }||kD  r|}|dk  rt        d      |dk(  r|j	                  dd      ||<   || d }|j	                  d|      ||<    dj%                  |      }|dk(  rd}n|| d }|r|dj	                  d|      z   z  }nd}|j	                  dd      }t/        t#        |            D ]2  }|t        vrt        d|z        |j!                  |      dk(  s.||z  }4 dj%                  t/        t#        |      t#        |      z
              }|d|z   |z   z  }d|v r|j'                  d      \  }}n_|}|j	                  dd      }d}t/        t#        |            D ]2  }|t        vrt        d|z        |j!                  |      dk(  s.||z  }4 |D ]  }||vst        d|z         t        |j'                  d            t        |       k7  rt        d      ||| fS c c}w c c}	w )a  
    A reproduction of numpy's _parse_einsum_input()
    which in itself is a reproduction of
    c side einsum parsing in python.

    Returns
    -------
    input_strings : str
        Parsed input strings
    output_string : str
        Parsed output string
    operands : list of array_like
        The operands to use in the numpy contraction
    Examples
    --------
    The operand list is simplified to reduce printing:
    >> a = np.random.rand(4, 4)
    >> b = np.random.rand(4, 4, 4)
    >> __parse_einsum_input(('...a,...a->...', a, b))
    ('za,xza', 'xz', [a, b])
    >> __parse_einsum_input((a, [Ellipsis, 0], b, [Ellipsis, 0]))
    ('za,xza', 'xz', [a, b])
    r   zNo input operands  r   Nz.,->z#Character %s is not a valid symbol.   z...z=For this input type lists must contain either int or Ellipsis,->->z%Subscripts can only contain one '->'..TF   zInvalid Ellipses. zEllipses lengths do not match.z/Output character %s did not appear in the inputzDNumber of einsum subscripts must be equal to the number of operands.)len
ValueError
isinstancestrreplacer   einsum_symbols_setlistrangeappendr   	enumerateEllipsisinteinsum_symbols	TypeErrorcountsetjoinsplitr   maxndimsorted)r   r   ostmp_operandsoperand_listsubscript_list_output_listvlastnumsubinvalidusedunusedellipse_indslongest	input_tmp
output_subsplit_subscriptsout_subellipse_countrep_indsout_ellipseoutput_subscripttmp_subscriptsnormal_indsinput_subscriptschars                                 r   parse_einsum_inputr[      s   2 8},--(1+s#a[((b1
(051GAJ55  	LAF{** !F!JKK		L H~s8})* 	7A 0 0 34!!,"2"21"56	7 +.l*;l2&(451GAJ55
>"Q&!.1 	"HC 	=%'J3'."33J#1 	 d{c!
	" "$J  	=%'J3'."33J#1 	 	zsj0##C(1,L*2B2B32G!2Kz''-2DEE j!!#r*223;CCD"M(3t945wwv:$.$4$4T$:!Iz(s3G)//4G!"23 	IHCczIIcNa'SYYu-=-B$%899 C=&&",$%M$'(:(:A$>M!SX\1M 7*+G 1$$%EFF"a',/KKr,B$S)+]NO<H,/KKx,H$S)+	I. XX./
a<K&xy1K$!3!3E;!GGGJ  "'//R8NC/0 *..$%JQ%NOO!''*a/$)$	*
 ''&-=)>[AQ)Q"RSK$,{::J z-7-=-=d-C**%#++C4N+, 	&A** !F!JKK##A&!+ A% 		& ! W''NQUUVVW
 !!#&'3x=8R
 	
 .99S 6$ 6s   	T)T.F)r   optimizesplit_everyc                J   | }t        |      \  }}}dj                  ||f      }	| *t        j                  |D 
cg c]  }
|
j                   c}
 } |durc|D 
cg c]<  }
t        j
                  |
j                  j                  d      |
j                        > }}
t        j                  |	g|d|i\  }}|j                  d      D cg c]  }t        |       }}|D ch c]  }|D ]  }|  }}}|t        |      z
  }t        |      }t        |      dkD  rt        |      dkD  ri d}}t        ||      D ]  \  }}t        t        j                   t#        t$        |j&                              |      }|j)                  t+        |      D ci c]<  \  }}||vr3|t        t%        |j&                  |         |j-                  |d            > c}}        t        j                   |j/                               }||t1        j,                  d      z  z  }g }t        ||      D ]  \  }}t+        |      D ch c]  \  }}||v s| }}}t        |t        |      t        |      z  z  d      }t3        |j&                  t5        |j&                        |t        j                   t#        t$        |j&                              |z        }|j7                  |j9                  |              |}t;        t<        t        |      t        |      z   gd	 t        ||      D        t>        jA                  |d      | |	|||d
|}|dkD  r4t        |      }|jC                  t5        tE        |||z               |      S |S c c}
w c c}
w c c}w c c}}w c c}}w c c}}w )a  Dask added an additional keyword-only argument ``split_every``.

    split_every: int >= 2 or dict(axis: int), optional
        Determines the depth of the recursive aggregation.
        Defaults to ``None`` which would let dask heuristically
        decide a good default.
    r$   Fr   )r   r\   r#   r   zarray.chunk-size-tolerancec              3  .   K   | ]  }|D ]  }|   y w)Nr)   ).0apas      r   	<genexpr>zeinsum.<locals>.<genexpr>  s     	3	31!	3!	3s   )adjust_chunksr   r   r   r   r\   )axisr]   )#r[   r:   npresult_typer   broadcast_tor   r   einsum_pathr;   tupler9   r*   zipr<   mathprodmapr   chunksupdater3   getvaluesr   r   r0   r2   rechunkr   r   dictfromkeyssumr1   )r   r\   r]   r   r   einsum_dtypeinputsoutputsopsr   r?   fake_opsrD   irb   all_indscontract_indsr   max_chunk_sizesmax_chunk_size_inputopinputinpmax_chunk_size_outputfactornew_opsctrchangeable_dimensionsfresultsizes                                  r   r   r      s    L-h7FGSFG,-J }# 6Q 67u NQQBOOAGGLLO177CQQnnZN(NXN! &S 121eAh2F2 "-a1-a--H- s7|+M'N
6{Q3w<!+02A-S&) 
	IB#&		#j"))457K$  "" #,E"23-/ Z		!57J7J3PQ7RSS	
	 !%		/*@*@*B C& 6::.J#KK
 S&) 		/IB7@7G$XVS!1PW<S$X!$XFs#89CLHI1MA.		RYY%		#j"))459	F NN2::f-.		/  g}-- 
4c#v&	3
 mmM15!% F  7|zzeD$"789{  
 	
 M[ !7 R 3 .  %Ys+   NAN	N$NAN?NN)
__future__r   rl   numpyrf   dask.array._shuffler   dask.array.corer   r   r   
dask.utilsr   r	   r6   r9   r/   daskr   r   r[   r   r)   r   r   <module>r      s[    "   9 = = /G(  	>g:T b 5d ] ]r   