U
    9%ec                     @   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eed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   c/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/sympy/tensor/array/expressions/utils.py_get_mapping_from_subranks	   s    r   c           
         sv   t |  fdd|D }tt}|D ]B}t|dkr&|\\}}\}}	||	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   "   s     z-_sort_contraction_indices.<locals>.<listcomp>c                 S   s   t | S N)minxr   r   r   <lambda>#       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                    s   g | ]}| kr|qS r   r   r   axes_contractionr   r   r   0   s      z)_get_diagonal_indices.<locals>.<listcomp>c                    s
     | S r   )indexr   )flattened_indicesr   r   r!   2   r"   z'_get_diagonal_indices.<locals>.<lambda>r#   c                    s   g | ]}t  | qS r   )tupler   r+   r   r   r   3   s     )
r   listr   
isinstanceintr   appenditemsr%   r/   )r.   r   indZret_indicesZdiag_indicesZdiagonal_indicesr   )r,   r.   r   _get_diagonal_indices'   s    r6   c                 C   sT   t | D ]6\}}||kr |  S t|ttfr||kr|  S qtd|| f d S )Nz%s not found in %s)r   r1   set	frozenset
IndexError)Z
subindicesr5   r   Zsindr   r   r   _get_argindex9   s    
r:   c                    sT   t |tttfr&t fdd|D S t |trHt fdd|D S  |S d S )Nc                 3   s   | ]}t  |V  qd S r   $_apply_recursively_over_nested_listsr   funcr   r   	<genexpr>D   s     z7_apply_recursively_over_nested_lists.<locals>.<genexpr>c                 3   s   | ]}t  |V  qd S r   r;   r   r=   r   r   r?   F   s     )r1   r/   r0   r   Zfromiter)r>   Zarrr   r=   r   r<   B   s
    
r<   c                    s   ddid}d}|t  k rrd}|| t  k rT | |  ||  krJqT|d7 }q ||7 }| | < ||7 }q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   |  krd S | |  S d S r   r   r   )flattened_contraction_indicesr>   r   r   	transform]   s    z=_build_push_indices_up_func_transformation.<locals>.transform)r   r   keys)rF   r   Z
cumulativer   rG   r   )rF   r>   rC   rD   r   *_build_push_indices_up_func_transformationK   s     

rI   c                    s2    d d } fddt |D fdd}|S )Nr   c                    s   g | ]}| kr|qS r   r   r   )rF   r   r   r   i   s      z@_build_push_indices_down_func_transformation.<locals>.<listcomp>c                    s0   | t  k r |  S |  d  t   d S d S )NrJ   r   r'   rE   )rD   r   r   rG   k   s    z?_build_push_indices_down_func_transformation.<locals>.transform)r   )rF   NrG   r   )rF   rD   r   ,_build_push_indices_down_func_transformationf   s    rL   )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   )rM   rN   Znew_listr   er   r   r   _apply_permutation_to_listt   s    rQ   )r@   collectionsr   Zsympy.combinatoricsr   Zsympy.core.containersr   Zsympy.core.numbersr   r   r   r&   r6   r:   r<   rI   rL   r0   rQ   r   r   r   r   <module>   s   
		