o
    jgc                     @   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irankj r   \/var/www/html/zoom/venv/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	   contraction_indicescontraction_tuplesdlinkslinksarg1pos1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)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   r4   r   r   r   3   r$   )
r   listr   
isinstanceintr   appenditemsr,   r8   )r7   r   indret_indicesdiag_indicesdiagonal_indicesr   )r5   r7   r   _get_diagonal_indices'   s   rB   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   r:   set	frozenset
IndexError)
subindicesr>   r   sindr   r   r   _get_argindex9   s   rH   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   rI   r%   rJ   r   rL   r   r   rN   F   rO   )r:   r8   r9   r   fromiter)rM   arrr   rL   r   rK   B   s
   
rK   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   rM   Z   s   z8_build_push_indices_up_func_transformation.<locals>.funcc                    s   |  v rd S | |  S r%   r   r   )flattened_contraction_indicesrM   r   r   	transform]   s   z=_build_push_indices_up_func_transformation.<locals>.transform)r   r#   keys)rX   r   
cumulativer   rY   r   )rX   rM   rU   rV   r   *_build_push_indices_up_func_transformationK   s$   	r\   c                    s2    d d } fddt |D fdd}|S )Nr   c                    r3   r   r   r   )rX   r   r   r   i   r$   z@_build_push_indices_down_func_transformation.<locals>.<listcomp>c                    s,   | t  k r
 |  S |  d  t   d S )Nr]   r   r/   rW   )rV   r   r   rY   k   s   z?_build_push_indices_down_func_transformation.<locals>.transform)r   )rX   NrY   r   )rX   rV   r   ,_build_push_indices_down_func_transformationf   s   r_   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   )r`   ra   new_listr   er   r   r   _apply_permutation_to_listt   s   re   )rR   collectionsr   sympy.combinatoricsr   sympy.core.containersr   sympy.core.numbersr   r   r"   r.   rB   rH   rK   r\   r_   r9   re   r   r   r   r   <module>   s    
		