+
    h,                   B   ^ RI Ht ^ RIt^ RIt^ RIHtHt ^ RIHt ^ RI	t	^ RI	H
t
 ^ RIt^ RIHt ^ RIHt ^ RIHt ^ R	IHt ^ R
IHt ^ RIHt ^ RIHtHt ^ RIHt ^ RIHt ^ RIH t  ^ RI!H"t"H#t# ^ RI$H%t% ^ RI&H't' ^ RI(H)t) ^ RI*H+t+ ^ RI,H-t-H.t.H/t/H0t0 ^ RI1H2t2 ^ RI3H4t4 ^ RI5H6t6H7t7 ^ RI(H8t8 ^ RI9H:t:H;t;H<t<H=t=H>t>H?t? ^ RI@HAtA ^ RIBHCtC ^ RIDHEtE  ! R R]4      tF ! R  R!]F4      tG ! R" R#]4      tH ! R$ R%]F4      tI ! R& R']F4      tJ ! R( R)]4      tK ! R* R+]K4      tL ! R, R-]K4      tM ! R. R/]K4      tN ! R0 R1]K4      tO ! R2 R3]K4      tP ! R4 R5]K4      tQ ! R6 R7]K4      tR ! R8 R94      tS ! R: R;4      tT ! R< R=4      tUR> tVR? tWR@ tXRA tYRB tZRC t[RD t\RE t]RF t^RG t_RH t`R# )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                  .    ] tR t^'t$ R]R&   R tR tRtR# )
_ArrayExprztuple[Expr, ...]shapec                	    \        V\        P                  P                  4      '       g   V3p\        P                  W4       V P                  V4      # N)
isinstancecollectionsabcIterableArrayElement_check_shape_getselfitems   &&R/usr/lib/python3/dist-packages/sympy/tensor/array/expressions/array_expressions.py__getitem___ArrayExpr.__getitem__*   s<    $ 8 8997D!!$-yy    c                	    \        W4      # r2   )_get_array_element_or_slicer:   s   &&r=   r9   _ArrayExpr._get0   s    *466r@    N)__name__
__module____qualname____firstlineno____annotations__r>   r9   __static_attributes__rD   r@   r=   r/   r/   '   s    7r@   r/   c                  P    ] tR t^4tRtRtR R lt]R 4       t]R 4       t	R t
RtR	# )
ArraySymbolz)
Symbol representing an array expression
Fc                    V ^8  d   QhRRRR/# )   r0   ztyping.Iterablereturnz'ArraySymbol'rD   )formats   "r=   __annotate__ArraySymbol.__annotate__;   s      O  r@   c                	    \        V\        4      '       d   \        V4      p\        \	        \
        V4      !  p\        P                  ! WV4      pV# r2   )r3   strr   r   mapr-   r   __new__)clssymbolr0   objs   &&& r=   rV   ArraySymbol.__new__;   s>    fc""F^Fs8U+,ll3.
r@   c                	(    V P                   ^ ,          # r   _argsr;   s   &r=   nameArraySymbol.nameC       zz!}r@   c                	(    V P                   ^,          #    r]   r_   s   &r=   r0   ArraySymbol.shapeG   rb   r@   c                	   \         ;QJ d&    R  V P                   4       F  '       d   K   RM	  RM! R  V P                   4       4      '       g   \        R4      h\        P                  ! V P                   Uu. uF  p\        V4      NK  	  up!   Uu. uF  q V,          NK  	  pp\        V4      P                  ! V P                  !  # u upi u upi )c              3  8   "   T F  qP                   x  K  	  R # 5ir2   
is_Integer.0is   & r=   	<genexpr>*ArraySymbol.as_explicit.<locals>.<genexpr>L        4A<<   FTz1cannot express explicit array with symbolic shape)allr0   
ValueError	itertoolsproductranger   reshape)r;   jrm   datas   &   r=   as_explicitArraySymbol.as_explicitK   s    s44sss4444PQQ!*!2!2tzz4Rz!U1Xz4R!ST!SAQ!ST&t,44djjAA 5STs   8C	CrD   N)rE   rF   rG   rH   __doc__	_iterablerV   propertyr`   r0   rz   rJ   rD   r@   r=   rL   rL   4   sA     I    Br@   rL   c                  b    ] tR t^RtRtRtRtRtR t]	R 4       t
]R 4       t]R 4       tR tRtR	# )
r7   z
An element of an array.
Tc                	0   \        V\        4      '       d   \        V4      p\        V4      p\        V\        P
                  P                  4      '       g   V3p\        \        V4      4      pV P                  W4       \        P                  ! WV4      pV# r2   )r3   rT   r   r-   r4   r5   r6   tupler8   r   rV   )rW   r`   indicesrY   s   &&& r=   rV   ArrayElement.__new__[   sp    dC  $<D~';??#;#;<<jG5>*'ll3g.
r@   c                	    \        V4      p\        VR 4      '       d   \        R4      p\        V4      \        VP                  4      8w  d   Vh\
        ;QJ d/    R \        W!P                  4       4       F  '       g   K   RM$	  RM ! R \        W!P                  4       4       4      '       d   \        R4      h\
        ;QJ d    R V 4       F  '       g   K   RM	  RM! R V 4       4      '       d   \        R4      hR# )	r0   z3number of indices does not match shape of the arrayc              3  4   "   T F  w  rW8  R 8H  x  K  	  R# 5i)TNrD   )rl   rm   ss   &  r=   rn   ,ArrayElement._check_shape.<locals>.<genexpr>m   s     I0HAFt#0H   TFzshape is out of boundsc              3  0   "   T F  q^ 8  R8H  x  K  	  R# 5i)r   TNrD   rk   s   & r=   rn   r   o   s     01A$s   zshape contains negative valuesN)r   hasattr
IndexErrorlenr0   anyziprs   )rW   r`   r   index_errors   &&& r=   r8   ArrayElement._check_shapef   s    .4!!$%Z[K7|s4::.!!sIGZZ0HIsssIGZZ0HIII !9::3003330000=>> 1r@   c                	(    V P                   ^ ,          # r\   r]   r_   s   &r=   r`   ArrayElement.namer   rb   r@   c                	(    V P                   ^,          # rd   r]   r_   s   &r=   r   ArrayElement.indicesv   rb   r@   c                	F   \        V\        4      '       g   \        P                  # W8X  d   \        P                  # VP
                  V P
                  8w  d   \        P                  # \        P                  ! R  \        V P                  VP                  4       4       4      # )c              3  <   "   T F  w  r\        W4      x  K  	  R # 5ir2   r
   rl   rm   rx   s   &  r=   rn   0ArrayElement._eval_derivative.<locals>.<genexpr>   s     Z=YTQN100=Ys   )
r3   r7   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Zr@   rD   N)rE   rF   rG   rH   r|   	_diff_wrt	is_symbolis_commutativerV   classmethodr8   r~   r`   r   r   rJ   rD   r@   r=   r7   r7   R   s_     IIN	 	? 	?    
[r@   r7   c                  <    ] tR t^tRtR t]R 4       tR tR t	Rt
R# )	ZeroArrayzE
Symbolic array of zeros. Equivalent to ``ZeroMatrix`` for matrices.
c                	    \        V4      ^ 8X  d   \        P                  # \        \        V4      p\
        P                  ! V .VO5!  pV# r\   )r   r   r   rU   r-   r   rV   rW   r0   rY   s   &* r=   rV   ZeroArray.__new__   s:    u:?66MHe$ll3''
r@   c                	    V P                   # r2   r]   r_   s   &r=   r0   ZeroArray.shape       zzr@   c                	    \         ;QJ d&    R  V P                   4       F  '       d   K   RM	  RM! R  V P                   4       4      '       g   \        R4      h\        P                  ! V P                  !  # )c              3  8   "   T F  qP                   x  K  	  R # 5ir2   ri   rk   s   & r=   rn   (ZeroArray.as_explicit.<locals>.<genexpr>   rp   rq   FT/Cannot return explicit form for symbolic shape.)rr   r0   rs   r   zerosr_   s   &r=   rz   ZeroArray.as_explicit   sL    s44sss4444NOO&,,djj99r@   c                	"    \         P                  # r2   )r   r   r:   s   &&r=   r9   ZeroArray._get   s    vvr@   rD   NrE   rF   rG   rH   r|   rV   r~   r0   rz   r9   rJ   rD   r@   r=   r   r      s*      :
r@   r   c                  <    ] tR t^tRtR t]R 4       tR tR t	Rt
R# )OneArrayz
Symbolic array of ones.
c                	    \        V4      ^ 8X  d   \        P                  # \        \        V4      p\
        P                  ! V .VO5!  pV# r\   )r   r   r   rU   r-   r   rV   r   s   &* r=   rV   OneArray.__new__   s:    u:?55LHe$ll3''
r@   c                	    V P                   # r2   r]   r_   s   &r=   r0   OneArray.shape   r   r@   c                	   \         ;QJ d&    R  V P                   4       F  '       d   K   RM	  RM! R  V P                   4       4      '       g   \        R4      h\        \	        \        \        P                  V P                  4      4       Uu. uF  p\        P                  NK  	  up4      P                  ! V P                  !  # u upi )c              3  8   "   T F  qP                   x  K  	  R # 5ir2   ri   rk   s   & r=   rn   'OneArray.as_explicit.<locals>.<genexpr>   rp   rq   FTr   )rr   r0   rs   r   rv   r   operatormulr   r   rw   )r;   rm   s   & r=   rz   OneArray.as_explicit   s    s44sss4444NOO&uVHLLRVR\R\=]7^'_7^!7^'_`hhjnjtjtuu'_s   Cc                	"    \         P                  # r2   )r   r   r:   s   &&r=   r9   OneArray._get   s    uur@   rD   Nr   rD   r@   r=   r   r      s+      v
r@   r   c                  B    ] tR t^t]R 4       tR t]R 4       tR tRt	R# )_CodegenArrayAbstractc                (    V P                   R,          # )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]
NNN)	_subranksr_   s   &r=   subranks_CodegenArrayAbstract.subranks   s    4 ~~a  r@   c                ,    \        V P                  4      # )z
The sum of ``subranks``.
)sumr   r_   s   &r=   subrank_CodegenArrayAbstract.subrank   s     4==!!r@   c                	    V P                   # r2   _shaper_   s   &r=   r0   _CodegenArrayAbstract.shape       {{r@   c           
     	    VP                  R R4      pV'       dH   V P                  ! V P                   Uu. uF  q3P                  ! R/ VB NK  	  up!  P	                  4       # V P	                  4       # u upi )deepTrD   )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   A2rD   N)
rE   rF   rG   rH   r~   r   r   r0   r   rJ   rD   r@   r=   r   r      s2    ! !6"  (r@   r   c                  <    ] tR t^tRtR tR t]R 4       tR t	Rt
R# )ArrayTensorProductz>
Class to represent the tensor product of array-like objects.
c                	6   V Uu. uF  p\        V4      NK  	  ppVP                  R R4      pV Uu. uF  p\        V4      NK  	  pp\        P                  ! V .VO5!  pWVn        V Uu. uF  p\        V4      NK  	  pp\        ;QJ d    R V 4       F  '       g   K   RM	  RM! R V 4       4      '       d	   RVn        M1\        ;QJ d    . R V 4       F  NK  	  5M! R V 4       4      Vn        V'       d   VP                  4       # V# u upi u upi u upi )canonicalizeFc              3  (   "   T F  qR J x  K
  	  R # 5ir2   rD   rk   s   & r=   rn   -ArrayTensorProduct.__new__.<locals>.<genexpr>        )&QDy&   TNc              3  4   "   T F  q F  q"x  K  	  K  	  R # 5ir2   rD   r   s   &  r=   rn   r      s     <&Q!Qq!q&r   )r-   popget_rankr   rV   r   	get_shaper   r   r   r   )	rW   r   kwargsr   r   ranksrY   rm   shapess	   &*,      r=   rV   ArrayTensorProduct.__new__   s    )-.#.zz.%8*./$3#$/mmC'$'(,-1)A,-3)&)333)&)))CJ<&<<&<<CJ$$&&
! / 0 .s   DD)Dc                	P  aaa V P                   pV P                  V4      pV Uu. uF  p\        V4      NK  	  pp. p\        V4       F  w  op\	        V\
        4      '       g   K  TP                  VP                  P                   UUu. uF&  qU Uu. uF  qf\        VR S 4      ,           NK  	  upNK(  	  upp4       VP                  VS&   K  	  V'       d=   \        \        V!  \        \        V4      ^,
          4      \        V4      ,          4      # \        V4      ^8X  d
   V^ ,          # \        ;QJ d    R V 4       F  '       g   K   RM	  RM! R V 4       4      '       d>   \!        \"        P$                  V Uu. uF  p\'        V4      NK  	  upR4      p\)        V!  # \        V4       UUu/ uF  w  rr\	        V\*        4      '       g   K  WrbK!  	  p	ppV	'       Ed   V Uu. uF/  p\	        V\*        4      '       d   \-        V4      M
\        V4      NK1  	  pp\/        \1        ^ .V,           4      4      R R o\        V Uu. uF'  p\	        V\*        4      '       d   VP                  MTNK)  	  up!  p
V	P3                  4        UaUUu. uFL  w  oq"P4                   F7  p\6        ;QJ d    . VV3R lV 4       F  NK  	  5M! VV3R lV 4       4      NK9  	  KN  	  pppp\9        V
.VO5!  # \        V4       UUu/ uF  w  rr\	        V\:        4      '       g   K  WrbK!  	  pppV'       Ed   . p. pV Uu. uF  p\        V4      NK  	  pp\/        \1        ^ .V,           4      4      R R o\        V4       EF  w  op\	        V\:        4      '       d   \        V4      \        VP<                  4      ,
          p\        VP<                  4      pTP                  \?        V4       Uu. uF  pSS,          V,           NK  	  up4       TP                  \?        WV,           4       Uu. uF  pSS,          V,           NK  	  up4       K  TP                  \?        \        V4      4       Uu. uF  pSS,          V,           NK  	  up4       EK  	  VP                  V4       \        V Uu. uF'  p\	        V\:        4      '       d   VP                  MTNK)  	  up!  p
V Uu. uF/  p\	        V\:        4      '       d   \-        V4      M
\        V4      NK1  	  pp\/        \1        ^ .V,           4      4      R R oVP3                  4        UaUUu. uFL  w  oq"P<                   F7  p\6        ;QJ d    . VV3R lV 4       F  NK  	  5M! VV3R lV 4       4      NK9  	  KN  	  pppp\        \A        V
.VO5!  \C        V4      4      # V PD                  ! VRR/ # u upi u upi u uppi u upi u uppi u upi u upi u upppi u uppi u upi u upi u upi u upi u upi u upi u upppi )	Nc              3  N   "   T F  p\        V\        \        34      x  K  	  R # 5ir2   )r3   r   r   )rl   r   s   & r=   rn   3ArrayTensorProduct._canonicalize.<locals>.<genexpr>  s     H4Cz#	:6774s   #%TFc              3  D   <"   T F  pSS,          V,           x  K  	  R # 5ir2   rD   )rl   kcumulative_ranksrm   s   & r=   rn   r     s     (L!Q)9!)<q)@)@!    c              3  D   <"   T F  pSS,          V,           x  K  	  R # 5ir2   rD   )rl   r   cumulative_ranks2rm   s   & r=   rn   r   4  s     %J1&7&:Q&>&>r   r   rD   )#r   _flattenr   	enumerater3   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_indicesrv   _array_diagonalr,   r   )r;   r   r   r   permutation_cyclesrx   r   rm   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 3H4H333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tu55(L!(L55(L!(L#L  zQ#L[o  #R%b?+>??*3D/\/Z]=[VQV/	\9"$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@0Ca0G0GQZ/[\$$uRVXQXGY%ZGY!&6q&9A&=&=GY%Z['..QVW_`cWdQe/fQeA0@0Ca0G0GQe/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%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s   W
WW/W W!
3W&W&(5W,-W1 W6/1W6?W=W=8X'X
&X
)X
(-X5X	 X!*1X!Wc                	    V UUu. uF+  p\        W 4      '       d   VP                  MV. F  q3NK  	  K-  	  pppV# u uppi r2   )r3   r   )rW   r   r   rm   s   &&  r=   r   ArrayTensorProduct._flatten9  sA    !YTc
38L8LCHHSVRW,Wa,WTY Zs   1<c           	     	    \        V P                   Uu. uF'  p\        VR 4      '       d   VP                  4       MTNK)  	  up!  # u upi rz   )r   r   r   rz   r;   r   s   & r=   rz   ArrayTensorProduct.as_explicit>  sB    dhdmdmndm]`GC4O4Os0UXXdmnoons   -ArD   N)rE   rF   rG   rH   r|   rV   r   r   r   rz   rJ   rD   r@   r=   r   r      s,    &74r  pr@   r   c                  <    ] tR tRtRtR tR t]R 4       tR t	Rt
R# )	ArrayAddiB  z(
Class for elementwise array additions.
c                	   V Uu. uF  p\        V4      NK  	  ppV Uu. uF  p\        V4      NK  	  pp\        \        V4      4      p\	        V4      ^8w  d   \        R4      hV Uu. uF  q3P                  NK  	  pp\	        V Uu0 uF
  qff   K  VkK  	  up4      ^8  d   \        R4      hVP                  RR4      p\        P                  ! V .VO5!  pWHn
        \        ;QJ d    R V 4       F  '       g   K   RM	  RM! R V 4       4      '       d	   RVn        MV^ ,          Vn        V'       d   VP                  4       # V# u upi u upi u upi u upi )re   z!summing arrays of different ranksNzmismatching shapes in additionr   Fc              3  (   "   T F  qR J x  K
  	  R # 5ir2   rD   rk   s   & r=   rn   #ArrayAdd.__new__.<locals>.<genexpr>U  r   r   T)r-   r   r  setr   rs   r0   r   r   rV   r   r   r   r   )	rW   r   r   r   r   r   rm   r   rY   s	   &*,      r=   rV   ArrayAdd.__new__G  s   )-.#.*./$3#$/SZ u:?@AA'+,t))t,636a634q8=>>zz.%8mmC'$'3)&)333)&)))CJCJ$$&&
' // -3s   EE(EEEc                	   V P                   pV P                  V4      pV Uu. uF  p\        V4      NK  	  ppV Uu. uF#  p\        V\        \
        34      '       d   K!  VNK%  	  pp\        V4      ^ 8X  dV   \        ;QJ d    R V 4       F  '       g   K   RM	  RM! R V 4       4      '       d   \        R4      h\	        V^ ,          !  # \        V4      ^8X  d
   V^ ,          # V P                  ! VRR/ # u upi u upi )r   c              3  0   "   T F  qe   K  Vx  K  	  R # 5ir2   rD   rk   s   & r=   rn   )ArrayAdd._canonicalize.<locals>.<genexpr>f  s     2f11fs   
TFzIcannot handle addition of ZeroMatrix/ZeroArray and undefined shape objectr   )
r   _flatten_argsr   r3   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>s2f2sss2f222)*uvvfQi((Y!^7Nyy$3U33 2Ts   C7C<C<c                	    . pV FG  p\        V\        4      '       d   VP                  VP                  4       K6  VP	                  V4       KI  	  V# r2   )r3   r  r   r   append)rW   r   new_argsr   s   &&  r=   r"  ArrayAdd._flatten_argsm  sA    C#x(()$	 
 r@   c           
     	    \        \        P                  V P                   Uu. uF'  p\	        VR 4      '       d   VP                  4       MTNK)  	  up4      # u upi r  )r   r   r   r   r   rz   r  s   & r=   rz   ArrayAdd.as_explicitw  sM    LLRVR[R[\R[3'#}"="=S__3FR[\^ 	^\s   -A
rD   N)rE   rF   rG   rH   r|   rV   r   r   r"  rz   rJ   rD   r@   r=   r  r  B  s+    ,4   ^r@   r  c                      ] tR tRtRtRR ltR t]R 4       t]R 4       t	]
R 4       t]
R	 4       t]
R
 4       t]
R 4       tR t]
R 4       tR t]
R 4       t]
R 4       tRtR# )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                	>  aa ^ RI Hp \        V4      p\        V4      pV P	                  SW4V4      oV! S4      oSP
                  pW8w  d   \        R4      hVP                  RR4      p	\        P                  ! WS4      p
\        V4      .V
n
        \        V4      oSf	   RV
n        M]\        ;QJ d*    . VV3R l\        \        S4      4       4       F  NK  	  5M#! VV3R l\        \        S4      4       4       4      V
n        V	'       d   V
P!                  4       # V
# )r   r*   z8Permutation size must be the length of the shape of exprr   FNc              3  B   <"   T F  pSS! V4      ,          x  K  	  R # 5ir2   rD   )rl   rm   r   r0   s   & r=   rn   &PermuteDims.__new__.<locals>.<genexpr>  s     P>Ou[^44>O   )sympy.combinatoricsr+   r-   r   _get_permutation_from_argumentssizers   r   r   rV   r   r   r   r   rv   r   r   )rW   r   r   index_order_oldindex_order_newr   r+   	expr_rankpermutation_sizer   rY   r0   s   &&f&&,     @r=   rV   PermuteDims.__new__  s    3~TN	99+irs!+.&++(WXXzz.%8mmC{3!$($=CJPeCJ>OPPeCJ>OPPCJ$$&&
r@   c                	P   V P                   pV P                  p\        V\        4      '       d#   VP                   pVP                  pW$,          pTp\        V\        4      '       d   V P                  W4      w  r\        V\        4      '       d   V P                  W4      w  r\        V\        \        34      '       d4   \        VP                   Uu. uF  qQP                  V,          NK  	  up!  # VP                  pV\        V4      8X  d   V# V P                  WR R7      # u upi )F)r   )r   r   r3   r   r   '_PermuteDims_denestarg_ArrayContractionr   )_PermuteDims_denestarg_ArrayTensorProductr   r   
array_formr0   sortedr   )r;   r   r   subexprsubpermrm   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                	(    V P                   ^ ,          # r\   r   r_   s   &r=   r   PermuteDims.expr      yy|r@   c                	(    V P                   ^,          # rd   rB  r_   s   &r=   r   PermuteDims.permutation  rD  r@   c           
     	   \        VP                  4      p\        VP                  4      p\        \	        ^ .VP
                  ,           4      4      p\        \        V4      4       Uu. uF  qcWV,          WV^,           ,           NK  	  pp\        V4       UUu. uF  w  rhV\        V4      3NK  	  p	ppV	P                  R R7       V	 Uu. uF  qf^ ,          NK  	  p
pV
 Uu. uF  qdV,          NK  	  ppV
 Uu. uF  qgV,          NK  	  pp\        \        V UUu. uF  qf F  qNK  	  K  	  upp4      4      p\        V!  V3# u upi u uppi u upi u upi u upi u uppi )r   c                    V ^,          # rd   rD   xs   &r=   <lambda>GPermuteDims._PermuteDims_denestarg_ArrayTensorProduct.<locals>.<lambda>  s    adr@   key)r,   r;  r  r   r   r   rv   r   r   r<  sortr+   r   )rW   r   r   perm_image_formr   cumulrm   perm_image_form_in_componentscomppsperm_args_image_formargs_sortedperm_image_form_sorted_argsrx   new_permutations   &&&            r=   r:  5PermuteDims._PermuteDims_denestarg_ArrayTensorProduct  sJ    %[%;%;<DIIZdmm 345 X]]`ae]fWg(hWgRS%!*)MWg%(h/89V/WX/WGAq&,/WX 	N#.01b!b1(<=(<1Aww(<=Qe&fQeAQ'G'GQe#&f%j=X1d=Xbc]^!bc!=X1d&ef$k2OCC )iX  2=&f1ds$   ' EE	E E7EEc                	  a \        V\        4      '       g   W3# \        VP                  \        4      '       g   W3# VP                  P                  pVP                  P                   Uu. uF  p\        V4      NK  	  ppVP                  pV UUu. uF  qw F  qNK  	  K  	  p	pp\        \        ^ .V,           4      4      p
. p\        VP                  4      p^ p\        \        V4      4       Fd  p. p\        W,          W^,           ,          4       F+  pW9   d   K  VP                  W,          4       V^,          pK-  	  VP                  V4       Kf  	  \        VP                  4       UUu. uF-  w  r\        \        W,          W^,           ,          4      4      NK/  	  pppVP!                  VP                  V4      pVR,          pV UUu. uF  qw Uu. uF  qf   K  V! V4      NK  	  upNK!  	  ppp\        \        V4      4      pVP#                  R R7       V Uu. uF  qw^ ,          NK  	  ppV Uu. uF  pVV,          NK  	  pp\        V UUu. uF  qw F  qNK  	  K  	  upp4      oV Uu. uF  qsV,          NK  	  ppV Uu. uF5  p\$        ;QJ d    . V3R lV 4       F  NK  	  5M! V3R lV 4       4      NK7  	  pp\'        \)        V!  .VO5!  p\+        \        V Uu. uF  pVV,          NK  	  up UUu. uF  qw F  qNK  	  K  	  upp4      4      pVV3# u upi u uppi u uppi u upi u uppi u upi u upi u uppi u upi u upi u upi u uppi )r   c                    V ^,          # rd   rD   rI  s   &r=   rK  EPermuteDims._PermuteDims_denestarg_ArrayContraction.<locals>.<lambda>6  s    !r@   rM  c              3  6   <"   T F  pSV,          x  K  	  R # 5ir2   rD   )rl   rx   new_index_perm_array_forms   & r=   rn   FPermuteDims._PermuteDims_denestarg_ArrayContraction.<locals>.<genexpr>>  s     (Qq!)B1)E)Eq   r   )r3   r   r   r   r   r   r  r  r   r,   r;  rv   r   r&  r   r   _push_indices_uprO  r   r  r   r+   )rW   r   r   r   r   r   r  rm   rx   contraction_indices_flatrQ  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   rX  r^  s   &&&                         @r=   r9  3PermuteDims._PermuteDims_denestarg_ArrayContraction  s4   $ 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- .::\tt\:5HI5HLOO5HI$.;K/W;KaUVPQUV;K/W$X!%89%8GG%89[n"o[nVW55(Qq(Q55(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s`   ,L'L,13L2L=L8#L8/L=$M;MM
7MM+M-MM"8L=c                	:   VP                   p\        VP                  4       UUUu. uF)  w  rE\        VP                   V,          4       F  qdNK  	  K+  	  pppp\        VP	                  4       4       Uu. uF
  qB! V4      NK  	  pp\        VP                  4      p	Wx^ ,          ,          p
. p. p\        4       p\        V4       F  w  rNW~,          V
8w  d   VP                  V4       . pW~,          p
VP                  V4       W:,          p\        V4      V8X  g   KX  VP                  \        V4      4       V Uu. uF  qf\        V4      ,
          NK  	  ppWt,          p\        V	V,          \        V4      4      V	V&   VP                  V
4       . pK  	  VP                  V4       \        \        \        V	4      4      4      p/ p^ .\        \        V4      4      ,           p\        \        V4      4       Fs  p\        VV,          VV^,           ,          4       Uu0 uF  qgW,          ,          kK  	  pp\        V4      ^8w  d   KQ  \!        \#        V4      4      pVV8w  g   Kn  VVV&   Ku  	  . p. pV'       d   \        V4      ^ 8X  d&   VP%                  4       w  ppVP                  V4       M%VR,          pVV9  d   . pKQ  VP'                  V4      pVV9   d   VP                  V4       . pK~  VP                  V4       K  V F;  p\        V4       F)  w  ppVVVV^,           \        V4      ,          ,          &   K+  	  K=  	  \        V4       UUUu. uF5  w  pp\        V4       Uu. uF  qlVV,          V,           ,          NK  	  upNK7  	  ppppV Uu. uF  qIV,          NK  	  p	pV Uu. uF  pVV,          NK  	  ppV UUu. uF  qD F  qfNK  	  K  	  ppp\)        V	!  \        V4      3# u upppi u upi u upi u upi u upi u upppi u upi u upi u uppi )r   r   )r   r   r   rv   r   r  r  r   r&  r   r<  minr   r+   r   r   nextiterpopitemr   r   ) rW   r   r   r   rm   r   rx   	index2argpermuted_indicesr'  arg_candidate_indexcurrent_indicesrX  inserted_arg_cand_indicesidxarg_candidate_ranklocal_current_indicesr  args_positionsmapscumulative_subranksr   elemlinescurrent_liner   vlinerg  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)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!$1<=tQUc$i$789 ( 
 ktt|j}~j}bfbcefTYZ[T\]T\q/B1/E/IJJT\]j}~)78AQKK8AO!PA"4Q"7"7!P'=I'=!q!AqA'=I$h/=M1NNNA \J )\ q< ^~8!PIsA   /O+4O29O7O<P$PPP)PPPc                	   \        VP                  4      pVP                  pVP                  p^ .\        \	        V4      4      ,           pV Uu. uF  p\        V4      NK  	  ppV Uu. uF  p\        V4      NK  	  p	p. p
\        V4       F  w  r{Rp\        \        V4      ^,
          4       Fp  pW,          Wm,          8  g   K  W,          Wm^,           ,          8  g   K4  \        W=,          \        V Uu. uF  qWm,          ,
          NK  	  up.4      4      W=&   Rp M	  V'       g   K  V
P                  V4       K  	  \        V!  \        WP                  R7      3# u upi u upi u upi )r   TF)r2  )r  r   r   r   r   rr  maxr   rv   r   r   r+   r&  r   r2  )rW   r   r   r   r   r   r  rm   
cyclic_min
cyclic_maxcyclic_keepcycleflagrx   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]C]glBmAn5opDG D 9 t""5) / %d+[K[K[-\\\ 32 Cns   E(E$6E)c                ^    V P                  V P                  V P                  4      pVf   V # V# )z
DEPRECATED.
)_nest_permutationr   r   )r;   rets   & r=   nest_permutationPermuteDims.nest_permutation  s/     $$TYY0@0@A;K
r@   c           	     	^  a \        V\        4      '       d   \        V P                  W4      !  # \        V\        4      '       d   VP
                  p\        P                  ! V.VO5!  p\        V4      oVP                   Uu. uF5  p\        ;QJ d    . V3R  lV 4       F  NK  	  5M! V3R  lV 4       4      NK7  	  pp\        \        VP                  S4      .VO5!  # \        V\        4      '       d-   \        VP                   Uu. uF  p\        Wr4      NK  	  up!  # R# u upi u upi )c              3  4   <"   T F  pS! V4      x  K  	  R # 5ir2   rD   )rl   rx   newpermutations   & r=   rn   0PermuteDims._nest_permutation.<locals>.<genexpr>  s     &D!Q~a'8'8!   N)r3   r   r   r  r   r   '_convert_outer_indices_to_inner_indicesr+   r  r   r  r   r   r  
_array_addr   )	rW   r   r   cycles	newcyclesrm   new_contr_indicesr   r  s	   &&&     @r=   r  PermuteDims._nest_permutation  s    d.// #"G"G"Z[[.// ,,F(PPQU_X^_I(3NNRNfNf gNf&D!&D&D!&D!DNf g%k$))^&LaO`aah''S#C =STT	 !h  Ts   D%+D%
D*c                	    V P                   p\        VR 4      '       d   VP                  4       p\        WP                  4      # r  )r   r   rz   r   r   r;   r   s   & r=   rz   PermuteDims.as_explicit  s7    yy4''##%D4!1!122r@   c                	    Vf*   Ve   Vf   \        R4      h\        P                  W#V4      # Ve   \        R4      hVe   \        R4      hV# )NzPermutation not definedz2index_order_new cannot be defined with permutationz2index_order_old cannot be defined with permutation)rs   r   "_get_permutation_from_index_orders)rW   r   r3  r4  dims   &&&&&r=   r1  +PermuteDims._get_permutation_from_arguments  s]    &/*A !:;;AA/dghh* !UVV* !UVVr@   c                	`   \        \        V4      4      V8w  d   \        R 4      h\        \        V4      4      V8w  d   \        R4      h\        \        P                  \        V4      \        V4      4      4      ^ 8  d   \        R4      hV Uu. uF  qAP	                  V4      NK  	  ppV# u upi )z*wrong number of indices in index_order_newz*wrong number of indices in index_order_oldz>index_order_new and index_order_old must have the same indices)r   r  rs   symmetric_differenceindex)rW   r3  r4  r  rm   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+rD   )NNN)rE   rF   rG   rH   r|   rV   r   r~   r   r   r   r:  r9  r  r  r  r  rz   r1  r  rJ   rD   r@   r=   r   r   }  s    GR.@&     D D0 .) .)` BO BOH ] ](  3 
 
  r@   r   c                      ] tR tRtRtR tR t]R 4       t]R 4       t	]
R 4       t]
R 4       t]R	 4       t]R
 4       t]R 4       t]R R l4       tR tR t]R 4       t]R 4       t]R 4       tR tRtR# )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                	   \        V4      pV Uu. uF  p\        \        V4      !  NK  	  ppVP                  R R4      p\	        V4      pVe+   V P
                  ! V.VO5/ VB  V P                  Wb4      w  rvMRp\        V4      ^ 8X  d   V# \        P                  ! W.VO5!  pWxn
        \        V4      Vn        Whn        V'       d   VP                  4       # V# u upi )r   FN)r-   r   r<  r   r   	_validate_get_positions_shaper   r   rV   
_positions_get_subranksr   r   r   )	rW   r   r  r   rm   r   r0   	positionsrY   s	   &&*,     r=   rV   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                	   V P                   pV P                  pV Uu. uF  p\        V4      ^8X  g   K  VNK  	  pp\        V4      ^ 8  Ed   \        V4       UUu/ uF!  w  r5\        V4      ^8X  g   K  V^ ,          VbK#  	  ppp\        V4       UUu/ uF  w  r5\        V4      ^8  g   K  WSbK  	  pppV Uu. uF  p\        V4      ^8  g   K  VNK  	  pp\	        V 4      p	\        V4      p
W,
          p. p^ p\
        P                  V\        \        V	4      4      \	        V4      4      pV F  pW69   d!   VP                  WV,          ,           4       K)  \        V\        \        34      '       d   VP                  V4       V^,          pKa  VP                  WV,          ,           4       K  	  \        V4      p\        V4      ^ 8  d   \        \        V.VO5!  V4      # \        W4      # \        V\         4      '       d   V P"                  ! V.VO5!  # \        V\
        4      '       d   V P$                  ! V.VO5!  # \        V\&        4      '       d   V P(                  ! V.VO5!  # \        V\*        \,        34      '       d)   V P/                  VP0                  V4      w  pp\+        V!  # V P2                  ! V.VO5RR/ # u upi u uppi u uppi u upi )re   r   F)r   r  r   r   r   r  _push_indices_downr  rv   r&  r3   r   intr,   r   r  r  _ArrayDiagonal_denest_ArrayAdd#_ArrayDiagonal_denest_ArrayDiagonalr   !_ArrayDiagonal_denest_PermuteDimsr   r   r  r0   r   )r;   r   r  rm   trivial_diagsrg  trivial_posdiag_posdiagonal_indices_shortrank1rank2rank3inv_permutationcounter1indices_downr   r  r0   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VaZaa1A"%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.   KKK4KK,K9KKc                	  a \        V 4      oV F  p\        ;QJ d    V3R  lV 4       F  '       g   K   RM	  RM! V3R  lV 4       4      '       d   \        R4      h\        V Uu0 uF  pSV,          kK  	  up4      ^8w  d   \        R4      hVP	                  RR4      '       g   \        V4      ^8:  d   \        R4      h\        \        V4      4      \        V4      8w  g   K  \        R4      h	  R# u upi )	c              3  >   <"   T F  q\        S4      8  x  K  	  R # 5ir2   r   )rl   rx   r0   s   & r=   rn   *ArrayDiagonal._validate.<locals>.<genexpr>0  s     .AqE
?As   TFz%index is larger than expression shapez-diagonalizing indices of different dimensionsallow_trivial_diagsz%need at least two axes to diagonalizezaxis index cannot be repeatedN)r   r   rs   r   r   r  )r   r  r   rm   rx   r0   s   &*,  @r=   r  ArrayDiagonal._validate*  s     $!As.A.sss.A... !HIIa(aE!HHa()Q. !PQQ::3U;;A! !HII3q6{c!f$ !@AA " )s   &C5
c                	    V Uu. uFE  q V^ ,          ,          ^8w  g   K  \         ;QJ d    . R V 4       F  NK  	  5M! R V 4       4      NKG  	  up# u upi )r   c              3  $   "   T F  qx  K  	  R # 5ir2   rD   )rl   rx   s   & r=   rn   ;ArrayDiagonal._remove_trivial_dimensions.<locals>.<genexpr>;  s     ^Aas   r   )r0   r  rm   s   &* r=   _remove_trivial_dimensions(ArrayDiagonal._remove_trivial_dimensions9  sB    -=R-=qtPQAQ#^^^^#-=RRRs   A
A%Ac                	(    V P                   ^ ,          # r\   rB  r_   s   &r=   r   ArrayDiagonal.expr=  rD  r@   c                	(    V P                   R ,          # re   NNrB  r_   s   &r=   r  ArrayDiagonal.diagonal_indicesA      yy}r@   c                	P  a V P                   pV UUu. uF  q3 F  qDNK  	  K  	  pppVP                  4        \        V 4      p\        V4      pWg,
          p\	        V4       Uu. uF  p^ NK  	  upo^ p	^ p
\	        V4       FH  pW8  d"   WV
,          8  d   V	^,          p	V
^,          p
K'  SV;;,          V
,          uu&   V	^,          p	KJ  	  \
        ;QJ d    . V3R lV 4       F  NK  	  5M! V3R lV 4       4      pW!,           p\        V P                  .VO5!  # u uppi u upi )r   c              3     <"   T F7  p\         ;QJ d    . V3R  lV 4       F  NK  	  5M! V3R  lV 4       4      x  K9  	  R# 5i)c              3  D   <"   T F  pSV,          V,           x  K  	  R # 5ir2   rD   rl   rx   shiftss   & r=   rn   3ArrayDiagonal._flatten.<locals>.<genexpr>.<genexpr>W  s     ,FAqVAY]]Ar   Nr  rl   rm   r  s   & r=   rn   )ArrayDiagonal._flatten.<locals>.<genexpr>W  s/     &gPf1uu,FA,Fuu,FA,F'F'FPf
   A/A)r  rO  r   r   rv   r   r  r   )r   outer_diagonal_indicesinner_diagonal_indicesrm   rx   	all_inner
total_rank
inner_rank
outer_rankre  pointerr  r  s   &*          @r=   r   ArrayDiagonal._flattenE  s   !%!6!6 6B 611QQ 6	B!$'
^
,
":./.!./z"A&76H+H111I IqLG # "'&gPf&g&gPf&g!g1Jtyy<+;<<# C 0s   D#D#c           	     	h    \        VP                   Uu. uF  p\        V.VO5!  NK  	  up!  # u upi r2   )r  r   r  )rW   r   r  r   s   &&* r=   r  ,ArrayDiagonal._ArrayDiagonal_denest_ArrayAdd[  s.    tyyYyOCC2BCyYZZY   /c                	*    V P                   ! V.VO5!  # r2   r   )rW   r   r  s   &&*r=   r  1ArrayDiagonal._ArrayDiagonal_denest_ArrayDiagonal_  s    ||D4#344r@   c                   V ^8  d   QhRR/# )rN   r   r   rD   )rP   s   "r=   rQ   ArrayDiagonal.__annotate__d  s     
 
[ 
r@   c           
     	  a V UUu. uF"  q3 Uu. uF  qAP                  V4      NK  	  upNK$  	  ppp\        \        V4      4       Uau. uFG  o\        ;QJ d    V3R  lV 4       F  '       g   K   RM	  RM! V3R  lV 4       4      '       d   KE  SNKI  	  ppV Uu. uF  q1P                  V4      NK  	  pp\	        \        V4      4       UUu/ uF  w  r8WbK	  	  p	ppV Uu. uF  q9V,          NK  	  p
p\        V
4      p\        \        V4      4       Uu. uF  q3V,           NK  	  ppW,           p\        \        VP                  .VO5!  V4      # u upi u uppi u upi u upi u uppi u upi u upi )c              3  .   <"   T F
  pSV9   x  K  	  R # 5ir2   rD   rl   rx   rm   s   & r=   rn   BArrayDiagonal._ArrayDiagonal_denest_PermuteDims.<locals>.<genexpr>f  s     >`O_!qAvO_   TF)
r   rv   r   r   r   r<  r   r   r  r   )rW   r   r  rm   rx   back_diagonal_indicesnondiagback_nondiagrg  remapnew_permutation1shiftdiag_block_permrX  s   &&*`          r=   r  /ArrayDiagonal._ArrayDiagonal_denest_PermuteDimsc  sE   K[ \K[aq!Aq!"2"21"5q!AK[ \#HTN3a333>`O_>`333>`O_>`;`113a5<=W((+W="+F<,@"AB"A$!"AB.:;l!HHl;$%.3C8M4N.OP.Ou99.OP*<		& 
 	
 "B \a=B;PsE   EEE	EE0EEE"E'"E-E2Ec                	&   a  V 3R  lp\        W!4      # )c                b   < V \        SP                  4      8  d   SP                  V ,          # R # r2   )r   r  )rJ  r;   s   &r=   rK  <ArrayDiagonal._push_indices_down_nonstatic.<locals>.<lambda>v  s'    ADOO8L4Ldooa0VRVVr@   r$   r;   r   	transforms   f& r=   _push_indices_down_nonstatic*ArrayDiagonal._push_indices_down_nonstaticu  s    V	3IGGr@   c                	&   a  V 3R  lp\        W!4      # )c                   < \        SP                  4       FC  w  r\        V\        4      '       d   W8X  g!   \        V\        4      '       g   K9  W9   g   KA  Vu # 	  R # r2   )r   r  r3   r  r   )rJ  rm   rg  r;   s   &  r=   r  ;ArrayDiagonal._push_indices_up_nonstatic.<locals>.transform{  s@    !$//2q#&&16z!U7K7KPQPVH 3r@   r  r  s   f& r=   _push_indices_up_nonstatic(ArrayDiagonal._push_indices_up_nonstaticy  s    	
 4IGGr@   c                	b   a V P                  \        V4      V4      w  opV3R  lp\        WR4      # )c                :   < V \        S4      8  d
   SV ,          # R # r2   r  )rJ  r  s   &r=   rK  2ArrayDiagonal._push_indices_down.<locals>.<lambda>  s    a#i..@ilJdJr@   r  rv   r$   rW   r  r   rankr0   r  r  s   &&&&  @r=   r   ArrayDiagonal._push_indices_down  s/    33E$KAQR	5J	3IGGr@   c                	b   a V P                  \        V4      V4      w  opV3R  lp\        WR4      # )c                   < \        S4       FI  w  r\        V\        4      '       d   W8X  g'   \        V\        \        34      '       g   K?  W9   g   KG  Vu # 	  R # r2   )r   r3   r  r   r   )rJ  rm   rg  r  s   &  r=   r  1ArrayDiagonal._push_indices_up.<locals>.transform  s@    !),q#&&16z!eU^7T7TZ[Z`H -r@   r  r  s   &&&&  @r=   ra  ArrayDiagonal._push_indices_up  s1    33E$KAQR	5	
 4IGGr@   c                	n  aa \         ;QJ d     . V3R  l\        S4       4       F  NK  	  5M! V3R  l\        S4       4       4      pV'       d
   \        V!  MRw  rE\         ;QJ d    . V3R lS 4       F  NK  	  5M! V3R lS 4       4      pV'       d
   \        V!  MRw  rxWG,           p	WX,           oV	S3# )c              3     <a"   T FN  w  op\         ;QJ d    V3R  lS 4       F  '       g   K   RM	  RM! V3R  lS 4       4      '       d   KH  SV3x  KP  	  R# 5i)c              3  .   <"   T F
  pSV9   x  K  	  R # 5ir2   rD   r  s   & r=   rn   ?ArrayDiagonal._get_positions_shape.<locals>.<genexpr>.<genexpr>  s     HjYiTUaYir  TFNr   )rl   shprm   r  s   & @r=   rn   5ArrayDiagonal._get_positions_shape.<locals>.<genexpr>  s<     k-=61cSSHjYiHjSSSHjYiHjEjhq#h-=s   AAAAc              3  F   <"   T F  qSV^ ,          ,          3x  K  	  R# 5i)r   NrD   )rl   rm   r0   s   & r=   rn   r    s     A0@1%!+&0@s   !)rD   rD   )r   r   r   )
rW   r0   r  data1pos1shp1data2pos2shp2r  s
   &ff       r=   r  "ArrayDiagonal._get_positions_shape  s    kYu-=kkYu-=kk$)S%[x
A0@AA0@AA$)S%[x
K	%r@   c                	    V P                   p\        VR 4      '       d   VP                  4       p\        V.V P                  O5!  # r  )r   r   rz   r   r  r  s   & r=   rz   ArrayDiagonal.as_explicit  s<    yy4''##%Dd;T%:%:;;r@   rD   N)rE   rF   rG   rH   r|   rV   r   staticmethodr  r  r~   r   r  r   r   r  r  r  r  r  r  ra  r  rz   rJ   rD   r@   r=   r  r    s
   2,$FL B B S S     = =* [ [ 5 5 
 
"HH H H
 H H    <r@   r  c                  X    ] tR tRtR t]R 4       t]R 4       t]R 4       tR t	R t
RtR	# )
ArrayElementwiseApplyFunci  c                	    \        V\        4      '       g   \        R 4      p\        W1! V4      4      p\        P	                  WV4      p\        V4      Vn        V# d)r3   r   r   r   rV   r  r   )rW   functionelementr/  rY   s   &&&  r=   rV   !ArrayElementwiseApplyFunc.__new__  sK    (F++c
Aa!-H#++C7C%g.
r@   c                	(    V P                   ^ ,          # r\   rB  r_   s   &r=   r0  "ArrayElementwiseApplyFunc.function  rD  r@   c                	(    V P                   ^,          # rd   rB  r_   s   &r=   r   ArrayElementwiseApplyFunc.expr  rD  r@   c                	.    V P                   P                  # r2   r   r0   r_   s   &r=   r0   ArrayElementwiseApplyFunc.shape  s    yyr@   c                	    \        R 4      pV P                  V4      pVP                  V4      p\        V\        4      '       d   \        V4      pV# \        W4      pV# r.  )r   r0  diffr3   r   typer   )r;   r/  r0  fdiffs   &   r=   _get_function_fdiff-ArrayElementwiseApplyFunc._get_function_fdiff  sU    #J==#a eX&&KE  1$Er@   c                	    V P                   p\        VR 4      '       d   VP                  4       pVP                  V P                  4      # r  )r   r   rz   	applyfuncr0  r  s   & r=   rz   %ArrayElementwiseApplyFunc.as_explicit  s9    yy4''##%D~~dmm,,r@   rD   N)rE   rF   rG   rH   rV   r~   r0  r   r0   r>  rz   rJ   rD   r@   r=   r,  r,    sM          -r@   r,  c                     ] tR tRtRtR tR tR tR t]	R 4       t
]R 4       t]R	 4       t]R
 4       tR tR t]	R 4       t]	R 4       t]	R 4       t]	R 4       t]R 4       t]R 4       t]R 4       t]R 4       t]R R l4       t]R 4       tR t]	R 4       t]R 4       t]R 4       t]R 4       t ]R 4       t!R t"R t#R  t$R! t%R"t&R## )$r   i  zl
This class is meant to represent contractions of arrays in a form easily
processable by the code printers.
c                	  aa \        S4      o\        V4      pVP                  R R4      p\        P                  ! W.SO5!  p\        V4      Vn        \        VP                  4      Vn        \        \        VP                  4      4       Uau/ uFH  o\        ;QJ d    V3R lS 4       F  '       d   K   RM	  RM! V3R lS 4       4      '       g   KE  SSbKJ  	  ppWun        \        V4      pV P                  ! V.SO5!   V'       dE   \        ;QJ d     . V3R l\!        V4       4       F  NK  	  5M! V3R l\!        V4       4       4      pWn        V'       d   VP%                  4       # V# u upi )r   Fc              3  .   <"   T F
  pSV9  x  K  	  R # 5ir2   rD   )rl   cindrm   s   & r=   rn   +ArrayContraction.__new__.<locals>.<genexpr>  s'       SB  nAeiST\`S`  nAr  Tc              3     <a"   T FL  w  op\         ;QJ d    V3R  lS 4       F  '       g   K   RM	  RM! V3R  lS 4       4      '       d   KH  Vx  KN  	  R# 5i)c              3  .   <"   T F
  pSV9   x  K  	  R # 5ir2   rD   r  s   & r=   rn   5ArrayContraction.__new__.<locals>.<genexpr>.<genexpr>  s     GlXkSTQXkr  TFNr  )rl   r  rm   r  s   & @r=   rn   rG    s8     m,<&!SCCGlXkGlCCCGlXkGlDl##,<s   AAA
A)r%   r-   r   r   rV   r  r   r&   _mappingrv   r   rr   _free_indices_to_positionr   r  r   r   r   r   )	rW   r   r  r   r   rY   rm   free_indices_to_positionr0   s	   &&j,  `  r=   rV   ArrayContraction.__new__  s2   78KL~zz.%8mmC<(;<%d+1#--@27CMM8J2K  $C2KQss  SB  nA  SBsss  SB  nA  SB  PBDAqD2K   $C(@%$d101EmIe,<mEEmIe,<mmE
$$&&
 $Cs   E/E/5E/E/c                	~   V P                   pV P                  p\        V4      ^ 8X  d   V# \        V\        4      '       d   V P
                  ! V.VO5!  # \        V\        \        34      '       d   V P                  ! V.VO5!  # \        V\        4      '       d   V P                  ! V.VO5!  # \        V\        4      '       d9   V P                  W4      w  rV P                  W4      w  r\        V4      ^ 8X  d   V# \        V\        4      '       d   V P                  ! V.VO5!  # \        V\         4      '       d   V P"                  ! V.VO5!  # V Uu. uF5  p\        V4      ^8  g!   \%        V4      V^ ,          ,          ^8w  g   K3  VNK7  	  pp\        V4      ^ 8X  d   V# V P&                  ! V.VO5RR/ # u upi )r   r   F)r   r  r   r3   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  rm   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_cOdhiOiqq*=j"#q(KyyH 3H%HH	 ks   0F:
F:c                	*    V^8X  d   V # \        R4      hre   zDProduct of N-dim arrays is not uniquely defined. Use another method.r#  r;   others   &&r=   __mul__ArrayContraction.__mul__      A:K%&lmmr@   c                	*    V^8X  d   V # \        R4      hrY  rZ  r[  s   &&r=   __rmul__ArrayContraction.__rmul__  r_  r@   c                	    \        V 4      pVf   R # V FC  p\        V Uu0 uF  qBV,          R8w  g   K  W$,          kK  	  up4      ^8w  g   K:  \        R4      h	  R # u upi )Nz+contracting indices of different dimensionsr   )r   r   rs   )r   r  r0   rm   rx   s   &*   r=   r  ArrayContraction._validate  sX    $= %Aa:a8r>HEHHa:;q@ !NOO %:s
   A
A
c                	    V UUu. uF  q3 F  qDNK  	  K  	  pppVP                  4        \        V4      p\        Wb4      # u uppi r2   )rO  r)   r$   rW   r  r   rm   rx   flattened_contraction_indicesr  s   &&&    r=   r  #ArrayContraction._push_indices_down#  sK    4G(S4GqQRAQR4G%(S%**,@A^_	3IGG )T   Ac                	    V UUu. uF  q3 F  qDNK  	  K  	  pppVP                  4        \        V4      p\        Wb4      # u uppi r2   )rO  r'   r$   rf  s   &&&    r=   ra  !ArrayContraction._push_indices_up*  sK    4G(S4GqQRAQR4G%(S%**,>?\]	3IGG )Tri  c           
     	  aaa \        V\        4      '       d   \        4       h\        V\        4      '       g   W3# VP                  p\        \        ^ .V,           4      4      o. pVP                   Uu. uF  p. NK  	  pp\        4       pV F  p\        \        VP                  4      4       F  o\        VP                  S,          \        4      '       g   K,  \        ;QJ d     VV3R lV 4       F  '       d   K   RM	  RM! VV3R lV 4       4      '       g   Kp  VS,          P                  V U	u. uF  qSS,          ,
          NK  	  up	4       VP                  V4        K  	  VP                  V4       K  	  \        V4      \        V4      8X  d   W3# \        V4      p
\        \        \        V
4       Uu. uF  qUV9   d   ^M^ NK  	  up4      4      oV Uu. uF#  p\        P                   ! V3R lV 4       4      NK%  	  pp\#        \%        VP                  V4       UUu. uF  w  r\'        V.VO5!  NK  	  upp!  pW3# u upi u up	i u upi u upi u uppi )r   c              3  z   <"   T F0  pSS,          Tu;8*  ;'       d    SS^,           ,          8  Mu x  K2  	  R# 5ire   NrD   )rl   r   cumranksrx   s   & r=   rn   AArrayContraction._lower_contraction_to_addends.<locals>.<genexpr>@  s/     SARAx{a77(1Q3-77ARs   ;;FTc              3  B   <"   T F  qSV,          ,
          x  K  	  R # 5ir2   rD   r  s   & r=   rn   rp  J  s     7Qq!F1Iqr/  )r3   r  r#  r   r   r  r   r   r  rv   r   rr   r&  updater   r   r   r   r   r  )rW   r   r  r   contraction_indices_remainingrm   contraction_indices_args	backshiftcontraction_groupr   r  r   contrr  ro  rx   r  s   &&&           @@@r=   rT  .ArrayContraction._lower_contraction_to_addends1  s   dH%%%''$ 233,,==
A3>23(*%04		#:	1B	 #:E	!43tyy>*!$))A,993SARS333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   5I	5I=I)I(I
c           	       aa \        V 4      pV P                  p. p\        V4       EFf  w  op\        V4      ^8:  d   K  VP	                  S4      pV P
                  P                  V^ ,          ,          p. p. pV F  w  rVP                  V	,          pVP                  pVP                  V4      w  r^V
,
          pW,           o^VP                  9  go   V^8H  RJ d   VP                  R8w  gU   \        ;QJ d)    VV3R l\        V4       4       F  '       g   K   RM	  RM! VV3R l\        V4       4       4      '       d   VP                  W34       K  VP                  W34       K  	  \        V4      ^8  d   EKX  V F   w  pp
\        VP                  4      Vn        K"  	  VR,          V,           VR,          ,           pV^ ,          w  pp
VP                  V
,          pV^R  F  w  pp
VVP                  V
&   VP                  4       pVP                  P                  R4      ^V
,
          8X  g   Q hVVP                  VP                  P                  R4      &   VP                  V4       K  	  VR,          w  pp
VVP                  V
&   EKi  	  V F)  pVP!                  V\#        \%        ^4      R.4      4       K+  	  VP'                  4       # )	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.
Tc              3  D   <"   T F  w  rVS8w  g   K  SV9   x  K  	  R # 5ir2   rD   )rl   lilindlother_arg_abss   &  r=   rn   ?ArrayContraction.split_multiple_contractions.<locals>.<genexpr>  s&     e8VurZ\`dZd**8Vs     F:Nre   Nr  N)re   re   r   )_EditArrayContractionr  r   r   get_mapping_for_indexr   r0   args_with_indr1  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_vecr}  r~  s   &                    @@r=   split_multiple_contractions,ArrayContraction.split_multiple_contractionsP  sq   . '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Ce	BU8VeCCCe	BU8Veee&&~6NNC>2 %. ;!# 
 &
7.qyy9	 &)"o7+b/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#=> ! **,,r@   c           
     	   \        V P                  \        4      '       g   V # V P                  P                  V P                  P                  V P
                  4      p. pV P                  P                  R ,          pV F  p\        V4      pV F_  pV Uu. uF  qvV9   g   K  VNK  	  ppTP                  V UU	u. uF  qw F  qNK  	  K  	  up	p4       V Uu. uF  qwV9  g   K  VNK  	  ppKa  	  VP                  \        \        V4      4      4       K  	  \        P                  W24      p\        \        V P                  P                  .VO5!  .VO5!  # u upi u up	pi u upi r   )r3   r   r  r  r  r  r  r   r&  r<  r  ra  r  r  )
r;   contraction_downrn  r  rm   rv  rx   r   diagonal_withr|  s
   &         r=   flatten_contraction_of_diagonal0ArrayContraction.flatten_contraction_of_diagonal  sH   $))]33K9977		8R8RTXTlTlm"$9955a8!A $Q,< G,<qQ,< G!((])N]Aq!A!])NO/?#Z/?!MCYAA/? #Z   $**6#6G2H+IJ " #0"@"@AQ"k!		!

 %
 	
 !H)N#Zs   E"E:EE$%E$c                	    / pV UUu. uF  q3 F  qDNK  	  K  	  ppp^ pV  F!  pWe9   d   V^,          pK  WbV&   V^,          pK#  	  V# u uppi r\   rD   )free_indicesr  rM  rm   rx   rg  re  inds   &&      r=   !_get_free_indices_to_position_map2ArrayContraction._get_free_indices_to_position_map  sf    #% 4G(S4GqQRAQR4G%(SC:1,3S)qLG	  
 (' )Ts   Ac                   V P                   pV UUu. uF  q" F  q3NK  	  K  	  pppVP                  4        \        V 4      p\        V4      pWV,
          p\	        V4       Uu. uF  p^ NK  	  pp^ p	^ p
\	        V4       FG  pW8  d"   WV
,          8  d   V	^,          p	V
^,          p
K'  W;;,          V
,          uu&   V	^,          p	KI  	  V# u uppi u upi )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  rO  r   r   rv   )r   inner_contraction_indicesrm   rx   r  r  r  r  r  re  r  s   &          r=   _get_index_shifts"ArrayContraction._get_index_shifts  s    * %)$<$<! 9E 911aQ1Q 9	E!$'
^
,
":./.!./z"A&76H+H11I IqLG #  F 0s   C"Cc                	   a \         P                  V 4      o\        ;QJ d    . V3R  lV 4       F  NK  	  5pV# ! V3R  lV 4       4      pV# )c              3     <"   T F7  p\         ;QJ d    . V3R  lV 4       F  NK  	  5M! V3R  lV 4       4      x  K9  	  R# 5i)c              3  D   <"   T F  pSV,          V,           x  K  	  R # 5ir2   rD   r  s   & r=   rn   UArrayContraction._convert_outer_indices_to_inner_indices.<locals>.<genexpr>.<genexpr>  s     /Iq!q	Aqr   Nr  r  s   & r=   rn   KArrayContraction._convert_outer_indices_to_inner_indices.<locals>.<genexpr>  s/     )mSla%%/Iq/I%%/Iq/I*I*ISlr  )r   r  r   )r   outer_contraction_indicesr  s   &*@r=   r  8ArrayContraction._convert_outer_indices_to_inner_indices  sF    !33D9$)E)mSl)mE!(( %*)mSl)m$m!((r@   c                	    V P                   p\        P                  ! V .VO5!  pW!,           p\        V P                  .VO5!  # r2   )r  r   r  r  r   )r   r  r  r  s   &*  r=   r   ArrayContraction._flatten  sD    $($<$<!$4$\$\]a$~d}$~!7S!$))B.ABBr@   c                	*    V P                   ! V.VO5!  # r2   r  )rW   r   r  s   &&*r=   rP  :ArrayContraction._ArrayContraction_denest_ArrayContraction  s    ||D7#677r@   c                	    V UUu. uF  q3 F  qDNK  	  K  	  ppp\        VP                  4       UUu. uF  w  r6W59  g   K  VNK  	  ppp\        V!  # u uppi u uppi r2   )r   r0   r   )rW   r   r  rm   rx   rb  rg  r0   s   &&*     r=   rQ  3ArrayContraction._ArrayContraction_denest_ZeroArray  s]    /B#N/B!AqAAA/B #N(4Z4tq8Y4Z%   $OZs   A
AAc           	     	h    \        VP                   Uu. uF  p\        V.VO5!  NK  	  up!  # u upi r2   )r  r   r  )rW   r   r  rm   s   &&* r=   rV  2ArrayContraction._ArrayContraction_denest_ArrayAdd  s4    QUQZQZ[QZA.qG3FGQZ[\\[r  c                	  aa VP                   oSP                  pV Uu. uF5  p\        ;QJ d    . V3R  lV 4       F  NK  	  5M! V3R  lV 4       4      NK7  	  ppV Uau. uFG  o\        ;QJ d    V3R lV 4       F  '       g   K   RM	  RM! V3R lV 4       4      '       d   KE  SNKI  	  ppV P	                  WV4      p\        \        VP                  .VO5!  \        V4      4      # u upi u upi )c              3  4   <"   T F  pS! V4      x  K  	  R # 5ir2   rD   )rl   rx   r   s   & r=   rn   HArrayContraction._ArrayContraction_denest_PermuteDims.<locals>.<genexpr>  s     (CAQr  c              3  .   <"   T F
  pSV9   x  K  	  R # 5ir2   rD   r  s   & r=   rn   r    s     0YAXAaAXr  TF)	r   r;  r   r   ra  r   r  r   r+   )rW   r   r  r?  rm   rn  	new_plistr   s   &&* `  @r=   rR  5ArrayContraction._ArrayContraction_denest_PermuteDims  s    &&&&M`"aM`55(C(C55(C(C#CM`"a %Z1SS0YAX0YSSS0YAX0Y-YQQ	Z(()@L	tyyC+BC	"
 	
 #bZs"   C++C+!C01C0C0(C0c                   V ^8  d   QhRR/# )rN   r   z'ArrayDiagonal'rD   )rP   s   "r=   rQ   ArrayContraction.__annotate__&  s     
 
/ 
r@   c                	8  a \        VP                  4      pVP                  VP                  V\        VP                  4      4      pV UUUu. uF=  qU UUu. uF,  p\        V\        \        34      '       d   TMV. F  qwNK  	  K.  	  uppNK?  	  pppp. pV F  p	V	R ,          p
\        V4       Fc  w  poSf   K  \        ;QJ d    V3R lV	 4       F  '       g   K   RM	  RM! V3R lV	 4       4      '       g   KN  V
P                  S4       RW6&   Ke  	  VP                  \        \        V
4      4      4       K  	  V Uu. uF
  qUf   K  VNK  	  pp\        P                  W4      p\!        \#        VP                  .VO5!  .VO5!  # u uppi u upppi u upi )r   Nc              3  ,   <"   T F	  qS9   x  K  	  R # 5ir2   rD   )rl   rm   diag_indgrps   & r=   rn   JArrayContraction._ArrayContraction_denest_ArrayDiagonal.<locals>.<genexpr>1  s     >AK'   TF)r  r  r  r   r   r3   r   r   r   r   r   r&  r<  r  r   ra  r  r  )rW   r   r  r  down_contraction_indicesrm   rx   r   rn  contr_indgrpr  new_diagonal_indices_downnew_diagonal_indicesr  s   &&*          @r=   rU  7ArrayContraction._ArrayContraction_denest_ArrayDiagonal%  s    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"+,<"=;&3>>333>>>>JJ{+*.$' #> $**6#c(+;< 5 1A$R0@1QQ0@!$R/@@AXttyyC+BC
!
 	
 %j  $M %Ss$   	F2F
FFF
Fc                	t  aaaa SP                   f   SV3# \        \        ^ .SP                  ,           4      4      p\	        \        SP                  4      4       Uu. uF+  p\        \	        W4,          W4^,           ,          4      4      NK-  	  ppV UUu0 uF  qD F  qfkK  	  K  	  uppo\        SP                  4       UUu. uFy  w  rG\        ;QJ d<    V3R l\	        W4,          W4^,           ,          4       4       F  '       d   K   RM1	  RM-! V3R l\	        W4,          W4^,           ,          4       4       4      NK{  	  uppo\        \	        \        SP                  4      4      VV3R lR7      pV Uu. uF  pSP                  V,          NK  	  p	pV UUu. uF  qEV,           F  qfNK  	  K  	  p
pp\        V
4      oV Uu. uF5  p\        ;QJ d    . V3R lV 4       F  NK  	  5M! V3R lV 4       4      NK7  	  pp\        V4      p\        V	!  V3# u upi u uppi u uppi u upi u uppi u upi )Nc              3  ,   <"   T F	  qS9   x  K  	  R # 5ir2   rD   )rl   rx   rb  s   & r=   rn   ?ArrayContraction._sort_fully_contracted_args.<locals>.<genexpr>D  s     cGb!%= =Gbr  FTc                b   < SV ,          '       d   ^ \        SP                  V ,          4      3# R# )r   rd   )r   r   )rJ  r   fully_contracteds   &r=   rK  >ArrayContraction._sort_fully_contracted_args.<locals>.<lambda>E  s<    euvwexexqBRSWS\S\]^S_B`>a  ?C  C  ?Cr@   rM  c              3  6   <"   T F  pSV,          x  K  	  R # 5ir2   rD   )rl   rx   index_permutation_array_forms   & r=   rn   r  I  s     (TRSQ)Ea)H)HRSr`  )r0   r  r   r   rv   r   r   r   rr   r<  r,   r   r%   r   )rW   r   r  rQ  rm   rh  rx   r   new_posr'  new_index_blocks_flatrn  rb  r  r  s   &f&         @@@r=   rS  ,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hnhiCCcuUXW\_`]`WaGbcCCCcuUXW\_`]`WaGbcc  sG  Hs499~.  5C  D*12'QDIIaLL'2,3 MGq!___G M'12G'H$^q"r^qYZ55(TRS(T55(TRS(T#T^q"r";<S"T$h/1HHH Z#N H2 M"rs1   1HHAH$	9H$8H*H/H5+H5c           	         V P                   pV P                   UUu. uF  q" Uu. uF  q1V,          NK  	  upNK  	  upp# u upi u uppi )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`).
)rK  r  )r;   mappingrm   rx   s   &   r=   _get_contraction_tuples(ArrayContraction._get_contraction_tuplesM  sC    8 --151I1IJ1IAQ'QQ'1IJJ'Js   AA A Ac                	   a V P                   p^ .\        \        V4      4      ,           oV Uu. uF5  p\        ;QJ d    . V3R lV 4       F  NK  	  5M! V3R lV 4       4      NK7  	  up# u upi )r   c              3  H   <"   T F  w  rSV,          V,           x  K  	  R # 5ir2   rD   )rl   rx   r   r   s   &  r=   rn   NArrayContraction._contraction_tuples_to_contraction_indices.<locals>.<genexpr>q  s!     :&q)!++s   ")r   r  r   r   )r   contraction_tuplesr   rm   r   s   &&  @r=   *_contraction_tuples_to_contraction_indices;ArrayContraction._contraction_tuples_to_contraction_indicesl  sX     3j&7!88DVWDVq:::::DVWWWs
   A,+A,c                	(    V P                   R ,          # r  )_free_indicesr_   s   &r=   r  ArrayContraction.free_indicess  s    !!!$$r@   c                	,    \        V P                  4      # r2   )dictrL  r_   s   &r=   rM  )ArrayContraction.free_indices_to_positionw  s    D2233r@   c                	(    V P                   ^ ,          # r\   rB  r_   s   &r=   r   ArrayContraction.expr{  rD  r@   c                	(    V P                   R ,          # r  rB  r_   s   &r=   r  $ArrayContraction.contraction_indices  r  r@   c                	    V P                   p\        V\        4      '       g   \        R 4      hVP                  p/ p^ p\        V4       F%  w  rV\        V4       F  pWW3W4&   V^,          pK  	  K'  	  V# )z(only for contractions of tensor products)r   r3   r   r#  r   r   rv   )r;   r   r   r  re  rm   r  rx   s   &       r=   "_contraction_indices_to_components3ArrayContraction._contraction_indices_to_components  sq    yy$ 233%&PQQ 'GA4[$%6 1 ! ( r@   c                   V P                   p\        V\        4      '       g   V # VP                  p\	        \        V4      R R7      p\        V!  w  rE\        V4       UUu/ uF  w  rgWdP                  V4      bK  	  pppV P                  4       p	V	 UU
Uu. uF   qf U
Uu. uF  w  rW,          V3NK  	  upp
NK"  	  p	p
pp\        V!  pV P                  VV	4      p\        V.VO5!  # u uppi u upp
i u upp
pi )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                &    \        V ^,          4      # rd   r   rI  s   &r=   rK  4ArrayContraction.sort_args_by_name.<locals>.<lambda>  s    <LQqT<Rr@   rM  )r   r3   r   r   r<  r   r   r  r  r   r  r  )r;   r   r   sorted_data
pos_sortedrV  rm   r   reordering_mapr  rx   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(	C4C.4C4.C4c                P    \        V .V P                  .V P                  O5!  w  rV# )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^_r@   c                	    V P                   p\        VR 4      '       d   VP                  4       p\        V.V P                  O5!  # r  )r   r   rz   r   r  r  s   & r=   rz   ArrayContraction.as_explicit  s<    yy4''##%D A(@(@AAr@   rD   N)'rE   rF   rG   rH   r|   rV   r   r]  ra  r*  r  r   r  ra  rT  r  r  r  r  r  r   rP  rQ  rV  rR  rU  rS  r  r  r~   r  rM  r   r  r  r  r(   rz   rJ   rD   r@   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r@   r   c                  L    ] tR tRtRtR t]R 4       t]R 4       tR t	R t
RtR	# )
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                	P   \        V4      p\        V\        4      '       g
   \        V!  p\        \        P
                  ! VP                  4      \        P
                  ! V4      4      R 8X  d   \        R4      h\        P                  ! WV4      p\        V4      Vn        Wn        V# )Fzshape mismatch)r-   r3   r   r	   r   r   r0   rs   r   rV   r   r   _expr)rW   r   r0   rY   s   &&& r=   rV   Reshape.__new__  sw    ~%''5MECLL,cll5.ABeK-..ll3e,5\
	
r@   c                	    V P                   # r2   r   r_   s   &r=   r0   Reshape.shape
  r   r@   c                	    V P                   # r2   )r  r_   s   &r=   r   Reshape.expr  r   r@   c                	   VP                  R R4      '       d   V P                  P                  ! V/ VB pMV P                  p\        V\        \
        34      '       d   VP                  ! V P                  !  # \        W0P                  4      # )r   T)	r   r   r   r3   r   r    rw   r0   r  )r;   r   r   r   s   &*, r=   r   Reshape.doit  sg    ::fd##99>>4262D99DdZ344<<,,tZZ((r@   c                	   V P                   p\        VR 4      '       d   VP                  4       p\        V\        4      '       d   ^ RIHp V! V4      pM\        V\        4      '       d   V # VP                  ! V P                  !  # )rz   )Array)
r   r   rz   r3   r   sympyr  r   rw   r0   )r;   eer  s   &  r=   rz   Reshape.as_explicit  sb    YY2}%%!Bb*%%#rBJ''Kzz4::&&r@   rD   N)rE   rF   rG   rH   r|   rV   r~   r0   r   r   rz   rJ   rD   r@   r=   r  r    s>    ,	    )	'r@   r  c                  @    ] tR tRt$ RtR]R&   R
R R lltR t]tR	t	R# )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                   V ^8  d   QhRR/# )rN   r   zlist[int | None] | NonerD   )rP   s   "r=   rQ   _ArgE.__annotate__9  s     # #)@ #r@   c                	    Wn         Vf,   \        \        V4      4       Uu. uF  pR NK  	  upV n        R # W n        R # u upi r2   )r1  rv   r   r   )r;   r1  r   rm   s   &&& r=   __init___ArgE.__init__9  s9    ?*/0A*BC*BQD*BCDL"L Ds   >c                	@    R V P                   : RV P                  : R2# )z_ArgE(z, )r1  r   r_   s   &r=   __str___ArgE.__str__@  s    "&,,==r@   r  r2   )
rE   rF   rG   rH   r|   rI   r  r  __repr__rJ   rD   r@   r=   r  r  '  s      #> Hr@   r  c                  6    ] tR tRtRtR R ltR t]tR tRt	R# )	_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                    V ^8  d   QhRRRR/# )rN   r   r  relrD   )rP   s   "r=   rQ   _IndPos.__annotate__M  s      C c r@   c                	    Wn         W n        R # r2   r   r  )r;   r   r  s   &&&r=   r  _IndPos.__init__M  s    r@   c                	@    R V P                   V P                  3,          # )z_IndPos(%i, %i)r  r_   s   &r=   r  _IndPos.__str__Q  s     DHHdhh#777r@   c              #  	R   "   V P                   V P                  . R j  xL
  R #  L5ir2   r  r_   s   &r=   __iter___IndPos.__iter__V  s     HHdhh'''s   '%'r  N)
rE   rF   rG   rH   r|   r  r  r  r!  rJ   rD   r@   r=   r  r  F  s    8 H(r@   r  c                      ] tR tRtRtR R ltR R ltR tR tR	 t	R
 t
R R ltR R ltR R ltR R ltR R lt]R 4       tR tR R ltR R ltR R ltRtR# )r  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                   V ^8  d   QhRR/# )rN   
base_arrayzAtyping.Union[ArrayContraction, ArrayDiagonal, ArrayTensorProduct]rD   )rP   s   "r=   rQ   "_EditArrayContraction.__annotate__i  s     7F 7F#d 7Fr@   c                	   \        V\        4      '       d2   \        VP                  4      pVP                  pVP
                  pRpEM7\        V\        4      '       d   \        VP                  \        4      '       d   \        VP                  P                  4      pVP                  P                  p\        P                  VP                  P
                  VP                  4      pVP                  P
                  pM\        VP                  \        4      '       d   / pVP                  pVP                  p. pMD/ pVP                  pVP                  p. pM'\        V\        4      '       d   Tp. pRpM
\        4       h\        V\        4      '       d   \        VP                  4      pMV.pV Uu. uF  p\        V4      NK  	  pp\        V4       F.  w  rV
 F#  pXV,          w  rWV,          P                  V&   K%  	  K0  	  Wn        \#        V4      V n        R V n        \        VP                  4      p\        V4       F?  w  rV F4  pW+,          w  rRV	,
          V P                   V,          P                  V&   K6  	  KA  	  R # u upi )NrD   r   )r3   r   r&   r   r   r  r  r  r  r   r#  r  r   r  r   r   r  r   number_of_contraction_indices_track_permutation)r;   r%  r  r   r  diagonalizedr   r   r  rm   contraction_tuplerx   arg_posrel_posrg  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   2Jc                    V ^8  d   QhRRRR/# )rN   r   r  new_argrD   )rP   s   "r=   rQ   r&    s     4 4 4 4r@   c                	    V P                   P                  V4      pV P                   P                  V^,           V4       R# rn  )r  r  insert)r;   r   r0  poss   &&& r=   r  "_EditArrayContraction.insert_after  s2      &&s+!!#'73r@   c                	Z    V ;P                   ^,          un         V P                   ^,
          # rd   )r(  r_   s   &r=   r  /_EditArrayContraction.get_new_contraction_index  s$    **a/*11A55r@   c                	   / pV P                    F4  pTP                  VP                   Uu/ uF  q3f   K  VRbK  	  up4       K6  	  \        \	        V4      4       F	  w  r4W1V&   K  	  \        V4      V n        V P                    F3  pVP                   Uu. uF  q1P                  VR 4      NK  	  upVn        K5  	  R # u upi u upi )Nr   )r  rr  r   r   r<  r   r(  r   )r;   updatesarg_with_indrm   rg  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                	8   . pV P                    F0  p\        VP                  4      ^ 8X  g   K  VP                  V4       K2  	  V F  pV P                   P	                  V4       K   	  \
        P                  ! V Uu. uF  q3P                  NK  	  up4      p\        V P                   4      ^ 8X  d'   V P                   P                  \        V4      4       R# ^ RI	H
p V! W@P                   ^ ,          P                  4      V P                   ^ ,          n        R# u upi )r   )_a2m_tensor_productN)r  r   r   r&  remover   r   r1  r  3sympy.tensor.array.expressions.from_array_to_matrixr=  )r;   scalarsr9  rm   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           	     	   ^ p\        \        4      p\        4       pV P                   F'  pVP	                  \        VP
                  4      4       K)  	  VR,          p. p. p\        4       p^ p	V P                   EFX  p^ p
VP
                   F  pVf&   VP                  V	4       V
^,          p
V	^,          p	K,  V^ 8  d   K5  VRV,
          ,          P                  W,           4       W;,          ^8X  d8   W9  d2   VP                  V^,
          V,
          4       VP                  V4       M6W9  d1   VP                  V^,
          V,
          4       VP                  V4       V
^,          p
K  	  VP
                   Uu. uF  qe
   V^ 8  d   TMRNK  	  upVn        T\        VP
                   Uu. uF  qe
   V^ 8  g   K  VNK  	  up4      ,          pEK[  	  Wg,           p\        V4      pVP                  4        Uu. uF   p\        V4      ^8  g   K  \        V4      NK"  	  ppV P                  4        V P                  4        V P                   Uu. uF  pVP                  NK  	  ppV P!                  4       p\#        \%        V!  .VO5!  p\'        V.VO5!  pV P(                  e<   \        V P(                   UUu. uF  q F  pVNK  	  K  	  upp4      p\+        VV4      p\+        VV4      pV# u upi u upi u upi u upi u uppi )r   Nr   )r   r  r   r  rr  r   r  r&  r   r   r,   valuesr   rB  r:  r1  get_contraction_indicesr  r   r  r)  r   )r;   re  diag_indicescount_index_freqr9  free_index_count	inv_perm1	inv_perm2donecounter4counter2rm   r  r   r  diag_indices_filteredr   r   r  r   expr2rx   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';!yAPQEAA';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*   9K/,K4=K4:K9K9K>:L
c                   V ^8  d   QhRR/# )rN   rO   zlist[list[int]]rD   )rP   s   "r=   rQ   r&    s     # # #r@   c                	    \        V P                  4       Uu. uF  p. NK  	  pp^ pV P                   F:  pVP                   F'  pVe   W%,          P	                  V4       V^,          pK)  	  K<  	  V# u upi r\   )rv   r(  r  r   r&  )r;   rm   r  current_positionr9  rx   s   &     r=   rF  -_EditArrayContraction.get_contraction_indices  sz    <A$BdBd<e/f<eq<e/f ! ..L!))='*112BC A%  * /
 #" 0gs   A3c                   V ^8  d   QhRR/# )rN   rO   zlist[_IndPos]rD   )rP   s   "r=   rQ   r&    s      M r@   c                	    WP                   8  d   \        R 4      h. p\        V P                  4       FE  w  r4\        VP                  4       F'  w  rVW8X  g   K  VP                  \        W54      4       K)  	  KG  	  V# )z%index value exceeding the index range)r(  rs   r   r  r   r&  r  )r;   r  r  rm   r9  rx   r  s   &&     r=   r  +_EditArrayContraction.get_mapping_for_index  sl    444DEE#%	(););<OA'(<(<=
>$$WQ]3 >  = r@   c                   V ^8  d   QhRR/# )rN   rO   zlist[list[_IndPos]]rD   )rP   s   "r=   rQ   r&    s     # #8K #r@   c                	   \        V P                  4       Uu. uF  p. NK  	  pp\        V P                  4       FI  w  r\        VP                  4       F+  w  rEVf   K  W%,          P                  \        W4      4       K-  	  KK  	  V# u upi r2   )rv   r(  r   r  r   r&  r  )r;   rm   r  r9  rx   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                    V ^8  d   QhRRRR/# )rN   r  r  rO   rD   )rP   s   "r=   rQ   r&  !  s      3 3 r@   c                f    ^ pV P                    F  pWP                  9   g   K  V^,          pK   	  V# )z:
Count the number of arguments that have the given index.
r  r   )r;   r  re  r9  s   &&  r=   count_args_with_index+_EditArrayContraction.count_args_with_index!  s5      ..L,,,1 / r@   c                    V ^8  d   QhRRRR/# )rN   r  r  rO   zlist[_ArgE]rD   )rP   s   "r=   rQ   r&  +  s        r@   c                j    V P                    Uu. uF  q!VP                  9   g   K  VNK  	  ppV# u upi )z1
Get a list of arguments having the given index.
ra  )r;   r  rm   r  s   &&  r=   get_args_with_index)_EditArrayContraction.get_args_with_index+  s6     (,'9'9P'9!aii=OAA'9P
 Qs   00c                	    \        4       pV P                   F<  pTP                  VP                   Uu0 uF  q3f   K  V^ 8  g   K  VkK  	  up4       K>  	  \	        V4      # u upi r2   )r  r  rr  r   r   )r;   ry   r   rm   s   &   r=   number_of_diagonal_indices0_EditArrayContraction.number_of_diagonal_indices2  sR    u%%CKKCKKKKqQUKKL &4y Ls   A"
A"
A"
c                	   . p. p^ pRpV P                    Fj  p. pVP                   FD  pVe$   V^ 8  d   VP                  V4       V^,          pK*  VP                  V4       V^,          pKF  	  VP                  V4       Kl  	  V'       d   \        R V 4       4      MRpV Uu. uF  qxV,
          NK  	  ppW.,           V n        R# u upi )r   Nc              3  J   "   T F  q'       d   \        V4      MRx  K  	  R# 5i)re   Nr   )r  rk   s   & r=   rn   @_EditArrayContraction.track_permutation_start.<locals>.<genexpr>I  s     ?;ac!fr);s   !#r   )r  r   r&  r  r)  )	r;   r   	perm_diagre  rN  r9  permrm   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   #Cc                    V ^8  d   QhRRRR/# )rN   destinationr  from_elementrD   )rP   s   "r=   rQ   r&  M  s     3 35 3 3r@   c                	   V P                   P                  V4      pV P                   P                  V4      pV P                  V,          P                  V P                  V,          4       V P                  P	                  V4       R # r2   )r  r  r)  r   r   )r;   rt  ru  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2r@   c                    V ^8  d   QhRRRR/# rN   r   r  rO   ztyping.Tuple[int, int]rD   )rP   s   "r=   rQ   r&  S  s     / /5 /5K /r@   c                    ^ pV P                    FG  p\        VP                   Uu. uF
  qDe   K  VNK  	  up4      pW18X  d   W"V,           3u # W%,          pKI  	  \        R4      hu upi )z_
Return the range of the free indices of the arg as absolute positions
among all free indices.
argument not foundr  r   r   r   )r;   r   re  r9  rm   number_free_indicess   &&    r=   get_absolute_free_range-_EditArrayContraction.get_absolute_free_rangeS  sn    
  ..L"%,2F2F&T2FQqq2F&T"U"*= ===*G	 /
 -..	 'Us
   A%
A%
c                    V ^8  d   QhRRRR/# r|  rD   )rP   s   "r=   rQ   r&  `  s     / /e /0F /r@   c                    ^ pV P                    F2  p\        VP                  4      pW18X  d   W"V,           3u # W$,          pK4  	  \        R4      h)zK
Return the absolute range of indices for arg, disregarding dummy
indices.
r~  r  )r;   r   re  r9  number_indicess   &&   r=   r  (_EditArrayContraction.get_absolute_range`  sS    
  ..L !5!56N". 888%G	 /
 -..r@   )r)  r  r(  N)rE   rF   rG   rH   r|   r  r  r  r:  rB  r  rF  r  r]  rb  rf  r~   ri  rq  ry  r  r  rJ   rD   r@   r=   r  r  Z  sq    7Fr46XgAF##  <(3// /r@   r  c                   \        V \        \        34      '       d   ^# \        V \        4      '       d   \	        V P
                  4      # \        V \        4      '       d   V P                  4       # \        V \        4      '       d   V P                  # \        V \        4      '       d   V P
                  pVf   R# \	        V4      # \        V R4      '       d   \	        V P
                  4      # ^ # )rN   r0   r   )r3   r   r#   r   r   r0   r    r  r!   r"   r   r8  s   & r=   r   r   n  s    $]344$-..4::$	""yy{$  yy$$$

=Iu:tW4::r@   c                d    \        V \        4      '       d   V P                  4       # \        V 4      # r2   )r3   r   r   r   r   s   &r=   r   r     s&    $-..||~D>r@   c                ^    \        V \        4      '       d   V P                  # \        V 4      .# r2   )r3   r   r   r   r  s   &r=   r  r    s'    $-..}}r@   c                B    \        V R 4      '       d   V P                  # R# )r0   rD   )r   r0   r  s   &r=   r   r     s    tWzzIr@   c                R    \        V \        4      '       d   V P                  4       # V # r2   )r3   r   r  r  s   &r=   r  r    s#    $$$$$&&r@   c                     \        V R R/VB # r   T)r   r   r   s   *,r=   r   r     s    tA$A&AAr@   c                $    \        V .VO5R R/VB # r  )r   )r   r  r   s   &*,r=   r  r    s    DT#6TTTVTTr@   c                $    \        V .VO5R R/VB # r  )r  )r   r  r   s   &*,r=   r  r    s    N 0NtNvNNr@   c                    \        W3R R/VB # r  )r   )r   r   r   s   &&,r=   r   r     s    tFtFvFFr@   c                     \        V R R/VB # r  )r  r  s   *,r=   r  r    s    T7777r@   c                    \        W4      # r2   )r7   )r   r   s   &&r=   rB   rB     s    &&r@   )a
__future__r   collections.abcr4   r   r   r   	functoolsr   rt   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)   r0  r+    sympy.combinatorics.permutationsr,   sympy.core.sympifyr-   r/   rL   r7   r   r   r   r   r  r   r  r,  r   r  r  r  r  r   r   r  r   r  r   r  r  r   r  rB   rD   r@   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'r@   