o
    *ήce                     @   s   d dl Zd dlZd dlmZ d dlmZmZmZm	Z	 d dl
mZmZmZmZ d dlmZmZmZmZmZmZmZ d dlmZ dd ZG d	d
 d
ZdS )    N)
accumulate)MulSumDummyAdd)PermuteDimsArrayAddArrayElementwiseApplyFuncReshape)ArrayTensorProductget_rankArrayContractionArrayDiagonal	get_shape_get_array_element_or_slice
_ArrayExpr)_apply_permutation_to_listc                 C   s   t  | |S N)_ConvertArrayToIndexed
do_convert)exprindices r   [/tmp/pip-target-vg8gfxp4/lib/python/sympy/tensor/array/expressions/conv_array_to_indexed.pyconvert_array_to_indexed   s   r   c                   @   s   e Zd Zdd Zdd ZdS )r   c                 C   s
   d| _ d S )Nr   )count_dummiesselfr   r   r   __init__   s   
z_ConvertArrayToIndexed.__init__c                    sl  t |tr3ttdgdd |jD    fddtt|jD }tfddt	|j|D S t |t
rdd tt|jD }g }t|j}|jD ]*}tdj } jd	7  _||d  }	||d|	d	 f |D ]}
|||
< qqqNd}tt|D ]}
||
 d u r| ||
< |d	7 }q|j|}t|g|R  S t |trd
d tt|jD }||jtttt|}t	|D ]\}
}t |
tjjr|
D ]}|||< qq|||
< qɈ|j|}|S t |trt|j}|j|S t |trtfdd|jD S t |tr| t!S t |t"r/|#|jS t |t$r|j%}t|j}ttd	gtt&| t'j( tdd t	t& D }dd |D }d	}t)t&|D ];\}
}|d	kr|
t|d	 kr|||
< n|| ||
< n|
t|d	 kr|| ||
< n|| | ||
< ||9 }qj|*  |j|S t+|S )Nr   c                 S   s   g | ]}t |qS r   )r   .0argr   r   r   
<listcomp>   s    z5_ConvertArrayToIndexed.do_convert.<locals>.<listcomp>c                    s$   g | ]} |  |d    qS )   r   r    i)cumulr   r   r   r"      s   $ c                 3   s     | ]\}}  ||V  qd S r   r   )r    r!   indr   r   r   	<genexpr>   s    z4_ConvertArrayToIndexed.do_convert.<locals>.<genexpr>c                 S      g | ]}d qS r   r   r$   r   r   r   r"          dr#   c                 S   r*   r   r   r$   r   r   r   r"   -   r+   c                 3   s    | ]	} | V  qd S r   r'   r   )r   r   r   r   r)   ;       c                 s   s    | ]	\}}|| V  qd S r   r   )r    r%   sr   r   r   r)   D   r-   c                 S   r*   r   r   )r    _r   r   r   r"   E   r+   ),
isinstancer   listr   argsrangelenr   fromiterzipr   r   r   r   contraction_indicesr   r   appendr   r   r   _push_indices_downdiagonal_indicescollectionsabcIterabler   r   permutationr   r   r   __getitem__tupler	   functionr
   shapereversedoperatormul	enumeratereverser   )r   r   r   indices_grpnew_indiceslimitsbottom_shapecontraction_index_grpr,   dimr%   jnewexprind_posindexpermuted_indicesshape_up
shape_down	one_indexdest_indicescer   )r&   r   r   r   r      s~   
 




 






z!_ConvertArrayToIndexed.do_convertN)__name__
__module____qualname__r   r   r   r   r   r   r      s    r   )collections.abcr;   rD   	itertoolsr   sympyr   r   r   r   sympy.tensor.array.expressionsr   r   r	   r
   0sympy.tensor.array.expressions.array_expressionsr   r   r   r   r   r   r   $sympy.tensor.array.expressions.utilsr   r   r   r   r   r   r   <module>   s    $