o
    Ei                     @   s   d dl Z d dlZd dlmZ d dlmZmZ d dlmZ	 e j
ddddZddd	ed
e jdede	jdeeee	jf  f
ddZddd	ed
e jdede	jdef
ddZe jjddd	ed
e jdede	jddf
ddZdS )    N)defaultdict)MappingOptionalTcsec             
   O   s  t jj| |\} }t jj| }t|t|kr'tdt| dt| zt jj	dd t
||D dd | D  |d}W n t jjjt jjjt jjjfyX   Y d S w tt}|D ].}| D ]'}t|t jjjr|jd}||d  td	d
 |dd  D |jf qeq_i }	| D ]1\}
}tjdd |D ddd }tj|dd}|D ]\}}|||< q|jdkrt|}||	|
< q|	S )N	Expected  tensors, got c                 S      g | ]\}}t j||qS  einxexprEquation.0r   tensor_shaper
   r
   J/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/einx/op/solve.py
<listcomp>   s    z_solve.<locals>.<listcomp>c                 S   4   g | ]\}}t jj|t|d tjf dddqS .N)depth1depth2r   r   r   npasarraynewaxisr   kvr
   r
   r   r          "r   .r   c                 s   s    | ]}t |V  qd S )N)int)r   tr
   r
   r   	<genexpr>*   s    z_solve.<locals>.<genexpr>   c                 S   s   g | ]\}}|qS r
   r
   )r   coordvaluer
   r
   r   r   .   s    )axisint32)dtyper
   ) r   oputil!_clean_description_and_parametersr   stage1
parse_argslen
ValueErrorsolvezipitemsstage2SolveDepthExceptionSolveExpansionExceptionstage3SolveValueExceptionr   listall
isinstanceAxisnamesplitappendtupler&   r   amaxzerosshaper!   )descriptionr   tensor_shapes
parametersexprsvaluesrootr   tokensvalues2r=   xsrC   r&   r%   r   r
   r
   r   _solve   sT   

.


rM   FrD   tensorsr   rF   returnc                O   s$   t | gdd |D R d|i|S )a  Solve for the axis values of the given expressions and tensors.

    Args:
        description: Description string for the tensors in einx notation.
        tensors: Input tensors or tensor factories matching the description string.
        cse: Whether to apply common subexpression elimination to the expressions.
            Defaults to False.
        **parameters: Additional parameters that specify values for single axes, e.g. ``a=4``.

    Returns:
        A mapping from axis name to axis value, or ``None`` if no solution was found.

    Examples:
        >>> x = np.zeros((10, 5))
        >>> einx.solve("a b", x)
        {'a': 10, 'b': 5}
    c                 S      g | ]}t j|qS r
   r   tracer	get_shaper   tensorr
   r
   r   r   N       zsolve.<locals>.<listcomp>r   )rM   rD   r   rN   rF   r
   r
   r   r1   9   s   r1   c                O   s   t | g|R d|i|duS )a  Check whether the given expressions and tensors match.

    Args:
        description: Description string for the tensors in einx notation.
        tensors: Input tensors or tensor factories matching the description string.
        cse: Whether to apply common subexpression elimination to the expressions.
            Defaults to False.
        **parameters: Additional parameters that specify values for single axes, e.g. ``a=4``.

    Returns:
        True if the expressions and tensors match, False otherwise.

    Examples:
        >>> x = np.zeros((10, 5))
        >>> einx.matches("a b", x)
        True
        >>> einx.matches("a b c", x)
        False
    r   N)r1   rW   r
   r
   r   matchesR   s   rX   c                O   s   t jj| |\} }t jj| }t|t|kr'tdt| dt| dd |D }t jj	dd t
||D dd | D  |d dS )	a  Check whether the given expressions and tensors match and raise an exception if they don't.

    Args:
        description: Description string for the tensors in einx notation.
        tensors: Input tensors or tensor factories matching the description string.
        cse: Whether to apply common subexpression elimination to the expressions.
            Defaults to False.
        **parameters: Additional parameters that specify values for single axes, e.g. ``a=4``.
    r   r   c                 S   rP   r
   rQ   rT   r
   r
   r   r      rV   zcheck.<locals>.<listcomp>c                 S   r	   r
   r   r   r
   r
   r   r      s    c                 S   r   r   r   r   r
   r
   r   r      r   r   N)r   r*   r+   r,   r   r-   r.   r/   r0   r1   r2   r3   )rD   r   rN   rF   rG   rE   r
   r
   r   checkk   s   
rY   )r   numpyr   collectionsr   typingr   r   numpy.typingnpt	lru_cacherM   strTensorbool	ArrayLiker1   rX   traceback_utilfilterrY   r
   r
   r
   r   <module>   sX    1

