
    sh,                   @   S SK Jr  S SKrS SKrS SKJrJr  S SKJr  S SK	r	S SK	J
r
  S SKrS SKJr  S SKJr  S SKJr  S S	KJr  S S
KJr  S SKJr  S SKJrJr  S SKJr  S SKJr  S SKJ r   S SK!J"r"J#r#  S SK$J%r%  S SK&J'r'  S SK(J)r)  S SK*J+r+  S SK,J-r-J.r.J/r/J0r0  S SK1J2r2  S SK3J4r4  S SK5J6r6J7r7  S SK(J8r8  S SK9J:r:J;r;J<r<J=r=J>r>J?r?  S SK@JArA  S SKBJCrC  S SKDJErE   " S S\5      rF " S  S!\F5      rG " S" S#\5      rH " S$ S%\F5      rI " S& S'\F5      rJ " S( S)\5      rK " S* S+\K5      rL " S, S-\K5      rM " S. S/\K5      rN " S0 S1\K5      rO " S2 S3\K5      rP " S4 S5\K5      rQ " S6 S7\K5      rR " S8 S95      rS " S: S;5      rT " S< S=5      rUS> rVS? rWS@ rXSA rYSB rZSC r[SD r\SE r]SF r^SG r_SH r`g)I    )annotationsN)defaultdictCounter)reduce)
accumulate)Integer)EqualityKroneckerDelta)Basic)Tuple)Expr)FunctionLambda)Mul)Sdefault_sort_key)DummySymbol)
MatrixBase)diagonalize_vector)
MatrixExpr)
ZeroMatrix)permutedimstensorcontractiontensordiagonaltensorproduct)ImmutableDenseNDimArray)	NDimArray)IndexedIndexedBase)MatrixElement)$_apply_recursively_over_nested_lists_sort_contraction_indices_get_mapping_from_subranks*_build_push_indices_up_func_transformation_get_contraction_links,_build_push_indices_down_func_transformationPermutation)
_af_invert)_sympifyc                  ,    \ rS rSr% S\S'   S rS rSrg)
_ArrayExpr'   ztuple[Expr, ...]shapec                    [        U[        R                  R                  5      (       d  U4n[        R                  X5        U R                  U5      $ N)
isinstancecollectionsabcIterableArrayElement_check_shape_getselfitems     ڄ/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/sympy/tensor/array/expressions/array_expressions.py__getitem___ArrayExpr.__getitem__*   s<    $ 8 8997D!!$-yy    c                    [        X5      $ r3   )_get_array_element_or_slicer;   s     r>   r:   _ArrayExpr._get0   s    *466rA    N)__name__
__module____qualname____firstlineno____annotations__r?   r:   __static_attributes__rE   rA   r>   r/   r/   '   s    7rA   r/   c                  L    \ rS rSrSrSrS
S jr\S 5       r\S 5       r	S r
Srg	)ArraySymbol4   z)
Symbol representing an array expression
Fc                    [        U[        5      (       a  [        U5      n[        [	        [
        U5      6 n[        R                  " XU5      nU$ r3   )r4   strr   r   mapr-   r   __new__)clssymbolr1   objs       r>   rR   ArraySymbol.__new__;   s>    fc""F^Fs8U+,ll3.
rA   c                     U R                   S   $ Nr   _argsr<   s    r>   nameArraySymbol.nameC       zz!}rA   c                     U R                   S   $ N   rY   r[   s    r>   r1   ArraySymbol.shapeG   r^   rA   c                D   [        S U R                   5       5      (       d  [        S5      e[        R                  " U R                   Vs/ s H  n[        U5      PM     sn6  Vs/ s H  o U   PM	     nn[        U5      R                  " U R                  6 $ s  snf s  snf )Nc              3  8   #    U  H  oR                   v   M     g 7fr3   
is_Integer.0is     r>   	<genexpr>*ArraySymbol.as_explicit.<locals>.<genexpr>L        4A<<   z1cannot express explicit array with symbolic shape)allr1   
ValueError	itertoolsproductranger   reshape)r<   jri   datas       r>   as_explicitArraySymbol.as_explicitK   s    4444PQQ!*!2!2tzz4Rz!U1Xz4R!ST!SAQ!ST&t,44djjAA 5STs   B&BrE   N)r1   ztyping.Iterablereturnz'ArraySymbol')rF   rG   rH   rI   __doc__	_iterablerR   propertyr\   r1   rv   rK   rE   rA   r>   rM   rM   4   sA     I    BrA   rM   c                  `    \ rS rSrSrSrSrSrS r\	S 5       r
\S 5       r\S 5       rS rS	rg
)r8   R   z
An element of an array.
Tc                ,   [        U[        5      (       a  [        U5      n[        U5      n[        U[        R
                  R                  5      (       d  U4n[        [        U5      5      nU R                  X5        [        R                  " XU5      nU$ r3   )r4   rP   r   r-   r5   r6   r7   tupler9   r   rR   )rS   r\   indicesrU   s       r>   rR   ArrayElement.__new__[   sp    dC  $<D~';??#;#;<<jG5>*'ll3g.
rA   c                H   [        U5      n[        US5      (       ad  [        S5      n[        U5      [        UR                  5      :w  a  Ue[        S [        X!R                  5       5       5      (       a  [        S5      e[        S U 5       5      (       a  [        S5      eg )Nr1   z3number of indices does not match shape of the arrayc              3  4   #    U  H  u  pX:  S :H  v   M     g7f)TNrE   )rh   ri   ss      r>   rj   ,ArrayElement._check_shape.<locals>.<genexpr>m   s     I0HAFt#0Hs   zshape is out of boundsc              3  0   #    U  H  oS :  S:H  v   M     g7f)r   TNrE   rg   s     r>   rj   r   o   s     01A$s   zshape contains negative values)r   hasattr
IndexErrorlenr1   anyzipro   )rS   r\   r   index_errors       r>   r9   ArrayElement._check_shapef   s    .4!!$%Z[K7|s4::.!!IGZZ0HIII !9::0000=>> 1rA   c                     U R                   S   $ rX   rY   r[   s    r>   r\   ArrayElement.namer   r^   rA   c                     U R                   S   $ r`   rY   r[   s    r>   r   ArrayElement.indicesv   r^   rA   c                @   [        U[        5      (       d  [        R                  $ X:X  a  [        R                  $ UR
                  U R
                  :w  a  [        R                  $ [        R                  " S [        U R                  UR                  5       5       5      $ )Nc              3  <   #    U  H  u  p[        X5      v   M     g 7fr3   r
   rh   ri   rt   s      r>   rj   0ArrayElement._eval_derivative.<locals>.<genexpr>   s     Z=YTQN100=Ys   )
r4   r8   r   ZeroOner\   r   fromiterr   r   )r<   r   s     r>   _eval_derivativeArrayElement._eval_derivativez   sc    !\**66M955L66TYY66M||ZSqyy=YZZZrA   rE   N)rF   rG   rH   rI   ry   	_diff_wrt	is_symbolis_commutativerR   classmethodr9   r{   r\   r   r   rK   rE   rA   r>   r8   r8   R   s_     IIN	 	? 	?    
[rA   r8   c                  :    \ rS rSrSrS r\S 5       rS rS r	Sr
g)		ZeroArray   zE
Symbolic array of zeros. Equivalent to ``ZeroMatrix`` for matrices.
c                    [        U5      S:X  a  [        R                  $ [        [        U5      n[
        R                  " U /UQ76 nU$ rX   )r   r   r   rQ   r-   r   rR   rS   r1   rU   s      r>   rR   ZeroArray.__new__   s:    u:?66MHe$ll3''
rA   c                    U R                   $ r3   rY   r[   s    r>   r1   ZeroArray.shape       zzrA   c                    [        S U R                   5       5      (       d  [        S5      e[        R                  " U R                  6 $ )Nc              3  8   #    U  H  oR                   v   M     g 7fr3   re   rg   s     r>   rj   (ZeroArray.as_explicit.<locals>.<genexpr>   rl   rm   /Cannot return explicit form for symbolic shape.)rn   r1   ro   r   zerosr[   s    r>   rv   ZeroArray.as_explicit   s8    4444NOO&,,djj99rA   c                "    [         R                  $ r3   )r   r   r;   s     r>   r:   ZeroArray._get   s    vvrA   rE   NrF   rG   rH   rI   ry   rR   r{   r1   rv   r:   rK   rE   rA   r>   r   r      s*      :
rA   r   c                  :    \ rS rSrSrS r\S 5       rS rS r	Sr
g)	OneArray   z
Symbolic array of ones.
c                    [        U5      S:X  a  [        R                  $ [        [        U5      n[
        R                  " U /UQ76 nU$ rX   )r   r   r   rQ   r-   r   rR   r   s      r>   rR   OneArray.__new__   s:    u:?55LHe$ll3''
rA   c                    U R                   $ r3   rY   r[   s    r>   r1   OneArray.shape   r   rA   c                <   [        S U R                   5       5      (       d  [        S5      e[        [	        [        [        R                  U R                  5      5       Vs/ s H  n[        R                  PM     sn5      R                  " U R                  6 $ s  snf )Nc              3  8   #    U  H  oR                   v   M     g 7fr3   re   rg   s     r>   rj   'OneArray.as_explicit.<locals>.<genexpr>   rl   rm   r   )rn   r1   ro   r   rr   r   operatormulr   r   rs   )r<   ri   s     r>   rv   OneArray.as_explicit   so    4444NOO&uVHLLRVR\R\=]7^'_7^!7^'_`hhjnjtjtuu'_s   !Bc                "    [         R                  $ r3   )r   r   r;   s     r>   r:   OneArray._get   s    uurA   rE   Nr   rE   rA   r>   r   r      s+      v
rA   r   c                  @    \ rS rSr\S 5       rS r\S 5       rS rSr	g)_CodegenArrayAbstract   c                     U R                   SS $ )aN  
Returns the ranks of the objects in the uppermost tensor product inside
the current object.  In case no tensor products are contained, return
the atomic ranks.

Examples
========

>>> from sympy.tensor.array import tensorproduct, tensorcontraction
>>> from sympy import MatrixSymbol
>>> M = MatrixSymbol("M", 3, 3)
>>> N = MatrixSymbol("N", 3, 3)
>>> P = MatrixSymbol("P", 3, 3)

Important: do not confuse the rank of the matrix with the rank of an array.

>>> tp = tensorproduct(M, N, P)
>>> tp.subranks
[2, 2, 2]

>>> co = tensorcontraction(tp, (1, 2), (3, 4))
>>> co.subranks
[2, 2, 2]
N)	_subranksr[   s    r>   subranks_CodegenArrayAbstract.subranks   s    4 ~~a  rA   c                ,    [        U R                  5      $ )z
The sum of ``subranks``.
)sumr   r[   s    r>   subrank_CodegenArrayAbstract.subrank   s     4==!!rA   c                    U R                   $ r3   _shaper[   s    r>   r1   _CodegenArrayAbstract.shape       {{rA   c           
         UR                  SS5      nU(       aG  U R                  " U R                   Vs/ s H  o3R                  " S0 UD6PM     sn6 R	                  5       $ U R	                  5       $ s  snf )NdeepTrE   )getfuncargsdoit_canonicalize)r<   hintsr   args       r>   r   _CodegenArrayAbstract.doit   s_    yy&99DIIFISxx0%0IFGUUWW%%'' Gs   A1rE   N)
rF   rG   rH   rI   r{   r   r   r1   r   rK   rE   rA   r>   r   r      s2    ! !6"  (rA   r   c                  :    \ rS rSrSrS rS r\S 5       rS r	Sr
g)	ArrayTensorProduct   z>
Class to represent the tensor product of array-like objects.
c                   U Vs/ s H  n[        U5      PM     nnUR                  SS5      nU Vs/ s H  n[        U5      PM     nn[        R                  " U /UQ76 nXVl        U Vs/ s H  n[        U5      PM     nn[        S U 5       5      (       a  S Ul        O[        S U 5       5      Ul        U(       a  UR                  5       $ U$ s  snf s  snf s  snf )NcanonicalizeFc              3  (   #    U  H  oS L v   M
     g 7fr3   rE   rg   s     r>   rj   -ArrayTensorProduct.__new__.<locals>.<genexpr>        )&QDy&   c              3  6   #    U  H  o  H  o"v   M     M     g 7fr3   rE   r   s      r>   rj   r      s     <&Q!Qq!q&s   )r-   popget_rankr   rR   r   	get_shaper   r   r   r   )	rS   r   kwargsr   r   ranksrU   ri   shapess	            r>   rR   ArrayTensorProduct.__new__   s    )-.#.zz.%8*./$3#$/mmC'$'(,-1)A,-)&)))CJ<&<<CJ$$&&
! / 0 .s   CC*Cc                  ^^^ U R                   nU R                  U5      nU Vs/ s H  n[        U5      PM     nn/ n[        U5       H  u  mn[	        U[
        5      (       d  M  UR                  UR                  R                   VVs/ s H#  oU Vs/ s H  of[        US T 5      -   PM     snPM%     snn5        UR                  UT'   M     U(       a3  [        [        U6 [        [        U5      S-
  5      [        U5      -  5      $ [        U5      S:X  a  US   $ [        S U 5       5      (       a=  [!        ["        R$                  U Vs/ s H  n['        U5      PM     snS5      n[)        U6 $ [        U5       VVs0 s H  u  pr[	        U[*        5      (       d  M  Xr_M      n	nnU	(       a  U Vs/ s H.  n[	        U[*        5      (       a  [-        U5      O
[        U5      PM0     nn[/        [1        S/U-   5      5      S S m[        U Vs/ s H&  n[	        U[*        5      (       a  UR                  OUPM(     sn6 n
U	R3                  5        V^VVs/ s H/  u  mo"R4                    H  n[7        UU4S jU 5       5      PM     M1     nnnn[9        U
/UQ76 $ [        U5       VVs0 s H  u  pr[	        U[:        5      (       d  M  Xr_M      nnnU(       GaJ  / n/ nU Vs/ s H  n[        U5      PM     nn[/        [1        S/U-   5      5      S S m[        U5       H  u  mn[	        U[:        5      (       a  [        U5      [        UR<                  5      -
  n[        UR<                  5      nUR                  [?        U5       Vs/ s H  nTT   U-   PM     sn5        UR                  [?        XU-   5       Vs/ s H  nTT   U-   PM     sn5        M  UR                  [?        [        U5      5       Vs/ s H  nTT   U-   PM     sn5        M     UR                  U5        [        U Vs/ s H&  n[	        U[:        5      (       a  UR                  OUPM(     sn6 n
U Vs/ s H.  n[	        U[:        5      (       a  [-        U5      O
[        U5      PM0     nn[/        [1        S/U-   5      5      S S mUR3                  5        V^VVs/ s H/  u  mo"R<                    H  n[7        UU4S jU 5       5      PM     M1     nnnn[        [A        U
/UQ76 [C        U5      5      $ U RD                  " USS	06$ s  snf s  snf s  snnf s  snf s  snnf s  snf s  snf s  snnnf s  snnf s  snf s  snf s  snf s  snf s  snf s  snf s  snnnf )
Nra   r   c              3  N   #    U  H  n[        U[        [        45      v   M     g 7fr3   )r4   r   r   )rh   r   s     r>   rj   3ArrayTensorProduct._canonicalize.<locals>.<genexpr>  s     H4Cz#	:6774s   #%rE   c              3  4   >#    U  H  nTT   U-   v   M     g 7fr3   rE   )rh   kcumulative_ranksri   s     r>   rj   r     s     (L!Q)9!)<q)@!   c              3  4   >#    U  H  nTT   U-   v   M     g 7fr3   rE   )rh   r   cumulative_ranks2ri   s     r>   rj   r   4  s     %J1&7&:Q&>r   r   F)#r   _flattenr   	enumerater4   PermuteDimsextendpermutationcyclic_formr   expr_permute_dims_array_tensor_productr+   r   r   r   r   addr   r   ArrayContraction_get_subranklistr   itemscontraction_indicesr   _array_contractionArrayDiagonaldiagonal_indicesrr   _array_diagonalr,   r   )r<   r   r   r   permutation_cyclesrt   r   ri   r   contractionstpr  	diagonalsinverse_permutation	last_permi1i2ranks2r  r   r   s          `           @@r>   r    ArrayTensorProduct._canonicalize   s   yy}}T"*./$3#$/  oFAsc;//%%PSP_P_PkPk&lPk1A'FAqCbq	N(:A'FPk&lmhhDG	 &
  !6!={3u:VW<?XYdewYx?xyyt9>7N H4HHHHLL*FA9Q<*FKFf%% .7t_b_61
3P`@a_bjnojncf*S:J*K*K\#&QYZ]Q^^jnEo#JsU{$;<SbA&ko(pkodgZEU5V5V\_)_ko(pqB[g[m[m[o  #R[oQWQRTW  zQ  zQtu5(L!(L#L  zQ#L[o  #R%b?+>??*3D/\/Z]=[VQV/	\"$I.23dsXc]dE3#JsU{$;<SbA#D/3c=11!#S-A-A)BBBS112B'..QVWYQZ/[QZA0@0Ca0GQZ/[\$$uRVXQXGY%ZGY!&6q&9A&=GY%Z['..QVW_`cWdQe/fQeA0@0Ca0GQe/fg *  &&y1&hl(mhladZ]5S5SY\)\hl(mnBhlmhlad:c=+I+Il3'xX[}\hlFm $Zf%= >s CYbYhYhYj   JYjvqRU  uI  uIop%J%J J  uI JYj   J !G6F!GTgIhiiyy$3U33g 0 (G&l +G
 co(p #R ] 4 0\%Z/f(mm  Jsw   T3
	T=T8-T=U
?UU25U-U6U'UUU%U*
9U/
5U4
,-U9!5U>6V8T=c                    U VVs/ s H+  n[        X 5      (       a  UR                  OU/  H  o3PM     M-     nnnU$ s  snnf r3   )r4   r   )rS   r   r   ri   s       r>   r   ArrayTensorProduct._flatten9  sA    !YTc
38L8LCHHSVRW,Wa,WTY Zs   2=c           	         [        U R                   Vs/ s H&  n[        US5      (       a  UR                  5       OUPM(     sn6 $ s  snf Nrv   )r   r   r   rv   r<   r   s     r>   rv   ArrayTensorProduct.as_explicit>  sB    dhdmdmndm]`GC4O4Os0UXXdmnoons   -ArE   N)rF   rG   rH   rI   ry   rR   r   r   r   rv   rK   rE   rA   r>   r   r      s,    &74r  prA   r   c                  :    \ rS rSrSrS rS r\S 5       rS r	Sr
g)	ArrayAddiB  z(
Class for elementwise array additions.
c                h   U Vs/ s H  n[        U5      PM     nnU Vs/ s H  n[        U5      PM     nn[        [        U5      5      n[	        U5      S:w  a  [        S5      eU Vs/ s H  o3R                  PM     nn[	        U Vs1 s H	  ofc  M  UiM     sn5      S:  a  [        S5      eUR                  SS5      n[        R                  " U /UQ76 nXHl
        [        S U 5       5      (       a  S Ul        O
US   Ul        U(       a  UR                  5       $ U$ s  snf s  snf s  snf s  snf )Nra   z!summing arrays of different rankszmismatching shapes in additionr   Fc              3  (   #    U  H  oS L v   M
     g 7fr3   rE   rg   s     r>   rj   #ArrayAdd.__new__.<locals>.<genexpr>U  r   r   r   )r-   r   r  setr   ro   r1   r   r   rR   r   r   r   r   )	rS   r   r   r   r   r   ri   r   rU   s	            r>   rR   ArrayAdd.__new__G  s   )-.#.*./$3#$/SZ u:?@AA'+,t))t,636a634q8=>>zz.%8mmC'$')&)))CJCJ$$&&
' // -3s   D D%)D*	D/D/c                   U R                   nU R                  U5      nU Vs/ s H  n[        U5      PM     nnU Vs/ s H"  n[        U[        [
        45      (       a  M   UPM$     nn[        U5      S:X  a-  [        S U 5       5      (       a  [        S5      e[	        US   6 $ [        U5      S:X  a  US   $ U R                  " USS06$ s  snf s  snf )Nr   c              3  .   #    U  H  ob  M  Uv   M     g 7fr3   rE   rg   s     r>   rj   )ArrayAdd._canonicalize.<locals>.<genexpr>f  s     2f11fs   	zIcannot handle addition of ZeroMatrix/ZeroArray and undefined shape objectra   r   F)
r   _flatten_argsr   r4   r   r   r   r   NotImplementedErrorr   )r<   r   r   r   s       r>   r   ArrayAdd._canonicalize]  s    yy !!$',01DS)C.D1#Tt:cIz;R+StTt9>2f222)*uvvfQi((Y!^7Nyy$3U33 2Ts   C
C Cc                    / nU HF  n[        U[        5      (       a  UR                  UR                  5        M5  UR	                  U5        MH     U$ r3   )r4   r  r   r   append)rS   r   new_argsr   s       r>   r"  ArrayAdd._flatten_argsm  sA    C#x(()$	 
 rA   c           
         [        [        R                  U R                   Vs/ s H&  n[	        US5      (       a  UR                  5       OUPM(     sn5      $ s  snf r  )r   r   r   r   r   rv   r  s     r>   rv   ArrayAdd.as_explicitw  sM    LLRVR[R[\R[3'#}"="=S__3FR[\^ 	^\s   -A
rE   N)rF   rG   rH   rI   ry   rR   r   r   r"  rv   rK   rE   rA   r>   r  r  B  s+    ,4   ^rA   r  c                      \ rS rSrSrSS jrS r\S 5       r\S 5       r	\
S 5       r\
S	 5       r\
S
 5       r\
S 5       rS r\
S 5       rS r\
S 5       r\
S 5       rSrg)r   i}  a  
Class to represent permutation of axes of arrays.

Examples
========

>>> from sympy.tensor.array import permutedims
>>> from sympy import MatrixSymbol
>>> M = MatrixSymbol("M", 3, 3)
>>> cg = permutedims(M, [1, 0])

The object ``cg`` represents the transposition of ``M``, as the permutation
``[1, 0]`` will act on its indices by switching them:

`M_{ij} \Rightarrow M_{ji}`

This is evident when transforming back to matrix form:

>>> from sympy.tensor.array.expressions.from_array_to_matrix import convert_array_to_matrix
>>> convert_array_to_matrix(cg)
M.T

>>> N = MatrixSymbol("N", 3, 2)
>>> cg = permutedims(N, [1, 0])
>>> cg.shape
(2, 3)

There are optional parameters that can be used as alternative to the permutation:

>>> from sympy.tensor.array.expressions import ArraySymbol, PermuteDims
>>> M = ArraySymbol("M", (1, 2, 3, 4, 5))
>>> expr = PermuteDims(M, index_order_old="ijklm", index_order_new="kijml")
>>> expr
PermuteDims(M, (0 2 1)(3 4))
>>> expr.shape
(3, 1, 2, 5, 4)

Permutations of tensor products are simplified in order to achieve a
standard form:

>>> from sympy.tensor.array import tensorproduct
>>> M = MatrixSymbol("M", 4, 5)
>>> tp = tensorproduct(M, N)
>>> tp.shape
(4, 5, 3, 2)
>>> perm1 = permutedims(tp, [2, 3, 1, 0])

The args ``(M, N)`` have been sorted and the permutation has been
simplified, the expression is equivalent:

>>> perm1.expr.args
(N, M)
>>> perm1.shape
(3, 2, 5, 4)
>>> perm1.permutation
(2 3)

The permutation in its array form has been simplified from
``[2, 3, 1, 0]`` to ``[0, 1, 3, 2]``, as the arguments of the tensor
product `M` and `N` have been switched:

>>> perm1.permutation.array_form
[0, 1, 3, 2]

We can nest a second permutation:

>>> perm2 = permutedims(perm1, [1, 0, 2, 3])
>>> perm2.shape
(2, 3, 5, 4)
>>> perm2.permutation.array_form
[1, 0, 3, 2]
Nc                  ^^ SSK Jn  [        U5      n[        U5      nU R	                  TX4U5      mU" T5      mTR
                  nX:w  a  [        S5      eUR                  SS5      n	[        R                  " XT5      n
[        U5      /U
l
        [        U5      mTc  S U
l        O-[        UU4S j[        [        T5      5       5       5      U
l        U	(       a  U
R!                  5       $ U
$ )Nr   r*   z8Permutation size must be the length of the shape of exprr   Fc              3  :   >#    U  H  nTT" U5         v   M     g 7fr3   rE   )rh   ri   r   r1   s     r>   rj   &PermuteDims.__new__.<locals>.<genexpr>  s     P>Ou[^4>Os   )sympy.combinatoricsr+   r-   r   _get_permutation_from_argumentssizero   r   r   rR   r   r   r   r   rr   r   r   )rS   r   r   index_order_oldindex_order_newr   r+   	expr_rankpermutation_sizer   rU   r1   s     `        @r>   rR   PermuteDims.__new__  s    3~TN	99+irs!+.&++(WXXzz.%8mmC{3!$($=CJPeCJ>OPPCJ$$&&
rA   c                0   U R                   nU R                  n[        U[        5      (       a  UR                   nUR                  nX$-  nUn[        U[        5      (       a  U R                  X5      u  p[        U[        5      (       a  U R                  X5      u  p[        U[        [        45      (       a/  [        UR                   Vs/ s H  oQR                  U   PM     sn6 $ UR                  nU[        U5      :X  a  U$ U R                  XSS9$ s  snf )NF)r   )r   r   r4   r   r   '_PermuteDims_denestarg_ArrayContractionr   )_PermuteDims_denestarg_ArrayTensorProductr   r   
array_formr1   sortedr   )r<   r   r   subexprsubpermri   plists          r>   r   PermuteDims._canonicalize  s    yy&&dK((iiG&&G%/KDd,-- $ L LT _Dd.// $ N Nt aDdY
344k6L6LM6Lzz!}6LMNN&&F5M!Kyyy??	 Ns   
Dc                     U R                   S   $ rX   r   r[   s    r>   r   PermuteDims.expr      yy|rA   c                     U R                   S   $ r`   rA  r[   s    r>   r   PermuteDims.permutation  rC  rA   c           
        [        UR                  5      n[        UR                  5      n[        [	        S/UR
                  -   5      5      n[        [        U5      5       Vs/ s H  ocXV   XVS-       PM     nn[        U5       VVs/ s H  u  phU[        U5      4PM     n	nnU	R                  S S9  U	 Vs/ s H  ofS   PM	     n
nU
 Vs/ s H  odU   PM	     nnU
 Vs/ s H  ogU   PM	     nn[        [        U VVs/ s H  of  H  oPM     M     snn5      5      n[        U6 U4$ s  snf s  snnf s  snf s  snf s  snf s  snnf )Nr   ra   c                    U S   $ r`   rE   xs    r>   <lambda>GPermuteDims._PermuteDims_denestarg_ArrayTensorProduct.<locals>.<lambda>  s    adrA   key)r,   r:  r  r   r   r   rr   r   r   r;  sortr+   r   )rS   r   r   perm_image_formr   cumulri   perm_image_form_in_componentscomppsperm_args_image_formargs_sortedperm_image_form_sorted_argsrt   new_permutations                  r>   r9  5PermuteDims._PermuteDims_denestarg_ArrayTensorProduct  sC    %[%;%;<DIIZdmm 345 X]]`ae]fWg(hWgRS%!*)MWg%(h/89V/WX/WGAq&,/WX 	N#.01b!b1(<=(<1Aw(<=Qe&fQeAQ'GQe#&f%j=X1d=Xbc]^!bc!=X1d&ef$k2OCC )iX  2=&f1ds$   #D)D.8D4D9 D>?Ec                2  ^ [        U[        5      (       d  X4$ [        UR                  [        5      (       d  X4$ UR                  R                  nUR                  R                   Vs/ s H  n[        U5      PM     nnUR                  nU VVs/ s H  ow  H  oPM     M     n	nn[        [        S/U-   5      5      n
/ n[        UR                  5      nSn[        [        U5      5       HO  n/ n[        X   XS-      5       H"  nX;   a  M
  UR                  X   5        US-  nM$     UR                  U5        MQ     [        UR                  5       VVs/ s H!  u  p[        [        X   XS-      5      5      PM#     nnnUR!                  UR                  U5      nUS-  nU VVs/ s H  ow Vs/ s H  oc  M  U" U5      PM     snPM!     nnn[        [        U5      5      nUR#                  S S9  U Vs/ s H  owS   PM	     nnU Vs/ s H  nUU   PM
     nn[        U VVs/ s H  ow  H  oPM     M     snn5      mU Vs/ s H  osU   PM	     nnU Vs/ s H  n[%        U4S jU 5       5      PM     nn['        [)        U6 /UQ76 n[+        [        U Vs/ s H  nUU   PM
     sn VVs/ s H  ow  H  oPM     M     snn5      5      nUU4$ s  snf s  snnf s  snnf s  snf s  snnf s  snf s  snf s  snnf s  snf s  snf s  snf s  snnf )Nr   ra   r   c                    U S   $ r`   rE   rH  s    r>   rJ  EPermuteDims._PermuteDims_denestarg_ArrayContraction.<locals>.<lambda>6  s    !rA   rL  c              3  .   >#    U  H
  nTU   v   M     g 7fr3   rE   )rh   rt   new_index_perm_array_forms     r>   rj   FPermuteDims._PermuteDims_denestarg_ArrayContraction.<locals>.<genexpr>>  s     (Qq!)B1)Eq   )r4   r   r   r   r   r   r  r  r   r,   r:  rr   r   r&  r   r   _push_indices_uprN  r   r  r   r+   )rS   r   r   r   r   r   r  ri   rt   contraction_indices_flatrP  permutation_array_blocks_up
image_formcountercurrenteindex_blocksindex_blocks_upr  index_blocks_up_permutedsorting_keysnew_perm_image_formnew_index_blocksr'  new_contraction_indicesnew_exprr   rW  r]  s                               @r>   r8  3PermuteDims._PermuteDims_denestarg_ArrayContraction  s   $ 011$$$))%788$$yy~~-1YY^^<^cHSM^<"66/B#N/B!AqAAA/B #NZh/0 ')# 6 67
s8}%AG58UQ3Z00z231	 1
 (..w7 & GPPTP]P]F^_F^daU58UQ3Z89F^_//0H0H,W)B/bq#rbq]^Q$XQ%;%8%;Q$Xbq #r I&>?@n- .::\t\:5HI5HLO5HI$.;K/W;KaUVPQUV;K/W$X!%89%8G%89[n"o[nVW5(Qq(Q#Q[n"o%&;X&FaI`a%jfy=zfyab>YZ[>\fy=z  2G=z  EF  @A!  EF!=z  2G  'H  I((Q = $O$ ` %Y#r ;I/W9"o=z  2GsZ   *KK(K#*	K.3K)=K)	K.;K4K9*K>

LL	L4L)K.c                   UR                   n[        UR                  5       VVVs/ s H&  u  pE[        UR                   U   5        H  odPM     M(     nnnn[        UR	                  5       5       Vs/ s H
  oB" U5      PM     nn[        UR                  5      n	XxS      n
/ n/ n[        5       n[        U5       H  u  pNX~   U
:w  a  UR                  U5        / nX~   n
UR                  U5        X:   n[        U5      U:X  d  MJ  UR                  [        U5      5        U Vs/ s H  of[        U5      -
  PM     nnXt   n[        U	U   [        U5      5      U	U'   UR                  U
5        / nM     UR                  U5        [        [        [        U	5      5      5      n0 nS/[        [        U5      5      -   n[        [        U5      5       H^  n[        UU   UUS-      5       Vs1 s H	  ogX      iM     nn[        U5      S:w  a  M=  [!        [#        U5      5      nUU:w  d  MY  UUU'   M`     / n/ nU(       a  [        U5      S:X  a%  UR%                  5       u  nnUR                  U5        O US   nUU;  a  / nMJ  UR'                  U5      nUU;   a  UR                  U5        / nMv  UR                  U5        U(       a  M  U H/  n[        U5       H  u  nnUUUUS-   [        U5      -     '   M     M1     [        U5       VVVs/ s H*  u  nn[        U5       Vs/ s H  olUU   U-      PM     snPM,     nnnnU Vs/ s H  oIU   PM	     n	nU Vs/ s H  nUU   PM
     nnU VVs/ s H  oD  H  ofPM     M     nnn[)        U	6 [        U5      4$ s  snnnf s  snf s  snf s  snf s  snf s  snnnf s  snf s  snf s  snnf Nr   ra   r   )r   r   r   rr   r   r  r  r   r&  r   r;  minr   r+   r   r   nextiterpopitemr   r   ) rS   r   r   r   ri   r   rt   	index2argpermuted_indicesr'  arg_candidate_indexcurrent_indicesrW  inserted_arg_cand_indicesidxarg_candidate_ranklocal_current_indicesr  args_positionsmapscumulative_subranksr   elemlinescurrent_liner   vlinerf  permutation_blocksnew_permutation_blocksnew_permutation2s                                    r>   _check_permutation_mapping&PermuteDims._check_permutation_mappingC  s   ==%.tyy%9[%9615WXIYCZaQCZQ%9	[49$,,.4IJ4IqKN4IJ		?'(;<$'E! 01FA~!44&&7"$&/n#""3'!)!>?#'99&&vo'>?KZ([?aS-A)A?%([\,Xb\;G\;]^)--.AB"$ 2 	/ eCM23 cDH)=$>>s8}%A8=>QRS>TVijklmjmVn8op8o1?-.8oAp1v{Q=DDyQ & < A%||~1##A& $D=#%LHHQKL \*!" d D!$1<=tQUc$i$789 ( 
 ktt|j}~j}bfbcefTYZ[T\]T\q/B1/E/IJT\]j}~)78AQK8AO!PA"4Q"7!P'=I'=!q!AqA'=I$h/=M1NNNA \J )\ q< ^~8!PIsA   -N2N"N 'N%N/N*1N/ N6N;*O *N/c                   [        UR                  5      nUR                  nUR                  nS/[        [	        U5      5      -   nU Vs/ s H  n[        U5      PM     nnU Vs/ s H  n[        U5      PM     n	n/ n
[        U5       H  u  p{Sn[        [        U5      S-
  5       HO  nX   Xm   :  d  M  X   XmS-      :  d  M  [        X=   [        U Vs/ s H	  oXm   -
  PM     sn/5      5      X='   Sn  O   U(       d  Mz  U
R                  U5        M     [        U6 [        XR                  S94$ s  snf s  snf s  snf )Nr   Tra   F)r1  )r  r   r   r   r   rr  maxr   rr   r   r   r+   r&  r   r1  )rS   r   r   r   r   r   r  ri   
cyclic_min
cyclic_maxcyclic_keepcycleflagrt   r   s                  r>   !_check_if_there_are_closed_cycles-PermuteDims._check_if_there_are_closed_cycles  s;   DII==!-- cDH)=$>>&12kc!fk
2&12kc!fk
2!+.HAD323a78=$7$::z}ObefcfOg?g+DG[glBmglbcGZG]C]glBmAn5opDG D 9 t""5) / %d+[K[K[-\\\ 32 Cns   
D3%D8D=c                \    U R                  U R                  U R                  5      nUc  U $ U$ )z
DEPRECATED.
)_nest_permutationr   r   )r<   rets     r>   nest_permutationPermuteDims.nest_permutation  s/     $$TYY0@0@A;K
rA   c           	       ^ [        U[        5      (       a  [        U R                  X5      6 $ [        U[        5      (       a}  UR
                  n[        R                  " U/UQ76 n[        U5      mUR                   Vs/ s H  n[        U4S jU 5       5      PM     nn[        [        UR                  T5      /UQ76 $ [        U[        5      (       a,  [        UR                   Vs/ s H  n[        Xr5      PM     sn6 $ g s  snf s  snf )Nc              3  4   >#    U  H  nT" U5      v   M     g 7fr3   rE   )rh   rt   newpermutations     r>   rj   0PermuteDims._nest_permutation.<locals>.<genexpr>  s     &D!Q~a'8'8!r   )r4   r   r   r  r   r   '_convert_outer_indices_to_inner_indicesr+   r  r   r  r   r   r  
_array_addr   )	rS   r   r   cycles	newcyclesri   new_contr_indicesr   r  s	           @r>   r  PermuteDims._nest_permutation  s    d.// #"G"G"Z[[.// ,,F(PPQU_X^_I(3NNRNfNf gNf&D!&D!DNf g%k$))^&LaO`aah''S#C =STT	 !h  Ts   ?D(Dc                    U R                   n[        US5      (       a  UR                  5       n[        XR                  5      $ r  )r   r   rv   r   r   r<   r   s     r>   rv   PermuteDims.as_explicit  s7    yy4''##%D4!1!122rA   c                    Uc'  Ub  Uc  [        S5      e[        R                  X#U5      $ Ub  [        S5      eUb  [        S5      eU$ )NzPermutation not definedz2index_order_new cannot be defined with permutationz2index_order_old cannot be defined with permutation)ro   r   "_get_permutation_from_index_orders)rS   r   r2  r3  dims        r>   r0  +PermuteDims._get_permutation_from_arguments  s]    &/*A !:;;AA/dghh* !UVV* !UVVrA   c                \   [        [        U5      5      U:w  a  [        S5      e[        [        U5      5      U:w  a  [        S5      e[        [        R                  [        U5      [        U5      5      5      S:  a  [        S5      eU Vs/ s H  oAR	                  U5      PM     nnU$ s  snf )Nz*wrong number of indices in index_order_newz*wrong number of indices in index_order_oldr   z>index_order_new and index_order_old must have the same indices)r   r  ro   symmetric_differenceindex)rS   r2  r3  r  ri   r   s         r>   r  .PermuteDims._get_permutation_from_index_orders  s    s?#$+IJJs?#$+IJJs''O(<c/>RSTWXX]^^9HIA,,Q/I Js   B)rE   )NNN)rF   rG   rH   rI   ry   rR   r   r{   r   r   r   r9  r8  r  r  r  r  rv   r0  r  rK   rE   rA   r>   r   r   }  s    GR.@&     D D0 .) .)` BO BOH ] ](  3 
 
  rA   r   c                      \ rS rSrSrS rS r\S 5       r\S 5       r	\
S 5       r\
S 5       r\S	 5       r\S
 5       r\S 5       r\SS j5       rS rS r\S 5       r\S 5       r\S 5       rS rSrg)r  i  a  
Class to represent the diagonal operator.

Explanation
===========

In a 2-dimensional array it returns the diagonal, this looks like the
operation:

`A_{ij} \rightarrow A_{ii}`

The diagonal over axes 1 and 2 (the second and third) of the tensor product
of two 2-dimensional arrays `A \otimes B` is

`\Big[ A_{ab} B_{cd} \Big]_{abcd} \rightarrow \Big[ A_{ai} B_{id} \Big]_{adi}`

In this last example the array expression has been reduced from
4-dimensional to 3-dimensional. Notice that no contraction has occurred,
rather there is a new index `i` for the diagonal, contraction would have
reduced the array to 2 dimensions.

Notice that the diagonalized out dimensions are added as new dimensions at
the end of the indices.
c                   [        U5      nU Vs/ s H  n[        [        U5      6 PM     nnUR                  SS5      n[	        U5      nUb*  U R
                  " U/UQ70 UD6  U R                  Xb5      u  pvOS n[        U5      S:X  a  U$ [        R                  " X/UQ76 nXxl
        [        U5      Ul        Xhl        U(       a  UR                  5       $ U$ s  snf )Nr   Fr   )r-   r   r;  r   r   	_validate_get_positions_shaper   r   rR   
_positions_get_subranksr   r   r   )	rS   r   r  r   ri   r   r1   	positionsrU   s	            r>   rR   ArrayDiagonal.__new__  s    ~7GH7G!E6!9-7GHzz.%8$MM$<!1<V<"77PIuI A%KmmC9(89"%d+
$$&&
% Is   Cc                f   U R                   nU R                  nU Vs/ s H  n[        U5      S:X  d  M  UPM     nn[        U5      S:  Ga  [        U5       VVs0 s H  u  p5[        U5      S:X  d  M  US   U_M     nnn[        U5       VVs0 s H  u  p5[        U5      S:  d  M  XS_M     nnnU Vs/ s H  n[        U5      S:  d  M  UPM     nn[	        U 5      n	[        U5      n
X-
  n/ nSn[
        R                  U[        [        U	5      5      [	        U5      5      nU Hi  nX6;   a  UR                  XU   -   5        M   [        U[        [        45      (       a  UR                  U5        US-  nMS  UR                  XU   -   5        Mk     [        U5      n[        U5      S:  a  [        [        U/UQ76 U5      $ [        X5      $ [        U[         5      (       a  U R"                  " U/UQ76 $ [        U[
        5      (       a  U R$                  " U/UQ76 $ [        U[&        5      (       a  U R(                  " U/UQ76 $ [        U[*        [,        45      (       a'  U R/                  UR0                  U5      u  nn[+        U6 $ U R2                  " U/UQ7SS06$ s  snf s  snnf s  snnf s  snf )Nra   r   r   F)r   r  r   r   r   r  _push_indices_downr  rr   r&  r4   r   intr,   r   r  r  _ArrayDiagonal_denest_ArrayAdd#_ArrayDiagonal_denest_ArrayDiagonalr   !_ArrayDiagonal_denest_PermuteDimsr   r   r  r1   r   )r<   r   r  ri   trivial_diagsrf  trivial_posdiag_posdiagonal_indices_shortrank1rank2rank3inv_permutationcounter1indices_downr   r  r1   s                     r>   r   ArrayDiagonal._canonicalize  s   yy00$4D$4qA!$4D}!/89I/JZ/JtqcRSfXYk71Q47/JKZ)23C)DS)DAQR
)DHS1A%P1AASVaZa1A"%PTNE()EME OH(;;<RTXY^_dYeTfhpquhvwL!##**5q>+ABGS>22#**84MH#**5A;+>? " %_5K)*Q.$_T%S<R%SU`aa$T77dH%%66tO>NOOdM**;;DTCSTTdK((99$RAQRRdY
344#88EUVIue$$yyE 0EuEEC EZS%Ps.   JJJ"3
J"J('J(4J.J.c                  ^ [        U 5      mU H  n[        U4S jU 5       5      (       a  [        S5      e[        U Vs1 s H  nTU   iM
     sn5      S:w  a  [        S5      eUR	                  SS5      (       d  [        U5      S::  a  [        S5      e[        [        U5      5      [        U5      :w  d  M  [        S5      e   g s  snf )	Nc              3  >   >#    U  H  o[        T5      :  v   M     g 7fr3   r   )rh   rt   r1   s     r>   rj   *ArrayDiagonal._validate.<locals>.<genexpr>0  s     .AqE
?As   z%index is larger than expression shapera   z-diagonalizing indices of different dimensionsallow_trivial_diagsFz%need at least two axes to diagonalizezaxis index cannot be repeated)r   r   ro   r   r   r  )r   r  r   ri   rt   r1   s        @r>   r  ArrayDiagonal._validate*  s     $!A.A... !HIIa(aE!Ha()Q. !PQQ::3U;;A! !HII3q6{c!f$ !@AA " )s    C
c                l    U Vs/ s H"  o US      S:w  d  M  [        S U 5       5      PM$     sn$ s  snf )Nr   ra   c              3  $   #    U  H  ov   M     g 7fr3   rE   )rh   rt   s     r>   rj   ;ArrayDiagonal._remove_trivial_dimensions.<locals>.<genexpr>;  s     ^Aas   r   )r1   r  ri   s      r>   _remove_trivial_dimensions(ArrayDiagonal._remove_trivial_dimensions9  s6    -=R-=qtPQAQ#^^#-=RRRs   11c                     U R                   S   $ rX   rA  r[   s    r>   r   ArrayDiagonal.expr=  rC  rA   c                     U R                   SS  $ r`   rA  r[   s    r>   r  ArrayDiagonal.diagonal_indicesA      yy}rA   c                  ^ U R                   nU VVs/ s H  o3  H  oDPM     M     nnnUR                  5         [        U 5      n[        U5      nXg-
  n[	        U5       Vs/ s H  nSPM     snmSn	Sn
[	        U5       H;  nX:  a!  XU
   :  a  U	S-  n	U
S-  n
X:  a
  XU
   :  a  M  TU==   U
-  ss'   U	S-  n	M=     [        U4S jU 5       5      nX!-   n[        U R                  /UQ76 $ s  snnf s  snf )Nr   ra   c              3  N   >#    U  H  n[        U4S  jU 5       5      v   M     g7f)c              3  4   >#    U  H  nTU   U-   v   M     g 7fr3   rE   rh   rt   shiftss     r>   rj   3ArrayDiagonal._flatten.<locals>.<genexpr>.<genexpr>W  s     ,FAqVAY]Ar   Nr  rh   ri   r  s     r>   rj   )ArrayDiagonal._flatten.<locals>.<genexpr>W  s!     &gPf1u,FA,F'F'FPf   "%)r  rN  r   r   rr   r   r  r   )r   outer_diagonal_indicesinner_diagonal_indicesri   rt   	all_inner
total_rank
inner_rank
outer_rankrd  pointerr  r  s               @r>   r   ArrayDiagonal._flattenE  s   !%!6!6 6B 611QQ 6	B!$'
^
,
":./.!./z"A&76H+H11 &76H+H 1I IqLG # "'&gPf&g!g1Jtyy<+;<<# C 0s   C-!C3c           	     f    [        UR                   Vs/ s H  n[        U/UQ76 PM     sn6 $ s  snf r3   )r  r   r  )rS   r   r  r   s       r>   r  ,ArrayDiagonal._ArrayDiagonal_denest_ArrayAdd[  s.    tyyYyOCC2BCyYZZY   .c                (    U R                   " U/UQ76 $ r3   r   )rS   r   r  s      r>   r  1ArrayDiagonal._ArrayDiagonal_denest_ArrayDiagonal_  s    ||D4#344rA   c           
       ^ U VVs/ s H#  o3 Vs/ s H  oAR                  U5      PM     snPM%     nnn[        [        U5      5       V^s/ s H!  m[        U4S jU 5       5      (       a  M  TPM#     nnU Vs/ s H  o1R                  U5      PM     nn[	        [        U5      5       VVs0 s H  u  p8X_M	     n	nnU Vs/ s H  o9U   PM	     n
n[        U
5      n[        [        U5      5       Vs/ s H  o3U-   PM	     nnX-   n[        [        UR                  /UQ76 U5      $ s  snf s  snnf s  snf s  snf s  snnf s  snf s  snf )Nc              3  .   >#    U  H
  nTU;   v   M     g 7fr3   rE   rh   rt   ri   s     r>   rj   BArrayDiagonal._ArrayDiagonal_denest_PermuteDims.<locals>.<genexpr>f  s     >`O_!qAvO_r_  )
r   rr   r   r   r   r;  r   r   r  r   )rS   r   r  ri   rt   back_diagonal_indicesnondiagback_nondiagrf  remapnew_permutation1shiftdiag_block_permrW  s      `          r>   r  /ArrayDiagonal._ArrayDiagonal_denest_PermuteDimsc  s.   K[ \K[aq!Aq!"2"21"5q!AK[ \#HTN3a33>`O_>`;`13a5<=W((+W="+F<,@"AB"A$!"AB.:;l!Hl;$%.3C8M4N.OP.Ou9.OP*<		& 
 	
 "B \a=B;Ps9   	D+D&D+D1-D19D6,D;E2E&D+c                &   ^  U 4S jn[        X!5      $ )Nc                X   > U [        TR                  5      :  a  TR                  U    $ S $ r3   )r   r  )rI  r<   s    r>   rJ  <ArrayDiagonal._push_indices_down_nonstatic.<locals>.<lambda>v  s'    ADOO8L4Ldooa0VRVVrA   r$   r<   r   	transforms   `  r>   _push_indices_down_nonstatic*ArrayDiagonal._push_indices_down_nonstaticu  s    V	3IGGrA   c                &   ^  U 4S jn[        X!5      $ )Nc                   > [        TR                  5       H?  u  p[        U[        5      (       a  X:X  d  [        U[        5      (       d  M6  X;   d  M=  Us  $    g r3   )r   r  r4   r  r   )rI  ri   rf  r<   s      r>   r   ;ArrayDiagonal._push_indices_up_nonstatic.<locals>.transform{  s@    !$//2q#&&16z!U7K7KPQPVH 3rA   r  r  s   `  r>   _push_indices_up_nonstatic(ArrayDiagonal._push_indices_up_nonstaticy  s    	
 4IGGrA   c                b   ^ U R                  [        U5      U5      u  mnU4S jn[        XR5      $ )Nc                0   > U [        T5      :  a  TU    $ S $ r3   r  )rI  r  s    r>   rJ  2ArrayDiagonal._push_indices_down.<locals>.<lambda>  s    a#i..@ilJdJrA   r  rr   r$   rS   r  r   rankr1   r   r  s         @r>   r   ArrayDiagonal._push_indices_down  s/    33E$KAQR	5J	3IGGrA   c                b   ^ U R                  [        U5      U5      u  mnU4S jn[        XR5      $ )Nc                   > [        T5       HE  u  p[        U[        5      (       a  X:X  d$  [        U[        [        45      (       d  M<  X;   d  MC  Us  $    g r3   )r   r4   r  r   r   )rI  ri   rf  r  s      r>   r   1ArrayDiagonal._push_indices_up.<locals>.transform  s@    !),q#&&16z!eU^7T7TZ[Z`H -rA   r  r  s         @r>   r`  ArrayDiagonal._push_indices_up  s1    33E$KAQR	5	
 4IGGrA   c                   ^^ [        U4S j[        T5       5       5      nU(       a  [        U6 OSu  pE[        U4S jT 5       5      nU(       a  [        U6 OSu  pxXG-   n	XX-   mU	T4$ )Nc              3  l   >^#    U  H(  u  mn[        U4S  jT 5       5      (       a  M"  TU4v   M*     g7f)c              3  .   >#    U  H
  nTU;   v   M     g 7fr3   rE   r  s     r>   rj   ?ArrayDiagonal._get_positions_shape.<locals>.<genexpr>.<genexpr>  s     HjYiTUaYir_  Nr   )rh   shpri   r  s     @r>   rj   5ArrayDiagonal._get_positions_shape.<locals>.<genexpr>  s+     k-=61cSHjYiHjEjhq#h-=s   !44)rE   rE   c              3  6   >#    U  H  oTUS       4v   M     g7f)r   NrE   )rh   ri   r1   s     r>   rj   r    s     A0@1%!+&0@s   )r   r   r   )
rS   r1   r  data1pos1shp1data2pos2shp2r  s
    ``       r>   r  "ArrayDiagonal._get_positions_shape  s`    kYu-=kk$)S%[x
A0@AA$)S%[x
K	%rA   c                    U R                   n[        US5      (       a  UR                  5       n[        U/U R                  Q76 $ r  )r   r   rv   r   r  r  s     r>   rv   ArrayDiagonal.as_explicit  s<    yy4''##%Dd;T%:%:;;rA   rE   N)r   r   )rF   rG   rH   rI   ry   rR   r   staticmethodr  r  r{   r   r  r   r   r  r  r  r  r  r  r`  r  rv   rK   rE   rA   r>   r  r    s
   2,$FL B B S S     = =* [ [ 5 5 
 
"HH H H
 H H    <rA   r  c                  V    \ rS rSrS r\S 5       r\S 5       r\S 5       rS r	S r
Srg	)
ArrayElementwiseApplyFunci  c                    [        U[        5      (       d  [        S5      n[        X1" U5      5      n[        R	                  XU5      n[        U5      Ul        U$ Nd)r4   r   r   r   rR   r  r   )rS   functionelementr)  rU   s        r>   rR   !ArrayElementwiseApplyFunc.__new__  sK    (F++c
Aa!-H#++C7C%g.
rA   c                     U R                   S   $ rX   rA  r[   s    r>   r*  "ArrayElementwiseApplyFunc.function  rC  rA   c                     U R                   S   $ r`   rA  r[   s    r>   r   ArrayElementwiseApplyFunc.expr  rC  rA   c                .    U R                   R                  $ r3   r   r1   r[   s    r>   r1   ArrayElementwiseApplyFunc.shape  s    yyrA   c                    [        S5      nU R                  U5      nUR                  U5      n[        U[        5      (       a  [        U5      nU$ [        X5      nU$ r(  )r   r*  diffr4   r   typer   )r<   r)  r*  fdiffs       r>   _get_function_fdiff-ArrayElementwiseApplyFunc._get_function_fdiff  sU    #J==#a eX&&KE  1$ErA   c                    U R                   n[        US5      (       a  UR                  5       nUR                  U R                  5      $ r  )r   r   rv   	applyfuncr*  r  s     r>   rv   %ArrayElementwiseApplyFunc.as_explicit  s9    yy4''##%D~~dmm,,rA   rE   N)rF   rG   rH   rI   rR   r{   r*  r   r1   r8  rv   rK   rE   rA   r>   r&  r&    sM          -rA   r&  c                     \ rS rSrSrS rS rS rS r\	S 5       r
\S 5       r\S	 5       r\S
 5       rS rS r\	S 5       r\	S 5       r\	S 5       r\	S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S#S j5       r\S 5       rS r\	S 5       r\S 5       r\S 5       r\S 5       r \S 5       r!S r"S r#S r$S  r%S!r&g")$r   i  zl
This class is meant to represent contractions of arrays in a form easily
processable by the code printers.
c                J  ^^ [        T5      m[        U5      nUR                  SS5      n[        R                  " X/TQ76 n[        U5      Ul        [        UR                  5      Ul        [        [        UR                  5      5       V^s0 s H"  m[        U4S jT 5       5      (       d  M  TT_M$     nnXul        [        U5      nU R                  " U/TQ76   U(       a  [        U4S j[!        U5       5       5      nXl        U(       a  UR%                  5       $ U$ s  snf )Nr   Fc              3  .   >#    U  H
  nTU;  v   M     g 7fr3   rE   )rh   cindri   s     r>   rj   +ArrayContraction.__new__.<locals>.<genexpr>  s'       SB  nAeiST\`S`  nAr_  c              3  h   >^#    U  H&  u  mn[        U4S  jT 5       5      (       a  M"  Uv   M(     g7f)c              3  .   >#    U  H
  nTU;   v   M     g 7fr3   rE   r  s     r>   rj   5ArrayContraction.__new__.<locals>.<genexpr>.<genexpr>  s     GlXkSTQXkr_  Nr  )rh   r  ri   r  s     @r>   rj   rA    s'     m,<&!SCGlXkGlDl##,<s   !2	2)r%   r-   r   r   rR   r  r   r&   _mappingrr   r   rn   _free_indices_to_positionr   r  r   r   r   r   )	rS   r   r  r   r   rU   ri   free_indices_to_positionr1   s	     `   `  r>   rR   ArrayContraction.__new__  s   78KL~zz.%8mmC<(;<%d+1#--@27CMM8J2K  $C2KQs  SB  nA  SB  PBDAqD2K   $C(@%$d101mIe,<mmE
$$&&
 $Cs   D /D c                P   U R                   nU R                  n[        U5      S:X  a  U$ [        U[        5      (       a  U R
                  " U/UQ76 $ [        U[        [        45      (       a  U R                  " U/UQ76 $ [        U[        5      (       a  U R                  " U/UQ76 $ [        U[        5      (       a7  U R                  X5      u  pU R                  X5      u  p[        U5      S:X  a  U$ [        U[        5      (       a  U R                  " U/UQ76 $ [        U[         5      (       a  U R"                  " U/UQ76 $ U Vs/ s H+  n[        U5      S:  d  [%        U5      US      S:w  d  M)  UPM-     nn[        U5      S:X  a  U$ U R&                  " U/UQ7SS06$ s  snf )Nr   ra   r   F)r   r  r   r4   r   )_ArrayContraction_denest_ArrayContractionr   r   "_ArrayContraction_denest_ZeroArrayr   $_ArrayContraction_denest_PermuteDimsr   _sort_fully_contracted_args_lower_contraction_to_addendsr  &_ArrayContraction_denest_ArrayDiagonalr  !_ArrayContraction_denest_ArrayAddr   r   )r<   r   r  ri   s       r>   r   ArrayContraction._canonicalize  s   yy"66"#q(Kd,--AA$]I\]]dY
344::4VBUVVdK((<<TXDWXXd.//(,(H(H(c%D(,(J(J4(e%D&'1,dM**>>tZFYZZdH%%99$UATUU +>j*=QQ!yY]_`ab_cOdhiOiq*=j"#q(KyyH 3H%HH	 ks   (F#4F#c                (    US:X  a  U $ [        S5      eNra   zDProduct of N-dim arrays is not uniquely defined. Use another method.r#  r<   others     r>   __mul__ArrayContraction.__mul__      A:K%&lmmrA   c                (    US:X  a  U $ [        S5      erS  rT  rU  s     r>   __rmul__ArrayContraction.__rmul__  rY  rA   c                    [        U 5      nUc  g U H:  n[        U Vs1 s H  oBU   S:w  d  M  X$   iM     sn5      S:w  d  M1  [        S5      e   g s  snf )Nr   ra   z+contracting indices of different dimensions)r   r   ro   )r   r  r1   ri   rt   s        r>   r  ArrayContraction._validate  sV    $= %Aa:a8r>HEHa:;q@ !NOO %:s
   A
A
c                    U VVs/ s H  o3  H  oDPM     M     nnnUR                  5         [        U5      n[        Xb5      $ s  snnf r3   )rN  r)   r$   rS   r  r   ri   rt   flattened_contraction_indicesr   s          r>   r  #ArrayContraction._push_indices_down#  sK    4G(S4GqQRAQR4G%(S%**,@A^_	3IGG )T   Ac                    U VVs/ s H  o3  H  oDPM     M     nnnUR                  5         [        U5      n[        Xb5      $ s  snnf r3   )rN  r'   r$   r`  s          r>   r`  !ArrayContraction._push_indices_up*  sK    4G(S4GqQRAQR4G%(S%**,>?\]	3IGG )Trc  c           
     L  ^^^ [        U[        5      (       a
  [        5       e[        U[        5      (       d  X4$ UR                  n[        [        S/U-   5      5      m/ nUR                   Vs/ s H  n/ PM     nn[        5       nU H  n[        [        UR                  5      5       H  m[        UR                  T   [        5      (       d  M'  [        UU4S jU 5       5      (       d  MD  UT   R                  U V	s/ s H
  oTT   -
  PM     sn	5        UR                  U5          M     UR                  U5        M     [        U5      [        U5      :X  a  X4$ [        U5      n
[        [        [        U
5       Vs/ s H  oUU;   a  SOSPM     sn5      5      mU Vs/ s H#  n[        R                   " U4S jU 5       5      PM%     nn[#        [%        UR                  U5       VVs/ s H  u  p['        U/UQ76 PM     snn6 nX4$ s  snf s  sn	f s  snf s  snf s  snnf )Nr   c              3  `   >#    U  H#  nTT   Us=:*  =(       a    TTS -      :  Os  v   M%     g7f)ra   NrE   )rh   r   cumranksrt   s     r>   rj   AArrayContraction._lower_contraction_to_addends.<locals>.<genexpr>@  s/     SARAx{a77(1Q3-77ARs   +.ra   c              3  2   >#    U  H  oTU   -
  v   M     g 7fr3   rE   r  s     r>   rj   ri  J  s     7Qq!F1Iqs   )r4   r  r#  r   r   r  r   r   r  rr   r   rn   r&  updater   r   r   r   r   r  )rS   r   r  r   contraction_indices_remainingri   contraction_indices_args	backshiftcontraction_groupr   r  r   contrr  rh  rt   r  s                 @@@r>   rN  .ArrayContraction._lower_contraction_to_addends1  s   dH%%%''$ 233,,==
A3>23(*%04		#:	1B	 #:E	!43tyy>*!$))A,99SARSSS,Q/66Qb7cQbAHQKQb7cd$$%67 + .445FG "5 ,-5H1II,,d^
jeJFW!XFWI~!1"<FW!XYZ[x(y[xVW7Qq7Q)Q[x%(y#>A$))Me>f&
>f
s+U+>f&
  11) $; 8d "Y(y&
s   /H H H"*H,H 
c           	       ^^ [        U 5      nU R                  n/ n[        U5       GH  u  mn[        U5      S::  a  M  UR	                  T5      nU R
                  R                  US      n/ n/ nU H  u  pUR                  U	   nUR                  nUR                  U5      u  pSU
-
  nX-   mSUR                  ;  d<  US:H  SL a  UR                  S:w  d$  [        UU4S j[        U5       5       5      (       a  UR                  X45        M  UR                  X45        M     [        U5      S:  a  GM  U H   u  nn
[        UR                  5      Ul        M"     USS U-   USS -   nUS   u  nn
UR                  U
   nUSS  H  u  nn
UUR                  U
'   UR                  5       nUR                  R                  S5      SU
-
  :X  d   eUUR                  UR                  R                  S5      '   UR                  U5        M     US   u  nn
UUR                  U
'   GM     U H)  nUR!                  U[#        [%        S5      S/5      5        M+     UR'                  5       $ )	a  
Recognize multiple contractions and attempt at rewriting them as paired-contractions.

This allows some contractions involving more than two indices to be
rewritten as multiple contractions involving two indices, thus allowing
the expression to be rewritten as a matrix multiplication line.

Examples:

* `A_ij b_j0 C_jk` ===> `A*DiagMatrix(b)*C`

Care for:
- matrix being diagonalized (i.e. `A_ii`)
- vectors being diagonalized (i.e. `a_i0`)

Multiple contractions can be split into matrix multiplications if
not more than two arguments are non-diagonals or non-vectors.
Vectors get diagonalized while diagonal matrices remain diagonal.
The non-diagonal matrices can be at the beginning or at the end
of the final matrix multiplication line.
   r   ra   T)ra   ra   c              3  B   >#    U  H  u  pUT:w  d  M  TU;   v   M     g 7fr3   rE   )rh   lilindlother_arg_abss      r>   rj   ?ArrayContraction.split_multiple_contractions.<locals>.<genexpr>  s&     e8VurZ\`dZd**8Vs   Nr   )_EditArrayContractionr  r   r   get_mapping_for_indexr   r1   args_with_indr+  get_absolute_ranger   r&  r   r   get_new_contraction_indexr  insert_after_ArgEr   to_array_contraction)r<   editorr  onearray_insertlinksr  current_dimensionnot_vectorsvectorsarg_indrel_indr   matabs_arg_startabs_arg_endother_arg_posr  vectors_to_loopfirst_not_vector	new_indexlast_vecrw  rx  s                        @@r>   split_multiple_contractions,ArrayContraction.split_multiple_contractionsP  s_   . 't,"66$%89KD%5zQ& 44T:I !%		a 9KG$- **73kk-3-F-Fs-K* !'	 - =cii''1,5#))v:Me	BU8Veee&&~6NNC>2 %. ;!# 
 &
7.qyy9	 &)"1o7+ab/IO(7(:%g(009I-a3
7%.		'""<<>	yyt,G;;;3<		!))//$/0&&q) 4 !0 3Hg(1HW%A :D !A5!tf#=> ! **,,rA   c           
        [        U R                  [        5      (       d  U $ U R                  R                  U R                  R                  U R
                  5      n/ nU R                  R                  S S  nU H  n[        U5      nU Ha  nU Vs/ s H  ovU;   d  M
  UPM     nnUR                  U VV	s/ s H  ow  H  oPM     M     sn	n5        U Vs/ s H  owU;  d  M
  UPM     nnMc     UR                  [        [        U5      5      5        M     [        R                  X25      n[        [        U R                  R                  /UQ76 /UQ76 $ s  snf s  sn	nf s  snf r3   )r4   r   r  r  r  r  r  r   r&  r;  r  r`  r  r  )
r<   contraction_downrm  r  ri   ro  rt   r   diagonal_withrv  s
             r>   flatten_contraction_of_diagonal0ArrayContraction.flatten_contraction_of_diagonal  sD   $))]33K9977		8R8RTXTlTlm"$9955a8!A $Q,< G,<qQ,< G!((])N]Aq!A!])NO/?#Z/?!MCYA/? #Z   $**6#6G2H+IJ " #0"@"@AQ"k!		!

 %
 	
 !H)N#Zs   	EE5E	E"Ec                    0 nU VVs/ s H  o3  H  oDPM     M     nnnSnU  H  nXe;   a  US-  nXe;   a  M  XbU'   US-  nM     U$ s  snnf Nr   ra   rE   )free_indicesr  rG  ri   rt   ra  rd  inds           r>   !_get_free_indices_to_position_map2ArrayContraction._get_free_indices_to_position_map  sn    #% 4G(S4GqQRAQR4G%(SC:1 :,3S)qLG	  
 (' )Ts   Ac                   U R                   nU VVs/ s H  o"  H  o3PM     M     nnnUR                  5         [        U 5      n[        U5      nXV-
  n[	        U5       Vs/ s H  nSPM     nnSn	Sn
[	        U5       H:  nX:  a!  XU
   :  a  U	S-  n	U
S-  n
X:  a
  XU
   :  a  M  X==   U
-  ss'   U	S-  n	M<     U$ s  snnf s  snf )a  
Get the mapping of indices at the positions before the contraction
occurs.

Examples
========

>>> from sympy.tensor.array import tensorproduct, tensorcontraction
>>> from sympy import MatrixSymbol
>>> M = MatrixSymbol("M", 3, 3)
>>> N = MatrixSymbol("N", 3, 3)
>>> cg = tensorcontraction(tensorproduct(M, N), [1, 2])
>>> cg._get_index_shifts(cg)
[0, 2]

Indeed, ``cg`` after the contraction has two dimensions, 0 and 1. They
need to be shifted by 0 and 2 to get the corresponding positions before
the contraction (that is, 0 and 3).
r   ra   )r  rN  r   r   rr   )r   inner_contraction_indicesri   rt   r  r  r  r  r  rd  r  s              r>   _get_index_shifts"ArrayContraction._get_index_shifts  s    * %)$<$<! 9E 911aQ1Q 9	E!$'
^
,
":./.!./z"A&76H+H11 &76H+H I IqLG #  F 0s   B= Cc                \   ^ [         R                  U 5      m[        U4S jU 5       5      nU$ )Nc              3  N   >#    U  H  n[        U4S  jU 5       5      v   M     g7f)c              3  4   >#    U  H  nTU   U-   v   M     g 7fr3   rE   r  s     r>   rj   UArrayContraction._convert_outer_indices_to_inner_indices.<locals>.<genexpr>.<genexpr>  s     /Iq!q	Aqr   Nr  r  s     r>   rj   KArrayContraction._convert_outer_indices_to_inner_indices.<locals>.<genexpr>  s!     )mSla%/Iq/I*I*ISlr  )r   r  r   )r   outer_contraction_indicesr  s     @r>   r  8ArrayContraction._convert_outer_indices_to_inner_indices  s+    !33D9$))mSl)m$m!((rA   c                |    U R                   n[        R                  " U /UQ76 nX!-   n[        U R                  /UQ76 $ r3   )r  r   r  r  r   )r   r  r  r  s       r>   r   ArrayContraction._flatten  sD    $($<$<!$4$\$\]a$~d}$~!7S!$))B.ABBrA   c                (    U R                   " U/UQ76 $ r3   r  )rS   r   r  s      r>   rJ  :ArrayContraction._ArrayContraction_denest_ArrayContraction  s    ||D7#677rA   c                    U VVs/ s H  o3  H  oDPM     M     nnn[        UR                  5       VVs/ s H  u  p6X5;  d  M  UPM     nnn[        U6 $ s  snnf s  snnf r3   )r   r1   r   )rS   r   r  ri   rt   ra  rf  r1   s           r>   rK  3ArrayContraction._ArrayContraction_denest_ZeroArray  s[    /B#N/B!AqAAA/B #N(4Z4tq8Y4Z%   $OZs   AAAc           	     f    [        UR                   Vs/ s H  n[        U/UQ76 PM     sn6 $ s  snf r3   )r  r   r  )rS   r   r  ri   s       r>   rP  2ArrayContraction._ArrayContraction_denest_ArrayAdd  s4    QUQZQZ[QZA.qG3FGQZ[\\[r  c                f  ^^ UR                   mTR                  nU Vs/ s H  n[        U4S jU 5       5      PM     nnU V^s/ s H!  m[        U4S jU 5       5      (       a  M  TPM#     nnU R	                  XV5      n[        [        UR                  /UQ76 [        U5      5      $ s  snf s  snf )Nc              3  4   >#    U  H  nT" U5      v   M     g 7fr3   rE   )rh   rt   r   s     r>   rj   HArrayContraction._ArrayContraction_denest_PermuteDims.<locals>.<genexpr>  s     (CAQr   c              3  .   >#    U  H
  nTU;   v   M     g 7fr3   rE   r  s     r>   rj   r    s     0YAXAaAXr_  )	r   r:  r   r   r`  r   r  r   r+   )rS   r   r  r>  ri   rm  	new_plistr   s       `  @r>   rL  5ArrayContraction._ArrayContraction_denest_PermuteDims  s    &&&&M`"aM`5(C(C#CM`"a %Z1S0YAX0Y-YQ	Z(()@L	tyyC+BC	"
 	
 #bZs   B)B.'B.c                  ^ [        UR                  5      nUR                  UR                  U[        UR                  5      5      nU VVVs/ s H>  oU VVs/ s H,  n[        U[        [        45      (       a  UOU/  H  owPM     M.     snnPM@     nnnn/ nU Hv  n	U	S S  n
[        U5       H<  u  nmTc  M  [        U4S jU	 5       5      (       d  M'  U
R                  T5        S X6'   M>     UR                  [        [        U
5      5      5        Mx     U Vs/ s H	  oUc  M  UPM     nn[        R                  X5      n[!        [#        UR                  /UQ76 /UQ76 $ s  snnf s  snnnf s  snf )Nc              3  ,   >#    U  H	  oT;   v   M     g 7fr3   rE   )rh   ri   diag_indgrps     r>   rj   JArrayContraction._ArrayContraction_denest_ArrayDiagonal.<locals>.<genexpr>1  s     >AK'   )r  r  r  r   r   r4   r   r   r   r   r   r&  r;  r  r   r`  r  r  )rS   r   r  r  down_contraction_indicesri   rt   r   rm  contr_indgrpr  new_diagonal_indices_downnew_diagonal_indicesr  s                @r>   rO  7ArrayContraction._ArrayContraction_denest_ArrayDiagonal%  st    5 56#'#:#:4;P;PRegoptpypygz#{  tL  $M  tLno$i1APUW\~A^A^Aefdg<gaQ<gQ$i  tL   $M"$4Lq/C"+,<"=;&>>>>JJ{+*.$' #> $**6#c(+;< 5 1A$R0@1Q0@!$R/@@AXttyyC+BC
!
 	
 %j  $M %Ss$   
E%3E
E%E,"E,E%c                t  ^^^^ TR                   c  TU4$ [        [        S/TR                  -   5      5      n[	        [        TR                  5      5       Vs/ s H  n[        [	        X4   X4S-      5      5      PM!     nnU VVs1 s H  oD  H  ofiM     M     snnm[        TR                  5       VVs/ s H+  u  pG[        U4S j[	        X4   X4S-      5       5       5      PM-     snnm[        [	        [        TR                  5      5      UU4S jS9nU Vs/ s H  nTR                  U   PM     n	nU VVs/ s H  oEU     H  ofPM     M     n
nn[        U
5      mU Vs/ s H  n[        U4S jU 5       5      PM     nn[        U5      n[        U	6 U4$ s  snf s  snnf s  snnf s  snf s  snnf s  snf )Nr   ra   c              3  ,   >#    U  H	  oT;   v   M     g 7fr3   rE   )rh   rt   ra  s     r>   rj   ?ArrayContraction._sort_fully_contracted_args.<locals>.<genexpr>D  s     cGb!%= =Gbr  c                P   > TU    (       a  S[        TR                  U    5      4$ S$ )Nr   )ra   )r   r   )rI  r   fully_contracteds    r>   rJ  >ArrayContraction._sort_fully_contracted_args.<locals>.<lambda>E  s9    euvwexqBRSWS\S\]^S_B`>a  ?C  C  ?CrA   rL  c              3  .   >#    U  H
  nTU   v   M     g 7fr3   rE   )rh   rt   index_permutation_array_forms     r>   rj   r  I  s     (TRSQ)Ea)HRSr_  )r1   r  r   r   rr   r   r   r   rn   r;  r,   r   r%   r   )rS   r   r  rP  ri   rg  rt   r   new_posr'  new_index_blocks_flatrm  ra  r  r  s    `          @@@r>   rM  ,ArrayContraction._sort_fully_contracted_args=  s   ::,,,Zdmm 345CHTYYCXYCXaU58UQ3Z89CXY/B#N/B!AqAAA/B#N r{  }A  }F  }F  sG  H  sGhnhiCcuUXW\_`]`WaGbcc  sG  Hs499~.  5C  D*12'QDIIaL'2,3 MGq!__G M'12G'H$^q"r^qYZ5(TRS(T#T^q"r";<S"T$h/1HHH Z#N H2 M"rs$   &FF52F$F*:F/#F5c           	         U R                   nU R                   VVs/ s H  o" Vs/ s H  o1U   PM	     snPM     snn$ s  snf s  snnf )a6  
Return tuples containing the argument index and position within the
argument of the index position.

Examples
========

>>> from sympy import MatrixSymbol
>>> from sympy.abc import N
>>> from sympy.tensor.array import tensorproduct, tensorcontraction
>>> A = MatrixSymbol("A", N, N)
>>> B = MatrixSymbol("B", N, N)

>>> cg = tensorcontraction(tensorproduct(A, B), (1, 2))
>>> cg._get_contraction_tuples()
[[(0, 1), (1, 0)]]

Notes
=====

Here the contraction pair `(1, 2)` meaning that the 2nd and 3rd indices
of the tensor product `A\otimes B` are contracted, has been transformed
into `(0, 1)` and `(1, 0)`, identifying the same indices in a different
notation. `(0, 1)` is the second index (1) of the first argument (i.e.
        0 or `A`). `(1, 0)` is the first index (i.e. 0) of the second
argument (i.e. 1 or `B`).
)rE  r  )r<   mappingri   rt   s       r>   _get_contraction_tuples(ArrayContraction._get_contraction_tuplesM  sA    8 --151I1IJ1IAQ'QQ'1IJJ'Js   	A>AAc                   ^ U R                   nS/[        [        U5      5      -   mU Vs/ s H  n[        U4S jU 5       5      PM     sn$ s  snf )Nr   c              3  8   >#    U  H  u  pTU   U-   v   M     g 7fr3   rE   )rh   rt   r   r   s      r>   rj   NArrayContraction._contraction_tuples_to_contraction_indices.<locals>.<genexpr>q  s     :&q)!+s   )r   r  r   r   )r   contraction_tuplesr   ri   r   s       @r>   *_contraction_tuples_to_contraction_indices;ArrayContraction._contraction_tuples_to_contraction_indicesl  sJ     3j&7!88DVWDVq:::DVWWWs   Ac                     U R                   S S  $ r3   )_free_indicesr[   s    r>   r  ArrayContraction.free_indicess  s    !!!$$rA   c                ,    [        U R                  5      $ r3   )dictrF  r[   s    r>   rG  )ArrayContraction.free_indices_to_positionw  s    D2233rA   c                     U R                   S   $ rX   rA  r[   s    r>   r   ArrayContraction.expr{  rC  rA   c                     U R                   SS  $ r`   rA  r[   s    r>   r  $ArrayContraction.contraction_indices  r  rA   c                    U R                   n[        U[        5      (       d  [        S5      eUR                  n0 nSn[        U5       H!  u  pV[        U5       H  nXW4X4'   US-  nM     M#     U$ )Nz(only for contractions of tensor productsr   ra   )r   r4   r   r#  r   r   rr   )r<   r   r   r  rd  ri   r  rt   s           r>   "_contraction_indices_to_components3ArrayContraction._contraction_indices_to_components  sq    yy$ 233%&PQQ 'GA4[$%6 1 ! ( rA   c                   U R                   n[        U[        5      (       d  U $ UR                  n[	        [        U5      S S9n[        U6 u  pE[        U5       VVs0 s H  u  pgXdR                  U5      _M     nnnU R                  5       n	U	 VV
Vs/ s H  of V
Vs/ s H  u  pX   U4PM     snn
PM     n	n
nn[        U6 nU R                  UU	5      n[        U/UQ76 $ s  snnf s  snn
f s  snn
nf )a\  
Sort arguments in the tensor product so that their order is lexicographical.

Examples
========

>>> from sympy.tensor.array.expressions.from_matrix_to_array import convert_matrix_to_array
>>> from sympy import MatrixSymbol
>>> from sympy.abc import N
>>> A = MatrixSymbol("A", N, N)
>>> B = MatrixSymbol("B", N, N)
>>> C = MatrixSymbol("C", N, N)
>>> D = MatrixSymbol("D", N, N)

>>> cg = convert_matrix_to_array(C*D*A*B)
>>> cg
ArrayContraction(ArrayTensorProduct(A, D, C, B), (0, 3), (1, 6), (2, 5))
>>> cg.sort_args_by_name()
ArrayContraction(ArrayTensorProduct(A, D, B, C), (0, 3), (1, 4), (2, 7))
c                    [        U S   5      $ r`   r   rH  s    r>   rJ  4ArrayContraction.sort_args_by_name.<locals>.<lambda>  s    <LQqT<RrA   rL  )r   r4   r   r   r;  r   r   r  r  r   r  r  )r<   r   r   sorted_data
pos_sortedrU  ri   r   reordering_mapr  rt   r   c_tpr  s                 r>   sort_args_by_name"ArrayContraction.sort_args_by_name  s    * yy$ 233KyyYt_2RS"%{"3
?HOVQ!--a00O!99;N`aN`!D!$! 115!DN`a$k2 KK"
 "$;):;; PDas   C 
C,C&.C,&C,c                N    [        U /U R                  /U R                  Q76 u  pU$ )a  
Returns a dictionary of links between arguments in the tensor product
being contracted.

See the example for an explanation of the values.

Examples
========

>>> from sympy import MatrixSymbol
>>> from sympy.abc import N
>>> from sympy.tensor.array.expressions.from_matrix_to_array import convert_matrix_to_array
>>> A = MatrixSymbol("A", N, N)
>>> B = MatrixSymbol("B", N, N)
>>> C = MatrixSymbol("C", N, N)
>>> D = MatrixSymbol("D", N, N)

Matrix multiplications are pairwise contractions between neighboring
matrices:

`A_{ij} B_{jk} C_{kl} D_{lm}`

>>> cg = convert_matrix_to_array(A*B*C*D)
>>> cg
ArrayContraction(ArrayTensorProduct(B, C, A, D), (0, 5), (1, 2), (3, 6))

>>> cg._get_contraction_links()
{0: {0: (2, 1), 1: (1, 0)}, 1: {0: (0, 1), 1: (3, 0)}, 2: {1: (0, 0)}, 3: {0: (1, 1)}}

This dictionary is interpreted as follows: argument in position 0 (i.e.
matrix `A`) has its second index (i.e. 1) contracted to `(1, 0)`, that
is argument in position 1 (matrix `B`) on the first index slot of `B`,
this is the contraction provided by the index `j` from `A`.

The argument in position 1 (that is, matrix `B`) has two contractions,
the ones provided by the indices `j` and `k`, respectively the first
and second indices (0 and 1 in the sub-dict).  The link `(0, 1)` and
`(2, 0)` respectively. `(0, 1)` is the index slot 1 (the 2nd) of
argument in position 0 (that is, `A_{\ldot j}`), and so on.
)r(   r   r  )r<   r   dlinkss      r>   r(   'ArrayContraction._get_contraction_links  s)    R .tfdmm_dF^F^_rA   c                    U R                   n[        US5      (       a  UR                  5       n[        U/U R                  Q76 $ r  )r   r   rv   r   r  r  s     r>   rv   ArrayContraction.as_explicit  s<    yy4''##%D A(@(@AArA   rE   N)r   z'ArrayDiagonal')'rF   rG   rH   rI   ry   rR   r   rW  r[  r$  r  r   r  r`  rN  r  r  r  r  r  r   rJ  rK  rP  rL  rO  rM  r  r  r{   r  rG  r   r  r  r  r(   rv   rK   rE   rA   r>   r   r     s   
,!IFnn P P H H H H 2 2<b-H
. 	( 	( $ $L ) )
 C C 8 8 ! !
 ] ] 	
 	
 
 
. I IK> X X % % 4 4    #<J*XBrA   r   c                  J    \ rS rSrSrS r\S 5       r\S 5       rS r	S r
Srg	)
Reshapei  a  
Reshape the dimensions of an array expression.

Examples
========

>>> from sympy.tensor.array.expressions import ArraySymbol, Reshape
>>> A = ArraySymbol("A", (6,))
>>> A.shape
(6,)
>>> Reshape(A, (3, 2)).shape
(3, 2)

Check the component-explicit forms:

>>> A.as_explicit()
[A[0], A[1], A[2], A[3], A[4], A[5]]
>>> Reshape(A, (3, 2)).as_explicit()
[[A[0], A[1]], [A[2], A[3]], [A[4], A[5]]]

c                J   [        U5      n[        U[        5      (       d  [        U6 n[        [        R
                  " UR                  5      [        R
                  " U5      5      S:X  a  [        S5      e[        R                  " XU5      n[        U5      Ul        Xl        U$ )NFzshape mismatch)r-   r4   r   r	   r   r   r1   ro   r   rR   r   r   _expr)rS   r   r1   rU   s       r>   rR   Reshape.__new__  sw    ~%''5MECLL,cll5.ABeK-..ll3e,5\
	
rA   c                    U R                   $ r3   r   r[   s    r>   r1   Reshape.shape
  r   rA   c                    U R                   $ r3   )r  r[   s    r>   r   Reshape.expr  r   rA   c                   UR                  SS5      (       a  U R                  R                  " U0 UD6nOU R                  n[        U[        [
        45      (       a  UR                  " U R                  6 $ [        X0R                  5      $ )Nr   T)	r   r   r   r4   r   r    rs   r1   r  )r<   r   r   r   s       r>   r   Reshape.doit  sg    ::fd##99>>4262D99DdZ344<<,,tZZ((rA   c                   U R                   n[        US5      (       a  UR                  5       n[        U[        5      (       a  SSKJn  U" U5      nO[        U[        5      (       a  U $ UR                  " U R                  6 $ )Nrv   r   )Array)
r   r   rv   r4   r   sympyr  r   rs   r1   )r<   eer  s      r>   rv   Reshape.as_explicit  sb    YY2}%%!Bb*%%#rBJ''Kzz4::&&rA   rE   N)rF   rG   rH   rI   ry   rR   r{   r1   r   r   rv   rK   rE   rA   r>   r  r    s>    ,	    )	'rA   r  c                  <    \ rS rSr% SrS\S'   S	S
S jjrS r\rSr	g)r  i'  a<  
The ``_ArgE`` object contains references to the array expression
(``.element``) and a list containing the information about index
contractions (``.indices``).

Index contractions are numbered and contracted indices show the number of
the contraction. Uncontracted indices have ``None`` value.

For example:
``_ArgE(M, [None, 3])``
This object means that expression ``M`` is part of an array contraction
and has two indices, the first is not contracted (value ``None``),
the second index is contracted to the 4th (i.e. number ``3``) group of the
array contraction object.
zlist[int | None]r   Nc                    Xl         Uc+  [        [        U5      5       Vs/ s H  nS PM     snU l        g X l        g s  snf r3   )r+  rr   r   r   )r<   r+  r   ri   s       r>   __init___ArgE.__init__9  s9    ?*/0A*BC*BQD*BCDL"L Ds   <c                @    SU R                   < SU R                  < S3$ )Nz_ArgE(z, )r+  r   r[   s    r>   __str___ArgE.__str__@  s    "&,,==rA   r  r3   )r   zlist[int | None] | None)
rF   rG   rH   rI   ry   rJ   r  r	  __repr__rK   rE   rA   r>   r  r  '  s      #> HrA   r  c                  2    \ rS rSrSrSS jrS r\rS rSr	g)	_IndPosiF  z
Index position, requiring two integers in the constructor:

- arg: the position of the argument in the tensor product,
- rel: the relative position of the index inside the argument.
c                    Xl         X l        g r3   r   rel)r<   r   r  s      r>   r  _IndPos.__init__M  s    rA   c                8    SU R                   U R                  4-  $ )Nz_IndPos(%i, %i)r  r[   s    r>   r	  _IndPos.__str__Q  s     DHHdhh#777rA   c              #  P   #    U R                   U R                  / S h  vN   g  N7fr3   r  r[   s    r>   __iter___IndPos.__iter__V  s     HHdhh'''s   &$&r  N)r   r  r  r  )
rF   rG   rH   rI   ry   r  r	  r  r  rK   rE   rA   r>   r  r  F  s    8 H(rA   r  c                      \ rS rSrSrSS jrSS jrS rS rS r	S r
SS	 jrSS
 jrSS jrSS jrSS jr\S 5       rS rSS jrSS jrSS jrSrg)rz  iZ  a  
Utility class to help manipulate array contraction objects.

This class takes as input an ``ArrayContraction`` object and turns it into
an editable object.

The field ``args_with_ind`` of this class is a list of ``_ArgE`` objects
which can be used to easily edit the contraction structure of the
expression.

Once editing is finished, the ``ArrayContraction`` object may be recreated
by calling the ``.to_array_contraction()`` method.
c                   [        U[        5      (       a1  [        UR                  5      nUR                  nUR
                  nSnGO3[        U[        5      (       a  [        UR                  [        5      (       a  [        UR                  R                  5      nUR                  R                  n[        R                  UR                  R
                  UR                  5      nUR                  R
                  nO[        UR                  [        5      (       a  0 nUR                  nUR                  n/ nOC0 nUR                  nUR                  n/ nO&[        U[        5      (       a  Un/ nSnO
[        5       e[        U[        5      (       a  [        UR                  5      nOU/nU Vs/ s H  n[        U5      PM     nn[        U5       H&  u  pU
 H  nWU   u  pXU   R                  U'   M     M(     Xl        [#        U5      U l        S U l        [        UR                  5      n[        U5       H3  u  pU H(  nX+   u  pSU	-
  U R                   U   R                  U'   M*     M5     g s  snf )NrE   r   )r4   r   r&   r   r   r  r  r  r  r   r#  r  r   r  r   r   r|  r   number_of_contraction_indices_track_permutation)r<   
base_arrayr  r   r  diagonalizedr   r   r|  ri   contraction_tuplert   arg_posrel_posrf  s                  r>   r  _EditArrayContraction.__init__i  s$   
 j"23301D1DEG??D","@"@L
M22*//+;<<4Z__5M5MN!++/BB:??CfCfhr  iD  iD   E&0oo&I&I#JOO-?@@!)::&(#!)::&(#
$677D"$L%''d.//		?D6D<@%ADSeCjD%A$-.A$B A&#*1: :;g&..w7 ' %C +8256I2J*:>,Z-@-@A l+DA#*: ?AAv""7+33G<  , &Bs   ,I-c                x    U R                   R                  U5      nU R                   R                  US-   U5        g r`   )r|  r  insert)r<   r   new_argposs       r>   r  "_EditArrayContraction.insert_after  s2      &&s+!!#'73rA   c                J    U =R                   S-  sl         U R                   S-
  $ r`   )r  r[   s    r>   r~  /_EditArrayContraction.get_new_contraction_index  s$    **a/*11A55rA   c                   0 nU R                    H4  nUR                  UR                   Vs0 s H
  o3c  M  US_M     sn5        M6     [        [	        U5      5       H	  u  p4X1U'   M     [        U5      U l        U R                    H4  nUR                   Vs/ s H  o1R                  US 5      PM     snUl        M6     g s  snf s  snf )Nr   )r|  rk  r   r   r;  r   r  r   )r<   updatesarg_with_indri   rf  s        r>   refresh_indices%_EditArrayContraction.refresh_indices  s     ..LNN<+?+?Q+?aEArE+?QR /fWo.DAAJ /-0\* ..LBNBVBV#WBVQKK4$8BV#WL  /	 R
 $Xs   B=
B=
Cc                "   / nU R                    H/  n[        UR                  5      S:X  d  M  UR                  U5        M1     U H  nU R                   R	                  U5        M      [
        R                  " U Vs/ s H  o3R                  PM     sn5      n[        U R                   5      S:X  a%  U R                   R                  [        U5      5        g SSK	J
n  U" X@R                   S   R                  5      U R                   S   l        g s  snf )Nr   )_a2m_tensor_product)r|  r   r   r&  remover   r   r+  r  3sympy.tensor.array.expressions.from_array_to_matrixr.  )r<   scalarsr*  ri   scalarr.  s         r>   merge_scalars#_EditArrayContraction.merge_scalars  s     ..L<''(A-|, / A%%a( ':'Qyy':;t!!"a'%%eFm4_,?HZHZ[\H]HeHe,fDq!) ;s   :Dc           	        Sn[        [        5      n[        5       nU R                   H'  nUR	                  [        UR
                  5      5        M)     US    n/ n/ n[        5       nSn	U R                   GH!  nSn
UR
                   H  nUc  UR                  U	5        U
S-  n
U	S-  n	M#  US:  a  M+  USU-
     R                  X-   5        X;   S:X  a.  X;  a)  UR                  US-
  U-
  5        UR                  U5        O-X;  a(  UR                  US-
  U-
  5        UR                  U5        U
S-  n
M     UR
                   Vs/ s H  ob  US:  a  UOS PM     snUl        U[        UR
                   Vs/ s H  ob  US:  d  M  UPM     sn5      -  nGM$     Xg-   n[        U5      nUR                  5        Vs/ s H  n[        U5      S:  d  M  [        U5      PM!     nnU R                  5         U R                  5         U R                   Vs/ s H  nUR                  PM     nnU R!                  5       n[#        [%        U6 /UQ76 n['        U/UQ76 nU R(                  b=  [        U R(                   VVs/ s H  o  H  nUPM     M     snn5      n[+        UU5      n[+        UU5      nU$ s  snf s  snf s  snf s  snf s  snnf rq  )r   r  r   r|  rk  r   r  r&  r   r   r,   valuesr   r3  r+  r+  get_contraction_indicesr  r   r  r  r   )r<   rd  diag_indicescount_index_freqr*  free_index_count	inv_perm1	inv_perm2donecounter4counter2ri   r  r   r  diag_indices_filteredr   r   r  r   expr2rt   permutation2expr3s                           r>   r  *_EditArrayContraction.to_array_contraction  s    "4("9 ..L##GL,@,@$AB / ,D1 		u  ..L H!))9$$X.MHMH6R!V$++G,>?#&!+$$%5%9A%=>HHQK]$$%5%9A%=>HHQKA! *$ VbUiUi#jUiPQ16At$KUi#jL s|';';R';!yAPQEA';RSSG1 /4 (3 !45 4@3F3F3H W3HaCPQFUVJq3H W'+'9'9:'9'9:"::<!"7">UATU='<="".%$2I2I&U2IQSTaqSTq2I&UVL!%6Ee[1) $kR !X ;
 'Vs*   J/6J4J4;J9J9J>8K
c                    [        U R                  5       Vs/ s H  n/ PM     nnSnU R                   H1  nUR                   H  nUb  X%   R	                  U5        US-  nM      M3     U$ s  snf r  )rr   r  r|  r   r&  )r<   ri   r  current_positionr*  rt   s         r>   r7  -_EditArrayContraction.get_contraction_indices  sz    <A$BdBd<e/f<eq<e/f ! ..L!))='*112BC A%  * /
 #" 0gs   A+c                    XR                   :  a  [        S5      e/ n[        U R                  5       HD  u  p4[        UR                  5       H&  u  pVX:X  d  M  UR                  [        X55      5        M(     MF     U$ )Nz%index value exceeding the index range)r  ro   r   r|  r   r&  r  )r<   r  r  ri   r*  rt   r  s          r>   r{  +_EditArrayContraction.get_mapping_for_index  sl    444DEE#%	(););<OA'(<(<=
>$$WQ]3 >  = rA   c                   [        U R                  5       Vs/ s H  n/ PM     nn[        U R                  5       HD  u  p[        UR                  5       H&  u  pEUc  M
  X%   R                  [        X5      5        M(     MF     U$ s  snf r3   )rr   r  r   r|  r   r&  r  )r<   ri   r  r*  rt   r  s         r>   &get_contraction_indices_to_ind_rel_pos<_EditArrayContraction.get_contraction_indices_to_ind_rel_pos  sz    @EdFhFh@i3j@i1B@i3j(););<OA#L$8$89?',33GAMB :  = #" 4ks   Bc                \    SnU R                    H  nXR                  ;   d  M  US-  nM     U$ )z:
Count the number of arguments that have the given index.
r   ra   r|  r   )r<   r  rd  r*  s       r>   count_args_with_index+_EditArrayContraction.count_args_with_index!  s5      ..L,,,1 / rA   c                j    U R                    Vs/ s H  o!UR                  ;   d  M  UPM     nnU$ s  snf )z1
Get a list of arguments having the given index.
rN  )r<   r  ri   r  s       r>   get_args_with_index)_EditArrayContraction.get_args_with_index+  s4     (,'9'9P'9!aii=OA'9P
 Qs   00c                    [        5       nU R                   H;  nUR                  UR                   Vs1 s H  o3c  M  US:  d  M  UiM     sn5        M=     [	        U5      $ s  snf rX   )r  r|  rk  r   r   )r<   ru   r   ri   s       r>   number_of_diagonal_indices0_EditArrayContraction.number_of_diagonal_indices2  sP    u%%CKKCKKKKqQUKKL &4y Ls   A!
A!
A!
c                p   / n/ nSnSnU R                    H`  n/ nUR                   H:  nUb  US:  a  UR                  U5        US-  nM$  UR                  U5        US-  nM<     UR                  U5        Mb     U(       a  [        S U 5       5      OSnU Vs/ s H  oxU-
  PM	     nnX/-   U l        g s  snf )Nr   r   ra   c              3  H   #    U  H  o(       a  [        U5      OS v   M     g7f)r   N)r  rg   s     r>   rj   @_EditArrayContraction.track_permutation_start.<locals>.<genexpr>I  s     ?;ac!fr);s    ")r|  r   r&  r  r  )	r<   r   	perm_diagrd  r?  r*  permri   max_inds	            r>   track_permutation_start-_EditArrayContraction.track_permutation_start9  s    	 ..LD!))=1u!((2 AG$1 * t$ / DO#?;??TV*34)Qq[)	4"-"; 5s   B3c                    U R                   R                  U5      nU R                   R                  U5      nU R                  U   R                  U R                  U   5        U R                  R	                  U5        g r3   )r|  r  r  r   r   )r<   destinationfrom_elementindex_destinationindex_elements        r>   track_permutation_merge-_EditArrayContraction.track_permutation_mergeM  si     ..44[A**00> 1299$:Q:QR_:`a##M2rA   c                    SnU R                    H>  n[        UR                   Vs/ s H	  oDb  M  UPM     sn5      nX1:X  a  X"U-   4s  $ X%-  nM@     [        S5      es  snf )z_
Return the range of the free indices of the arg as absolute positions
among all free indices.
r   argument not foundr|  r   r   r   )r<   r   rd  r*  ri   number_free_indicess         r>   get_absolute_free_range-_EditArrayContraction.get_absolute_free_rangeS  sl    
  ..L"%,2F2F&T2FQq2F&T"U"*= ===*G	 /
 -..	 'Us
   A
A
c                    SnU R                    H)  n[        UR                  5      nX1:X  a  X"U-   4s  $ X$-  nM+     [        S5      e)zK
Return the absolute range of indices for arg, disregarding dummy
indices.
r   rg  rh  )r<   r   rd  r*  number_indicess        r>   r}  (_EditArrayContraction.get_absolute_range`  sS    
  ..L !5!56N". 888%G	 /
 -..rA   )r  r|  r  N)r  zAtyping.Union[ArrayContraction, ArrayDiagonal, ArrayTensorProduct])r   r  r#  r  )rx   zlist[list[int]])rx   zlist[_IndPos])rx   zlist[list[_IndPos]])r  r  rx   r  )r  r  rx   zlist[_ArgE])r`  r  ra  r  )r   r  rx   ztyping.Tuple[int, int])rF   rG   rH   rI   ry   r  r  r~  r+  r3  r  r7  r{  rK  rO  rR  r{   rU  r]  rd  rj  r}  rK   rE   rA   r>   rz  rz  Z  sl    7Fr46XgAF##  <(3//rA   rz  c                   [        U [        [        45      (       a  g[        U [        5      (       a  [	        U R
                  5      $ [        U [        5      (       a  U R                  5       $ [        U [        5      (       a  U R                  $ [        U [        5      (       a  U R
                  nUc  g[	        U5      $ [        U S5      (       a  [	        U R
                  5      $ g)Nrs  r   r1   r   )r4   r   r#   r   r   r1   r    r  r!   r"   r   r2  s     r>   r   r   n  s    $]344$-..4::$	""yy{$  yy$$$

=u:tW4::rA   c                b    [        U [        5      (       a  U R                  5       $ [        U 5      $ r3   )r4   r   r   r   r   s    r>   r   r     s&    $-..||~D>rA   c                \    [        U [        5      (       a  U R                  $ [        U 5      /$ r3   )r4   r   r   r   rq  s    r>   r  r    s'    $-..}}rA   c                >    [        U S5      (       a  U R                  $ g)Nr1   rE   )r   r1   rq  s    r>   r   r     s    tWzzrA   c                P    [        U [        5      (       a  U R                  5       $ U $ r3   )r4   r   r  rq  s    r>   r  r    s#    $$$$$&&rA   c                     [        U SS0UD6$ Nr   T)r   r   r   s     r>   r   r     s    tA$A&AArA   c                $    [        U /UQ7SS0UD6$ rv  )r   )r   r  r   s      r>   r  r    s    DT#6TTTVTTrA   c                $    [        U /UQ7SS0UD6$ rv  )r  )r   r  r   s      r>   r  r    s    N 0NtNvNNrA   c                    [        X4SS0UD6$ rv  )r   )r   r   r   s      r>   r   r     s    tFtFvFFrA   c                     [        U SS0UD6$ rv  )r  rw  s     r>   r  r    s    T7777rA   c                    [        X5      $ r3   )r8   )r   r   s     r>   rC   rC     s    &&rA   )a
__future__r   collections.abcr5   r   r   r   	functoolsr   rp   r   typingsympy.core.numbersr   sympy.core.relationalr	   (sympy.functions.special.tensor_functionsr   sympy.core.basicr   sympy.core.containersr   sympy.core.exprr   sympy.core.functionr   r   sympy.core.mulr   sympy.core.singletonr   sympy.core.sortingr   sympy.core.symbolr   r   sympy.matrices.matrixbaser   #sympy.matrices.expressions.diagonalr   "sympy.matrices.expressions.matexprr   "sympy.matrices.expressions.specialr   sympy.tensor.array.arrayopr   r   r   r   #sympy.tensor.array.dense_ndim_arrayr   sympy.tensor.array.ndim_arrayr    sympy.tensor.indexedr!   r"   r#   $sympy.tensor.array.expressions.utilsr$   r%   r&   r'   r(   r)   r/  r+    sympy.combinatorics.permutationsr,   sympy.core.sympifyr-   r/   rM   r8   r   r   r   r   r  r   r  r&  r   r  r  r  rz  r   r   r  r   r  r   r  r  r   r  rC   rE   rA   r>   <module>r     s   "   ,      & * C " '   2  " / - 0 B 9 9 f f G 3 7 <1 1 , 7 '
7 
7B* B<2[4 2[j
 2z 2-(E -(^Wp. Wpt8^$ 8^vT' Tn
M<) M<`&- 5 &-RXB, XBv<'# <'~ >( ((Q/ Q/h( BUOG8'rA   