
    gc                     ~    d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d Z	d Z
d Zd	 Zd
 Zd Zd Zd ZdedefdZdS )    N)defaultdict)Permutation)Tuple)Integerc                 t    i }d}t          |           D ]#\  }}t          |          D ]}||f||<   |dz  }$|S )Nr      )	enumeraterange)subranksmappingcounterirankjs         `/var/www/html/ai-engine/env/lib/python3.11/site-packages/sympy/tensor/array/expressions/utils.py_get_mapping_from_subranksr   	   sa    GGX&&  4t 	 	A !1vGGqLGG	 N    c                   
 t          |          

fd|D             }t          t                    }|D ];}t          |          dk    r&|\  \  }}\  }}	||	f||         |<   ||f||         |	<   ;<| t          |          fS )Nc                 ,    g | ]}fd |D             S )c                      g | ]
}|         S  r   ).0r   r   s     r   
<listcomp>z5_get_contraction_links.<locals>.<listcomp>.<listcomp>   s    111!71:111r   r   )r   r   r   s     r   r   z*_get_contraction_links.<locals>.<listcomp>   s.    OOOa1111q111OOOr      )r   r   dictlen)argsr   contraction_indicescontraction_tuplesdlinkslinksarg1pos1arg2pos2r   s             @r   _get_contraction_linksr&      s    (22GOOOO;NOOOF#  u::??).&LT4,4"&F4L"&F4L	  fr   c                 L    d | D             } |                      d            | S )Nc                 :    g | ]}t          t          |           S r   )r   sortedr   r   s     r   r   z-_sort_contraction_indices.<locals>.<listcomp>"   s#    BBBQufQii(BBBr   c                      t          |           S N)min)xs    r   <lambda>z+_sort_contraction_indices.<locals>.<lambda>#   s    s1vv r   key)sort)pairing_indicess    r   _sort_contraction_indicesr4   !   s6    BB/BBBO--...r   c                     t          t                    t                     D ]=\  }}t          |t          t
          f          r"|                             |           >d                                 D             fd D             }t                    }|                     fd           fd|D             }||z  }t          |          }||fS )Nc                 @    i | ]\  }}t          |          d k    ||S )r   r   )r   kvs      r   
<dictcomp>z)_get_diagonal_indices.<locals>.<dictcomp>.   s)    PPPASVVaZZ1ZZZr   c                     g | ]}|v|	S r   r   r   r   axes_contractions     r   r   z)_get_diagonal_indices.<locals>.<listcomp>0   s$    MMM1<L3L3L13L3L3Lr   c                 .                         |           S r,   )index)r.   flattened_indicess    r   r/   z'_get_diagonal_indices.<locals>.<lambda>2   s    $5$;$;A$>$> r   r0   c                 :    g | ]}t          |                   S r   )tupler<   s     r   r   z)_get_diagonal_indices.<locals>.<listcomp>3   s'    IIIq.q122IIIr   )
r   listr	   
isinstanceintr   appenditemsr2   rB   )r@   r   indret_indicesdiag_indicesdiagonal_indicesr=   s   `     @r   _get_diagonal_indicesrL   '   s	   "4((-.. ( (3cC>** 	$$Q''''PP)9)?)?)A)APPPMMMM/MMMK())L>>>>???IIIILIII<K$$K[((r   c                     t          |           D ]3\  }}||k    r|c S t          |t          t          f          r||v r|c S 4t	          |d|           )Nz not found in )r	   rD   set	frozenset
IndexError)
subindicesrH   r   sinds       r   _get_argindexrS   9   sq    Z((  4$;;HHHdS),-- 	#++HHH
SSS**=
>
>>r   c                      t          |t          t          t          f          rt           fd|D                       S t          |t                    r t          j         fd|D                       S   |          S )Nc              3   8   K   | ]}t          |          V  d S r,   $_apply_recursively_over_nested_listsr   r   funcs     r   	<genexpr>z7_apply_recursively_over_nested_lists.<locals>.<genexpr>D   s.      PPq9$BBPPPPPPr   c              3   8   K   | ]}t          |          V  d S r,   rV   rX   s     r   rZ   z7_apply_recursively_over_nested_lists.<locals>.<genexpr>F   s/      YYPQB4KKYYYYYYr   )rD   rB   rC   r   fromiter)rY   arrs   ` r   rW   rW   B   s    #tU+,, PPPPCPPPPPP	C		 ~YYYYUXYYYYYYtCyyr   c                     ddid}d}|t                     k     rtd}||z   t                     k     r4 |         |z    ||z            k    rn|dz  }||z   t                     k     4||z  }| |         <   ||z  }|t                     k     tt                                                    fd fd}|S )Nr   r   c                 L    t          j        |           dz
                    S )Nr   )bisectbisect_right)idx
shift_keysshiftss    r   rY   z8_build_push_indices_up_func_transformation.<locals>.funcZ   s%    j!4Z!E!Ea!GHIIr   c                 ,    | v rd S |  |           z
  S r,   r   )r   flattened_contraction_indicesrY   s    r   	transformz=_build_push_indices_up_func_transformation.<locals>.transform]   s&    ---4ttAww;r   )r   r)   keys)rf   r   
cumulativer   rg   rY   rc   rd   s   `    @@@r   *_build_push_indices_up_func_transformationrj   K   s-   VF	AJ
c/00
0
0cC56666,Q/!37TUVWXUX7YYYFA cC56666 	a
3=,Q/0	Q c/00
0
0 &&JJ J J J J J      r   c                 `      d         dz   } fdt          |          D             fd}|S )Nr   c                     g | ]}|v|	S r   r   )r   r   rf   s     r   r   z@_build_push_indices_down_func_transformation.<locals>.<listcomp>i   s$    LLLAQ.K%K%Ka%K%K%Kr   c                 v    | t                    k     r|          S | d         z   t                    z
  dz   S )Nrl   r   r7   )r   rd   s    r   rg   z?_build_push_indices_down_func_transformation.<locals>.transformk   s:    s6{{??!9vbz>CKK/!33r   )r
   )rf   Nrg   rd   s   `  @r   ,_build_push_indices_down_func_transformationrp   f   sS    %b)!+ALLLLqLLLF4 4 4 4 4 r   permtarget_listc                     d t          | j                  D             }t          |          D ]\  }}|| | |          <   |S )z<
    Permute a list according to the given permutation.
    c                     g | ]}d S r,   r   r*   s     r   r   z._apply_permutation_to_list.<locals>.<listcomp>x   s    //////r   )r
   sizer	   )rq   rr   new_listr   es        r   _apply_permutation_to_listrx   t   sV     0/eDI..///H+&&  1aOr   )r`   collectionsr   sympy.combinatoricsr   sympy.core.containersr   sympy.core.numbersr   r   r&   r4   rL   rS   rW   rj   rp   rC   rx   r   r   r   <module>r}      s     # # # # # # + + + + + + ' ' ' ' ' ' & & & & & &      ) ) )$? ? ?    6  [ t      r   