o
    gij                     @   sL   d dl mZ d dl mZ d dl mZ ddlZdd Zdd	 Z	dddZdS )   )stage1)stage2)stage3    Nc                 C   s>   |dkrdS | dkrdS | dkrdS | dkrdS | d  dS )	Nr    z1st r   z2nd    z3rd zth  )idxnr   r   Y/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/einx/_src/namedtensor/solve.py_idx_to_ordinal   s   r   c                 C   s\   | d u rdS t | tjr| jdkrt|  S tt|  S t | tr*tt| S t| S )NNoner   )
isinstancenpndarrayndimstrtolisttuplelist)lr   r   r   _arr_to_str   s   

r   TFc
                    sv  |d u ri }t  t |krtdt   dt | d| D ]`\}
}t|
ts6td|
 dt|
 zt|}W n tyV } ztd| dt| |d }~ww t|t	r`|dkst|t
ri|g kst|jtjstd| d	t| d
|j q!dd t
 t
 D fdd| D }|rtd t |ddD ]\}}td| dt|  qD ]	}td|  q fddtt |ddD fddtD  dd | D  }tj||d\}}|rtd t||ddD ]\}}td| d|  q|rJtj|| ||d}|d t | |t |d  }}|rJtd t||ddD ]\}}td| d|  q:dd t|||ddD }|	d urz|d t    |t  t  t   ||	  tj||d\}}|rtd t||ddD ]\}}td| d|  q|d t    |t  t  t    fS ) Nz'The number of input expressions (found z0) must match the number of input tensors (found )zBAxis names passed as constraints must be strings, but found value z	 of type zaValues passed for axis names as constraints must be convertible to numpy arrays, but found value r   zXValues passed for axis names as constraints must have an integral type, but found value z with type z and dtype c                 S   s,   h | ]}|  D ]}t|tjr|jqqS r   )nodesr   r   Axisname).0exprr   r   r   	<setcomp>7   s   , zsolve.<locals>.<setcomp>c                    s   i | ]\}}| v r||qS r   r   r   kv)used_axisnamesr   r   
<dictcomp>8   s    zsolve.<locals>.<dictcomp>zStage1:F)strictz  IN  z = z  OUT c                    sn   g | ]3\}\}}t j||t|t  d | d|dur1t|t  dddd |D  dnddqS )	zinput expression ("")Nzinput tensor (with shape (z, c                 S   s   g | ]}t |qS r   )r   )r   xr   r   r   
<listcomp>H   s    z$solve.<locals>.<listcomp>.<listcomp>z)))desc1desc2)r   Equationr   lenjoin)r   ir   tensor_shape)exprs_inr   r   r'   C   s    
	*zsolve.<locals>.<listcomp>c                    s4   g | ]\}}t j|t|t  d | ddqS )zoutput expression ("r%   )r(   )r   r*   r   r+   )r   r-   r   )	exprs_outr   r   r'   N   s   4 c                 S   sT   g | ]&\}}t j|t|d tjf dddd| dtt| ddqS ).int32Nzaxis zconstraint (r   )depth1depth2r(   r)   )r   r*   r   asarraynewaxisastyper   r   r   r   r   r'   O   s    	)
invocationzStage2:z  )
cse_concatcse_in_bracketszStage2 (after CSE):c                 S   s&   g | ]\}}}t |||j|jqS r   )r   r*   r(   r)   )r   eqexpr1expr2r   r   r   r'   n   s   & zStage3:)r+   
ValueErroritemsr   r   typer   r4   	Exceptionr   r   
issubdtypedtypeintegerprintzipr   	enumerater   solvecseextendr   )r/   r0   tensor_shapesr7   
parametersrH   r8   r9   verboseequations_stage3r    r!   r&   eexpr_inr.   expr_out	equationsexprs1exprs2r;   r<   exprsr   )r/   r0   r"   r   rG   "   sz   
4
		"
rG   )NTFFFN)	r   r   r   r   numpyr   r   r   rG   r   r   r   r   <module>   s    