o
    GZhc                     @   s   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d Z	dd	 Z
d
d Zdd Zdd Zdd Zdd Zdd ZdedefddZdS )    N)defaultdict)Permutation)Tuple)Integerc                 C   s@   i }d}t | D ]\}}t|D ]}||f||< |d7 }qq|S )Nr      )	enumeraterange)subranksmappingcounteriZrankj r   S/var/www/auris/lib/python3.10/site-packages/sympy/tensor/array/expressions/utils.py_get_mapping_from_subranks	   s   
r   c           
         sv   t |  fdd|D }tt}|D ]!}t|dkr4|\\}}\}}	||	f|| |< ||f|| |	< qq| t|fS )Nc                    s   g | ]} fd d|D qS )c                    s   g | ]} | qS r   r   ).0r   r
   r   r   
<listcomp>   s    z5_get_contraction_links.<locals>.<listcomp>.<listcomp>r   r   r   r   r   r   r      s    z*_get_contraction_links.<locals>.<listcomp>   )r   r   dictlen)
argsr	   Zcontraction_indicesZcontraction_tuplesZdlinkslinksZarg1Zpos1Zarg2pos2r   r   r   _get_contraction_links   s   r   c                 C   s"   dd | D } | j dd d | S )Nc                 S   s   g | ]}t t| qS r   )r   sortedr   r   r   r   r   "       z-_sort_contraction_indices.<locals>.<listcomp>c                 S   s   t | S N)minxr   r   r   <lambda>#   s    z+_sort_contraction_indices.<locals>.<lambda>key)sort)Zpairing_indicesr   r   r   _sort_contraction_indices!   s   r&   c                    s   t t tD ]\}}t|ttfrq | | qdd   D   fddD }t }|jfddd  fdd|D }||7 }t	|}||fS )	Nc                 S   s"   i | ]\}}t |d kr||qS )r   r   )r   kvr   r   r   
<dictcomp>.   s   " z)_get_diagonal_indices.<locals>.<dictcomp>c                       g | ]}| vr|qS r   r   r   axes_contractionr   r   r   0   r   z)_get_diagonal_indices.<locals>.<listcomp>c                    s
     | S r   )indexr    )flattened_indicesr   r   r"   2   s   
 z'_get_diagonal_indices.<locals>.<lambda>r#   c                    s   g | ]}t  | qS r   )tupler   r,   r   r   r   3   r   )
r   listr   
isinstanceintr   appenditemsr%   r0   )r/   r   indZret_indicesZdiag_indicesZdiagonal_indicesr   )r-   r/   r   _get_diagonal_indices'   s   r7   c                 C   sP   t | D ]\}}||kr|  S t|ttfr||v r|  S qtd|| f )Nz%s not found in %s)r   r2   set	frozenset
IndexError)Z
subindicesr6   r   Zsindr   r   r   _get_argindex9   s   r;   c                    sP   t |tttfrt fdd|D S t |tr$t fdd|D S  |S )Nc                 3       | ]}t  |V  qd S r   $_apply_recursively_over_nested_listsr   funcr   r   	<genexpr>D       z7_apply_recursively_over_nested_lists.<locals>.<genexpr>c                 3   r<   r   r=   r   r?   r   r   rA   F   rB   )r2   r0   r1   r   Zfromiter)r@   Zarrr   r?   r   r>   B   s
   
r>   c                    s   ddid}d}|t  k rEd}|| t  k r1 | |  ||  kr%n|d7 }|| t  k s||7 }| | < ||7 }|t  k st fdd fdd}|S )Nr   r   c                    s    t  | d   S )Nr   )bisectbisect_right)idx)
shift_keysshiftsr   r   r@   Z   s   z8_build_push_indices_up_func_transformation.<locals>.funcc                    s   |  v rd S | |  S r   r   r   )flattened_contraction_indicesr@   r   r   	transform]   s   z=_build_push_indices_up_func_transformation.<locals>.transform)r   r   keys)rI   r   Z
cumulativer   rJ   r   )rI   r@   rF   rG   r   *_build_push_indices_up_func_transformationK   s$   	rL   c                    s2    d d } fddt |D fdd}|S )Nr   c                    r+   r   r   r   )rI   r   r   r   i   r   z@_build_push_indices_down_func_transformation.<locals>.<listcomp>c                    s,   | t  k r
 |  S |  d  t   d S )NrM   r   r'   rH   )rG   r   r   rJ   k   s   z?_build_push_indices_down_func_transformation.<locals>.transform)r   )rI   NrJ   r   )rI   rG   r   ,_build_push_indices_down_func_transformationf   s   rO   permtarget_listc                 C   s6   dd t | jD }t|D ]
\}}||| |< q|S )z<
    Permute a list according to the given permutation.
    c                 S   s   g | ]}d qS r   r   r   r   r   r   r   x   s    z._apply_permutation_to_list.<locals>.<listcomp>)r   sizer   )rP   rQ   Znew_listr   er   r   r   _apply_permutation_to_listt   s   rT   )rC   collectionsr   Zsympy.combinatoricsr   Zsympy.core.containersr   Zsympy.core.numbersr   r   r   r&   r7   r;   r>   rL   rO   r1   rT   r   r   r   r   <module>   s    
		